// ###########################  OBJETO DE CONFIGURACION #################################

function submitForm(){
	if (validateFields()){
		waitFor();
		$('#form1').attr("action", configForm.myLocation);
		document.form1.submit();
		$('#titleCerrar').addClass("notShow");
	}
}

function initialize(){
	$('#hidepage table tr td object').attr("data", configForm.spinerURL);
	$('#hidepage table tr td object param').attr("value", configForm.spinerURL);
	
	
	
	setOfferedProducts();
	setChecked(document.getElementsByName("viaje"), configForm.defautlProduct)
	configureFields(getCheckedValue(document.getElementsByName("viaje")));
}

function setOfferedProducts(){
	if (configForm.productsOffered.indexOf("|H|")  >= 0) $('#viaje_0').parent().addClass("show-inline"); else $('#viaje_0').parent().addClass("notShow");
	if (configForm.productsOffered.indexOf("|F|")  >= 0) $('#viaje_1').parent().addClass("show-inline"); else $('#viaje_1').parent().addClass("notShow");
	if (configForm.productsOffered.indexOf("|HF|") >= 0) $('#viaje_2').parent().addClass("show-inline"); else $('#viaje_2').parent().addClass("notShow");
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	for(var i = 0; i < radioObj.length; i++)
		if(radioObj[i].checked)
			return radioObj[i].value;
	return "";
}

function setChecked(radioObj, Value) {
	if(!radioObj)
		return;
	for(var i = 0; i < radioObj.length; i++)
		if(radioObj[i].value == Value) {
			radioObj[i].checked = 'checked';
			return;
		}
}
//-----------------------------------------------------------------------------------------


//	#################### 	FUNCION PARA LIMPIAR LOS CAMPOS		################################

function clearForm(){
	$('#origen').val('');
	$('#origenHidden').val('');
	$('#destino').val('');
	$('#destinoHidden').val('');
	$('#fecha-Inicio').val('');
	$('#fecha-Fin').val('');
	$('#fechaInicio').val('');
	$('#fechaFin').val('');
	$('#iHora').get(0).selectedIndex = 0;
	$('#fHora').get(0).selectedIndex = 0;
	$('#noAdultos_0').get(0).selectedIndex = 0;
	$('#noChildren_0').get(0).selectedIndex = 0; modifyChildren('noChildren_0','0');
	$('#habitaciones').get(0).selectedIndex = 0; modifyHab('1');
	$('#origen').removeClass('missingField');
	$('#destino').removeClass('missingField');
	$('#fecha-Inicio').removeClass('missingField');
	$('#fecha-Fin').removeClass('missingField');
	$('#errors div').remove();
}


//------------------------------------------------------------------------------------------

// #################  FUNCIONES MOSTRAR y/o OCULTAR CAMPOS DEPENDIENDO DE LA BUSQUEDA ###########


function configureFields(searchType){	
	configForm.selectedProduct = searchType;
	switch (searchType){
		case 'HF'	:	$('#flightTypeTr').addClass("notShow");
						$('#origentr').removeClass("notShow");
						$('#horaRegreso').removeClass("notShow");
						$('#horaSalida').removeClass("notShow");
						$('#numeroDeHabitaciones').removeClass("notShow");
						$('#hotelNametr').removeClass("notShow");
						$('#promotionCodetr').removeClass("notShow");
						$('#directFlighttr').removeClass("notShow");
						$('#searchPanel').removeClass("notShow");
						$('#childrenPanel').removeClass("notShow");
						configForm.autocompleteOrigenMode = 'airport';
						configForm.autocompleteDestinoMode = 'city';
						configForm.iAge = configForm.PackageInfantMaxAge;
						configForm.cAge = configForm.PackageChildMaxAge;
						setHF();
						break;
		case 'F'	:	$('#flightTypeTr').removeClass("notShow");
						$('#origentr').removeClass("notShow");
						$('#horaRegreso').removeClass("notShow");
						$('#horaSalida').removeClass("notShow");
						$('#numeroDeHabitaciones').addClass("notShow");
						$('#hotelNametr').addClass("notShow");
						$('#promotionCodetr').addClass("notShow");
						$('#directFlighttr').removeClass("notShow");
						$('#searchPanel').removeClass("notShow");
						$('#childrenPanel').addClass("notShow");
						configForm.autocompleteOrigenMode = 'airport';
						configForm.autocompleteDestinoMode = 'airport';
						configForm.iAge = configForm.AirInfantMaxAge;
						configForm.cAge = configForm.AirChildMaxAge;
						setF();
						break ;
		case 'H'	:	$('#origentr').addClass("notShow");
						$('#horaRegreso').addClass("notShow");
						$('#horaSalida').addClass("notShow");
						$('#flightTypeTr').addClass("notShow");
						$('#numeroDeHabitaciones').removeClass("notShow");
						$('#hotelNametr').removeClass("notShow");
						$('#promotionCodetr').removeClass("notShow");
						$('#directFlighttr').addClass("notShow");
						$('#searchPanel').removeClass("notShow");
						$('#childrenPanel').removeClass("notShow");
						configForm.autocompleteOrigenMode = 'city';
						configForm.autocompleteDestinoMode = 'city';
						configForm.iAge = configForm.HotelInfantMaxAge;
						configForm.cAge = configForm.HotelChildMaxAge;
						setHF();
	}
	clearForm();
}

