var strRetEarlierLeav = false;

//**********************************************************************************************************
// Function		: hideCellContent()
// Inputs		: tableID as string
//			  rowIndex as string
//			  cellIndex as string
// Description		: To hide the cells related to room2 viz., Adults in room2, children ages etc, bassed on 
//			  number of rooms selected.
// Outputs		: return
//**************************************************************************************************************

function hideCellContent (tableID, rowIndex, cellIndex) 
{
  	if (document.layers) 
  	{	
    		if (typeof rowIndex != undefined)
      		var cell = document[tableID + 'CellR' + rowIndex + 'C' + cellIndex];
    		cell.visibility = 'hide';
  	}
  	else if (window.opera) 
  	{
    		var cell = document.getElementById(tableID + 'CellR' + rowIndex + 'C' + cellIndex);
    		cell.style.visibility = 'hidden';
  	}
  	else if (document.all)
    		document.all[tableID].rows[rowIndex].cells[cellIndex].style.visibility = 'hidden';
  	else if (document.getElementById)
    		document.getElementById(tableID).rows[rowIndex].cells[cellIndex].style.visibility = 'hidden';
}


//**************************************************************************************************************
// Function		: showCellContent()
// Inputs		: tableID as string
//			  rowIndex as string
//			  cellIndex as string
// Description		: To show the cells related to room2 viz., Adults in room2, children ages etc, bassed on 
//			  number of rooms selected.
// Outputs		: return
//***************************************************************************************************************

function showCellContent (tableID, rowIndex, cellIndex) 
{
  	if (document.layers) 
  	{
   		if (typeof rowIndex != undefined)
      			var cell = document[tableID + 'CellR' + rowIndex + 'C' + cellIndex];
    		cell.visibility = 'show';
  	}
  	else if (window.opera) 
  	{
    		var cell = document.getElementById(tableID + 'CellR' + rowIndex + 'C' + cellIndex);
    		cell.style.visibility = 'visible';
  	}
  	else if (document.all)
    		document.all[tableID].rows[rowIndex].cells[cellIndex].style.visibility = 'visible';
  	else if (document.getElementById)
    		document.getElementById(tableID).rows[rowIndex].cells[cellIndex].style.visibility = 'visible';
}


//**********************************************************************************************************
// Function		: loadPage()
// Inputs		: -
// Description		: This function is triggered on the event of loading the page. The default values 
//			  for Leave Date (i.e., current date + 15 days) and Return date (i.e., Leave Date +
//			  7 days) are set.Also it default options for Adults in room1 and Children in room1 
//			  are set by calling the function - chkDate(date obj, limit)
// Outputs		: return
//**********************************************************************************************************

function loadPage()
{
	
	var today;
        	var DepartDate;
        	var intDepartMM;
        	var intDepartDD;
        	var intDepartYY;
        	var inReturnDate;
 
        	today = new Date();
                                 
        	DepartDate = new Date();

	document.getElementById('txtLeave').value = chkDate(today,15);

       	DepartDate=document.getElementById('txtLeave').value;
                     
       	intDepartMM = DepartDate.substring(0,2);
       	intDepartDD = DepartDate.substring(3,5);
       	intDepartYY = DepartDate.substring(6,10);
    
	inReturnDate = new Date(intDepartYY,intDepartMM-1, intDepartDD);
               	
	document.getElementById('txtReturn').value = chkDate(inReturnDate,7);
     	               
	document.getElementById('aTable').rows[7].style.display="none";	
	document.getElementById('aTable').rows[8].style.display="none";
	document.getElementById('aTable').rows[9].style.display="none";	

        show2Opt();
}


