var strSearchPrincipal;
var MostrandoDatos = false;
var ElemMouseover = '';
var busc_spain = false;

function cargaIdiomas() {
	document.getElementById('tag_find').innerHTML = lng_find;
	document.getElementById('tag_titulo_recogida').innerHTML = lng_titulo_recogida;
	document.getElementById('tag_zona_recogida').innerHTML = lng_zona_recogida;
	document.getElementById('tag_destino_recogida').innerHTML = lng_destino_recogida;
	document.getElementById('tag_desdehora').innerHTML = lng_desdehora;
	document.getElementById('tag_desdefecha').innerHTML = lng_desdefecha;
	document.getElementById('tag_titulo_dev').innerHTML = lng_titulo_dev;
	document.getElementById('tag_zona_dev').innerHTML = lng_zona_dev;
	document.getElementById('tag_destino_dev').innerHTML = lng_destino_dev;
	document.getElementById('tag_hastahora').innerHTML = lng_hastahora;
	document.getElementById('tag_hastafecha').innerHTML = lng_hastafecha;
	document.getElementById('tag_categoria').innerHTML = lng_categoria;
	document.getElementById('tag_buscar').innerHTML = '<input id="Btn_Search" type="button" value="'+lng_buscar+'" onclick="validaSearch();">'
	var fecha = new Date();
	fecha.setDate(fecha.getDate()+1);
	document.getElementById('cbo_ddesde').value = fecha.print("%d-%m-%Y");
	fecha.setDate(fecha.getDate()+7);
	document.getElementById('cbo_dhasta').value = fecha.print("%d-%m-%Y");
//	document.getElementById('').innerText = ;
}

// reusable generic function, modified to include real asyncronous behaviour by MASH
function loadXMLDoc(url, elemento, modo, txtSelect) {
	var req = getXmlHttpRequestObject();
	var result = "";
	req.onreadystatechange = function() {
		
		if (req.readyState == 4) {
			if (req.status == 200) {
				switch(modo) {
					case 'list' :
						clearList(elemento);
						buildList(elemento, req, txtSelect);
						break;
					case 'SimpleData' :
						var result = "";
						var bgColor = "";
						var items = req.responseXML.getElementsByTagName("item");
						result = '<table width="100%">';
						for (var i = 0; i < items.length; i++) {
								if (Mod(i,2) > 0) {
									bgColor = '#fafafa';
								} else {
									bgColor = '#ffffff';
								};
							result = result + '<tr style="background-color:'+bgColor+'"><td valign="top"><strong>' + getElementTextNSAttrib("","label",items[i],0) +': </strong></td><td valign="top">'+ getElementTextNSAttrib("","data",items[i],0)+'</td></tr>';
						}
						result = result + '</table>';
						//return overlib(elemento+' '+ElemMouseover);
						// Si el Elemento tiene onmouseover hacemos el tooltip, si no nada
						if ((ElemMouseover == elemento) && !(MostrandoDatos)) {
							return overlib(result);
						}
						break;
					case 'search' :
						buidSearchResults(req);
						break;
				}

			} else {
				//alert("There was a problem retrieving the XML data:\n" + req.statusText);
			}
		}
	}
	// branch for native XMLHttpRequest object
	if (window.ActiveXObject) {
		req.open("GET", url, true);
		req.send();
	// branch for IE/Windows ActiveX version
	} else {
		req.open("GET", url, true);
		req.send(null);
	}
}


function cargaFechahasta() {
	var ddesde = document.getElementById('cbo_ddesde').value;
	//Convertimos las Fechas
	var ddesde_array=ddesde.split("-");
	fecha = new Date(ddesde_array[2],(ddesde_array[1]-1),ddesde_array[0]);
	fecha.setDate(fecha.getDate()+7);
	document.getElementById('cbo_dhasta').value = fecha.print("%d-%m-%Y");
}

function cargaHorahasta() {
	document.getElementById('cbo_hhasta').selectedIndex = document.getElementById('cbo_hdesde').selectedIndex;
}

