﻿function swapImage(objId,sImage){
	var objRef = document.getElementById(objId);
	objRef.src = sImage;	
}

function resetImg(objId){
    var objRef = document.getElementById(objId);
    objRef.src = sMainProductImage;
}

function preSubmit(sFormName){
//**Preform basic validation prior to submittal of the form.
	//Set the innerHTML of the element that will contain the error text to null
	var objFormRef = document.getElementById("frmMain");

	document.getElementById("divErrors").innerHTML = "<b><span class='error'>The following errors were found in your form:</span></b><br />";

	if (isNullCheck(sFormName) == 0) {
		display('divErrors' , 'none');				
		if (document.getElementById('hosted_button_id')){
		   objFormRef.submit();
		   hideGreetingCard();
		 } else {
		   createElement("hosted_button_id","frmMain"); 
		   objFormRef.submit();
		   hideGreetingCard();
		}
		
	} else {
		display('divErrors' , 'block');
	}
}

function createElement(objId,formID){
    var element = document.createElement("Input");
    var objFormRef = document.getElementById(formID);        
        element.setAttribute("name",objId);
        element.setAttribute("type","hidden");
        element.setAttribute("value",document.getElementById('tempValue').value);        
        objFormRef.appendChild(element);
}

function viewCartSubmit(){
   //Drop the hosted_button_id object from the 
   var objRef = document.getElementById("hosted_button_id");
   var objFormRef = document.getElementById("frmMain");
   objRef.parentNode.removeChild(objRef);   
   objFormRef.submit();    
}

function checkUserPref(objRef){
//Define variables
var dropDownObjRef = document.getElementById(objRef.id);

if ((dropDownObjRef.options[dropDownObjRef.selectedIndex].value != "Yes - Include Card with Message Below")) {
   document.getElementById("os1").disabled = true;
   document.getElementById("os1").style.backgroundColor="#cccccc"; 
   document.getElementById("os1").value = "";
  } else {
    document.getElementById("os1").disabled = false;
    document.getElementById("os1").style.backgroundColor="#ffffff"; 
}
}

/*****************LightBox**********************/
function viewGreetingCard(){
  setVisibility("divGreetingCard","block");
  setVisibility("fade","block");
  }
function hideGreetingCard(){
  setVisibility("divGreetingCard","none");
  setVisibility("fade","none");
}

//****************************Form Scripts*******************************
function userInfoValidation(sFormName){
//Form Validation for the Start Registration page.

var objFormRef = eval("document." + sFormName);
var iErrorCnt = 0;

//Reset contents in divErrors
document.getElementById("divErrors").innerHTML = "<b><span class='error'>The following errors were found in your form:</span></b><br />";

iErrorCnt = isNullCheck(sFormName);
iErrorCnt = iErrorCnt + usernameValidation(sFormName);
iErrorCnt = iErrorCnt + passwordValidation(sFormName);
iErrorCnt = iErrorCnt + emailAddressValidation(sFormName);
iErrorCnt = iErrorCnt + customQuestionValidation(sFormName);

if (iErrorCnt == 0) {
    display('divErrors' , 'none');
    objFormRef.submit();
  } else { display('divErrors' , 'block');}

}//End userInfoValidation

function trimString(sVal){
  return sVal.replace(/(^\s+|\s+$)/g, '');
}

function trimZeros(sVal){
  return sVal.replace(/^([0\s]+)/, '');
}

function isNullCheck(sFormName)
{
//Loop through all text input form objects and check if their respective values are
//greater than null.  If the value is null then call the addErrors function passing the title value
//of the field.  If no element is null then iErrorCnt will be 0 indicating that all
//required fields have been completed.  

//Define variables
var objFormRef = eval("document." + sFormName);
var arrElement;
var iErrorCnt = 0;

for (var iIndex = 0; iIndex < objFormRef.elements.length; iIndex++)
{      
	arrElement = objFormRef.elements[iIndex];	 	         	    
	if ((arrElement.value == "") && (arrElement.type != "hidden") && (arrElement.name != "customSecretQuestion") && ((arrElement.type == "text") || (arrElement.type == "password")))	      
	{        
		addError("Please fill in the " + arrElement.title + " field");
		iErrorCnt = iErrorCnt + 1;
      }                        
	else if ((arrElement.type == "checkbox") && (arrElement.checked == false))	      
	{               
		iErrorCnt = iErrorCnt + 1;
      }                        
	                  

}//end for
	return iErrorCnt;
}//end function