function flightFields (type){
	switch (type){
		case 'return' 	:	$('#camposRegreso').removeClass("notShow");break;
		case 'oneway'	:	$('#camposRegreso').addClass("notShow");
	}
}

function advancedSearch(checkbox){
	if (!checkbox)
		return;
	if (checkbox.checked)
		$('#advancedSearch').removeClass("notShow");
	else
		$('#advancedSearch').addClass("notShow");
}

//--------------------------------------------------------------------------------------------------

// #############   FUNCIONES PARA VALIDACION DEL FORMULARIOS   #####################################

function validateFields(){
	var missingFields = '';
	
	if (configForm.selectedProduct == 'H'){
		if ($('#destino').get(0).value.length < 3 ){
			$('#destino').addClass('missingField');
			missingFields += '<li>Destino</li>';
		}else 
			$('#destino').removeClass('missingField');
	}else{
		if ($('#origen').get(0).value.length < 3 ){
			$('#origen').addClass('missingField');
			missingFields += '<li>Origen</li>';
		}else 
			$('#origen').removeClass('missingField');
		if ($('#destino').get(0).value.length < 3 ){
			$('#destino').addClass('missingField');
			missingFields += '<li>Destino</li>';
		}else 
			$('#destino').removeClass('missingField');
	}
	
	if ($('#fecha-Inicio').get(0).value.length < 8 ){
		$('#fecha-Inicio').addClass('missingField');
		missingFields += '<li>Salida</li>';
	}else $('#fecha-Inicio').removeClass('missingField');
	if ($('#fecha-Fin').get(0).value.length < 8 ){
		$('#fecha-Fin').addClass('missingField');
		missingFields += '<li>Regreso</li>';
	}else $('#fecha-Fin').removeClass('missingField');
					
	if (missingFields.length > 0){
		$('#errors div').remove();
		$('#errors').append('<div class="errors">' + configForm.missingFieldMessage +'<ul>'+ missingFields + '</ul></div>');
		return false;
	}
	return true;
	
}

//-----------------------------------------------------------------------------------------------------------

//   ###########################	FUNCION PARA MOSTRAR EL SPINNER		############3########################

function waitFor() {
	if (document.getElementById) {  // DOM3 = IE5, NS6
		document.getElementById('hidepage').style.display = 'block';
	}else {
		if (document.layers) {  // Netscape 4
			document.hidepage.display = 'block';
		}else {  // IE 4
			document.all.hidepage.style.display = 'block';
		 }
	}
}
//-----------------------------------------------------------------------------------------------------------
			

// #####################	FUNCIONES PARA HABITACIONES Y NIŅOS		#############################################
			
function modifyHab(number){
var id = document.getElementById("hab").value;
	id--;
	number--;
	while(id > number){
		
		removeFormField("#guests_row" + id);
		modifyChildren('noChildren_'+id,0);
		removeFormField("#childrenAge_" + id);
		
		id--;
		}
	while(id < number){
		id++;
		addFormField("#guest",id,"<tr id='guests_row" + id + "'><td><label>Adultos <select name='noAdultos_"+id+"' id='noAdultos_" + id + "'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select></label></td><td><label>Ni&ntilde;os <select name='noChildren_"+id+"' id='noChildren_"+id+"' onChange='modifyChildren(this.id, this.value)'><option value='0' selected='selected'>0</option><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option></select></label></td></tr>");		
		addFormField("#guest",id,"<input type='hidden' name='childrenAge_"+id+"' id='childrenAge_"+id+"' value='0' />");
		}
	id++;
	document.getElementById("hab").value = id;
}
/*
id - indice de la fila
index - valor acual del numero de ninios
objValue - numero de ninios deseados
*/
function modifyChildren(objId,objValue){
	var id=objId.substring(objId.indexOf('_')+1);
	var index=document.getElementById("childrenAge_"+id).value;
	var parent=($("#childrenPanel").children(":eq(0)").children(":eq("+id+")").children(":eq(1)"));

	if(objValue==0)
		$("#childrenPanel").children(":eq(0)").children(":eq("+id+")").removeClass("show").addClass("notShow");
	else 
		$("#childrenPanel").children(":eq(0)").children(":eq("+id+")").removeClass("notShow").addClass("show");
	parent.children().remove();
	index=0;
	while(index < objValue){
		index++;
		parent.append("<span>Ni&ntilde;o "+index+" <select name='childAge_"+id+"_"+index+"' id='childAge_"+id+"_"+index+"'></select></span>");
		for (var i= parseInt(configForm.iAge) +1 ; i <= configForm.cAge; i++ )
			$("#childAge_" + id + "_" + index).append("<option value='" + i +"'>"+ i +"</option>");
	}
	$("#childrenAge_"+id).val(index);
}

