/*
'===========================================================================
'Name:			ContactForm.js
'Purpose:		Functions for the standard Contact Form class
'Author:		Unknown
'Creation Date:	Unknown
'
'Change History:
'------------------------------------------------------------------------------------
'DATE		AUTHOR			REQUEST #	DESCRIPTION
'09/11/2006	Jacob Stuart	751			Added validation for BirthDate,
'										Relationship, Under18 and OnePhone as well
'										as the bug fix for the PhoneChanged event
'11/03/2006	Jacob Stuart	788			Added requirement for birthday year
'01/20/2007	Susan McDowell	882			Web to Compass UI Changes
'02/09/2007	Jacob Stuart	882			Continued web to compass changes
'04/09/2007	Jacob Stuart	920			Added FormPersist function for saving
'										forms via AJAX
'04/16/2007	John Lamberton	915			Changed "Volunteer" to "Advocate"
'07/05/2007	Cathy Hubin     939			Added strFormPrefix to ChangeStateInfo
'01/18/2008	George Blouin	I35072		Changed ChangeStateInfo to use the Form
'										object instead of objectID because of collision on childbiorequest
'07/02/2008	Sean Higley		1007		Added Property for Email Confirmation
'01/09/2009	Jacob Stuart	1344		Added function for new inline process
'===========================================================================
*/
	var blnFormDataChanged		= false;
	var blnCountryIsUS			= false;
	var blnReqFirstName			= true;
	var blnShowFirstName		= true;
	var blnReqLastName			= true;
	var blnShowLastName			= true;
	var blnShowSponsorNumber	= true;
	var blnReqVolunteerFields	= false;
	var blnShowVolunteerFields	= false;
	var blnReqConType			= false
	var blnReqBusinessGroup		= false;
	var blnShowBusinessGroup	= true;
	var blnReqAddress1			= true;
	var blnShowAddress1			= true;
	var blnReqAddress2			= false;
	var blnShowAddress2			= true;
	var blnReqCity				= true;
	var blnShowCity				= true;
	var blnReqState				= true;
	var blnShowState			= true;
	var blnReqPostalCode		= true;
	var blnShowPostalCode		= true;
	var blnReqCountry			= true;
	var blnShowCountry			= true;
	var blnReqHomePhone			= false;
	var blnReqWorkPhone			= false;
	var blnReqCellPhone			= false;
	var blnReqOnePhone			= false;
	var blnShowPhone			= true;
	var blnShowFax				= false;
	var blnReqFax				= false;
	var blnReqEmail				= false;
	var blnShowEmail			= true;
	var blnShowConfEmail		= false;
	var blnReqBirthDate			= false;
	var blnReqBirthYear			= false;
	var blnShowBirthDate		= false;
	var blnReqRelationship		= false;
	var blnShowRelationship		= false;
	var blnReqOnePhone			= false;
	var blnShowUnder18			= false;
	var blnAllowPACOOption		= false;
	var blnShowSponsorButtons	= false;
	
	var strReadOnlyContact;
	var strUpdateContact;
	var intSponsorNumber;
	var intVolunteerNumber;
	var strStateDropDown;
	var strStateTextBox;
	
    strSpacer = ""; //Defined in Validation.js
	blnImageSpacer = true; //Defined in Validation.js
	
	var arrPACOCountryCode;
	var arrPACOCountryDesc;
	
	function ChangeContactForm(objForm, strURL) {
		objForm.action = strURL + '#SponsorButtons';
		objForm.submit();
	}
	
	function ChangeStateInfo(objForm,strFormPrefix)
	{
		if (! window['blnShow' + strFormPrefix + 'State']) return;
		if(objForm["cbo" + strFormPrefix + "Country"].value == "US")
		{
			hideDiv(strFormPrefix + "StateInfoText");
			showDiv(strFormPrefix + "StateInfoCombo");
			blnCountryIsUS = true;
		} 
		else
		{
			hideDiv(strFormPrefix + "StateInfoCombo");
			showDiv(strFormPrefix + "StateInfoText");
			blnCountryIsUS = false;
		} 
	}
	
	function CheckForPACO(strFormPrefix, objCountrySelection, blnChecked) {
		var strPACOCountryCode = '';
		var strPACOCountryDesc = '';
		var strChecked = '';
		if (objCountrySelection.selectedIndex > 1) {
			strPACOCountryCode = arrPACOCountryCode[objCountrySelection.selectedIndex - 2];
			strPACOCountryDesc = arrPACOCountryDesc[objCountrySelection.selectedIndex - 2];
			}
		if (window['bln' + strFormPrefix + 'AllowPACOOption'] && strPACOCountryCode.length > 0) {
			if (arguments.length == 3) {
				if (blnChecked) strChecked = ' checked="checked"';
				}
			writeLayer(strFormPrefix + 'SendToPACO', '<table border="0" cellpadding="3" cellspacing="0"><tr><td valign="top"><input type="checkbox" name="chk' + strFormPrefix + 'SendToPACO" id="chk' + strFormPrefix + 'SendToPACO" value="True"' + strChecked + ' /></td><td><label for="chk' + strFormPrefix + 'SendToPACO">I would like my account managed by the Compassion office in ' + strPACOCountryDesc + '</label>.<input type="hidden" name="txt' + strFormPrefix + 'PACOCountry" id="txt' + strFormPrefix + 'PACOCountry" value="' + strPACOCountryCode + '" /><input type="hidden" name="txt' + strFormPrefix + 'PACOCountryDesc" id="txt' + strFormPrefix + 'PACOCountryDesc" value="' + strPACOCountryDesc + '" /></td></tr></table>', 'Rewritable');
			}
		else {
			writeLayer(strFormPrefix + 'SendToPACO', '', 'Rewritable');
			}
		} 

	function EmailChanged(objForm, strFormPrefix)
	{
		var objEmail = objForm["cbo" + strFormPrefix + "EmailType"];
		if(objectID("txt" + strFormPrefix + "Email").value == "")
		{
			objEmail.selectedIndex = 0;
			objEmail.disabled = true;
		}
		else
		{
			objEmail.disabled = false;
		}
	}
		
	function PhoneChanged(objForm, strFormPrefix)
	{
		if (! window['blnShow' + strFormPrefix + 'Phone']) return;
		var objPreferredHomePhone = objectID("rdo" + strFormPrefix + "PreferredHomePhone");
		var objPreferredWorkPhone = objectID("rdo" + strFormPrefix + "PreferredWorkPhone");
		var objPreferredCellPhone = objectID("rdo" + strFormPrefix + "PreferredCellPhone");
		if(objectID("txt" + strFormPrefix + "HomePhone").value.length == 0 && objectID("txt" + strFormPrefix + "WorkPhone").value.length == 0 && objectID("txt" + strFormPrefix + "CellPhone").value.length == 0)
		{
			objPreferredHomePhone.disabled = true;
			objPreferredWorkPhone.disabled = true;
			objPreferredCellPhone.disabled = true;
		}
		else
		{
			objPreferredHomePhone.disabled = false;
			objPreferredWorkPhone.disabled = false;
			objPreferredCellPhone.disabled = false;
		}
	}
		
	function FormDataChanged()
	{
		blnFormDataChanged = true;
	}
	
	function SponsorBox(objForm, blnSponsor, strFormPrefix){
		if (blnSponsor == 0) {
			writeLayer(strFormPrefix + "SponsorBox",'',"Rewritable") 
			writeLayer(strFormPrefix + "SponsorNumberError",'&nbsp;',"Rewritable");
		} else {
			writeLayer(strFormPrefix + "SponsorBox",' - Sponsor Number &nbsp;&nbsp;<input type="text" size="15" name="txtSponsorNumber" value="' + intSponsorNumber + '" />',"Rewritable");
			writeLayer(strFormPrefix + "SponsorNumberError","(6 or 7 digit number if handy)","Rewritable");
		}
		if (blnShowState) {
			hideDiv(strFormPrefix + "StateInfoCombo");
			hideDiv(strFormPrefix + "StateInfoText");
		}
		ChangeStateInfo(objForm, strFormPrefix)
	}

   
   function VolunteerBox(objForm, blnVolunteer, strFormPrefix){
		if (blnVolunteer == 0) {
			writeLayer(strFormPrefix + "VolunteerBox",'',"Rewritable") 
			writeLayer(strFormPrefix + "VolunteerNumberError",'&nbsp;',"Rewritable");
		} else {
			writeLayer(strFormPrefix + "VolunteerBox",' - Advocate Number &nbsp;&nbsp;<input type="text" size="15" name="txtVolunteerNumber" value="' + intVolunteerNumber + '" />',"Rewritable");
			writeLayer(strFormPrefix + "VolunteerNumberError",'&nbsp;',"Rewritable");
		}
	}

   
   function UpdateInfo(objForm, strFormPrefix)
	{
	
		if(objForm["chk" + strFormPrefix + "UpdateProfile"].value != 'true')
		{
			hideDiv(strFormPrefix + "ContactFormDefault");
			showDiv(strFormPrefix + "ContactFormUpdate");
			ChangeStateInfo(objForm, strFormPrefix);
			PhoneChanged(objForm, strFormPrefix);
			objForm["chk" + strFormPrefix + "UpdateProfile"].value = 'true';
		}
		else
		{
			hideDiv(strFormPrefix + "ContactFormUpdate");
			showDiv(strFormPrefix + "ContactFormDefault");
			objForm["chk" + strFormPrefix + "UpdateProfile"].value = '';
			if (blnShowState) {
				hideDiv(strFormPrefix + "StateInfoCombo");
				hideDiv(strFormPrefix + "StateInfoText");
			}
		}
	}
	
