
function validaContacto()
{	var texto;
	texto = "";

	var cadenaEnvio;
	cadenaEnvio = "";

	if ( document.getElementById("formularioContacto").nombres.value == "" ) {
		texto+=" * Debe indicar su nombre(s).\n";
	}/*else{
		cadenaEnvio="nombres=" + document.getElementById("formularioContacto").nombres.value;
	}*/

	if ( document.getElementById("formularioContacto").apellidos.value == "" ) {
		texto+=" * Debe indicar su apellido(s).\n";
	}/*else{
		cadenaEnvio+="&apellidos=" + document.getElementById("formularioContacto").apellidos.value;
	}*/

	if ( document.getElementById("formularioContacto").email.value == "" ) {
		texto+=" * Debe indicar su e-mail.\n";
	}
	
	if(document.getElementById("formularioContacto").email.value!=""){
		if(!validarEmail(document.getElementById("formularioContacto").email.value)){
				texto+=" * Formato de E-mail incorrecto.\n";
		}/*else{
			cadenaEnvio+="&email=" + document.getElementById("formularioContacto").email.value;
		}*/
	}
	if ( document.getElementById("formularioContacto").comentario.value == "" ){
		texto+=" * Debe incluir el motivo de su consulta.\n";
	}/*else{
		cadenaEnvio+="&comentarios=" + document.getElementById("formularioContacto").comentario.value;
	}*/	
	
	//Aqui vamos añadiendo los demas campos sino estan vacios	
	/*if ( document.getElementById("formularioContacto").telefono.value != "" ){
		cadenaEnvio+="&telefono=" + document.getElementById("formularioContacto").telefono.value;
	}*/

	if ( texto != "" ){
		/*document.getElementById("mensajeError").innerHTML=texto;
		document.getElementById("capaError").style.display='block';*/
    	 alert("Se han encontrado las siguientes omisiones:\n\n"+texto);		
		
	}else{
		document.formularioContacto.submit();
		/*alert("se enviaaaaaaa");
		document.getElementById("mensaje-respuesta").innerHTML="";
		document.getElementById("mensajeError").innerHTML="";
		document.getElementById("capaError").style.display='none';
		http.open("GET", "/includes/enviocontacto.php?" + cadenaEnvio, true);
		http.onreadystatechange = handleHttpResponseContacto;
		http.send(null);*/
	}
	
}

function validaNif(nif)
{	//funcion que valida el nif
	var letrita= "TRWAGMYFPDXBNJZSQVHLCKE";
	var numerete;
	var letrika;
	var indicek;
 
	//numero y letra
	if ((nif.length<2)||(nif.length>9)) return false;
	//extranjero
	if (((nif.substr(0,1))=='x')||((nif.substr(0,1))=='X')) numerete= nif.substr(1,(nif.length-2));
	else numerete= nif.substr(0,(nif.length-1));
	//if (IsNumeric(numerete,true,true)) {
	if (validarNumero(numerete))
	{	numerete=parseInt(numerete);
		letrika=nif.substr((nif.length-1),(nif.length));
		letrika=letrika.toUpperCase();
		indicek=(numerete%23);
		if ((letrika)==(letrita.charAt(indicek))) {return true;}
		else {return false;}
	}
	else return false;
}

function validarEmail(valor)
{	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor));		}


function compruebaFecha(dia, mes, anyo){
	if ((dia == 0) || (mes == 0) || (anyo == 0)){
		return false;
	}
	
	var error;
	if ((dia > 30) && ((mes == 2) || (mes == 4) || (mes == 6) || (mes == 9) || (mes == 11))){
		return false;
	}
	if ((dia > 29) && (mes == 2)){
		return false;
	}
	var resto = parseInt(anyo)%4;
	if (resto != 0){
		if ((dia > 28) && (mes == 2)){
			return false;
		}
	}
	
	return true;
}
 	
