var currentDate = new Date();

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;
	var  F2dd;
	var  F2mm;
	var  F2yy;
	cal2 = document.getElementById('cal2');
	cal2.setAttribute("readOnly","true");
	cal2.style.backgroundColor='eeeeee';
	
	F2dd = document.getElementById('F2dd');
	F2dd.setAttribute("readOnly","true");
	F2dd.style.backgroundColor='eeeeee';
	F2mm = document.getElementById('F2mm');
	F2mm.setAttribute("readOnly","true");
	F2mm.style.backgroundColor='eeeeee';
	F2yy = document.getElementById('F2yy');
	F2yy.setAttribute("readOnly","true");
	F2yy.style.backgroundColor='eeeeee';
	
	var obj;
	obj = document.getElementById('FLRetDates');
	obj.style.display="none";
	obj = document.getElementById('FLRet');
	obj.style.display="none";
}

function setReturn()
{
	var  cal2;
	cal2 = document.getElementById('cal2');
	cal2.removeAttribute("readOnly","false");
	cal2.style.backgroundColor='ffffff';
	
	F2dd = document.getElementById('F2dd');
	F2dd.removeAttribute("readOnly","false");
	F2dd.style.backgroundColor='ffffff';
	F2mm = document.getElementById('F2mm');
	F2mm.removeAttribute("readOnly","false");
	F2mm.style.backgroundColor='ffffff';
	F2yy = document.getElementById('F2yy');
	F2yy.removeAttribute("readOnly","false");
	F2yy.style.backgroundColor='ffffff';
	
	var obj;
	obj = document.getElementById('FLRetDates');
	obj.style.display="";
	obj = document.getElementById('FLRet');
	obj.style.display="";
}

function formsubmit()
{
   var canSubmit= false;
   
   if(checkInfantsNo() && checkTotalPassengers()){

	  if (document.Search.ORM[0].checked == true)
	  {
		  document.Search.Flight2_Date.value = "";
		  document.Search.src2.value = "";
		  document.Search.des2.value = "";
		  canSubmit = checkDateFormat( Search.Flight1_Date );

		  document.Search.action = "http://www.travelpaths.com/flights/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 ) && compareDates(document.Search.Flight1_Date.value,document.Search.Flight2_Date.value)  ;
		  document.Search.action = "http://www.travelpaths.com/flights/ValidationInputOneRet.php";
	  }

	  else
	  {
	          if (document.Search.src1.value !=="" || document.Search.des1.value!="")
			  {
				  canSubmit = checkDateFormat( Search.Flight1_Date );
				  if( document.Search.src2.value !="" || document.Search.des2.value!="")
				  {
					canSubmit = canSubmit && checkDateFormat( document.Search.Flight1_Date ) && checkDateFormat( document.Search.Flight2_Date ) && compareDates(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 ) && compareDates(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 ) && compareDates(document.Search.Flight3_Date.value,document.Search.Flight4_Date.value) ;
						}
					}
				  }
				 }


	          document.Search.action = "http://www.travelpaths.com/flights/ValidationInput.php";
	  }

      if (canSubmit)
      {
      	if ( validateFlight1Date( document.Search.Flight1_Date.value ))
		{
			document.Search.Searched.value="yes";
			document.Search.submit();
		}
		else
		{
			//do nothing
		}

      }
      else
      {
      	alert("Check Dates.");
      }

   }
}
function setFlightType()
{
	var orm;
	orm = getQueryVariable("ORM");
	if (orm=="O")
	{	
		document.Search.ORM[0].checked = true;
		document.Search.ORM[1].checked = false;		
		setOneWay();
	}
	else if (orm=="R")
	{	
		document.Search.ORM[0].checked = false;
		document.Search.ORM[1].checked = true;		
		setReturn();
	}
	else
	{
		document.Search.ORM[0].checked = false;
		document.Search.ORM[1].checked = true;		
		setReturn();
	}

}

function getQueryVariable(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) {     
      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 )
		 {             
			 return true;
		 }
         else
         {
            alert( "Invalid Return Date" );
            return false;
         }

      }
      else
      {
         alert( "Invalid Return Date" );
         return false;
      }

}




  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(adt, chd, inf, airline, bookingClass, orm)
{
   var F1_date="";
   var F2_date="";
   
   
   F1_date = document.Search.Flight1_Date.value;
   F2_date = document.Search.Flight2_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;

	}
    

  	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.F1dd.value = F1_date.split("/")[0];
		document.Search.F1mm.value = F1_date.split("/")[1];
		document.Search.F1yy.value = F1_date.split("/")[2];		
		setOneWay();
		
	}
	else if (orm == 'R')
	{
		document.Search.ORM[0].checked = false;
		document.Search.ORM[1].checked = true;			
		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.F2dd.value = F2_date.split("/")[0];
		document.Search.F2mm.value = F2_date.split("/")[1];
		document.Search.F2yy.value = F2_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 ) && compareDates(Search.Flight1_Date.value,Search.Flight2_Date.value) ;

			if( document.Search.src3.value !="" )
			{
				canSubmit = canSubmit && checkDateFormat( Search.Flight3_Date ) && compareDates(Search.Flight2_Date.value,Search.Flight3_Date.value) ;

				if( document.Search.src4.value !="" )
				{
					canSubmit = canSubmit && checkDateFormat( Search.Flight4_Date ) && compareDates(Search.Flight3_Date.value,Search.Flight4_Date.value) ;
				}
			}
		  }
		 }

	document.Search.action = "AmmendFlights.php";
	if (canSubmit)
      {
      	if ( validateFlight1Date( document.Search.Flight1_Date.value ))
		{
			document.Search.submit();
		}
		else
		{
			// do nothing
		}
      }
      else
      {
      	alert("Check Dates.");
      }
}
