<!--  ************************************************************
//** System						:   B2C				       
//** File Name					:   validate_search_entries.js       			       
//** Author	           		    :   old file          		
//** Purpose 	 				: 	to validate entries given by user
//** Version					:                       		         			
//** Modification History 		:                           				
//** Sr.     Date           Modified by 	Why and What is Modified		
//** 1      08/11/2006                               					        
//** 2      28/aug/2006     Anil            put comment  
//** 3      31/Aug/2006     Anil            code changed for parameter in funtion startdate and enddate  
//** 4      31/Aug/2006     Anil            done validation code for multi-city 
//** 5      01/sep/2006     Anil            done changes in validation code for multi-city   
//** 6      14/dec/2006     Anil            made changes for date validation for 3 day's constraint 
//**************************************************************  -->


// validate_search_entries.js

//FUNTION TO TOGGLE A DIVISION TAG HIDE AND SHOW
var maxDayToBook = 335;
var one_day=1000*60*60*24

var indexM = 0;
var check=0;


function resetDate(obj)
  {
   obj.value="";
   return true;
  }


  function dispHandle(obj) {
       
      
    
	if (obj.style.display == "none")
	    {
	    
	    obj.style.display = "";
		}
	else
	    {
	   	    
		obj.style.display = "none";
		}
		
		if(obj.id == "expandforthflight")
		{
		  
		  document.getElementById("expandfifthflight").style.display = "";
		}
		
		if(obj.id == "expandfifthflight")
		{
		  
		  document.getElementById("expandsixthflight").style.display = "";
		}
		if(obj.id == "sixthflight" || obj.id == "fifthflight" || obj.id == "forthflight")
		{
		  indexM = indexM + 1 ;		  
		}
  }

//FUNTION TO TOGGLE A DIVISION TAG HIDE AND SHOW END'S HERE



//SWITCHING BETWEEN ONEWAY, ROUNDTRIP AND MULTICITY ACCORDING TO PRIVIOUS SEARCH
function selectCurrent(obj){

if(obj!=null)
{
var newTripType = obj;
}else{
var newTripType = 'F';

}
			if(newTripType=="F")
                       {
                          document.getElementById('RtripTypea').checked=true;
                          document.getElementById('roundtrip').style.display = "";
                          document.getElementById('multicity').style.display = "none";
                          document.getElementById('oneway').style.display = "none";
                                           
					      
                       }
                     if(newTripType=="C")
                      {
                        document.getElementById('OtripTypeb').checked=true; 
                      	document.getElementById('roundtrip').style.display = "none";
                      	document.getElementById('oneway').style.display = "";
                        document.getElementById('multicity').style.display = "none";
                                                    
                       }
                      if(newTripType=="M")
                      {
                          document.getElementById('MtripTypec').checked=true;
                          document.getElementById('multicity').style.display = "";
                          document.getElementById('oneway').style.display = "none";
                          document.getElementById('roundtrip').style.display = "none"; 
                       }

}



//SWITCHING BETWEEN ONEWAY AND ROUNDTRIP  FOR SEARCH START'S HERE
function defaulttrip()
{
     
    
     if(document.getElementById('OtripTypeb').checked)
    {
      document.getElementById('roundtrip').style.display = "none";
      document.getElementById('multicity').style.display = "none";
      document.getElementById('oneway').style.display = "";
     }
     if(document.getElementById('MtripTypec').checked)
    {
      document.getElementById('roundtrip').style.display = "none";
      document.getElementById('multicity').style.display = "";
      document.getElementById('oneway').style.display = "none";
     }
     if(document.getElementById('RtripTypea').checked)
    {
      document.getElementById('roundtrip').style.display = "";
      document.getElementById('multicity').style.display = "none";
      document.getElementById('oneway').style.display = "none";
     }
}
function selectdiv(obj) {
                                 
                    obj.checked=false;
                   
                    if(obj.value=="F")
                       {
                          document.getElementById('RtripTypea').checked=true;
                          document.getElementById('roundtrip').style.display = "";
                          document.getElementById('multicity').style.display = "none";
                          document.getElementById('oneway').style.display = "none";
                                           
					      
                       }
                     if(obj.value=="C")
                      {
                        document.getElementById('OtripTypeb').checked=true; 
                      	document.getElementById('roundtrip').style.display = "none";
                      	document.getElementById('oneway').style.display = "";
                        document.getElementById('multicity').style.display = "none";
                                                    
                       }
                      if(obj.value=="M")
                      {
                          document.getElementById('MtripTypec').checked=true;
                          document.getElementById('multicity').style.display = "";
                          document.getElementById('oneway').style.display = "none";
                          document.getElementById('roundtrip').style.display = "none"; 
                       }

}

