//helper function to create the form
function getNewSubmitForm(){
 var submitForm = document.createElement("FORM");
 document.body.appendChild(submitForm);
 submitForm.method = "POST";
 submitForm.target = "_blank";
 return submitForm;
}

//helper function to add elements to the form
function createNewFormElement(inputForm, elementName, elementValue){
 var newElement = document.createElement("input");
	newElement.setAttribute("type", "hidden");
	newElement.setAttribute("name", elementName);
	newElement.setAttribute("value", elementValue);
 inputForm.appendChild(newElement);
 return newElement;
}

//function that creates the form, adds some elements
//and then submits it
function createFormAndSubmit(direccion,fechaInicio,fechaFin,idPortal,idZona, idAsociado, passwordAsociado){
 var submitForm = getNewSubmitForm();
 createNewFormElement(submitForm, "PARAM_v_idportal", idPortal);
 createNewFormElement(submitForm, "FECHA_INICIO", fechaInicio);
 createNewFormElement(submitForm, "FECHA_FIN", fechaFin);
 createNewFormElement(submitForm, "ESTANCIA", "");
 createNewFormElement(submitForm, "MINIMO", "");
 createNewFormElement(submitForm, "ORDER", "");
 createNewFormElement(submitForm, "IDORDEN", "");
 createNewFormElement(submitForm, "START", "");
 createNewFormElement(submitForm, "STOP", "");
 createNewFormElement(submitForm, "ORDER_DIRECTION", "");
 createNewFormElement(submitForm, "CONDICIONES", "");
 createNewFormElement(submitForm, "ROWS_PER_PAGE", "");
 createNewFormElement(submitForm, "PARAM_r_idprovincia", "");
 createNewFormElement(submitForm, "PARAM_v_idcomarca", "");
 createNewFormElement(submitForm, "PARAM_v_idcp", "");
 createNewFormElement(submitForm, "PARAM_v_idzona",idZona);
 createNewFormElement(submitForm, "USUARIO_ASOCIADO", idAsociado);
 createNewFormElement(submitForm, "PASSWORD_ASOCIADO",passwordAsociado);
 submitForm.action= direccion;
 submitForm.submit();
}




function ourDateStatusFunc(date, y, m, d) {
var ano = parseInt("2010",10);
  var mes = parseInt("01",10)-1;
  var dia = parseInt("26",10);
miFecha = new Date(ano,mes,dia); 
var date1Comp = date.getTime(); // milliseconds
var date2Comp = miFecha.getTime();
if (date1Comp < date2Comp)
{
return true;
}else{
return false;
}
}
function ourDateStatusFunc2(date, y, m, d) {
var ano = parseInt("2010",10);
  var mes = parseInt("01",10)-1;
  var dia = parseInt("26",10);
  var date3Comp = 1;
miFecha = new Date(ano,mes,dia); 
var date1Comp = date.getTime(); // milliseconds
var date2Comp = miFecha.getTime();
var field = document.getElementById("FECHA_INICIO");
if (field.value!=""){
var ano1 = parseInt(field.value.substring(6,10),10);
var mes1 = parseInt(field.value.substring(3,5),10)-1;
var dia1 = parseInt(field.value.substring(0,2),10);
miFecha2 = new Date(ano1,mes1,dia1);
date3Comp = miFecha2.getTime();
date3Comp += Date.DAY;
date2Comp = date3Comp;
}
if (date1Comp < date2Comp)
{
return true;
}else{
return false;
}
}
function data(){
var field = document.getElementById("FECHA_INICIO");
if (field.value!=""){						 
 var date3Comp = 1;
var ano1 = parseInt(field.value.substring(6,10),10);
var mes1 = parseInt(field.value.substring(3,5),10)-1;
var dia1 = parseInt(field.value.substring(0,2),10);
miFecha2 = new Date(ano1,mes1,dia1);
date3Comp = miFecha2.getTime();
date3Comp += Date.DAY;
if (date3Comp>1){
var field2 = document.getElementById("FECHA_FIN");
var date2 = new Date(date3Comp);
        field2.value = date2.print("%d/%m/%Y");
}
}
}
function checks(fieldInicio, fieldFin){
var field = document.getElementById("FECHA_INICIO");
var field2 = document.getElementById("FECHA_FIN");
var subm = true;
if ((field.value!="") && (field2.value=="")){
alert("La fecha de salida no esta informada");
subm= false;
}
if ((field.value=="") && (field.value!="")){
alert("La fecha de entrada no esta informada");
subm= false;
}
if ((field.value!="") && (field2.value!="")){
if (!validarFecha(field.value)){
alert("La fecha de entrada no esta en un formato correcto");
subm= false;
}
if (!validarFecha(field2.value)){
alert("La fecha de salida no esta en un formato correcto");
subm= false;
}
var ano1 = parseInt(field.value.substring(6,10),10);
var mes1 = parseInt(field.value.substring(3,5),10)-1;
var dia1 = parseInt(field.value.substring(0,2),10);
inicio = new Date(ano1,mes1,dia1);
var ano = parseInt(field2.value.substring(6,10),10);
var mes = parseInt(field2.value.substring(3,5),10)-1;
var dia = parseInt(field2.value.substring(0,2),10);
fin = new Date(ano,mes,dia);
var anoH = parseInt("2010",10);
var mesH = parseInt("01",10)-1;
var diaH = parseInt("26",10);
hoy = new Date(anoH,mesH,diaH); 
if ((inicio.getTime()<hoy.getTime()) || (fin.getTime()<hoy.getTime())){
alert("La fecha de entrada y de salida han de ser iguales o superiores al dia de hoy");
subm= false;
}
}
return subm;
}
function checkData(idEstablecimiento){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
	    wopen('https://www.tucarroya.com/tucarroya/servlet/Buscador?establecimiento='+idEstablecimiento+'&fechaInicio='+fieldInicio.value+'&fechaFin='+fieldFin.value+'&agregar=N&POPUP=N','window',1024,600);
	}else{
	    wopen('https://www.tucarroya.com/tucarroya/servlet/Home?establecimiento='+idEstablecimiento+'&agregar=N&POPUP=N','window',1024,600);
	}
	}else{
	return false;
	}
}
function checkPortalCerdanya(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
	    createFormAndSubmit('http://www.hotelscerdanya.com/portal/hotels_i_campings?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1',fieldInicio.value,fieldFin.value,'12','','','');
	 }else{
	   wopen('http://www.hotelscerdanya.com','window',1024,600);
	}
	}else{
	return false;
	}
}

