var estara_url = 'http://callcenter.ulisesinteractive.com/servicio/';

// Código para estara directo
function isCallNowCp(cp)
{
	if ( (cp == '') || (cp == null) ) {
		return false;
	}

	if ( callnow_cps == undefined ) {
		return true;
	}

	for ( var i = 0; i < callnow_cps.length; i++ ) {
		var myregexp = new RegExp(callnow_cps[i] + '\\d{3}', "i");
		if ( myregexp.test(cp) ) {
			return true;
		}
	}

	return false;
}

	function getTrueOrFalse(true_percent)
	{
		return true_percent >= Math.ceil((Math.random() * 100));
	}

	$(document).ready(function ()
	{
		if ( $('#calloption').length > 0 )
		{
			var testEstara = 0;

			$("#cp").blur(function(){
				if(testEstara == 0)
				{
					//var displayEst = Math.floor(Math.random()*1001);
					displayEst = getTrueOrFalse(100); //displayEst%2;
					if (estara_hash !='undefined' && displayEst && isCallNowCp($("#cp").val()))
				 	{
				 		$.getJSON(estara_url + "activo-"+estara_hash+"-"+$("#pais option:selected").text()+"-"+$("#cp").val()+".html?callback=?",function(data){
				 		estara_active = data.active;
				 		if(estara_active)
				 		{
				 			$("#calloption").val(1);
				 			$("#horario").append("<option value='Ahora mismo' selected='selected'>Ahora mismo</option>");
				 			_gaq.push(["_setCustomVar",1,"TestEstaraDirecto","CallCenter activo: Posibilidad de llamar ahora mismo",2]);

				 		}
				 		});
				 	}
					else if(estara_hash !='undefined' && displayEst==0)
					{
						_gaq.push(["_setCustomVar",1,"TestEstaraDirecto","CallCenter activo: Sin posibilidad de llamar ahora mismo",2]);
					}
					else
					{
						_gaq.push(["_setCustomVar",1,"TestEstaraDirecto","CallCenter no activo",2]);
					}
				}
				testEstara = 1;
			});

			 $("#sendLead").click(function () {
				 if( ($('#calloption').val() == 1) && ($('#horario').val() != "Ahora mismo"))
				 {
					 $('#calloption').val(2);
				 }

				 if($('#horario').val() == "Ahora mismo")
				 {
					 $('[name=pressed_btn]').val("callnow");
				 }

			 });
		}
	});

