function enviarClave(){
	var error = "";
	var email = document.us_fr.Email.value;
	if(email!=""){
		if(!esEmail(email)){error+="- El <b>Email</b> debe ser una dirección correcta.<br/>";}
	}else{
		error+="- <b>Email</b> es requerido.<br/>";
	}
	if(error==""){
		document.us_fr.submit();
	}else{
		getMensaje_720(error);	
	}
}
function modificarClave(){
	var error = "";
	var email = document.us_fr.Email.value;
	var clave = document.us_fr.Clave.value;
	var clave2 = document.us_fr.Confirmacion.value;
	
	if(email!=""){
		if(!esEmail(email)){error+="- El <b>Email</b> debe ser una dirección correcta.<br/>";}
	}else{
		error+="- <b>Email</b> es requerido.<br/>";
	}
	if(clave==""){
		error+="- <b>Clave</b> es requerida.<br/>";
	}
	if(clave2==""){
		error+="- <b>Repetir clave</b> es requerida.<br/>";
	}
	if(clave!=""){
		if(clave.length<6 || clave.length>20){
			error+="- La <b>Clave</b> debe contener entre 6 y 20 caracteres.<br/>";
		}else{
			if(clave2!=clave){error+="- La <b>Clave</b> y la <b>Repetición</b> deben ser iguales.<br/>";}
		}
	}
	if(error==""){
		var cl_MD5 = calcMD5(document.us_fr.Clave.value);	
		document.us_fr.Cl.value = cl_MD5;
		document.us_fr.Clave.value = asterisco(document.us_fr.Clave.value);
		document.us_fr.Confirmacion.value = asterisco(document.us_fr.Clave.value);
		document.us_fr.submit();
	}else{
		getMensaje_720(error);	
	}
}

/// LOGIN /////////////////////////////////////////////////////////////////////////

function enviarLogin(){
	var error = "";
	var email = document.us_fr.Email.value;
	var clave = document.us_fr.Clave.value;
	if(email!=""){
		if(!esEmail(email)){error+="- El <b>Email</b> debe ser una dirección correcta.<br/>";}
	}else{
		error+="- <b>Email</b> es requerido.<br/>";
	}
	if(clave==""){
		error+="- <b>Clave</b> es requerida.<br/>";
	}else{
		if(clave.length<6 || clave.length>20){
			error+="- La <b>Clave</b> debe contener entre 6 y 20 caracteres.<br/>";
		}
	}
	if(error==""){
		var cl_MD5 = calcMD5(document.us_fr.Clave.value);	
		document.us_fr.Cl.value = cl_MD5;
		document.us_fr.Clave.value = asterisco(document.us_fr.Clave.value);
		//document.us_fr.submit();
		return true;
	}else{
		getMensaje_720(error);	
		return false;
	}
}
/// USUARIO RETAIL ////////////////////////////////////////////////////////////////////////
function esCedula(cedula) {
	//var re = /^(([0-9]){1,3}([.])+)?([0-9]){3}([.]){1}([0-9]){3}([-]){1}([0-9]){1}$/;
	var re = /^([0-9]){6,7}([-]){1}([0-9]){1}$/;
    if (!cedula.match(re)) {
        return false;
    } else {
        return true;
    }
}

function RetailP1(){
	var error="";
	error+=esVacio(document.us_fr.Nombre.value,"Nombre");
	error+=esVacio(document.us_fr.Apellido.value,"Apellido");
	error+=esVacio(document.us_fr.Telefono.value,"Telefono");
	var cedula = document.us_fr.NumeroDocumento.value+"-"+document.us_fr.NumeroDocumento_dig.value;
	if(document.us_fr.NumeroDocumento.value+document.us_fr.NumeroDocumento_dig.value!=""){
		if(!esCedula(cedula)){
			error+="- El <b>Documento de identidad</b> no es correcto. Debes ingresar el número sin puntos ni guión, más el dígito de control.<br/>";	
		}
	}
	if(document.us_fr.dia.value+document.us_fr.mes.value+document.us_fr.anio.value=="00000000"){
		error+="- Selecciona la <b>Fecha de nacimiento</b>.<br/>";	
	}
	error+=esVacio(document.us_fr.Calle.value,"Calle");
	error+=esVacio(document.us_fr.NumeroPuerta.value,"Número de puerta");
	error+=esVacio(document.us_fr.EntreCalle.value,"Entre calles");
	var idDep = document.us_fr.idDepartamento;
	if(idDep.length>1 && idDep.value==0){
		error+="- Selecciona el <b>Departamento</b>.<br/>";	
	}
	/*var idLoc = document.us_fr.idLocalidad;
	if(idLoc.length>1 && idLoc.value==0){
		error+="- Selecciona la <b>Localidad</b>.<br/>";	
	}*/
	return error;
}