//SWITCHING BETWEEN ONEWAY AND ROUNDTRIP  FOR SEARCH END'S HERE



//CALENDER CODE START'S HERE
var gd;                         

var tyear;
var tmonth;
var tday;
var syear;
var smonth;
var sday;
var lyear;
var lmonth;
var lday;


function validateStartDate(obj)
{

 tyear=datesplit(obj.value,2);
 tmonth=datesplit(obj.value,0);
 tday=datesplit(obj.value,1);

}


function today(obj)
{
               
gd= new Date(obj.year.value,obj.month.value,obj.date.value);

 tyear =gd.getFullYear();
 tmonth=gd.getMonth();
 tday  =gd.getDate();
 
}



function datesplit(dt,i)
{

var sdate = dt.split("/");

       switch(i)
       {
        case 0:
               return parseInt(sdate[0],10);
               break;
        case 1:
               return parseInt(sdate[1],10);
               break;
        case 2:
               return parseInt(sdate[2],10);
               break;
       }  

}

                   


                        //THIS FUNCTION CROSS CHECKS FOR DEPARTURE DATE WITH RETURN DATE
                                   function startdate(obj)
                                   {
                                     
                                     if(obj.value=='')
                                       {
                                       
                                         syear=tyear+1;
                                         smonth=tmonth ;
                                         sday=tday ;
                                         
                                         
                                       }
                                       else
                                       {
                                        syear=datesplit(obj.value,2);
                                        smonth=datesplit(obj.value,0);
                                        sday=datesplit(obj.value,1);
                                        }
                                   }
                          //THIS FUNCTION CROSS CHECKS FOR RETURN DATE WITH DEPARTURE DATE        
                                   function enddate(obj)
                                   {
                                      
                                      if(obj.value=='')
                                        {
                                         
                                         lyear= tyear; 
                                         lmonth=tmonth;
                                         lday=tday;
                                        }
                                      else 
                                        {
                                         lyear=datesplit(obj.value,2);
                                         lmonth=datesplit(obj.value,0);
                                         lday=datesplit(obj.value,1);
                                         
                                        }    
                                  }     
                                  

//CALENDER CODE END'S HERE


