/* This javascript is to hide/show the search boxes */
//////////////////////////////////// Global Variables /////////////////////////////////////////
var SearchType = 1;

function QuickSearch(){
    if ( SearchType == 1 ) {
	   //document.forms['QuickSearchForm'].submit();
	   //alert("Manfredo Valente!");
	   SubmitIt();
	}else{
	    /*
	    ClassTicket.style.display = 'none';
		FlightOptions.style.display = 'none';
		Carrier.style.display = 'none';
		*/
		extraOptz.style.display = 'none';
		
		DestinationeAdv.style.display = 'none';
		Destinatione.style.display = 'block';
		
		SearchType = 1;
		searchHeader.innerHTML = "Quick search";
	}
}

function AdvancedSearch(){
    if ( SearchType == 2 ) {
	   //document.forms['QuickSearchForm'].submit();
	   //alert("Manfredo Valente!");
	   SubmitIt();
	}else{
	    /*
	    ClassTicket.style.display = 'block';
		FlightOptions.style.display = 'block';
		Carrier.style.display = 'block';
		*/
		extraOptz.style.display = 'block';
		
		DestinationeAdv.style.display = 'block';
		Destinatione.style.display = 'none';
		
		SearchType = 2;
		searchHeader.innerHTML = "Advanced search";
	}
}
/******************************************************/

function validateInsurance(){
   document.InsuranceForm.submit();
}

/*Holiday seaech starts here*/
function Validate() {
var datemonth  = document.Form1.date_month ? document.Form1.date_month.value : "";
var dateday    = document.Form1.date_day ? document.Form1.date_day.value : "";
var dep_out_gr = document.Form1.fromarea ? document.Form1.fromarea.value : "";
var dep_out_1  = document.Form1.from_1 ? document.Form1.from_1.value.replace(/\s+/g,"") : "";
var dep_out_2  = document.Form1.from_2 ? document.Form1.from_2.value.replace(/\s+/g,"") : "";
var dep_out_3  = document.Form1.from_3 ? document.Form1.from_3.value.replace(/\s+/g,"") : "";
var nights     = document.Form1.nights ? document.Form1.nights.value.replace(/\s+/g,"") : "";
var year = "20" + datemonth.substring(2,0);
var month = datemonth.substring(4,2);
if((datemonth == "0511" && parseInt(dateday,10) < 3) || !dateCheck(year,month,dateday))
{
alert("Please make sure you select a valid departure date that is in the future.");
return false;
}
else if(!(dep_out_gr || dep_out_1 || dep_out_2 || dep_out_3))
{
alert("Please specify your departure point or grouping.");
return false;
}
else if(!nights)
{
alert("Please specify number of nights.");
return false;
}
return true;
}
function dateCheck(y, m, d)
{
if(m != '')
{
if((m == "04" || m == "06" || m == "09" || m == "11") && d == "31")
{
return false;
}
if(m == "02" && ((parseInt(y)%4 != 0 && d == "29") || d == "30" || d == "31"))
{
return false;
}
return true;
}
return false;
}
/*Holiday search ends here*/

/* START: new JavaScript for the flight search */

function CheckForm(){

if (!CheckDate()){
	return false;
}else if (!IsDestinationSelected()){
	return false;
}
return true;

}

function IsDestinationSelected(){

if ( SearchType == 1 ){
	if (document.QuickSearchForm.Destination[document.QuickSearchForm.Destination.selectedIndex].value=='none'){
		alert("Please Select a Destination Airport");
		return false;
	}
	//alert(document.QuickSearchForm.Departure[document.QuickSearchForm.Departure.selectedIndex].value);
}else{

    if (document.QuickSearchForm.DepartureAdv.value=='' ){
	    document.QuickSearchForm.DepartureAdv.focus();
		alert("Please Specify a Departure Airport");
		return false;
	}
    if (document.QuickSearchForm.DestinationAdv.value=='' ){
		alert("Please Specify a Destination Airport");
		document.QuickSearchForm.DestinationAdv.focus();
		return false;
	}
	
	//alert( document.QuickSearchForm.DepartureAdv.value );
	//alert( document.QuickSearchForm.DestinationAdv.value );
	
}

return true;

}


