<!--

function maximizeWin() {
  if (window.screen) {
    var aw = screen.availWidth;
    var ah = screen.availHeight;
    window.moveTo(0, 0);
    window.resizeTo(aw, ah);
  }
}


function submitForm(theForm){
   theForm.submit()
   window.onunload='';
}

function no_pop(){
   window.onunload='';
}

function changeAction(theForm,newAction){
   theForm.action=newAction;
}

function QualifyFormSubmit(theForm,newAction,forceSurvey){
   theForm.forceProfile.value=forceSurvey;
   theForm.action=newAction;
   var validate = continue_button(theForm);
   if (validate) {
      theForm.submit();
   }
   if (!forceSurvey ) {
      if (validate) {
         return true;
      }else{
         return false;
      }
   }
   //return false;
}

function addBookmark() {

    var title = document.title;
    var url = document.URL;

    if (window.sidebar) {

      window.sidebar.addPanel(title, url,"");

    } else if( document.all ) {

      window.external.AddFavorite( url, title);

    } else if( window.opera && window.print ) {
      return true;
    }
  }
  function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// this function will be called if checked a program OR selected a loss weight lbs
function show_information(theForm){
   //alert('show');
   // if validate the form
   if (ValidateForm(theForm,0)) {
      var today=new Date();
      date = today.getMonth()+1+"/"+today.getDate()+"/"+today.getYear();
       document.getElementById('today').innerHTML=date;

       document.getElementById('age').style.display = '';
       document.getElementById('gender').style.display = '';
       document.getElementById('country').style.display = '';

       if (document.getElementById('weight')) {
          document.getElementById('weight').style.display = '';
       }
       if (document.getElementById('goalWeight')) {
          document.getElementById('goalWeight').innerHTML = get_goal_weight(theForm);
       }

       if (document.getElementById('skin_type')) {
          var my_skin_type = theForm.q62.options[theForm.q62.selectedIndex].value;
          if (my_skin_type) {
             var my_answer = my_skin_type.split("_");
             document.getElementById('skin_type').innerHTML=my_answer[1];
          }
       }

       if (document.getElementById('hair_type')) {
          var my_hair_type = theForm.q76.options[theForm.q76.selectedIndex].value;
          if (my_hair_type) {
             var my_answer = my_hair_type.split("_");
             document.getElementById('hair_type').innerHTML=my_answer[1];
          }
       }

       if (document.getElementById('bmi_result')) {
          document.getElementById('bmi_result').style.display = '';
       }
       document.getElementById('program').style.display = '';
       //document.getElementById('program').innerHTML=get_program(theForm,theForm.q7);
   }
}

//######################
function get_goal_weight(theForm){
//######################
   var current_weight = theForm.LWeight.value; // in kg
   var loss_weight = theForm.q5.options[theForm.q5.selectedIndex].value;
   if (loss_weight) {
      var my_answer = loss_weight.split("_");
      //alert("my_answer " + my_answer);
      var my_value = my_answer[1];
      //alert("my_value " + my_value);
      var my_extracted_value = my_value.split(" "); // no %
      var loss_weight = getDigits(my_extracted_value[0]);
      
      if (loss_weight.length == 0) {
         // if this is not a digit - e.g. None, loss_weight = 0
         loss_weight = 0;
         my_extracted_value[1] = 'lbs';
      }

      if (my_extracted_value[1] == 'lbs' ) {
         // unit is in kgs -> convert the unit to lbs
         current_weight = Math.round(current_weight*2.2);
      }
      return current_weight-loss_weight + ' ' + my_extracted_value[1];
   }else{
      return '';
   }
        //alert(my_extracted_value[0] + ' 2nd ' + my_extracted_value[1]);
        //return  parseFloat(my_extracted_value[0]);
}

