/*****************************************************
******************************************************

AREG-CSS site
	Template for CSS sites built with the SiteBuilder tool

	A Compilation of all the javascript used on the site

	Created Date:	9/14/07
	Created By:		scott

******************************************************
******************************************************/


//----------------------------------------------------------------------
//	Javascript Functionality for the ELS Login Module in:
//		modules/ incModELSLogin.asp
//
//	NOTE: gfunIsValidEmailAddress From: gbinc/jsfiles/incValidationAPI.js
//
//	-scott 9/29/06
//  UPDATES
//  -christina 8/14/08 - WO#63421
//  -Christina 10/3/08 , added search validation for photowall wo#64570
//----------------------------------------------------------------------

function funValLogin()
{
	var objForm = g_getRawObject("elsLoginForm");
	var isOK = true;
	if (isOK && !gfunIsValidEmailAddress(objForm.fldUserName.value, false)) {
		alert("Please enter a valid Email Address.\n\r A valid email address should look like yourname@yourdomain.com");
		objForm.fldUserName.focus();
		isOK = false;
	}
	if (isOK && (objForm.fldPassword.value.length < 1)) {
		alert("Please enter a valid Password.");
		objForm.fldPassword.focus();
		isOK = false;
	}
	return isOK;
}

function funLoginPass() {

	var objForm = g_getRawObject("elsLoginForm");

	if (objForm.fldUserName.value.length < 1) {
		alert("Please enter a valid Email Address to send your password to.");
		objForm.fldUserName.focus();
	}
	else {
		objForm.Action.value = "mailpass";
		objForm.submit();
	}
}

//----------------------------------------------------------------------
//	Javascript Functionality for the Poll Module in:
//		modules/ incModPoll.asp
//
//	-scott 6/28/07
//----------------------------------------------------------------------

// Make sure a Choice is selected before submitting the Poll
function funValidatePollMod(objForm) {
	
	var isOK = false;
	var objRadio = objForm.Response;
	
	if (objRadio.length) {
		for (var i = 0; i < objRadio.length; i++) {
			if (objRadio[i].checked) {
				isOK = true;
				break;
			}
		}
	}
	if (!isOK) {
				alert("Please select an answer.");
				}	
	
	return isOK;
}

//----------------------------------------------------------------------
//	Javascript Functionality for the Search Module in:
//		modules/ incModSearchForm.asp
//
//	Requires: gbinc/jsfiles/incCommonAPI.js
//	-scott 9/29/06
//----------------------------------------------------------------------

function funValidateSearchForm(sFormID)
{

	var objForm = g_getRawObject(sFormID);

	var sSearchFor = objForm.searchFor.value;
	
	if (sSearchFor.length < 1) {
		alert("Please enter something to search for.");
		objForm.searchFor.focus();
		return false;
	}
	else {
		objForm.LOOKFOR.value = sSearchFor;
		objForm.keyword.value = sSearchFor;
		objForm.action = objForm.FormAction.options[objForm.FormAction.selectedIndex].value;

		return true;
	}
	
}

//----------------------------------------------------------------------
//	Javascript Functionality for the Search Module in:
//		modules/ incModHomeSectional.asp
//
//	Requires: gbinc/jsfiles/incCommonAPI.js
//	-scott 9/29/06
//----------------------------------------------------------------------

// Global Variable for Rotating Stories Timer
var gnRotateDelay = 5000; // 5 Seconds

function getCurLayer(sModuleID) {
	return eval("sCurLayer" + sModuleID);
}

function setCurLayer(sModuleID, sLayerID) {
	eval("sCurLayer" + sModuleID + " = \"" + sLayerID + "\";");
}

function getIndex(sModuleID) {
	return eval("nCurIndex" + sModuleID);
}

function setIndex(sModuleID, i) {
	eval("nCurIndex" + sModuleID + " = " + i + ";");
}

function funGetNumStories(sModuleID) {
	return eval("nNumStories" + sModuleID);
}

function swapLayers(sModuleID, sLayerID) {
	if (getCurLayer(sModuleID)) hideLayer(getCurLayer(sModuleID));
	showLayer(sLayerID);
	setCurLayer(sModuleID, sLayerID);
}