//VALIDATION FOR SEARCH START'S HERE
function validateEntries(obj) {
 

//FOR ROUND TRIP

if(obj.tripType.value=="F" || obj.tripType.value=="C")
{

 if( validateForRT(obj) == false ) 
  { 
      return false; 
      }
 

}



//FOR MULTI CITY TRIP
if(obj.tripType.value=="M")
{

 if( validateForMC(obj) == false ) 
  { 
      return false; 
      }
 }
// Issue 614| All messages Changed
	var adults = obj.adult.value;
	var child = obj.child.value;
	var senior = obj.senior.value;
	var youth = obj.youth.value;
	var infantL = obj.infantInLap.value;
	var infantS = obj.infantInSeat.value;
if (adults == 0 && child == 0 && senior == 0 && youth == 0 && infantL == 0 && infantS == 0 ) {
  	alert ("We are unable to complete your request, please specify number of passengers.");
	return false;
	}
if ( (infantL  > 0 || infantS  > 0 || child > 0) && (senior == 0 && adults == 0 && youth == 0)) {
  	alert ("Travel regulations state that a child under the age of 12 must be accompanied by an adult or must be placed in the custodial care of the airline, please contact our Customer Care team @ 1-800-553-7127 for further details.");
	return false;
	}
	
	if ( infantL  > (parseInt(senior) + parseInt(adults) + parseInt(youth))) {
	
	alert ("Travel regulations state that a child under the age of 12 must be accompanied by an adult or must be placed in the custodial care of an airline, please contact our Customer Care team @ 1-800-553-7127 for further details.");
	return false;
	}
var totpass = 0;
totpass = parseInt(adults) + parseInt(child) + parseInt(senior) +parseInt(youth) + parseInt(infantL) + parseInt(infantS);
if ( totpass > 6 ) {
  	alert ("Reservations in excess of 6 passengers are handled by our Customer Care Team at 1-800-553-7127. Please contact them directly or book your reservations in groups of 6 or less. Thank you.");
	return false;
	}



        if(obj.includeAirline!=null)
        { 

        var noOfAirline = obj.includeAirline.length; 
        var countI = 0;
        var countS = 0;
         
       
                
       for( i = 1 ; i < noOfAirline ; i++)
       {
            if((obj.includeAirline.options[i].selected == true) &&  (obj.skipAirline.options[i].selected  == true))
            {
                alert("You have requested that we search and skip the same airline, please clarify. Identical Airlines cannot be selected and rejected at the same time.");
                return false;
            }
            
            if(obj.includeAirline.options[i].selected == true)
            {
                countI = countI +1;
                if (countI > 5)
                {
                alert("You have selected 6 or more airlines to be included in your Advance search. Please adjust your Advance search to a maximum of 5 airlines. Thank you.");
                return false;
                }
            }
            
            if(obj.skipAirline.options[i].selected  == true)
            {
                countS = countS +1;
                if (countS > 5)
                {
                alert("You have selected 6 or more airlines to be excluded for your Advance search. Please adjust your Advance search to a maximum Of 5 airlines. Thank you.");
                return false;
                }
            }
            
       }

       }
 

        var PsgTypeCount=0;
        if(obj.adult.value >0) PsgTypeCount++;            
        if(obj.senior.value >0) PsgTypeCount++;
        if(obj.youth.value >0) PsgTypeCount++;
        if(obj.child.value >0) PsgTypeCount++;
        if(obj.infantInLap.value >0) PsgTypeCount++;
        if(obj.infantInSeat.value >0) PsgTypeCount++;     

        
        if(PsgTypeCount>4)
        {
            alert("You have selected more than 4 separate passenger types, please adjust your passenger types to a maximum of 4 or contact our Customer Care Team @ 1-800-553-7127 for further assistance.");
            obj.adult.focus();
            return  false;
        }
  
 
}


// Trim whitespace from left and right sides of s.
            function trim(s) {
                return s.replace( /^\s*/, "" ).replace( /\s*$/, "" );
            }
            
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;



function Hello() {

if (ns4) document.layers[f].visibility = "show"
else if (ie4) document.all[f].style.visibility = "visible"

if (ns4) document.layers[f].visibility = "hide"
else if (ie4) document.all[t].style.visibility = "hidden"

}

