/*
NumericValidators.js
Rev 12/22/2005 by Jon Vandermeulen

isNumeric(oInput) - This function is used to check if a text input's value is numeric. This function will work in current versions of IE and Netscape, but may not work in future versions
isNumericText(value) - This function is used to check if a text value is numeric. This function will work in current versions of IE and Netscape, but may not work in future versions
isCurrency(this_value)
numericOnly(oTextbox,evt) - This function will only allow numeric entries into an input. It also allows a user to move around and delete. To use this function, include the following in your <input id=text1 name=text1> tag: onKeyDown="return numericOnly(this,event)"
dashNumericOnly(oTextbox,evt) - This function will only allow numeric and dash(-) entries into an input. It also allows a user to move around and delete. To use this function, include the following in your <input id=text1 name=text1> tag: onKeyDown="return numericOnly(this,event)"
decimalsOnly(oTextbox,evt) - This function will only allow numeric entries into an input. It also allows a user to move around and delete. To use this function, include the following in your <input id=text1 name=text1> tag: onKeyDown="return numericOnly(this,event)"
onFocusNumericOnly(oTextbox) - This function strips an input of any values that are not numeric. To use this function, include the following in your <input id=text1 name=text1> tag: onFocus="onFocusNumericOnly(this)"
onFocusNumericOnlyString(oValue) - This function returns the value passed in with only the numeric characters left
formatPhone(oTextbox) - This function formats an inputs value into a phonenumber format. It is best to use the numericOnly function with this function. To use this function, include the following in your <input id=text1 name=text1> tag: onBlur="formatPhone(this)"
checkccnumber(ccnumber,cctype) - This function can be used to validate that a credit card input is valid for a credit card type

*/


function isCurrency(this_value)
{
	for(i=0;i<this_value.length;i++)
	{
		if((this_value.charCodeAt(i) < 48 || this_value.charCodeAt(i) > 57) && this_value.substr(i,1) != '.' && this_value.substr(i,1) != '$')
		{
			return false;
		}
	}
	return true;
}


//This function will only allow numeric entries into an input. It also allows a user to move around and delete 
//To use this function, include the following in your <input id=text1 name=text1> tag: onKeyDown="return numericOnly(this,event)"
function numericOnly(oTextbox,evt) {
	if (window.event) { // IE
		if (evt.keyCode == 13){
			return true;
		}
		if (((evt.keyCode < 48 || evt.keyCode > 57) && (evt.keyCode < 96 || evt.keyCode > 105) && evt.keyCode != 8 && evt.keyCode != 9 && evt.keycode != 13 && evt.keyCode != 37 && evt.keyCode != 39 && evt.keyCode != 46) || evt.shiftKey == 1) {
			return false;
		} else {
			return true;
		}
	} else { // Netscape
		if ((evt.which < 48 || evt.which > 57) && (evt.keyCode < 96 || evt.keyCode > 105) && evt.which != 8 && evt.which != 13) {
			return false;
		} else {
			return true;
		}
	}
}

//This function will only allow numeric and dash(-) entries into an input. It also allows a user to move around and delete 
//To use this function, include the following in your <input id=text1 name=text1> tag: onKeyDown="return numericOnly(this,event)"
function dashNumericOnly(oTextbox,evt) {
	if (window.event) { // IE
		if (evt.keyCode == 13){
			return true;
		}
		if (((evt.keyCode < 48 || evt.keyCode > 57) && (evt.keyCode < 96 || evt.keyCode > 105) && evt.keyCode != 189 && evt.keyCode != 109 && evt.keyCode != 8 && evt.keyCode != 9 && evt.keycode != 13 && evt.keyCode != 37 && evt.keyCode != 39 && evt.keyCode != 46) || evt.shiftKey == 1) {
			return false;
		} else {
			return true;
		}
	} else { // Netscape
		if ((evt.which < 96 || evt.which > 57)  && evt.which != 8 && evt.which != 13) {
			return false;
		} else {
			return true;
		}
	}
}


//This function will only allow numeric entries into an input. It also allows a user to move around and delete 
//To use this function, include the following in your <input id=text1 name=text1> tag: onKeyDown="return numericOnly(this,event)"
function decimalsOnly(oTextbox,evt) {
	if (window.event) { // IE
		if (evt.keyCode == 13){
			return true;
		}
		if (((evt.keyCode < 48 || evt.keyCode > 57) && (evt.keyCode < 96 || evt.keyCode > 105) && evt.keyCode != 8 && evt.keyCode != 9 && evt.keycode != 13 && evt.keyCode != 37 && evt.keyCode != 39 && evt.keyCode != 46 && evt.keyCode != 110 && evt.keyCode != 190) || evt.shiftKey == 1) {
			return false;
		} else {
			return true;
		}
	} else { // Netscape
		if ((evt.which < 48 || evt.which > 57)  && evt.which != 8 && evt.which != 13 && evt.which != 46) {
			return false;
		} else {
			return true;
		}
	}
}