function showLayer(sLayerID) {
	var lyr = g_getObject(sLayerID);
	if (lyr) {
		if (lyr.css != null) lyr = lyr.css;
		if (lyr.visibility != null) lyr.visibility = "visible";
	}
}

function hideLayer(sLayerID) {
	var lyr = g_getObject(sLayerID);
	if (lyr) {
		if (lyr.css != null) lyr = lyr.css;
		if (lyr.visibility != null) lyr.visibility = "hidden";
	}
}

function initRotatingStories(sModuleID) {
	if (getCurLayer(sModuleID)) hideLayer(getCurLayer(sModuleID));
	var sLayerID = sModuleID + "_story0";
	showLayer(sLayerID);
	setCurLayer(sModuleID, sLayerID);
	setIndex(sModuleID, 0);
}

function RotateLayer(sModuleID) {
	var nIndex = getIndex(sModuleID);
	nIndex++;
	if (nIndex > funGetNumStories(sModuleID)) nIndex = 0;
	swapLayers(sModuleID, sModuleID + "_story" + nIndex );
	eval("objTimer" + sModuleID + " = setTimeout(\"RotateLayer('" + sModuleID + "')\",gnRotateDelay);");
	setIndex(sModuleID, nIndex);
}

function StopRotation(sModuleID) {
	if (window["objTimer" + sModuleID]) {
		clearTimeout(window["objTimer" + sModuleID]);
	}
}

function Prev(sModuleID) {
	StopRotation(sModuleID);
	var nIndex = getIndex(sModuleID);
	nIndex--;
	if (nIndex == -1) nIndex = funGetNumStories(sModuleID);
	swapLayers(sModuleID, sModuleID + "_story" + nIndex );
	setIndex(sModuleID, nIndex);
}

function Next(sModuleID) {
	StopRotation(sModuleID);
	var nIndex = getIndex(sModuleID);
	nIndex++;
	if (nIndex > funGetNumStories(sModuleID)) nIndex = 0;
	swapLayers(sModuleID, sModuleID + "_story" + nIndex );
	setIndex(sModuleID, nIndex);
}

//----------------------------------------------------------------------
//	Javascript Functionality for the CustomForm page in:
//		customform.asp and customformJS.asp
//
//	NOTE: gfunIsValidEmailAddress From: gbinc/jsfiles/incValidationAPI.js
//
//	-patricia 11/18/06 
//	-Christina 8/14/08 WO63421 Add Captcha validation to Customform.asp
//----------------------------------------------------------------------