function customQuestionValidation(sFormName)
{
var objRef = eval("document.getElementById('" + sFormName + "')." + 'secretQuestionId');
var objFormRef = eval("document.getElementById('" + sFormName + "')." + 'customSecretQuestion');
iCnt = 0;
   if (objRef.options[objRef.selectedIndex].value == "select")
   {	
     addError("Please select a Security question.");
     iCnt=iCnt+1;
   }        
   if (objRef.options[objRef.selectedIndex].value == "custom")
   {	
	   if (objFormRef.value == "")
	   {	
	     addError("Please provide a Custom security question.");
	     iCnt=iCnt+1;
	   }        
   }        
   return iCnt;
}//end function

function emailAddressValidation(sFormName) 
{   //validate that emails are in proper format and both entries match eachother
	var objFormRef = eval("document." + sFormName);
	var emailAddress = objFormRef.elements['emailAddress']; 
	if(objFormRef.elements['emailAddress1']){var emailAddress1 = objFormRef.elements['emailAddress1'];}
	iCnt = 0;	 
	if (emailAddress.value != "")
	{
		if (emailAddress.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1) 
		{        
			 addError("Invalid e-mail address format.");
			 iCnt=iCnt+1;
		}
	}
	if ((emailAddress1) && ((emailAddress1.value != "") && (emailAddress.value != "") && (emailAddress.value != emailAddress1.value)))
	{
		addError("E-mail addresses do not match.");
		iCnt=iCnt+1;
	}
	return iCnt;	
}//end function

function consumerNumberValidation(sFormName)
{
	//validate they are only using numbers in these three fields
	//
	//Define variables
	var objFormRef = eval("document." + sFormName);
	var consumerNumber = objFormRef.elements['consumerNumber'].value; 
	var validNums = /^[0-9]+$/;
	iCnt = 0;
	
	if (consumerNumber != "")
	{
		if (!consumerNumber.match(validNums))
		{			 
			 addError("Please only use numbers to enter your Member ID.");
			 iCnt=iCnt+1;
		} 					
	}
	return iCnt;
}//end function

function codeValidation(sFormName)
{
	//validate they are only using numbers in this field
	//
	//Define variables
	var objFormRef = eval("document." + sFormName);
	var activationCode = objFormRef.elements['activationCode'].value; 
	var validNums = /^[0-9]+$/;
	if (activationCode != "")
	{
		if (!activationCode.match(validNums))
		{
			 addError("Please only use numbers to enter your Activation Code");
		}
	}
}//end function


function rdoBtnIsChecked(sFormName,sFieldName)
{
//Define variables
var objRadio = eval("document.getElementById('" + sFormName + "')." + sFieldName);     	  		   		   		 
var sChecked = "False";	  

for(var i = 0; i < objRadio.length; i++)
   {		   		
     if (objRadio[i].checked)
      {				   
	sChecked = "True";
      }								    
   }//end radio loop
   return sChecked;
}
function populateDropDown(sFormName,sFieldName)
{
//Define variables
//pre-select the selected dropdown for secret question on pageload.

var objRef = eval("document.getElementById('" + sFormName + "')." + sFieldName);

var selectedOption = document.getElementById("secretQuestionIdpassed").value;
objRef.selectedIndex = selectedOption; 
if (selectedOption == "custom")
	{
		document.getElementById("showCustomQuestion").style.display = "block";
	}
}

function isDropDownSelected(sFormName,sFieldName)
{
//Define variables
var objRef = eval("document.getElementById('" + sFormName + "')." + sFieldName);

if ((objRef.options[objRef.selectedIndex].value == "custom") || (document.getElementById("customSecretQuestion").value != ""))
   {	
     document.getElementById("showCustomQuestion").style.display = "block";	
   }        
if (objRef.options[objRef.selectedIndex].value != "custom")
   {	
     document.getElementById("showCustomQuestion").style.display = "none";
     document.getElementById("customSecretQuestion").value = "";     
   }        
}

// check if character is a number
function isDigit(c)
{
    return ((c >= "0") && (c <= "9"));
}


function checkNonWordChar(s){
/*checks the input string for the existing of any
  non word characters.  Word characters being defined
  as A-Z, a-z, 0-9, and the underscore character.
  
  If a non-word character is found the function returns true
  else false.
  */
var sRegExp = /\w/;
return (sRegExp.test(s));

}