// Carga el parametro seleccionado en el combo de zonas y
// lo pasa a loadXMLDoc para que carge las listas
function cargaLista(evt, modo, text) {
	//Diferentes navegadores...
	evt = (evt) ? evt : ((window.event) ? window.event : null);
	if (evt) {
		//Diferentes navegadores 2, el regreso
		var elem = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
		if (elem) {
			try {
				if (elem.selectedIndex > 0) {
					//var strSublic = '';
					//alert(sublic);
					if (sublic) {
						strSublic = '&sublic='+sublic;
					}
					switch (modo) {
						case 'ofis_recogida' :
							var seleccionado = elem.options[elem.selectedIndex].value;
							var strSearch = libURL + '?lang='+lng+'&modo=2&zona='+seleccionado+strSublic;
							var numsel = elem.selectedIndex;
							loadXMLDoc(strSearch, 'cbo_ofis_recogida', 'list', text);
							document.getElementById('cbo_ofis_recogida').disabled = false;
							break;
						case 'zonas_dev' :
							//alert(strSearch);
							//Si seleccionamos espaņa podemos hacer One Way solo hacia zonas diferentes a la original
							var seleccionado = document.getElementById('cbo_zonas_recogida').value;
							busc_spain = false;
							if ((Number(seleccionado) >= 1) && (Number(seleccionado) <= 14)) {
								busc_spain = true;
							} else if ((Number(seleccionado) >= 20) && (Number(seleccionado) <= 23)) {
								busc_spain = true;
							} else if ((Number(seleccionado) >= 25) && (Number(seleccionado) <= 33)) {
								busc_spain = true;
							} else if (Number(seleccionado) == 36) {
								busc_spain = true;
							} else if ((Number(seleccionado) >= 38) && (Number(seleccionado) <= 40)) {
								busc_spain = true;
							} else if ((Number(seleccionado) >= 52) && (Number(seleccionado) <= 54)) {
								busc_spain = true;
							} else {
							busc_spain = false;
							}
							var strSearch = libURL + '?lang='+lng+'&modo=1&zdesde='+seleccionado+strSublic;
							clearList('cbo_zonas_dev');
							loadXMLDoc(strSearch, 'cbo_zonas_dev', 'list', lng_mismoqrec);
							document.getElementById('cbo_zonas_dev').disabled = false;
							clearList('cbo_ofis_dev');
							document.getElementById('cbo_ofis_dev').disabled = true;
							break;
						case 'ofis_dev' :
							if ((document.getElementById('cbo_zonas_recogida').value != '') && (document.getElementById('cbo_zonas_dev').value != '') ) {
								var seleccionado = elem.options[elem.selectedIndex].value;
								var strSearch = libURL + '?lang='+lng+'&modo=2&zhasta='+seleccionado+'&zona='+document.getElementById('cbo_zonas_recogida').value+strSublic;
								loadXMLDoc(strSearch, 'cbo_ofis_dev', 'list', text);
								document.getElementById('cbo_ofis_dev').disabled = false;
							}
							break;
						case 'categorias' :
							var seleccionado = elem.options[elem.selectedIndex].value;
							var strSearch = libURL + '?lang='+lng+'&modo=3&ofi='+seleccionado+strSublic;
							loadXMLDoc(strSearch, 'cbo_cat', 'list', text);
							document.getElementById('cbo_cat').disabled = false;
							break;
					}
				}
			}
			catch (e) {
				alert ("Error");
				return;
			}
		}
	}
}


function clearDev() {
	clearList('cbo_zonas_dev');
	document.getElementById('cbo_zonas_dev').disabled = true;
	clearList('cbo_ofis_dev');
	document.getElementById('cbo_ofis_dev').disabled = true;
}		  

function cargaOfisdev(event) {
	if (document.getElementById('cbo_ofis_recogida').value) {
		var ofi = document.getElementById('cbo_ofis_recogida').value;
	} else {
		var ofi = '';
	}	
	var zonahasta = document.getElementById('cbo_zonas_dev').value;
	if (busc_spain == true) {
		if (zonahasta != '') {
			document.getElementById('cbo_ofis_dev').disabled = false;
			cargaLista(event, 'ofis_dev', lng_select);			
		} else {
			clonaUnaOfi();
		}
	} else {
		if (zonahasta != '') {
			document.getElementById('cbo_ofis_dev').disabled = false;
			cargaLista(event, 'ofis_dev', lng_select);
		} else {
			if (ofi != '') {
				// Tema clonacion solo si no hay ya seleccionado algo diferente arriba...
				clearList('cbo_ofis_dev');
				clonaOfis();
				document.getElementById('cbo_ofis_dev').disabled = false;
			} else {
				clearList('cbo_ofis_dev');
				document.getElementById('cbo_ofis_dev').disabled = true;
			}
		}
	}
}

