function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

$(document).ready(function (){
	
	$("#formsubmitdata").click(submitformdatauser);  
	$("#sendprofinder").click(submitformprofinder);  
	
}
);

function checkCP(){
	//document.getElementById ('paso1').style.display = 'none';
	var f = document.getElementById('formcontract');
	if (f.cp.value=='' || f.cp.value==undefined){
		alert("Debe introducir un código postal");
		document.getElementById ('cp_ok').value = '0';
		return false;
	}
	var _cp = f.cp.value;
	//alert("VALE="+_cp);
	//alert("USER="+_user+"PASSWD="+_password);
	
		        
	    
        $.ajax({
           url: "checkCP.php",
           type: "POST",
           dataType: "xml",
           data: {cp: _cp},	           		
           error: function(req, err, obj) {
           	alert('error');
            alert(err);
           },
           success: function(xml) {
           	//alert (txt);
			if ($("res",xml).text()=='OK'){
				if ($("service",xml).text() == '0'){
					document.getElementById ('cp_ok').value = '0';
					document.getElementById ('paso1').style.display = 'none';
					document.getElementById ('paso2').style.display = 'none';
					document.getElementById ('resultado').style.display = 'none';
					document.getElementById ('seleccion').style.display = 'none';
					document.getElementById ('paso_email').style.display = 'none';
					//document.getElementById ('resultados').style.display = 'none';
					xt_med('F','','TIENDA::shopping_alarma::CoberturaGeograficaKo');
					document.getElementById ('feedback2').style.display = 'block';
				}else{
					//alert(txt);
					document.getElementById ('cp_ok').value = '1';
					document.getElementById ('muestraacuda').value = $("acuda",xml).text();
					document.getElementById ('service').value = '0';
					document.getElementById ('continuar1').style.display = 'none';
					if (document.getElementById ('paso2').style.display=='none')
						document.getElementById ('continuar2').style.display = '';
					document.getElementById ('paso2').style.display = 'block';
					xt_med('F','','TIENDA::shopping_alarma::CoberturaGeograficaOk');
					
				}
			}else {
				//alert("VALE="+txt);
				document.getElementById ('cp_ok').value = '0';
				alert("El código postal no es válido")
			}

                        
           }
        });            
	
	
	return false;
	
}

function checkCPProf(){
	//document.getElementById ('paso1').style.display = 'none';
	var f = document.getElementById('formprofinder');
	
	var _cp = f.cp.value;
	//alert("VALE="+_cp);
	//alert("USER="+_user+"PASSWD="+_password);
	
		        
	 if (_cp != '')  {
        $.ajax({
           url: "checkCP.php",
           type: "POST",
           dataType: "xml",
           data: {cp: _cp},	           		
           error: function(req, err, obj) {
           	alert('error');
            alert(err);
           },
           success: function(xml) {
           	//alert (txt);
			if ($("res",xml).text()=='OK'){
				
				if (($("numprov",xml).text() == '07')||($("numprov",xml).text() == '7')){
					
					document.getElementById ('ship').value = document.getElementById ('baleares').value;
					$("#ship_cost").text (document.getElementById ('baleares').value*document.getElementById ('num_prof_1').value);

					checkPriceTotalProf();						
				}else{
					
					document.getElementById ('ship').value = document.getElementById ('peninsula').value;
					$("#ship_cost").text (document.getElementById ('peninsula').value*document.getElementById ('num_prof_1').value);					
					
					checkPriceTotalProf();							
					
					
				}
			}else {
				
				//document.getElementById ('cp_ok').value = '0';
				alert("El código postal no es válido");
				f.cp.value='';
			}

                        
           }
        });            
	
	 }
	 
	 
	return false;
	
}

