var aLeap = new Array
aLeap[0] = 2008
aLeap[1] = 2012
aLeap[2] = 2016
aLeap[3] = 2020
aLeap[4] = 2024
aLeap[5] = 2028
aLeap[6] = 2032
aLeap[7] = 2036
aLeap[8] = 2040
aLeap[9] = 2044
aLeap[10] = 2048
aLeap[11] = 2052
aLeap[12] = 2056
aLeap[13] = 2060
aLeap[14] = 2064
aLeap[15] = 2068
aLeap[16] = 2072
aLeap[17] = 2076
aLeap[18] = 2080
aLeap[19] = 2084


function mSubBooks(e) {
		
	if (window.Event){
		entKey = e.which;
	}else{
		entKey = event.keyCode;
	}
	if (entKey == 13) {
		var frm = document.forms[1];		
			
		if (frm.txtSearch.value != '') {

			if (frm.payType && frm.payType.value == -1) {
		
				alert('Seleccione el tipo de pago...')		
				frm.payType.focus()

			}else{

				if (frm.agency && 
					(frm.txtNameHid.value == '' && 	(frm.agency.value == 'OLET' || frm.agency.value == 'OVIS')) && 
					!confirm('Esta seguro de querer empezar una reserva sin seleccionar un Agente ?...')) {
		
					frm.agency.focus()
					return false
			
				}else{		
				
					frm.txtNameHid.value = 'OLET'
				
					//if (frm.txtSearch.value != '' && entKey == 13) {	
						frm.submit()
					//}
				}
			}			
		}	
	}
}

function setAllCat(t) {
	
	var fr = document.forms[1];
		
	if (fr.StarsBox) {

		if (t == 'H') {

			var st1 = document.images["imStar1"];
			var st2 = document.images["imStar2"];
			var st3 = document.images["imStar3"];
			var st4 = document.images["imStar4"];
			var st5 = document.images["imStar5"];
		
			st1.style.visibility = 'hidden'
			st2.style.visibility = 'hidden'
			st3.style.visibility = 'hidden'
			st4.style.visibility = 'hidden'
			st5.style.visibility = 'hidden'
		}		
		fr.StarsBox.selectedIndex = 0
	}
}


function chkRooms(x) {
	
	if (x == 'H') {
		
		var fr = document.forms[1];
		var rx = Number(fr.habs.value);		
		var p1 = Number(fr.pax1.value);			
		var p2 = Number(fr.pax2.value);
		var px = p1 + p2;
		var res = 1;
												
		if (rx == 1) {
			fr.rooms.value = px;					
		}else if (rx == 2) {
			if (px > 2) {
				if (px/2 > parseInt(px/2)) {	
					res = parseInt(px/2) + 1;
				}else{
					res = parseInt(px/2);
				}			
			}
			fr.rooms.value = res;				
		}else if (rx == 3) {
			if (px > 3) {
				if (px/3 > parseInt(px/3)) {	
					res = parseInt(px/3) + 1;
				}else{
					res = parseInt(px/3);
				}			
			}
			fr.rooms.value = res;				
		}else if (rx == 4 || rx == 5 || rx == 6) {
			if (px > 4) {
				if (px/4 > parseInt(px/4)) {	
					res = parseInt(px/4) + 1;
				}else{
					res = parseInt(px/4);
				}			
			}
			fr.rooms.value = res;				
		}else if (rx == 7) {
			if (px > 6) {
				if (px/6 > parseInt(px/6)) {
					res = parseInt(px/6) + 1;
				}else{
					res = parseInt(px/6);
				}			
			}
			fr.rooms.value = res;				
		}else if (rx == 8) {
			if (px > 9) {
				if (px/9 > parseInt(px/9)) {
					res = parseInt(px/9) + 1;
				}else{
					res = parseInt(px/9);
				}			
			}
			fr.rooms.value = res;
		}
	}
}