/*
IMPORTANTE:
	El tema va asi:
	Para Las zonas de espaņa (cargaLista -> zonas_dev, busc_spain==true), solo se puede hacer la
	devolucion en una zona diferente a la de recogida, o en la misma oficina de recogida. Por lo
	que al cargar los 2 combos de devolucion se clona en ofi_devolucion el valor que haya en ofi_recogida
	(clonaUnaOfi), y aparte se aņaden el resto de zonas a las que se puede hacer one way, con sus
	respectivas oficinas. Para las zonas del extranjero (cargaLista -> zonas_dev, busc_spain==false),
	al cargar los 2 combos de devolucion se clona en ofi_devolucion el contenido completo del select
	ofi_recogida (clonaOfis), y aparte se aņaden el resto de zonas a las que se puede hacer one way,
	con sus respectivas oficinas.
*/

function clonaOfis() {
	p_prime = document.getElementById('cbo_ofis_recogida').cloneNode(true);
	ofis_nodes = document.getElementById('cbo_ofis_recogida').childNodes;
	for (var i = 0; i < ofis_nodes.length; i++) {
		nodo = ofis_nodes[i].cloneNode(true);
		document.getElementById('cbo_ofis_dev').appendChild(nodo);
	}
	indice = document.getElementById('cbo_ofis_recogida').selectedIndex;
	document.getElementById('cbo_ofis_dev').selectedIndex = indice;
}

function clonaUnaOfi() {
			clearList('cbo_ofis_dev');
			document.getElementById('cbo_ofis_dev').disabled = false;
			var selectOrig = document.getElementById('cbo_ofis_recogida');
			var select = document.getElementById('cbo_ofis_dev');
			//appendToSelect( select, document.createTextNode( getElementTextNSAttrib("", "data", items[i], 0) ), document.createTextNode( getElementTextNSAttrib("", "label", items[i], 0) ) );
			appendToSelect( select, document.createTextNode(selectOrig.value), document.createTextNode(selectOrig.options[selectOrig.selectedIndex].text));
}

// empty Topics select list content
function clearList(lista) {
	var select = document.getElementById(lista);
	if(select.hasChildNodes()) {
		while (select.length > 0) {
			select.remove(0);
		}
	}
}

// add item to select element the less
// elegant, but compatible way.
function appendToSelect(select, value, content) {
    var opt;
    opt = document.createElement("option");
    opt.value = value.data;
    opt.appendChild(content);
    select.appendChild(opt);
}

function clearInfo(x) {
	div = document.getElementById(x);
	div.style.display = 'none';
	div.innerHTML = '';
	
}

// fill Topics select list with items from
// the current XML document
function buildList(lista, req, text) {
	var select = document.getElementById(lista);
	// Recorre el XML y selecciona los elementos marcados con <item>
	var items = req.responseXML.getElementsByTagName("item");
	// Aņade un primer elemento a la lista con un texto indicativo
	if ( (lista == 'cbo_hdesde' ) || (lista == 'cbo_hhasta') ) {
		
	} else {
		appendToSelect( select, document.createTextNode(""), document.createTextNode(text));
	}
	// Aņade los elementos del XML a la lista, sacandolo de la plantilla:
	// <item label="texto" data="value"/>
	for (var i = 0; i < items.length; i++) {
		//alert(getElementTextNSAttrib("", "label", items[i], 0));
		appendToSelect( select, document.createTextNode( getElementTextNSAttrib("", "data", items[i], 0) ), document.createTextNode( getElementTextNSAttrib("", "label", items[i], 0) ) );
	}
	if ( document.getElementById('cbo_ofis_dev').length > 1 ) {
		var selObj = document.getElementById('cbo_ofis_dev');
		selObj.selectedIndex = document.getElementById('cbo_ofis_recogida').selectedIndex;
	}
}

function clearErrors(div) {
	div = document.getElementById('info_results');
	div.style.display = 'none';
	div.innerHTML = '';
}

function clearInfoResults() {
	var div = document.getElementById('info_busqueda');
	div.style.display = 'none';
	div.innerHTML = '';
}