function checkResults(aux){
	
	
	//document.getElementById ('paso1').style.display = 'none';
	var f = document.getElementById('formcontract');
	
	if ((f.flag.value=='1') || (aux=='1')){
		if ($("input[@name='metros']:checked").val()=='' || $("input[@name='metros']:checked").val()==undefined){
			alert("Debe seleccionar el número de metros cuadrados");
			document.getElementById ('results_ok').value = '0';
			return false;
		}else if ($("input[@name='plantas']:checked").val()=='' || $("input[@name='plantas']:checked").val()==undefined){
			document.getElementById ('results_ok').value = '0';
			alert("Debe seleccionar el número de plantas");
			return false;
		}else if ($("input[@name='habitaciones']:checked").val()=='' || $("input[@name='habitaciones']:checked").val()==undefined){
			document.getElementById ('results_ok').value = '0';
			alert("Debe seleccionar el número de habitaciones");
			return false;
		}else if ($("input[@name='vivienda']:checked").val()=='' || $("input[@name='vivienda']:checked").val()==undefined){
			document.getElementById ('results_ok').value = '0';
			alert("Debe seleccionar el tipo de vivienda");
			return false;
		}else if ($("input[@name='accesos']:checked").val()=='' || $("input[@name='accesos']:checked").val()==undefined){
			document.getElementById ('results_ok').value = '0';
			alert("Debe seleccionar el número de accesos a la calle");
			return false;
		}else if ($("input[@name='terrazas']:checked").val()=='' || $("input[@name='terrazas']:checked").val()==undefined){
			document.getElementById ('results_ok').value = '0';
			alert("Debe seleccionar el número de terrazas o patios");
			return false;
		}else if ($("input[@name='comunicaciones']:checked").val()=='' || $("input[@name='comunicaciones']:checked").val()==undefined){
			document.getElementById ('results_ok').value = '0';
			alert("Debe seleccionar el tipo de comunicaciones");
			return false;
		}else if ($("input[@name='mascotas']:checked").val()=='' || $("input[@name='mascotas']:checked").val()==undefined){
			document.getElementById ('results_ok').value = '0';
			alert("Debe indicar si posee mascotas");
			return false;
		}
		
		var _metros = $("input[@name='metros']:checked").val();
		var _plantas = $("input[@name='plantas']:checked").val();
		var _habitaciones = $("input[@name='habitaciones']:checked").val();
		var _vivienda = $("input[@name='vivienda']:checked").val();
		var _accesos = $("input[@name='accesos']:checked").val();
		var _terrazas = $("input[@name='terrazas']:checked").val();
		var _comunicaciones = $("input[@name='comunicaciones']:checked").val();
		var _mascotas = $("input[@name='mascotas']:checked").val();
		
		if (f.gsm.checked==true)
			var _gsm = 1;
		else
			var _gsm = 0;
			
		if (f.volum.checked==true)
			var _volum = 1;
		else
			var _volum = 0;
			
		if (f.magnet.checked==true)
			var _magnet = 1;
		else
			var _magnet = 0;
		
		/*var _gsm = false;
		var _volum = true;
		var _magnet = true;
		*/
				        
		if ((_metros==4)||(_plantas==2)||(_habitaciones==4)
			||(_vivienda==5)||(_accesos>1)||(_terrazas>2)){
			document.getElementById ('paso2').style.display = 'none';
			document.getElementById ('resultado').style.display = 'none';
			document.getElementById ('feedback2').style.display = 'none';
			document.getElementById ('paso1').style.display = 'none';
			document.getElementById ('feedback1').style.display = 'block';
			document.getElementById ('results_ok').value = '0';
			document.getElementById ('paso_email').style.display = 'none';
			xt_med('F','','TIENDA::shopping_alarma::CondicionesKo');
			return false;
		}
		
	        $.ajax({
	           url: "checkResult.php",
	           type: "POST",
	           dataType: "xml",
	           data: {metros: _metros, plantas: _plantas, habitaciones: _habitaciones,
	            comunicaciones: _comunicaciones, gsm: _gsm, volum: _volum, magnet: _magnet},	           		
	           error: function(req, err, obj) {
	           	alert('error');
	            alert(err);
	           },
	           success: function(xml) {
	           	//alert (txt);
				if ($("res",xml).text()=='OK'){
					document.getElementById ('results_ok').value = '1';
					$("#price").text ($("price",xml).text());
					$("#numvolum").text ($("num_volum",xml).text());
					$("#nummagnet").text ($("num_magnet",xml).text());
					
					document.getElementById ('kit').value = $("kit",xml).text();
					
					if (($("gsm",xml).text()=='1')||($("gsm",xml).text()=='2'))
						f.gsm.checked=true;
					else
						f.gsm.checked=false;
					
					if ($("gsm",xml).text()=='2')
						f.gsm.disabled=true;
					else
						f.gsm.disabled=false;
						
					var row = document.getElementById("racuda");
					document.getElementById ('flag').value = '1';	
					if (document.getElementById ('resultado').style.display == 'none'){
						if (document.getElementById ('muestraacuda').value == '0')
							//document.getElementById ('racuda').style.display = 'none';
							row.style.display = 'none'
						else
							//document.getElementById ('racuda').style.display = 'row';
							row.style.display = '';
						document.getElementById ('continuar2').style.display = 'none';
						xt_med('F','','TIENDA::shopping_alarma::Resultado');
						document.getElementById ('resultado').style.display = 'block';
						document.getElementById ('seleccion').style.display = 'block';
					}
					
				}else {
					document.getElementById ('results_ok').value = '0';
					//alert("VALE="+txt);
					alert("Error al calcular el precio total.")
				}
	
	                        
	           }
	        });            
		
	}
	return false;
	
}

