// JavaScript Document

function contactFormValidator(theForm)
{
 	if (dataInRequiredField('yName','Your name',theForm.id)==false) return (false);	//required
	if (validLetterField('yName','Your Name',theForm.id)==false) return (false);
 	if (dataInRequiredField('yEmail','Your Email',theForm.id)==false) return (false); //required
 	if (validEmailField('yEmail','Your Email',theForm.id)==false) return (false);
 	if (dataInRequiredField('message','Message',theForm.id)==false) return (false);	//required

	return (true);  
}

function tellFormValidator(theForm)
{
 	if (dataInRequiredField('yName','Your name',theForm.id)==false) return (false);	//required
	if (validLetterField('yName','Your Name',theForm.id)==false) return (false);
 	if (dataInRequiredField('yEmail','Your Email',theForm.id)==false) return (false); //required
 	if (validEmailField('yEmail','Your Email',theForm.id)==false) return (false);
 	if (dataInRequiredField('toName','Friend\'s name',theForm.id)==false) return (false);	//required
	if (validLetterField('toName','Friend\'s Name',theForm.id)==false) return (false);
 	if (dataInRequiredField('toEmail','Friend\'s Email',theForm.id)==false) return (false); //required
 	if (validEmailField('toEmail','Friend\'s Email',theForm.id)==false) return (false);

	return (true);  
}

function quickFormValidator(theForm)
{
 	if (dataInRequiredField('firstname','First Name',theForm.id)==false) return (false);	 //required
	if (validLetterField('firstname','First Name',theForm.id)==false) return (false);
	
 	if (dataInRequiredField('lastname','Last Name',theForm.id)==false) return (false);	 //required
	if (validLetterField('lastname','Last Name',theForm.id)==false) return (false);
 
 	if (dataInRequiredField('email','Email',theForm.id)==false) return (false); //required
 	if (validEmailField('email','Email',theForm.id)==false) return (false);
	return (true);  
}

function joinFormValidator(theForm)
{
 	if (dataInRequiredField('fName','First name',theForm.id)==false) return (false);	//required
	if (validLetterField('fName','First Name',theForm.id)==false) return (false);
	
 	if (dataInRequiredField('lName','Last name',theForm.id)==false) return (false);	//required
	if (validLetterField('lName','Last Name',theForm.id)==false) return (false);


 	
	if (dataInRequiredField('email','Your Email',theForm.id)==false) return (false);	//required
 	if (validEmailField('email','Your Email',theForm.id)==false) return (false);
	
	if(theForm.mailYesNo.checked==true){
		if (validAddressFields('street','Street Address','city','City','state','State','zipcode','ZipCode',theForm.id)==false) return (false);	}
	
	if (dataInOptionalField('phone',theForm.id)!=false) {			
		if (validDigitField('phone','Phone Number',theForm.id)==false) return (false);
		if (validLengthField('phone','Phone Number',theForm.id,10)==false) return (false);		}
	
	if (dataInOptionalField('fax',theForm.id)!=false) {			
		if (validDigitField('fax','Fax Number',theForm.id)==false) return (false);
		if (validLengthField('fax','Fax Number',theForm.id,10)==false) return (false);		}
	
 	if (checkboxRequiredField('emailYesNo','Email sign-up acceptance',theForm.id)==false) return (false);	//required

	return (true);  
}