function validaCuentaBancaria(i_entidad, i_oficina, i_digito, i_cuenta)
{  		var wtotal,wcociente, wresto;
		if (i_entidad.length != 4)
		{	return false;		}
		if (i_oficina.length != 4)
		{	return false;		}
		if (i_digito.length != 2)
		{	return false;		}
		if (i_cuenta.length != 10)
		{	return false;		}
		wtotal = i_entidad.charAt(0) * 4;
		wtotal += i_entidad.charAt(1) * 8;
		wtotal += i_entidad.charAt(2) * 5;
		wtotal += i_entidad.charAt(3) * 10;
		wtotal += i_oficina.charAt(0) * 9;
		wtotal += i_oficina.charAt(1) * 7;
		wtotal += i_oficina.charAt(2) * 3;
		wtotal += i_oficina.charAt(3) * 6;
	
		// busco el resto de dividir wtotal entre 11
		wcociente = Math.floor(wtotal / 11);
		wresto = wtotal - (wcociente * 11);
	//
		wtotal = 11 - wresto;
		if (wtotal == 11)
		{	wtotal=0;		}
		if (wtotal == 10)
		{	wtotal=1;		}
		if (wtotal != i_digito.charAt(0))
		{	return false;		}
	//hemos validado la entidad y oficina
	//-----------------------------------
		wtotal = i_cuenta.charAt(0) * 1;
		wtotal += i_cuenta.charAt(1) * 2;
		wtotal += i_cuenta.charAt(2) * 4;
		wtotal += i_cuenta.charAt(3) * 8;
		wtotal += i_cuenta.charAt(4) * 5;
		wtotal += i_cuenta.charAt(5) * 10;
		wtotal += i_cuenta.charAt(6) * 9;
		wtotal += i_cuenta.charAt(7) * 7;
		wtotal += i_cuenta.charAt(8) * 3;
		wtotal += i_cuenta.charAt(9) * 6;
	
	// busco el resto de dividir wtotal entre 11
		wcociente = Math.floor(wtotal / 11);
		wresto = wtotal - (wcociente * 11);
	//
		wtotal = 11 - wresto;
		if (wtotal == 11)
		{	wtotal=0;		}
		if (wtotal == 10)
		{	wtotal=1;		}
		
		if (wtotal != i_digito.charAt(1))
		{	//alert(wtotal+' y no '+i_digito.charAt(1));
			return false;
		}
		// hemos validado la cuenta corriente
		return true;
}
  		
		
function validaFormCasa()
{	var texto;
	texto = "";

	var cadenaEnvio;
	cadenaEnvio = "";
	//1	
	if ( document.getElementById("formularioContacto").nombresCliente.value == "" )
		texto+=" * Debe indicar su nombre(s).\n\n";

	if ( document.getElementById("formularioContacto").apellidosCliente.value == "" )
		texto+=" * Debe indicar su apellido(s).\n\n";

	if(document.getElementById("formularioContacto").email.value!="")
		if(!validarEmail(document.getElementById("formularioContacto").email.value))
				texto+=" * Formato de E-mail incorrecto.\n\n";

	if ( document.getElementById("formularioContacto").fonoCliente.value == "" )
	{	if ( document.getElementById("formularioContacto").movilCliente.value == "" )
			texto+=" * Debe indicar un teléfono o un móvil.\n\n"		
		else		
		{	if ( document.getElementById("formularioContacto").movilCliente.value.length != 9 )
				texto+=" * El móvil debe tener 9 números (ahora tiene="+ document.getElementById("formularioContacto").movilCliente.value.length +").\n";
			else	
				if (!validarNumero(document.getElementById("formularioContacto").movilCliente.value))
					texto+=" * El número de móvil debe ser numérico.\n\n";
		}
	}
	else
	{	if ( document.getElementById("formularioContacto").fonoCliente.value.length != 9 )
				texto+=" * El teléfono debe tener 9 números (ahora tiene="+ document.getElementById("formularioContacto").fonoCliente.value.length +").\n";
		else	
			if (!validarNumero(document.getElementById("formularioContacto").fonoCliente.value))
				texto+=" * El teléfono debe ser numérico.\n\n";
	}


	if ( document.getElementById("formularioContacto").horarioContactoCliente.value == "-1" )
		texto+=" * Debe indicar el horario de contactar al Cliente.\n\n";

	//2
	if ( document.getElementById("formularioContacto").direccionOrigen.value == "" ) {
		texto+=" * Debe indicar su dirección origen.\n\n";
	}

	if ( document.getElementById("formularioContacto").ciudadOrigen.value == "" ) {
		texto+=" * Debe indicar su ciudad origen.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").cpOrigen.value)){
		texto+=" * El código postal debe ser numérico.\n\n";
	}

	if ( document.getElementById("formularioContacto").provinciaOrigen.value == "-1" ) {
		texto+=" * Debe indicar la provincia dónde vive el cliente.\n\n";
	}

	if ( document.getElementById("formularioContacto").tipoViviendaOrigen.value == "-1" ) {
		texto+=" * Debe indicar su tipo de vivienda.\n\n";
	}

	if ( document.getElementById("formularioContacto").desmontaje.value == "-1" ) {
		texto+=" * Debe indicar si desea el servicio de desmontaje.\n\n";
	}


	//3
	if ( document.getElementById("formularioContacto").direccionDestino.value == "" ) {
		texto+=" * Debe indicar su dirección destino.\n\n";
	}

	if ( document.getElementById("formularioContacto").ciudadDestino.value == "" ) {
		texto+=" * Debe indicar su ciudad destino.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").cpDestino.value)){
		texto+=" * El código postal destino debe ser numérico.\n\n";
	}

	if ( document.getElementById("formularioContacto").provinciaDestino.value == "-1" ) {
		texto+=" * Debe indicar la provincia dónde residirá el cliente.\n\n";
	}

	if ( document.getElementById("formularioContacto").tipoViviendaDestino.value == "-1" ) {
		texto+=" * Debe indicar su tipo de vivienda destino.\n\n";
	}

	if ( document.getElementById("formularioContacto").montaje.value == "-1" ) {
		texto+=" * Debe indicar si desea el servicio de montaje.\n\n";
	}
	
	
	/*
	if (!validarNumero(document.getElementById("formularioContacto").numKm.value)){
		texto+=" * La distancia entre origen y destino debe ser numérica.\n\n";
	}
	*/

	/*if ((!validarNumero(document.getElementById("formularioContacto").Dmudanza.value)) || (!validarNumero(document.getElementById("formularioContacto").Mmudanza.value)) || (!validarNumero(document.getElementById("formularioContacto").Amudanza.value)))
	{			texto+=" * La fecha de mudanza es incorrecta.\n\n"; }
	else
	{	var dia, mes, anyo;
	
		dia = document.getElementById('formularioContacto').Dmudanza.value;;
		mes = document.getElementById('formularioContacto').Mmudanza.value;
		anyo= document.getElementById('formularioContacto').Amudanza.value;
		
		if (!compruebaFecha(dia, mes, anyo))
			texto+=" * La fecha de mudanza es incorrecta.\n\n";
	}
	*/
	//alert (document.getElementById("formularioContacto").fechaMudanza.value);
	
	if (document.getElementById("formularioContacto").fechaMudanza.value=="999")
	{	//var xDia=document.getElementById("formularioContacto").DIAfechaMudanza.value;
		//var xMes=document.getElementById("formularioContacto").MESfechaMudanza.value;
		//var xAnio=document.getElementById("formularioContacto").ANIOfechaMudanza.value;
		//alert("fecha firefox"+xDia+"--"+xMes+"--"+xAnio);
		document.getElementById("formularioContacto").fechaMudanza.value=document.getElementById("formularioContacto").ANIOfechaMudanza.value+"-"+document.getElementById("formularioContacto").MESfechaMudanza.value+"-"+document.getElementById("formularioContacto").DIAfechaMudanza.value;
	}
	
	//alert (document.getElementById("formularioContacto").fechaMudanza.value);

	var xDia=document.getElementById("formularioContacto").fechaMudanza.value.substr(8,2);
	var xMes=document.getElementById("formularioContacto").fechaMudanza.value.substr(5,2);
	var xAnio=document.getElementById("formularioContacto").fechaMudanza.value.substr(0,4);

	//alert("fecha firefox"+xDia+"--"+xMes+"--"+xAnio);

	if (!compruebaFecha(xDia, xMes, xAnio)){
		texto+=" * La fecha de mudanza es incorrecta.\n\n";
		//texto+="---- " + xDia + "----" + xMes + "-----" + xAnio + "---" ;		
	}
	

	if (document.getElementById("formularioContacto").aceptaCondiciones.checked==true){
		;
	}else
		texto+=" * Debe indicar su conformidad con las condiciones.\n\n";