function showFormBasic(){
	
	//checkCP();
	//if (document.getElementById ('cp_ok').value == '0')
	//	return false;
	
	checkResults();
	if (document.getElementById ('results_ok').value == '0')
		return false;
	
	document.getElementById ('continuar2').style.display = 'none';
	document.getElementById ('seleccion').style.display = 'none';
	xt_med('F','','TIENDA::shopping_alarma::Muestra_email');
	document.getElementById ('paso_email').style.display = 'block';
	return true;
}

function showFormBasicProf(){
	var _ok = wbValidateFormL(document.getElementById('formprofinder'),'es');
	
	var f = document.getElementById('formprofinder');	
	
	
	
	if (_ok && (!f.num_prof_1.value>0)){
		alert ("Debe seleccionar la cantidad de profinder");
		return false;
	}
	if (_ok && (!$("input[@name='num_prof_2']:checked").val()>0)){
		alert ("Debe seleccionar la mensualidad");
		return false;
	}
		
	//document.getElementById ('continuar2').style.display = 'none';
	if (_ok){
		document.getElementById ('seleccion').style.display = 'none';
		document.getElementById ('paso_email').style.display = 'block';
		xt_med('F','','TIENDA::shopping_profinder::Muestra_email');
		return true;
	}
}

function returnFormBasicProf(){
	
	document.getElementById ('paso_email').style.display = 'none';
	document.getElementById ('seleccion').style.display = 'block';
		
	return true;
}

function returnFormBasic(){
	
	document.getElementById ('paso_email').style.display = 'none';	
	document.getElementById ('continuar2').style.display = 'block';
	document.getElementById ('seleccion').style.display = 'block';
		
	return true;
}