function buildInfoResults () {
	var ofiSel = document.getElementById('cbo_ofis_recogida');
	ofiSel = ofiSel.options[ofiSel.selectedIndex].text;
	var znaSel = document.getElementById('cbo_zonas_recogida');
	znaSel = znaSel.options[znaSel.selectedIndex].text;
	var znadevSel = document.getElementById('cbo_zonas_dev');
	var ofidevSel = document.getElementById('cbo_ofis_dev');
	if (znadevSel.options[znadevSel.selectedIndex].value == '') {
		znadevSel = znaSel;
		ofidevSel = ofiSel;
	} else {
		znadevSel = znadevSel.options[znadevSel.selectedIndex].text;
		ofidevSel = ofidevSel.options[ofidevSel.selectedIndex].text;
	}
	var catSel = document.getElementById('cbo_cat');
	catSel = catSel.options[catSel.selectedIndex].text;
	var ddesde = document.getElementById('cbo_ddesde').value;
	var dhasta = document.getElementById('cbo_dhasta').value;	
	var hdesde = document.getElementById('cbo_hdesde').value;
	var hhasta = document.getElementById('cbo_hhasta').value;	
	var div = document.getElementById('info_busqueda');
	//Convertimos las Fechas
	var ddesde_array=ddesde.split("-");
	var hdesde_array=hdesde.split(":");
	fecha_desde = new Date(ddesde_array[2],(ddesde_array[1]-1),ddesde_array[0],hdesde_array[0],hdesde_array[1],0,0);
	var dhasta_array=dhasta.split("-");
	var hhasta_array=hhasta.split(":");
	fecha_hasta = new Date(dhasta_array[2],(dhasta_array[1]-1),dhasta_array[0],hhasta_array[0],hhasta_array[1],0,0);
	var numhoras = ( ( (fecha_hasta - fecha_desde) / 1000 ) / 60 ) / 60;
	var numdias = numhoras / 24;
	// Bastaria con redondear a la alta
	numdias = Math.ceil(numdias)
	if (numdias == "0") {
		numdias = 1;
	}
	// 86400000 milisegundos en un dia;
	div.innerHTML = '<h4>'+lng_resbuscando+'</h4><strong>'+lng_resdesde+'</strong>'+znaSel+', '+ofiSel+'<strong>'+lng_resfecha+'</strong>'+ddesde+'<strong>'+lng_reshora+'</strong>'+hdesde+'<br/><strong>'+lng_reshasta+'</strong>'+znadevSel+', '+ofidevSel+'<strong>'+lng_resfecha+'</strong>'+dhasta+'<strong>'+lng_reshora+'</strong>'+hhasta+'<br/><strong>'+lng_numdias+'</strong>: '+numdias;
	div.style.display = 'block';
}

function Mod(a, b) { return a-Math.floor(a/b)*b; } 

function Capitalize(obj) {
	newVal = '';
	val = obj.split(' ');
	for (var c=0; c < val.length; c++) {
		newVal += val[c].substring(0,1).toUpperCase() +
		val[c].substring(1,val[c].length) + ' ';
	}
	obj = newVal;
	return obj;
}


function muestraDatos(strSearch,elemento,cadena) {
	ElemMouseover = elemento;
	if (cadena) {
		return overlib(strSearch);
	} else {
		loadXMLDoc(strSearch, elemento, 'SimpleData', 'xxx');
	}
}

function borraDatos() {
	ElemMouseover = false;
	MostrandoDatos = false;
	document.getElementById('overDiv').style.visibility = "hidden";
	return nd();
}