/*
// get WL program that have tried
//######################
function get_program (theForm){
//######################
   var program = '';
   
   var program_num = 0;
   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 == 'q7' 
          && theForm.elements[i].checked == true){
            var my_answer = theForm.elements[i].value.split("_");
            if (program_num) {
               // more than one program - 3 in a row
               if (program_num % 3) {
                  program += ', ' + my_answer[1];
               }else{
                  program += '<BR>' + my_answer[1];
               }
            }else{
               // first program
               program = my_answer[1];
            }
            program_num++;
      }
  }
   return program;
} */


function continue_button(theForm) {

   if (ValidateForm(theForm,1)) {
      window.onunload='';
          return true;
      } else {          
          return false;
      }
}



function calculate_BMI(theForm){

   var validate = 0;
   if (theForm.LHeight.type == 'hidden' ||
       validRequired_dropDown(theForm.LHeight)){
      if (theForm.LHeight.type == 'hidden' ||
          validRequired_dropDown(theForm.LWeight)) {
          validate = 1;
      }
   }

   if (validate) {
         //theForm.BMI.value = Math.round((theForm.LWeight.value)/(theForm.LHeight.value*theForm.LHeight.value)*10000*100)/100;
         //my_BMI = Math.round(theForm.BMI.value);
         //my_BMI = Math.round((theForm.LWeight.value)/(theForm.LHeight.value*theForm.LHeight.value)*10000*100)/100;
         my_BMI = Math.round((theForm.LWeight.value)/(theForm.LHeight.value*theForm.LHeight.value)*10000);
         //alert(my_BMI);
        
         document.getElementById('default').style.display = 'none';
         document.getElementById('underweight').style.display = 'none';
         document.getElementById('normalweight').style.display = 'none';
         document.getElementById('overweight').style.display = 'none';
         document.getElementById('obese').style.display = 'none';

         document.getElementById('underweight_result').style.display = 'none';
         document.getElementById('normalweight_result').style.display = 'none';
         document.getElementById('overweight_result').style.display = 'none';

         document.getElementById('bmiResult').innerHTML = 'Il tuo BMI è ' + my_BMI + '.';
         //my_BMI = Math.round(my_BMI);
         if (my_BMI < 19) {
            document.getElementById('underweight').style.display = '';
            document.getElementById('underweight_result').style.display = '';
         }else if (19 <= my_BMI && my_BMI < 25) {
            document.getElementById('normalweight').style.display = '';
            document.getElementById('normalweight_result').style.display = '';
         }else if (25 <= my_BMI && my_BMI < 30 ) {
            document.getElementById('overweight').style.display = '';
            document.getElementById('overweight_result').style.display = '';
         }else {
            document.getElementById('obese').style.display = '';
            document.getElementById('overweight_result').style.display = '';
         }
   }

   
}



 
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["q40"]="Pre_Process, Additional validate";   // confirm email
FieldsNotes["LFirstName"]="Pre_Process"; 
FieldsNotes["LLastName"]="Pre_Process"; 
FieldsNotes["LAddress"]="Not required, Additional validate";
//FieldsNotes["q30"]="Pre_Process, Post_Process"; 
FieldsNotes["BMI"]="Not required"; 
//FieldsNotes["q41"]="Not required"; // CSS + term filed 
//FieldsNotes["LCity"]="Not required";    
//FieldsNotes["LPostalCode"]="Not required"; 

FieldsNotes["LSelectProv"] = "Not required, Additional validate";
FieldsNotes["LEnterProv"] = "Not required";
FieldsNotes["bannerOptions"] = "Additional validate";


FieldsNotes["Q2"]="Not required"; // If you rated the customer experience as Unsatisfactory, would you please tell us what happened
FieldsNotes["tipo_allergia"]="Not required, Additional validate"; 
FieldsNotes["q98"]="Not required, Additional validate"; 
FieldsNotes["Q12"]="Not required"; 




// ---------------------------------------------------------------
 // error msg is store into the ErrorMsgTable
 // ErrorMsgTable["fieldName"] = "error msg";