function addFormField(parent,id, obj) {
	$(parent).append(obj);
}

function removeFormField(id) {
	$(id).remove();
}

//------------------------------------------------------------------------------------------------		

// ###################################### GOOGLE ANALYTICS #########################################

function analytics(){
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
	try {
		var pageTracker = _gat._getTracker(configForm.GoogleAnalyticsAccount);
		pageTracker._trackPageview();
	} catch(err) {}
}
//--------------------------------------------------------------------------------------------------


// ###################################### CAMBIO DE LOCALIDADES #########################################

var cityLocations = [ 
						{ valor: 'CITY_ACA_MX', 	texto: 'Acapulco'},
						{ valor: 'CITY_AGU_MX', 	texto: 'Aguascalientes'},
						{ valor: 'CITY_CUN_MX', 	texto: 'Cancun'},
						{ valor: 'CITY_CTM_MX', 	texto: 'Chetumal'},
						{ valor: 'CITY_CUU_MX', 	texto: 'Chihuahua'},
						{ valor: 'CITY_CME_MX', 	texto: 'Ciudad del Carmen'},
						{ valor: 'CITY_CJS_MX', 	texto: 'Ciudad Juarez'},
						{ valor: 'StaMxVeCoatz398', texto: 'Coatzacoalcos'},
						{ valor: 'CITY_CLQ_MX', 	texto: 'Colima'},
						{ valor: 'CITY_CZM_MX', 	texto: 'Cozumel'},
						{ valor: 'CITY_CUL_MX', 	texto: 'Culiacan'},
						{ valor: 'CITY_DGO_MX', 	texto: 'Durango'},
						{ valor: 'CITY_GDL_MX', 	texto: 'Guadalajara'},
						{ valor: 'CITY_HMO_MX', 	texto: 'Hermosillo'},
						{ valor: 'CITY_LAP_MX', 	texto: 'La Paz'},
						{ valor: 'REG_MX_LosCabo', 	texto: 'Cabo'},
						{ valor: 'CITY_MZT_MX', 	texto: 'Mazatlan'},
						{ valor: 'CITY_MXL_MX', 	texto: 'Mexicali'},
						{ valor: 'CITY_MEX_MX', 	texto: 'Ciudad de Mexico'},
						{ valor: 'CITY_LOV_MX', 	texto: 'Monclova'},
						{ valor: 'CITY_MTY_MX', 	texto: 'Monterrey'},
						{ valor: 'CITY_MLM_MX', 	texto: 'Morelia'},
						{ valor: 'CITY_NLD_MX', 	texto: 'Nuevo Laredo'},
						{ valor: 'CITY_OAX_MX', 	texto: 'Oaxaca'},
						{ valor: 'CITY_PAZ_MX', 	texto: 'Poza Rica'},
						{ valor: 'CITY_PBC_MX', 	texto: 'Puebla'},
						{ valor: 'CITY_PVR_MX', 	texto: 'Puerto Vallarta'},
						{ valor: 'CITY_QRO_MX', 	texto: 'Queretaro'},
						{ valor: 'CITY_REX_MX', 	texto: 'Reynosa'},
						{ valor: 'CITY_SLW_MX', 	texto: 'Saltillo'},
						{ valor: 'CITY_SLP_MX', 	texto: 'San Luis Potosi'},
						{ valor: 'CITY_TAM_MX', 	texto: 'Tampico'},
						{ valor: 'CITY_TIJ_MX', 	texto: 'Tijuana'},
						{ valor: 'CITY_TLC_MX', 	texto: 'Toluca'},
						{ valor: 'CITY_TRC_MX', 	texto: 'Torreon'},
						{ valor: 'CITY_TGZ_MX', 	texto: 'Tuxtla'},
						{ valor: 'CITY_VER_MX', 	texto: 'Veracruz'},
						{ valor: 'CITY_VSA_MX', 	texto: 'Villahermosa'},
						{ valor: 'CITY_BHZ_MG_BR', 	texto: 'Belo Horizonte'},
						{ valor: 'CITY_RIO_RJ_BR', 	texto: 'Rio de Janeiro'},
						{ valor: 'CITY_SAO_SP_BR', 	texto: 'Sao Paulo'},
						{ valor: 'CITY_BUE_BA_AR', 	texto: 'Buenos Aires'}
					];

