// JavaScript Document

 function mostra(desp)
 {
	for(a=0;a<desp.length;a++)
	{
		if(desp[a].selected)
			valor = desp[a].value;
	}
	window.open('../cargas/infoCarga.asp?idCarga=' + valor, 'finestra', 'width=380,height=280,menubar=no,toolbar=no,location=no,status=no,scrollbars=no') 
 }




	function muestraTablaIdioma(idioma)
	{

	 elem = document.getElementsByTagName("DIV");

 	for (x=1;x<elem.length;x++)
		{
		elem[x].style.visibility = "hidden";
		}
	eval("document.all.div"+idioma+".style.visibility = 'visible'");
	}

// Activa/Desactiva que tingui activar = 1 de un formulari
function activa(valor, form)
{
 for(i = 0; i < form.length; i++)
 { if(form[i].activar)
    { if(valor)
	  { form[i].disabled = false;
		form[i].obligatorio = 1; }
	  else
	  { form[i].disabled = true; 
		form[i].obligatorio = 0; } 
    }	
 }
}

//Es crida en el keypress per a controlar el tamany dels textareas
function maxLen(maxim, actual)
{
	if (actual >= maxim)
		return false;
}


// Control general del formulari
function control(form, esPromocion, lang)
{
	
	
	
	if(!form.title[lang].value || !form.body[lang].value || !form.descripcion.value)
	{
		alert("Debe rellenar todos los campos en negrita.");
		return false;
	}

	if(form.descripcion.value.length > 2500)
	{
		alert("El campo descripción es demasiado largo, reduzca el texto o el código HTML.");
		return false;
	}
	
	if (esPromocion == "true" || esPromocion == "verdadero")
	{
		 		 
		 if( parseInt(form.precioI.value) > parseInt(form.precioF.value))
		 {
			alert("Comprueba los precios.");
			form.precioI.focus();
			return false;
		 }
		 
		 
		if(parseInt(form.metrosI.value) > parseInt(form.metrosF.value))
		 {
			alert("Hay un error en los metros cuadrados.");
			form.metrosI.focus();
			return false;
		 }	 

		 if(parseInt(form.habitacionesI.value) > parseInt(form.habitacionesF.value))
		 {
			alert("Hay un error en las habitaciones.");
			form.habitacionesI.focus();
			return false;
		 }

		 for(i = 0; i < form.length; i++)
		 {
		  if(form[i].obligatorio && !form[i].value)
		  {
		   alert("Debe rellenar todos los campos en negrita.");
		   form[i].focus();
		   return false;
		  }
		  
		 }
	}
}



// Comprobación de las fechas
function compruebaFechas()
{
var newDate = flipDate(document.all.fechaI.value);
var endDate = flipDate(document.all.fechaF.value);



  if(newDate != false && endDate != false)
  {
	 	if(endDate < newDate)
		{
		 document.all.cmdSubmit.disabled = true;
		 document.all.okFechaI.src = "../../images/falso.gif";
		}	
		else
		{
		 document.all.okFechaI.src = "../../images/verdadero.gif";
		 document.all.okFechaF.src = "../../images/verdadero.gif";		 
		 document.all.cmdSubmit.disabled = false;
		}
   }
   else
   { 
    document.all.cmdSubmit.disabled = true;
	if(newDate != false)
		document.all.okFechaF.src = "../../images/falso.gif";
	if(endDate != false)
		document.all.okFechaI.src = "../../images/falso.gif";
	}  
 
}

//Actualizar fecha
function actualizaFecha()
{

 if(document.all.actFecha.checked)
 {
  document.all.newDate.value = document.all.fechaActual.value;
  document.all.newDate.disabled = true;
 }
 else
 {
  document.all.newDate.value = document.all.copiaFecha.value;
  document.all.newDate.disabled = false;
 }
 
}



// etsa funcion cambia una fecha del formato DD/MM/AAAA al formato AAAA/MM/DD
function flipDate(fecha)
{
anio = fecha.substring(6,10);
mes = fecha.substring(3,5);
dia = fecha.substring(0,2);

	if(checkDate(fecha) == true)
	 return anio+"/"+mes+"/"+dia;
	else return false;
	
}

