// This function checks for a valid date on the form
function CheckDate() {
	
	SelDay = document.forms.MainForm.Day.options[document.forms.MainForm.Day.selectedIndex].value;
	l = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).length;
	if (l==7)
	{
		SelMonth = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(0,2);
		SelYear = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(3,7);
	}
	else // if l==6
	{
		SelMonth = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(0,1);
		SelYear = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(2,6);
	}
	
	LeapYear = SelYear % 4;

	if (LeapYear == 0) {
		DateArray = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
	} else {
		DateArray = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	}

	if (SelDay > DateArray[SelMonth]) {
		alert("You have selected an invalid departure date. Please check and try again");
		return false;
	} else {
		// DEP DATE IS OK - CHECK RETURN DATE
		SelDay = document.MainForm.RetDay.options[document.MainForm.RetDay.selectedIndex].value;
		SelMonth = Math.abs((document.MainForm.month_2.options[document.MainForm.month_2.selectedIndex].value).slice(0,2));
		SelYear = (document.MainForm.month_2.options[document.MainForm.month_2.selectedIndex].value).slice(2,6);

		LeapYear = SelYear % 4;

		if (LeapYear == 0) {
			DateArray = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
		} else {
			DateArray = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
		}

		if (SelDay > DateArray[SelMonth]) {
			alert("You have selected an invalid return date. Please check and try again");
			return false;
		} else {
			// DATES CHECK OUT OK
			return true;
		}
	}
}

function dateAlter() 
{
	// GET TODAYS DATE
	dateToday = new Date();
	// GET CURRENT MONTH
	thisMonth = dateToday.getMonth() + 1;
	// GET CURRENT DAY
	thisDate   = dateToday.getDate();
	thisYear   = dateToday.getFullYear();

	// GET CHOSEN DEPARTURE DAY
	departDay = document.MainForm.Day.selectedIndex
	// GET CHOSEN DEPARTURE MONTH&YEAR
	departMonthYear = document.MainForm.month_1.value
	// GET CURRENT RETURN DAY
	returnDay = document.MainForm.RetDay.selectedIndex
	// GET CURRENT RETURN MONTH&YEAR
	returnMonthYear = document.MainForm.month_2.value
	

	// BUILD AN ARRAY OF THE MONTH&YEAR VALUES
	var MonthArray = new Array();
	
	for (i=thisMonth;i<=12;i++) {
			MonthArray[i] = i.toString()+'-'+thisYear.toString();
	}
	
	for (i=1;i<thisMonth;i++) {
			MonthArray[i] = i.toString()+'-'+(parseInt(thisYear,10)+1).toString();
	}
	
	// NOW FIND OUT HOW MANY DAYS ARE IN EACH MONTH DEPENDING ON WHETHER THE CURRENT YEAR IS A LEAP YEAR OR NOT
	if (thisYear % 4 == 0){
		var DateArray = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}
	else {
		var DateArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}
	
	// GET CHOSEN DEPARTURE MONTH NUMBER & MAX DAYS IN THAT MONTH
	if (departMonthYear.length==7)
		monthNumber = departMonthYear.substr(0,2);
	else
		monthNumber = departMonthYear.substr(0,1);

	maxDays = DateArray[monthNumber-1];
	if (departDay + 14 >= maxDays){
		document.MainForm.RetDay.selectedIndex = ((departDay-maxDays) + 14);
		if (departMonthYear.substr(0,2) == '12'){
			//thisYear = parseInt(thisYear,10)+1
			//departMonthYear = (parseInt(thisYear,10)+1).toString() + '01';
			document.MainForm.month_2.value = MonthArray[1];
		} else {
			document.MainForm.month_2.value = MonthArray[parseInt(departMonthYear.substr(0,2),10)+1];
		}
	}
	else if (departDay + 14 < maxDays){
		document.MainForm.RetDay.selectedIndex = departDay + 14;
		document.MainForm.month_2.value = MonthArray[parseInt(departMonthYear.substr(0,2),10)];
	}
}
	
