

/*********************
Controlador de errores
**********************/
//onerror=handleErr;
var txt="";
function handleErr(msg,url,l){
	txt="There was an error on this page.\n\n";
	txt+="Error: " + msg + "\n";
	txt+="URL: " + url + "\n";
	txt+="Line: " + l + "\n\n";
	txt+="Click OK to continue.\n\n";
//alert(txt);
return true;
}


function envia_paso01(provincia)
{

	if (provincia == undefined || provincia == false)
	{
		var provincia = $("#provincia").val();
	}else{
		var provincia = provincia;
	}

 if($("#provincia").val() != "")
 {

			 $.ajax({
			   type: "POST",
			   url: "paso_02.php",
			   data: "provincia="+provincia,
			   success: function(msg){

					try {
						var nombreProvincia = $("#provincia option:selected").text();
						pageTracker._trackPageview("listadoProvincia/"+nombreProvincia);
					} catch(err) {}

			   	//alert(msg)
			   	$("#oBloqueSuperior").empty();
			    $("#oBloqueSuperior").html(msg);
			   }
			 });


	}
	else
	{
		alert("Debes seleccionar una provincia");
		return false;
	}


}

function vuelveaPaso02(provincia)
{
	$.ajax({
			   type: "POST",
			   url: "paso_02.php",
			   data: "provincia="+provincia,
			   success: function(msg){
			   	//alert(msg)
			   	$("#oBloqueSuperior").empty();
			    $("#oBloqueSuperior").html(msg);
			   }
			 });
}

function pagina(pag,provincia)
{

	 $.ajax({
			   type: "POST",
			   url: "paso_02.php",
			   data: "provincia="+provincia+"&pag="+pag,
			   success: function(msg){
			   	//alert(msg)
			   	$("#oBloqueSuperior").empty();
			    $("#oBloqueSuperior").html(msg);
			   }
			 });

}

function mas_info(param,provincia,unico)
{
	$.ajax({
			   type: "POST",
			   url: "paso_03.php",
			   data: "distribuidor="+param+"&provincia="+provincia+"&unico="+unico,
			   success: function(msg){

			   	try {
						pageTracker._trackPageview("DetalleDistribuidor/"+param);
					} catch(err) {}

			   	$("#oBloqueSuperior").empty();
			    $("#oBloqueSuperior").html(msg);
			   }
			 });

}

function contacta()
{
	var error = '';

	if($("#formContacto #nombre").val() == "")
	{
		error += "- Debe escribir su nombre\n";
	}
	if($("#formContacto #apellidos").val() == "")
	{
		error += "- Debe escribir sus apellidos\n";
	}
	if($("#formContacto #ciudad").val() == "")
	{
		error += "- Debe escribir su ciudad\n";
	}
	if($("#formContacto #telefono").val() == "" && $("#formContacto #email").val() == "")
	{
		error += "- Debe escribir su telefono\n";
	}
	else if ($("#formContacto #email").val() == "")
	{

		if($("#formContacto #telefono").val().length < 9)
		{
			error += "- El telefono debe contener 9 cifras\n";
		}
		else if($("#formContacto #telefono").val().substring(0,1) !=  '6' && $("#formContacto #telefono").val().substring(0,1) != '9')
		{
			error += "- El telefono debe comenzar por 6 o 9\n";
		}
		else if(!validarSiNumero($("#formContacto #telefono").val()))
		{
			error += "- El telefono solo debe contener numeros\n";
		}

	}

	if($("#formContacto #email").val() == "" && $("#formContacto #telefono").val() == "")
	{
		error += "- Debe escribir su email\n";
	}
	else if($("#formContacto #telefono").val() == "")
	{

		if(!valEmail($("#formContacto #email").val()))
		{
			error += "- Debe escribir un email correcto\n";
		}

	}

	if($("#formContacto #terminos:checked").val() != 'on')
	{
		error += "- Debe aceptar los terminos y condiciones\n";
	}

		if(error != '')
		{
			alert(error);
			return false;
		}else{

			var datos = $("#formContacto").serialize();

			$.ajax({
			   type: "POST",
			   url: "back/x_guarda_contacto.php",
			   data: datos,
			   success: function(msg){
			   	$("#oBloqueSuperior").empty();
			    $("#oBloqueSuperior").html("<div class='gracias'>Tu solicitud ha sido enviada con &eacute;xito. Pronto nos pondremos en contacto contigo</div>");
			   }
			 });

			return false;
		}



}



function valEmail(valor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor))    {
        return false;
    }else{
        return true;
    }
}


// Validar si una cadena es numero
function validarSiNumero(numero){
	if (!/^([0-9])*$/.test(numero)){
		return false;
	}else{
		return true;
	}
}

function terminos_condiciones()
{
	window.open('terminos_condiciones.html','','height=320,width=480,scrollbars=yes');
}

function abre_legal()
{
	window.open('aviso_legal.html','','height=320,width=480,scrollbars=yes');
}



//INTERSTICIAL

$(document).ready(function() {
	var anchoVentana = $(window).width();
	var altoVentana = $(document).height();
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7)
	{
		//oculto <select> para ie6
		$('#provincia').hide();
		$('#provincia').css({'z-index':-1});
	}
	
	$('#velo').css({'width':anchoVentana, 'height':altoVentana, 'opacity':.5, 'display':'block'});
	$('#intersticial').css({'display':'block'});
	$('#btn_cerrar').click(cierraIntesticial);
	$('#velo').click(cierraIntesticial);
});

function cierraIntesticial()
{
	$('#velo').fadeOut(200);
	$('#intersticial').fadeOut(200, eliminaBanner);
	
	return false;
}

function eliminaBanner()
{
	if ($.browser.msie && $.browser.version.substr(0,1)<7)
	{
		//oculto <select> para ie6
		$('#provincia').show();
		$('#provincia').css({'z-index':1});
	}
	$('#velo').remove();
	$('#intersticial').remove();
}