$(document).ready(function ()
{
	// predicción a traves del CP
	$("#cp").blur(function(){
	 	$.getScript("http://services.ulisesmedia.es/getlocationdatafrompostalcode/JS/?cp="+$(this).val(),function(){
	 		if(($("#poblacion").val()=="")&&($("#pais").val()=="España")){
	 			$("#poblacion").val(location_poblacion)
	 		}
 		});
	});


	$(".avisoLegal").click(function(){
		window.open(this.href,null,'width=440,height=510,scrollbars=yes');
		return false;
	});

	$(".usoLegal").click(function(){
		window.open(this.href,null,'width=465, height=278, scrollbars=yes');
		return false;
	});

	$("#legal_1").click(function(){
		checkParentField('legal_1',$(this).is(':checked') );
	});

	$("#legal_2").click(function(){
		checkParentField('legal_2',$(this).is(':checked') );
	});

	$("#legal_3").click(function(){
		checkParentField('legal_3',$(this).is(':checked') );
	});

	$("#legal_4").click(function(){
		checkParentField('legal_4',$(this).is(':checked') );
	});

	$("#aceptar_aviso").click(function(){
		checkParentField('legal_1',$("#legal_1").is(':checked') );
		checkParentField('legal_2',$("#legal_2").is(':checked') );
		checkParentField('legal_3',$("#legal_3").is(':checked') );
		checkParentField('legal_4',$("#legal_4").is(':checked') );
		window.opener.$("#avisoLegal").attr("checked","checked");
		window.close();
	});

	var checkParentField = function(parentFieldName, checked) {
		if(checked){
			window.opener.$("#"+parentFieldName).attr("value","1");
		}else{
			window.opener.$("#"+parentFieldName).attr("value","0");
		}
	};

	$("#sendLead").click(function () {
		var es_telefono	=/^(6|8|9)[0-9]{8}$/;
		var es_telefono1=/0{6,}|1{6,}|2{6,}|3{6,}|4{6,}|5{6,}|6{6,}|7{6,}|8{6,}|9{6,}/;
		var ok_cupon 	= true;
		var nombre 		= $('#nombre').val();
		var apellido1 	= $('#apellido1').val();
		var edad		= $('#edad').val();
		var email 		= $('#email').val();
		var telefono 	= $('#telef1').val();
		var cp 			= $('#cp').val();
		var pais 		= $('#pais').val();
		var poblacion 	= $('#poblacion').val();
		var nivel		= $('#nivel').val();
		var alerta 		= '';

		if((nombre == '') || (!isNaN(nombre)))
		{
			alerta = "Debes introducir tu Nombre.\n";
			ok_cupon = false;
		}
		if((apellido1 == '') || (!isNaN(apellido1)))
		{
			alerta = alerta + "Debes introducir tus Apellidos.\n";
			ok_cupon = false;
		}
		if((edad == '') || (isNaN(edad)))
		{
			alerta = alerta + "Debes introducir tu Edad.\n";
			ok_cupon = false;
		}
		if((edad != '') && (!isNaN(edad)) && (edad < '18'))
		{
			alerta = alerta + "Debes ser mayor de edad.\n";
			ok_cupon = false;
		}
		if(edad > 100)
		{
			alerta = alerta + "La edad es incorrecta, no debe ser mayor de 100 años.\n";
			ok_cupon = false;
		}
		if((telefono == '') || (isNaN(telefono)) || ((telefono.length) > 9))
		{
			alerta = alerta + "Debes introducir tu Teléfono (Fijo o Móvil).\n";
			ok_cupon = false;
		}
		else if(!es_telefono.test(telefono) || es_telefono1.test(telefono))
		{
			alerta = alerta + "El Teléfono no es correcto.\n";
			ok_cupon = false;
		}
		if((email == '') || (!validar_email(email)))
		{
			alerta = alerta + 'Debes introducir tu Email.\n';
			ok_cupon = false;
		}

		if (pais == 'España')
		{
			if((cp == '') || (isNaN(cp)) || (cp.length != 5))
			{
				alerta = alerta + "Debes introducir el Código Postal.\n";
				ok_cupon = false;
			}
		}
		else{
			if(cp == '')
			{
				alerta = alerta + "Debes introducir el Código Postal.\n";
				ok_cupon = false;
			}
		}
		if((poblacion == '') || (!isNaN(poblacion)))
		{
			alerta = alerta + "Debes introducir tu Población.\n";
			ok_cupon = false;
		}
		if((pais == '') || (!isNaN(pais)))
		{
			alerta = alerta + "Debes introducir tu País de residencia.\n";
			ok_cupon = false;
		}
		if((nivel =='')){
			alerta = alerta + "Debes introducir tu Nivel de estudios.\n";
			ok_cupon = false;
		}
		if (!$('#avisoLegal').is(':checked'))
		{
			alerta = alerta + "Lee el aviso legal y marca la casilla para aceptarlo.";
			ok_cupon = false;
		}

		if( ok_cupon )
		{
			$("#sendLead").attr('disabled','disabled');
			$("#informacion").submit();

		}
		else
		{
			alert("Hemos detectado datos incorrectos en el formulario. Por favor, revisa los siguientes campos:\n\n"+alerta);
			return false;
		}
	});
});

function validar_email(email){
	var str = email;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(str)){
		return true;
	} else {
		return false;
	}
}