function SubmitIt(){

if ( CheckForm() ){
	document.QuickSearchForm.submit();
	window.scroll(0,0);
	return true;
}else{
    return false;
}

document.forms['QuickSearchForm'].submit();

}


function CheckDate(){
var strDepartDay=document.QuickSearchForm.DD1[document.QuickSearchForm.DD1.selectedIndex].value;
var strReturnDay=document.QuickSearchForm.DD2[document.QuickSearchForm.DD2.selectedIndex].value;
var strDepartDate=document.QuickSearchForm.MD1[document.QuickSearchForm.MD1.selectedIndex].value;
var strReturnDate=document.QuickSearchForm.MD2[document.QuickSearchForm.MD2.selectedIndex].value;
if (IsReturnDateAfterDepartureDate(strDepartDay, strReturnDay,  strDepartDate, strReturnDate)!=1 ){
alert("Return Date MUST BE AFTER Departure Date"); return false;}
return true;
}

function IsReturnDateAfterDepartureDate(vD1,vD2,vM1,vM2){
var m1,y1,m2,y2;if (vM1.substr(0,1) == "0"){m1=parseInt(vM1.substr(1,1));}else{m1=parseInt(vM1.substr(0,2));}y1=parseInt(vM1.substr(2,4));
if (vM2.substr(0,1) == "0"){m2=parseInt(vM2.substr(1,1));}else{m2=parseInt(vM2.substr(0,2));}y2=parseInt(vM2.substr(2,4));
vD1=parseInt(vD1);vD2=parseInt(vD2);
if (y1>y2){return 0;}else if (y1==y2){if (m1>m2){return 0;}else if ((m1==m2) && (vD1>=vD2)){return 0;}}return 1;}

function MM_checkPlugin(plgIn, theURL, altURL, autoGo) { //v3.0
  var ok=false; document.MM_returnValue = false;
  with (navigator) if (appName.indexOf('Microsoft')==-1) ok=(plugins && plugins[plgIn]);
  else if (appVersion.indexOf('3.1')==-1) { //not Netscape or Win3.1
    if (plgIn.indexOf("Flash")!=-1 && window.MM_flash!=null) ok=window.MM_flash;
    else if (plgIn.indexOf("Director")!=-1 && window.MM_dir!=null) ok=window.MM_dir;
    else ok=autoGo; }
  if (!ok) theURL=altURL; if (theURL) window.location=theURL;
}

function CheckDest()
{
	var blnField;
	if (document.QuickSearchForm.Field.value.length == 0)
	{
		if (document.QuickSearchForm.List.options[document.QuickSearchForm.List.selectedIndex].value == 'none')
		{
			alert("Please select a destination airport.");
			return;
		}
		
		blnField = false;
	}
	else
		blnField = true;
	
	if (blnField)
		document.QuickSearchForm.CA1.value = document.form.Field.value;
	else
		document.QuickSearchForm.CA1.value = document.form.List.options[document.QuickSearchForm.List.selectedIndex].value;

	setqueryURL('form');	
}

var arrMonth = ["January", "February", "March", "April",
					   "May", "June", "July", "August", "September",
					   "October", "November", "December"];

var g_intDay, g_strMonthYear;

function GenerateDayList(strID, intDaysOffset)
{

	GenerateBookDate(intDaysOffset);

	document.write('<select style="font-family:Verdana;font-size:8pt; color: #666666; width: 40px;" name="' + strID + '" onSubmit="CheckDate()">');
	
	var intCount;
	for(intCount=1; intCount <= 31; intCount++)
	{
		document.write('<option value="' + intCount + '"');
		if (g_intDay == intCount)
			document.write(' selected');
		document.write('>' + intCount + '</option>');
	}
	
	document.write('</select> ');
}

