function trim(str) {
     str = str.replace( /^\s+/g, "" ); // strip leading
     str = str.replace( /\s+$/g, "" ); // strip trailing
     return str;
}
function cargarCat(cat){
	document.getElementById("cat").value=cat;
	document.Form1.action='../index.aspx';
	document.Form1.submit();
	//document.location.href="index.aspx?cat=" + cat;
}
function validarCliente(){
	var email=trim(document.getElementById("email").value);
	var password=trim(document.getElementById("password").value);
	var mensaje=document.getElementById("mensaje");
	if (email==""){
		mensaje.innerHTML="Debe introducir su CORREO ELECTRONICO";
		document.getElementById("email").focus();
		document.getElementById("email").style.border="1px solid Red";
		document.getElementById("password").style.border="1px solid #999";
		return false;
	}
	if (!valida_correo(email)){
		mensaje.innerHTML="El CORREO ELECTRONICO no tiene un formato correcto";
		document.getElementById("email").focus();
		document.getElementById("email").select();
		document.getElementById("email").style.border="1px solid Red";
		document.getElementById("password").style.border="1px solid #999";
		return false;
	}
	if (password==""){
		mensaje.innerHTML="Debe introducir una CONTRASE" + String.fromCharCode(241).toUpperCase() + "A";
		document.getElementById("password").focus();
		document.getElementById("password").style.border="1px solid Red";
		document.getElementById("email").style.border="1px solid #999";
		return false;
	}
	document.Form1.submit();
}

function valida_correo(inputvalue){	
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)){         
		return true;
    }else{   
		return false;
    }
}
function reenviarPass(){
	if (trim(document.getElementById('email').value)==''){
		document.getElementById('mensaje').innerHTML="Debe rellenar el campo 'correo electronico'";
		document.getElementById('email').style.border='solid 1px red';
		document.getElementById("email").focus();
		return false;
	}
	else{
		if (!valida_correo(trim(document.getElementById('email').value))){
			mensaje.innerHTML="El CORREO ELECTRONICO no tiene un formato correcto";
			document.getElementById('email').style.border='solid 1px red';
			document.getElementById("email").focus();
			document.getElementById("email").select();
			return false;
		}
	}
	document.Form1.submit();
}
function tryAgain(){
	document.location.href="comprando.aspx";
}
function iraPaso2_3(){
	if (validarPaso2()){
		document.Form1.action='comprando.aspx';
		document.getElementById("paso").value="2-3";
		document.Form1.submit();
	}
}
function validarPaso2(){
	var numCajas=document.getElementById("numCajas");
	if (trim(numCajas.value)==""){
		alert("ATENCION: debe introducir el NUMERO DE CAJAS que desea enviar");
		numCajas.focus();
		numCajas.style.border="1px solid #999";
		return false;
	}
	return true;
}
function calcularPrecio(cajaNumCajas){
	var numCajas=new String(document.getElementById("numCajas").value);
	if (numCajas=="0"){
		document.getElementById("numCajas").value="";
		numCajas="";
	}
	if (numCajas!=""){
		if (document.getElementById("recogeCasi").value=="RECOGECASI"){
			if (document.getElementById("PrecioResult")){
				document.getElementById("PrecioResult").innerHTML=(document.getElementById("numCajas").value * document.getElementById("costeCaja").value) + " &#8364";
			}
			document.getElementById("PrecioResult1").innerHTML=(document.getElementById("numCajas").value * document.getElementById("costeCaja").value) + " &#8364";
		}
		if (document.getElementById("recogeCasi").value=="NORECOGECASI"){
			if (document.getElementById("PrecioResult")){
				document.getElementById("PrecioResult").innerHTML=((document.getElementById("numCajas").value * document.getElementById("costeCaja").value)) + " &#8364";
			}
			document.getElementById("PrecioResult1").innerHTML=(document.getElementById("numCajas").value * document.getElementById("costeCaja").value) + " &#8364";
		}		
	}
	else{
		if (document.getElementById("PrecioResult")){
			document.getElementById("PrecioResult").innerHTML="";
		}
		document.getElementById("PrecioResult1").innerHTML="";
	}
}
function isNumberKey(evt){
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57)){
            return false;
         }
         return true;
}
function iraPaso2_1(){
		document.Form1.action='comprando.aspx';
		document.getElementById("paso").value="2-1";
		document.Form1.submit();
}

function iraPaso1_2(){
	if (document.getElementById("recogerCasi").checked==false){
		if (validarPaso1()){
			document.Form1.action='comprando.aspx';
			document.getElementById("paso").value="1-2";
			document.Form1.submit();
		}
		else
		{
			return false;
		}
	}
	else{
		document.Form1.action='comprando.aspx';
		document.getElementById("paso").value="1-2";
		document.Form1.submit();
	}
	
}