function buidSearchResults(req) {

	var div = document.getElementById('info_results');
	div.className = "none";
	//Recorre el XML y selecciona los elementos marcados con <item>
	var items = req.responseXML.getElementsByTagName("item");
	if (items.length == 0) {
		bgColor = '#fff3f3';
		div.innerHTML = div.innerHTML + '<table border="0" cellspacing="0" cellpadding="0" style="width:475px; overflow:hidden;border-bottom: 1px solid #e0e0e0; text-align:center; background-color: '+bgColor+'; padding:0;margin:0;"><tr><td>'+lng_nodisponibles+'</td></tr></table>';
	} else {
		datosCabecera = '<table class="tabla" border="0" cellspacing="0" cellpadding="0" style="text-align:center; border:none;"><tr><th style="border-right: 1px solid #fff;" align="center" width="149px"><h4>'+lng_partner+'</h4></th><th style="border-right: 1px solid #fff;" align="center" width="89px"><h4>'+lng_precio+'</h4></th><th style="border-right: 1px solid #fff;" align="center" width="149px" ><h4>'+lng_coche+'</h4></th><th align="center" width="90px"><h4>'+lng_go+'</h4></th></tr></table>';
		div.innerHTML = div.innerHTML + datosCabecera;
		var bgColor = '#ffffff';
	}
	for (var i = 0; i < items.length; i++) {
		if (Mod(i,2) > 0) {
			bgColor = '#fafafa';
		} else {
			bgColor = '#ffffff';
		};
		var t_codofi = getElementTextNSAttrib("", "t_codofi", items[i], 0);
		var t_codofiret = getElementTextNSAttrib("", "t_ofiret", items[i], 0);
		var t_codgrp = getElementTextNSAttrib("", "t_codgrp", items[i], 0);
		var t_grpsub = getElementTextNSAttrib("", "t_grpsub", items[i], 0);
		var t_tarifa = getElementTextNSAttrib("", "t_tarifa", items[i], 0);
		var t_dias = getElementTextNSAttrib("", "t_dias", items[i], 0);
		var t_codvehi = getElementTextNSAttrib("", "t_codvehi", items[i], 0);
		var t_request = getElementTextNSAttrib("", "t_request", items[i], 0);
		var nameCar = getElementTextNSAttrib("", "t_modelo", items[i], 0);
		var grupoCar = getElementTextNSAttrib("", "t_grupo", items[i], 0);
		var t_dtoweb = getElementTextNSAttrib("", "t_dtoweb", items[i], 0);
		var t_total = getElementTextNSAttrib("", "t_total", items[i], 0);
		var t_totaldiv = getElementTextNSAttrib("", "t_totaldiv", items[i], 0);
		if ( (t_total == '9999,00') || (t_total == '9999.00') ) {
			bgColor = '#fff3f3';
			var infofechas_array=grupoCar.split("-");
			var desdeinfofechas_array=infofechas_array[0].split("_");
			var hastainfofechas_array=infofechas_array[1].split("_");
			var infofechas = desdeinfofechas_array[0]+'/'+desdeinfofechas_array[1] +' '+lng_al+' '+hastainfofechas_array[0]+'/'+hastainfofechas_array[1];
			var infoClosedofi = '<span style="font-size:12px; font-weight:bold;color:#ff3333">'+lng_closedofi+'</span><br/>'+lng_openfrom+' '+infofechas+'<br/>'+lng_sugerenciabuscar;
			var logoSrc = '<img onmouseover="muestraDatos('+"'"+strSearch+"','"+i+"b'"+');" onmouseout="borraDatos();" width="130px" style="border:none;" src="'+libImg +'LogPartn/'+getElementTextNSAttrib("", "t_sublic", items[i], 0) + '_sm.gif"/>';
			var infoSublic = '<table border="0" cellspacing="0" cellpadding="0" align="center"><tr><td align="center" style="font-size:10px;color:#a0a0a0;" valign="bottom">'+lng_inforaton+'</td></tr><tr><td align="center" valign="top">'+logoSrc+'</td></tr></table>';
			div.innerHTML = div.innerHTML + '<table border="0" cellspacing="0" cellpadding="0" style="width:475px; overflow:hidden;border-bottom: 1px solid #e0e0e0; text-align:center; background-color: '+bgColor+'; padding:0;margin:0;"><tr onMouseOver="className='+"'"+'marcado'+"'"+'" onMouseOut="className='+"'"+'none'+"'"+'"><td style="border-right: 1px solid #f0f0f0" width="149px" align="center">'+infoSublic+'</td><td style="border-right: 1px solid #f0f0f0;" align="center">'+infoClosedofi+'</td></tr></table>';
		} else {
			if (t_request != '0') {
				t_request = '';
				//t_request = '<tr><td align="center" style="font-weight:bold;color:#ff0000" >'+lng_onrequest+' <a href="javascript:void(0);" onmouseover="muestraDatos('+"'"+lng_infoRequest+"',null,true"+');" onmouseout="borraDatos();">'+lng_moreinfo+'</a></td></tr>';
			} else {
				t_request = '';
			}
			var strSearch = libURL + '?lang='+lng+'&modo=6&vehi=' + t_codvehi;
			var imgCar = '<img onmouseover="muestraDatos('+"'"+strSearch+"','"+i+"a'"+');" onmouseout="borraDatos();" width="140px" style="border:none;margin:0px;padding:0px;" src="'+libImg +'Cars/'+getElementTextNSAttrib("", "t_modelo", items[i], 0) + '_lg.gif"/>';
			var infoCar = '<table border="0" cellspacing="0" cellpadding="0" align="center"><tr><td align="center" style="font-size:10px;color:#a0a0a0;padding-top:5px;">'+lng_inforaton+'</td></tr><tr><td align="center">'+imgCar+'</td></tr>'+t_request+'<tr><td align="center" style="font-size:11px;padding-bottom:5px;"><strong>'+lng_grupo+' '+t_grpsub.toUpperCase()+' - '+Capitalize(nameCar)+'<span style="font-size:9px; color:#999999">'+lng_equiv+'</span></strong></td></tr></table>';
			var img_descuento = '';
			switch (t_dtoweb) {
				case '5,00' :
					img_descuento = '<tr><td align="center"><img src="'+libStyles+'_img/5descuento.gif"/></td></tr>';
					break;
			}
			var datosPrecio = '<table border="0" cellspacing="0" cellpadding="0" align="center" style="text-align:center; font-size:12px; font-weight:bold">'+img_descuento+'<tr><td align="center"><span style="font-size:20px;color:#66AA33;background-color:#F0FFF0;">'+t_total+' &euro; </span></td></tr><tr><td align="center" style="color:#66AA33;background-color:#F0FFF0;">'+t_totaldiv+' &pound; <span style="font-size:8px;">aprox</span></td></tr></table>';
			var strSearch = libURL + '?lang='+lng+'&modo=4'+'&ofi='+t_codofi;
			var img_logo = getElementTextNSAttrib("", "t_sublic", items[i], 0);
			switch (img_logo) {
			//Quitamos Herz -> sublicencia 39 ...
				case '39' :
					img_logo = '39bis';
					break;
			}
			var logoSrc = '<img onmouseover="muestraDatos('+"'"+strSearch+"','"+i+"b'"+');" onmouseout="borraDatos();" width="130px" style="border:none;" src="'+libImg +'LogPartn/'+img_logo+'_sm.gif"/>';
			var infoSublic = '<table border="0" cellspacing="0" cellpadding="0" align="center"><tr><td align="center" style="font-size:10px;color:#a0a0a0;" valign="bottom">'+lng_inforaton+'</td></tr><tr><td align="center" valign="top">'+logoSrc+'</td></tr></table>';
			var btn_GO = '<a href="'+libDest+strSearchPrincipal+'&idofi='+t_codofi+'&idofiret='+t_codofiret+'&grp='+t_codgrp+'&grpsub='+t_grpsub+'&vehi='+t_codvehi+'"><img src="'+libStyles+'_img/btn_go.gif"/></a>';
			div.innerHTML = div.innerHTML + '<table border="0" cellspacing="0" cellpadding="0" style="width:475px; overflow:hidden;border-bottom: 1px solid #e0e0e0; text-align:center; background-color: '+bgColor+'; padding:0;margin:0;"><tr onMouseOver="className='+"'"+'marcado'+"'"+'" onMouseOut="className='+"'"+'none'+"'"+'"><td style="border-right: 1px solid #f0f0f0" width="149px" align="center">'+infoSublic+'</td><td style="border-right: 1px solid #f0f0f0;" align="center" width="89px">'+datosPrecio+'</td><td style="border-right: 1px solid #f0f0f0;" align="center" width="149px" >'+infoCar+'</td><td align="center" width="89px">'+btn_GO+'</td></tr></table>';
		}
	}
	if (items.length == 0) {
	} else {
		div.innerHTML = div.innerHTML + datosCabecera;
	}
	div.style.display = 'block';
	window.scrollBy(0, 150);	
}