function clubFormValidator(theForm,membershipType)
{
	if (dataInRequiredField('yfirstname','Your Firstname',theForm.id)==false) return (false);	//required
	if (validLetterField('yfirstname','Your Firstname',theForm.id)==false) return (false);
	
	if (dataInRequiredField('ylastname','Your Lastname',theForm.id)==false) return (false);	//required
	if (validLetterField('ylastname','Your Lastname',theForm.id)==false) return (false);

 	
	if (dataInRequiredField('yEmail','Your Email',theForm.id)==false) return (false);	//required
	if (validEmailField('yEmail','Your Email',theForm.id)==false) return (false);
	
	if (validAddressFields('bStreet','Billing Street Address','bCity','Billing City','bState','Billing State','bZipcode','Billing ZipCode',theForm.id)==false) return (false);	
	
 	if (dataInRequiredField('bDayPhone','Billing Day Phone',theForm.id)==false) return (false);		//required
	if (validDigitField('bDayPhone','Billing Day Phone',theForm.id)==false) return (false);
	if (validLengthField('bDayPhone','Billing Day Phone',theForm.id,10)==false) return (false);
	
	if (dataInOptionalField('bEvePhone',theForm.id)!=false) {			
		if (validDigitField('bEvePhone','Billing Evening Phone',theForm.id)==false) return (false);
		if (validLengthField('bEvePhone','Billing Evening Phone',theForm.id,10)==false) return (false);		}
		
	if((membershipType=="Personal" || membershipType=="Both") && theForm.diffShipAdd.checked==true){
		if (validAddressFields('sStreet','Shipping Street Address','sCity','Shipping City','sState','Shipping State','sZipcode','Shipping ZipCode',theForm.id)==false) return (false);	}
	if (dataInRequiredField('cName','Your name on Credit Card',theForm.id)==false) return (false);	
	if (validLetterField('cName','Your Name on Credit Card',theForm.id)==false) return (false);
	
	if (radioRequiredField('cardType','Credit Card Type','cardTypeValue','cName',theForm.id)==false) return (false);	
	
	if (dataInRequiredField('cardNumber','Credit Card Number',theForm.id)==false) return (false);	
	if (validDigitField('cardNumber','Credit Card Number',theForm.id)==false) return (false);
	if (validLengthField('cardNumber','Credit Card Number',theForm.id,16)==false) return (false);

	if(comboboxRequiredField('expMonth','Card Expiration Month',theForm.id)==false) return (false);	

	if(comboboxRequiredField('expYear','Card Expiration Year',theForm.id)==false) return (false);	

	if(membershipType=="Gift" || membershipType=="Both"){
		if (dataInRequiredField('rName','Gift Recipient\'s Name',theForm.id)==false) return (false);	
		if (validLetterField('rName','Gift Recipient\'s Name',theForm.id)==false) return (false);

		if (dataInRequiredField('rEmail','Gift Recipient\'s Email',theForm.id)==false) return (false);
		if (validEmailField('rEmail','Gift Recipient\'s Email',theForm.id)==false) return (false);

		if (validAddressFields('rStreet','Gift Recipient\'s Street Address','rCity','Gift Recipient\'s City','rState','Gift Recipient\'s State','rZipcode','Gift Recipient\'s ZipCode',theForm.id)==false) return (false);			
		
	 	if (dataInRequiredField('rDayPhone','Gift Recipient\'s Day Phone',theForm.id)==false) return (false);	
		if (validDigitField('rDayPhone','Gift Recipient\'s Day Phone',theForm.id)==false) return (false);
		if (validLengthField('rDayPhone','Gift Recipient\'s Day Phone',theForm.id,10)==false) return (false);

		if (dataInOptionalField('rEvePhone',theForm.id)!=false) {			
		if (validDigitField('rEvePhone','Gift Recipient\'s Evening Phone',theForm.id)==false) return (false);
		if (validLengthField('rEvePhone','Gift Recipient\'s Evening Phone',theForm.id,10)==false) return (false);		}

		if (radioRequiredField('numShipments','Number of shipments','numShipsValue','rEvePhone',theForm.id)==false) return (false);	
	}
	
	if (checkboxRequiredField('certify','Age certification acceptance',theForm.id)==false) return (false);	

	if (dataInRequiredField('initials','Initials',theForm.id)==false) return (false);
	if (validLetterField('initials','Initials',theForm.id)==false) return (false);


	if (dataInRequiredField('dob','Your Date of Birth',theForm.id)==false) return (false);	
	if(validDateField('dob','Your Date of Birth',theForm.id)==false) return (false);
	


	
	return (true);  
}