// esta funcion comprueba si una fecha es correcta
function checkDate(fecha)
{
anio = fecha.substring(6,10);
mes = fecha.substring(3,5);
dia = fecha.substring(0,2);


	if(fecha.charAt(2) != "/" || fecha.charAt(5) != "/" || fecha.length != 10)
	{ return false; }
	else
	{
	 if(isNaN(anio) || isNaN(mes) || isNaN(dia)) return false;
	 else return true;
	}
	
}

// Ens genera un option de la llista en blanc
function genItemList(idList)
{
	num = document.getElementById(idList).options.length;

	if (document.getElementById(idList).options[num - 1].value != '')
	{
     document.getElementById(idList).options[num] =  new Option('', '');
	}
	return true;
}


// Per a veure la imatge seleccionada
function imageViewer(path, type, message)
{
 num = document.getElementById(type).selectedIndex;
 image = document.getElementById(type).options[num].value;
 
 newWindow =  window.open("../../admin/upload/images.asp?image=" + path + image + '&message=' + message + '&type=' + type,"imagen",'height=500,width=455,scrollbars=yes');

}

// Eliminar la imatge seleccionada
function imageDelete(path, type, message)
{

 if(window.confirm(message))
 {
  num = window.opener.document.getElementById(type).selectedIndex;
  window.opener.document.getElementById(type).options[num] = null;
  window.location = "images.asp?image=" + path + "&message=" + message + "&type=" + type + "&del=true";
 }

}

function Comproba(form)
{
	var i = 0;
			
	while ( document.getElementsByName("precio" + i ).length > 0  )
	{

		campo = eval("form.precio" + i )
		
		if ( isNaN( campo.value - 0 ) )
		{
			alert("Revise los precios") 
			campo.focus();
			return false;
		}
		i++;
		
	}
	
	
	
	for(i = 0; i < form.length; i++)
	{
	
		if(form[i].obligatorio && !form[i].value)
		{
		 //alert(form[i].obligatorio)
		 if(form[i].obligatorio == 0) { return true; }
		 
		 alert("Debe rellenar todos los campos.");
		 form[i].focus();
		 return false;
		}
		
	}
	
	return true;
}

function ComprobaPed(form) {

for(i = 0; i < form.length; i++) {

		if( form[i].obligatorio && !form[i].value) {
			if(form[i].obligatorio == 0) { return true; }
			
			alert("Debe rellenar todos los campos.");
			form[i].focus();
			return false;
			
		}
		
		if(form[i].condicion && !form[i].checked){
	
			 if(form[i].condicion == 0) { 
			 
				 return true;
				 
			 } else {
				 
				 alert("Debe aceptar las condiciones.");
				 form[i].focus();
				 return false;
			 }
		}
	}
}

//funcio que revisa si hi ha un numero amb o sense punt o coma
function checkNumber(obj){
	var num =  new Number(obj.value.replace(",","."))
	if(!isNaN(num)){
		obj.value = num
	}
	else{
		alert("Introduzca un número válido Ej: 12 o 12.3 o 12,3")
		return false;
	}
}
//funcio que revisa si hi ha un numero sense punt o coma
function checkNumber2(obj){

	var num =  new Number(parseInt(obj.value))
	if(!isNaN(num)){
		obj.value = num
		}
	else{
		alert("Introduzca un número válido Ej: 12 o 12.3 o 12,3")
		return false;
	}
}

//funcio per al menu administradors que comproba que el camp
//email sigui vàlid i que el password sigui d'almenys 3 caràcters
function funAdmin(email,pass) {
	var errorMsg = "";
	var re  = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	var error1 = 0;
		if (!re.test(email.value)) {
			error1 = 1;
			}		
	if (error1 == 1){
	errorMsg += "Introduzca un e-mail válido\n";
	}

	if (pass.value.length < 3){
	errorMsg += "\Introduzca un password mayor de tres carácteres";
	}
	if (errorMsg != ""){
		alert(errorMsg);
		return false;
	}
	return true;
}