function GenerateMonthList(strID, intDaysOffset)
{

	document.write('<select style="font-family:Verdana;font-size:8pt; width: 130px; color: #666666;" name="' + strID + '" onSubmit="CheckDate()">');
	GenerateBookDate(intDaysOffset);
	var dateCurrent = new Date();
	dateCurrent.setDate(1);
	var intCount, strCurMonthYear, strMonthYearValue;
	for(intCount=0; intCount < 11; intCount++)
	{
		strCurMonthYear = arrMonth[dateCurrent.getMonth()] + " " + dateCurrent.getFullYear();
		if (dateCurrent.getMonth() < 9)
			strCurMonthYearValue = "0" + (dateCurrent.getMonth()+1) + dateCurrent.getFullYear();
		else
			strCurMonthYearValue = "" + (dateCurrent.getMonth()+1) + dateCurrent.getFullYear();
		document.write('<option value="' + strCurMonthYearValue + '"');
		if (strCurMonthYear == g_strMonthYear)
			document.write(' selected');
		document.write('>' + strCurMonthYear + '</option>');
		
		// Add a month to the current date
		dateCurrent.setMonth(dateCurrent.getMonth()+1);
	}
	
	document.write('</select>');
}

function GenerateBookDate(intDaysOffset)
{
	// Get the current date

	var dateBook = new Date;
	
	// Add 7 days to the date
	g_intDay = dateBook.getDate() + 7 + intDaysOffset;
	dateBook.setDate(g_intDay);
	
	// Get the booking date
	g_intDay = dateBook.getDate();
	g_strMonthYear = arrMonth[dateBook.getMonth()] + " " + dateBook.getFullYear();
	return dateBook;
}
/*********************************************/
/* END: new JavaScript for the flight search */

function EW_checkemail(object_value) {
   punto = object_value.indexOf('.');
   et = object_value.indexOf("@");
   
   if (et == -1 || punto == -1){      
		return false;
   }else{
      et1 = object_value.indexOf("@",et+1);
	  if (et1 != -1)	    
		  return false;
	  for (var i = punto+1; i < object_value.length; i++)	{
		if(object_value.charAt(i) == '.')
		   punto = i;
	  }
	  if (et > punto)        
		     return false;
   }
	return true;
}

function getObj(nm) {
	if (document.getElementById) {
		return document.getElementById(nm);
	} else if (document.all) {
		return document.all[nm];
	} else {
		return eval("document." + nm);
	}
}

function CheckForward2Friend(formName){
    valid = 1;
    message = "You have not completed all the required fields, please check:\n";

	if (formName.elements['Name'].value == "") {
		valid = 0;
		message += "Name\n";
		getObj("Name").style.border = "#aa0000 solid 1px";
	} else {
		getObj("Name").style.border = "#A5ACB2 solid 1px";
	}
	
	if  ( (formName.elements['Email'].value == "") ){
		valid = 0;
		message += "Email address \n";
		getObj("Email").style.border = "#aa0000 solid 1px";
	} else {
	    if ( !EW_checkemail( formName.elements['Email'].value ) ){
		     valid = 0;
		     message += "Email address is wrong \n";
		     getObj("Email").style.border = "#aa0000 solid 1px";
		}else{
	       getObj("Email").style.border = "#A5ACB2 solid 1px";
		}
	}

    if (valid == 1){
        formName.submit();
    }else{
	    alert(message);
	}

}

function CheckNewsLetter(formName){
    valid = 1;
    message = "You have not completed all the required fields, please check:\n";

	if  ( (formName.elements['Email2'].value == "") ){
		valid = 0;
		message += "Email address \n";
		getObj("Email2").style.border = "#aa0000 solid 1px";
	} else {
	    if ( !EW_checkemail( formName.elements['Email2'].value ) ){
		     valid = 0;
		     message += "Email address is wrong \n";
		     getObj("Email2").style.border = "#aa0000 solid 1px";
		}else{
	       getObj("Email2").style.border = "#A5ACB2 solid 1px";
		}
	}

    if (valid == 1){
        formName.submit();
    }else{
	    alert(message);
	}

}

// Function for the navigation in the search hotels page:
function DisplayOffSet(OffSet){
     var formName = document.forms['displayOffsetForm'];
	 formName.displayOffset.value = OffSet;
	 //alert(formName.displayOffset.value);
	 formName.submit();
}
