function gfnTrimLeadingTrailingSpaces(ptxValue) {
	ptxValue = ptxValue.replace(/^[ ]+/i,"");
	return ptxValue.replace(/[ ]+$/i,"");
}

function gfnTrimAllSpaces(ptxValue) {
	return ptxValue.replace(/ /gi,"");
}

function gfnTrimToNumeric(ptxValue) {
	ptxValue = ptxValue.replace(/ /gi,"");
	ptxValue = ptxValue.replace(/,/gi,"");
	return ptxValue.replace(/\$/gi,"");
}

function gfnValidatePhoneStandard(ptxString) {

	//true if argument matches standard format nnn-nnn-nnnn

	vobRegExp = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/i
	if (ptxString.match(vobRegExp))
	{
		vynReturn = true;
	}
	else
	{
		vynReturn = false;
	}
	return vynReturn;
}

function gfnValidatePhoneAlt(ptxString) {

	//true if argument matches alternate format (nnn)nnn-nnnn

	vobRegExp = /^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$/i
	if (ptxString.match(vobRegExp))
	{
		vynReturn = true;
	}
	else
	{
		vynReturn = false;
	}
	return vynReturn;
}

function gfnValidatePhoneNumOnly(ptxString) {

	//true if argument matches alternate format nnnnnnnnnn

	vobRegExp = /^[0-9]{10}$/i
	if (ptxString.match(vobRegExp))
	{
		vynReturn = true;
	}
	else
	{
		vynReturn = false;
	}
	return vynReturn;
}

function gfnValidatePostalCode(ptxString) {

	vobRegExp = /^[A-Z]{1}[0-9]{1}[A-Z]{1}[0-9]{1}[A-Z]{1}[0-9]{1}$/
	if (ptxString.match(vobRegExp))
	{
		vynReturn = true;
	}
	else
	{
		vynReturn = false;
	}
	return vynReturn;
}

function gfnValidateZipCode(ptxString) {

	vobRegExp = /^[0-9]{5}(-[0-9]{4})?$/
	if (ptxString.match(vobRegExp))
	{
		vynReturn = true;
	}
	else
	{
		vynReturn = false;
	}
	return vynReturn;
}

function gfnCheckPhone(pobControl,ptxFieldName) {

	//strip any spaces the user may have entered, to make this easier!
	vtxValue = gfnTrimAllSpaces(pobControl.value);
	
	vynReturn = true;
	
	if (gfnValidatePhoneStandard(vtxValue)) {
		//phone number already in standard format nnn-nnn-nnnn
	} else if (gfnValidatePhoneAlt(vtxValue)) {
		//phone number in format (nnn)nnn-nnnn, convert to standard
		vtxValue = vtxValue.substr(1,3) + "-" + vtxValue.substr(5,8);
	} else if (gfnValidatePhoneNumOnly(vtxValue)) {
		//phone number in format nnnnnnnnnnn, convert to standard
		vtxValue = vtxValue.substr(0,3) + "-" + vtxValue.substr(3,3) + "-" + vtxValue.substr(6,4);
		
	} else {
		//value not correct, give error message
		alert(ptxFieldName + " is invalid. Please enter it in one of the following formats:\n\nnnn-nnn-nnnn\n(nnn)nnn-nnnn\nnnn nnn nnnn\nnnnnnnnnnn"); 
		vynReturn = false;
	}
	if (vynReturn) {
		//since the number has been pushed to a standard format and is valid, keep it that way!
		pobControl.value = vtxValue;
	}
	return vynReturn;
}
	
function gfnCheckZipPostal(pobControl,ptxFieldName,ptxCountry) {

	//strip any spaces the user may have entered, to make this easier!
	vtxValue = gfnTrimAllSpaces(pobControl.value);
	vtxValue = vtxValue.toUpperCase();
	
	vynReturn = true;
	
	if (ptxCountry == "CA") {
	
		if (gfnValidatePostalCode(vtxValue)) {
			//passes validation, with spaces removed, so now put central space back
			vtxValue = vtxValue.substr(0,3) + " " + vtxValue.substr(3,3);
		} else {
			//value not correct, give error message
			alert(ptxFieldName + " is invalid. Please enter it in one of the following formats:\n\nana nan\nananan"); 
			vynReturn = false;
		}
	} else if (ptxCountry == "US") {	
		if (gfnValidateZipCode(vtxValue)) {
			//passes validation
		} else {
			//value not correct, give error message
			alert(ptxFieldName + " is invalid. Please enter it in one of the following formats:\n\nnnnnn\nnnnnn-nnnn"); 
			vynReturn = false;
		}
	}
	if (vynReturn) {
		//since the number has been pushed to a standard format and is valid, keep it that way!
		pobControl.value = vtxValue;
	}
	return vynReturn;
}	

function gfnValidateEmail(ptxString,ptxDescription) {
	//note: this is NOT the gfnValidateEmail function from the standard library (which would use a regular expression); it is 
	//instead a copy of the conditions (and more importantly, the resulting error messages) of the email validation done by
	//the function _Field_isEmail in the file validation.js of the qForms library.
	vtxReturn = "";
	
	if( ptxString.indexOf(" ") != -1 ){
		vtxReturn = "Invalid " + ptxDescription + ". An e-mail address should not contain a space.";
	} else if( ptxString.indexOf("@") == -1 ){
		vtxReturn = "Invalid " + ptxDescription + ". An e-mail address must contain the @ symbol.";
	} else if( ptxString.indexOf("@") == 0 ){
		vtxReturn = "Invalid " + ptxDescription + ". The @ symbol can not be the first character of an e-mail address.";
	} else if( ptxString.substring(ptxString.indexOf("@")+2).indexOf(".") == -1 ){
		vtxReturn = "Invalid " + ptxDescription + ". An e-mail address must contain at least one period after the @ symbol.";
	} else if( ptxString.lastIndexOf("@") == ptxString.length-1 ){
		vtxReturn = "Invalid " + ptxDescription + ". The @ symbol can not be the last character of an e-mail address.";
	} else if( ptxString.lastIndexOf(".") == ptxString.length-1 ){
		vtxReturn = "Invalid " + ptxDescription + ". A period can not be the last character of an e-mail address.";
	}

	return vtxReturn;
}

function setQFormError(ptxControlName) {
	
	if (objForm[ptxControlName].obj.style) {
		objForm[ptxControlName].obj.style.backgroundColor = qFormAPI.errorColor;
	}
}

function gfnFormatAsDollars(ptxString) {
	//ptxString = gfnTrimLeadingTrailingSpaces(ptxString);
	if (ptxString.indexOf(".") == -1) {
		//no decimal, add decimal and two zeros
		vtxString = ptxString + ".00";
	} else if (ptxString.indexOf(".") == (ptxString.length - 2)) {
		//decimal followed by one digit, add one zero
		vtxString = ptxString + "0";
	} else {
		vtxString = ptxString;
	}
	return "$" + vtxString;
}

function gfnShowHideObject(ptxObjectName,pynShow) {
	vobObj = document.getElementById(ptxObjectName);
	if (pynShow) {
		vobObj.style.display = "block";
	} else {
		vobObj.style.display = "none";
	}
	return;
}

function gfnGetValueRadioButtons(pobControl) {
	for (vinIndex = 0; vinIndex < pobControl.length; vinIndex++) {
		vtxValue = pobControl[vinIndex].value;
		vynChecked = pobControl[vinIndex].checked;
		if (vynChecked) {
			return vtxValue;
		}
	}
	return "";
}
 
 
 
 
 
 
 
 

