/**************
 * Flie Name : Search.estValidation.js
 * Description :
 *
 * Date of Creation : 9- June -2004
 *
 * *****************************/


// added Nirasha

//var ipAndPortForFlights = document.Search.FlightPageIpAndPort.value ;   // ex: http://192.168.0.32:9009


var currentDate = new Date();

//function for returning how many days there are in a month including leap years
function DaysInMonth( Month, Year )
{
 var DaysInMonth = 31;
  var WhichMonth = parseInt( Month );
  if (WhichMonth == 4 || WhichMonth == 6 || WhichMonth == 9 || WhichMonth == 11 ) DaysInMonth = 30;
  if (WhichMonth == 2 && ( Year/4) != Math.floor( Year/4))	DaysInMonth = 28;
  if (WhichMonth == 2 && ( Year/4) == Math.floor( Year/4))	DaysInMonth = 29;
  return DaysInMonth;
}

function setOneWay()
{
	var  cal2;
	cal2 = document.getElementById('cal2');
	cal2.setAttribute("readOnly","true");
	cal2.style.backgroundColor='eeeeee';
}

function setReturn()
{
	var  cal2;
	cal2 = document.getElementById('cal2');
	cal2.removeAttribute("readOnly","false");
	cal2.style.backgroundColor='ffffff';
}

function trim( s )
{
    return rtrim(ltrim(s));
}

function ltrim( s )
{
    var l = 0;
    while( l < s.length && s[l] == ' ' )
    {
        l++;
    }
    return s.substring(l, s.length);
}

function rtrim( s )
{
    var r = s.length - 1;
    while( r > 0 && s[r] == ' ' )
    {
        r -= 1;
    }
    return s.substring(0, r + 1);
}


function checkCityCodeEmptyness()
{
	var userSelectedDepCity = trim(document.Search.src1.value) ;
	var userSelectedDesCity = trim(document.Search.des1.value) ;
	
	//alert("userSelectedDepCity = " + userSelectedDepCity);
	
	if(userSelectedDepCity != "" && userSelectedDesCity != "" && userSelectedDepCity.length > 2 && userSelectedDesCity.length > 2)
	{
		return true;
	}
	else
	{
		alert("Please enter at least 3 characters for a city.");
		return false;	
	}	
}

function formsubmit()
{
   var canSubmit= false;

   //alert("ipAndPortForFlights  ::  " + document.Search.FlightPageIpAndPort.value);


   if(checkEndMonth() && checkInfantsNo() && checkTotalPassengers() && checkCityCodeEmptyness()) // checkCityCodeEmptyness() &&
   {

	  if (document.Search.ORM[0].checked == true)
	  {
		  document.Search.Flight2_Date.value = "";
		  document.Search.src2.value = "";
		  document.Search.des2.value = "";
		  canSubmit = checkDateFormat( document.Search.Flight1_Date );
          
		   //alert(" if --> ipAndPortForFlights  ::  " + document.Search.FlightPageIpAndPort.value);
		  document.Search.action = document.Search.FlightPageIpAndPort.value + "/ValidationInputOneRet.php";
	  }
	  else if (document.Search.ORM[1].checked ==true)
	  {
          document.Search.src2.value = document.Search.des1.value;
		  document.Search.des2.value = document.Search.src1.value;
          
          canSubmit = checkDateFormat( document.Search.Flight1_Date ) && checkDateFormat( document.Search.Flight2_Date ) && compareDatesFlight(document.Search.Flight1_Date.value,document.Search.Flight2_Date.value)  ;
  
		  
		  //alert("else --> ipAndPortForFlights  ::  " + document.Search.FlightPageIpAndPort.value + "/ValidationInputOneRet.php");
		  document.Search.action = document.Search.FlightPageIpAndPort.value + "/ValidationInputOneRet.php";
		  //alert("document.Search.action  ::  " + document.Search.action);
	  }

	  else
	  {
	          if (document.Search.src1.value !=="" || document.Search.des1.value!="")
			  {
				  canSubmit = checkDateFormat( document.Search.Flight1_Date );
				  if( document.Search.src2.value !="" || document.Search.des2.value!="")
				  {
					canSubmit = canSubmit && checkDateFormat( document.Search.Flight1_Date ) && checkDateFormat( document.Search.Flight2_Date ) && compareDatesFlight(document.Search.Flight1_Date.value,document.Search.Flight2_Date.value) ;

					if( document.Search.src3.value !="" || document.Search.des3.value!="")
					{
						canSubmit = canSubmit && checkDateFormat( document.Search.Flight3_Date ) && compareDatesFlight(document.Search.Flight2_Date.value,document.Search.Flight3_Date.value) ;

						if( document.Search.src4.value !="" || document.Search.des4.value!="")
						{
							canSubmit = canSubmit && checkDateFormat( document.Search.Flight4_Date ) && compareDatesFlight(document.Search.Flight3_Date.value,document.Search.Flight4_Date.value) ;
						}
					}
				  }
				 }


	          document.Search.action = document.Search.FlightPageIpAndPort.value + "/ValidationInput.php";
	  }


       
      if (canSubmit)
      {
      	if ( validateFlight1Date( document.Search.Flight1_Date.value ))
		{
			document.Search.submit();
		}
		else
		{
			//do nothing
            //return false;
		}

      }
      else
      {
      	alert("Check Dates.");
       
      }

   }

   //return false;
}
function setFlightType()
{
	var orm;
	orm = getQueryVariable("ORM");
	if (orm=="O")
	{
	//	alert("Oneway");
		document.Search.ORM[0].checked = true;
		document.Search.ORM[1].checked = false;
		//document.Search.ORM[2].checked = false;
		setOneWay();
	}
	else if (orm=="R")
	{
	//	alert("Return");
		document.Search.ORM[0].checked = false;
		document.Search.ORM[1].checked = true;
		//document.Search.ORM[2].checked = false;
		setReturn();
	}
	else
	{
		document.Search.ORM[0].checked = false;
		document.Search.ORM[1].checked = true;
		//document.Search.ORM[2].checked = false;
		setReturn();
	}

}

