/**********************************************************************************/
/** © 1998 - 2009 Ultratec, Inc.                                                 **/
/**                                                                              **/
/** Disclaimer:                                                                  **/
/**   Ultratec, Inc. is providing this code as an example of how to access their **/
/**   Externally Hosted CapTel Application Programming Interface. All example    **/
/**   source code obtained from Ultratec, Inc is delivered "AS IS." Ultratec,    **/
/**   Inc specifically disclaims any and all warranties, expressed or implied,   **/
/**   including but not limited to any implied warranties of merchantability or  **/
/**   fitness for a particular purpose. Ultratec, Inc. does not represent or     **/
/**   warrant that any program is error free or that its use will be             **/
/**   uninterrupted. Ultratec, Inc. shall not be liable for any loss of profit,  **/
/**   loss of business or goodwill, loss of data, interruption in business, nor  **/
/**   for indirect, special, incidental, or consequential damages of any kind    **/
/**   under or arising out of the use or implementation of the example source    **/
/**   code, however caused, whether for breach or warranty, breach or            **/
/**   repudiation of contract, tort, negligence, or otherwise, even if Ultratec, **/
/**   Inc has been advised of the possibility of such a loss. Also Ultratec,     **/
/**   Inc. shall not have any obligation to provide techincal support or         **/
/**   development assistance for the example source code obtained from Ultratec, **/
/**   Inc.                                                                       **/
/**********************************************************************************/
/**********************************************************************************/
/** Misc. Functions                                                              **/
/**********************************************************************************/
//This function will return True or False depending on whether or not the specified text validates to a valid US phone number
function validateUSPhone(strPhone,eError){
	var regex = /^([2,3,4,5,6,7,8,9]{1})(\d{9})$/; //this is a regular expression that validates a 10 digit number that doesn't start with 0 or 1
	return regex.test(numbersOnly(strPhone,eError));
}

//This function will return True or False depending on whether or not their are 10 or more numbers in the phone number passed to it.
function validateIntlPhone(strPhone,eError){
	return (numbersOnly(strPhone).length>=10);
}

//This function changes the page to the specified URL
function changeURL(strURL){
	window.location.href = strURL;
}

/**********************************************************************************/
/** Key Press Functions                                                          **/
/**********************************************************************************/
//This array will match the validation type to the element that is being validated
//	It is defined on the page because pages can have different validation for different fields...
var aCheckKey = new Array();  //This should hold an array with the following array: strFilter, bDisallow, eError -- where bDisallow determines whether you are allowing only the characters in the strFilter, or disallowing only the characters in strFilter; and eError is the id of the element that will display the error message.

//this function shows which key was pressed
function checkKey(event){
	event = (!event)?window.event:event;
	var iChar = (event.which)?event.which:event.keyCode;
	if(iChar==13 && document.getElementById("btn1")){//if the enter key is pressed (character 13) and a message box is displayed that has an input field, then do the following...
		document.getElementById("btn1").click(); //click the first button...
	}else{ //process the validation for the element...
		var element = (event.srcElement)?event.srcElement:event.target;
		
		var strCurrent="", strNew="", strPrefix="", strStart="", strMiddle="", strEnd="", strMAC1="", strMAC2="", strMAC3="", strMAC4="", strMAC5="", strMAC6="";
		var strFilter = aCheckKey[element.name.toLowerCase()][0];
		var eError = aCheckKey[element.name.toLowerCase()][2];
		var bDisallow = aCheckKey[element.name.toLowerCase()][1];
		
		switch(strFilter.toLowerCase()){
			case "alpha":
				strNew = alphaOnly(element.value, eError);
				break;
			
			case "alphanumeric":
				strNew = alphaOnly(element.value, eError);
				break;
			
			case "numeric":
				strNew = numbersOnly(element.value, eError);
				break;
			
			case "last":
				strNew = lastOnly(element.value, eError);
				break;
			
			case "email":
				strNew = emailOnly(element.value, eError);
				break;
			
			case "postalcode":
				strNew = postalCodeOnly(element.value, eError);
				break;
			
			case "phone":
				//the if statement below is used to strip the '-' that is automatically placed into the string in the event that the backspace button was selected and that is the last character in the string
				if(element.value.charAt(element.value.length-1) == '-'){
					element.value = element.value.substr(0,element.value.length-1);
				}
				strNew = formatPhone(element.value, eError);
				break;
			
			case "intlphone":
				//the if statement below is used to strip the '-' that is automatically placed into the string in the event that the backspace button was selected and that is the last character in the string
				if(element.value.charAt(element.value.length-1) == '-'){
					element.value = element.value.substr(0,element.value.length-1);
				}
				strNew = formatIntlPhone(element.value, eError);
				break;
			
			case "nonsqlspecialcharacters":
				strNew = nonSQLSpecialCharacters(element.value, eError);
				break;
			
			default:
				strNew = filterString(element.value, strFilter, bDisallow, eError);
				break;
		}
		element.value = strNew;
	}
}