function validaSearch() {
	window.scrollBy(0, 150);
	var zona = document.getElementById('cbo_zonas_recogida').value;
	var ofi = document.getElementById('cbo_ofis_recogida').value;
	var zonahasta = document.getElementById('cbo_zonas_dev').value;
	var ofihasta = document.getElementById('cbo_ofis_dev').value;
	var cat = document.getElementById('cbo_cat').value;
	var textoError = '';
	var ddesde = document.getElementById('cbo_ddesde').value;
	var hdesde = document.getElementById('cbo_hdesde').value;
	var dhasta = document.getElementById('cbo_dhasta').value;
	var hhasta = document.getElementById('cbo_hhasta').value;
	var fecha_desde;
	var fecha_hasta;
	clearErrors();
	var ok = true;
	if ((document.getElementById('cbo_ofis_recogida').disabled != true) && (!ofihasta)) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_ofi_hasta+'</p>';
	}
	if (!zona) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_zona+'</p>';
	}
	if ( (zona != '') && (!ofi) ) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_destino+'</p>';
	}
	if ( (ofi != '') && (!cat) ) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_categoria+'</p>';
	}
	if(!ddesde) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_nofechadesde+'</p>';
	} else {
		var ddesde_array=ddesde.split("-");
		var today = new Date();
		today.setDate(today.getDate()-1);
		fecha_desde = new Date(ddesde_array[2],(ddesde_array[1]-1),ddesde_array[0]);
		if (fecha_desde < today) {
			ok = false;
			textoError = textoError + '<p style="font-size:12px">'+lng_error_malfechadesde+'</p>';
		}
	}
	if(!hdesde) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_nohoradesde+'</p>';
	}
	if(!dhasta) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_nofechahasta+'</p>';
	} else {
		var dhasta_array=dhasta.split("-");
		fecha_hasta = new Date(dhasta_array[2],(dhasta_array[1]-1),dhasta_array[0]);
		if (fecha_desde > fecha_hasta) {
			ok = false;
			textoError = textoError + '<p style="font-size:12px">'+lng_error_malfechahasta+'</p>';
		}
	}
	if(!hhasta) {
		ok = false;
		textoError = textoError + '<p style="font-size:12px">'+lng_error_nohorahasta+'</p>';
	}
	//Aqui comprobamor
	if (ok) {
		var div = document.getElementById('info_busqueda');
		div.innerHTML = '<h4>'+lng_resbuscando+'</h4><strong>Buscando, por favor espere...';
		div.style.display = 'block';
		buildInfoResults();
		doSearch();
	} else {
		clearInfoResults();
		div = document.getElementById('info_results');
		div.innerHTML = "";
		div.className = "errors";
		div.innerHTML = div.innerHTML + '<h4 style="font-size:12px">'+lng_error_titulo+'</h4>';
		div.innerHTML = div.innerHTML + textoError;
		div.style.display = 'block';
	}
}