function getQueryVariable(variable) {
  //alert(variable);
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      //alert (pair[1]);
      return pair[1];

    }
    else
    {
    	return 'R';
    }
  }

}




function validateFlight1Date( day )
{
     var year, month, date;
     var t = day.split("/");
     date= t[0];
     month= t[1];
     year=t[2];
	 var depDate = new Date(year, month-1,date );
	 var bottomDate = new Date();
	 bottomDate.setDate( bottomDate.getDate() + 2);

	var future = new Date();
	future.setDate (future.getDate() + 330);
	if( depDate.getTime() > bottomDate.getTime())
	{
		if( depDate.getTime() <  future.getTime()  )
	    {
		    return true;
	    }
	    else
	    {
		alert ("Departure date should be less than an year ahead from the current date.");
		return false;
	    }
	}
	else
	{
		alert("Departure date should be at least more than 2 days ahead from the current date.");
		return false;
	}




}



function setRetDate()
{
    var selectedDepDate = new Date(document.Search.departureMonth.value + "/" + document.Search.departureDate.value + "/" + document.Search.departureYear.value);
    selectedDepDate.setDate( selectedDepDate.getDate() + 7);
    document.Search.returnDate.options[ selectedDepDate.getDate() - 1 ].selected = true;
    document.Search.returnMonth.options[ selectedDepDate.getMonth() ].selected = true;



}

function setRetMonth()
{

    document.Search.returnMonth.value = document.Search.departureMonth.value;
}

function validateFlight2Date( day )
{
	var F2Year, F2Month, F2Date;
	var F2t = day.split("/");
	F2Date= F2t[0];
	F2Month= F2t[1];
     	F2Year= F2t[2];

     	var F1t;
     	var F1Date, F1Month, F1Year;
     	
     	F1t = document.Search.Flight1_Date.value.split("/");
     	F1Date = F1t[0];
     	F1Month = F1t[1];
     	F1Year = F1t[2];

      var DaysForSelectedMonth = DaysInMonth( F2Month , F2Year );

      if( DaysForSelectedMonth >= F2Date )
      {
         depDate = new Date(F1Year + "/" + F1Month + "/" + F1Date );
         retDate = new Date(F2Year + "/" + F2Month + "/" + F2Date );

         if( retDate > depDate )
		 {
             //alert("ret date is greater than dep date");
			 return true;
		 }
         else
         {
            alert( "Invalid Return Date" );
            return false;
         }

      }
      else
      {
         alert( "Invalid Return Date" );
         return false;
      }

}