//This function strips an input of any values that are not numeric
//To use this function, include the following in your <input id=text1 name=text1> tag: onFocus="onFocusNumericOnly(this)"
function onFocusNumericOnly(oTextbox) {
	var sReturnValue;
	sReturnValue = '';
	var i;
	for (i=0;i<oTextbox.value.length;i++) {
		if (oTextbox.value.charCodeAt(i) >= 48 && oTextbox.value.charCodeAt(i) <= 57) {
			sReturnValue = sReturnValue + oTextbox.value.charAt(i);
		}
	}
	oTextbox.value = sReturnValue;
	return true;
}

//This function returns the value passed in with only the numeric characters left
function onFocusNumericOnlyString(oValue) {
	var sReturnValue;
	sReturnValue = '';
	var i;
	for (i=0;i<oValue.length;i++) {
		if (oValue.charCodeAt(i) >= 48 && oValue.charCodeAt(i) <= 57) {
			sReturnValue = sReturnValue + oValue.charAt(i);
		}
	}
	
	return sReturnValue;
}

//This function formats an inputs value into a phonenumber format. It is best to use the numericOnly function with this function.
//To use this function, include the following in your <input id=text1 name=text1> tag: onBlur="formatPhone(this)"
function formatPhone(oTextbox) {
	var sReturnValue;
	
	sReturnValue = '';
	oTextbox.value.replace(" ","");
	if (oTextbox.value.length > 0) {
		while(oTextbox.value.substr(0,1) == "1" || oTextbox.value.substr(0,1) == "0"){
			oTextbox.value = oTextbox.value.substr(1,(oTextbox.value.length-1))
		}
		if (oTextbox.value.length < 10) {
			alert('Please enter your area code and phone number in the following format: 8001234567\nIf your phone number includes an extension,\nadd it to the end like this: 8001234567111\nPlease do not include the "1" prefix.\n\nThis page will automatically reformat the phone number for you.');
			oTextbox.value="";
			oTextbox.focus();
		} else {
			sReturnValue = '(' + oTextbox.value.substr(0,3) + ')' + oTextbox.value.substr(3,3) + '-' + oTextbox.value.substr(6,4);
			if (oTextbox.value.length > 10) {
				sReturnValue = sReturnValue + 'x' + oTextbox.value.substr(10);
			}
			oTextbox.value = sReturnValue;
		}
	}
	return true;
}


// This function can be used to validate that a credit card input is valid for a credit card type
function checkccnumber(ccnumber,cctype)
{
	var ctype, cclength, ccprefix, prefixes, lengths, number, prefixvalid, lengthvalid;
	var result, qsum, x, ch, sum, prefix, length;
  
	//alert('cctype = ' + cctype + '\nccnumber = ' + ccnumber); 
  
	ctype = cctype.substr(0,1);
	ctype = ctype.toUpperCase();
  
	switch(ctype){
		case "V":
			cclength="13;16";
			ccprefix="4";
			break;
		case "M":
			cclength="16";
			ccprefix="51;52;53;54;55";
			break;
		case "A":
			cclength="15";
			ccprefix="34;37";
			break;
		case "C":
			cclength="14";
			ccprefix="300;301;302;303;304;305;36;38";
			break;
		case "D":
			cclength="16";
			ccprefix="6011";
			break;
		case "E":
			cclength="15";
			ccprefix="2014;2149";
			break;
		case "J":
			cclength="15;16";
			ccprefix="3;2131;1800";
			break;
		default:
			cclength=""
			ccprefix=""
			break;
	}
	
	prefixes=ccprefix.split(";");
	lengths=cclength.split(";");
	number=onFocusNumericOnlyString(ccnumber);
  
	prefixvalid=false;
	lengthvalid=false;
  
	for(var i=0; i<prefixes.length;i++){
		if(number.indexOf(prefixes[i]) == 0)
		{
			prefixvalid=true;
		}
	}
   
	for(var i=0; i<lengths.length;i++){
		if(number.length == lengths[i])
		{
			lengthvalid=true;
		}
	}
  
	result=0;
  
	if(!prefixvalid){
		result=result+1;
	}
    
	if(!lengthvalid){
		result=result+2;
	}  
	
	qsum=0;
  
	for(var x=1;x<=number.length;x++){
		ch = number.substr(number.length - (x),1);
		if(x % 2 == 0){
			sum = 2 * parseInt(ch);
			qsum = qsum + (sum % 10);
			if(sum > 9){
				qsum = qsum + 1;
			}
		}
		else{
			qsum = qsum + parseInt(ch);
		}
	}
	if(qsum % 10 != 0){
		result = result + 4;
	} 
	if(cclength == ""){
		result = result + 8;
	}
	return result;
}
