﻿var strRetEarlierLeav = false;

//**********************************************************************************************************
// Function		: chkDate()
// Inputs		: inDate as Date object
//			  limit as String 
// Description		: To return a date in mm/dd/yyyy format by adding number of days specified in limit
// 			  argument to the date argument passed
// Outputs		: date  in mm/dd/yyyy format
//**********************************************************************************************************

function chkDate(inDate,limit)
{
   	var inDD;
   	var inMM;
   	var inYY;
   	var nDays;
   	var curDate;
   	var monthDays = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

       	inYY = inDate.getYear();
        	if (inYY < 1900) {
          		inYY = inYY + 1900;
        	}
       
        	//do the classic leap year calculation
   	if( ((inYY % 4 == 0) && (inYY % 100 != 0)) ||(inYY % 400 == 0))
     		monthDays[1] = 29;

   	inDD = inDate.getDate();
   	inDD = inDD + limit;
   	inMM = inDate.getMonth();
   	nDays = monthDays[inMM];

   	if (inMM != 11){
     		if (inDD > nDays){
           			inMM= inMM + 1;
           			inDD= inDD - nDays;
        		}
   	}
   	else {
          		if (inDD > nDays){
           			inMM= 0;
           			inDD= inDD - nDays;
           			inYY = inYY + 1;
        		}
    	}
       
   	inMM = inMM + 1;
   	if (inDD <= 9){
         		 inDD = "0" + inDD;
        	}
   	if(inMM <= 9){
      	  	inMM = "0" + inMM;
   	}
   	curDate = inMM + "/" + inDD + "/" + inYY;
   	return curDate;
}


//**********************************************************************************************************
// Function		: chkDt()
// Inputs		: inDate as string
//			  flag as String
// Description		: To validate the date value passed 
// Outputs		: return
//**********************************************************************************************************

function chkDt(inDate,flag)
{
	
 	var inEntDD;
 	var inEntMM;
 	var inEntYY;
 	var today;
	var entDate;
 	var one_day;
 	var numdays;
 	var opt;
 	//opt = document.getElementById('option').value;
 	

	var arrayDate = inDate.split('/');
		
 	one_day = 1000*60*60*24;
 
	if(flag == "txtDepDate"){ 
		flag = "Departure";
        
        // Set date to yyyy format is yy entered
        document.getElementById('txtDepDate').value = set4DigitYear(document.getElementById('txtDepDate').value);
		if(parse_date(document.getElementById('txtDepDate').value) == "false"){
            document.form1.txtDepDate.select();
			return false;
		}
	}
	else{
    	 	flag = "Return";

        // Set date to yyyy format is yy entered
        document.getElementById('txtRetDate').value = set4DigitYear(document.getElementById('txtRetDate').value);
        if(parse_date(document.getElementById('txtRetDate').value) == "false"){
            document.form1.txtRetDate.select();
			return false;
		}
   	}

	inEntMM = arrayDate[0];
        inEntDD = arrayDate[1];
	inEntYY = arrayDate[2];
	
	if (inEntDD <= 9){
         		inEntDD= "0" + inEntDD;
        	}
   	if(inEntMM <= 9){
      	  	inEntMM = "0" + inEntMM;
   	}

 	if (inEntYY  < 100) 
		inEntYY  = Number(inEntYY) + (inEntYY  < 30 ? 2000 : 1900);

	entDate = new Date(inEntYY,inEntMM - 1,inEntDD);
		
 	today = new Date();

 	numdays = Math.ceil((entDate.getTime() - today.getTime())/one_day);

       	strField = "txt" + flag;
      
//	 if (numdays < 0){
//       	alert("Please select an alternate "+ flag + " date. Your request is prior to the today's date.");
//		return false;
//	 }
     if (numdays >= 0 && numdays < 2) {
       	alert("Please select an alternate "+ flag + " date. Your request should be at least 2 days from today's date.");
		return false;
     }
//	 if (numdays > 331 ){
//		alert("Please select an alternate "+ flag + " date. Your request is beyond 331 days"); 
//		return false;
//	 }
	
	strRetEarlierLeav = false;
	return (entDate);
}

