/*
-----------------------------------------------------------------
Function    : runFunction
Language    : JavaScript
Description : Used to pass an argument object to another function
Parameters  : objFunction = function object to run
	      argList = an argument object 
Result	    : Returns the result of the function object.
-----------------------------------------------------------------
*/
function runFunction(objFunction, argList) {
	var strCall = 'objFunction (';
	for (var idx=0; idx < argList.length; idx++)
		strCall = strCall + 'argList[' + idx + '] , ';
	strCall = strCall.substring(0, strCall.length-2) + ')';
	return eval(strCall);
}

/* 
-----------------------------------------------------------------
Function	: isValidUSPhone
Language	: JavaScript
Description	: validate that a string of characters can be converted to a valid US telephone number.
Arguments	:	1st is Phone number. Defaults to blank
				:  2nd is boolean - blank phones are valid. Default is true
				:  3rd is boolean - allow extension in the number. Default is true
Result	    : True = valid US Phone, False = not a valid US Phone
-----------------------------------------------------------------
*/
function isValidUSPhone() {
	var strPhone = '';
	var boolAllowBlank = true;
	var boolAllowExt = true;
	var intArgCount = (arguments.length > 3)? 3 : arguments.length;
	switch (intArgCount) {
		case 3:
			boolAllowExt = arguments[2];
		
		case 2:
			boolAllowBlank = arguments[1];

		case 1:
			strPhone = arguments[0];
	}
			
	if (boolAllowBlank && !strPhone.length)
		return true;
	// remove the allowed non-numeric characters AND allow for ONE leading the areacode
	strPhone = (strPhone.replace(/[\s\(\)-\.\\\/xX]|(ext)/gi, '')).replace(/\b1/, '');
	// phone only valid now IF it has at least 10 characters and contains only numeric characters
	
	if (boolAllowExt)
		return ((strPhone.length >= 10) && (strPhone.match(/\D/)==null) && (strPhone.substring(0,1) > 1));
	else
		return ((strPhone.length == 10) && (strPhone.match(/\D/)==null) && (strPhone.substring(0,1) > 1));
}

/* 
-----------------------------------------------------------------
Function    : formatUSPhone
Language    : JavaScript
Description : formats a PREVALIDATED string of characters  a valid US telephone number.
Parameters  : strPhone - This is the string of characters to foramtted
Result	    : Returns the formatted string
-----------------------------------------------------------------
*/
function formatUSPhone(strPhone) {
	// remove all non-numeric characters and a leading 1 if it exists. Then format the string right and remove the extension marker if no extension exists
	return (((strPhone.replace(/\D/g, '')).replace(/\b1/, '')).replace(/(\d{3})(\d{3})(\d{4})(\d{0,10})/, "($1) $2-$3 x$4")).replace(/ x\b/, "");
}

/* 
-----------------------------------------------------------------
Function    : fixFormatPhone
Language    : JavaScript
Description : Validates and formats a US telephone number in a text field
Parameters  : 1st is Phone number flied
				2nd is boolean - blank phones are valid. Default is true
				3rd is boolean - allow extension in the number. Default is true
Result	    : Returns true if the field was formatted, false if not.
-----------------------------------------------------------------
*/
function fixFormatPhone() {
	var fldPhone = null;
	var boolAllowBlank = true;
	var boolAllowExt = true;
	var intArgCount = (arguments.length > 3)? 3 : arguments.length;
	switch (intArgCount) {
		case 3:
			boolAllowExt = arguments[2];
		
		case 2:
			boolAllowBlank = arguments[1];

		case 1:
			fldPhone = arguments[0];
	}

	if (!isValidUSPhone(fldPhone.value, boolAllowBlank, boolAllowExt)) {
		var strExt = (boolAllowExt) ? ' x1234': '';
		alert(fldPhone.value + ' is not a valid U.S. telephone number.\nPlease enter as (123) 456-7890' + strExt + ' format.\n(Valid U.S. telephone numbers do not start with a zero [0] or one [1])'+((boolAllowExt?'':'\nExtensions are not allowed in this field.')));

		setFocus(fldPhone, true);	// this call is in FormChek.js. If it failed here make sure to include the other JS
		return false;
	}
	fldPhone.value = formatUSPhone(fldPhone.value);
	return true;
}