function chkHabs(x) {
		
	if (x == 'H') {				
			
		var fr = document.forms[1];
			
		//alert(fr.name)
			
		var rx = Number(fr.habs.value);
		var p1 = Number(fr.pax1.value);			
		var p2 = Number(fr.pax2.value);
		var px = p1  //+ p2;
					
		if (px == 0) {
			fr.rooms.value = 1;			
			fr.habs.value = 1;
		}else if (px > 0 && px < 5) {	
			fr.rooms.value = 1;			
			fr.habs.value = px;
		}else{
			if (px == 5) {
				fr.rooms.value = 2;
				fr.habs.value = 3;
			}else if (px == 6) {
				fr.rooms.value = 2;
				fr.habs.value = 3;
			}else if (px == 7 || px == 8) {
				fr.rooms.value = 2;
				fr.habs.value = 4;
			}else{	
				//if (rx < 5) {
					if (p1 > 4 && p2 == 0) {
						if (p1/2 > parseInt(p1/2)) {	
							fr.rooms.value = parseInt(p1/2) + 1;
						}else{
							fr.rooms.value = parseInt(p1/2);
						}	
						fr.habs.value = 2;								
					}
					if (p2 > 4 && p1 == 0) {
						if (p2/4 > parseInt(p2/4)) {
							fr.rooms.value = parseInt(p2/4) + 1;
						}else{
							fr.rooms.value = parseInt(p2/4);
						}	
						fr.habs.value = 4;			
					}	
				//}					
			}
		}
	}
}