function RetailP2(){
	var error="";
	error+=esVacio(document.us_fr.CalleEnv.value,"Calle");
	error+=esVacio(document.us_fr.NumeroPuertaEnv.value,"Número de puerta");
	error+=esVacio(document.us_fr.EntreCalleEnv.value,"Entre calles");
	if(document.us_fr.idPaisEnv.value==0){
		error+="- Selecciona el <b>País</b>.<br/>";	
	}
	var idDep = document.us_fr.idDepartamentoEnv;
	if(idDep.length>1 && idDep.value==0){
		error+="- Selecciona el <b>Departamento</b>.<br/>";	
	}
	/*var idLoc = document.us_fr.idLocalidadEnv;
	if(idLoc.length>1 && idLoc.value==0){
		error+="- Selecciona la <b>Localidad</b>.<br/>";	
	}*/
	error+=esVacio(document.us_fr.CodigoPostalEnv.value,"Código Postal");
	var entrega1 = document.us_fr.Entrega1.value.split(":");
	var entrega2 = document.us_fr.Entrega2.value.split(":");
	if(entrega1[0]>entrega2[0]){
		error+="- El <b>Horario de entrega</b> no es correcto.<br/>";	
	}
	if(document.us_fr.Hijos.value!=""){
		if(isNaN(document.us_fr.Hijos.value)){
			error+="- <b>Cantidad de hijos</b> debe ser un valor numérico.<br/>";		 
		}else{
			if(document.us_fr.Hijos.value<0){
				error+="- <b>Cantidad de hijos</b> debe ser un número positivo.<br/>";		
			}
		}
	}
	return error;
}
function RetailP3(nuevo){
	var error="";
	var email = document.us_fr.Email.value;
	var clave = document.us_fr.Clave.value;
	var clave2 = document.us_fr.Confirmacion.value;
	if(email!=""){
		if(!esEmail(email)){error+="- El <b>Email</b> debe ser una dirección correcta.<br/>";}
	}else{
		error+="- <b>Email</b> es requerido.<br/>";
	}
	
	if(nuevo==1){
		if(clave==""){
			error+="- <b>Clave</b> es requerida.<br/>";
		}else{
			if(clave.length<6 || clave.length>20){
				error+="- La <b>Clave</b> debe contener entre 6 y 20 caracteres.<br/>";
			}else{
				if(clave2==""){
					error+="- <b>Repetir clave</b> es requerido.<br/>";
				}else{
					if(clave2!=clave){error+="- La <b>Clave</b> y la <b>Repetición</b> deben ser iguales.<br/>";}
				}	
			}
		}
	}else{
		if(clave!=""){
			if(clave.length<6 || clave.length>20){
				error+="- La <b>Clave</b> debe contener entre 6 y 20 caracteres.<br/>";
			}else{
				if(clave2==""){
					error+="- <b>Repetir clave</b> es requerido.<br/>";
				}else{
					if(clave2!=clave){error+="- La <b>Clave</b> y la <b>Repetición</b> deben ser iguales.<br/>";}
				}	
			}
		}
	}
	error+=esVacio(document.us_fr.Codigo.value,"Código de Seguridad");
	return error;
}