//**********************************************************************************************************
// 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 arrayDate = inDate.split('/');
		
 	one_day = 1000*60*60*24;
 
	if(flag == "txtLeave"){ 
		flag = "Depart";
				       
		if(cal5.prs_date(document.getElementById('txtLeave').value) == "false"){
            document.SearchForm.txtLeave.select();
			return false;
		}
	}
	else{
    	 	flag = "Return";

	    if(cal6.prs_date(document.getElementById('txtReturn').value) == "false"){
            document.SearchForm.txtReturn.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 < 4) {
       	alert("Please select an alternate "+ flag + " date. Your request should be at least 4 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.SearchForm.txtLeave.value;
		strValidDepartDt = chkDt(strDepartDate,"txtLeave");
		
		if(strValidDepartDt != false){
			document.getElementById('txtReturn').value = chkDate(strValidDepartDt,7);
		}
		else{
			document.SearchForm.txtLeave.select();
			return false;
		}
	}
	document.SearchForm.txtReturn.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.SearchForm.txtLeave.value;
	strReturnDate = document.SearchForm.txtReturn.value;

	chkDtIndDepart = chkDt(strDepartDate,"txtLeave");
	chkDtIndReturn = chkDt(strReturnDate,"txtReturn");

	if(chkDtIndDepart == false){
			document.SearchForm.txtLeave.select();
			return false;
	}
	if(chkDtIndReturn == false){
			document.SearchForm.txtReturn.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.SearchForm.txtReturn.select();
			return false;
			
		}

	//end added 
	
}


//**********************************************************************************************************
// Function		: room2Rows()
// Inputs		: roomValue as string
// Description		: To show the drop downs related to Adults in room2 and Children in Room2 when two 
//			  rooms are selected.Also it validates against the condition that the total number of
// 			  passengers cannot be more than 7. It internally calls the function show2opt()
// Outputs		: return
//**********************************************************************************************************

function room2Rows(roomValue)
{
	var strAdult1;
	var strChild1;
	var strNoOfPasngr;
	
	if (roomValue == 1){
		document.getElementById('aTable').rows[8].style.display="none";
		document.getElementById('aTable').rows[9].style.display="none";
	}else{
		strAdult1 = document.SearchForm.NoAdultsRoom1[document.SearchForm.NoAdultsRoom1.selectedIndex].value;
		strChild1 = document.SearchForm.NoChldRm1[document.SearchForm.NoChldRm1.selectedIndex].value;
		strNoOfPasngr = 7 - parseInt(strAdult1) - parseInt(strChild1);
		if(strNoOfPasngr > 0){
			document.getElementById('aTable').rows[8].style.display='';
			show2Opt();
		}
		else{
			alert("The maximum number of passengers, 7 already has been selected for room1 hence Please change number of passengers in room1 and then select another room");
			document.SearchForm.NoRooms[0].selected=true;
			document.SearchForm.NoRooms.focus();
			return false;
		}
	}
}

	
//**********************************************************************************************************
// Function		: chkChldAgeRm1()
// Inputs		: chldRm1Value as string
// Description		: To validate  the  total number of passengers not to exceed 7 and  to display the 
//			  Children age drop downs based on the number of children selected for room1 by 
//			  calling the function displayRm1ChldAges()
// Outputs		: return
//**********************************************************************************************************

function chkChldAgeRm1(chldRm1Value)
{

        	var selAdult1Index;
        	var delChild1Index;
        	var selAdult1Value;
        	var selChild1Value;
        	var totPassengers;
       		var strRoomCount;

           	strRoomCount = document.SearchForm.NoRooms[document.SearchForm.NoRooms.selectedIndex].value;

        
		selAdult1Index = document.SearchForm.NoAdultsRoom1.selectedIndex;
 		selAdult1Value = document.SearchForm.NoAdultsRoom1[selAdult1Index].value;
        

 		selChild1Index = document.SearchForm.NoChldRm1.selectedIndex;
 		selChild1Value = document.SearchForm.NoChldRm1[selChild1Index].value;	
  
        	totPassengers = parseInt(selAdult1Value) + parseInt(selChild1Value);
        	if ( totPassengers > 7){
          		alert("The number of Passengers exceeds our maximum of 7.");
          		document.SearchForm.NoChldRm1[0].selected = true;
          		document.getElementById('aTable').rows[7].style.display="none";
          		return false;
        	} else{
			if (chldRm1Value == 0){
				document.getElementById('aTable').rows[7].style.display="none";
			}
			else{
				displayRm1ChldAges(chldRm1Value);
			}
       		}
      		if(strRoomCount > 1){
 			show2Opt();
     		}
}


//**********************************************************************************************************
// Function		: chkChldAgeRm2()
// Inputs		: chldRm2Value as string
// Description		: To display the Children age drop downs based on the number of children selected for
//			  room2 by calling the function displayRm2ChldAges()
// Outputs		: return
//**********************************************************************************************************

function chkChldAgeRm2(chldRm2Value)
{
	if (chldRm2Value == 0){	
		document.getElementById('aTable').rows[9].style.display="none";
	}
	else{
		displayRm2ChldAges(chldRm2Value);
	}
}


//**********************************************************************************************************
// Function		: displayRm1ChldAges()
// Inputs		: ageValue as string
// Description		: To hide/show the drop downs for Children ages in room1 based on the number of 
//			  children selected
// Outputs		: return
//**********************************************************************************************************

function displayRm1ChldAges(ageValue)
{
	var max = 6 - ageValue;
	var col = 1 + parseInt(ageValue);

	document.getElementById('aTable').rows[7].style.display='';

	for (count=0;count<max;count++)
	{
		hideCellContent('aTable',7,col);
		col = col + 1;
	}
	for (count=0;count<ageValue;count++)
	{
		var showCol = parseInt(count) + 1;
		showCellContent('aTable',7,showCol);
	}
}


//**********************************************************************************************************
// Function		: displayRm2ChldAges()
// Inputs		: ageValue as string
// Description		: To hide/show the drop downs for Children ages in room2 based on the number of 
//			  children selected
// Outputs		: return
//**********************************************************************************************************

function displayRm2ChldAges(ageValue)
{
	var max = 5 - ageValue;
	var col = 1 + parseInt(ageValue);

	document.getElementById('aTable').rows[9].style.display='';

	for (count=0;count<max;count++)
	{
		hideCellContent('aTable',9,col);
		col = col + 1;
	}
	for (count=0;count<ageValue;count++)
	{
		var showCol = parseInt(count) + 1;
		showCellContent('aTable',9,showCol);
	}
}


//**********************************************************************************************************
// Function		: show2Opt()
// Inputs		: 
// Description		: To show the number of adults in room 2. The options in the drop down are dynamically 
//			  added based on the selected values of no of adults in room1 and no of children in 
//			  room1
// Outputs		: return
//**********************************************************************************************************

function show2Opt()
{

 	var exstAdult2Options;
 	var selAdult1Ind;
 	var selAdult1;
 	var selAdult2Ind;
 	var selAdult2;
 	var selChild1Ind;
 	var selChild1;
 	var newOptionName;
 	var noAdult2Opt;
	var strRoomCount;

	strRoomCount = document.SearchForm.NoRooms[document.SearchForm.NoRooms.selectedIndex].value;

 
 	exstAdult2Options=document.SearchForm.NoAdultsRoom2.options.length;

 
  
 	selAdult1Ind = document.SearchForm.NoAdultsRoom1.selectedIndex;
 	selAdult1 = document.SearchForm.NoAdultsRoom1[selAdult1Ind].value;

 	selChild1Ind = document.SearchForm.NoChldRm1.selectedIndex;
 	selChild1 = document.SearchForm.NoChldRm1[selChild1Ind].value;
  
 	noAdult2Opt= 7 - selAdult1 - selChild1;

 	if((strRoomCount > 1)&&(noAdult2Opt <= 0)){
		show2ChildOpt();
		
	}
 	else{ 
            
        		if ( exstAdult2Options > noAdult2Opt){
			for(i=0; i < exstAdult2Options ; i++)
    			{
				if(i >= noAdult2Opt){	
                            			document.SearchForm.NoAdultsRoom2.options[noAdult2Opt] = null;
				}
       				else{
          		    			newOptionName = new Option(i+1,i+1);
 			    		document.SearchForm.NoAdultsRoom2.options[i] = newOptionName;
                        			}
     			}
		
		}
 		else{
 			for(i=0; i < noAdult2Opt ; i++)
	 		{
 				newOptionName= new Option(i+1,i+1);
 				document.SearchForm.NoAdultsRoom2.options[i] = newOptionName;
			
 	 		}
 		}
		show2ChildOpt();
	}
}


//**********************************************************************************************************
// Function		: show2ChildOpt()
// Inputs		: 
// Description		: To show the number of children in room 2. The options in the drop down are dynamically 
//			  added based on the selected values of no of adults in room1, no of children in 
//			  room1 and no of adults in room2.
// Outputs		: return
//**********************************************************************************************************


function show2ChildOpt()
{
 	var exstChild2Options;
 	var selAdult1Ind;
 	var selAdult1;
 	var selAdult2Ind;
 	var selAdult2;
 	var selChild1Ind;
 	var selChild1;
 	var noChild2Opt;
 	var newOption;
	var strRoomCount;

	strRoomCount = document.SearchForm.NoRooms[document.SearchForm.NoRooms.selectedIndex].value;

	if(strRoomCount > 1){

	
	 	exstChild2Options=document.SearchForm.NoChldRm2.options.length;
 		
		selAdult1Ind = document.SearchForm.NoAdultsRoom1.selectedIndex;
 		selAdult1 = document.SearchForm.NoAdultsRoom1[selAdult1Ind].value;
 
 		selChild1Ind = document.SearchForm.NoChldRm1.selectedIndex;
 		selChild1 = document.SearchForm.NoChldRm1[selChild1Ind].value;
    
 		selAdult2Ind = document.SearchForm.NoAdultsRoom2.selectedIndex;
 		selAdult2 = document.SearchForm.NoAdultsRoom2[selAdult2Ind].value;

	 	noChild2Opt= 7 - parseInt(selAdult1) - parseInt(selChild1) - parseInt(selAdult2);
		
		if((strRoomCount > 1)&&(noChild2Opt < 0)){

    		 	alert("The number of Passengers exceeds our maximum of 7");
			document.SearchForm.NoChldRm1[0].selected = true;
			document.getElementById('aTable').rows[7].style.display="none";
			document.SearchForm.NoAdultsRoom2[0].selected = true;
    			document.SearchForm.NoChldRm2[0].selected = true;
     			return false;
   		}
   		else{
 			if ( exstChild2Options > noChild2Opt){
				for(i=0; i < exstChild2Options  ; i++)
    				{
					if(i > noChild2Opt )	{
						document.SearchForm.NoChldRm2.options[noChild2Opt +1] = null;
					}
       					else{
          		    				newOption = new Option(i,i);
						document.SearchForm.NoChldRm2.options[i] = newOption;
					}
     				}
			}
 			else{
 				for(i=0; i < noChild2Opt+1 ; i++)
	 			{
 					newOption= new Option(i,i);
 					document.SearchForm.NoChldRm2.options[i] = newOption;
			
 	 			}	
   			}
		}
   		chkChldAgeRm2(document.SearchForm.NoChldRm2.options[document.SearchForm.NoChldRm2.selectedIndex].value);
	} 
}
//**********************************************************************************************************
// Function		: checkChildAges()
// Inputs		: ChildCount in each room and no.of rooms flag
// Description		: This function is called on the event of clicking the Find image of SearchPage.
//			  It validates the age fields of children 
// Outputs		: return
//**********************************************************************************************************
function checkChildAges(strChildCount,strRoomFlag){
	
	var selAgeInd;
	var strFieldName;
	var selAgeValue;
		
	for (i=0; i < strChildCount; i++){
		
		if(strRoomFlag ==1){
			strFieldName = "ChildRoom1Age"+(i+1);
		}else{
			strFieldName = "ChildRoom2Age"+(i+1);
		}
		
		selAgeInd = eval( "document." + "SearchForm" + "." + strFieldName + ".selectedIndex");	
		selAgeValue = eval( "document." + "SearchForm" + "." + strFieldName + "[" +selAgeInd+"]" + ".value");

		if(selAgeValue == "??"){
			alert("Please select an age for the Child " + (i+1) +" in Room " + strRoomFlag);
			eval( "document." + "SearchForm" + "." + strFieldName +".focus()");
			return false;
		}
		
	}
}


//**********************************************************************************************************
// 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.SearchForm.selFrom[document.SearchForm.selFrom.selectedIndex].value;
	strSelDest= document.SearchForm.selTo[document.SearchForm.selTo.selectedIndex].value;
	

	strLeavDate = document.SearchForm.txtLeave.value;
	strRetDate = document.SearchForm.txtReturn.value;

        strRoomCount = document.SearchForm.NoRooms[document.SearchForm.NoRooms.selectedIndex].value;

	strAdult1 = document.SearchForm.NoAdultsRoom1[document.SearchForm.NoAdultsRoom1.selectedIndex].value;
	strChild1 = document.SearchForm.NoChldRm1[document.SearchForm.NoChldRm1.selectedIndex].value;
   	
	if(document.SearchForm.selFrom[0].selected == true){
		alert("Please select a departure city");
		document.SearchForm.selFrom.focus();
		return false;
	}
	else if(document.SearchForm.selTo[0].selected == true){
		alert("Please select a destination city");
		document.SearchForm.selTo.focus();
		return false;
	}
	else if(strSelDepart == strSelDest){
		alert("Please check departure and destination cities, they cannot be the same");
		document.SearchForm.selFrom.focus();
		return false;
	}

	chkLeavDtInd = chkDt(strLeavDate,"txtLeave");
	chkRetDtInd = chkDt(strRetDate,"txtReturn");

	
	if(chkLeavDtInd == false){
			document.SearchForm.txtLeave.select();
			return false;
	}

	if(chkRetDtInd == false){
			document.SearchForm.txtReturn.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.SearchForm.txtReturn.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.SearchForm.txtReturn.select();
			return false;
			
		}
	}
	if(strRoomCount > 1){

		strAdult2 = document.SearchForm.NoAdultsRoom2[document.SearchForm.NoAdultsRoom2.selectedIndex].value;
		strChild2 = document.SearchForm.NoChldRm2[document.SearchForm.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.SearchForm.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.SearchForm.NoAdultsRoom1.focus();
			return false;
		}
		else{
            if(checkChildAges(strChild1,1) == false){
			return false;
			}
		}
	}

    //date format bug fix
    document.SearchForm.txtLeave.value = chkDate(chkLeavDtInd,0);
    document.SearchForm.txtReturn.value = chkDate(chkRetDtInd,0);

   return true;
 }