function SetHiddenValue( strFieldID, strValue ) {
	objectID(strFieldID).value = strValue;
	}	
	
	
function ValidateContactForm(objForm)
	//This will only validate the regular forms. Nothing with prefixes
	{
		ValidateContactFormPrefix(objForm, '');
	}
	
function AttemptLogin(objForm)
	{
		alert("login function");
	}	       
	
function ValidateContactFormPrefix(objForm, strFormPrefix)
	//Validation of Forms with Prefixes
	{
		PhoneChanged(objForm, strFormPrefix);
		// This Phone Check is here to work around an IE bug with
		// the PhoneChanged onBlur event's disabling of radio
		// buttons causing other div layers' text to be cleared.
		// Placing it here causes the state of the radio buttons
		// to not change when it's fired after the validation.
		
	    var strReadOnly = new String("");

    	if (window['blnShow'+strFormPrefix+'Country']) window['bln' + strFormPrefix + 'CountryIsUS'] = (objForm['cbo' + strFormPrefix + 'Country'].selectedIndex==0);

	    if ((! isEmpty(objForm['chk' + strFormPrefix + 'UpdateProfile'])) && (objForm['chk' + strFormPrefix + 'UpdateProfile'].value != 'true')) strReadOnly = "RO";
		
		if ((window['blnShow' + strFormPrefix + 'SponsorButtons']) && (! isEmpty(objForm['cbo' + strFormPrefix + 'Sponsor'])) && (objForm['cbo' + strFormPrefix + 'Sponsor'].length > 0)) ValidateRadio(objForm['cbo' + strFormPrefix + 'Sponsor'], objForm['cbo' + strFormPrefix + 'Sponsor'].length, strFormPrefix + 'SponsorError', 'Please tell us if you have donated to Compassion in the past.');
	    if ((window['blnShow' + strFormPrefix + 'FirstName']) && (window['blnReq' + strFormPrefix + 'FirstName']) && (! isEmpty(objForm['txt' + strFormPrefix + 'FirstName']))) ValidateNonBlank(objForm['txt' + strFormPrefix + 'FirstName'], strFormPrefix + 'FirstNameError' + strReadOnly);
	    if ((window['blnShow' + strFormPrefix + 'LastName']) && (window['blnReq' + strFormPrefix + 'LastName']) && (! isEmpty(objForm['txt' + strFormPrefix + 'LastName']))) ValidateNonBlank(objForm['txt' + strFormPrefix + 'LastName'], strFormPrefix + 'LastNameError' + strReadOnly);
	    if ((window['blnShow' + strFormPrefix + 'BusinessGroup']) && (window['blnReq' + strFormPrefix + 'BusinessGroup']) && (! isEmpty(objectID('txt' + strFormPrefix + 'BusinessGroup')))) ValidateNonBlank(objForm['txt' + strFormPrefix + 'BusinessGroup'], strFormPrefix + 'BusinessGroupError' + strReadOnly);
	    if ((window['blnShow' + strFormPrefix + 'Relationship']) && (window['blnReq' + strFormPrefix + 'Relationship']) && (! isEmpty(objForm['txt' + strFormPrefix + 'Relationship']))) ValidateNonBlank(objForm['txt' + strFormPrefix + 'Relationship'], strFormPrefix + 'RelationshipError' + strReadOnly);
	    if ((window['blnShow' + strFormPrefix + 'SponsorNumber']) && isEmpty(objForm['chk' + strFormPrefix + 'UpdateProfile']) && (! isEmpty(objForm['txt' + strFormPrefix + 'SponsorNumber']))) ValidateSponsorNumber(objForm['txt' + strFormPrefix + 'SponsorNumber'], strFormPrefix + 'SponsorNumberError' + strReadOnly, false);//Only check if not logged in
		if ((window['blnShow' + strFormPrefix + 'VolunteerFields']) && (window['blnReq' + strFormPrefix + 'VolunteerFields']) &&  (! isEmpty(objForm['txt' + strFormPrefix + 'VolunteerNumber']))) ValidateNonBlank(objForm['txt' + strFormPrefix + 'VolunteerNumber'], strFormPrefix + 'VolunteerNumberError' + strReadOnly);
		if ((window['blnShow' + strFormPrefix + 'Address1']) && (window['blnReq' + strFormPrefix + 'Address1'])) ValidateNonBlank(objForm['txt' + strFormPrefix + 'Address1'], strFormPrefix + 'AddressError' + strReadOnly);
		if ((window['blnShow' + strFormPrefix + 'Address2']) && (window['blnReq' + strFormPrefix + 'Address2'])) ValidateNonBlank(objForm['txt' + strFormPrefix + 'Address2'], strFormPrefix + 'Address2Error' + strReadOnly);
		if ((window['blnShow' + strFormPrefix + 'City']) && (window['blnReq' + strFormPrefix + 'City'])) ValidateNonBlank(objForm['txt' + strFormPrefix + 'City'], strFormPrefix + 'CityError' + strReadOnly);
		if ((window['blnShow' + strFormPrefix + 'Country']) && (window['blnReq' + strFormPrefix + 'Country'])) ValidateCountry(objForm['cbo' + strFormPrefix + 'Country'], strFormPrefix + 'CountryError' + strReadOnly);
		if (window['bln' + strFormPrefix + 'CountryIsUS']) {
			if ((window['blnShow' + strFormPrefix + 'State']) && (window['blnReq' + strFormPrefix + 'State'])) ValidateCombo(objForm['cbo' + strFormPrefix + 'State'], strFormPrefix + 'StateError' + strReadOnly);
			if (window['blnShow' + strFormPrefix + 'PostalCode']) {
				if (window['blnReq' + strFormPrefix + 'PostalCode']) {
					if (! ValidateNonBlank(objForm['txt' + strFormPrefix + 'PostalCode'], strFormPrefix + 'ZipError' + strReadOnly)) {
						ValidateUSPostalCode(objForm['txt' + strFormPrefix + 'PostalCode'], strFormPrefix + 'ZipError' + strReadOnly);
					}
				}
				else {
					ValidateUSPostalCode(objForm['txt' + strFormPrefix + 'PostalCode'], strFormPrefix + 'ZipError' + strReadOnly);
				}
			}
			if (window['blnShow' + strFormPrefix + 'Phone']) {
				ValidateUSPhone(objForm['txt' + strFormPrefix + 'HomePhone'], strFormPrefix + 'HomePhoneError' + strReadOnly, window['blnReq' + strFormPrefix + 'HomePhone']);
				ValidateUSPhone(objForm['txt' + strFormPrefix + 'WorkPhone'], strFormPrefix + 'WorkPhoneError' + strReadOnly, window['blnReq' + strFormPrefix + 'WorkPhone']);
				ValidateUSPhone(objForm['txt' + strFormPrefix + 'CellPhone'], strFormPrefix + 'CellPhoneError' + strReadOnly, window['blnReq' + strFormPrefix + 'CellPhone']);
			}
			if (window['blnShow' + strFormPrefix + 'Fax']) ValidateUSPhone(objForm['txt' + strFormPrefix + 'Fax'], strFormPrefix + 'FaxError' + strReadOnly, window['blnReq' + strFormPrefix + 'Fax']);
		}
		else {
			if ((window['blnShow' + strFormPrefix + 'State']) && (window['blnReq' + strFormPrefix + 'State'])) ValidateNonBlank(objForm['txt' + strFormPrefix + 'State'], strFormPrefix + 'StateError' + strReadOnly);
			if ((window['blnShow' + strFormPrefix + 'PostalCode']) && (window['blnReq' + strFormPrefix + 'PostalCode'])) ValidateNonBlank(objForm['txt' + strFormPrefix + 'PostalCode'], strFormPrefix + 'ZipError' + strReadOnly);
			if (window['blnShow' + strFormPrefix + 'Phone']) {
				ValidateGenericPhone(objForm['txt' + strFormPrefix + 'HomePhone'], strFormPrefix + 'HomePhoneError' + strReadOnly, window['blnReq' + strFormPrefix + 'HomePhone']);
				ValidateGenericPhone(objForm['txt' + strFormPrefix + 'WorkPhone'], strFormPrefix + 'WorkPhoneError' + strReadOnly, window['blnReq' + strFormPrefix + 'WorkPhone']);
				ValidateGenericPhone(objForm['txt' + strFormPrefix + 'CellPhone'], strFormPrefix + 'CellPhoneError' + strReadOnly, window['blnReq' + strFormPrefix + 'CellPhone']);
			}
			if (window['blnShow' + strFormPrefix + 'Fax']) ValidateGenericPhone(objForm['txt' + strFormPrefix + 'Fax'], strFormPrefix + 'FaxError' + strReadOnly, window['blnReq' + strFormPrefix + 'Fax']);
		}
		if (window['blnShow' + strFormPrefix + 'Phone']) {
			ValidatePhoneExtn(objForm['txt' + strFormPrefix + 'WorkPhoneExtn'], strFormPrefix + 'WorkPhoneExtnError' + strReadOnly, false);
			ValidateWorkPhoneandExtn(objForm['txt' + strFormPrefix + 'WorkPhone'], objForm['txt' + strFormPrefix + 'WorkPhoneExtn'], strFormPrefix + 'WorkPhoneandExtnError' + strReadOnly);
			ValidatePreferredPhone(objForm['txt' + strFormPrefix + 'HomePhone'], objForm['txt' + strFormPrefix + 'WorkPhone'], objForm['txt' + strFormPrefix + 'CellPhone'], objForm['rdo' + strFormPrefix + 'PreferredPhone'], strFormPrefix + 'PreferredPhoneError' + strReadOnly);
			if (window['blnReq' + strFormPrefix + 'OnePhone']) {
				var normalizedHomePhone = stripCharsInBag(objForm['txt' + strFormPrefix + 'HomePhone'].value, phoneNumberDelimiters);
				var normalizedWorkPhone = stripCharsInBag(objForm['txt' + strFormPrefix + 'WorkPhone'].value, phoneNumberDelimiters);
				var normalizedCellPhone = stripCharsInBag(objForm['txt' + strFormPrefix + 'CellPhone'].value, phoneNumberDelimiters);
				if ((!isNonnegativeInteger(normalizedHomePhone)) && (!isNonnegativeInteger(normalizedWorkPhone)) && (!isNonnegativeInteger(normalizedCellPhone))) {
					ShowTheErrorMessage(strFormPrefix + 'OnePhoneError' + strReadOnly, 'Please supply at least one valid phone number.', true);
					}
				else {
					ShowTheErrorMessage(strFormPrefix + 'OnePhoneError' + strReadOnly, '', false);
					}
				}
		}
		
		if (window['blnShow' + strFormPrefix + 'Email']) if (window['blnReq' + strFormPrefix + 'Email']) {
			ValidateAnEmailRequired(objForm['txt' + strFormPrefix + 'Email'], strFormPrefix + 'EmailError' + strReadOnly);
		} else {
			ValidateAnEmail(objForm['txt' + strFormPrefix + 'Email'], strFormPrefix + 'EmailError' + strReadOnly)
		}
		//Confirmation Email
		if (window['blnShow' + strFormPrefix + 'ConfEmail']) {
			if (objForm['txt' + strFormPrefix + 'ConfEmail'].value.toLowerCase() != objForm['txt' + strFormPrefix + 'Email'].value.toLowerCase()) {
				ShowTheErrorMessage(strFormPrefix + 'ConfEmailError' + strReadOnly, 'The e-mail addresses you entered do not match.<br />Please try again.', true);
			}
		}
	    if ((window['blnShow' + strFormPrefix + 'BirthDate']) && (window['blnReq' + strFormPrefix + 'BirthDate'])) {
			(objForm['cbo' + strFormPrefix + 'BirthDateMonth'].selectedIndex <= 0)
			? ShowTheErrorMessage(strFormPrefix + 'BirthDateMonthError' + strReadOnly, 'Please choose your birth month', true)
			: ShowTheErrorMessage(strFormPrefix + 'BirthDateMonthError' + strReadOnly, '', false);
			(objForm['cbo' + strFormPrefix + 'BirthDateDay'].selectedIndex <= 0)
			? ShowTheErrorMessage(strFormPrefix + 'BirthDateDayError' + strReadOnly, 'Please choose your birth day', true)
			: ShowTheErrorMessage(strFormPrefix + 'BirthDateDayError' + strReadOnly, '', false);
			}
	    if ((window['blnShow' + strFormPrefix + 'BirthDate']) && (window['blnReq' + strFormPrefix + 'BirthYear'])) {
			(objForm['cbo' + strFormPrefix + 'BirthDateYear'].selectedIndex <= 0)
			? ShowTheErrorMessage(strFormPrefix + 'BirthDateYearError' + strReadOnly, 'Please choose your birth year', true)
			: ShowTheErrorMessage(strFormPrefix + 'BirthDateYearError' + strReadOnly, '', false);
			}
	}