//FUNCTION FOR ROUND TRIP
function validateForRT(obj)
{   
     
     var PickUpCity = trim(obj.fromCity.value) ;  
          obj.fromCity.value = PickUpCity.toUpperCase(); 
			  if (PickUpCity == "")  {
			  alert("We are unable to complete your request, please specify the departure city or airport code.");
			  return false;
			  }
			  if (!PickUpCity.match(/^[a-zA-Z\s]+$/))  {
			  alert("We are unable to identify the Departure City or Airport code you have entered, please ensure that it is in Alphabets. Thank you.");
			  return false;
			  }
			  
			  if (PickUpCity.length < 3 || PickUpCity.length > 25) {
			  	alert ("Please enter a departure city of 3 to 25 characters");
				return false;
				}
				
	 var DropOffCity= trim(obj.toCity.value);
	 	obj.toCity.value =	DropOffCity.toUpperCase();	
			  if (DropOffCity == "")  {
			  alert("We are unable to complete your request, please specify the return city or airport code.");
			  return false;
			 }
			
			 if (!DropOffCity.match(/^[a-zA-Z\s]+$/))  {
			  alert("We are unable to identify the Return City or Airport code you have entered, please ensure that it is in Alphabets. Thank you.");
			  return false;
			  }
			 if (DropOffCity.length < 3 || DropOffCity.length > 25) {
			  	alert ("Please enter a return city of 3 to 25 characters");
				return false;
				} 	
		
		     if (PickUpCity == DropOffCity)
		        {
		         alert("We are unable to complete your request, your departure and return city is identical. Please clarify.");
		         return false;
		        }
	 var departure=obj.startDate.value;
	 
	 
			 if (trim(departure) =="")  {
			  alert("We are unable to complete your request, please specify your departure date.");
			  return false;
			  }
			
		 if(obj.tripType.value=="F" )  {
		 
		      var returndate=obj.returnDate.value;
			  if (trim(returndate) =="")  {
			  alert("We are unable to complete your request, please specify your return date.");
			  return false;
			  }  
			}		
        
             if( validateDate(obj) == false ) { return false; }
                else {
				return true ;
				 }                                       
	                 
                     
}



//FUNCTION FOR MULTI-CITY TRIP
function validateForMC(obj)
{
   
	count(obj);	
	
	for(i=0;i<=check;++i)
     {    
          
       PickUpCity  = trim(obj.fromCity[i].value);  
       DropOffCity = trim(obj.toCity[i].value);
       departure   = trim(obj.startDate[i].value);  
      
      
           if( (PickUpCity == "") || (DropOffCity == "") || (departure == "") )
             {    
                   
                    
				  if (PickUpCity == "")  {
				  alert("We are unable to complete your request, please specify your Departure City or Airport Code for Flight " + (i+1));
				  
				  return false;
				  }
				  
				  if (PickUpCity.length < 3 || PickUpCity.length > 25) {
				  	alert ("Please enter a departure city  of 3 to 25 characters for Flight " + (i+1));
				  	
					return false;
					}
				
				  if (DropOffCity == "")  {
				   alert("We are unable to complete your request, please specify your Return City or Airport Code for Flight " +(i+1));
				  
				   return false;
				   }
				
				  if (DropOffCity.length < 3 || DropOffCity.length > 25) {
				  	alert ("Please enter a return city  of 3 to 25 characters for Flight " + (i+1));
				  	
					return false;
					} 
					
					 	
					
				 if (departure =="")  {
				    alert("We are unable to complete your request, please specify your Departure date for Flight " + (i+1));
				   
				    return false;
				    }
				  
			}
			else
			{
			  if (!PickUpCity.match(/^[a-zA-Z\s]+$/))  {
			  alert("Please check spelling for departure city or airport code for Flight " + (i+1)+", it should be alphabetic");
			  return false;
			  }
			  if (!DropOffCity.match(/^[a-zA-Z\s]+$/))  {
			  alert("Please check spelling for return city or airport code for Flight " + (i+1)+", it should be alphabetic");
			  return false;
			  }
			  if (PickUpCity == DropOffCity)
			        {
			         alert("You've specified same departure and return city. Please make either of two different.");
			         return false;
			        }	
			}
			
       	
                                                
	 }	
	
	 			         
     
	 if( validateDate(obj) == false ) {  return false; }
                else {
				return true ;
				 }   		 
				                  
 
      return false;            
}


