﻿// whitespace characters
var whitespace = " \t\n\r";

// Check whether string s is empty.

var reOneOrMoreDigits = /[\d+]/;
var reNoDigits = /[^\d]/gi;

function doCheck()
{
   var i,exceptions=[8,46,37,39,13,9];   // backspace, delete, arrowleft & right, enter, tab
   var isException=false;
   var isDot=(190==event.keyCode);       // dot
   var k=String.fromCharCode(event.keyCode);

   for(i=0;i<exceptions.length;i++)
      if(exceptions[i]==event.keyCode)
	     isException=true;

   if(isNaN(k) && (!isException) && (!isDot))
      event.returnValue=false;
   else{
      var p=new String(currency.value+k).indexOf(".");
      if((p<currency.value.length-2 || isDot) && p>-1 && (!isException))
         event.returnValue=false;
      else if(currency.value.length>=15 && (!isException))
         event.returnValue=false;
   }
}


function doMask(textBox) {
	var keyCode = event.which ? event.which : event.keyCode;
    // enter, backspace, delete and tab keys are allowed thru
	if(keyCode == 13 || keyCode == 8 || keyCode == 9 || keyCode == 46)
		return true;
	// get character from keyCode....dealing with the "Numeric KeyPad" 
    // keyCodes so that it can be used
	var keyCharacter = cleanKeyCode(keyCode);
	// grab the textBox value and the mask
	var val = textBox.value;
	var mask = textBox.mask;
	// simple Regex to check if key is a digit
	if(reOneOrMoreDigits.test(keyCharacter) == false)
		return false;
	// get value minus any masking by removing all non-numerics
	val = val.replace(reNoDigits,'');			
	// add current keystroke
	val += keyCharacter;
	// mask it...val holds the existing TextBox.value + the current keystroke
	textBox.value = val.maskValue(mask);
	setCaretAtEnd(textBox);
return false;
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
	if (s.indexOf(":",0) != -1) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


// puts starting chars in field

function onFocusMask(textBox) {
	var val = textBox.value;
    var mask = textBox.mask;
	if(val.length == 0 || val == null) {
		var i = mask.indexOf('#');
		textBox.value = mask.substring(0,i);
	}
	setCaretAtEnd(textBox);
	// set just in case.
	textBox.maxlength = mask.length;
}
// blank field if no digits entered

function onBlurMask(textBox) {
	var val = textBox.value;
	// if no digits....nada entered.....blank it.
	if(reOneOrMoreDigits.test(val) == false) {
		textBox.value = '';
	}
}

String.prototype.maskValue = function(mask) {
	var retVal = mask;
	var val = this;

	//loop thru mask and replace #'s with current value one at a time
	for(var i=0;i<val.length;i++) {
		retVal = retVal.replace(/#/i, val.charAt(i));
	}
	// get rid of rest of #'s
	retVal = retVal.replace(/#/gi, "");
	return retVal;
}

function cleanKeyCode(key)
{
	switch(key)
	{
		case 96: return "0"; break;
		case 97: return "1"; break;
		case 98: return "2"; break;
		case 99: return "3"; break;
		case 100: return "4"; break;
		case 101: return "5"; break;
		case 102: return "6"; break;
		case 103: return "7"; break;
		case 104: return "8"; break;
		case 105: return "9"; break;
		default: return String.fromCharCode(key); break;
	}
}

function setCaretAtEnd (field) {
  if (field.createTextRange) {
    var r = field.createTextRange();
    r.moveStart('character', field.value.length);
    r.collapse();
    r.select();
  }
}

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.JobApplication.txtFirstName.value))
  {
    alert("First Name is missing. Please enter information in all the required fields.");
    document.JobApplication.txtFirstName.focus();
    return false;
  }

  if (isWhitespace(document.JobApplication.txtLastName.value)) 
  {
    alert("Last Name is missing. Please enter information in all the required fields.");
    document.JobApplication.txtLastName.focus();
    return false;
  }
  
  if (isWhitespace(document.JobApplication.txtHomePhone.value)) 
  {
    alert("Home Phone number is missing. Please enter information in all the required fields.");
    document.JobApplication.txtHomePhone.focus();
    return false;
  }
  
  if (!isEmail(form.elements["txtEmail"].value))
  {
    alert("You must enter a valid email address (like yourname@yahoo.com).");
    document.JobApplication.txtEmail.focus();
    return false;
  }

  if (isWhitespace(document.JobApplication.txtAddress.value)) 
  {
    alert("Address is missing.  Please enter information in all the required fields.");
    document.JobApplication.txtAddress.focus();
    return false;
  }

  if (isWhitespace(document.JobApplication.txtCity.value))
  {
    alert("City is missing. Please enter information in all the required fields.");
    document.JobApplication.txtCity.focus();
    return false;
  }
  if (isWhitespace(document.JobApplication.txtState.value))
  {
    alert("State is missing. Please enter information in all the required fields.");
    document.JobApplication.txtState.focus();
    return false;
  }
  if (isWhitespace(document.JobApplication.txtZip.value))
  {
    alert("Zip Code is missing. Please enter information in all the required fields.");
    document.JobApplication.txtZip.focus();
    return false;
  }
  if (isWhitespace(document.JobApplication.intLoanAmount.value))
  {
    alert("Loan Amount is missing. Please enter information in all the required fields.");
    document.JobApplication.intLoanAmount.focus();
    return false;
  }
  if (isWhitespace(document.JobApplication.intPropertyValue.value))
  {
    alert("Property Value is missing. Please enter information in all the required fields.");
    document.JobApplication.intPropertyValue.focus();
    return false;
  }

return true;
}