function validarPaso1(){
	var nombre=document.getElementById("nombre");
	if (trim(nombre.value)==""){
		alert("ATENCION: debe rellenar el campo NOMBRE");
		document.getElementById("nombre").style.border="1px solid #999";
		nombre.focus();
		return false;
	}
	var provincia=document.getElementById("provincia")
	if (provincia.options[provincia.selectedIndex].value=="0"){
		alert("ATENCION: debe seleccionar una PROVINCIA");
		provincia.focus();
		provincia.style.border="1px solid #999";
		return false;
	}
	var codPostal=document.getElementById("codPostal");
	if (trim(codPostal.value)==""){
		alert("ATENCION: debe rellenar el campo CODIGO POSTAL");
		codPostal.focus();
		codPostal.style.border="1px solid #999";
		return false;
	}
	if (trim(codPostal.value).length!=5){
		alert("ATENCION: debe rellenar correctamente los 5 DIGITOS del CODIGO POSTAL");
		codPostal.focus();
		codPostal.select();
		codPostal.style.border="1px solid #999";
		return false;
	}
	var municipio=document.getElementById("municipio");
	if (trim(municipio.value)==""){
		alert("ATENCION: debe rellenar el campo MUNICIPIO");
		municipio.focus();
		municipio.style.border="1px solid #999";
		return false;
	}
	var nomVia=document.getElementById("nomVia");
	if (trim(nomVia.value)==""){
		alert("ATENCION: debe rellenar el campo NOMBRE DE LA VIA");
		nomVia.focus();
		nomVia.style.border="1px solid #999";
		return false;
	}
	var numPortal=document.getElementById("numPortal");
	var sinNum=document.getElementById("sinNum");
	if ((trim(numPortal.value)=="")&&(sinNum.checked==false)){
		alert("ATENCION: debe rellenar el campo NUMERO o marcar la casilla S/N (SIN NUMERO)");
		numPortal.focus();
		numPortal.style.border="1px solid #999";
		return false;
	}
	var telf1=document.getElementById("telf1");
	if (trim(telf1.value)==""){
		alert("ATENCION: debe rellenar el campo TELEFONO 1");
		telf1.focus();
		telf1.style.border="1px solid #999";
		return false;
	}
	codPostal.value=trim(codPostal.value);
	strCodPostal=codPostal.value;
	if (strCodPostal.substring(2,0)!=provincia.options[provincia.selectedIndex].value){
		alert("ATENCION: el CODIGO POSTAL no se corresponde con ninguna localidad de la PROVINCIA seleccionada");
		codPostal.focus();
		codPostal.select();
		codPostal.style.border="1px solid #999";
		return false;
	}
	var strMensaje=document.getElementById("areaMensaje").value;
	if (strMensaje.length>250){
		document.getElementById("areaMensaje").value=strMensaje.substring(250,0);
	}
	return true;
}

function conOsinNum(){
	if (document.getElementById("sinNum").checked){
		document.getElementById("numPortal").disabled=true;
	}
	else{
		document.getElementById("numPortal").disabled=false;
	}
}
function limita_mensaje(campo,max)
{
	if(campo.value.length>=max){campo.value=campo.value.substring(0,max);}
	var texto=campo.value;
	var numCharAreaTexto=texto.length;
	if (numCharAreaTexto!=0){
		document.getElementById("leyendaMensaje").innerHTML="Observaciones (" + numCharAreaTexto + "/" + max + " caracteres MAXIMO; opcional)";
	}
	else{
		document.getElementById("leyendaMensaje").innerHTML="Observaciones (" + max + " caracteres MAXIMO; opcional)";
	}
}