function checkFormatoFile(archivo){
	var er="";
	var arA =archivo.split(".");
	var ext = arA[(arA.length)-1].toLowerCase();
	if(archivo==""){
		return true;
	}else{
		if(ext!="doc" & ext!="txt" & ext!="pdf" & ext!="docx" & ext!="rtf" & ext!="jpg" & ext!="zip" & ext!="rar"){
			return false
		}else{
			return true;	
		}
	}
}
function RetailP4(){
	
	var error="";
	var str_clubes = "";
	var checks = $$("paso4").getElementsByTagName("input");
	var id_club = 0;
	var titulo_club = "";
	
	// Chequeo Cédula
	
	
	for(var i = 0;i<checks.length;i++){
		var obj = checks[i];
		var name = obj.id;
		var ar_name = name.split("_");
		if(ar_name[0]=="checkclub"){
			if(obj.checked){
				str_clubes+=ar_name[1]+"_"+ar_name[2]+",";
				var obj_file = null;
				id_club = ar_name[1];
				titulo_club = ar_name[3];
				if($$("fileclub_"+id_club+"_0") && obj.disabled == false){
					obj_file=$$("fileclub_"+id_club+"_0");
					if(!checkFormatoFile(obj_file.value)){
						error+="- El Comprobante (Archivo) debe ser un documento de texto, PDF, jpg, zip o rar en el club: <b>"+titulo_club+"</b>.<br>";
					}
				}
				if($$("fileclub_"+id_club+"_1") && obj.disabled == false){
					obj_file=$$("fileclub_"+id_club+"_1");
					if(obj_file.value==""){
						error+="- Ingresa el Comprobante (Archivo) en el club: <b>"+titulo_club+"</b>.<br>";
					}else{
						if(!checkFormatoFile(obj_file.value)){
							error+="- El Comprobante (Archivo) debe ser un documento de texto, PDF, jpg, zip o rar en el club: <b>"+titulo_club+"</b>.<br>";
						}
					}
				}
			}
		}
	}
	if(str_clubes!=""){
		if(document.us_fr.NumeroDocumento.value+document.us_fr.NumeroDocumento_dig.value==""){
			error+="- Ingresa el <b>Documento de identidad</b> en el Paso 1, ya que es un dato requerido para pertenecer a los Clubes de Amigos.<br/>";	
		}
	}
	$$("en_clubes").value=str_clubes;
	return error;
}
function enviarPaso(paso,nuevo){
	var error = RetailP1();
	switch(paso){
		case 1:
			error = RetailP1();
		break;
		case 2:
			error = RetailP2();
		break;
		case 3:
			error = RetailP3();
		break;
	}
	if(error!=""){
		getMensaje_720(error);
	}else{
		switch(paso){
		case 1:
			quitarMensaje("mensaje");
			mostrarFormularioRetail(2);	
		break;
		case 2:
			quitarMensaje("mensaje");
			mostrarFormularioRetail(3);	
		break;
		case 3:
			quitarMensaje("mensaje");
			mostrarFormularioRetail(4);	
		break;
	}
	}
}
function finalizarRetail(nuevo){
	var error="";
	quitarMensaje("mensaje");
	error+=RetailP1();
	error+=RetailP2();
	error+=RetailP3(nuevo);
	if(document.getElementById("paso4")){
		error+=RetailP4();
	}
	
	if(error!=""){
		getMensaje_720(error);	
		document.location="#msg_anc";
	}else{
		if(nuevo==1){
			document.us_fr.NumeroDocumento.disabled = false;
			document.us_fr.NumeroDocumento_dig.disabled = false;
			document.us_fr.NumeroDocumento.disabled = false;
			document.us_fr.Cl.value = cl_MD5;
			var cl_MD5 = calcMD5(document.us_fr.Clave.value);	
			document.us_fr.Cl.value = cl_MD5;
			document.us_fr.Clave.value = asterisco(document.us_fr.Clave.value);
			document.us_fr.Confirmacion.value = asterisco(document.us_fr.Clave.value);
		}else{
			if(document.us_fr.Clave.value!=""){
				var cl_MD5 = calcMD5(document.us_fr.Clave.value);	
				document.us_fr.Cl.value = cl_MD5;
				document.us_fr.Clave.value = cl_MD5;
				document.us_fr.Clave.value = asterisco(document.us_fr.Clave.value);
				document.us_fr.Confirmacion.value = asterisco(document.us_fr.Clave.value);
			}
		}
		document.us_fr.submit();
	}
}