var airLocations = [ 
						{ valor: 'AIR_ACA_MX', 		texto: 'Acapulco'},
						{ valor: 'AIR_AGU_MX', 		texto: 'Aguascalientes'},
						{ valor: 'AIR_CUN_MX', 		texto: 'Cancun'},
						{ valor: 'AIR_CTM_MX', 		texto: 'Chetumal'},
						{ valor: 'AIR_CUU_MX', 		texto: 'Chihuahua'},
						{ valor: 'AIR_CME_MX', 		texto: 'Ciudad del Carmen'},
						{ valor: 'AIR_CJS_MX', 		texto: 'Ciudad Juarez'},
						{ valor: 'AirMxMinatit419', texto: 'Coatzacoalcos'},
						{ valor: 'AirMxManzani054', texto: 'Colima'},
						{ valor: 'AIR_CZM_MX', 		texto: 'Cozumel'},
						{ valor: 'AIR_CUL_MX', 		texto: 'Culiacan'},
						{ valor: 'StaEsPaDuran429', texto: 'Durango'},
						{ valor: 'AIR_GDL_MX', 		texto: 'Guadalajara'},
						{ valor: 'AIR_HMO_MX', 		texto: 'Hermosillo'},
						{ valor: 'AIR_LAP_MX', 		texto: 'La Paz'},
						{ valor: 'AIR_SJD_MX', 		texto: 'Cabo'},
						{ valor: 'AIR_MZT_MX', 		texto: 'Mazatlan'},
						{ valor: 'AIR_MXL_MX', 		texto: 'Mexicali'},
						{ valor: 'AirMxBenitoJ875', texto: 'Ciudad de Mexico'},
						{ valor: 'AIR_LOV_MX', 		texto: 'Monclova'},
						{ valor: 'AirMx545', 		texto: 'Monterrey'},
						{ valor: 'AIR_MLM_MX', 		texto: 'Morelia'},
						{ valor: 'AIR_NLD_MX', 		texto: 'Nuevo Laredo'},
						{ valor: 'AIR_OAX_MX', 		texto: 'Oaxaca'},
						{ valor: 'AIR_PAZ_MX', 		texto: 'Poza Rica'},
						{ valor: 'AIR_TCN_MX', 		texto: 'Puebla'},
						{ valor: 'AIR_PVR_MX', 		texto: 'Puerto Vallarta'},
						{ valor: 'AIR_QRO_MX', 		texto: 'Queretaro'},
						{ valor: 'AIR_REX_MX', 		texto: 'Reynosa'},
						{ valor: 'AIR_SLW_MX', 		texto: 'Saltillo'},
						{ valor: 'AIR_SLP_MX', 		texto: 'San Luis Potosi'},
						{ valor: 'AIR_TAM_MX', 		texto: 'Tampico'},
						{ valor: 'AIR_TIJ_MX', 		texto: 'Tijuana'},
						{ valor: 'AIR_TLC_MX', 		texto: 'Toluca'},
						{ valor: 'AIR_TRC_MX', 		texto: 'Torreon'},
						{ valor: 'AirMxLlanoSa895', texto: 'Tuxtla'},
						{ valor: 'AirMxVeracru521', texto: 'Veracruz'},
						{ valor: 'AIR_VSA_MX', 		texto: 'Villahermosa'},
						{ valor: 'AIR_CNF_MG_BR', 	texto: 'Belo Horizonte'},
						{ valor: 'AIR_SDU_RJ_BR', 	texto: 'Rio de Janeiro'},
						{ valor: 'AIR_GRU_SP_BR', 	texto: 'Sao Paulo'},
						{ valor: 'AirArBa722', 		texto: 'Buenos Aires'}
					];
					
function setHF(){
	var sele = document.getElementById('destinoHidden');
	while (sele.options[1])
		sele.options[1] = null;
	for (i=0; cityLocations[i]; i++){
		opt = new Option(cityLocations[i].texto,cityLocations[i].valor);
		sele.options[sele.options.length] = opt;
	}
}

function setF(){
	var sele = document.getElementById('destinoHidden');
	while (sele.options[1])
		sele.options[1] = null;
	for (i=0; airLocations[i]; i++){
		opt = new Option(airLocations[i].texto,airLocations[i].valor);
		sele.options[sele.options.length] = opt;
	}
}

//--------------------------------------------------------------------------------------------------