function chkDtMC1(inDate,flag)
{
	
 	var inEntDD;
 	var inEntMM;
 	var inEntYY;
 	var today;
	var entDate;
 	var one_day;
 	var numdays;
 	var opt;
 	//opt = document.getElementById('option').value;
 	

	var arrayDate = inDate.split('/');
		
 	one_day = 1000*60*60*24;
 
	if(flag == "txtDepDate1"){ 
		flag = "Departure";
        
        // Set date to yyyy format is yy entered
        document.getElementById('txtDepDate1').value = set4DigitYear(document.getElementById('txtDepDate1').value);
		if(parse_date(document.getElementById('txtDepDate1').value) == "false"){
            document.form1.txtDepDate.select();
			return false;
		}
	}
	else{
    	 	flag = "Return";

        // Set date to yyyy format is yy entered
        document.getElementById('txtDepDate2').value = set4DigitYear(document.getElementById('txtDepDate2').value);
        if(parse_date(document.getElementById('txtDepDate2').value) == "false"){
            document.form1.txtRetDate.select();
			return false;
		}
   	}

	inEntMM = arrayDate[0];
        inEntDD = arrayDate[1];
	inEntYY = arrayDate[2];
	
	if (inEntDD <= 9){
         		inEntDD= "0" + inEntDD;
        	}
   	if(inEntMM <= 9){
      	  	inEntMM = "0" + inEntMM;
   	}

 	if (inEntYY  < 100) 
		inEntYY  = Number(inEntYY) + (inEntYY  < 30 ? 2000 : 1900);

	entDate = new Date(inEntYY,inEntMM - 1,inEntDD);
		
 	today = new Date();

 	numdays = Math.ceil((entDate.getTime() - today.getTime())/one_day);

       	strField = "txt" + flag;
      
//	 if (numdays < 0){
//       	alert("Please select an alternate "+ flag + " date. Your request is prior to the today's date.");
//		return false;
//	 }
//     if (numdays >= 0 && numdays < 2) {
//       	alert("Please select an alternate "+ flag + " date. Your request should be at least 2 days from today's date.");
//		return false;
//     }
//	 if (numdays > 331 ){
//		alert("Please select an alternate "+ flag + " date. Your request is beyond 331 days"); 
//		return false;
//	 }
	
	strRetEarlierLeav = false;
	return (entDate);
}

function chkDtMC2(inDate,flag)
{
	
 	var inEntDD;
 	var inEntMM;
 	var inEntYY;
 	var today;
	var entDate;
 	var one_day;
 	var numdays;
 	var opt;
 	//opt = document.getElementById('option').value;
 	

	var arrayDate = inDate.split('/');
		
 	one_day = 1000*60*60*24;
 
	if(flag == "txtDepDate2"){ 
		flag = "Departure";
        
        // Set date to yyyy format is yy entered
        document.getElementById('txtDepDate2').value = set4DigitYear(document.getElementById('txtDepDate2').value);
		if(parse_date(document.getElementById('txtDepDate2').value) == "false"){
            document.form1.txtDepDate.select();
			return false;
		}
	}
	else{
    	 	flag = "Return";

        // Set date to yyyy format is yy entered
        document.getElementById('txtDepDate3').value = set4DigitYear(document.getElementById('txtDepDate3').value);
        if(parse_date(document.getElementById('txtDepDate3').value) == "false"){
            document.form1.txtRetDate.select();
			return false;
		}
   	}

	inEntMM = arrayDate[0];
        inEntDD = arrayDate[1];
	inEntYY = arrayDate[2];
	
	if (inEntDD <= 9){
         		inEntDD= "0" + inEntDD;
        	}
   	if(inEntMM <= 9){
      	  	inEntMM = "0" + inEntMM;
   	}

 	if (inEntYY  < 100) 
		inEntYY  = Number(inEntYY) + (inEntYY  < 30 ? 2000 : 1900);

	entDate = new Date(inEntYY,inEntMM - 1,inEntDD);
		
 	today = new Date();

 	numdays = Math.ceil((entDate.getTime() - today.getTime())/one_day);

       	strField = "txt" + flag;
      
//	 if (numdays < 0){
//       	alert("Please select an alternate "+ flag + " date. Your request is prior to the today's date.");
//		return false;
//	 }
//     if (numdays >= 0 && numdays < 2) {
//       	alert("Please select an alternate "+ flag + " date. Your request should be at least 2 days from today's date.");
//		return false;
//     }
//	 if (numdays > 331 ){
//		alert("Please select an alternate "+ flag + " date. Your request is beyond 331 days"); 
//		return false;
//	 }
	
	strRetEarlierLeav = false;
	return (entDate);
}

