<!--
 
var CurrencyForThisCreative = "&pound;";
 
var FieldsNotes = new Object();
// FormFields[field name]="lead field specifial thing" 
// lead field specifial thing: 
// 1.  Pre_Process - that field will go thru the pre-validation process
//     method like get digits, stripping space.
//     The method is in the "Pre_Process" function

// 2.  Not required - not required fields (will not go thru the require field check)

// 3.  Additional validate - we have additional validation, beside "require field"
//    the validation method.  Examples:  email check, phone format check
//    The method is in the "AdditionalValidate" function
FieldsNotes["LPhone1"]="Pre_Process, Not required, Additional validate"; 
FieldsNotes["LPhone2"]="Pre_Process, Not required, Additional validate"; 
FieldsNotes["LEmail"]="Pre_Process, Additional validate"; 
FieldsNotes["LFirstName"]="Pre_Process"; 
FieldsNotes["LLastName"]="Pre_Process"; 
FieldsNotes["q30"]="Pre_Process, Post_Process"; 
//FieldsNotes["LCity"]="Not required";    
//FieldsNotes["LPostalCode"]="Not required"; 


// ---------------------------------------------------------------
 // error msg is store into the ErrorMsgTable
 // ErrorMsgTable["fieldName"] = "error msg";
var ErrorMsgTable = new Object();
ErrorMsgTable["LPostalCode"] = "Please fill in your postal code.";
ErrorMsgTable["LCity"] = "Please fill in your city.";
ErrorMsgTable["LProv"] = "Please fill in your state/county/province.";
ErrorMsgTable["q11"] = "Please tell us if you'd like to work as part time or full time";


ErrorMsgTable["LGender"] = "Please indicate whether you are a man or a woman.";
ErrorMsgTable["q12"] = "Please tell us what you are seeking.";
ErrorMsgTable["LAge"] = "Please fill in a valid age.";
ErrorMsgTable["LCountry"] = "Please tell us the country you live in.";
ErrorMsgTable["q13"] = "Please tell us your education level.";
ErrorMsgTable["q14"] = "Please tell us how much time you could invest.";
ErrorMsgTable["q30"] = "Please tell us how much you would like to earn.";
ErrorMsgTable["q15"] = "Please tell us how much you could invest to start.";
ErrorMsgTable["q16"] = "Please tell us how serious you are.";
ErrorMsgTable["q17"] = "Please tell us if you'd like to be your own boss.";
ErrorMsgTable["q18"] = "Please tell us why you want to work from home.";
ErrorMsgTable["q19"] = "Please tell us how soon you could start.";
ErrorMsgTable["q20"] = "Please tell us if you like dealing with people as equals.";
ErrorMsgTable["q21"] = "Do you have enough time to enjoy life? Please tell us.";
ErrorMsgTable["q22"] = "Would a cruise be an ideal vacation? Please tell us.";
ErrorMsgTable["q23"] = "Do you want to retire before 60? Please tell us.";
ErrorMsgTable["q24"] = "Are you concerned about retiring? Please tell us.";
ErrorMsgTable["q25"] = "Please tell us if you want more security for your family.";
ErrorMsgTable["q26"] = "Please tell us about your education.";
ErrorMsgTable["q27"] = "Please tell us about your fitness.";
ErrorMsgTable["q28"] = "Do you know about network marketing? Please tell us.";
ErrorMsgTable["q29"] = "Do you have enough time? Please tell us.";
ErrorMsgTable["LFirstName"] = "Please fill in your first name.";
ErrorMsgTable["LLastName"] = "Please fill in your second name.";
//ErrorMsgTable["LPhone2"] = "Please enter at least one phone or mobile number."; // either the LPhone1 or LPhone2 fields
// invalide phone # length (should be btw 5-20), either the LPhone1 or LPhone2 fields is required
ErrorMsgTable["Phone"] = "Please enter a valid phone number including area code.";  
ErrorMsgTable["LPhone1"] = "Please enter a valid phone number including area code.";  
ErrorMsgTable["LPhone2"] = "Please enter a valid phone number including area code.";  
ErrorMsgTable["LEmail"] = "Please enter a valid e-mail address.";
ErrorMsgTable["LCallTime"] = "Please indicate the best time to reach you.";
ErrorMsgTable["q31"] = "Please indicate how you would prefer to be reached."; // need to confirm the question #
ErrorMsgTable["q32"] = "Please indicate if you own a home."; // need to confirm the question #
ErrorMsgTable["q42"] = "Please tell us do you have a credit card or checking account to transact business over the Internet.";
 
//  no related question for "Please indicate how you would prefer to be reached.";