function iraPaso3_2(){
	document.Form1.action='comprando.aspx';
	document.getElementById("paso").value="3-2";
	document.Form1.submit();	
}
function iraPaso3_4(){
	if (validarPaso3()){
		document.Form1.action='comprando.aspx';
		document.getElementById("paso").value="3-4";
		document.Form1.submit();
	}
	else{
		return false;
	}
	
}
function validarPaso3(){
	var f_nombre=document.getElementById("f_nombre");
	if (trim(f_nombre.value)==""){
		alert("ATENCION: debe rellenar el campo NOMBRE/RAZON SOCIAL");
		document.getElementById("f_nombre").style.border="1px solid #999";
		f_nombre.focus();
		return false;
	}
	var f_nif_cif=document.getElementById("f_nif_cif");
	if (trim(f_nif_cif.value)==""){
		alert("ATENCION: debe rellenar el campo NIF/CIF");
		f_nif_cif.focus();
		f_nif_cif.style.border="1px solid #999";
		return false;
	}
	var f_direccion=document.getElementById("f_direccion");
	if (trim(f_direccion.value)==""){
		alert("ATENCION: debe rellenar el campo DIRECCION");
		f_direccion.focus();
		f_direccion.style.border="1px solid #999";
		return false;
	}
	var f_provincia=document.getElementById("f_provincia")
	if (f_provincia.options[f_provincia.selectedIndex].value=="0"){
		alert("ATENCION: debe seleccionar una PROVINCIA");
		f_provincia.focus();
		f_provincia.style.border="1px solid #999";
		return false;
	}
	var f_codPostal=document.getElementById("f_codPostal");
	if (trim(f_codPostal.value)==""){
		alert("ATENCION: debe rellenar el campo CODIGO POSTAL");
		f_codPostal.focus();
		f_codPostal.style.border="1px solid #999";
		return false;
	}
	if (trim(f_codPostal.value).length!=5){
		alert("ATENCION: debe rellenar correctamente los 5 DIGITOS del CODIGO POSTAL");
		f_codPostal.focus();
		f_codPostal.select();
		f_codPostal.style.border="1px solid #999";
		return false;
	}
	var f_poblacion=document.getElementById("f_poblacion");
	if (trim(f_poblacion.value)==""){
		alert("ATENCION: debe rellenar el campo POBLACION");
		f_poblacion.focus();
		f_poblacion.style.border="1px solid #999";
		return false;
	}
	
	f_codPostal.value=trim(f_codPostal.value);
	strCodPostal=f_codPostal.value;
	if (strCodPostal.substring(2,0)!=f_provincia.options[f_provincia.selectedIndex].value){
		alert("ATENCION: el CODIGO POSTAL no se corresponde con ninguna localidad de la PROVINCIA seleccionada");
		f_codPostal.focus();
		f_codPostal.select();
		f_codPostal.style.border="1px solid #999";
		return false;
	}
	return true;
}
function confirmReembolso(){
	if (document.getElementById("checkCondiciones").checked){
		document.Form1.action='comprando.aspx';
		document.getElementById("paso").value="confirmando_reembolso";
		document.Form1.submit();
	}
	else
	{
		alert("ATENCION: para confirmar su pedido, debe aceptar las CONDICIONES DE COMPRA");
		document.getElementById("checkCondiciones").focus();
	}
}
function confirmTarjeta(){
	if (document.getElementById("checkCondiciones").checked){
		document.Form1.action='pagandoTarjeta.aspx';
		document.getElementById("paso").value="confirmando_tarjeta";
		document.Form1.submit();
	}
	else
	{
		alert("ATENCION: para confirmar su pedido, debe aceptar las CONDICIONES DE COMPRA");
		document.getElementById("checkCondiciones").focus();
	}
}

function iraPaso4_3(){
	document.Form1.action='comprando.aspx';
	document.getElementById("paso").value="4-3";
	document.Form1.submit();	

}
function nuevoCliente(){
	cargarCat(3);
}
function abrirCondiciones(){
	window.open('http://www.rafdecasi.com/condicionesCompra.aspx');
}
function recogerEnCasi(casilla){
	if (casilla.checked){
		document.getElementById("nombre").value="";
		document.getElementById("provincia").selectedIndex=0;
		document.getElementById("codPostal").value="";
		document.getElementById("municipio").value="";
		document.getElementById("urbPed").value="";
		document.getElementById("nomVia").value="";
		document.getElementById("numPortal").value="";
		document.getElementById("sinNum").checked=false;
		document.getElementById("pep").value="";
		document.getElementById("telf1").value="";
		document.getElementById("telf2").value="";
		document.getElementById("hor_entrega").selectedIndex=0;
		document.getElementById("areaMensaje").value="";

		document.getElementById("nombre").disabled=true;
		document.getElementById("provincia").disabled=true;
		document.getElementById("codPostal").disabled=true;
		document.getElementById("municipio").disabled=true;
		document.getElementById("urbPed").disabled=true;
		document.getElementById("nomVia").disabled=true;
		document.getElementById("numPortal").disabled=true;
		document.getElementById("sinNum").disabled=true;
		document.getElementById("pep").disabled=true;
		document.getElementById("telf1").disabled=true;
		document.getElementById("telf2").disabled=true;
		document.getElementById("hor_entrega").disabled=true;
		document.getElementById("areaMensaje").disabled=true;
		
	}
	else{
		document.getElementById("nombre").disabled=false;
		document.getElementById("provincia").disabled=false;
		document.getElementById("codPostal").disabled=false;
		document.getElementById("municipio").disabled=false;
		document.getElementById("urbPed").disabled=false;
		document.getElementById("nomVia").disabled=false;
		document.getElementById("numPortal").disabled=false;
		document.getElementById("sinNum").disabled=false;
		document.getElementById("pep").disabled=false;
		document.getElementById("telf1").disabled=false;
		document.getElementById("telf2").disabled=false;
		document.getElementById("hor_entrega").disabled=false;
		document.getElementById("areaMensaje").disabled=false;
	}
}
function descargarFactura(){
	document.Form1.target='_blank';
	document.Form1.action='facturaRaf.aspx';
	document.Form1.submit();
}
function irZonaCompra(){
	document.Form1.action='http://www.rafdecasi.com/zonaCompra/comprando.aspx';
	document.Form1.submit();
}