function ValidateCustomForm(objForm, nSignUpAge)
{
	if (objForm == null) objForm = document.customForm;
	if (nSignUpAge == null) nSignUpAge = 13;
	var isOK = true;
	var today = new Date();
	var theYear = today.getFullYear();
	var theMonth = today.getMonth();
	with (objForm) {
	
		//check for required response questions by looping through form elements and find matching form field name
		var sTemp = "";
		var nQID = 0;
		var objAnswer;
		var nQuestionNumber;
		for (i = 0; i < length; i++) {
			sTemp = elements[i].name;
			if (sTemp.indexOf("QuestionRequired") > -1) {	
				if (elements[i].value == "1") {
					//this question requires response
					nQID = sTemp.substr(16);
					if (!isNaN(nQID)) {
						//get the answer object
						//use try just in case answer object does not exist
						nQuestionNumber = parseInt(nQID) + 1;
						try {
							objAnswer = eval("Question" + nQID + "answer");
							//validate based on type of object
							var nAnswerLen = parseInt(objAnswer.length);
							if (!isNaN(nAnswerLen)) {
								//answer object is collection
								if (objAnswer.type != "select-one") {
									//dropdown does not need to be validated as first option is selected
									isOK = false;
									for (x = 0; x < nAnswerLen; x++) {
										if (objAnswer[x].checked) {
											isOK = true;
										}
									}
									if (!isOK) {
										alert("Please select an answer for question number " + nQuestionNumber);
										objAnswer[0].focus();
										break;
									}
								}
							}
							else {
								//only one answer to the question
								//check for type
								//alert("answer one type: " + objAnswer.type);
								if (objAnswer.type == "text") {
									if (objAnswer.value.length < 1) {
										isOK = false;
										alert("Please enter an answer for question number " + nQuestionNumber);
										objAnswer.focus();
										break;
									}
								}
								if (objAnswer.type == "textarea") {
									if (objAnswer.value.length < 1) {
										isOK = false;
										alert("Please enter an answer for question number " + nQuestionNumber);
										objAnswer.focus();
										break;
									}
								}
								if (objAnswer.type == "radio" || objAnswer.type == "checkbox") {
									//answer type is radio or checkbox and only 1 answer
									if (!objAnswer.checked) {
										isOK = false;
										alert("Please select an answer for question number " + nQuestionNumber);
										objAnswer.focus();
										break;
									}
								}
								//dropdown does not need to be validated as first option is selected
							}
						}
						catch(e) {
							//do nothing
						}
					}	
				}
			}
		}
						
		if (objForm.NameRequired && isOK) {
			if (isOK && (FirstName.value.length < 2)) {
				alert("Please enter your First Name.");
				FirstName.focus();
				isOK = false;
			}
			if (isOK && (LastName.value.length < 2)) {
				alert("Please enter your Last Name.");
				LastName.focus();
				isOK = false;
			}
		}
		if (objForm.AddressRequired && isOK) {
			if (isOK && (Address.value.length < 2)) {
				alert("Please enter your Address.");
				Address.focus();
				isOK = false;
			}
		}
		if (objForm.CityRequired && isOK) {
			if (isOK && (City.value.length < 2)) {
				alert("Please enter the City where you live.");
				City.focus();
				isOK = false;
			}
		}
		if (objForm.StateRequired && isOK) {
			var nStateLen = parseInt(StateSize.value);
			if ( (nStateLen > 0 && State.value.length < nStateLen) || (State.value.length == 0)	) {
				alert("Please enter your " + StateLabel.value + ".");
				State.focus();
				isOK = false;
			}
		}
		if (objForm.PostalCodeRequired && isOK) {
			var nZipLen = parseInt(PostalCodeSize.value);
			if ( (nZipLen > 0 && PostalCode.value.length < nZipLen) || (PostalCode.value.length == 0)	) {
				alert("Please enter your " + PostalCodeLabel.value + ".");
				PostalCode.focus();
				isOK = false;
			}
		}
		if (objForm.CountryCodeRequired) {
			if (isOK && (Country.value.length < 2)) {
				alert("Please enter your Country.");
				Country.focus();
				isOK = false;
			}
		}
		if (objForm.DayPhoneRequired) {
			if (isOK && (DayPhone.value.length < 10)) {
				alert("Please enter your Daytime Phone. (Please include your area code.)");
				DayPhone.focus();
				isOK = false;
			}
		}
		if (objForm.MobilePhoneRequired) {
			if (isOK && (MobilePhone.value.length < 10)) {
				alert("Please enter your Mobile Phone. (Please include your area code.)");
				MobilePhone.focus();
				isOK = false;
			}
		}
		if (objForm.EvePhoneRequired) {
			if (isOK && (EvePhone.value.length < 10)) {
				alert("Please enter your Evening Phone. (Please include your area code.)");
				EvePhone.focus();
				isOK = false;
			}
		}
		if (objForm.BirthdateRequired) {
			if (isOK && (BirthMonth.selectedIndex == 0)) {
				alert("Please select the month of your birth.");
				BirthMonth.focus();
				isOK = false;
			}
			if (isOK && (BirthDay.selectedIndex == 0)) {
				alert("Please select the day of your birth.");
				BirthDay.focus();
				isOK = false;
			}
			if (isOK && (BirthYear.selectedIndex == "" || BirthYear.value == "" || isNaN(BirthYear.value)) ) {
				alert("Please enter the Year of your Birthdate.");
				BirthYear.focus();
				isOK = false;
			}
			else if (isOK) { // Validate Age
				//updated 4/26/06 djb, set correct month&day&year, per wo#31757
				var nAge;
				var dBirthDay = new Date(today);
				dBirthDay.setMonth(BirthMonth.options[BirthMonth.selectedIndex].value,BirthDay.options[BirthDay.selectedIndex].value);
				if (BirthYear.options) {
					dBirthDay.setYear(BirthYear.options[BirthYear.selectedIndex].value);
				}
				else {
					dBirthDay.setYear("/19" + BirthYear.value);
				}
	  
				//Define a variable to hold the anniversary of theBirthdate in the current year
				var dBirthdateThisYear = new Date(today);
				dBirthdateThisYear.setDate(dBirthDay.getDate());
				dBirthdateThisYear.setMonth(dBirthDay.getMonth());
	  
				//Compare current date to dBirthdateThisYear
				//Calculate age based on whether or not we have reached dBirthdateThisYear
				if (today < dBirthdateThisYear) {
					nAge = today.getFullYear() - dBirthDay.getFullYear() - 1;
				}
				else {
					nAge = today.getFullYear() - dBirthDay.getFullYear();
				}
				if (nAge < nSignUpAge) {
					alert("Sorry, you must be at least " + nSignUpAge + " to complete this form.");
					BirthYear.focus();
					isOK = false;
				}
			}
		}
		else if (objForm.BirthYear) {
			if (isOK) {
				var nYear = 0;
				if (BirthYear.options) {
					if (BirthYear.selectedIndex > 0) nYear = BirthYear.options[BirthYear.selectedIndex].value;
				}
				else {
					if (BirthYear.value != "" && !isNaN(BirthYear.value)) nYear = parseInt("19" + BirthYear.value);
				}
				
				if ((theYear - nYear) < nSignUpAge) {
					alert("Sorry, you must be at least " + nSignUpAge + " to complete this form.");
					BirthYear.focus();
					isOK = false;
				}
			}
		}
		if (objForm.GenderRequired) {
			if (isOK && !Gender[0].checked && !Gender[1].checked) {
				alert("Please select your gender.");
				Gender[0].focus();
				isOK = false;
			}
		}
		if (objForm.UserTitleRequired) {
			if (isOK && (UserTitle.value.length < 1)) {
				alert("Please enter a Title.");
				UserTitle.focus();
				isOK = false;
			}
		}
		if (objForm.EmailFromRequired) {
			if (isOK && !gfunIsValidEmailAddress(EmailFrom.value, false)) {
				alert("Please enter a valid Email Address.\n\r A valid email address should look like yourname@yourdomain.com");
				EmailFrom.focus();
				isOK = false;
			}
		}
			// Captcha	
		// Has the imaged loaded?
		if (isOK && document.getElementById("imgCaptcha").complete == false) {
			alert("Please wait till the Validation Code image has loaded.");
			document.getElementById("txtUFCaptchaUserCode").focus();
			isOK = false;
		}				
		// Do they enter a code?
		if (isOK && document.getElementById("txtUFCaptchaUserCode").value.length != 5) {
			alert("Please enter the five character Validation Code.");
			isOK = false;
			document.getElementById("txtUFCaptchaUserCode").focus();
		}
	    // Do they enter a correct code?
        if (isOK && document.getElementById("txtUFCaptchaUserCode").value != document.getElementById("txtUserShouldEnter").value) {
	        alert("Please enter Correct Validation Code.");
		    isOK = false;
	        document.getElementById("txtUFCaptchaUserCode").focus();
	    }
	}
	
	return isOK;
}

