function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function ensure_field (form, field)
{

  if (!form || !field)
    return;
  var doc = form.ownerDocument;
  if (!form.elements[field]) {
        var node = doc.createElement ("input");
        node.setAttribute ("type", "hidden");
        node.setAttribute ("value", "");
        node.setAttribute ("name", field);

        form.appendChild (node);
        if (!form.elements[field]) {
                form.elements[field] = node;
        }
  }
}


function apsel(a, b) {
 
    /* Added by Air Malta IT dept to allow LGW-CTA and STO-OSL */
  if ((a[a.selectedIndex].value == 'CTA') && (b[b.selectedIndex].value == 'LGW')) {return true;}  
  if ((a[a.selectedIndex].value == 'LGW') && (b[b.selectedIndex].value == 'CTA')) {return true;}
  if ((a[a.selectedIndex].value == 'ARN') && (b[b.selectedIndex].value == 'OSL')) {return true;}
  if ((a[a.selectedIndex].value == 'OSL') && (b[b.selectedIndex].value == 'ARN')) {return true;}
  if ((a[a.selectedIndex].value == 'CTA') && (b[b.selectedIndex].value == 'MUC')) {return true;}
  if ((a[a.selectedIndex].value == 'MUC') && (b[b.selectedIndex].value == 'CTA')) {return true;}  
  if ((a[a.selectedIndex].value == 'CTA') && (b[b.selectedIndex].value == 'GVA')) {return true;}
  if ((a[a.selectedIndex].value == 'GVA') && (b[b.selectedIndex].value == 'CTA')) {return true;}  

  if ((a[a.selectedIndex].value == 'BCN') && (b[b.selectedIndex].value == 'CTA')) {return true;}
  if ((a[a.selectedIndex].value == 'CTA') && (b[b.selectedIndex].value == 'BCN')) {return true;}
  if ((a[a.selectedIndex].value == 'CMN') && (b[b.selectedIndex].value == 'CTA')) {return true;}
  if ((a[a.selectedIndex].value == 'CTA') && (b[b.selectedIndex].value == 'CMN')) {return true;}
  
  if ((a[a.selectedIndex].value == 'FCO') && (b[b.selectedIndex].value == 'REG')) {return true;}
  if ((a[a.selectedIndex].value == 'REG') && (b[b.selectedIndex].value == 'FCO')) {return true;}  
  if ((a[a.selectedIndex].value == 'PMO') && (b[b.selectedIndex].value == 'NAP')) {return true;}
  if ((a[a.selectedIndex].value == 'NAP') && (b[b.selectedIndex].value == 'PMO')) {return true;}  

  if (a[a.selectedIndex].value!='MLA') {
    for (i=0; i<b.length; i++) {
      if (b[i].value == 'MLA') {
        b.selectedIndex=i;
        break;
      }
    }
  }
}

function amval(frm) {
    
      
      
    
     if ((document.getElementById("originAirports").value == "") || (document.getElementById("destinationAirports").value == "")) 
    {
       alert ('The Origin or Destination cannot be left out. Please review your search criteria');
       return false;
    }
    
     if (document.getElementById("originAirports").value ==  document.getElementById("destinationAirports").value) 
    {
       alert ('The Origin and Destination cannot be the same. Please review your search criteria');
       return false;
    } 
     /* The number of infants cannot exceed the number of adults. */         
    if (document.getElementById("ctl00_Sidebooking1_lstinfant").value > document.getElementById("ctl00_Sidebooking1_lstadult").value) 
    {
       alert("The number of Infants cannot exceed the number of Adults");
       return false;
    }


    function getDaysOfMonth (month, year)
    {
       var days;
       if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
          days=31;
       else if (month==4 || month==6 || month==9 || month==11) days=30;
       else if (month==2)
       {
          if (isLeapYear(year)) days=29;
          else days=28;
       }
       return (days);
    }


    function isLeapYear (year){
       if (((year % 4)==0) && ((year % 100)!=0) || ((year % 400)==0))
          return (true);
       else
          return (false);
    }

    var now = new Date();
    var month = now.getMonth();
    month++;
    var year = now.getYear();
    
    
    var selectedoutyearmonth = document.getElementById("ctl00_Sidebooking1_outmonth").value
    var selectedoutyear =  selectedoutyearmonth .substring(0,4)
    var selectedoutmonth = selectedoutyearmonth .substring(5,selectedoutyearmonth .length)
    var selectedoutday = document.getElementById("ctl00_Sidebooking1_outday").value

    
    out_days_limit=getDaysOfMonth (parseInt(selectedoutmonth), selectedoutyear);
    if (selectedoutday > out_days_limit) {
       alert ("Departure date does not exist, please select another one");
       document.getElementById("ctl00_Sidebooking1_outday").focus();
       return false;
    }

    
    if (document.getElementById("ctl00_Sidebooking1_tripreturn").checked == true)
	{
        var selectedretyearmonth = document.getElementById("ctl00_Sidebooking1_retmonth").value
        var selectedretyear =  selectedretyearmonth.substring(0,4)
        var selectedretmonth = selectedretyearmonth.substring(5,selectedretyearmonth .length)
        var selectedretday = document.getElementById("ctl00_Sidebooking1_retday").value

        ret_days_limit=getDaysOfMonth (selectedretmonth, selectedretyear);
        if (selectedretday>ret_days_limit) {
           alert ("Return date does not exist, please select another one");
           document.getElementById("ctl00_Sidebooking1_retday").focus();
           return false;
        }
    }


    var today = new Date();
    var outDate = new Date(selectedoutyear, selectedoutmonth-1, selectedoutday);
     var retDate = new Date(selectedretyear, selectedretmonth-1, selectedretday);
    var outDateSeconds=outDate.getTime();
    var todayDateSeconds=today.getTime();
    var daysAfterNow=parseInt(((outDateSeconds)-todayDateSeconds)/86400000);
  
		
		 	
  	today = Date.UTC(y2k(today.getYear()),today.getMonth(),today.getDate(),0,0,0);
	starttime = Date.UTC(y2k(outDate.getYear()),outDate.getMonth(),outDate.getDate(),0,0,0);
    if (starttime < today)
	{
		alert("Departure Date Cannot occur prior to todays date. Please arrange selected date.")
		return false;	
	}
		
	
	if (document.getElementById("ctl00_Sidebooking1_tripreturn").checked == true)
	{	
	    starttime = Date.UTC(y2k(outDate.getYear()),outDate.getMonth(),outDate.getDate(),0,0,0);
  	    endtime = Date.UTC(y2k(retDate.getYear()),retDate.getMonth(),retDate.getDate(),0,0,0);
	    if (starttime > endtime)
	    {
		    alert("Departure Date Cannot occur after then Return date. Please arrange selected dates.")
		    return false;	
	    }
	}

  return true;
}