function isDigit(s){
/*checks if the input string is a number.
  If a word character is found the function returns false
  else true.
  */
var sRegExp = /\d/g;
for (var x=0; x < s.length; x++){
    if (!(sRegExp.test(s))){
         return false;
         break;
      }
   }
   return true;
}

function chkLength(iFieldValue,length){
  return (iFieldValue < length);
}

function chkIsNull(objField){
 return (objField.value = "")
}

function clearField(objField)
{
//function clears the value of the form field if the value is equal to the 
//form fields default value.
if (objField.value == objField.defaultValue)
   {      
       objField.value="";
   }
}

function getNextFrmElement(objField,sFormName)
{
//function returns the next form field after the field passed to the funciton
//Define variables
var objFormRef = eval("document." + sFormName);
var objNextElement;
var arrElement;

for (var iIndex = 0; iIndex < objFormRef.elements.length; iIndex++)
   {      
	  arrElement = objFormRef.elements[iIndex];	 	         	    
	  if ((arrElement.name == objField.name) && ((iIndex + 1) <= objFormRef.elements.length))	      
	   {               			   			   			  
		objNextElement = objFormRef.elements[iIndex + 1];					
		break;	   
            }
	     else
	       {
		  objNextElement = "";
		}                        
	}//end for loop
	return objNextElement;
}

function ckMoveNext(iMaxLength,objField,sFormName)
{
var objNextField;
if (objField.value.length == iMaxLength)
   {
      //set focus to next element in form
      objNextField = getNextFrmElement(objField,sFormName);	 
      if (objNextField)
        {
	  objNextField.focus();
	}
   }
}


//**************************End Form Scripts***************************** 

function setVisibility(objId,sStyleProperty)
{  
  var objRef = document.getElementById(objId);  
  objRef.style.display = sStyleProperty;
}

function addError(sErrorText){
  document.getElementById("divErrors").innerHTML += "<span class='error'>&#149;&nbsp;" + sErrorText + "</span><br />";
}

function display(id, v) {
	var obj = document.getElementById(id);
	if (obj && obj.style) obj.style.display = v;
} 

function ghcContentPopUp(sPath){
//Opens pop-up and displays GHC content.  Need a specific function call
      
	var sTempUrl = "";
	var sHost = window.location.hostname;
	var sEnv ="";      

	//determine the enviornment the link is being called from so we can point to the correct ghc resource	
	if (sHost.indexOf("-") != -1) {
	   sEnv = sHost.substring(sHost.indexOf("-"), sHost.indexOf("."));
          }
       
      sTempUrl = "http://www" + sEnv + ".ghc.org" + sPath;
      var launchProcess = window.open(sTempUrl,"_new","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=650,height=500,left=30,top=30");
      launchProcess.focus();
}
function ghcPreviewPopUp(sPath){
//Opens pop-up and displays GHC content.  Need a specific function call
      
	var sTempUrl = "";
	var sHost = window.location.hostname;
	var sEnv ="";      

	//determine the enviornment the link is being called from so we can point to the correct ghc resource	
	if (sHost.indexOf("-") != -1) {
	   sEnv = sHost.substring(sHost.indexOf("-"), sHost.indexOf("."));
          }
       
      sTempUrl = "http://www" + sEnv + ".ghc.org" + sPath;
      var launchProcess = window.open(sTempUrl,"_new","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=860,height=758,left=30,top=30");
      launchProcess.focus();
}
function momentumPopUp(sPath){
//Opens pop-up and momentum (mxv registration) content.   

	var helpWindow = window.open(sPath,"_new","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,width=650,height=500,left=30,top=30");
      helpWindow.focus();
}
function dobValues(isError)
{
	if (isError == 'noErrors')
	{
		window.document.frmMain.elements['dobMonth'].value = 'MM';
		window.document.frmMain.elements['dobDay'].value = 'DD';
		window.document.frmMain.elements['dobYear'].value = 'YYYY';
		 
	}
	 
}
function onUserInfoPageError()
//if there is a system error and this only happens if the page passes all checks. Then copy the bound field emailAddress to the unbound emailAddress1 so user doesnt loose the field value.
{
	window.document.frmMain.elements['emailAddress1'].value = window.document.frmMain.elements['emailAddress'].value; 
}