//::Trim String Function
function Trim(str){
	str = str.replace(/\s+/,'');
	str = str.replace(/\s+$/,'');
	return str;
}

//::IsNumeric Function
function IsNumeric(val){
	if (val != "") {
		for (i=0; i<val.length; i++){
			if (isNaN(val.charAt(i)) &&
			   (val.charAt(i) != "," &&
				val.charAt(i) != ".")){
				return false;
			}
		}
	}
	else {
		return false;
	}
	return true;
}

//::Valid E-Mail
function ValidEmail(str){
	SpcPos = str.indexOf(' ');
	AccPos = str.indexOf('@');
	AccStr = str.substr(0,AccPos);
	AccLen = AccStr.length;
	DomPos = str.lastIndexOf('.');
	DomStr = str.substr(AccPos+1,(DomPos-AccPos)-1);
	DomLen = DomStr.length;
	ExtPos = DomPos + 1;
	ExtLen = str.length - ExtPos;
	ExtStr = str.substr(ExtPos,ExtLen);
	if(	   (SpcPos == -1)
		&& (AccPos != -1)
		&& (DomPos != -1)
		&& (AccLen >= 2)
		&& (DomLen >= 2)
		&& (ExtLen >= 2)
		&& (ExtLen <= 4)){
		return true;
	}
	else{return false}
}

//::Valid Telephone
function ValidTelephone(str){
	str = Trim(str);
	str = str.replace("(","");
	str = str.replace(")","");
	str = str.replace(".","");
	str = str.replace(".","");
	str = str.replace("-","");
	str = str.replace("-","");
	str = str.replace(" ","");
	strLen = str.length;
	if((strLen == 10 || strLen == 7) && IsNumeric(str)){
		return true;
	}
	else{
		return false;
	}
}

//::check if radio or checkbox items are selected
function CheckSelected(arr){
	if(arr.length > 0){
		for(var index = 0; index < arr.length; index++){
			if(arr[index].checked){
				return true;
				break;
			}
		}
	}
	else{
		if(arr.checked){
			return true;
		}
		else{
			return false;
		}
	}
	return false;
}

//::Validate Form
function ValidateContact() {
	strInfo = "";
	var objForm = document.forms[0];

	if (objForm.firstname.value == "") {
		strInfo += "\n\t- " + "First Name";
	}
	if (objForm.lastname.value == "") {
		strInfo += "\n\t- " + "Last Name";
	}
	if (!ValidTelephone(objForm.phone.value)) {
		strInfo += "\n\t- " + "Phone";
	}

	if (strInfo != "") {
		strInfo = "The following information was entered incorrectly:\n" +
		strInfo + "\n\nPlease re-enter the information and try again...";
		alert(strInfo);
		return false;
	}
	else {
		return true;
	}
}

//::Validate Form
function ValidateQuote() {
	strInfo = "";
	var objForm = document.forms[0];

	if (objForm.firstname.value == "") {
		strInfo += "\n\t- " + "Business Name";
	}
	if (objForm.lastname.value == "") {
		strInfo += "\n\t- " + "Contact Name";
	}
	if (!ValidTelephone(objForm.phone.value)) {
		strInfo += "\n\t- " + "Phone";
	}

	if (strInfo != "") {
		strInfo = "The following information was entered incorrectly:\n" +
		strInfo + "\n\nPlease re-enter the information and try again...";
		alert(strInfo);
		return false;
	}
	else {
		return true;
	}
}


//::Validate Form
function ValidateQuoteFull() {
	strInfo = "";
	var objForm = document.forms[0];

	if (objForm.firstname.value == "") {
		strInfo += "\n\t- " + "Business Name";
	}
	if (objForm.lastname.value == "") {
		strInfo += "\n\t- " + "Contact Name";
	}
	if (objForm.state.value == "") {
		strInfo += "\n\t- " + "State";
	}
	if (!ValidTelephone(objForm.phone.value)) {
		strInfo += "\n\t- " + "Phone";
	}

	if (strInfo != "") {
		strInfo = "The following information was entered incorrectly:\n" +
		strInfo + "\n\nPlease re-enter the information and try again...";
		alert(strInfo);
		return false;
	}
	else {
		return true;
	}
}


//::Numeric Mask Function
//ex. onkeydown="javascript:return NumericMask(event,this,'(###) ###-####');"
function NumericMask(objEvent,objText,strMask) {
	if (!objEvent) {
		objEvent = window.event;
	}
	key = objEvent.keyCode;
	FilterNum = FilterStrip(objText.value,strMask);
	if (key==9)	{
		return true;
	}
	else if (key==8&&FilterNum.length!=0) {
		FilterNum = FilterNum.substring(0,FilterNum.length-1);
	}
	else if (((key>47 && key<58)||(key>95 && key<106)) && FilterNum.length<FilterMax(strMask)) {
		if (key>95 && key<106) {
			key = (key-48);
		}
		FilterNum=FilterNum+String.fromCharCode(key);
	}
	var FilterFinal='';
	for (FilterStep = 0; FilterStep < strMask.length; FilterStep++) {
		if (strMask.charAt(FilterStep)=='#') {
			if (FilterNum.length!=0) {
				FilterFinal = FilterFinal + FilterNum.charAt(0);
				FilterNum = FilterNum.substring(1,FilterNum.length);
			}
			else {
				FilterFinal = FilterFinal + "";
			}
		}
		else if (strMask.charAt(FilterStep)!='#') {
			FilterFinal = FilterFinal + strMask.charAt(FilterStep);
		}
	}
	objText.value = FilterFinal;
	return false;
}
function FilterStrip(FilterTemp,strMask) {
	strMask = ReplaceAll(strMask,'#','');
	for (FilterStep = 0; FilterStep < strMask.length++; FilterStep++) {
		FilterTemp = ReplaceAll(FilterTemp,strMask.substring(FilterStep,FilterStep+1),'');
	}
	return FilterTemp;
}
function FilterMax(strMask) {
	FilterTemp = strMask;
	for (FilterStep = 0; FilterStep < (strMask.length+1); FilterStep++) {
		if (strMask.charAt(FilterStep)!='#') {
			FilterTemp = ReplaceAll(FilterTemp,strMask.charAt(FilterStep),'');
		}
	}
	return FilterTemp.length;
}

//::Replace All Function
function ReplaceAll(strFull,strOld,strNew) {
	var strLength = strFull.length, txtLength = strOld.length;
	if ((strLength == 0) || (txtLength == 0)) return strFull;
	var i = strFull.indexOf(strOld);
	if ((!i) && (strOld != strFull.substring(0,txtLength))) return strFull;
	if (i == -1) return strFull;
	var strReplace = strFull.substring(0,i) + strNew;
	if (i+txtLength < strLength) strReplace += ReplaceAll(strFull.substring(i+txtLength,strLength),strOld,strNew);
	return strReplace;
}