//Envio s todo OK
	if ( texto != "" )
    	 alert("Se han encontrado las siguientes omisiones:\n\n"+texto);
	else
		 document.formularioContacto.submit();
}


function validaFormOficina()
{	var texto;
	texto = "";

	var cadenaEnvio;
	cadenaEnvio = "";
	//1	
	if ( document.getElementById("formularioContacto").nombreEmpresa.value == "" )
		texto+=" * Debe indicar el nombre de la empresa.\n\n";
		
	//1.1
	if ( document.getElementById("formularioContacto").nombresCliente.value == "" )
		texto+=" * Debe indicar su nombre(s).\n\n";

	if ( document.getElementById("formularioContacto").apellidosCliente.value == "" )
		texto+=" * Debe indicar su apellido(s).\n\n";

	if(document.getElementById("formularioContacto").email.value!="")
		if(!validarEmail(document.getElementById("formularioContacto").email.value))
				texto+=" * Formato de E-mail incorrecto.\n\n";

	if (!validarNumero(document.getElementById("formularioContacto").fonoCliente.value)){
		texto+=" * El número de teléfono debe ser numérico.\n\n";
	}

	if ( document.getElementById("formularioContacto").horarioContactoCliente.value == "-1" )
		texto+=" * Debe indicar el horario de contactar al Cliente.\n\n";

	//2
	if ( document.getElementById("formularioContacto").direccionOrigen.value == "" ) {
		texto+=" * Debe indicar su dirección origen.\n\n";
	}

	if ( document.getElementById("formularioContacto").ciudadOrigen.value == "" ) {
		texto+=" * Debe indicar su ciudad origen.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").cpOrigen.value)){
		texto+=" * El código postal debe ser numérico.\n\n";
	}

	if ( document.getElementById("formularioContacto").provinciaOrigen.value == "-1" ) {
		texto+=" * Debe indicar la provincia dónde vive el cliente.\n\n";
	}

	if ( document.getElementById("formularioContacto").tipoViviendaOrigen.value == "-1" ) {
		texto+=" * Debe indicar su tipo de vivienda.\n\n";
	}

	if ( document.getElementById("formularioContacto").desmontaje.value == "-1" ) {
		texto+=" * Debe indicar si desea el servicio de desmontaje.\n\n";
	}


	//3
	if ( document.getElementById("formularioContacto").direccionDestino.value == "" ) {
		texto+=" * Debe indicar su dirección destino.\n\n";
	}

	if ( document.getElementById("formularioContacto").ciudadDestino.value == "" ) {
		texto+=" * Debe indicar su ciudad destino.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").cpDestino.value)){
		texto+=" * El código postal destino debe ser numérico.\n\n";
	}

	if ( document.getElementById("formularioContacto").provinciaDestino.value == "-1" ) {
		texto+=" * Debe indicar la provincia dónde residirá el cliente.\n\n";
	}

	if ( document.getElementById("formularioContacto").tipoViviendaDestino.value == "-1" ) {
		texto+=" * Debe indicar su tipo de vivienda destino.\n\n";
	}

	if ( document.getElementById("formularioContacto").montaje.value == "-1" ) {
		texto+=" * Debe indicar si desea el servicio de montaje.\n\n";
	}
	
	
	/*
	if (!validarNumero(document.getElementById("formularioContacto").numKm.value)){
		texto+=" * La distancia entre origen y destino debe ser numérica.\n\n";
	}
	
	if ((!validarNumero(document.getElementById("formularioContacto").Dmudanza.value)) || (!validarNumero(document.getElementById("formularioContacto").Mmudanza.value)) || (!validarNumero(document.getElementById("formularioContacto").Amudanza.value)))
	{			texto+=" * La fecha de mudanza es incorrecta.\n\n"; }
	else
	{	var dia, mes, anyo;
	
		dia = document.getElementById('formularioContacto').Dmudanza.value;;
		mes = document.getElementById('formularioContacto').Mmudanza.value;
		anyo= document.getElementById('formularioContacto').Amudanza.value;
		
		if (!compruebaFecha(dia, mes, anyo))
			texto+=" * La fecha de mudanza es incorrecta.\n\n";
	}
*/
	var xDia=document.getElementById("formularioContacto").fechaMudanza.value.substr(8,2);
	var xMes=document.getElementById("formularioContacto").fechaMudanza.value.substr(5,2);
	var xAnio=document.getElementById("formularioContacto").fechaMudanza.value.substr(0,4);
	/*alert(xDia);
	alert(xMes);
	alert(xAnio);	*/

	if (!compruebaFecha(xDia, xMes, xAnio)){
		texto+=" * La fecha de mudanza es incorrecta.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").numLibros.value)){
		texto+=" * La cantidad de cajas para libros debe ser numérico.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").numOrdenadores.value)){
		texto+=" * La cantidad de Ordenadores debe ser numérico.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").numMesasTrabajo.value)){
		texto+=" * La cantidad de Mesas de Trabajo debe ser numérico.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").numMesasReunion.value)){
		texto+=" * La cantidad de Mesas de Reunión debe ser numérico.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").numArmarios.value)){
		texto+=" * La cantidad de Armarios debe ser numérico.\n\n";
	}
	
	if (!validaDecimales(document.getElementById("formularioContacto").CubicajeObjeto001.value)){
		texto+=" * El cubicaje 1 debe ser numérico.\n\n";
	}

	if (!validaDecimales(document.getElementById("formularioContacto").CubicajeObjeto002.value)){
		texto+=" * El cubicaje 2 debe ser numérico.\n\n";
	}

	if (!validaDecimales(document.getElementById("formularioContacto").CubicajeObjeto003.value)){
		texto+=" * El cubicaje 3 debe ser numérico.\n\n";
	}


	if (!validarNumero(document.getElementById("formularioContacto").CantidadObjeto001.value)){
		texto+=" * la Cantidad debe ser numérico.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").CantidadObjeto002.value)){
		texto+=" * la Cantidad debe ser numérico.\n\n";
	}

	if (!validarNumero(document.getElementById("formularioContacto").CantidadObjeto003.value)){
		texto+=" * la Cantidad debe ser numérico.\n\n";
	}	

	if (document.getElementById("formularioContacto").aceptaCondiciones.checked==true){
		;
	}else
		texto+=" * Debe indicar su conformidad con las condiciones.\n\n";