function chkDtMC3(inDate,flag)
{
	
 	var inEntDD;
 	var inEntMM;
 	var inEntYY;
 	var today;
	var entDate;
 	var one_day;
 	var numdays;
 	var opt;
 	//opt = document.getElementById('option').value;
 	

	var arrayDate = inDate.split('/');
		
 	one_day = 1000*60*60*24;
 
	if(flag == "txtDepDate3"){ 
		flag = "Departure";
        
        // Set date to yyyy format is yy entered
        document.getElementById('txtDepDate3').value = set4DigitYear(document.getElementById('txtDepDate3').value);
		if(parse_date(document.getElementById('txtDepDate3').value) == "false"){
            document.form1.txtDepDate.select();
			return false;
		}
	}
	else{
    	 	flag = "Return";

        // Set date to yyyy format is yy entered
        document.getElementById('txtDepDate4').value = set4DigitYear(document.getElementById('txtDepDate4').value);
        if(parse_date(document.getElementById('txtDepDate4').value) == "false"){
            document.form1.txtRetDate.select();
			return false;
		}
   	}

	inEntMM = arrayDate[0];
        inEntDD = arrayDate[1];
	inEntYY = arrayDate[2];
	
	if (inEntDD <= 9){
         		inEntDD= "0" + inEntDD;
        	}
   	if(inEntMM <= 9){
      	  	inEntMM = "0" + inEntMM;
   	}

 	if (inEntYY  < 100) 
		inEntYY  = Number(inEntYY) + (inEntYY  < 30 ? 2000 : 1900);

	entDate = new Date(inEntYY,inEntMM - 1,inEntDD);
		
 	today = new Date();

 	numdays = Math.ceil((entDate.getTime() - today.getTime())/one_day);

       	strField = "txt" + flag;
      
//	 if (numdays < 0){
//       	alert("Please select an alternate "+ flag + " date. Your request is prior to the today's date.");
//		return false;
//	 }
//     if (numdays >= 0 && numdays < 2) {
//       	alert("Please select an alternate "+ flag + " date. Your request should be at least 2 days from today's date.");
//		return false;
//     }
//	 if (numdays > 331 ){
//		alert("Please select an alternate "+ flag + " date. Your request is beyond 331 days"); 
//		return false;
//	 }
	
	strRetEarlierLeav = false;
	return (entDate);
}



//***************************************************************************************************************
// Function		: fCalDt()
// Inputs		: 
// Description		: To display the default value of return date as Depart date + 7 days by calling the
// 			  function chkDate(). Before assigning the value it validates the depart date by calling 
//			  the function chkDt() 
// Outputs		: return
//***************************************************************************************************************

function fCalDt(){
        	
	if (strRetEarlierLeav == true){
		return false;
	}
	else{
		var strDepartDate;
		var strValidDepartDt;
		strDepartDate = document.form1.txtDepDate.value;
		strValidDepartDt = chkDt(strDepartDate,"txtDepDate");
		
		if(strValidDepartDt != false){
			document.getElementById('txtRetDate').value = chkDate(strValidDepartDt,3);
		}
		else{
			document.form1.txtDepDate.select();
			return false;
		}
	}
	document.form1.txtRetDate.select();
  	
}

