//VALIDA EL CODIGO POSTAL CON EL CODIGO DE LA PROVINCIA
function cpValido(cp, codigo)
{
	var cod = cp.substring(0, 2);
	if (cod == codigo)
	{return true;}
	else
	{return false;}
}

//ELIMINA ESPACIOS DEL DNI Y OTROS CARACTERES
function formateaDNI(dni)
{
	var dniFormateado = "";
	dniFormateado = dni.replace("-", "");
	dniFormateado = dniFormateado.replace("_", "");
	dniFormateado = dniFormateado.replace(" ", "");
	dniFormateado = dniFormateado.replace(".", "");
	dniFormateado = dniFormateado.replace("*", "");
	dniFormateado = dniFormateado.toUpperCase();
	return dniFormateado;
}

//COMPRUEBA QUE EL DNI INTRODUCIDO SEA VALIDO
function dniValido(dni)
{
	var numero = "";
	var let = "";
	var letra = "RWAGMYFPDXBNJZSQVHLCKET";

	dniFormateado = formateaDNI(dni);
	if ((dniFormateado == "000000000T")||(dniFormateado == "111111111T")||(dniFormateado == "222222222J"))
	{
		return false;
	}
	let2 = dniFormateado.substr(0,1);
	if (let2 == "X")
	{
		numero = dniFormateado.substr(1, dniFormateado.length-2);
	}
	else if (let2 == "Y")
	{
		numero = "1"+dniFormateado.substr(1, dniFormateado.length-2);
	}
	else if (let2 == "Z")
	{
		numero = "2"+dniFormateado.substr(1, dniFormateado.length-2);
	}
	else
	{
		numero = dniFormateado.substr(0, dniFormateado.length-1);
	}
	let = dniFormateado.substr(dniFormateado.length-1, 1);
	numero = numero % 23;
	if (numero == 0) numero = 23;
	letra = letra.charAt(numero-1);
	if (letra != let)
	{
		return false;
	}
	else
		return true;
}

//COMPRUEBA QUE SEA UN EMAIL V&aacute;LIDO
function emailValido(address)
{
	if (address.indexOf('@') < 3)
		return false;
	var name = address.substring(0, address.indexOf('@'));
	var domain = address.substring(address.indexOf('@') + 1);
	var ext = address.substring(address.lastIndexOf('.') + 1);
	var serv = domain.substring(0,domain.indexOf('.'));
	
	if (name.indexOf('(') != -1 || name.indexOf(')') != -1 || name.indexOf('<') != -1 || name.indexOf('>') != -1 || name.indexOf(',') != -1 || name.indexOf(';') != -1 || name.indexOf(':') != -1 || name.indexOf('\\') != -1 || name.indexOf('"') != -1 || name.indexOf('[') != -1 || name.indexOf(']') != -1 || name.indexOf(' ') != -1)
		return false;
	if (domain == 'spam.la' || domain == 'mailinator.com' || domain == "" || domain.indexOf('(') != -1 || domain.indexOf(')') != -1 || domain.indexOf('<') != -1 || domain.indexOf('>') != -1 || domain.indexOf(',') != -1 || domain.indexOf('.') == -1 || domain.indexOf(';') != -1 || domain.indexOf(':') != -1 || domain.indexOf('\\') != -1 || domain.indexOf('"') != -1 || domain.indexOf('[') != -1 || domain.indexOf(']') != -1 || domain.indexOf(' ') != -1)
		return false;
	if (serv == "" || serv.length == 1)
		return false;
	
	if (ext == "" || ext.length == 1)
		return false;
	
	return true;
}

//COMPRUEBA QUE EL VALOR ENVIADO NO CONTENGA CARACTERES NO VALIDOS
function contieneCaracterInvalido(cadena)
{
	var caracteres = new Array("_","!","?","1","2","3","4","5","6","7","8","9","0", ",",";", "+", "=","/","¡"); 
	var encontrado = false;
	for (i = 0; i < caracteres.length; i++)
	{
		if(cadena.indexOf(caracteres[i]) != -1)
		{
			encontrado = true;
		}
	}
	return encontrado;
}		

