var flightType = 'rt';

function lib_bwcheck(){
	this.ver=navigator.appVersion
	this.agent=navigator.userAgent.toLowerCase()
	this.dom=document.getElementById?true:false
	this.op5=(this.agent.indexOf("opera 5")>-1 || this.agent.indexOf("opera/5")>-1) && window.opera 
  this.op6=(this.agent.indexOf("opera 6")>-1 || this.agent.indexOf("opera/6")>-1) && window.opera   
  this.ie5 = (this.agent.indexOf("msie 5")>-1 && !this.op5 && !this.op6)
  this.ie55 = (this.ie5 && this.agent.indexOf("msie 5.5")>-1)
  this.ie6 = (this.agent.indexOf("msie 6")>-1 && !this.op5 && !this.op6)
	this.ie4=(this.agent.indexOf("msie")>-1 && document.all &&!this.op5 &&!this.op6 &&!this.ie5&&!this.ie6)
  this.ie = (this.ie4 || this.ie5 || this.ie6)
	this.mac=(this.agent.indexOf("mac")>-1)
	this.ns6=(this.agent.indexOf("gecko")>-1 || window.sidebar)
	this.ns4=(!this.dom && document.layers)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns6 || this.op5 || this.op6)
  this.usedom= this.ns6//Use dom creation
  this.reuse = this.ie||this.usedom //Reuse layers
  this.px=this.dom&&!this.op5?"px":""
	return this
}
var bw=new lib_bwcheck()
function MM_findObj(n, d)
{ //v4.0
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i < d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i < d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers()
{ //v3.0
var i,p,v,obj,args=MM_showHideLayers.arguments;
for (i=0; i < (args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
obj.visibility=v;
}
}
function layerWrite(id,nestref,text)
{
if (bw.ns4)
{
var lyr = (nestref)? eval('document.'+nestref+'.document.'+id+'.document') : document.layers[id].document
lyr.open()
lyr.write(text)
lyr.close()
}
else if (bw.ns6)
{
document.getElementById(id).innerHTML = text;
}
else if (bw.ie4||bw.ie5||bw.ie6)
{
document.all[id].innerHTML = text;
}
}
var alert2,alert3,alert6,alert7,alert8,alert9,alertCubaFlight;
alertCubaFlight="En viajes con destino, salida o escala en Cuba no podrá realizar la compra de dichos boletos con tarjetas de crédito expedidas fuera de México excepto American Express. El proceso de cobro se realizará en México, donde no pueden ser procesadas tarjetas Visa y MasterCard expedidas fuera de México.";
alert2="Debe especificar ciudad origen y destino";
alert3="Debe especificar el país donde compra";
alert6="La fecha de salida no puede ser posterior a la de regreso";
alert7="Favor de seleccionar al menos un pasajero";
alert8="Fecha fuera de rango";
alert9="La fecha es invalida. Por favor introduzca la fecha en el formato dd/mm/aaaa";
alertChild="Estimado usuario. De momento es imposible realizar reservaciones para menores viajando solos a través del portal, por lo que le suplicamos realizar su reservación vía telefónica en los siguientes números En México al 54 48 89 90 o del interior de la República al 01 800 50 220 00. Desde Estados Unidos o resto del mundo al (800)531 7921. Gracias por su comprensión y lamentamos el inconveniente.";
var winx = null;
function send1(par)
{
if ((par!=null)&&(par!=""))
{
document.bookingEngine.oculto.value = 1;
winx=window.open('/g4mx/booking/html/CitySearch/0,1189,,00.html?origin='+document.bookingEngine.origen.value,'popuppage','width=300,height=300,top=30000,left=100');	
winx.focus();
}
}
function send1a()
{
document.bookingEngine.oculto.value = 1;
winx=window.open('/g4mx/booking/html/CitySearch/0,1189,,00.html?origin='+document.bookingEngine.origen.value,'popuppage','width=300,height=300,top=30000,left=100');	
winx.focus();
}
function send2(par)
{
if ((par!=null)&&(par!=""))
{
document.bookingEngine.oculto.value = 2;
winx=window.open('/g4mx/booking/html/CitySearch/0,1189,,00.html?origin='+document.bookingEngine.destino.value,'popuppage','width=300,height=300,top=30000,left=100');
winx.focus();
}	
}
function send2a()
{
document.bookingEngine.oculto.value = 2;
winx=window.open('/g4mx/booking/html/CitySearch/0,1189,,00.html?origin='+document.bookingEngine.destino.value,'popuppage','width=300,height=300,top=30000,left=100');
winx.focus();
}
function validaLugar(obj, objCity, send )
{
var arr = obj.value.split('(');
if (arr.length > 1){
objCity.value = arr[1].substr(0,3);
}else if (arr.length == 1){ eval(send);	}
}
function hideInfo()
{
MM_showHideLayers('informa','','hide');
}

aInf = new Array ("12 a 61 años",
"2 a 11 años",
"62 años o más",
"menos de 2 años");
function showInfo(n)
{
var re = / /g;
var newstr=aInf[n].replace(re, "&nbsp;");
//Hola
layerWrite('informa',null,'<div align="right" class="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 10px; font-style: normal; line-height: normal; font-weight: normal; color: #000000; text-decoration: none"><font color="#25618D" style="font-weight: bold">'+newstr+'</font></div>');
MM_showHideLayers('informa','','show');
}
var showcalendar,showcalendar2,lastChar;
lastChar=0;
function pageOnLoad()
{
if (flightType=="rt")
flightTypeChanged(ROUND_TRIP);
if (flightType=="ow")	
flightTypeChanged(ONE_WAY_TRIP);	
if (flightType=="ms")
flightTypeChanged(MULTI_STOP_TRIP);	
}
function msg()
{
alert("This page is under construction");
}
function validate()
{
var k = (bw.ns6) ? DnEvents.which : window.event.keyCode;
if ( ((k< 65) || ((k >90)&&(k< 97)) || (k >122)) && (k!=233) && (k!=32) && (k!=225) && (k!=250) && (k!=237) && (k!=243) && (k!=209) && (k!=241) )
event.returnValue = false;
else
{
if ((lastChar==32)&&(k==32))
{
event.returnValue = false;	
}
lastChar=k;
}	
}
//
// en lugar de ponerlo en onload lo pongo aquÃ­ para que no
// modifique el funcionamiento de la pag. principal. FAFA
//

function CloseWin() 
{ 
if(winx){winx.close(); winx = null;}
else { winx = null;}
}

var ROUND_TRIP = 0;
var ONE_WAY_TRIP = 1;
var MULTI_STOP_TRIP = 2;
var PRICE = 1;
var SCHEDULE = 2;
var DATE_RANGE = 3;
var flag = ROUND_TRIP;

var smallMonths = new Array ("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
var bOculta = false;
function LayerFecha( onoff )
{
bOculta = (onoff == "none");
if (bw.dom || bw.ie5 || bw.ie4 )
{
	if (bw.ns6)
	{
	document.bookingEngine.returnDate.style.display=onoff;	
	document.getElementById('arrow2').style.visibility= (onoff== "")?"visible":"hidden";
	document.getElementById('returnDateTitle').style.visibility=(onoff== "")?"visible":"hidden";
	document.getElementById('returnDateTitle2').style.visibility=(onoff== "")?"visible":"hidden";
	}
	else
	{
		try
		{
			document.bookingEngine.returnDate.style.display=onoff;	
			arrow2.style.display = onoff;
			returnDateTitle.style.display= onoff;
			returnDateTitle2.style.display= onoff;	
		}
		catch(e)
		{
		}
	}
}
}

function flightTypeChanged(type)
{
if ( ROUND_TRIP == type )
{
LayerFecha( "" );
document.bookingEngine.action.value="/g4mx/public/templates/Main/0,1655,,00.html?templateLink=|g4mx|public|booking|FareDisplay.jsp";
document.bookingEngine.__flightType__.value="rt";	
}
else
{
LayerFecha( "none" )
//document.bookingEngine.origen.focus();
document.bookingEngine.action.value="/g4mx/public/templates/Main/0,1655,,00.html?templateLink=|g4mx|public|booking|FareDisplay.jsp";
if ( ONE_WAY_TRIP == type )
{
document.bookingEngine.__flightType__.value="ow";	
}
else
{
document.bookingEngine.__flightType__.value="ms";
document.bookingEngine.action.value="/g4mx/public/templates/Main/0,1655,,00.html?templateLink=|g4mx|public|booking|StopDisplay.jsp";
}
}
}
function countPassenger()
{
var total=0,adult=0,senior=0,infant=0,child=0;	
var band=false;
if (validaPasajeros)
{
adult= (document.bookingEngine.adultNumber.options[document.bookingEngine.adultNumber.options.selectedIndex].value)*1;
child= (document.bookingEngine.childNumber.options[document.bookingEngine.childNumber.options.selectedIndex].value)*1;
senior= (document.bookingEngine.seniorNumber.options[document.bookingEngine.seniorNumber.options.selectedIndex].value)*1;
infant= (document.bookingEngine.infantNumber.options[document.bookingEngine.infantNumber.options.selectedIndex].value)*1;
if ( ( adult + senior )==0 )
{
if ( infant > 0)
{
band=false;
return false;
}
}
if ((adult+child+senior)>0)
{
band=true;	
}
if(infant > ( adult + senior))
{
child= child + (infant-(adult+senior));
infant = adult +senior;
}
if (((adult+senior)==0)&&(infant>0))
{
band=false;
}
if ((adult+child+senior) > 8)
{
document.paxRedirect.submit();
//passengerRedirects.submit();
//alert(adult+child+senior);
return false;
}
else
{
if (document.bookingEngine.__flightType__.value=="ms")
document.bookingEngine.action.value="/g4mx/public/templates/Main/0,1655,,00.html?templateLink=|g4mx|public|booking|StopDisplay.jsp";
else
document.bookingEngine.action.value="/g4mx/public/templates/Main/0,1655,,00.html?templateLink=|g4mx|public|booking|FareDisplay.jsp";
}
return band;
}
else
{
return true;
}
}
function changeFormat(someDate)
{
var day,month,year
var temp=someDate.split("/");
day=temp[0];
month=temp[1];
year=temp[2];
return (month+"/"+day+"/"+year);
}	
function validaMeses(day1,month1,year1,day2,month2,year2)
{
var aux;
var dday1,dday2,mmonth1,mmonth2,yyear1,yyear2;
dday1=parseInt(day1,10);
dday2=parseInt(day2,10);
mmonth1=parseInt(month1,10);
mmonth2=parseInt(month2,10);
yyear1=parseInt(year1,10);
yyear2=parseInt(year2,10);

if (yyear2>yyear1)
{
mmonth2=mmonth2+12;
}

if (mmonth1==mmonth2)
{
if (dday1<=dday2)	
return true;
}else{
aux=mmonth2-mmonth1;
if ((mmonth2-mmonth1<11)&&(mmonth2-mmonth1>0))
{
return true;
}
else
if ((mmonth2-mmonth1==11)&&(dday2<dday1))
return true;
}
return false;
}

function submitForm()
{
	var retDate,depDate,aa;
	var isCubaFlight = false;
	var isOK = false;

	depDate=new Date(changeFormat(document.bookingEngine.departDate.value));
	retDate=new Date(changeFormat(document.bookingEngine.returnDate.value));	
	if(document.bookingEngine.departCity.value=="HAV" || document.bookingEngine.arriveCity.value=="HAV")
		isCubaFlight = true;
	if ((isCubaFlight)&&(!confirm(alertCubaFlight)))
	{}
	else
	{
		if( document.bookingEngine.origen.value=="" || document.bookingEngine.destino.value=="" ||
		document.bookingEngine.origen.value==" " || document.bookingEngine.destino.value==" ")
			alert(alert2);
		else if( (document.bookingEngine.__flightType__.value==null ||
			document.bookingEngine.__flightType__.value=="rt") &&
			flightType == "rt" )
		{
			if(validateDate(2))
			{
				if(depDate > retDate)
					alert(alert6);
				else
				{
					if(countPassenger())
						isOK = true;
					else
					{
						if (childError)
							alert(alertChild);
					}
				}
			}
		}
		else
		{
			if(validateDate(1))
			{
				if (countPassenger())
					isOK = true;
			}	
		}
	}
	if (isOK && document.bookingEngine.countryTicket!=null && document.bookingEngine.countryTicket.value=="0")
	{
		isOK=false;
		alert(alert3);
	}
	if (isOK)
	{
		//aa=new Date();
		//document.bookingEngine.actualTime.value=aa.getTime();
	}
	return isOK; 
}


function checkStatus()
{
if ( document.bookingEngine.__flightType__[ROUND_TRIP].checked )
flightTypeChanged(ROUND_TRIP);	
else
flightTypeChanged(ONE_WAY_TRIP);	
}


function validateDate(type)
{
 var depDate,retDate;
 var depDateArray,retDateArray;
 var currDate;
 var tYear;
 currDate=new Date();

 if (type==1)
 {
  if (checkdate(document.bookingEngine.departDate))
  {
   depDate=document.bookingEngine.departDate.value;	
   depDateArray=depDate.split("/");
   document.bookingEngine.departDay.value=depDateArray[0];
   document.bookingEngine.departMonth.value=smallMonths[parseInt(depDateArray[1],10)-1];
   //	
   // Netscape empiza las fechas en 1900 por lo tanto 2001 == 101 + 1900
   // hay que agregar
   //	
   tYear = currDate.getYear()
   if (bw.ns4 || bw.ns6)
   {
    tYear += 1900;
   }
   if (validaMeses(currDate.getDate(),currDate.getMonth()+1,tYear,depDateArray[0],depDateArray[1],depDateArray[2]))
   {
    return true;
   }
   else
   {
    alert(alert8);
   }
  }
 }
 else if ((checkdate(document.bookingEngine.departDate))&&(checkdate(document.bookingEngine.returnDate)))
 {
  depDate=document.bookingEngine.departDate.value;	
  retDate=document.bookingEngine.returnDate.value;
  depDateArray=depDate.split("/");
  retDateArray=retDate.split("/");
  document.bookingEngine.departDay.value=depDateArray[0];
  document.bookingEngine.departMonth.value=smallMonths[parseInt(depDateArray[1],10)-1];
  document.bookingEngine.returnDay.value=retDateArray[0];
  document.bookingEngine.returnMonth.value=smallMonths[parseInt(retDateArray[1],10)-1];
  //alert(currDate.getDate()+","+currDate.getMonth()+","+currDate.getYear());
  //alert((validaMeses(currDate.getDate(),currDate.getMonth()+1,currDate.getYear(),retDateArray[0],retDateArray[1],retDateArray[2])));
  //alert((validaMeses(currDate.getDate(),currDate.getMonth()+1,currDate.getYear(),depDateArray[0],depDateArray[1],depDateArray[2])));	
  tYear = currDate.getYear()
  if (bw.ns4 || bw.ns6)
  {
   tYear += 1900;
  }
  if ((validaMeses(currDate.getDate(),currDate.getMonth()+1,tYear,retDateArray[0],retDateArray[1],retDateArray[2]))&&
(validaMeses(currDate.getDate(),currDate.getMonth()+1,tYear,depDateArray[0],depDateArray[1],depDateArray[2])))
  {
   return true;
  }
  else
  {
   alert(alert8);
  }
 }
 return false;
}

function checkdate(objName) {
var datefield = objName;
if (chkdate(objName) == false) {
datefield.select();
alert(alert9);
datefield.focus();
return false;
}else {	return true; }
}

function chkdate(objName) 
{
// var strDatestyle = "US"; //United States date style 
var strDatestyle = "EU"; //European date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);

strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
}
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
}
}
if (strYear != null && strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth))
{
err = 3;
return false;
}
/*
if (isNaN(intMonth))
{
for (i = 0;i < 12;i++)
{
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase())
{
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12; 
}
}
if (isNaN(intMonth))
{
err = 3;
return false;
}
}
*/
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth > 12 || intMonth < 1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
return true;
}

function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}	else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}