//////////////////////////////////////////////////////////////////////
///////////Common functions for form validations created by Rachna/////
//////////////////////////////////////////////////////////////////////


//function to check for empty fields (required fields)
function dataInRequiredField(fieldName,fieldLabel,theForm){
	if (eval(theForm+'.'+fieldName+'.value') == "")
  {
    alert("Please enter a value for the \""+fieldLabel+"\" field.");
   eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }	
}

//function to get the empty/filled state (optional fields)
function dataInOptionalField(fieldName,theForm){
	if (eval(theForm+'.'+fieldName+'.value') == "")
  {
    return (false);
  }	
}

//function to check whether required checkbox is checked
function checkboxRequiredField(fieldName,fieldLabel,theForm){
	if (eval(theForm+'.'+fieldName+'.checked') == false)
  {
   alert("Please check the \""+fieldLabel+"\" checkbox.");
   eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }	
}

//function to validate the combo box selected option
function comboboxRequiredField(fieldName,fieldLabel,theForm){
	if (eval(theForm+'.'+fieldName+'.selectedIndex') == 0)
  {
   alert("Please selet a  \""+fieldLabel+"\".");
   eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }	
}

//function to validate the radio button
function radioRequiredField(fieldName,fieldLabel,hiddenName,focusField,theForm){
	if (eval(theForm+'.'+hiddenName+'.value') == "")
  {
   alert("Please selet a  \""+fieldLabel+"\".");
   eval(theForm+'.'+focusField+'.focus()');
    return (false);
  }	
}



//function to check for letters only field
function validLetterField(fieldName,fieldLabel,theForm){
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz  ";
  var checkStr = eval(theForm+"."+fieldName+".value");
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter characters in the \"" + fieldLabel + "\" field.");
    eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }
}


//function to check for @ and . in email fields
function validEmailField(fieldName,fieldLabel,theForm){
  if (eval(theForm+"."+fieldName+".value.indexOf('@',0)<0") || eval(theForm+"."+fieldName+".value.indexOf('.',0)<0"))
{
    window.alert("Please enter a valid e-mail address in "+fieldLabel+" field.");
    eval(theForm+'.'+fieldName+'.focus()');
    return false;
}
}

//function to validate address fields street, city, state(combobox no 0 index) and zip(5 digit)
function validAddressFields(street,streetLabel,city,cityLabel,state,stateLabel,zip,zipLabel,theForm){
if(dataInRequiredField(street,streetLabel,theForm)==false) return (false);
if(dataInRequiredField(city,cityLabel,theForm)==false) return (false);
if(comboboxRequiredField(state,stateLabel,theForm)==false) return (false);	
if(dataInRequiredField(zip,zipLabel,theForm)==false) return (false);
if(validDigitField(zip,zipLabel,theForm)==false) return (false);
if(validLengthField(zip,zipLabel,theForm,5)==false) return (false);	
}


//functio to validate the digit only type field
function validDigitField(fieldName,fieldLabel,theForm){
  var checkOK = "0123456789";
  var checkStr = eval(theForm+"."+fieldName+".value");
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only digits in the \"" + fieldLabel + " \" field. No space or dash. ");
    eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }
}

//function to validate the length of the string given the required length as parameter
function validLengthField(fieldName,fieldLabel,theForm,requiredLength){
var checkStr = eval(theForm+"."+fieldName+".value");

 if (checkStr.length!=requiredLength)
  {
    alert("Please enter a "+ requiredLength +" digit \"" + fieldLabel + "\". ");
    eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }
}

function validDateField(fieldName,fieldLabel,theForm){
  var checkOK = "0123456789//";
  var checkStr = eval(theForm+"."+fieldName+".value");
  var allValid = true;
  var validGroups = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only digits or / in the \"" + fieldLabel + " \" field. No space or dash. ");
    eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }
  
   if (checkStr.length!=8)
  {
    alert("Please enter a valid date in format MM/DD/YY e.g. 01/01/07. ");
    eval(theForm+'.'+fieldName+'.focus()');
    return (false);
  }

}



