﻿// whitespace characters
var whitespace = " \t\n\r";

// Check whether string s is empty.

function isEmpty(s) {
  
  return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

var validChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

function isAlphanumeric (s) {
  var tmp = s.toUpperCase();
  for (i = 0; i < tmp.length; i++) {   
    var c = tmp.charAt(i);
    if (validChars.indexOf(c) == -1) {
      return false;
    }
  }
  return true;
}

function isNumeric (s) {
  var i;
 
  for(i = 0; i < s.length; i++) {
  if(isNaN(parseInt(s.substring(i, i + 1)))) {
    return false;
  }
}
return true;
}
function extractNumber(obj, decimalPlaces, allowNegative)
{
	var temp = obj.value;
	
	// avoid changing things if already formatted correctly
	var reg0Str = '[0-9]*';
	if (decimalPlaces > 0) {
		reg0Str += '\\.?[0-9]{0,' + decimalPlaces + '}';
	} else if (decimalPlaces < 0) {
		reg0Str += '\\.?[0-9]*';
	}
	reg0Str = allowNegative ? '^-?' + reg0Str : '^' + reg0Str;
	reg0Str = reg0Str + '$';
	var reg0 = new RegExp(reg0Str);
	if (reg0.test(temp)) return true;

	// first replace all non numbers
	var reg1Str = '[^0-9' + (decimalPlaces != 0 ? '.' : '') + (allowNegative ? '-' : '') + ']';
	var reg1 = new RegExp(reg1Str, 'g');
	temp = temp.replace(reg1, '');

	if (allowNegative) {
		// replace extra negative
		var hasNegative = temp.length > 0 && temp.charAt(0) == '-';
		var reg2 = /-/g;
		temp = temp.replace(reg2, '');
		if (hasNegative) temp = '-' + temp;
	}
	
	if (decimalPlaces != 0) {
		var reg3 = /\./g;
		var reg3Array = reg3.exec(temp);
		if (reg3Array != null) {
			// keep only first occurrence of .
			//  and the number of places specified by decimalPlaces or the entire string if decimalPlaces < 0
			var reg3Right = temp.substring(reg3Array.index + reg3Array[0].length);
			reg3Right = reg3Right.replace(reg3, '');
			reg3Right = decimalPlaces > 0 ? reg3Right.substring(0, decimalPlaces) : reg3Right;
			temp = temp.substring(0,reg3Array.index) + '.' + reg3Right;
		}
	}
	
	obj.value = temp;
}

function blockNonNumbers(obj, e, allowDecimal, allowNegative)
{
	var key;
	var isCtrl = false;
	var keychar;
	var reg;
		
	if(window.event) {
		key = e.keyCode;
		isCtrl = window.event.ctrlKey
	}
	else if(e.which) {
		key = e.which;
		isCtrl = e.ctrlKey;
	}
	
	if (isNaN(key)) return true;
	
	keychar = String.fromCharCode(key);
	
	// check for backspace or delete, or if Ctrl was pressed
	if (key == 8 || isCtrl)
	{
		return true;
	}

	reg = /\d/;
	var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
	var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
	
	return isFirstN || isFirstD || reg.test(keychar);
}

function validateForm(form)
{
//  var objfrm = document.forms.genreg
 
  if (isWhitespace(document.MortgageCalc.txtLoanAmount.value))
  {
    alert("Loan Amount is missing. Please enter information in all the required fields.");
    document.MortgageCalc.txtLoanAmount.focus();
    return false;
  }
  else
  {
	  if (document.MortgageCalc.txtLoanAmount.value.search(/[^0-9|^'.'|^-]/) != -1)
	  {
		alert("The Loan Amount can not contain any letters."); 
	    document.MortgageCalc.txtLoanAmount.focus();
		return false;
	   }
   }
  if (isWhitespace(document.MortgageCalc.txtYears.value)) 
  {
    alert("Term of the loan is missing. Please provide the Term for your loan in years.");
    document.MortgageCalc.txtYears.focus();
    return false;
  }
  else
  {
	  if (document.MortgageCalc.txtYears.value.search(/[^0-9|^-]/) != -1)
	  {
		alert("The Term of the Loan can not contain any letters."); 
	    document.MortgageCalc.txtYears.focus();
		return false;
	   }
   }
  if (isWhitespace(document.MortgageCalc.txtAnnualTax.value)) 
  {
    alert("Please provide amount of Annual Tax.  If you don't have this infomation, Enter 0.");
    document.MortgageCalc.txtAnnualTax.focus();
    return false;
  }
  else
  {
	  if (document.MortgageCalc.txtAnnualTax.value.search(/[^0-9|^-]/) != -1)
	  {
		alert("The Tax Amount can not contain any letters."); 
	    document.MortgageCalc.txtAnnualTax.focus();
		return false;
	   }
   }
  if (isWhitespace(document.MortgageCalc.txtInsurance.value))
  {
    alert("Insurance amount is missing. If you don't have this infomation, Enter 0.");
    document.MortgageCalc.txtInsurance.focus();
    return false;
  }
  else
  {
	  if (document.MortgageCalc.txtInsurance.value.search(/[^0-9|^-]/) != -1)
	  {
		alert("The Insurance Amount can not contain any letters."); 
	    document.MortgageCalc.txtInsurance.focus();
		return false;
	   }
   }
return true;
}