/*QUOTE ENGINE FORM VALIDATION. WORKS TOGETHER WITH THE dateValidationUtil.js file for dates validation 
var curr_yyyy,var curr_mm,var curr_dd are taken from CF server, so these values must be defined on the page before quote_validation file is linked.
*/
  
var counter = 0;//to calculate how many participants we have	
function validate(thisForm) {
     var part_age_under19 = false;
     var dep_age_over18 = false;
	 var confirmMsg = "";
	 var t_mm = curr_mm< 10?"0"+curr_mm:curr_mm;
	 var t_dd = curr_dd< 10?"0"+curr_dd:curr_dd;
	 var todaysDate = t_mm +"/"+t_dd+"/"+curr_yyyy;
	 
	  with (thisForm) {
	    //alert(typeof(prd_type_id[1]));alert(typeof(rx_option[1]))
	    if (prd_type_id.type =='hidden') planId = prd_type_id.value;
		else if (typeof(prd_type_id[1]) !='undefined' && prd_type_id[1].type == 'radio' && prd_type_id[1].checked == true) planId = '87';
	    else planId = '83';
	    //alert(planId);
	    //pr_type = prod_tp.value;
		d_mm = dep_month[dep_month.selectedIndex].value;
	    d_dd = dep_day[dep_day.selectedIndex].value;
		d_yyyy = dep_year[dep_year.selectedIndex].value;
		if (d_mm == "" || d_dd == "" || d_yyyy == "" ||  !checkDayMonthYear(d_mm,d_dd,d_yyyy)){
		    alert("Please select a valid effective date.");
			   return false;
			   }
		 else if (datesDaysDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd,curr_yyyy) <= 0){
		    alert("Please enter an effective date greater then todays date.");
			   return false;
		  
		  }
		  else if (datesDaysDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd,curr_yyyy) > 90){
		    alert("You must select an effective date within the next 90 days.");
			   return false;
		  
		  }
		  
		  else if (datesDaysDiff(d_mm,d_dd,d_yyyy,'5','1','2012') >= 0){
		    alert("Rates for policies with effective dates on or after May 1st, 2012 are currently awaiting approval. Please revisit our site within 1-2 weeks.");
			   return false;
		  
		  }
		  else if (typeof(rx_option[0]) !='undefined' && rx_option[0].type == 'radio' && !rx_option[0].checked && !rx_option[1].checked){
		      alert("Please indicate whether or not you want the Optional Prescription Drug Benefit.")
			  return false; 
		  }
		  else if (planId == '83' && mdc_elig[0].checked == true){
		    alert("You have indicated that you are eligible for Medicare but you also want U.S. Coverage. Please note, Medicare provides coverage in the U.S. \n\nPlease indicate that you do not want U.S. coverage and you will be given a Global Citizen EXP quote. Medicare eligible applicants can not buy a plan with U.S. coverage.");
			  prd_type_id[1].focus();
			  return false;
		  }
		  /*else if (deductible_type.selectedIndex == "0"){
		      alert("Please  select  Deductible/Out of Pocket Maximum.");
			  deductible_type.focus();
			  return false; 
		  }*/
		  else if (zip.value == "" || (zip.value!="0" && !checkZip(zip.value))){
				alert("Please enter the valid zip code/postal code in the format '#####'.");
                zip.focus();
			    return false; 
			}
		  
		  else if (month1.type !='hidden'){
			    if (checkOne_Dob()){
				    alert("Please enter a date of birth of the Participant.");
				           return false; 
				   }
				else if (month1.selectedIndex == 0 || day1.selectedIndex == 0||year1.selectedIndex == 0){
                      alert("Please enter a date of birth of the Participant.");
					  month1.focus();
				      return false; 
				   }
			   }
		  else if (age_1.type !='hidden'){
			       if (checkOne_Age()){
				    alert("Please enter an age of the Participant.");
				           return false; 
				   }
				   else if (age_1.value =="") {
				       alert("Please enter an age of the Participant.");
					       age_1.focus();
				           return false; 
				   }
			else if (sex_1.selectedIndex=="0"){
				alert("Please enter the gender of participant.");
                zip.focus();
			    return false; 
			    } 
			 }
			   
		  for(i=1; i<=10; i++) {
		  age_flag = true;
		  
			if (month1.type !='hidden'){
			  var month_value = eval("month"+i+"["+"month"+i+".selectedIndex].value");
			  var day_value = eval("day"+i +"["+"day"+i+".selectedIndex].value");
			  var year_value = eval("year"+i+"["+"year"+i+".selectedIndex].value");
			    if (month_value == "" || day_value == "" || year_value == "")
                    age_flag =false;
	            else if (!checkDayMonthYear(month_value,day_value,year_value)){
                    alert("Please enter the valid date of birth of the individual #"+i+".");
			        return false;
                  }
	            else {
	                  var dob_to_age = getAgefromDOB(month_value,day_value,year_value,curr_mm,curr_dd,curr_yyyy);
	                  var age_val = parseInt(dob_to_age);
	                }
				}
				else if (age_1.type !='hidden') {
				     var age_val = eval("age_"+ i +".value");
					 if (age_val == "") age_flag =false;
					  else
					    if(isNaN(age_val)){
				           alert("Please enter a valid age(s) of the individual(s).");
			               return false;
					   }
					  
				}
					
			  if(age_flag)
			  {
			  if (i == 1 && Number(age_val) < 19) part_age_under19 = true;
			  if (i > 2 && Number(age_val) > 17 && Number(age_val) < 26) 
			  dep_age_over18 = true;
			  //start DOB/AGE validation
			  if (Number(age_val) < 0){
			       
				   alert("The birth date you have entered for individual #"+i+" is in the future. Please edit.");
			           return false;
					  }
			  else if (i == 1 && Number(age_val) < 18){
				      alert("You must be 18 or older if you are individually applying for HTH coverage. Dependents should apply with all family members.");
					   return false;
		           }
			  else if (Number(age_val) > 74){
				      alert("Individuals older than age 74 are not eligible for this plan");
					   return false;
		           }
			  else if (i == 2 && sex_2.selectedIndex=="0"){
			            alert("Please enter your spouce's gender.");
						sex_2.focus();
					    return false;
			 }
			  else if (i > 2 && Number(age_val) > 25){
				           alert("A child older than 25 must apply separately for coverage.");
					       return false;
		               }
			
			else if (i > 1 && part_age_under19){
				           alert("Participants who are age 18 or younger cannot include dependents on the plan. Please edit and/or remove traveler age(s).");
					       return false;
		               }
			//end if DOB/AGE validation
			}
	 }//end of dob/age  loop
		 
		  
			   
/*               if(dep_age_over18 == true)
			      confirmMsg = "If children are 19 through 21 they must be enrolled as full time students.";
                 //confirm(msg);
				 if (confirmMsg != "") {
			       var ageConfirm = confirm(confirmMsg);
                   if (!ageConfirm) return false;
                 }*/
                 
				 //main else/if ends
		  
		
		}//form scope ends
		
		return true;
	}
	//function ends
	

function checkOne_Age(){//function returns true if all 10 age fields are blank
  for(i=1; i<=10; i++) {
   age = eval("document.quote.age_" + i + ".value");
	if (age !="")
	return false;
}
return true;
}
function checkOne_Dob(){//function returns true if all 10 mm/dd/yyyy fields are blank
  for(i=1; i<=10; i++) {
     mm_index = eval("document.quote.month" + i + ".selectedIndex");
     dd_index = eval("document.quote.day" + i + ".selectedIndex");
     yyyy_index = eval("document.quote.year" + i + ".selectedIndex");
	if (mm_index !="0" && dd_index !="0" && yyyy_index !="0")
	return false;
}
return true;
}