var ErrorMsgTable = new Object();



ErrorMsgTable["q41"] = "Prego accetta i Termini del Servizio."; // CSS + term filed 

ErrorMsgTable["LWeight"] = "Prego seleziona il tuo peso.";
ErrorMsgTable["LHeight"] = "Prego seleziona la tua altezza.";
ErrorMsgTable["LCompany"] = "Prego seleziona la compagnia.";
ErrorMsgTable["q89"] = "Prego seleziona la compagnia.";

ErrorMsgTable["LEnterProv"] = "Please enter a state/county/province.";
ErrorMsgTable["LSelectProv"] = "Please select a state/county/province.";




ErrorMsgTable["LCalltime"] = "Please indicate the best time to reach you.";
ErrorMsgTable["bannerOptions"] = "Please select one choice.";
ErrorMsgTable["LMarketing2"] = "Prego indica se vuoi ricevere informazioni per email.";
ErrorMsgTable["LMarketing1"] = "Prego indica se già conosci Herbalife.";
ErrorMsgTable["LMarketing"] = "Prego indica come ci hai trovato.";
ErrorMsgTable["LProv"] = "Please fill in your state/county/province.";
ErrorMsgTable["LSesso"] = "Prego indica il tuo sesso.";
ErrorMsgTable["LEta"] = "Prego inserisci la tua età.";
ErrorMsgTable["LProvincia"] = "Prego indica la tua provincia di residenza.";
ErrorMsgTable["LFirstName"] = "Prego inserisci il tuo nome.";
ErrorMsgTable["LName"] = "Please fill in your name.";
ErrorMsgTable["LLastName"] = "Prego inserisci il tuo cognome.";
//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"] = "Prego inserisci il tuo numero di telefono incluso prefisso.";  
ErrorMsgTable["LPhone1"] = "Please enter a valid phone number including area code.";  
ErrorMsgTable["LPhone2"] = "Please enter a valid phone number including area code.";  
ErrorMsgTable["LEmail"] = "Prego inserisci un indirizzo e-mail valido.";
ErrorMsgTable["Email"] = "Attenzione l'Email non è lo stesso.  Prego controlla.";
ErrorMsgTable["q40"] = "Prego ripeti il tuo email.";
ErrorMsgTable["LContatto"] = "Prego indica il tuo orario migliore per contattarti.";
ErrorMsgTable["LAddress"] = "Prego inserisci un indirizzo.";
ErrorMsgTable["LComune"] = "Prego inserisci il tuo comune di residenza."; 

// --- profile
// WL
ErrorMsgTable["q37"] = "Please tell us how would you rate your activity level.";
ErrorMsgTable["q38"] = "Please tell us where do you exercise.";
ErrorMsgTable["q9"] = "Please tell us if you had success with calorie-controlled diets in the past.";
ErrorMsgTable["q10"] = "Please tell us if you are satisfied with regularly spaced meals.";
ErrorMsgTable["q11"] = "Please tell us if you like the idea of having some of each food group.";
ErrorMsgTable["q12"] = "Please tell us if you tend to gain weight around the midsection of your body.";
ErrorMsgTable["q13"] = "Please tell us if you tend to get hungry throughout the day.";
ErrorMsgTable["q14"] = "Please tell us if you enjoy proteins such as meats, poultry, fish and eggs.";
ErrorMsgTable["q5"] = "Please tell us how much weight do you want to lose.";
ErrorMsgTable["q6"] = "Please tell us why do you want to lose weight.";
ErrorMsgTable["q7"] = "Please tell us which weight loss programs have you tried.";