function checkPortalVisualHoteles(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
	    createFormAndSubmit('http://www.visualhoteles.com/portal/reservas-online?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1',fieldInicio.value,fieldFin.value,'2','','','');
	 }else{
	   wopen('http://www.visualhoteles.com','window',1024,600);
	}
	}else{
	return false;
	}
}
function checkPortalPirineosOnline(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
	    createFormAndSubmit('http://www.pirineosonline.com/portal/alojamientos-pirineos?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1',fieldInicio.value,fieldFin.value,'13','','','');
	 }else{
	   wopen('http://www.pirineosonline.com','window',1024,600);
	}
	}else{
	return false;
	}
}

function checkPortalMadteam(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
	    createFormAndSubmit('http://www.refugiosonline.com/portal/refugios?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1',fieldInicio.value,fieldFin.value,'14','','','');
	 }else{
	   wopen('http://www.refugiosonline.com','window',1024,600);
	}
	}else{
	return false;
	}
}

function checkPortalFEEC(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
	    createFormAndSubmit('http://refugifeec.tucarroya.com/portal/reserva_refugios?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1',fieldInicio.value,fieldFin.value,'14','','','');
	 }else{
	   wopen('http://refugifeec.tucarroya.com','window',1024,600);
	}
	}else{
	return false;
	}
}

function checkPortalTural(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
	    createFormAndSubmit('http://www.turismoruralpirineos.info/portal/turismo_rural_huesca?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_count=1',fieldInicio.value,fieldFin.value,'8','','','');
	 }else{
	   wopen('http://www.turismoruralpirineos.info','window',1024,600);
	}
	}else{
	return false;
	}
}


function checkPortalRibagorza(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
		  //Comarca de la ribagorza
	    createFormAndSubmit('http://www.pirineosonline.com/portal/lista-alojamientos-pirineos?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_pos=1&p_p_col_count=3',fieldInicio.value,fieldFin.value,'13','34','','');
	 }else{
	   wopen('http://www.pirineosonline.com','window',1024,600);
	}
	}else{
	return false;
	}
}

function checkPortalBenasque(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
	var zona = document.getElementById("PARAM_v_idzona");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
		  //zona de benasque
	    createFormAndSubmit('http://www.pirineosonline.com/portal/lista-alojamientos-pirineos?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_pos=1&p_p_col_count=3',fieldInicio.value,fieldFin.value,'13',zona.options[zona.selectedIndex].value,'','');
	 }else{
	   wopen('http://www.pirineosonline.com','window',1024,600);
	}
	}else{
	return false;
	}
}

function checkPortalBenasqueAsociados(){
	var fieldInicio = document.getElementById("FECHA_INICIO");
	var fieldFin = document.getElementById("FECHA_FIN");
	var zona = document.getElementById("PARAM_v_idzona");
if (checks(fieldInicio, fieldFin)){
	
	if ((fieldInicio.value.length>0) && (fieldFin.value.length>0)){
		  //zona de benasque
	    createFormAndSubmit('http://www.pirineosonline.com/portal/lista-alojamientos-pirineos?p_p_id=cercador_portlet_WAR_tucarroya_portlets&p_p_lifecycle=1&p_p_state=maximized&p_p_mode=view&p_p_col_id=column-1&p_p_col_pos=1&p_p_col_count=3',fieldInicio.value,fieldFin.value,'13',zona.options[zona.selectedIndex].value,'1001','pipiolo');
	 }else{
	   wopen('http://www.pirineosonline.com','window',1024,600);
	}
	}else{
	return false;
	}
}


function checkUnaFecha(fieldInicio){
var field = fieldInicio;
var subm = true;
if ((field=="")){
alert("La fecha no esta informada");
subm= false;
}
if ((field!="")){
if (!validarFecha(field)){
alert("La fecha de entrada no esta en un formato correcto");
subm= false;
}
}
return subm;
}


function ourDateStatusFuncPorta(date, y, m, d) {
var ano = parseInt("2011",10);
  var mes = parseInt("06",10)-1;
  var dia = parseInt("3",10);
	miFecha = new Date(ano,mes,dia); 
	var date1Comp = date.getTime(); // milliseconds
	var date2Comp = miFecha.getTime();
	if (date1Comp < date2Comp)
	{
		return true;
	}else{
		ano = parseInt("2011",10);
	  mes = parseInt("9",10)-1;
	  dia = parseInt("12",10);
		miFecha = new Date(ano,mes,dia);
		date2Comp = miFecha.getTime();
		if ( date1Comp > date2Comp ){
			return true;
		}
		return false;
	}
}