//Envio s todo OK form OFICINAAAAAAAAA
	if ( texto != "" )
    	 alert("Se han encontrado las siguientes omisiones:\n\n"+texto);
	else
		 document.formularioContacto.submit();
}

function validarNumero(c_numero) 
{ 
   //chequeo la longitud de c_numero: 
   // Si (c_numero.length es igual a Cero) quiere decir que c_numero es una cadena Vacía. 
   // Si (c_numero.length es distinto(mayor) de Cero) podemos asegurar que c_numero contiene por lo menos una letra 
   //a la cual se le puede hacer la validación 
   if (c_numero.length == 0) 
       return false; 
   else 
   { 
       //Se recorre c_numero por todos sus caracteres chequeando que todos sean dígitos 
       //la condición >="0" y <="9" es basada en el valor ascii que tienen los números en la tabla ascii. 
       //Si alguno de los caracteres no es un número la función retornará un NaN 
       //Si no retornará el Número 
       for (i = 0; i < c_numero.length; i++) 
       { 
          if (!((c_numero.charAt(i) >= "0") && (c_numero.charAt(i) <= "9"))) 
          return false; 
       } 
       return true; 
   } 
} 

function validaDecimales(oInput)
{   if (! expesionValidaDecimales(oInput) )       
    	return(false);
    else
	    return(true);
}       