function dateChange(){
	dateToday = new Date();
	thisYear   = dateToday.getYear();
	thisMonth = dateToday.getMonth() + 1;
	thisDate   = dateToday.getDate();
	tomorrowDate = dateToday.getDate()+14;

	var MonthArray = new Array();


	//By Kamran
	//Firefox getYears() returns year-1900
	if (navigator.userAgent.indexOf("Firefox")!=-1)
		thisYear=1900+thisYear;

	for (i=thisMonth;i<=12;i++) {
				MonthArray[i] = i.toString()+'-'+thisYear.toString();
		}
		
		for (i=1;i<thisMonth;i++) {
				MonthArray[i] = i.toString()+'-'+(thisYear+1).toString();
	}
	
	if (thisYear % 4 == 0){
		var DateArray = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}
	else {
		var DateArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}

	maxDays    = DateArray[thisMonth-1];
	if (thisDate + 14 > maxDays){
		document.MainForm.Day.selectedIndex = ((thisDate-maxDays) + 13);
		if (thisMonth == 12){
		thisMonth = 0;
		}
		document.MainForm.month_1.value = MonthArray[thisMonth+1];
		}
	else if (thisDate + 14 < maxDays){
		document.MainForm.Day.selectedIndex = thisDate + 13;
		document.MainForm.month_1.value = MonthArray[thisMonth];
		}
	else if (thisDate + 14 == maxDays){
		document.MainForm.Day.selectedIndex = thisDate + 13;
		document.MainForm.month_1.value = MonthArray[thisMonth];
		}

	if ((tomorrowDate > maxDays) || (tomorrowDate + 14 > maxDays)) {
		document.MainForm.RetDay.selectedIndex = ((tomorrowDate-maxDays) + 13);
		if (thisMonth == 12){
		thisMonth = 00;
		}
		document.MainForm.month_2.value = MonthArray[thisMonth+1];
		}
	else if (tomorrowDate < maxDays){
		document.MainForm.RetDay.selectedIndex = tomorrowDate + 13;
		document.MainForm.month_2.value = MonthArray[thisMonth];
	}
}

	
	
//==============================================================================
// THIS SECTION GETS CURRENT DATE VALUES AND BUILDS ARRAY OF DATES FOR 12 MONTHS
//==============================================================================

dateToday = new Date();
thisYear   = dateToday.getFullYear();
nextYear	 = dateToday.getFullYear() + 1;
thisMonth = dateToday.getMonth() + 1;
thisDate   = dateToday.getDate();
//tomorrowDate = dateToday.getDate()+14;

var MonthArray = new Array();

function writemonths() {

	for (i=thisMonth;i<=12;i++) {
				document.write ('<option value="' +i.toString() + '-' + thisYear.toString() + '">'  + MonthName[i.toString()] + ' ' + (thisYear.toString()).slice(2) +  '</option>');
	}

	for (i=1;i<thisMonth;i++) {
				document.write ('<option value="' +i.toString() + '-' + (nextYear).toString() + '">'  + MonthName[i.toString()] + ' ' + (nextYear.toString()).slice(2) +  '</option>');
	}



}

var MonthName = new Array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');




function InsertOption(strValue, strText)
{
			inserthere = document.getElementById('destopt')
			var objOption = document.createElement('option')
			objOption.value = strValue
			objOption.text = strText
			
			try {
    			inserthere.add(objOption, null); // standards compliant; doesn't work in IE
  		}
  		catch(ex) {
    			inserthere.add(objOption); // IE only
  		}

}


function addDays(myDate,days) {
    return new Date(myDate.getTime() + days*24*60*60*1000);
}
var varDate = addDays(new Date(),5)

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// * hand all instances of the Date object to this function for "repairs"