function fCalDtMC1(){
        	
	if (strRetEarlierLeav == true){
		return false;
	}
	else{
		var strDepartDate;
		var strValidDepartDt;
		strDepartDate = document.form1.txtDepDate1.value;
		strValidDepartDt = chkDtMC1(strDepartDate,"txtDepDate1");
		
		if(strValidDepartDt != false){
			document.getElementById('txtDepDate2').value = chkDate(strValidDepartDt,3);
		}
		else{
			document.form1.txtDepDate1.select();
			return false;
		}
	}
	document.form1.txtDepDate2.select();
  	
}

function fCalDtMC2(){
        	
	if (strRetEarlierLeav == true){
		return false;
	}
	else{
		var strDepartDate;
		var strValidDepartDt;
		strDepartDate = document.form1.txtDepDate2.value;
		strValidDepartDt = chkDtMC1(strDepartDate,"txtDepDate2");
		
		if(strValidDepartDt != false){
			document.getElementById('txtDepDate3').value = chkDate(strValidDepartDt,3);
		}
		else{
			document.form1.txtDepDate2.select();
			return false;
		}
	}
	document.form1.txtDepDate3.select();
  	
}

function fCalDtMC3(){
        	
	if (strRetEarlierLeav == true){
		return false;
	}
	else{
		var strDepartDate;
		var strValidDepartDt;
		strDepartDate = document.form1.txtDepDate3.value;
		strValidDepartDt = chkDtMC1(strDepartDate,"txtDepDate3");
		
		if(strValidDepartDt != false){
			document.getElementById('txtDepDate4').value = chkDate(strValidDepartDt,3);
		}
		else{
			document.form1.txtDepDate3.select();
			return false;
		}
	}
	document.form1.txtDepDate4.select();
  	
}


//**********************************************************************************************************
// Function		: chkCalendar()
// Inputs		: 
// Description		: To validate the depart date and return date against the format and the application
//			  validations like  the  dates  cannnot be earlier to the current date and cannot be
//			  beyond 331 days. This is done by calling the function chkDt() 
// Outputs		: return
//**********************************************************************************************************

function chkCalendar()
{
	
	var strDepartDate;
	var strReturndate;
	var chkDtIndDepart;
	var chkDtIndReturn;
	var strDateGap;

	strDepartDate = document.form1.txtDepDate.value;
	strReturnDate = document.form1.txtRetDate.value;

	chkDtIndDepart = chkDt(strDepartDate,"txtDepDate");
	chkDtIndReturn = chkDt(strReturnDate,"txtRetDate");

	if(chkDtIndDepart == false){
			document.form1.txtDepDate.select();
			return false;
	}
	if(chkDtIndReturn == false){
			document.form1.txtRetDate.select();
			return false;
	}

	//added as part of change request

	strDateGap = Math.ceil((chkDtIndReturn.getTime() - chkDtIndDepart.getTime())/(1000*60*60*24));

		if(strDateGap == 0){
			alert("Please check the Depart and destination dates, they cannot be same");
			document.form1.txtRetDate.select();
			return false;
			
		}

	//end added 
	
}


//**********************************************************************************************************
// Function		: fSubmit()
// Inputs		: 
// Description		: This function is called on the event of clicking the Find image of SearchPage.
//			  It validates all the fields, submits the form and shows the wait page 
// Outputs		: return
//**********************************************************************************************************