function expesionValidaDecimales(value)
{    return(value.match(/^[0-9]+(,[0-9]+)*$/))
}


function validaMiniFormCasa()
{	var texto;
	texto = "";

	var cadenaEnvio;
	cadenaEnvio = "";
	//1	
	if ( document.getElementById("formularioContacto").nombresCliente.value == "" )
		texto+=" * Debe indicar su nombre(s).\n\n";

	if ( document.getElementById("formularioContacto").apellidosCliente.value == "" )
		texto+=" * Debe indicar su apellido(s).\n\n";

	if(document.getElementById("formularioContacto").email.value!="")
		if(!validarEmail(document.getElementById("formularioContacto").email.value))
				texto+=" * Formato de E-mail incorrecto.\n\n";

	if ( document.getElementById("formularioContacto").fonoCliente.value == "" )
	{	if ( document.getElementById("formularioContacto").movilCliente.value == "" )
			texto+=" * Debe indicar un teléfono o un móvil.\n\n"		
		else		
		{	if ( document.getElementById("formularioContacto").movilCliente.value.length != 9 )
				texto+=" * El móvil debe tener 9 números (ahora tiene="+ document.getElementById("formularioContacto").movilCliente.value.length +").\n";
			else	
				if (!validarNumero(document.getElementById("formularioContacto").movilCliente.value))
					texto+=" * El número de móvil debe ser numérico.\n\n";
		}
	}
	else
	{	if ( document.getElementById("formularioContacto").fonoCliente.value.length != 9 )
				texto+=" * El teléfono debe tener 9 números (ahora tiene="+ document.getElementById("formularioContacto").fonoCliente.value.length +").\n";
		else	
			if (!validarNumero(document.getElementById("formularioContacto").fonoCliente.value))
				texto+=" * El teléfono debe ser numérico.\n\n";
	}

	if ( document.getElementById("formularioContacto").horarioContactoCliente.value == "-1" )
		texto+=" * Debe indicar el horario de contactar al Cliente.\n\n";
	

	if (document.getElementById("formularioContacto").aceptaCondiciones.checked==true){
		;
	}else
		texto+=" * Debe indicar su conformidad con las condiciones.\n\n";

//Envio s todo OK
	if ( texto != "" )
    	 alert("Se han encontrado las siguientes omisiones:\n\n"+texto);
	else
		 document.formularioContacto.submit();
}