function sendFormBasic(flag){
	var f = document.getElementById('formcontract');
	
	if (flag!=1){
		_wbvfREEmail = /^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
		if (!_wbvfREEmail.test (f.email.value)){
			alert ("El email no es valido");
			return false;
		}
	}
	checkCP();
	if (document.getElementById ('cp_ok').value == '0')
		return false;
	
	checkResults();
	if (document.getElementById ('results_ok').value == '0')
		return false;
	
	//document.getElementById ('paso1').style.display = 'none';
	
	
	var _cp = f.cp.value;
	var _metros = $("input[@name='metros']:checked").val();
	var _plantas = $("input[@name='plantas']:checked").val();
	var _habitaciones = $("input[@name='habitaciones']:checked").val();
	var _vivienda = $("input[@name='vivienda']:checked").val();
	var _accesos = $("input[@name='accesos']:checked").val();
	var _terrazas = $("input[@name='terrazas']:checked").val();
	var _comunicaciones = $("input[@name='comunicaciones']:checked").val();
	var _mascotas = $("input[@name='mascotas']:checked").val();
	var _price = $("#price").text();
	var _voladic = $("#numvolum").text();
	var _nummagnet = $("#nummagnet").text();
	var _kit = f.kit.value;
	
	var _service = document.getElementById ('service').value;
	if (flag!=1){
		var _email = f.email.value;
		var _phone = f.phone.value;
	}else{
		var _email='';
		var _phone='';
	}
	
	
	
	if (f.gsm.disabled==true)
		var _gsm = '2';
	else if (f.gsm.checked==true)
		var _gsm = '1';
	else
		var _gsm = '1';
		
	var _acuda = '0';
	if (f.acuda.checked==true)
		_acuda = '1';
		
	var _voladic = '0';
	if (f.volum.checked==true)
		_voladic = '1';
		
	var _magnetadic='0';	
	if (f.magnet.checked==true)
		_magnetadic='1';
		
	/*alert ("CP:"+_cp);
	alert ("METROS:"+_metros);
	alert ("PLANTAS:"+_plantas);
	alert ("HABITACIONES"+_habitaciones);
	alert ("VIVIENDA:"+_vivienda);
	alert ("ACCESOS:"+_accesos);
	alert ("TERRAZAS:"+_terrazas);
	alert ("COMUNICACIONES:"+_comunicaciones);
	alert ("MASCOTAS:"+_mascotas);
	alert ("PRECIO:"+_price);
	alert ("VOLUM"+_numvolum);
	alert ("MAGNET"+_nummagnet);
	alert ("GSM"+_gsm);
	alert ("ACUDA"+_acuda);
	alert ("SERVICE"+_service);
	alert ("KIT"+_kit);*/
	
	$.ajax({
	           url: "insertUserBasic.php",
	           type: "POST",
	           dataType: "xml",
	           data: {cp: _cp,resp1: _metros, resp2: _plantas, resp3: _habitaciones, resp4: _vivienda,
	           resp5: _accesos, resp6: _terrazas, resp7: _comunicaciones, resp8: _mascotas,  
	           gsm: _gsm, voladic: _voladic, magnetadic: _magnetadic, acuda: _acuda, service: _service,kit: _kit,
	           email: _email, phone: _phone, flag: flag},	           		
	           error: function(req, err, obj) {
	           	alert('error');
	            alert(err);
	           },
	           success: function(xml) {
	           	//alert (txt);
				if ($("res",xml).text()=='OK'){
					
					if (flag==1){
						var _id = $("id",xml).text();
						
						xt_med('F','','TIENDA::shopping_alarma::Contratar');
						self.location = "https://prd-es-prosegur-alm537.geaportal.com/TIENDAONLINE/formulario.html?id="+_id;
						//self.location = 'shopping_alarma.php';
					}
					else{
						alert ("Sus datos se enviaron correctamente. Prosegur contactará con usted lo antes posible");
						xt_med('F','','TIENDA::shopping_alarma::Enviar_OK');
						self.location = 'shopping_alarma.php';
					}
					
				}else {
					//alert("VALE="+txt);
					xt_med('F','','TIENDA::shopping_alarma::Enviar_KO');
					alert("Error al enviar el formulario.")
				}
	
	                        
	           }
	        }); 
		
	
	return false;
}

function checkPriceTotalProf(){
	
	
	var f = document.getElementById('formprofinder');
	var _prof1 = 0;
	
	if (f.num_prof_1.value > 0)
		_prof1	= f.num_prof_1.value;
	
	$("#ship_cost").text (document.getElementById ('ship').value*document.getElementById ('num_prof_1').value);	
	_ship_cost	= $("#ship_cost").text();
	
	$.ajax({
	           url: "calculateProfPrice.php",
	           type: "POST",
	           dataType: "xml",
	           dataType: "xml",
	           data: { prof1: _prof1,ship_cost: _ship_cost},	           		
	           error: function(req, err, obj) {
	           	alert('error');
	            alert(err);
	           },
	           success: function(xml) {
	           	//alert (txt);
				if ($("res",xml).text()=='OK'){
					
					
					$("#price").text ($("price",xml).text());	
					
					
										
				}else {
					return false;
				}
	
	                        
	           }
	        }); 
		
	return false;
		
}