//This function strips all sql special characters
function nonSQLSpecialCharacters(strCheck,eError){
	var strFilter = "!@#$%^&*()_";
	return filterString(strCheck, strFilter, true, eError);
}

//This function returns all numbers from a string and strips everything else out...
function numbersOnly(strCheck,eError){
	var strFilter = "0123456789";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns all numbers from a string and strips everything else out...
function postalCodeOnly(strCheck,eError){
	strCheck = numbersOnly(strCheck, eError);
	return strCheck.substr(0,5);;
}

//This function returns all numbers from a string and strips everything else out...
function alphaNumericOnly(strCheck,eError){
	var strFilter = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns all numbers from a string and strips everything else out...
function alphaOnly(strCheck, eError){
	var strFilter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns all alpha characters and "'" from a string and strips everything else out...
function lastOnly(strCheck, eError){
	var strFilter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns valid last name characters from a string and strips everything else out...
function lastNameOnly(strCheck, eError){
	var strFilter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns valid email characters from a string and strips everything else out...
function emailOnly(strCheck, eError){
	var strFilter = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-.@";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns valid SQL Special characters from a string and strips everything else out...
function SQLSpecialCharactersOnly(strCheck, eError){
	var strFilter = "!%^[]";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns all numbers from a string and strips everything else out...
function hexOnly(strCheck,eError){
	var strFilter = "1234567890abcdefABCDEF";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns all numbers from a string and strips everything else out...
function ipOnly(strCheck,eError){
	var strFilter = "1234567890.";
	return filterString(strCheck, strFilter, false, eError);
}

//This function returns a string that filters out all characters except the characters in the filter...
// If bDisallow is set to "true", then the function will allow all characters except for the characters in strFilter
// eError is the element that will display any error for the given filterString call
function filterString(strCheck, strFilter, bDisallow, eError){
	var strReturn = "";
	var bError = false;
	for(var i=0; i<strCheck.length && !bError; i++){
		if(eError!=undefined && eError!=null && eError!=""){
			//document.getElementById(eError).innerHTML = "";
			//document.getElementById(eError).style.display = "none";
		}
		if(strFilter.indexOf(strCheck.charAt(i))>=0 && !bDisallow){
			strReturn += strCheck.charAt(i);
		}else if(strFilter.indexOf(strCheck.charAt(i))<0 && bDisallow){
			strReturn += strCheck.charAt(i);
		}else{
			if(eError!=null && eError!=undefined && eError!=""){
				//document.getElementById(eError).innerHTML = "The character entered, '" + strCheck.charAt(i) + "', is not allowed, so it was removed.";
				//document.getElementById(eError).style.display = "block";
			}
		}
	}
	return strReturn;
}

//This function formats a string into XXX-XXX-XXXX
function formatPhone(strPhone, eError){
	var strCurrent = numbersOnly(strPhone, eError), strNew = "", strStart = "", strMiddle = "", strEnd = "";
	if(strCurrent.length>10){ //NOTE: This forces the phone number to be only 10 digits long...
		strCurrent = strCurrent.substr(0,10);
	}
	
	if(eError!=null && eError!=undefined && eError!=""){ //only try to display an error message if there is a place to display the error
		if(strCurrent.charAt(0)=="0" || strCurrent.charAt(0)=="1"){ //us phone numbers can't start with a 0 or 1
			//document.getElementById(eError).innerHTML = "US phone numbers do not start with 0 or 1. Please enter a valid US phone number.";
			//document.getElementById(eError).style.display = "block";
		}else{
			//document.getElementById(eError).innerHTML = "";
			//document.getElementById(eError).style.display = "none";
		}
	}
	
	var iCurrentLength = strCurrent.length;
	if(iCurrentLength<=3){
		strNew = strCurrent;
	}else if(iCurrentLength>3 && iCurrentLength<=7){
		strStart = strCurrent.substr(0,3);
		if(iCurrentLength>3){
			strEnd = strCurrent.substr(3);
		}
		strNew = strStart + "-" + strEnd
	}else if(iCurrentLength>7 && iCurrentLength<=10){
		strStart = strCurrent.substr(0,3);
		strMiddle = strCurrent.substr(3,3);
		strEnd = strCurrent.substr(6);
		strNew = strStart + "-" + strMiddle + "-" + strEnd;
	}
	
	return strNew;
}

//This function formats a string into XXX-XXX-XXXX if it is 10 digits or less, otherwise it doesn't format the string
function formatIntlPhone(strPhone, eError){
	var strCurrent = numbersOnly(strPhone, eError), strNew = "", strStart = "", strMiddle = "", strEnd = "";
	if(strCurrent.length>20){ //NOTE: This forces the phone number to be only 10 digits long...
		strCurrent = strCurrent.substr(0,20);
	}
	
	var iCurrentLength = strCurrent.length;
	if(iCurrentLength<=3){
		strNew = strCurrent;
	}else if(iCurrentLength>3 && iCurrentLength<=7){
		strStart = strCurrent.substr(0,3);
		if(iCurrentLength>3){
			strEnd = strCurrent.substr(3);
		}
		strNew = strStart + "-" + strEnd
	}else if(iCurrentLength>7 && iCurrentLength<=10){
		strStart = strCurrent.substr(0,3);
		strMiddle = strCurrent.substr(3,3);
		strEnd = strCurrent.substr(6);
		strNew = strStart + "-" + strMiddle + "-" + strEnd;
	}else{
		strNew = strCurrent;
	}
	
	return strNew;
}

/**********************************************************************************/
/** Form Handling Functions                                                      **/
/**********************************************************************************/
//This function submits a form via javascript
function submitForm(strForm){
	document.forms[strForm].submit();
}

//This function gets the value of the element passed in
function getFormValue(strForm, strElement, strInValue){
	var strValue = "";
	if(strInValue != null && strInValue != undefined){
		switch(document.forms[strForm].elements[strElement].type){
			case "select-one":
				for(var i=0; i<document.forms[strForm].elements[strElement].options.length; i++){
					if(strInValue == document.forms[strForm].elements[strElement].options[i].value){
						strValue = document.forms[strForm].elements[strElement].options[i].text;
					}
				}
				break;
			default:
				//does nothing
				break;
		}
	}else{
		switch(document.forms[strForm].elements[strElement].type.toLowerCase()){
			case "checkbox":
				if(document.forms[strForm].elements[strElement].checked){
					strValue = document.forms[strForm].elements[strElement].value;
				}
				break;
			case "radio":
				if(document.forms[strForm].elements[strElement].checked){
					strValue = document.forms[strForm].elements[strElement].value;
				}
				break;
			//if you want the select value, then leave this commented. if you want the select-text, then uncomment the section below
			/** case "select-one":
				strValue = document.forms[strForm].elements[strElement].options[document.forms[strForm].elements[strElement].selectedIndex].text;
				break; **/
			default:
				strValue = document.forms[strForm].elements[strElement].value;
				break;
		}
	}
	return strValue;
}

//sets the value of the specified form element to the specified value
function setFormValue(strForm, strElement, strValue){
	if(document.forms[strForm]){
		if(document.forms[strForm].elements[strElement]){
			if(!document.forms[strForm].elements[strElement].disabled){
				document.forms[strForm].elements[strElement].value = strValue;
			}
		}
	}
}

//disables ALL elements in the specified form
function disableForm(strForm){
	for (var i=0; i<document.forms[strForm].elements.length; i++) {
		document.forms[strForm].elements[i].setAttribute("disabled","disabled");
	}
}

//enables ALL elements in the specified form unless the element name is in the aSkip array.
function enableForm(strForm,aSkip){
	for(var i=0; i<document.forms[strForm].elements.length; i++){
		var bEnable = true;
		if(aSkip!=null && aSkip.length>0){
			for(var x=0; x<aSkip.length; x++){
				if(document.forms[strForm].elements[i].name==aSkip[x]){
					bEnable = false;
					break;
				}
			}
		}
		if(bEnable){
			document.forms[strForm].elements[i].removeAttribute("disabled");
		}
	}
}

//This function is used to select a radio button or checkbox
function selectCheckbox(strForm, strElement){
	document.forms[strForm].elements[strElement].checked = true;
}

//This function selects an option in a dropdown list
function selectOption(strForm, strSelect, strValue){
	if(document.forms[strForm]){
		var selElement = document.forms[strForm].elements[strSelect];
		if(selElement.selectedIndex >= 0){
			selElement.value = strValue;
		}
	}
}

//This function adds an option to a dropdown list
function addOption(strForm, strSelect, strOptionValue, strOptionText){
	var selElement = document.forms[strForm].elements[strSelect];
	if(selElement.selectedIndex >= 0){
		var iIndex = selElement.length;
		selElement.options[iIndex] = new Option(strOptionText, strOptionValue);
	}
}

//This function removes an option from a dropdown list
function removeOption(strForm, strSelect, strOptionValue){
	var selElement = document.forms[strForm].elements[strSelect];
	if(selElement.selectedIndex >= 0){
		for(var i=0; i<selElement.length; i++){
			if(selElement[i].value == strOptionValue){
				selElement.remove(i);
			}
		}
	}
}

//This function is used to ensure that spaces don't get eaten up by ASP
// This function only needs to be used when the parameter could include
// a space in it
function formatParameterASP(strParameter){
	strParameter = escape(strParameter.replace(/ /gi, "+"));
	return strParameter;
}

// This function is used to trim the string
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


// This function is used to trim the string
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}

// This function is used to trim the string
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}