function doSearch(zona,ofi,zonahasta,ofihasta,cat,f_des,h_des,f_has,h_has) {
	if (!zona) { zona = document.getElementById('cbo_zonas_recogida').value; }
	if (!ofi) { ofi = document.getElementById('cbo_ofis_recogida').value; }
	if (!zonahasta) { zonahasta = document.getElementById('cbo_zonas_dev').value; }
	if (!ofihasta) { ofihasta = document.getElementById('cbo_ofis_dev').value; }
	if (!cat) { cat = document.getElementById('cbo_cat').value; }
	if (!f_des) { f_des = document.getElementById('cbo_ddesde').value; }
	if (!h_des) { h_des = document.getElementById('cbo_hdesde').value; }
	if (!f_has) { f_has = document.getElementById('cbo_dhasta').value; }
	if (!h_has) { h_has = document.getElementById('cbo_hhasta').value; }
	if (!h_has) { h_has = document.getElementById('cbo_hhasta').value; }
	
		//Convertimos las Fechas
		var f_des_array=f_des.split("-");
		fecha_desde = new Date(f_des_array[2],(f_des_array[1]-1),f_des_array[0]);
		fecha_desde = fecha_desde.print("%Y-%m-%d")
		var f_has_array=f_has.split("-");
		fecha_hasta = new Date(f_has_array[2],(f_has_array[1]-1),f_has_array[0]);
		fecha_hasta = fecha_hasta.print("%Y-%m-%d")

	var strSublic = '';
	if (sublic) {
		strSublic = '&sublic='+sublic;
	}
	if (ofihasta) {
		ofihasta = ofihasta;
	} else {
		ofihasta = ofi;
	}
	var strSearch = libURL + '?lang='+lng+'&modo=5'+'&zona='+zona+'&ofi='+ofi+'&ofiret='+ofihasta+'&cat='+cat+'&f_des='+fecha_desde+'&h_des='+h_des+'&f_has='+fecha_hasta+'&h_has='+h_has+strSublic;
	strSearchPrincipal = '?lang='+lng+'&idzna='+zona+'&fdesde='+fecha_desde+'&hdesde='+h_des+'&fhasta='+fecha_hasta+'&hhasta='+h_has+strSublic;
	var div = document.getElementById('info_results');
	//div.innerHTML = div.innerHTML + strSearch;
	loadXMLDoc(strSearch, 'xxx', 'search', 'xxx');
}