function mostrarFormularioRetail(id){
	var form1 = document.getElementById("paso1");
	var form2 = document.getElementById("paso2");
	var form3 = document.getElementById("paso3");
	
	var td1 = document.getElementById("td1");
	var td2 = document.getElementById("td2");
	var td3 = document.getElementById("td3");
	
	if(document.getElementById("paso4")){
		var form4 = document.getElementById("paso4");
		var td4 = document.getElementById("td4");
		var td4_c = document.getElementById("td4_c");
		var div4_c = document.getElementById("div4_c");
	}
	
	switch(id){
		case 1:
			form1.style.display = "block";
			form2.style.display = "none";
			form3.style.display = "none";
			
			td1.className = "sol_2";
			td2.className = "sol_1";
			td3.className = "sol_1";
			
			if(document.getElementById("paso4")){
				form4.style.display = "none";
				td4.className = "sol_1";
				if(td4_c || div4_c){
					td4_c.className = "sol_club_1";
					div4_c.className = "sol_logo_club_1";
				}
			}
		break;
		case 2:
			form1.style.display = "none";
			form2.style.display = "block";
			form3.style.display = "none";
			td1.className = "sol_1";
			td2.className = "sol_2";
			td3.className = "sol_1";
			if(document.getElementById("paso4")){
				form4.style.display = "none";
				td4.className = "sol_1";
				if(td4_c || div4_c){
					td4_c.className = "sol_club_1";
					div4_c.className = "sol_logo_club_1";
				}
			}
		break;
		case 3:
			form1.style.display = "none";
			form2.style.display = "none";
			form3.style.display = "block";
			td1.className = "sol_1";
			td2.className = "sol_1";
			td3.className = "sol_2";
			
			if(document.getElementById("paso4")){
				form4.style.display = "none";
				td4.className = "sol_1";
				if(td4_c || div4_c){
					td4_c.className = "sol_club_1";
					div4_c.className = "sol_logo_club_1";
				}
			}
		break;
		case 4:
			if($$("registro_usuario")){
				verificarClubInterno();
			}
			if(document.getElementById("paso4")){
				form1.style.display = "none";
				form2.style.display = "none";
				form3.style.display = "none";
				form4.style.display = "block";
				td1.className = "sol_1";
				td2.className = "sol_1";
				td3.className = "sol_1";
				td4.className = "sol_2";
				if(td4_c || div4_c){
					td4_c.className = "sol_club_2";
					div4_c.className = "sol_logo_club_2";
				}
			}
		break;
	}
}
function getVtnBajaClub(id){
	resetVtn("vtn");
	getPopUp("vtn","",493,350);
	$$("vtn_mostrar").style.display = "none";
	$$("vtn_cargador").style.display = "block";
	$$("vtn_titulo").innerHTML = "Clubes de Amigos";
	var ajax = Ajax();
	var valores = "id_club="+id;
	ajax.open("POST","../data/ajax_baja_club.php",true);
	ajax.onreadystatechange = function(){
		if(ajax.readyState==4){
			$$("vtn_mostrar").style.display = "block";
			$$("vtn_cargador").style.display = "none";
			$$("vtn_mostrar").innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(valores);
}
function enviarBajaClub(id){
	resetVtn("vtn");
	getPopUp("vtn","",493,350);
	$$("vtn_mostrar").style.display = "none";
	$$("vtn_cargador").style.display = "block";
	$$("vtn_titulo").innerHTML = "Clubes de Amigos";
	$$("vtn_texto_cargador").innerHTML = "Enviando solicitud...aguarda.";
	var ajax = Ajax();
	var valores = "solicitud=1&id_club="+id;
	ajax.open("POST","../data/ajax_baja_club.php",true);
	ajax.onreadystatechange = function(){
		if(ajax.readyState==4){
			$$("vtn_mostrar").style.display = "block";
			$$("vtn_cargador").style.display = "none";
			$$("vtn_mostrar").innerHTML = ajax.responseText;
		}
	}
	ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	ajax.send(valores);
}
function getVtnClub(){
	getPopUp("vtn_club","",493,350);
	$$("vtn_club_mostrar").style.display = "block";
	$$("vtn_club_cargador").style.display = "none";
}
function verificarClubInterno(){
	var cedula = $$("NumeroDocumento").value;
	var cedula_dig = $$("NumeroDocumento_dig").value;
	
	if(cedula+cedula_dig!=""){
		resetVtn("vtn");
		getPopUp("vtn","",250,300);
		$$("vtn_cerrar").style.display = "none";
		$$("vtn_mostrar").style.display = "none";
		$$("vtn_cargador").style.display = "block";
		$$("vtn_titulo").innerHTML = "Verificando Clubes de Amigos";
		$$("vtn_texto_cargador").innerHTML = "Verificando...aguarda.";
	
		var ajax = Ajax();
		var valores = "cedula="+cedula+"&cedula_dig="+cedula_dig;
		ajax.open("POST","../data/ajax_club_interno.php",true);
		ajax.onreadystatechange = function(){
			if(ajax.readyState==4){
				var result = trim(ajax.responseText);
				deletePopUp("vtn");
				var checks = $$("paso4").getElementsByTagName("input");
				if(result!=""){
					$$("hay_club_local").value = 1;
					var ar_club = result.split(",");
					for(var i = 0;i<checks.length;i++){
							var obj = checks[i];
							var name = obj.id;
							var ar_name = name.split("_");
							if(ar_name[0]=="checkclub"){
								for(var e=0;e<ar_club.length;e++){
									if(ar_name[1]==ar_club[e]){
										obj.checked = true;	
										obj.disabled = true;
										obj.id = "checkclub_"+ar_name[1]+"_1";
										obj.name = "checkclub_"+ar_name[1]+"_1";
										if($$("detalle_"+ar_club[e])){
											$$("detalle_"+ar_club[e]).style.display="none";
										}
										if($$("aprob_"+ar_club[e])){
											$$("aprob_"+ar_club[e]).style.display="block";
										}
										break;
									}
								}
							}
						}	
				}else{
					$$("hay_club_local").value = 0;
					for(var i = 0;i<checks.length;i++){
							var obj = checks[i];
							var name = obj.id;
							var ar_name = name.split("_");
							if(ar_name[0]=="checkclub"){
								
								if(obj.disabled){
									obj.checked = false;	
									obj.disabled = false;
									if($$("detalle_"+ar_name[1])){
										$$("detalle_"+ar_name[1]).style.display="block";
									}
									if($$("aprob_"+ar_name[1])){
										$$("aprob_"+ar_name[1]).style.display="none";
									}
								}
						}
					}
				}
			}
		}
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(valores);
	}
}
function verificarClub(){
	var cedula = $$("club_cedula").value;
	var cedula_dig = $$("club_cedula_dig").value;
	$$("vtn_club_msg").innerHTML = "";	
	var error = "";
	if(cedula+cedula_dig==""){
		error+="- Ingresa tu número de cédula más el dígito de control.";
	}else{
		if(!esCedula(cedula+"-"+cedula_dig)){
			error+="- Formato incorrecto. Ej.: 1111111-1";
		}
	}
	if(error!=""){
		$$("vtn_club_msg").style.display = "block";	
		$$("vtn_club_msg").innerHTML = error;	
		$$("club_cedula").style.borderStyle="double";
		$$("club_cedula").style.borderColor="#FFCC00";
		$$("club_cedula_dig").style.borderStyle="double";
		$$("club_cedula_dig").style.borderColor="#FFCC00";
	}else{
		$$("vtn_club_mostrar").style.display = "none";
		$$("vtn_club_cargador").style.display = "block";
		var ajax = Ajax();
		var valores = "cedula="+cedula+"&cedula_dig="+cedula_dig;
		ajax.open("POST","../data/ajax_club.php",true);
		ajax.onreadystatechange = function(){
			if(ajax.readyState==4){
				$$("vtn_club_mostrar").style.display = "block";
				$$("vtn_club_cargador").style.display = "none";
				$$("vtn_club_mostrar").innerHTML = ajax.responseText;
				if($$("cedula_club").value!="" && $$("cedula_club_dig").value!=""){
					$$("Nombre").value = $$("nombre_club").value;
					$$("Apellido").value = $$("apellido_club").value;
					$$("NumeroDocumento").value = $$("cedula_club").value;
					$$("NumeroDocumento_dig").value = $$("cedula_club_dig").value;
					/*$$("NumeroDocumento").disabled = true;
					$$("NumeroDocumento_dig").disabled = true;*/
					
					$$("hay_club_local").value = 1;
					var ar_club = $$("club_club").value.split(",");
					var checks = $$("paso4").getElementsByTagName("input");
					
					for(var i = 0;i<checks.length;i++){
						var obj = checks[i];
						var name = obj.id;
						var ar_name = name.split("_");
						if(ar_name[0]=="checkclub"){
							for(var e=0;e<ar_club.length;e++){
								if(ar_name[1]==ar_club[e]){
									obj.checked = true;	
									obj.disabled = true;
									obj.id = "checkclub_"+ar_name[1]+"_1";
									obj.name = "checkclub_"+ar_name[1]+"_1";
									if($$("detalle_"+ar_club[e])){
										$$("detalle_"+ar_club[e]).style.display="none";
									}
									if($$("aprob_"+ar_club[e])){
										$$("aprob_"+ar_club[e]).style.display="block";
									}
									break;
								}
							}
						}
					}
				}
			}
		}
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(valores);
	}
}