function validateUserRequest()
{

    if( document.Search.ORM[0].checked )
    {

        if( validateFlight1Date( document.Search.Flight1_Date.value ) &&
            checkInfantsNo() && checkTotalPassengers() )
        {
            document.Search.action= document.Search.FlightPageIpAndPort.value + "/SearchingFlightsAnimation.php";
            document.Search.submit();
        }
    }
    else if ( document.Search.ORM[1].checked )
    {
        if( validateFlight1Date( document.Search.Flight1_Date.value ) &&
            validateFlight2Date( document.Search.Flight2_Date.value ) &&
            checkInfantsNo() && checkTotalPassengers() )
        {
            document.Search.action= document.Search.FlightPageIpAndPort.value + "/SearchingFlightsAnimation.php";
            document.Search.submit();
        }
    }

}

function validateMulticenterReq()
{

   var noOfFlights
    if ( document.Search.Flight1_Date.value == 'dd/mm/yy' ||
	     document.Search.Flight1_Date.value == '' ||
		 document.Search.Flight1_Date.value == null )
		 {
			 noOfFlights = 0;
		 }
	else if (document.Search.Flight2_Date.value == 'dd/mm/yy' ||
	         document.Search.Flight2_Date.value == '' ||
		     document.Search.Flight2_Date.value == null )
			{
			    noOfFlights=1;
			}
	else if (document.Search.Flight3_Date.value == 'dd/mm/yy' ||
	         document.Search.Flight3_Date.value == '' ||
		     document.Search.Flight3_Date.value == null )
			{
			    noOfFlights=2;
			}
	else if (document.Search.Flight4_Date.value == 'dd/mm/yy' ||
	         document.Search.Flight4_Date.value == '' ||
		     document.Search.Flight4_Date.value == null )
			{
			    noOfFlights=3;
			}
	else
	    {
			noOfFlights=4;
	    }

	alert("No of Flights = " + noOfFlights);

	if (noOfFlights==0)
	{
	    alert ("Select Flight1 departure date");
		document.Search.Flight1_Date.focus();
	}
	else if (noOfFlights ==1)
	{
		alert ("Select at least two flights.");
		document.Search.Flight2_Date.focus();
	}
	else if (noOfFlights ==2 )
	{
		if( validateFlight1Date( document.Search.Flight1_Date.value ) &&
            validateFlight2Date( document.Search.Flight2_Date.value ) &&
			checkInfantsNo() && checkTotalPassengers() )
		{
			document.Search.action= document.Search.FlightPageIpAndPort.value + "/SearchingFlightsAnimation.php";
			document.Search.submit();
		}
	}

}



  function checkInfantsNo()
  {
       if ( parseInt( document.Search.Infants.options[ document.Search.Infants.selectedIndex ].value) > parseInt(document.Search.Adults.options[ document.Search.Adults.selectedIndex ].value ) )
        {
          alert("You can not travel with more infants than adults .");
          document.Search.Infants.options[0].selected = true;
          return false;
       }
       else
            return true;
  }

  function checkTotalPassengers()
  {
        if( parseInt( document.Search.Adults.options[ document.Search.Adults.selectedIndex ].value ) + parseInt( document.Search.Children.options[ document.Search.Children.selectedIndex ].value ) > 9 )
        {
            alert("You can not select total number of passengers more than 9 per journey excluding Infants .");
            return false;
        }
        else
             return true;
  }


    /*********************************************
    * Description : this functionality will chage
    *               entered code into upper case.
    *
    ********************************************/
/*
    function changeCase()
    {
        var code = document.Search.DestPortCode.value;
        document.Search.DestPortCode.value = code.toUpperCase();

        setDestinationPort( document.Search.DestPortCode.value );
    }
 */