function validateDate(obj) {

	     
 	  	var dd = new Date();
 	  	
 	  	var jdd = new Date(obj.year.value+"",obj.month.value-1,obj.date.value,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
 	  	     day=jdd.getDate();
	         month=jdd.getMonth();
	         year=jdd.getFullYear();
	    //var dateYearFromtoday = new Date(year+1,month,day,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
	   
		day=day+3;
   
	   var validdate = new Date(year,month,day,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
	     
	    var depdt;
	    var retdt;
	    if(obj.tripType.value=="M")
	    {
	         depdt = trim(obj.startDate[0].value);
	         var depdtm;
		     for(i=0;i<=check;++i)
		     {
		            depdtm = trim(obj.startDate[i].value); 
		 	  
					var depdtarr = depdtm.split("/");
				 	depmm = depdtarr[0]-1;
				 	depdd = depdtarr[1];
				 	depyyyy = depdtarr[2];
			
				    var depdate = new Date(depyyyy,depmm,depdd,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
			        //Calculate difference btw the two dates, and convert to days
					var day_difference = Math.ceil((depdate.getTime()-jdd.getTime())/(one_day));
			      if (day_difference > maxDayToBook ) {
					alert("We are unable to complete your request at this time, travel must be scheduled within 335 days from today's date for flight " +(i+1));
			
					return false;
					}
		 	  
		 	 }
	 	 }
	 	else if(obj.tripType.value=="F")
	 	 {
	 	  depdt = trim(obj.startDate.value);
	 	  retdt = trim(obj.returnDate.value);
	 	  
	 	    var retdtarr = retdt.split("/");
		 	retmm = retdtarr[0]-1;
		 	retdd = retdtarr[1];
		 	retyyyy = retdtarr[2];
		 	var retdate = new Date(retyyyy,retmm,retdd,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
		 	//Calculate difference btw the two dates, and convert to days
	        var day_difference = Math.ceil((retdate.getTime()-jdd.getTime())/(one_day));
		 	if (day_difference > maxDayToBook ) {
				alert("We are unable to complete your request at this time, travel must be scheduled within 335 days from today's date.");
		
				return false;
				}
		   
	 	 }
// End | Issue 614
	 	else if(obj.tripType.value=="C")
	 	 {
	 	  depdt = trim(obj.startDate.value);
	 	 } 
		var depdtarr = depdt.split("/");
	 	depmm = depdtarr[0]-1;
	 	depdd = depdtarr[1];
	 	depyyyy = depdtarr[2];

	var depdate = new Date(depyyyy,depmm,depdd,dd.getHours(),dd.getMinutes(),dd.getSeconds(),dd.getMilliseconds());
	//Calculate difference btw the two dates, and convert to days
	var day_difference = Math.ceil((depdate.getTime()-jdd.getTime())/(one_day));
		
    if (day_difference > maxDayToBook ) {
		alert("Please specify the Departure date within 335 days from today's date");

		return false;
		}
	
	if (depdate < validdate ) {
		alert("Departure date should be atleast 3 days from todays date");

		return false;
		}
		
	
	 
  
	return true;

}

function count(obj)
{

       
   for(i=0;i<(obj.fromCity.length);++i)
   {
       PickUpCity  = trim(obj.fromCity[i].value) ;  
       DropOffCity = trim(obj.toCity[i].value);
       departure   = trim(obj.startDate[i].value);
       obj.fromCity[i].value = PickUpCity.toUpperCase(); 
       obj.toCity[i].value = DropOffCity.toUpperCase();
       if( (PickUpCity != "") || (DropOffCity != "") || (departure != "") )
         {
          check=i;
         }
   } 


}

//VALIDATION FOR SEARCH END'S HERE

//THIS FUNCTION POPUP WINDOW FOR AIRPORT CODE
function OpenWindow(target)
{	window.open(target,'AirportCodes','scrollbars=yes,resizable=yes,left=0,top=100,height=500,width=640');
}