// ############## end of error msg set up #############################


/*
function continue_button(theForm) {
    if (ValidateForm(theForm)) {
        //theForm.submit.blur();
        window.onunload='';
      //  theForm.submit.disabled=true;
        return true;
     } else {
        return false;
     }
  }
  */

// it will go thur all of the forms elements n validate the form
function ValidateForm(theForm){

  //return (true);
   var result = true;

   // since we are using theForm.elements[i], and we dont' wanna double
   // check the element with the same field name
   var previous_field_name = '';   

   // if it is a not required field, FieldsNotes[fieldname] = "Not require*"
   var not_require=/Not required/;   // not_require filed_tag
   // if it has addition field validation, FieldsNotes[fieldname] = "*Additional validate*"
   var additional_validate = /Additional validate/; 
   // if the field need to have "Before Validate Process", FieldsNotes[fieldname] = "*Pre_Process*"
   var pre_process = /^Pre_Process/; 

   var post_process = /Post_Process/;

   var need_post_process = false;  // run the post process function
   
   for(var i=0; i<theForm.elements.length; i++){
       
       if(theForm.elements[i].type == "text" || 
          theForm.elements[i].type == "textarea" || 
          theForm.elements[i].type == "button" ||
          theForm.elements[i].type == "checkbox" || 
          theForm.elements[i].type == "radio" ||
          theForm.elements[i].type == "select-one"){
           // those are the only field type that we check


           // we only wanna check each field once 
           if (previous_field_name != theForm.elements[i].name) {

// --------- test for Before Validate Process fields -----------------
               if (pre_process.test(FieldsNotes[theForm.elements[i].name])){
                   // Pre_process has func like getting digits only
                   Pre_process(theForm.elements[i]);
               }


// --------- test for require fields -----------------
               if (!not_require.test(FieldsNotes[theForm.elements[i].name])){
                   
                   
                   if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" ){   // || theForm.elements[i].type == "button"
                       //alert("next element : " + i + " name: " + theForm.elements[i].name + " type: "  + theForm.elements[i].type );
                       result = validRequired_Value(theForm.elements[i]);
                   }else if(theForm.elements[i].type == "checkbox" || theForm.elements[i].type == "radio"){
                       //alert("next element : " + i + " name: " + theForm.elements[i].name + " type: "  + theForm.elements[i].type );
                       result = validRequired_CheckBox(theForm.elements[i],theForm);
                   }else if(theForm.elements[i].type == "select-one"){
                       //alert("next element : " + i + " name: " + theForm.elements[i].name + " type: "  + theForm.elements[i].type );
                       result = validRequired_dropDown(theForm.elements[i]);
                   }
                   if (!result) {
                      break;    // don't need to check other test, and striaghtly return false
                   }
    
               }
    
// --------- test for other validation -----------------           
               if (additional_validate.test(FieldsNotes[theForm.elements[i].name])){
                   // we have additional validation, beside "require field"
                   // the validation method is in AdditionalValidate function
                   result = AdditionalValidate(theForm,theForm.elements[i]);
                   if (!result) {
                      break; // don't need to check other test, and striaghtly return false
                   }
    
               }

// ---------------  test if need post process --------------------------------

               if (post_process.test(FieldsNotes[theForm.elements[i].name])){
                   // post process after this form is filled
                  need_post_process = true;
               }

           } // else we have already checked the fields
           previous_field_name = theForm.elements[i].name;

       }// else - other fields don't need to be checked
       
   }  //  for loop

   // --------------  post process -----------------------------------
    if (result == true && need_post_process){
       Post_process(theForm);
    }

    
   return result;

} 




function validRequired_Value(formField)
{
	var result = true;
	if (formField.value == "")
	{
       if (ErrorMsgTable[formField.name]) {
          alert(ErrorMsgTable[formField.name]);
       }else{
          alert('Please enter a value.');
       }
       formField.focus();
	   result = false;
	}
    return result;
}


function validRequired_CheckBox(formField,theForm)
{
  var fieldName = formField.name;
  var fieldType = formField.type;
  var selected = formField.checked;  // nothing is selected

  if (!selected) {
     for(var i=0; i<theForm.elements.length; i++){

        // loop thru to see if one of the element with the same type and name
        if (theForm.elements[i].name == fieldName 
            && theForm.elements[i].type == fieldType
            && theForm.elements[i].checked == true){
           selected = true;  // sth is selected
           break;
        }
     }
  }

  if (selected) {
     return true;
  }else{

     formField.focus();
     if (ErrorMsgTable[formField.name]) {
        alert(ErrorMsgTable[formField.name]);
     }else{
        alert('Please answer the question.');
     }
     return false;  // nothing selected, then return false
  }
}