ErrorMsgTable["q90"] = "Please tell us if you would like to improve the appearance of your skin.";
ErrorMsgTable["q91"] = "Please tell us if you would like to improve the look & feel of your hair.";
ErrorMsgTable["q92"] = "Please tell us if you eat regularly spaced meals.";  
ErrorMsgTable["bilanciamento_alimenti"] = "Prego indica se bilanci i gruppi di alimenti.";  
ErrorMsgTable["frutta_verdura"] = "Prego indica ogni quanto mangi frutta e verdura e cibi poco grassi.";  
ErrorMsgTable["fonte_proteine"] = "Prego indica la tua maggior fonte di proteine.";  
ErrorMsgTable["allergie"] = "Prego indica se hai allergie alimentari.";  
ErrorMsgTable["q97"] = "Please tell us if you had success with any of the above mentioned weight loss programs in the past.";  
ErrorMsgTable["q98"] = "Please tell us which one.";  
ErrorMsgTable["tipo_allergia"] = "Prego specifica la tua allergia."; 
ErrorMsgTable["effetto_yoyo"] = "Prego indica se il tuo peso oscilla.";  
ErrorMsgTable["stanchezza"] = "Prego indica se avverti mai stanchezza.";  
ErrorMsgTable["q102"] = "Please tell us what is your complexion.";  
ErrorMsgTable["q103"] = "Please tell us if you are looking for a skin care solution to help you avoid premature ageing.";  
ErrorMsgTable["q104"] = "Please tell us if you have any of the following skin conditions.";  
ErrorMsgTable["q105"] = "Please tell us if you wouldlike to pay extra attention to any of the following.";  
ErrorMsgTable["q106"] = "Please tell us if anyone in your family has a major skin problem.";  
ErrorMsgTable["q107"] = "Please tell us what is your hair type.";  
ErrorMsgTable["q108"] = "Please tell us how often do you swim in a chlorinated pool.";  
ErrorMsgTable["q109"] = "Please tell us what best describes your hair.";  
ErrorMsgTable["q110"] = "Please tell us if any of the following hair conditions affect you.";  
ErrorMsgTable["q111"] = "Please tell us how many hours, on average, do you sleep at night.";  
ErrorMsgTable["q112"] = "Please tell us if you balance your intake of each food group.";  
ErrorMsgTable["q113"] = "Please tell us if anyone in your family has a history of nutritional/diet problems.";  
ErrorMsgTable["q114"] = "Please tell us what best describes your present fitness activity.";  
ErrorMsgTable["q115"] = "Please tell us if anyone in your family has a history of chronic physical ailments.";  


/*ErrorMsgTable["q116"] = "Please tell us how much would you be willing to invest in a weightloss solution that really works for you.";  
ErrorMsgTable["q117"] = "Please tell us how much would you be willing to invest in a weight management solution that really works for you.";  
ErrorMsgTable["q118"] = "Please tell us how much would you be willing to invest in a skin care solution that really works for you.";  
ErrorMsgTable["q119"] = "Please tell us how much would you be willing to invest in a hair care solution that really works for you.";  
ErrorMsgTable["q120"] = "Please tell us how much would you be willing to invest in a nutrition solution that really works for you.";  
ErrorMsgTable["q121"] = "Please tell us how much would you be willing to invest in a fitness solution that really works for you.";  
*/

ErrorMsgTable["q122"] = "Please tell us how much would you be willing to budget in a weightloss solution that really works for you.";  
ErrorMsgTable["q123"] = "Please tell us how much would you be willing to budget in a weight management solution that really works for you.";  
ErrorMsgTable["q124"] = "Please tell us how much would you be willing to budget in a skin care solution that really works for you.";  
ErrorMsgTable["q125"] = "Please tell us how much would you be willing to budget in a hair care solution that really works for you.";  
ErrorMsgTable["q126"] = "Please tell us how much would you be willing to budget in a nutrition solution that really works for you.";  
ErrorMsgTable["q127"] = "Please tell us how much would you be willing to budget in a fitness solution that really works for you.";  



   
// WM

