
/*
   Method that validates fields that are common validation fields for
   all validated forms.
*/
function validateBasics(form)
{  
  	// ************ Email Validation **********************  	
	//validate the email address
	if (document.forms[form].txtEmailAddress.value == "")
	{
		window.alert("Please enter your email address.")
		document.forms[form].txtEmailAddress.focus();
		return false;
	}

	// Validate Email Address format
	if (!emailCheck(document.forms[form].txtEmailAddress.value)) {
		alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
		document.forms[form].txtEmailAddress.focus();
		return false;
	}
		
	
	// ***************** Name Fields *********************************
	//first name
	if (document.forms[form].txtFirstName.value == "")
	{
		window.alert("Please enter your first name.")
		document.forms[form].txtFirstName.focus();
		return false;
	}

    //last name				
	if (document.forms[form].txtLastName.value == "")
	{
		window.alert("Please enter your last name.")
		document.forms[form].txtLastName.focus();
		return false;
	}
		
	//if a return of false didn't happen, then the basics are valid	
	return true;
}



//contact us form field validation
function validateFrmContactUs(form)
{ 
  /*
    Validated fields:
      - email address
      - First Name
      - Last Name
      - Comments
  */  	  	
 
    //if the basic form components are valid then do the reset
    if(validateBasics(form))
    {

        //verifies that the text comments are filled out.
        if(document.forms[form].txtComments.value == "")
        {
			window.alert("Please enter comments.");
			document.forms[form].txtComments.focus();
			return false;
        }
        
        //only checks for malicious code/html tags in the comments field
		if( blnHasScript(document.forms[form].txtComments.value) )
		{
			window.alert("The form contains potentially malicious code and cannot be submitted.");
			document.forms[form].txtComments.focus();
			return false;
		}
    }
    else
    {
      //return false, since the basics didn't validate
      return false;
    }
   
   //default - return true
   return true
}



//offers link/page form validation for the email offers
function validateFrmSubscribe(form)
{
  /*
    Validated fields:
      - email address
      - First Name
      - Last Name
  */
  
    //if the basic form components are valid then do the reset
    return validateBasics(form);
}


//gift certificate form validation
function validateFrmGiftCertificate(form)
{
  /*
    Validated fields:
      - email address
      - First Name
      - Last Name
      - Address 1
      - City
      - Zip/Postal Code
      - Country
      - Phone Number
  */

    //if the basic form components are valid then do the reset
    if(validateBasics(form))
    {	
		//check the required address field.
		if(document.forms[form].txtAddress1.value == "")
		{
		   window.alert("Please enter your address.");
		   document.forms[form].txtAddress1.focus();
		   return false;
		}
		
		//check the required city field.
		if(document.forms[form].txtCity.value == "")
		{
		   window.alert("Please enter your city.");
		   document.forms[form].txtCity.focus();
		   return false;
		}		

		// State - make certain that State is supplied when country is US or CA 
		if(document.forms[form].selCountry.value == "US" || document.forms[form].selCountry.value == "CA"){
			if(document.forms[form].selState.value == "")
			{
			    window.alert("Please select your state.");
				return false;
			}
		}


		// Zip - make certain that Zipcode is supplied when country is US or CA
		if(document.forms[form].selCountry.value == "US" || document.forms[form].selCountry.value == "CA"){
			if (document.forms[form].txtZipCode.value == "")
			{
			  window.alert("Please enter your Zip or Postal Code.")
			  document.forms[form].txtZipCode.focus();
			  return false;
			}
		}
			
		// make sure zip code is alphanumeric only (and inludes a hyphen if 10 characters long)
		if(document.forms[form].selCountry.value == "US"){
			if(!isValidZipcode(document.forms[form].txtZipCode.value)){
				alert("The Zip Code entered is not in a valid format.");
				document.forms[form].txtZipCode.focus();
				return false;
			}	
		}
		
		if(document.forms[form].selCountry.value == "")
		{
		  window.alert("Please select your country.");
		  document.forms[form].selCountry.focus();
		  return false;
		}
					
        //validate the phone number
		if(document.forms[form].txtPhone.value == "")
		{
			window.alert("Please enter your telephone number.");
			document.forms[form].txtPhone.focus();
			return false;
		}
		
		// Phone Alphanumeric Check
		if(!isAlphanumeric(document.forms[form].txtPhone.value)){
			window.alert("The phone number is not in a valid format.");
			document.forms[form].txtPhone.focus();
			return false;
		}

    }
    else
    {
      return false;
    }
   
   return true  
}



// central form validation method that decides which core form 
// validation method to run
function validateForm(form)
{
   //get a var that represents the return true of false on the form submittal
   var doSubmit = false;
   
   //evaluate which form type to process
   switch(form)
   {
     case "frmContactUs":
          doSubmit = validateFrmContactUs(form);
          break;
     case "frmSubscribe":
          doSubmit = validateFrmSubscribe(form);
          break;
     case "frmGiftCertificate":
          doSubmit = validateFrmGiftCertificate(form);
          break;
     case "frmEmailOffers":
          doSubmit = validateFrmSubscribe(form);
          break;
     default:
          doSubmit = validateFrmContactUs(form);
          break;          
   }
   
   //return the result
   return doSubmit;
}


function emailCheck(emailStr) {
	// checks if the e-mail address is valid
	//var emailPat = /^(\".*\"|[A-Za-z]\w*)@(\[\d{1,3}(\.\d{1,3}){3}]|[A-Za-z]\w*(\.[A-Za-z]\w*)+)$/;
	var emailPat = /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/;
	var matchArray = emailStr.match(emailPat);
	if (matchArray == null) {
		return false;
	}
	return true;
}

function stripWhitespace(Argument)
{
	Argument = Argument.replace(/ /g,"");
	return Argument;
}

function blnHasScript (inStringx) {
	var tmpString = stripWhitespace(inStringx).toUpperCase();

	if (tmpString.indexOf("<" + "SCRIPT") != -1) return true;
	if (tmpString.indexOf("<" + "/" + "SCRIPT") != -1) return true;
	if (tmpString.indexOf("<" + "?" + "php") != -1) return true;
	if (tmpString.indexOf("?" + ">") != -1) return true;
	if (tmpString.indexOf("language=") != -1) return true;
	return false;
}

function isValidZipcode(val){
	var regex  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	return regex.test(val);
}

function isAlphanumeric(val) {
	var regex = /\(?\d{3}\)?([-\/\.])\d{3}\1\d{4}/;
	return regex.test(val);
}

function newWindow(url,name,features) { 
	var newWin;
	newWin = window.open(url, name, features);
	newWin.focus(); 
}