function mDate(m,a,s) {

	//alert(m)

	frm = document.forms[1]
	
	//alert(s)
	
	if (s == 1) {
	
		oIM = frm.inAccMon
		oID = frm.inAccDay
		oIY = frm.inAccYear
	
		oOM = frm.outAccMon
		oOD = frm.outAccDay
		oOY = frm.outAccYear
		
	}else{	
	
		oIM = frm.inMonth
		oID = frm.inDay
		oIY = frm.inYear
	
		oOM = frm.outMonth
		oOD = frm.outDay
		oOY = frm.outYear
	}	
	
	//alert(oIM)
	//alert(oID)
	//alert(oIY)
	//alert(oOM)
	//alert(oOD)
	//alert(oOY)
	
	var b = typeof(oOM) == 'object'
		
	bLeap = false

	for (i=0; i<20; i++) {		
		if (oIY.value == aLeap[i]) {			
			bLeap = true
			i = 20
		}			
	}
	
	if (m.substr(0,1) == 'i') {
		
		if (b && m.substr(1,1) == 'y') {

			oOY.value = oIY.value
				
		}else{
			
			if (b && m.substr(1,1) == 'd') {
				
				if (oIM.value == oOM.value) {
							
					//alert(oIM.value)
					//alert(oOM.value)
				
					//alert(oID.value)
					//alert(oOD.value)
				
					//alert(oOD.selectedIndex)
					
					if (oID.value > oOD.value) {oOD.selectedIndex = oID.value-1}
				}
			
			}else{
				
				inM = oIM.value
			
				//if (b && a != 1) {oOM.value = inM}				
			
				if (b) {oOM.value = inM}
				if (b) {ouM = oOM.value}
					
				if (inM == 4 || inM == 6 || inM == 9 || inM == 11) {
					
					if (oID.length > 27 && oID.length < 30) {
						if (oID.length == 28) {
							NewOpt1 = new Option;
							NewOpt1.value = 29;	
							NewOpt1.text =	'29'
							oID.options[oID.length] = NewOpt1;
						}	
						NewOpt1 = new Option;
						NewOpt1.value = 30;
						NewOpt1.text =	'30'
						oID.options[oID.length] = NewOpt1;
					}else{
						if (oID.length > 30) {oID.options[oID.length-1] = null}	
					}
					
				}else if (inM == 2){
					
					if (oID.length == 31 && !bLeap) {
						oID.options[oID.length-3] = null
					}
					oID.options[oID.length-2] = null
					oID.options[oID.length-1] = null				
					
				}else{
					if (oID.length < 31) {
						
						if (oID.length > 27 && oID.length < 30) {
							
							if (oID.length == 28) {
						
								NewOpt1 = new Option;
								NewOpt1.value = 29;	
								NewOpt1.text =	'29'
								
								oID.options[oID.length] = NewOpt1;
							}
							
							NewOpt1 = new Option;
							NewOpt1.value = 30;
							NewOpt1.text =	'30'
						
							oID.options[oID.length] = NewOpt1;
											
						}
			
						NewOpt1 = new Option;
						NewOpt1.value = 31;							
						NewOpt1.text =	'31'
						
						oID.options[oID.length] = NewOpt1;
					}					
				}	
					
				if (b) {
					
					if (ouM == 4 || ouM == 6 || ouM == 9 || ouM == 11) {
					
						if (oOD.length > 27 && oOD.length < 30) {
							if (oOD.length == 28) {
								NewOpt1 = new Option;
								NewOpt1.value = 29;	
								NewOpt1.text =	'29'
								oOD.options[oOD.length] = NewOpt1;
							}	
							NewOpt1 = new Option;
							NewOpt1.value = 30;
							NewOpt1.text =	'30'
							oOD.options[oOD.length] = NewOpt1;
						}else{
							if (oOD.length > 30) {oOD.options[oOD.length-1] = null}	
						}
					
					}else if (ouM == 2){
					
						if (oOD.length == 31 && !bLeap) {
							oOD.options[oOD.length-3] = null
						}
						oOD.options[oOD.length-2] = null
						oOD.options[oOD.length-1] = null				
					
					}else{
						if (oOD.length < 31) {
						
							if (oOD.length > 27 && oOD.length < 30) {
							
								if (oOD.length == 28) {
						
									NewOpt1 = new Option;
									NewOpt1.value = 29;	
									NewOpt1.text =	'29'
								
									oOD.options[oOD.length] = NewOpt1;
								}
							
								NewOpt1 = new Option;
								NewOpt1.value = 30;
								NewOpt1.text =	'30'
						
								oOD.options[oOD.length] = NewOpt1;
											
							}
			
							NewOpt1 = new Option;
							NewOpt1.value = 31;							
							NewOpt1.text =	'31'
						
							oOD.options[oOD.length] = NewOpt1;
						}					
					}
				}
			}
		}
	}
		
	if (b && m.substr(0,1) == 'o') {
			
		ouM = oOM.value
				
		if (ouM == 4 || ouM == 6 || ouM == 9 || ouM == 11) {
				
			if (oOD.length > 27 && oOD.length < 30) {
				if (oOD.length == 28) {
					NewOpt1 = new Option;
					NewOpt1.value = 29;	
					NewOpt1.text =	'29'
					oOD.options[oOD.length] = NewOpt1;
				}	
				NewOpt1 = new Option;
				NewOpt1.value = 30;
				NewOpt1.text =	'30'
				oOD.options[oOD.length] = NewOpt1;
			}else{
				if (oOD.length > 30) {oOD.options[oOD.length-1] = null}	
			}
				
		}else if (ouM == 2){
				
			if (oOD.length == 31 && !bLeap) {
				oOD.options[oOD.length-3] = null
			}
			oOD.options[oOD.length-2] = null
			oOD.options[oOD.length-1] = null				
				
		}else{
			if (oOD.length < 31) {
				
				if (oOD.length > 27 && oOD.length < 30) {
						
					if (oOD.length == 28) {
					
						NewOpt1 = new Option;
						NewOpt1.value = 29;	
						NewOpt1.text =	'29'
							
						oOD.options[oOD.length] = NewOpt1;
					}
						
					NewOpt1 = new Option;
					NewOpt1.value = 30;
					NewOpt1.text =	'30'
					
					oOD.options[oOD.length] = NewOpt1;
										
				}
			
				NewOpt1 = new Option;
				NewOpt1.value = 31;							
				NewOpt1.text =	'31'
					
				oOD.options[oOD.length] = NewOpt1;
			}					
		}
	}
}