ErrorMsgTable["movimento"] = "Prego indica quanto tempo pratichi sport.";
ErrorMsgTable["fumo"] = "Prego indica se sei un fumatore.";
ErrorMsgTable["stress"] = "Prego indica il tuo livello di stress.";
ErrorMsgTable["q44"] = "Please tell us if you had success with weight management programs in the past.";
ErrorMsgTable["q45"] = "Please tell us if you have a well-balanced diet.";
ErrorMsgTable["q46"] = "Please tell us if you eat fruits, vegetables and low fat foods.";

// Nutrition
ErrorMsgTable["q47"] = "Please tell us how would you rate your sleeping.";
ErrorMsgTable["q48"] = "Please tell us how would you rate your daily energy level.";
ErrorMsgTable["q49"] = "Please tell us if you have indigestion/constipation problems.";
ErrorMsgTable["q50"] = "Please tell us if you have gained weight in the last 12 months.";
ErrorMsgTable["q51"] = "Please tell us if anyone in your family have nutrition/diet history.";

// Fitness
ErrorMsgTable["q52"] = "Please tell us what is the main reason you exercise.";
ErrorMsgTable["q53"] = "Please tell us how would you rate your fitness level.";
ErrorMsgTable["q54"] = "Please tell us if your exercise programme is working.";
ErrorMsgTable["q55"] = "Please tell us if you mix cardio exercise with resistance training.";
ErrorMsgTable["q56"] = "Please describe your typical fitness or gym session.";
ErrorMsgTable["q57"] = "Please tell us which of the following fitness activities you enjoy most.";

// Quality
ErrorMsgTable["q58"] = "Please tell us your choice of free beauty profile.";
ErrorMsgTable["q59"] = "Please tell us if you currently have a hair problem.";
ErrorMsgTable["q60"] = "Please tell us if you currently have a skin problem.";
ErrorMsgTable["q61"] = "Please tell us if you know your skin type.";
ErrorMsgTable["q62"] = "Please tell us to the best of your knowledge what type is your skin.";
ErrorMsgTable["q63"] = "Please tell us if you get red or blotchy skin.";
ErrorMsgTable["q64"] = "Please tell us if you burn easily.";
ErrorMsgTable["q65"] = "Please tell us how would you rate your skin complexion relative to how easy it usually tans/burns in the sun.";
ErrorMsgTable["q66"] = "Please tell us if you are looking for a skin care solution that will help you to avoid premature aging.";
ErrorMsgTable["q67"] = "Please tell us what is your skin concern.";
ErrorMsgTable["q68"] = "Please tell us if you would require special attention because of one of the following skin conditions.";
ErrorMsgTable["q69"] = "Please tell us if you have eye puffiness that may require attention.";
ErrorMsgTable["q70"] = "Please tell us if anyone in your family had major skin problems.";
ErrorMsgTable["q71"] = "Please tell us if you are a smoker.";
ErrorMsgTable["q72"] = "Please tell us if you spend a lot of time in the sun.";
ErrorMsgTable["q73"] = "Please tell us if your daily diet includes vitamins.";
ErrorMsgTable["q74"] = "Please tell us how would you rate your weekly activity level.";
ErrorMsgTable["q75"] = "Please tell us if you know your hair type.";
ErrorMsgTable["q76"] = "Please tell us how does your hair feel.";
ErrorMsgTable["q77"] = "Please tell us what is the texture of your hair.";
ErrorMsgTable["q78"] = "Please tell us if you treat or color your hair.";
ErrorMsgTable["q79"] = "Please tell us what kind of shampoo do you use.";
ErrorMsgTable["q80"] = "Please tell us if you use conditioner every time you wash your hair.";
ErrorMsgTable["q81"] = "Please tell us if your hair is straight or curly.";
ErrorMsgTable["q82"] = "Please tell us what is your hair concern.";
ErrorMsgTable["q83"] = "Please tell us if you would require special attention because of one of the following hair conditions.";
ErrorMsgTable["q84"] = "Please tell us if if anyone in your family had major hair problems.";