function submitformprofinder(flag){
	
	var _ok = wbValidateFormL(document.getElementById('formprofinder'),'es');
	
	var f = document.getElementById('formprofinder');	
	if (_ok && f.legal.checked!=true && flag!=1){
		alert ("Debe aceptar las condiciones generales del contrato");
		return false;		
	}
	
	
	if (_ok && (!f.num_prof_1.value>0)){
		alert ("Debe seleccionar la cantidad de profinder");
		return false;
	}
	if (_ok && (!$("input[@name='num_prof_2']:checked").val()>0)){
		alert ("Debe seleccionar la mensualidad");
		return false;
	}
	//document.getElementById ('paso1').style.display = 'none';
	
	if (_ok && flag==1){
		_wbvfREEmail = /^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
		if (!_wbvfREEmail.test (f.email.value)){
			alert ("El email no es valido");
			_ok=false;
			return false;
		}
	}
	
	if (_ok){
	
	var _nombre = f.nombre.value;
	var _apellido1 = f.apellido1.value;
	var _apellido2 = f.apellido2.value;
	var _direccion = f.direccion.value;
	var _cp = f.cp.value;
	var _tfno = f.tfno.value;
	var _num_prof1 = f.num_prof_1.value;
	var _num_prof2 = $("input[@name='num_prof_2']:checked").val();
	
	if (flag==1)
		var _email = f.email.value;
	else
		var _email='';
		
	
	
	$.ajax({
	           url: "insertUserProfinder.php",
	           type: "POST",
	           dataType: "xml",
	           data: {nombre: _nombre, apellido1: _apellido1, apellido2: _apellido2, direccion: _direccion, tfno: _tfno, cp: _cp, num_prof1: _num_prof1, num_prof2: _num_prof2, email: _email, flag: flag},	           		
	           error: function(req, err, obj) {
	           	alert('error');
	            alert(err);
	           },
	           success: function(xml) {
	           	//alert (txt);
				if ($("res",xml).text()=='OK'){
					 if (flag!=1){
						var _id = $("id",xml).text();
						xt_med('F','','TIENDA::shopping_profinder::Contratar');
						self.location = 'https://prd-es-prosegur-alm537.geaportal.com/TIENDAONLINE/formularioProfinder.html?id='+_id;
						
					 }else{
					 	alert ("Sus datos se enviaron correctamente. Prosegur contactará con usted lo antes posible");
					 	xt_med('F','','TIENDA::shopping_profinder::Enviar_OK');
					 	self.location = 'shopping_alarma.php?type=profinder';
						
					 }
					 
										
				}else {
					//alert("VALE="+txt);
					xt_med('F','','TIENDA::shopping_profinder::Enviar_KO');
					alert("Error al enviar el formulario.")
				}
	
	                        
	           }
	        }); 
		
	}
	return false;
}





function submitformdatauser(action){
	checkCP();
	if (document.getElementById ('cp_ok').value == '0')
		return false;
	checkResults();
	if (document.getElementById ('results_ok').value == '0')
		return false;
	if ($("input[@name='legal']:checked").val()=='' || $("input[@name='legal']:checked").val()==undefined){
			alert("Debe aceptar las condiciones generales para poder contratar");
			return false;
	}
	sendFormBasic(1);
	return false;
}

function showCP(){
	xt_med('F','','TIENDA::shopping_alarma::CoberturaGeografica');
	document.getElementById ('feedback2').style.display = 'none';
	document.getElementById ('paso1').style.display = 'block';
	document.getElementById ('continuar1').style.display = '';
}

function showResults(){
	
	document.getElementById ('feedback1').style.display = 'none';
	document.getElementById ('paso1').style.display = 'block';
	document.getElementById ('paso2').style.display = 'block';
	document.getElementById ('continuar2').style.display = 'block';
}

function checkAll(){
	
	checkCP();
	if (document.getElementById ('cp_ok').value == '0')
		return false;
	checkResults();
	if (document.getElementById ('results_ok').value == '0')
		return false;
}