function fSubmit(){
    
	var strLeavDate;
	var strRetDate;
	var strPsngrCount;
	var strAdult1;
	var strChild1;
	var strAdult2;
	var strChild2;
	var strRoomCount;
	var strSelDepart;
	var strSelDest;
	var noOfDays;
		
	strSelDepart = document.form1.selFrom[document.form1.selFrom.selectedIndex].value;
	strSelDest= document.form1.selTo[document.form1.selTo.selectedIndex].value;
	
		
    // Set date to yyyy format is yy entered
    document.getElementById('txtDepDate').value = set4DigitYear(document.getElementById('txtDepDate').value);
    document.getElementById('txtRetDate').value = set4DigitYear(document.getElementById('txtRetDate').value);
	

	strLeavDate = document.form1.txtDepDate.value;
	strRetDate = document.form1.txtRetDate.value;

        strRoomCount = document.form1.NoRooms[document.form1.NoRooms.selectedIndex].value;

	strAdult1 = document.form1.NoAdultsRoom1[document.form1.NoAdultsRoom1.selectedIndex].value;
	strChild1 = document.form1.NoChldRm1[document.form1.NoChldRm1.selectedIndex].value;
   	
	if(document.form1.selFrom[0].selected == true){
		alert("Please select a departure city");
		document.form1.selFrom.focus();
		return false;
	}
	else if(document.form1.selTo[0].selected == true){
		alert("Please select a destination city");
		document.form1.selTo.focus();
		return false;
	}
	else if(strSelDepart == strSelDest){
		alert("Please check departure and destination cities, they cannot be the same");
		document.form1.selFrom.focus();
		return false;
	}
    
	chkLeavDtInd = chkDt(strLeavDate,"txtDepDate");
	chkRetDtInd = chkDt(strRetDate,"txtRetDate");

	
	if(chkLeavDtInd == false){
			document.form1.txtDepDate.select();
			return false;
	}

	if(chkRetDtInd == false){
			document.form1.txtRetDate.select();
			return false;
			
	}

	else {
		
		noOfDays = Math.ceil((chkRetDtInd.getTime() - chkLeavDtInd.getTime())/(1000*60*60*24));

	//added as a part of change request

		if(noOfDays == 0){
			alert("Please check the Depart and Return dates, they cannot be same");
			strRetEarlierLeav = true;
			document.form1.txtRetDate.select();
			return false;
			
		}

	//end added

		if(noOfDays < 0){
			alert("Please check the Return date, it cannot be earlier than the Depart date");
			strRetEarlierLeav = true;
			document.form1.txtRetDate.select();
			return false;
			
		}
	}
	if(strRoomCount > 1){

		strAdult2 = document.form1.NoAdultsRoom2[document.form1.NoAdultsRoom2.selectedIndex].value;
		strChild2 = document.form1.NoChldRm2[document.form1.NoChldRm2.selectedIndex].value;

		strPsngrCount =parseInt(strAdult1) + parseInt(strChild1) +parseInt(strAdult2) + parseInt(strChild2);
		if(strPsngrCount> 7){
			alert("The number of Passengers exceeds our maximum of 7");
			document.form1.NoAdultsRoom1.focus();
			return false;
		}
		else{
		   if((checkChildAges(strChild1,1) == false)||(checkChildAges(strChild2,2) == false)){
				return false;
		   }
		
		}
	}
	else if(strRoomCount == 1){
        strPsngrCount =parseInt(strAdult1) + parseInt(strChild1);
		if(strPsngrCount  > 7){
			alert("The number of Passengers exceeds our maximum of 7");
			document.form1.NoAdultsRoom1.focus();
			return false;
		}
		else{
            if(checkChildAges(strChild1,1) == false){
			return false;
			}
		}
	}

    //date format bug fix
    document.form1.txtDepDate.value = chkDate(chkLeavDtInd,0);
    document.form1.txtRetDate.value = chkDate(chkRetDtInd,0);

   return true;
 }
 
 //**********************************************************************************************************