function ClearContactForm( objForm ) {
	//--------------------------------------------------------------------------------------
	//	Function:	ClearContactForm
	//	Purpose:	If FormPersistByAjax is enabled, we want to allow the user to clear all
	//				of the form fields. Using form.reset() only sets them back to their value
	//				as it was when the form was loaded.
	//--------------------------------------------------------------------------------------
	for (var intIndex = 0; intIndex < objForm.elements.length; intIndex++) {
		switch(objForm.elements[intIndex].type) {
			case 'select-one':
			case 'select-multiple':
				objForm.elements[intIndex].selectedIndex = 0;
				break;
			case 'checkbox':
				objForm.elements[intIndex].checked = false;
				break;
			case 'radio':
				switch(objForm.elements[intIndex].name.toLowerCase()) {
					case 'cbosponsor':
						break;
					default:
						objForm.elements[intIndex].checked = false;
					}
				break;
			case 'hidden':
			case 'submit':
			case 'button':
			case 'image':
				// Do nothing
				break;
			default:
				objForm.elements[intIndex].value = '';
			}
		}
	}


function ToggleForm( strShow, strHide, strFormName, strAction, strFormPage ) {
	$('#div' + strHide).hide();
	$('#div' + strShow).show();
	document.forms[strFormName].onsubmit = new Function('return validate' + strShow + 'Form(document.' + strFormName + ')');
	document.forms[strFormName].action = strAction;
	document.forms[strFormName].txtLoginProcess.value = strShow;
	// Submit Login Process value
	var strFormNameValues = 'FormPage=' + encodeURIComponent(strFormPage) + '&txtLoginProcess=' + strShow;
	XMLHttpPost( '/Includes/Functions/FormPersist.asp', strFormNameValues );
	return false;
}