//VALIDA FORMULARIO CON DATOS CONTACTO DEL FORMULARIO GARANTÍAS CLIENTES (PASO 1)
function validar_garantia_contacto()
{
	error = false;
	if (document.getElementById('tipoCliente').value == 4)
	{		
		if (document.getElementById('empresa').value == "")
		{
			document.getElementById('empresa').style.border = "1px solid red";
			document.getElementById('empresa').style.background = "white";
			error = true;
		}
	}
	if (document.getElementById('tratamiento').value == "")
	{
		document.getElementById('tratamiento').style.border = "1px solid red";
		document.getElementById('tratamiento').style.background = "white";
		error = true;
	}
	if (document.getElementById('nombre').value == "")
	{
		document.getElementById('nombre').style.border = "1px solid red";
		document.getElementById('nombre').style.background = "white";
		error = true;
	}
	else
	{
		if (contieneCaracterInvalido(document.getElementById('nombre').value))
		{
			document.getElementById('nombre').style.border = "1px solid red";
			document.getElementById('nombre').style.background = "white";
			error = true;
		}
	}
	if (document.getElementById('apellidos').value == "")
	{
		document.getElementById('apellidos').style.border = "1px solid red";
		document.getElementById('apellidos').style.background = "white";
		error = true;
	}
	else
	{
		if (contieneCaracterInvalido(document.getElementById('apellidos').value))
		{
			document.getElementById('apellidos').style.border = "1px solid red";
			document.getElementById('apellidos').style.background = "white";
			error = true;
		}
	}

	if (document.getElementById('tipoVia').value == "")
	{
		document.getElementById('tipoVia').style.border = "1px solid red";
		document.getElementById('tipoVia').style.background = "white";
		error = true;
	}
	if (document.getElementById('direccion').value == "")
	{
		document.getElementById('direccion').style.border = "1px solid red";
		document.getElementById('direccion').style.background = "white";
		error = true;
	}
	if (document.getElementById('cp').value == "")
	{
		document.getElementById('cp').style.border = "1px solid red";
		document.getElementById('cp').style.background = "white";
		error = true;
	}
	else
	{
		if (isNaN(document.getElementById('cp').value))
		{
			document.getElementById('cp').style.border = "1px solid red";
			document.getElementById('cp').style.background = "white";
			error = true;
		}
		else
		{
			if ((document.getElementById('cp').value > 53000)||(document.getElementById('cp').value < 1000))
			{
				document.getElementById('cp').style.border = "1px solid red";
				document.getElementById('cp').style.background = "white";
				error = true;
			}
		}
	}
	if (document.getElementById('ciudad').value == "")
	{
		document.getElementById('ciudad').style.border = "1px solid red";
		document.getElementById('ciudad').style.background = "white";
		error = true;
	}
	if (document.getElementById('provincia').value == "")
	{
		document.getElementById('provincia').style.border = "1px solid red";
		document.getElementById('provincia').style.background = "white";
		error = true;
	}
	if ((document.getElementById('telefono').value == "")&&(document.getElementById('movil').value == ""))
	{
		document.getElementById('telefono').style.border = "1px solid red";
		document.getElementById('telefono').style.background = "white";
		document.getElementById('movil').style.border = "1px solid red";
		document.getElementById('movil').style.background = "white";
		error = true;
	}
	else
	{
		inputInactivo('telefono');
		inputInactivo('movil');
	}

	if (error)
	{
		document.getElementById('errorDiv').innerHTML = "Por favor, revise los campos señalados en rojo, no pueden estar vacíos o no son correctos."
	}
	else
	{
		document.getElementById('errorDiv').innerHTML = "Enviando datos..."
		document.getElementById('formGarantia1').submit();
	}
}
		