//----------------------------------------------------------------------
//	Javascript Functionality for the Multiple Polls form in:
//		showPollJS.asp
//
//	8/14/08 -Christina WO63421 
//----------------------------------------------------------------------


function ValidateShowPoll(objForm){
	if (objForm == null) objForm = document.pollForm;
	var isOK = true;
	with (objForm) {
		 var nPollAnswerLen = parseInt(objForm.Response.length);
		
		if (!isNaN(nPollAnswerLen)) {
				isOK = false;
				for (x = 0; x < nPollAnswerLen; x++) {
					if (objForm.Response[x].checked) {
					isOK = true;
					}
				}
				if (!isOK) {
				alert("Please select an answer.");
				}				
			}
			
			
		// Captcha	
		// Has the imaged loaded?
		if (isOK && document.getElementById("imgCaptchaPoll").complete == false) {
			alert("Please wait till the Validation Code image has loaded.");
			document.getElementById("txtUFCaptchaUserCodePoll").focus();
			isOK = false;
		}				
		// Do they enter a code?
		if (isOK && document.getElementById("txtUFCaptchaUserCodePoll").value.length != 5) {
			alert("Please enter the five character Validation Code.");
			isOK = false;
			document.getElementById("txtUFCaptchaUserCodePoll").focus();
		}
		
		
	}

	return isOK;
}