// choice page
ErrorMsgTable["q39"] = "Please tell us your choice.";
ErrorMsgTable["q85"] = "Please tell us your choice.";
ErrorMsgTable["q86"] = "Please tell us your choice.";
ErrorMsgTable["q87"] = "Please tell us your choice.";
ErrorMsgTable["q88"] = "Please tell us your choice.";


// other
ErrorMsgTable["LFeedback"] = "Please enter your feedback.";
ErrorMsgTable["AdsQ1"] = "Please tell us what is your advertising budget.";
ErrorMsgTable["AdsQ2"] = "Please tell us what do you advertise.";
ErrorMsgTable["AdsQ3"] = "Please tell us why do you want to advertise with us.";
ErrorMsgTable["AdsQ4"] = "Please tell us when do you want to start your advertising campaign.";
ErrorMsgTable["AdsQ5"] = "Please tell us how often do you visit GLB.com.";
ErrorMsgTable["LJobTitle"] = "Please tell us your job title.";
ErrorMsgTable["LCompanyName"] = "Please tell us your company name.";
ErrorMsgTable["LCompanyURL"] = "Please tell us your company URL.";
ErrorMsgTable["LIndustry"] = "Please tell us your industry.";
ErrorMsgTable["LAddress"] = "Please tell us your address.";


// CSS
ErrorMsgTable["Q1"] = "Please tell us in thinking about your most recent contact with Herbalife, was the quality of the overall experience.";
ErrorMsgTable["Q2"] = "If you rated the customer experience as Unsatisfactory, would you please tell us what happened?.";
ErrorMsgTable["Q3"] = "Please tell us when did the Fitness Consultant contact you.";
ErrorMsgTable["Q4"] = "Please tell us if your consultant was very courteous.";
ErrorMsgTable["Q5"] = "Please tell us if the Consultant handled the call quickly.";
ErrorMsgTable["Q6"] = "Please tell us if the Consultant was very knowledgeable.";
ErrorMsgTable["Q7"] = "Please tell us if the Consultant listened to my needs.";
ErrorMsgTable["Q8"] = "Please tell us how satisfied are you with the opportunity.";
ErrorMsgTable["Q9"] = "Please tell us if you would like us to match you with a new offering.";
ErrorMsgTable["Q10"] = "Please tell us how soon after you requested information did a consultant contact you?? ";
ErrorMsgTable["Q11"] = "Please tell us how would you rate the overall quality of service you received from the representative that contacted you?";




function changeCompanyName(selectedCompanyName){

   if (document.getElementById('acknowledgement')) {
      var myString = CompanyName[selectedCompanyName];
      rExp = /COMPANY_NAME/gi;   // search for COMPANY_NAME, replace with myString
      var context = acknowledgement;
      document.getElementById('acknowledgement').innerHTML = context.replace(rExp,myString);
      document.getElementById('acknowledgement').style.display = '';
   }
}