function setPreviousDataValues(cal1Date,cal2Date,adt, chd, inf, airline, bookingClass, orm)
{
   var F1_date="";
   var F2_date="";
   //var F3_Date;
   //var F4_Date;

   F1_date = cal1Date;
   F2_date = cal2Date;
   //F1_date = document.Search.Flight1_Date.value;
   //F2_date = document.Search.Flight2_Date.value;
   //F3_Date = document.Search.Flight3_Date.value;
   //F4_Date = document.Search.Flight4_Date.value;
   var noOfClasses = document.Search.Class.length;

	for ( var j=0; noOfClasses >j; j++ )
	{
		if( document.Search.Class.options[j].value == bookingClass )
			document.Search.Class.options[j].selected = true;

	}
    //var noOfAirlines = document.Search.AirlineSelect.length;
    //for ( var j=0; noOfAirlines >j; j++ )
	//{
	//	if( document.Search.AirlineSelect.options[j].value == airline )
	//		document.Search.AirlineSelect.options[j].selected = true;

	//}


  	document.Search.Adults.options[ parseInt( adt ) - 1 ].selected = true;

	if( parseInt( chd ) > 0 )
		document.Search.Children.options[ parseInt( chd )].selected = true;
	if( parseInt( inf ) > 0 )
		document.Search.Infants.options[ parseInt( inf )].selected = true;
	
	
	if (orm == 'O')
	{
		document.Search.ORM[0].checked = true;
		document.Search.ORM[1].checked = false;
		//document.Search.ORM[2].checked = false;
		document.Search.F1dd.value = F1_date.split("/")[0];
		document.Search.F1mm.value = F1_date.split("/")[1];
		document.Search.F1yy.value = F1_date.split("/")[2];
        document.Search.F1mmyy.value = F1_date.split("/")[1]+"/"+F1_date.split("/")[2];
        document.Search.F2dd.value = F1_date.split("/")[0];
		document.Search.F2mm.value = F1_date.split("/")[1];
		document.Search.F2yy.value = F1_date.split("/")[2];
        document.Search.F2mmyy.value = F1_date.split("/")[1]+"/"+F1_date.split("/")[2];
        document.Search.Flight1_Date.value =F1_date.split("/")[0]+"/" +F1_date.split("/")[1]+"/"+F1_date.split("/")[2];
        document.Search.Flight2_Date.value =F1_date.split("/")[0]+"/" +F1_date.split("/")[1]+"/"+F1_date.split("/")[2];
        document.Search.userSelectedMonth.value = F1_date.split("/")[1] ;
	    document.Search.userSelectedYear.value = F1_date.split("/")[2];
        //document.Search.F1dd.disable = true;
		//document.Search.F1mm.disable = true;
		//document.Search.F1yy.disable = true;
		setOneWay();
		
	}
	else if (orm == 'R')
	{
		document.Search.ORM[0].checked = false;
		document.Search.ORM[1].checked = true;
		//document.Search.ORM[2].checked = false;		
		document.Search.F1dd.value = F1_date.split("/")[0];
		document.Search.F1mm.value = F1_date.split("/")[1];
		document.Search.F1yy.value = F1_date.split("/")[2];
        document.Search.F1mmyy.value = F1_date.split("/")[1]+"/"+F1_date.split("/")[2];
        document.Search.F2dd.value = F2_date.split("/")[0];
		document.Search.F2mm.value = F2_date.split("/")[1];
		document.Search.F2yy.value = F2_date.split("/")[2];
        document.Search.F2mmyy.value = F2_date.split("/")[1]+"/"+F2_date.split("/")[2];
        document.Search.Flight1_Date.value =F1_date.split("/")[0]+"/" +F1_date.split("/")[1]+"/"+F1_date.split("/")[2];
        document.Search.Flight2_Date.value =F2_date.split("/")[0]+"/" +F2_date.split("/")[1]+"/"+F2_date.split("/")[2];
        document.Search.userSelectedMonth.value = F1_date.split("/")[1] ;
	    document.Search.userSelectedYear.value = F1_date.split("/")[2];
        setReturn();
	}


}

function ammendDFlight()
{
	var canSubmit = false;
	if (document.Search.src1.value !=="" )
	  {
		  canSubmit = checkDateFormat( Search.Flight1_Date );
		  if( document.Search.src2.value !="" )
		  {
			canSubmit = canSubmit && checkDateFormat( Search.Flight1_Date ) && checkDateFormat( Search.Flight2_Date ) && compareDatesFlight(Search.Flight1_Date.value,Search.Flight2_Date.value) ;

			if( document.Search.src3.value !="" )
			{
				canSubmit = canSubmit && checkDateFormat( Search.Flight3_Date ) && compareDatesFlight(Search.Flight2_Date.value,Search.Flight3_Date.value) ;

				if( document.Search.src4.value !="" )
				{
					canSubmit = canSubmit && checkDateFormat( Search.Flight4_Date ) && compareDatesFlight(Search.Flight3_Date.value,Search.Flight4_Date.value) ;
				}
			}
		  }
		 }

	document.Search.action = document.Search.FlightPageIpAndPort.value + "/AmmendFlights.php";
	if (canSubmit)
      {
      	if ( validateFlight1Date( document.Search.Flight1_Date.value ))
		{
			document.Search.submit();
		}
		else
		{
			// do nothing
		}
      }
      else
      {
      	alert("Check Dates.");
      }
}