function validRequired_dropDown(formField)
{
	var result = true;

    if (formField.options[formField.selectedIndex].value == "") {
       if (ErrorMsgTable[formField.name]) {
          alert(ErrorMsgTable[formField.name]);
       }else{
          alert("Please select a choice");
       }
       formField.selectedIndex=0;
       formField.focus();
       result = false;
    }
	
    return result;
}
  
// ###### end of require filed javascript #####################

// ###########  process this before validation  ##################
function Pre_process(formField)
{
    //alert("pre_process " + formField.name);
    if (formField.name == "LPhone1" || formField.name == "LPhone2" || formField.name == "q30"){
        formField.value = getDigits(formField.value);
    }else if(formField.name == "LFirstName" || formField.name == "LLastName" ){
        formField.value = striplt(formField.value);
    }else if(formField.name == "LEmail" ){
        formField.value = striplt(formField.value);
    }else{
        alert("error");
    }

}

// ######################## post process the data #################
function Post_process(theForm){
   if (theForm.q30.type != "hidden"){
      theForm.q30.value = CurrencyForThisCreative + theForm.q30.value;
   } 
}

// ###########  other validation like email check, phone check  ##################
function AdditionalValidate(theForm,formField){

    
    
    // ######################################################
      // q31 is best method to reach
      // make sure the method is filled
      /*var q31_value;
      for (counter = 0; counter < theForm.q31.length; counter++)
      {
         if (theForm.q31[counter].checked){
            q31_value = theForm.q31[counter].value;
         }
      }

      if (q31_value == "a_Phone") {
         if (!validRequired_Value(theForm.LPhone1)){
            return false;
         }
      }else if (q31_value == "c_Mobile Phone") {
         if (!validRequired_Value(theForm.LPhone2)){
            return false;
         }
      } */

      // ##################### either of phone fields is needed ####
    if (formField.name == "LPhone1"){
        var phone_filled = 0;
        if (theForm.LPhone1.value.length > 0) {
            phone_filled ++;
        }
        if (theForm.LPhone2.value.length > 0) {
            phone_filled ++;
        }
        if (phone_filled == 0) {
            alert(ErrorMsgTable["Phone"]);
            theForm.LPhone1.focus();
            return (false);
        }
    }

    // ##################### phone format ###################
    if (formField.name == "LPhone1" || formField.name == "LPhone2" ){
        // if the phone field is filled, then check the format
        if (formField.value.length > 0) {
            if (formField.value.length < 5 || formField.value.length > 20) {
        	//if (ErrorMsgTable[formField.name]){
	        //   alert(ErrorMsgTable[formField.name]);
        	//}else{
                alert(ErrorMsgTable["Phone"]);
                
        	//}
                formField.focus();
                return(false);
            }
        }

   }else if(formField.name == "LEmail"){
  // #####################  email ######################################
       var filter=/^.+@.+\..{2,3}$/;
       if (formField.value.length > 70 || !filter.test(formField.value)){
           alert(ErrorMsgTable["LEmail"]);
           formField.focus();
           return (false);
       }
    
    }else{
        alert("error");
    }

    return true;
}

//########################################################################
// #### below will be the js from old system lead form validation ######

function stripl(aString)
  {
    var newString = "";
    for (i=0; i<aString.length; i++)
      {
        if (aString.charAt(i) != ' ')
          {
            for (j=i; j<aString.length; j++)
              newString=newString+aString.charAt(j);
            return(newString);
          }
      }
    return(newString);
  }

function stript(aString)
  {
    var newString = "";
    var len = aString.length;
    for (i=aString.length-1; i>=0; i--)
      {
        if (aString.charAt(i) == ' ')
          len--;
        else
          {
            for (j=0; j<len; j++)
              newString=newString+aString.charAt(j);
            return(newString);
          }
      }
    return(newString);
  }

function striplt(aString)
  {
    var newString;
    newString = stripl(aString);
    newString = stript(newString);
    return(newString);
  }


function stripa(aString)
{
  var newString = "";
  for (i=0; i<aString.length; i++)
    {
      if (aString.charAt(i) != ' ')
        {
          newString=newString+aString.charAt(i);
        }
    }
  return(newString);
}

function getDigits(aString)
{
  var newString = "";
  for (i=0; i<aString.length; i++)
    {
      if (aString.charAt(i) >= '0' && aString.charAt(i) <= '9')
        {
          newString=newString+aString.charAt(i);
        }
    }
  return(newString);
}



//-->