function checkAirportCode() {
if (document.MainForm.onewayswitch[0].checked)
	setCookie('onewayswitch', 1, varDate);
else
	setCookie('onewayswitch', 0, varDate);

	if (document.MainForm.other.value=='')
	{
		if (document.MainForm.to_list.value == 'U/L' || document.MainForm.to_list.value == '0') 
		{
			alert('Please choose a destination before proceeding');
			return false;
		}
		else
		{
			document.MainForm.to.value = document.MainForm.to_list.value;
		}
	}
	else
	{
		
		if ( (document.MainForm.other.value).length == 3 )
		{
			document.MainForm.to.value = document.MainForm.other.value;
		}
		else if ( (document.MainForm.other.value).length < 6 )
		{
			alert('Specify 3 letter city code or click on "Other cities"!');
			return false;
		}
		else 
		{
			s = document.MainForm.other.value;
			if (s.substr(s.length-1,1) != ')' || s.substr(s.length-5,1) != '(')
			{
				alert('Specify 3 letter city code or click on "Other cities"!');
				return false;
			}
			document.MainForm.to.value = s.substr(s.length-4,3);
		}
	}
	
	if (!CheckDate()) {
		return false;
	}

	if ((document.MainForm.month_1.selectedIndex*31 + document.MainForm.Day.selectedIndex) > (document.MainForm.month_2.selectedIndex*31 + document.MainForm.RetDay.selectedIndex)) {
		alert('Return date is before departure date.\nPlease correct and try again.');
		return false;
	}
	
	setCookie('tarif_klasse', document.MainForm.tarif_klasse.value, varDate)
	setCookie('pax', document.MainForm.pax.value, varDate)
	setCookie('pax_chd', document.MainForm.pax_chd[document.MainForm.pax_chd.selectedIndex].value, varDate)
	setCookie('pax_inf', document.MainForm.pax_inf[document.MainForm.pax_inf.selectedIndex].value, varDate)
	setCookie('pax_adult', document.MainForm.pax_adult[document.MainForm.pax_adult.selectedIndex].value, varDate)
	setCookie('pax', document.MainForm.pax_adult[document.MainForm.pax_adult.selectedIndex].value, varDate)
	setCookie('From', document.MainForm.DepAptCode.value, varDate)
	setCookie('Destination', document.MainForm.to.value, varDate)
	// setCookie('DestinationDisplay', document.MainForm.txtArrAptCode.value, varDate)
	// setCookie('DestinationHidden', document.MainForm.ArrAptCode.value, varDate)
	setCookie('DepDay', document.MainForm.Day.value, varDate)
	setCookie('DepMonYr', document.MainForm.month_1.value, varDate)
	setCookie('RetDay', document.MainForm.RetDay.value, varDate)
	setCookie('RetMonYr', document.MainForm.month_2.value, varDate)
	setCookie('other', document.MainForm.other.value, varDate)
	setCookie('to_list', document.MainForm.to_list.value, varDate)

	return true;
}
function changeDest(strDest,strCode) {
	document.MainForm.txtArrAptCode.value=(document.MainForm.menuArrAptCode[document.MainForm.menuArrAptCode.selectedIndex].text + ' (' + document.MainForm.menuArrAptCode[document.MainForm.menuArrAptCode.selectedIndex].value + ')').toUpperCase();
	document.MainForm.ArrAptCode.value=(document.MainForm.menuArrAptCode[document.MainForm.menuArrAptCode.selectedIndex].value).toUpperCase();
}

function openpopupDest(){
window.open('index.php?main_page=otherairports','new1','toolbar=no,location=no,statusbar=no,menubar=no,scrollbars=1,resizable=yes,width=600,height=220,top=220,left=220');
//document.MainForm.to_list.value='U/L';
}
function rowOverEffect(object) {
  if (object.className == 'dataTableRow') object.className = 'dataTableRowOver';
}

function rowOutEffect(object) {
  if (object.className == 'dataTableRowOver') object.className = 'dataTableRow';
}