// JavaScript Document

function valida_form(theForm) {
	if ((theForm.prov.selectedIndex==0)) {
		alert(js_seleccione);
		return (false);
	}
	
	// 06.05.11 CC -- solo queremos que haga estas comprobaciones si la fecha está rellenada, pero no es campo requerido
	if (theForm.FechaI.value != "") {
		if (comparaFechas(theForm.FechaI.value) == false) {
			alert(js_valida_form_2);
			theForm.FechaI.focus();
			return (false);
		}
		if (!(esFechaValida(theForm.FechaI.value))) {
			alert(js_inicio_incorrecto);
			theForm.FechaI.focus();
			return (false);
		}
	}	
	
	if (theForm.FechaF.value != "") {	
		if (comparaFechas(theForm.FechaF.value) == false) {
			alert(js_valida_form_2);
			theForm.FechaF.focus();
			return (false);
		}
		
		if (!(esFechaValida(theForm.FechaF.value))) {
			alert(js_fin_incorrecto);
			theForm.FechaF.focus();
			return (false);
		}
	}

	// ECD - 26/10/2009 -  comprobamos si el precio es un integer / o no tiene nada
	if (theForm.precio.value != '') {
		if (!(IsNum(theForm.precio.value))) {
			alert(js_valida_form_6);
			return (false);
		}
	}	
	
	// 08.04.11 CC -- la fecha de fin no puede menor que la fecha de inicio
	if (compare_dates(theForm.FechaI.value, theForm.FechaF.value)){
		alert(js_inicio_posterior_fin);
		return (false);
	}
	
	// ECD - 06/06/2011 - la duración no puede ser superior a 90 días.
	if (DateDiff.inDays(GetDateStr(theForm.FechaI.value), GetDateStr(theForm.FechaF.value))>90){
		alert(js_duracion_supera_dias);
		return (false);
	}
	return (true);
}

//Elimina todas las opciones del combo correspondiente.
function removeAllOptions(selectbox) {
	var i;
	if (selectbox != null) {
		for(i=selectbox.options.length-1;i>=0;i--) {
			selectbox.remove(i);
		}
	}	
}

function loadLocalidades(provincia){
	var resultado;
	var sProvincia=js_strPosicion;
	
	//ECD - 20/12/2010 - comprobamos la ubicación donde cargaremos.
	
	//ECD - 08/07/2011 -  carga del desplegable en JQUERY
	var sURL = "http://"+ self.location.hostname;
	if (sURL.indexOf("http://localhost")>=0){
		sURL=sURL + "/casaspain/incluidos/formularios/listado_ciudades_select.asp?prov=" + provincia;
	}
	else sURL=sURL + "/incluidos/formularios/listado_ciudades_select.asp?prov=" + provincia;

	var resultado;
	var success;
	
	var jqxhr = $.get(sURL, function(data){
		if (document.getElementById("loc") != null) {
		fillNewSub(document.getElementById("loc"),data);
		}
		if (document.getElementById("vn_loc") != null) {
			fillNewSub(document.getElementById("vn_loc"),data);
		}     
   }); 
}

//ECD - Añade las opciones al combo
function addOption(selectbox, value, text, selected) {
	if (selectbox != null) {
		var optn=new Option(text, value, false, selected);
		selectbox.options.add(optn);
	}
}

//ECD - Rellena las opciones
function fillNewSub(selectbox,values) {	
	removeAllOptions(selectbox);
	var opciones=values.split(";");

	var auxValores;
	var strValor;
	var RES_loc=js_RES_loc;
	var i=0;
	for (i=0;i<opciones.length;i++){ 
		strValor=opciones[i];
		if(strValor != ""){
			auxValores=strValor.split(",");
			// anyadimos los valores al desplegable.
			// Comprobamos si es el valor seleccionado.
			if (auxValores[0]==RES_loc) {
				addOption(selectbox,auxValores[0],auxValores[1],true);
				} else addOption(selectbox,auxValores[0],auxValores[1],false);
				
		}
	}
}

//Validar si la fecha introducida manualmente es correcta.
function esFechaValida(fecha){
		
	if (fecha == null || fecha == '') {
		alert(js_valida_form_1);
		return (false);
	} else {
		
		if (!/^\d{2}\/\d{2}\/\d{4}$/.test(fecha)){		
			alert(js_formato_incorrecto);			
			return (false);		
		}
		
		var dia = parseInt(fecha.substring(0,2),10);		
		var mes = parseInt(fecha.substring(3,5),10);		
		var anio = parseInt(fecha.substring(6),10);	
	
		switch(mes) {
			case 1: case 3: case 5:	case 7:	case 8:	case 10: case 12:		
				numDias=31;			
				break;	
			case 4: case 6: case 9: case 11:	
				numDias=30;
				break;
			case 2:
				if (comprobarSiBisisesto(anio)){ numDias=29 }else{ numDias=28};
				break;
			default:
				alert("Fecha introducida erronea");
				return false;
		}
		
		if (dia > numDias || dia == 0) { 
			alert(js_fecha_erronea);
			return (false);
		}
		
		var f = new Date();
				
		var fecha_actual = DateToStr(f);
		
		if (!(compare_dates(fecha, fecha_actual))) {
			alert(js_valida_form_2);
			return(false);
		}
			
		return (true);
	}
	
}