// Adopted from Tigra Calendar
// URL: http://www.softcomplex.com/products/tigra_calendar/
// Version: 3.2 (American date format)
// Date: 10/14/2002 (mm/dd/yyyy)
// Feedback: feedback@softcomplex.com (specify product title in the subject)
// Note: Permission given to use this script in ANY kind of applications if
//    header lines are left unchanged.
// Note: Script consists of two files: calendar?.js and calendar.html
// About us: Our company provides offshore IT consulting services.
//    Contact us at sales@softcomplex.com if you have any programming task you
//    want to be handled by professionals. Our typical hourly rate is $20.
//
//
// Function		: parse_date()
// Inputs		: str_date as string
// Description	: To check the date passed and allert the user
// Outputs		: return
//**********************************************************************************************************

function parse_date (str_date) {

	var reg_exp = /^\-?\d+$/;
	var arr_date = str_date.split('/');
	
	if (arr_date.length != 3){
	alert ("Invalid date format: '" + str_date + "'.\nPlease correct format to mm/dd/yyyy.");
	goodDay = "false";	
	return "false";
	}
	if (!arr_date[1]){
	alert ("Invalid date format: '" + str_date + "'.\nThe day entered does not exist in that month.");
	goodDay = "false";
	 return "false";
	}
	if ((!reg_exp.exec(arr_date[1]))||(arr_date[1].length != 2)){
	alert ("Invalid day of month value: '" + arr_date[1] + "'.\nPlease correct format to mm/dd/yyyy.");
	goodDay = "false";
	 return "false";
	}
	if (!arr_date[0]){
	alert ("Invalid date format: '" + str_date + "'.\nPlease enter a value for the month");
	goodDay = "false";
	 return "false";
	}
	if ((!reg_exp.exec(arr_date[0]))||(arr_date[0].length != 2)){
	alert ("Invalid month value: '" + arr_date[0] + "'.\nPlease correct format to mm/dd/yyyy.");
	  goodDay = "false";
	  return "false";
	}
	if (!arr_date[2]){
	 alert ("Invalid date format: '" + str_date + "'.\nnPlease enter a value for the year");
	goodDay = "false";
	 return "false";
	}
	if (!reg_exp.exec(arr_date[2])){
	  alert ("Invalid year value: '" + arr_date[2] + "'.\nPlease correct format to mm/dd/yyyy.");
	  goodDay = "false";
	  return "false";
	}
	if ((arr_date[2].length != 4)&&(arr_date[2].length != 2)){
	alert ("Invalid date format: '" + str_date + "'.\nPlease correct year format mm/dd/yyyy.");
	 goodDay = "false";
	 return "false";
	}
      	
	var dt_date = new Date();
	dt_date.setDate(1);

	if (arr_date[0] < 1 || arr_date[0] > 12){
	alert ("Invalid month value: '" + arr_date[0] + "'.\nPlease enter a month value between 01-12.");
	return "false";
	}
	dt_date.setMonth(arr_date[0]-1);
	 
	if (arr_date[2] < 100) arr_date[2] = Number(arr_date[2]) + (arr_date[2] < NUM_CENTYEAR ? 2000 : 1900);
	dt_date.setFullYear(arr_date[2]);

	var dt_numdays = new Date(arr_date[2], arr_date[0], 0);
	dt_date.setDate(arr_date[1]);
	if (dt_date.getMonth() != (arr_date[0]-1)){
	alert ("Invalid day of month value: '" + arr_date[1] + "'.\nPlease enter a day value between 01-"+dt_numdays.getDate()+".");
      	return "false";
	}
	return (dt_date)
}

//**********************************************************************************************************
// Function		: set4DigitYear()
// Inputs		: date in format mm/dd/yyyy
// Description	: Common function used to set date to mm/dd/yyyy if mm/dd/yy entered
//			  
// Outputs		: date in format mm/dd/yyyy
//**********************************************************************************************************
function set4DigitYear (strDate){
    
    var sep = '/';
    var today = new Date();
    var year = today.getFullYear().toString();
    var str = year.substr(0,2);
    var date_array = strDate.split('/');
    if (date_array.length == 3 && date_array[2].length==2)
    {
      strDate = date_array[0] + sep +  date_array[1] + sep + str + date_array[2];
    }
    return strDate;
}
 