function validaMicroMicroFormCasa()
{	var texto;
	texto = "";

	var cadenaEnvio;
	cadenaEnvio = "";
	//1	
	if ( document.getElementById("formularioContacto").nombresCliente.value == "" )
		texto+=" * Debe indicar su nombre/apellido(s).\n\n";

	if(document.getElementById("formularioContacto").email.value!="")
		if(!validarEmail(document.getElementById("formularioContacto").email.value))
				texto+=" * Formato de E-mail incorrecto.\n\n";

	if ( document.getElementById("formularioContacto").fonoCliente.value == "" )
		texto+=" * Debe indicar un teléfono y/o un móvil.\n\n";		
	else
	{	if ( document.getElementById("formularioContacto").fonoCliente.value.length != 9 )
				texto+=" * El teléfono y/o móvil debe tener 9 números (ahora tiene="+ document.getElementById("formularioContacto").fonoCliente.value.length +").\n";
		else	
			if (!validarNumero(document.getElementById("formularioContacto").fonoCliente.value))
				texto+=" * El teléfono y/o móvildebe ser numérico.\n\n";
	}

	if ( document.getElementById("formularioContacto").horarioContactoCliente.value == "-1" )
		texto+=" * Debe indicar el horario de contactar al Cliente.\n\n";
	
//Envio s todo OK
	if ( texto != "" )
    	 alert("Se han encontrado las siguientes omisiones:\n\n"+texto);
	else
		 document.formularioContacto.submit();
}

