/*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.
*/
  
//to calculate how many participants we have	
function validate(thisForm) {
     var part_age_under19 = false;
     var age_over18 = false;
	 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;
	 var part_name = "traveler";
	 var dp_dt_msg = "Departure Date";
	 var ret_dt = true;
	 
	  with (thisForm) {
	    if (prod_tp.type == "select-one") pr_type = prod_tp[prod_tp.selectedIndex].value;
	    else pr_type = prod_tp.value;
		if (pr_type =="tm" || pr_type =="exc")
		    pr_type = "vac";
	    if(quote_spec.value == "1" && (pr_type == "tg" || pr_type == "stud" || pr_type == "ctzn"))
		 dp_dt_msg = "Effective Date";
	 
	    if (pr_type == "stud")  part_name = "Participant";
		
	    if(pr_type == "tg" || pr_type == "stud")
		{
		  ret_dt = false;
		  }
		  
	 //alert(pr_type);
	 
	    if (trip_departure_date.value =="" ||!dateFormat(trip_departure_date.value)) {
		       alert("Please select a valid "+dp_dt_msg+".");
			   trip_departure_date.focus();
			   return false;
			   }
		  else {
		      
	           d_mm = convertDtStr(trip_departure_date.value,'mm');
	           d_dd = convertDtStr(trip_departure_date.value,'dd');
		       d_yyyy = convertDtStr(trip_departure_date.value,'yy');
			      
			   
		
		    }
	   
		if (ret_dt) {
		    if (trip_return_date.value =="" ||!dateFormat(trip_return_date.value)) {
		         alert("Please enter a valid return date.");
			     return false;
				 trip_return_date.focus();
			   }
		      else {
	             r_mm = convertDtStr(trip_return_date.value,'mm');
		         r_dd = convertDtStr(trip_return_date.value,'dd');
		         r_yyyy = convertDtStr(trip_return_date.value,'yy');
		
		       }
		
		   }
			
		   if (pr_type == "stud" && datesDaysDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd+1,curr_yyyy) < 0){
		         alert("For student products, your must pick a departure date after today.");
				 
			     return false;
		     }
			 
		   if ((pr_type == "tp" ||pr_type == "tm" || pr_type == "vac"|| pr_type == "tg"|| pr_type == "tgChina")&& datesDaysDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd+1,curr_yyyy) < 0){
		         alert("For travel medical and trip protection products, your must pick a departure date after today.");
			     return false;
		    }
		  
		  if ((pr_type == ""||pr_type == "tp" )&& datesDaysDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd,curr_yyyy) < 0){
		        alert(dp_dt_msg+" cannot be earlier than todays date.");
			    return false;
		  
		  }
		   if (ret_dt && datesDaysDiff(r_mm,r_dd,r_yyyy,d_mm,d_dd,d_yyyy) < 0){
		        alert("Return date cannot be earlier than departure date.");
		        return false;
		  
		  }
		  if (pr_type == "tp" && datesDaysDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd,curr_yyyy) > 540){
		        alert("Your departure date must be within 540 days.");
			    return false;
		  }
		   
		   if ((pr_type == "tg" || pr_type == "stud" || pr_type == "vac")&& monthsDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd,curr_yyyy) >= 6){
		        alert("You must select " +dp_dt_msg+" within the next six months.");
			    return false;
		  
		  }
		  if (pr_type == "stud" &&  datesDaysDiff(4,1,2013,d_mm,d_dd,d_yyyy) <= 0){
		       alert("Please pick a date prior to April 1, 2013.");
			   return false;
		  
		  } 
		   if (pr_type == "ctzn" && datesDaysDiff(d_mm,d_dd,d_yyyy,curr_mm,curr_dd,curr_yyyy) > 365){
		        alert("Your departure date must be within the next  year.");
			    return false;
		  
		  } 
		  
		  
		   if (pr_type == "ctzn" && monthsDiff(r_mm,r_dd,r_yyyy,d_mm,d_dd,d_yyyy) < 6){
               alert("Global Citizen plan requires a minimum of 6 months of coverage.");
			   return false;
		  } 
		  
		  if ((pr_type=="vac") && datesDaysDiff(r_mm,r_dd,r_yyyy,d_mm,d_dd,d_yyyy) > 180){
		        alert("The plan is not available for trips that exceed 180 days in length.");
			    return false;
		  
		  }
		   if ((pr_type=="tp") && datesDaysDiff(r_mm,r_dd,r_yyyy,d_mm,d_dd,d_yyyy) > 365){
		        alert("The plan is not available for trips that exceed 365 days in length.");
			    return false;
		  
		  }     
		   if (state_prov.selectedIndex == '0'){
		           alert("Please select a state or province.");
				   state_prov.focus();
		           return false;
		        }
		  if (pr_type == "tp" && total_trip_cost.value == ""){
               alert("Trip Cost is required for Trip Protection Plans. Please enter your trip cost.");
              total_trip_cost.focus();
			  return false; 
           }
          	
		  if (total_trip_cost.type !='hidden' && (total_trip_cost.value != "" &&
!iscost(total_trip_cost.value)))
		   {
		    
			   alert("Please enter the valid trip cost using only numbers (no other symbols).");
               total_trip_cost.focus();
			   return false; 
		   }
		   
		    if (month1.type !='hidden'){
		       if (pr_type == "stud") {
				   if (month1.selectedIndex == 0 || day1.selectedIndex == 0||year1.selectedIndex == 0) {
                      alert("Please enter a date of birth of the "+part_name+".");
					  month1.focus();
				      return false;
				  }
				  var stud_age = parseInt(getAgefromDOB(month1[month1.selectedIndex].value,day1[day1.selectedIndex].value,year1[year1.selectedIndex].value,d_mm,d_dd,d_yyyy));
	              //alert(stud_age);
				   if (stud_age < 14) {
				     alert("You have to be 14  at the coverage start date.");
					 return false;
				   }
				   if (stud_age > 64) {
				   alert("You will be "+stud_age+" at the coverage start date. Individuals older than age 64 are not eligible for Global Student plans.");
					       return false;
				   }
				}
				else
				{
				 if (checkOne_Dob()){
				    alert("Please enter a date of birth of the "+part_name+".");
				           return false; 
				   }
				}
		  }
		  if (age_1.type !='hidden'){
			       if (checkOne_Age()){
				    alert("Please enter an age of the "+part_name+".");
				           return false; 
				   }
				   else if (quote_spec.value == "1" && pr_type == "stud" && age_1.value =="") {
				       alert("Please enter an age of the "+part_name+".");
					       age_1.focus();
				           return false; 
				   }
		  }
		  var trav_count = 0;//to calculate how many participants we have
		  if (pr_type != "stud") {
		  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);
					  if (pr_type == "stud" && i == 1 && age_val < 19)
					      {part_age_under19 = true;}
	                }
				}
				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 (pr_type == "stud" && i == 1 && age_val < 19)
					      {part_age_under19 = true;}  
				}
					//alert(part_age_under19);
			  if(age_flag)
			  {
			  trav_count=trav_count+1;
			  if (Number(age_val) >= 18) 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 (age_1.type !='hidden' && Number(age_val) == 0){
				  alert("Please enter a valid age(s) of the individual(s): For children under 1, enter 1.");
			           return false;
					  }*/
			  else if (pr_type=="vac"  && Number(age_val) > 84){
				      alert("Individuals older than age 84 are not eligible for TravelGap Single Trip.");
					   return false;
		           }
			 else if (pr_type=="ctzn"  && Number(age_val) > 74){
				      alert("Individuals older than age 74 are not eligible for Global Citizen.");
					   return false;
		           }
			 /*else if (pr_type=="vac"  && Number(age_val) > 69  && medical_limit.options[medical_limit.selectedIndex].value > 125000 ){
				       alert("Individuals age 70 - 74 are eligible only for the $125,000 medical limit.");
					   medical_limit.focus();
			           return false;
		              }*/
			 else if (pr_type=="tg" && Number(age_val) > 84){
				           alert("Individuals older than age 84 are not eligible for TravelGap Multi-Trip.");
					       return false;
		               }
			/*else if (pr_type=="tp" && Number(age_val) > 84){
				           alert("Individuals older than age 84 are not eligible for Trip Protection plans.");
					       return false;
		               }*/
			 else if (pr_type=="stud" && Number(age_val) > 64){
				           alert("Individuals older than age 64 are not eligible for HTH's Global Student Health plans.");
					       return false;
		               }
			 else if (quote_spec.value == "1" && pr_type=="stud" && i > 2 && Number(age_val) > 18){
				           alert("Your Child(ren) must be under age 19.");
					       return false;
		               }
			else if (quote_spec.value == "1" && pr_type=="ctzn" && i > 2 && Number(age_val) > 25){
				           alert("Your Child(ren) must be under age 25.");
					       return false;
		               }
			else if (quote_spec.value == "1" && pr_type=="stud" && 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
}// end of Stud vs. other products

	   //alert(total_trip_cost.value/trav_count);
		       if (pr_type == "tp" && ((cost_type[0].checked && total_trip_cost.value > 30000) || (cost_type[1].checked && total_trip_cost.value/trav_count >30000 )))
			   {
			     
			     alert("The Plan is not available for trips whose per person costs exceeds $30,000.");
					       return false;
			   }
			   
		       if ((pr_type == "tp" || pr_type == "vac") && trav_count > 1) {
			      var msg="You must be eligible Family members to apply. Please review eligibility requirements.";
                  var dependtsConfirm = confirm(msg);
				 
                  if (!dependtsConfirm) return false;
                   
			   }
			   if (age_over18== false && pr_type != "stud"){
			     
			      var msg="You must be age 18 or older to purchase HTH insurance products. Adults are permitted to purchase HTH products on behalf of individuals younger than 18.  \n\nBy clicking 'Ok' below, you acknowledge that you are an adult age 18 or older, purchasing on behalf of an individual younger than 18.";
                 //confirm(msg);
			      var ageConfirm = confirm(msg);
				 
                  if (!ageConfirm) return false;
               
                 }
			 
			 if (pr_type=="vac" && datesDaysDiff(r_mm,r_dd,r_yyyy,d_mm,d_dd,d_yyyy) < 6){
		          //var diff = datesDaysDiff_Str(return_date,departure_date);
		          var price_confirm = confirm ("Price quotes include 7 day minimum. While the coverage can be used for trips shorter than 7 days, the price quote you receive will be based on the 7 day minimum.");
                    if (!price_confirm) return false;
				}//main else/if ends
		  
		 
		}//form scope ends
		
		return true;
	}
	//function ends
	
function iscost(x) {

	for (j=0; j<x.length; j++) {
		if ((x.charAt(j) < "0") && (x.charAt(j) != ".") && (x.charAt(j) != "$") && (x.charAt(j) != ",")) {
			return false;
		}
		if ((x.charAt(j) > "9") && (x.charAt(j) != ".") && (x.charAt(j) != "$") && (x.charAt(j) != ",")) {
			return false;
		}
	}
	//check to avoid unvalid cost like 7.000.00
	if (x.indexOf(".",1) != -1 && x.indexOf(".",x.indexOf(".",1)+1)!= -1){
	    return false;
	}
	return true;
}	

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;
}