//  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,error_msg){
    
  //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" || 
          theForm.elements[i].type == "select-multiple" ){
           // 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],error_msg);
                   }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,error_msg);
                   }else if(theForm.elements[i].type == "select-one" || 
                            theForm.elements[i].type == "select-multiple"){
                       //alert("next element : " + i + " name: " + theForm.elements[i].name + " type: "  + theForm.elements[i].type );
                       result = validRequired_dropDown(theForm.elements[i],error_msg);
                   }
                   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],error_msg);
                   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,error_msg)
{
	var result = true;
	if (formField.value == "")
	{
       if (error_msg) {
          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,error_msg)
{
  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{

     if (error_msg) {
        formField.focus();
        if (ErrorMsgTable[formField.name]) {
           alert(ErrorMsgTable[formField.name]);
        }else{
           alert('Please answer the question.' + formField.name);
        }

     }
     
     return false;  // nothing selected, then return false
  }
}


function validRequired_dropDown(formField,error_msg)
{
   var result = true;
   if (formField.selectedIndex < 0 || 
       formField.options[formField.selectedIndex].value == "" ) {
       if (error_msg) {
          if (ErrorMsgTable[formField.name]) {
             alert(ErrorMsgTable[formField.name]);
          }else{
             alert("Please select a choice");//  + formField.name);
             
          }
          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 if(formField.name == "q40" ){
        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;
   } 
}

var email_confirm_error = 0;

// ###########  other validation like email check, phone check  ##################
function AdditionalValidate(theForm,formField,error_msg){

    
    
    // ######################################################
      // 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) {
           if (error_msg) {
              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(!(checkphone(theForm, formField.value))){
                formField.focus();
                return(false);
            }
        }
   }else if(formField.name == "LEmail"){
  // #####################  email ######################################
      //var filter=/^.+@.+\..{2,3}$/;
      var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;

       if (formField.value.length > 70 || !filter.test(formField.value)){
          if (error_msg) {
             alert(ErrorMsgTable["LEmail"]);
             formField.focus();
          }
           
           return (false);
       }
   }else if(formField.name == "q40"){
      
       var filter=/^.+@.+\..{2,3}$/;
       if (formField.value.length > 70 || !filter.test(formField.value)){
          if (error_msg) {
             alert(ErrorMsgTable["q40"]);
             formField.focus();
          }
           
           return (false);
       }

       
       if (formField.value != theForm.LEmail.value &&
          email_confirm_error < 3) {
         if (error_msg) {
             alert(ErrorMsgTable["Email"]);
             formField.focus();
         }
         email_confirm_error++;
          return (false);
       }
    }else if(formField.name == 'LSelectProv'){
       if (theForm.LGeo.value == 1) {
         // prov - select drop down
          if (theForm.LSelectProv.options[theForm.LSelectProv.selectedIndex].value == ""){
             alert(ErrorMsgTable["LSelectProv"]);
             theForm.LSelectProv.selectedIndex=0;
             theForm.LSelectProv.focus();
             return (false);
          }
      }else{
         // enter prov
         if (theForm.LEnterProv.value.length < 1) {
            alert(ErrorMsgTable["LEnterProv"]);
            theForm.LEnterProv.focus();
            return(false);
         }
      }
    }else if (formField.name == 'bannerOptions'){
       // select one option only 
       var selected = 0;
       for (var i=0; i<formField.length;i++){
          if (formField.options[i].selected == true) {
             selected++;
          }
       }
       if (selected > 1) {
            alert(ErrorMsgTable[formField.name]);
            formField.options[0].focus();
            return(false);
       }
    }else if (formField.name == 'tipo_allergia'){

       var allergyValue;
       for(var i=0; i<theForm.allergie.length; i++){
          // loop thru to which element is selected
          if (theForm.allergie[i].checked == true){
             allergyValue = theForm.allergie[i].value;
             break;
          }
       }

       var allergyValue_Yes=/^b_/;
       if (allergyValue_Yes.test(allergyValue)){
          // allergy
          if (formField.value.length < 1) {
             // specified field
             alert(ErrorMsgTable[formField.name]);
             formField.focus();
             return(false);
          }
       }

    }else if (formField.name == 'q98'){

       var successValue;
       for(var i=0; i<theForm.q97.length; i++){
          // loop thru to which element is selected
          if (theForm.q97[i].checked == true){
             successValue = theForm.q97[i].value;
             break;
          }
       }
       var successValue_Yes=/^a_/;
       if (successValue_Yes.test(successValue)){
          // allergy
          if (formField.value.length < 1) {
             // specified field
             alert(ErrorMsgTable[formField.name]);
             formField.focus();
             return(false);
          }
       }


       
    }else if (formField.name == 'LAddress'){

       if (theForm.Postman) {
          // we have post man field -> address is required
          if (formField.value.length < 1) {
             alert(ErrorMsgTable[formField.name]);
             formField.focus();
             return(false);
          }
       } 
         // otherwise, it is not requied -> Lead form
    }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 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);
}



//-->