//VALIDA FORMULARIO CON DATOS ENTREGA DEL FORMULARIO GARANTÍAS CLIENTES (PASO2)
function validar_garantia_entrega()
{	
	error = false;
	if(document.getElementById('mismaDireccionNo').checked)
	{	
		if (document.getElementById('direccionEntrega').value == "")
		{
			document.getElementById('direccionEntrega').style.border = "1px solid red";
			document.getElementById('direccionEntrega').style.background = "white";
			error = true;
		}
		if (document.getElementById('cpEntrega').value == "")
		{
			document.getElementById('cpEntrega').style.border = "1px solid red";
			document.getElementById('cpEntrega').style.background = "white";
			error = true;
		}
		else
		{
			if ((isNaN(document.getElementById('cpEntrega').value))||(document.getElementById('cpEntrega').value > 52999) || ( document.getElementById('cpEntrega').value < 1000))
			{
				document.getElementById('cpEntrega').style.border = "1px solid red";
				document.getElementById('cpEntrega').style.background = "white";
				error = true;

			}
		}	
		if (document.getElementById('ciudadEntrega').value == "")
		{
			document.getElementById('ciudadEntrega').style.border = "1px solid red";
			document.getElementById('ciudadEntrega').style.background = "white";
			error = true;
		}
		if (document.getElementById('provinciaEntrega').value == "")
		{
			document.getElementById('provinciaEntrega').style.border = "1px solid red";
			document.getElementById('provinciaEntrega').style.background = "white";
			error = true;
		}
	}
	if (document.getElementById('horarioEntrega').value == "")
	{
		document.getElementById('horarioEntrega').style.border = "1px solid red";
		document.getElementById('horarioEntrega').style.background = "white";
		error = true;	
	}

	if (document.getElementById('contactoEntrega').value == "")
	{
		document.getElementById('contactoEntrega').style.border = "1px solid red";
		document.getElementById('contactoEntrega').style.background = "white";
		error = true;	
	}
	if (document.getElementById('telefonoEntrega').value == "")
	{
		document.getElementById('telefonoEntrega').style.border = "1px solid red";
		document.getElementById('telefonoEntrega').style.background = "white";
		error = true;
	}
	else
	{
		if ((isNaN(document.getElementById('telefonoEntrega').value))||(document.getElementById('telefonoEntrega').value > 9999999999999) || ( document.getElementById('telefonoEntrega').value < 60000000))
		{
			document.getElementById('telefonoEntrega').style.border = "1px solid red";
			document.getElementById('telefonoEntrega').style.background = "white";
			error = true;
		}
	}


	if (error)
	{
		document.getElementById('errorDiv').innerHTML = "Por favor, revise los campos señalados en rojo, no pueden estar vacíos o no son correctos."
	}
	else
	{
		document.getElementById('errorDiv').innerHTML = "Enviando datos..."
		document.getElementById('formGarantia2').submit();
	}

}

//VALIDA FORMULARIO CON DATOS DE PRODUCTO DEL FORMULARIO GARANTÍAS CLIENTES (PASO 3)
function validar_garantia_productos()
{
	error = false; 
	error1 = false; 
	if (document.getElementById('nombre').value == "")
	{
		document.getElementById('nombre').style.border = "1px solid red";
		document.getElementById('nombre').style.background = "white";
		error = true;
	}
	else
	{
		if (contieneCaracterInvalido(document.getElementById('nombre').value))
		{
			document.getElementById('nombre').style.border = "1px solid red";
			document.getElementById('nombre').style.background = "white";
			error = true;
		}
	}
	if (document.getElementById('apellidos').value == "")
	{
		document.getElementById('apellidos').style.border = "1px solid red";
		document.getElementById('apellidos').style.background = "white";
		error = true;
	}
	else
	{
		if (contieneCaracterInvalido(document.getElementById('apellidos').value))
		{
			document.getElementById('apellidos').style.border = "1px solid red";
			document.getElementById('apellidos').style.background = "white";
			error = true;
		}
	}
	if (document.getElementById('dni').value == "")
	{
		document.getElementById('dni').style.border = "1px solid red";
		document.getElementById('dni').style.background = "white";
		error = true;
	}
	else
	{
		if (!dniValido(document.getElementById('dni').value))
		{
			document.getElementById('dni').style.border = "1px solid red";
			document.getElementById('dni').style.background = "white";
			error = true;
		}
	}
	if (document.getElementById('condiciones').checked == false)
	{
		document.getElementById('condiciones').style.border = "1px solid red";
		document.getElementById('condiciones').style.background = "white";
		error1 = true;
	}

	if (error || error1)
	{
		document.getElementById('errorDiv').innerHTML = "Por favor, revise los campos señalados en rojo, no pueden estar vacíos o no son correctos y recuerde aceptar las condiciones."
	}
	else
	{
		document.getElementById('errorDiv').innerHTML = "Enviando datos..."
		document.getElementById('formGarantia3').submit();
	}


}



function validar_garantia_cambio()
{
	error = false;
	if ((!document.getElementById('conformeSi').checked)&&(!document.getElementById('conformeNo').checked))
	{
		error = true;
	}
	if ((document.getElementById('conformeNo').checked) && (document.getElementById('observaciones').value == ""))
	{
		error = true;
		document.getElementById('observaciones').style.border = "1px solid red";
		document.getElementById('observaciones').style.background = "white";
	}
	if (error)
	{
		document.getElementById('errorDiv').innerHTML = "Por favor, si no está conforme con el cambio necesitamos que rellene el campo de observaciones."
	}
	else
	{
		document.getElementById('errorDiv').innerHTML = "Enviando datos..."
		document.getElementById('formGarantia4').submit();
	}

}