function CheckBooks(pak,ty) {
	
	var frm = document.forms[1];	
	var disp = frm.name == 'frmDispatch';
	
	//alert(frm.name);
	//alert(ty);	
	
	var dErr = false;	
	if (typeof(frm.outDay) == 'object') {	
		dIn = Number(frm.inDay.value);
		mIn = Number(frm.inMonth.value);
		yIn = Number(frm.inYear.value);		
		dOut = Number(frm.outDay.value);
		mOut = Number(frm.outMonth.value);
		yOut = Number(frm.outYear.value);		
		if (yOut == yIn) {		
			if (mOut == mIn) {
				if (ty == 'M') {				
					if (dOut < dIn) {dErr = true;}
				}else{
					if (ty == 'A') {
						if (dOut < dIn) {dErr = true;}
					}else{
						if (dOut <= dIn) {dErr = true;}
					}	
				}	
			}else{	
				if (mOut < mIn) {dErr = true;}				
			}
		}else{
			if (yOut < yIn) {dErr = true;}		
		}
	}	
	var pErr = false;
	pax1 = frm.pax1.value;
	pax2 = frm.pax2.value;
	if (pax1 == 0 && pax2 == 0) {pErr = true;}	
	
	if (!disp && frm.txtResName.value == '') {
		
		frm.txtResName.value = 'Nueva reserva...'		
	}
	
	//alert(ty)
	
	if (dErr) {
		if (ty == 'M') {
			alert("La fecha de partida no puede ser anterior a la de entrada...")
		}else{
			alert("La fecha de partida debe ser posterior a la de entrada...")
		}	
		frm.outMonth.focus();
		if (disp) {return false;}
	}else{
		if (pErr) {
			alert("El número de pasajeros no es correcto...")
			frm.pax1.value = 2;
			frm.pax1.focus();
			if (disp) {return false;}
		}else{	
			//alert(ty)
			if (ty == 'A' && frm.flight.value == '') {
				alert("Ingrese el número de vuelo...")
				frm.flight.focus();
				if (disp) {return false;}
			}else{
				if (ty == 'A' && frm.selHours.value == '00' && frm.selMins.value == '00') {
					alert("Ingrese la hora del vuelo...")
					frm.selHours.focus();
					if (disp) {
						return false;
					}
				}else{
					
					bSub = false
					
					if (frm.selPaid && frm.selPaid.value > 1) {
					
						if (frm.cDisNum.value == '') {

							var sDocNum
							if (frm.selPaid.value == 2) {						
							
								sDocNum = 'tarjeta'
							}else{	
								sDocNum = 'cheque'
							}	
							alert("Ingrese el numero de " + sDocNum + "...")
							frm.cDisNum.focus()
						
						}else{
						
							if (frm.cDisNam.value == '') {
					
								var sDocNam
								if (frm.selPaid.value == 2) {						
							
									sDocNam = 'la tarjeta'
								}else{	
									sDocNam = 'el cheque'
								}
								alert("Ingrese el nombre en " + sDocNam + "...")
								frm.cDisNam.focus()
								
							}else{	
							
								if (frm.cMon.value == '') {

									alert("Ingrese el mes de vencimiento...")
									frm.cMon.focus()									
								}else{
									
									if (frm.cMon.value == '') {

										alert("Ingrese el mes de vencimiento...")
										frm.cMon.focus()
									}else{					
										if (frm.cYea.value == '') {

											alert("Ingrese el año de vencimiento...")
											frm.cYea.focus()	
										}else{
										
											bSub = true
										}
									}
								}
							}
						}			
					}else{					
						bSub = true					
					}			
					if (bSub) {
						if (disp) {
							return true;					
						}else{
							if (pak == 'yes') {
								frm.txtSelect.value = 'pak';
							}else{
								frm.txtSelect.value = 'go';	
							}	
							frm.submit();
						}
					}else{
						if (disp) {
							return false;
						}						
					}	
				}
			}
		}	
	}		
}

		
function calcBothDates(theForm, ty, tm, td)	{
	var today	= new Date((ty%1900+1900), tm-1, td, 0,0,0);
	var inDate	= new Date((ty%1900+1900), parseInt(document.myform.CIMonth[document.myform.CIMonth.selectedIndex].value)-1, document.myform.CIDay[document.myform.CIDay.selectedIndex].value, 0,0,0);
	var outDate	= new Date((ty%1900+1900), parseInt(document.myform.CIMonth[document.myform.CIMonth.selectedIndex].value)-1, parseInt(document.myform.CIDay[document.myform.CIDay.selectedIndex].value)+2, 0,0,0);

	if (inDate.getTime()  < today.getTime()) { // Crossed the newyear boundary
		inDate.setYear((today.getYear()%1900+1900)+1);
		outDate.setYear((today.getYear()%1900+1900)+1);
	}

	theForm.CIYear.value	= (inDate.getYear()%1900) + 1900;
	doSelectOptions(inDate.getMonth()+1, 'CIMonth');
	doSelectOptions(inDate.getDate(), 'CIDay');

	theForm.COYear.value	= (outDate.getYear()%1900) + 1900;
	doSelectOptions(outDate.getMonth()+1, 'COMonth');
	doSelectOptions(outDate.getDate(), 'CODay');
}