/* 
-----------------------------------------------------------------
Function	: isZip
Language	: JavaScript
Description	: validate that a string of characters can be converted to a valid US Zipcode (5 or 9 digit)
Arguments	: 1st is Zip. Defaults to blank
			2nd is boolean - blank zips are valid. Default is true
			3rd is boolean - allow plus 4 in the zip. Default is true
Result	    : True = valid US Zip, False = not a valid US Zip
-----------------------------------------------------------------
*/
function isZip() {
	var strZip = '';
	var boolAllowBlank = true;
	var boolAllow4 = true;
	var intArgCount = (arguments.length > 3)? 3 : arguments.length;
	switch (intArgCount) {
		case 3:
			boolAllow4 = arguments[2];
		
		case 2:
			boolAllowBlank = arguments[1];

		case 1:
			strZip = arguments[0] + '';
	}
			
	if (!strZip.length)
		return boolAllowBlank;

	// remove the allowed non-numeric characters 
	strZip = strZip.replace(/\s/,'').replace(/(\d{5})[-\x20]?(\d{4})/, '$1$2');

	// Zip only valid now IF it has at the right number of digits
	if (boolAllow4)
		return (((strZip.length == 9) || (strZip.length == 5)) && (strZip.match(/\D/) == null));
	else
		return ((strZip.length == 5) && (strZip.match(/\D/) == null));
}

/* 
-----------------------------------------------------------------
Function    : formatZip
Language    : JavaScript
Description : formats a PREVALIDATED string of characters to a valid zipcode.
Parameters  : strZip - This is the string of characters to foramtted
Result	    : Returns the formatted string
-----------------------------------------------------------------
*/
function formatZip() {
	var strZip = null;
	var bAddDash = true;
	var intArgCount = (arguments.length > 2)? 2 : arguments.length;
	switch (intArgCount) {
		case 2:
			bAddDash = arguments[1];

		case 1:
			strZip = arguments[0];
	}

	if (strZip == null)
		return '';
		
	// remove all non-numeric characters, then format the string right
	if (bAddDash)
		return strZip.replace(/\D/g, '').replace(/(\d{5})(\d{4})/, "$1-$2");
	else
		return strZip.replace(/\D/g, '').replace(/(\d{5})(\d{4})/, "$1$2");
}

/* 
-----------------------------------------------------------------
Function    : fixFormatZip
Language    : JavaScript
Description : Validates and formats a US zipcode in a text field
Parameters  : 1st is zip field
			2nd is boolean - blank zips are valid. Default is true
			3rd is boolean - allow zip + 4 format. Default is true
			4th is boolean - add dash to format of 9 digit zips. Default is true
Result	    : Returns true if the field was formatted, false if not.
-----------------------------------------------------------------
*/
function fixFormatZip() {
	var fldZip = null;
	var boolAllowBlank = boolAllow4 = boolAddDash = true;
	var intArgCount = (arguments.length > 4)? 4 : arguments.length;
	switch (intArgCount) {
		case 4:
			boolAddDash = arguments[3];

		case 3:
			boolAllow4 = arguments[2];
		
		case 2:
			boolAllowBlank = arguments[1];

		case 1:
			fldZip = arguments[0];
	}
	if (!checkZip(fldZip, boolAllowBlank, boolAllow4))
		return false;

	fldZip.value = formatZip(fldZip.value, boolAddDash);
	return true;
}

/* 
-----------------------------------------------------------------
Function    : checkZip
Language    : JavaScript
Description : Validates  a US zipcode
Parameters  : 1st is zip field
			2nd is boolean - blank zips are valid. Default is true
			3rd is boolean - allow zip + 4 format. Default is true
Result	    : Returns true  or false.
-----------------------------------------------------------------
*/
function checkZip() {
	var strZip = null;
	var boolAllowBlank = true;
	var boolAllow4 = true;
	var intArgCount = (arguments.length > 3)? 3 : arguments.length;
	switch (intArgCount) {
		case 3:
			boolAllow4 = arguments[2];
		
		case 2:
			boolAllowBlank = arguments[1];

		case 1:
			strZip = arguments[0];
	}
	if (!isZip(strZip.value, boolAllowBlank, boolAllow4)) {
		var strExt = (boolAllow4) ? ' or 12345-1234': '';
		alert(strZip.value + ' is not a valid U.S. zip code.\nPlease enter as 12345' + strExt + ' format.');
		return false;
	}
	return true;
}

/* 
-----------------------------------------------------------------
Function	: isValidEmailAddress
Language	: JavaScript
Description	: validate that a string of characters conform to standard Email address standards.
Arguments	: 1st is Email Address. Defaults to blank
				: 2nd is boolean - blank Emails are valid. Default is true
Result	    : True = valid Email Address format, False = not a valid Email Address format
-----------------------------------------------------------------
*/
function isValidEmailAddress() {
	var strEmail = '';
	var boolAllowBlank = true;
	var intArgCount = (arguments.length > 2)? 2 : arguments.length;
	switch (intArgCount) {
		case 2:
			boolAllowBlank = arguments[1];

		case 1:
			strEmail = arguments[0];
	}
			
	if (boolAllowBlank && !strEmail.length)
		return true;

	return (strEmail.match(/^[\w-_\.]+\@([\w-_]+\.)+[a-zA-Z]{2,3}$/) != null);
}