function funFillForm(objForm) {
//Fill in the Form with the ELS User's Values

	if (objForm == null) objForm = document.customForm;

	with (objForm) {
		if (objForm.FirstName) FirstName.value = sFirstName;
		if (objForm.LastName) LastName.value = sLastName;
		if (objForm.Address) Address.value = sAddress;
		if (objForm.City) City.value = sCity;
		if (objForm.State) State.value = sState;
		if (objForm.PostalCode) PostalCode.value = sPostCode;
		if (objForm.DayPhone) DayPhone.value = sDayPhone;
		if (objForm.EvePhone) EvePhone.value = sEvePhone;
		if (objForm.Country) Country.value = sCountry;
		if (objForm.MobilePhone) {
			MobilePhone.value = sMobilePhone;
			SMSPermission.checked = bSMSPermission;
		}
		if (objForm.EmailFrom) EmailFrom.value = sEmailFrom;
		if (objForm.Gender) {
			if (sGender.toLowerCase() == "m") Gender[1].checked = true;
			else if (sGender.toLowerCase() == "f") Gender[0].checked = true;
		}
		if (sBirthdate != "") {
			//updated 4/26/06 djb, use addition variables, per wo#31757
			var dBirthDate = new Date();
			dBirthDate.setMonth(sBirthdateMonth-1,sBirthdateDay);
			dBirthDate.setYear(sBirthdateYear);
			BirthMonth.selectedIndex = dBirthDate.getMonth()+1;
			BirthDay.selectedIndex = dBirthDate.getDate();
			var TheYear = dBirthDate.getFullYear();
			for (i=0;i<BirthYear.options.length;i++) {
				if (BirthYear.options[i].value == TheYear) {
					BirthYear.selectedIndex = i;
				}
			}
		}
	}
}

//----------------------------------------------------------------------
//	Javascript Functionality for the Email form in:
//		emailformJS.asp
//
//	-patricia 12/05/06 
//  -Christina 8/27/08 , added captcha validation option, per wo#64431
//----------------------------------------------------------------------

function ValidateEmailForm(sFormID)
{
	var isOK = true;
	var objForm = g_getRawObject(sFormID);
	
	if (objForm != null) {
	  with (objForm) {
		if (isOK && (Name.value.length < 2)) {
			alert("Please enter your Name.");
			Name.focus();
			isOK = false;
		}

		if (isOK && ! (EmailFrom.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)) {
			alert("Please enter a valid Email Address.\n\r A valid email address should look like yourname@yourdomain.com");
			EmailFrom.focus();
			isOK = false;
		}

		if (isOK && (Message.value.length < 2)) {
			alert("Please enter a Message.");
			Message.focus();
			isOK = false;
		}
		
		// Captcha	
		// Has the imaged loaded?
		if (isOK && document.getElementById("imgCaptchaEmail").complete == false) {
			alert("Please wait till the Validation Code image has loaded.");
			document.getElementById("txtUFCaptchaUserCodeEmail").focus();
			isOK = false;
		}				
		// Do they enter a code?
		if (isOK && document.getElementById("txtUFCaptchaUserCodeEmail").value.length != 5) {
			alert("Please enter the five character Validation Code.");
			isOK = false;
			document.getElementById("txtUFCaptchaUserCodeEmail").focus();
		}
		

	  }
	}
	
	return isOK;
}

//----------------------------------------------------------------------
//	Javascript Functionality for the Search in:
//		photowall.asp
//
//  -Christina 10/3/08 , added search validation wo#64570
//----------------------------------------------------------------------

function funPhotoWallValidateSearchForm(sFormID)
{

	var objPhotowallForm = g_getRawObject(sFormID);

	var sPhotoSearchString = objPhotowallForm.SearchString.value;
	
	if (sPhotoSearchString.length < 1) {
		alert("Please enter something to search for.");
		objPhotowallForm.SearchString.focus();
		return false;
	}

	
}