function calcCODate(theForm, ty, tm, td) {
	var today	= new Date((ty%1900+1900), tm-1, td, 0,0,0);
	var outDate	= new Date((ty%1900+1900), parseInt(document.myform.COMonth[document.myform.COMonth.selectedIndex].value)-1, parseInt(document.myform.CODay[document.myform.CODay.selectedIndex].value), 0,0,0);

	if ((parseInt(theForm.CIYear.value)%1900) >(today.getYear()%1900)) {
		outDate.setYear((today.getYear()%1900+1900)+1);	// Already a year ahead for in date
	} else if (outDate.getTime() < today.getTime()) {
		outDate.setYear((today.getYear()%1900+1900)+1);	// Crossed the newyear boundary
	}

	theForm.COYear.value	= (outDate.getYear()%1900) + 1900;
	doSelectOptions(outDate.getMonth()+1, 'COMonth');
	doSelectOptions(outDate.getDate(), 'CODay');
}
		
		
function CheckStars(p) {

	var frm = document.forms['frmBooks']
	var frmStars = frm.StarsBox;
	
	if (p != 0) {frm.txtSearch.value = ''}
		
	if (typeof(frmStars) == 'object') {	
		
		var st1 = document.images["imStar1"];
		var st2 = document.images["imStar2"];
		var st3 = document.images["imStar3"];
		var st4 = document.images["imStar4"];
		var st5 = document.images["imStar5"];
				
		var sel = frmStars.value;
		
		if (sel == 0 || sel == 6) {		
			st5.style.visibility = 'hidden';
			st4.style.visibility = 'hidden';
			st3.style.visibility = 'hidden';
			st2.style.visibility = 'hidden';
			st1.style.visibility = 'hidden';			
		}else if (sel == 1) {			
			st5.style.visibility = 'visible';
			st4.style.visibility = 'hidden';
			st3.style.visibility = 'hidden';
			st2.style.visibility = 'hidden';
			st1.style.visibility = 'hidden';
		}else if (sel == 2) {			
			st5.style.visibility = 'visible';
			st4.style.visibility = 'visible';
			st3.style.visibility = 'hidden';
			st2.style.visibility = 'hidden';
			st1.style.visibility = 'hidden';			
		}else if (sel == 3) {			
			st5.style.visibility = 'visible';
			st4.style.visibility = 'visible';
			st3.style.visibility = 'visible';
			st2.style.visibility = 'hidden';
			st1.style.visibility = 'hidden';			
		}else if (sel == 4) {			
			st5.style.visibility = 'visible';
			st4.style.visibility = 'visible';
			st3.style.visibility = 'visible';
			st2.style.visibility = 'visible';
			st1.style.visibility = 'hidden';
		}else if (sel == 5) {			
			st5.style.visibility = 'visible';
			st4.style.visibility = 'visible';
			st3.style.visibility = 'visible';
			st2.style.visibility = 'visible';
			st1.style.visibility = 'visible';		
		}
	}	
}		
		
function setCheckIsland(ndx) {
	var frm = document.forms['frmBooks'];
	frm.selIsland[ndx].checked = true;		
}

function subNew() {
	var frm = document.forms['frmBooks'];	
	frm.txtSelect.value = 'new';
	frm.submit();
}

function availDate(o,m) {
	var b = typeof(document.all['outMonth_' + o]) == 'object'
	if (b) {
		if (m == 'y') {
			document.all['outYear_' + o].value = document.all['inYear_' + o].value;
		}else{
			document.all['outMonth_' + o].value = document.all['inMonth_' + o].value;
		}
	}	
}



