function errorValidacion(locator,mensaje){
	if(typeof(mensaje) != "undefined") jQuery(locator).html(mensaje);
	else jQuery(locator).html("");				
}

jQuery(document).ready(function($) {
	
  preparar_respuesta("/gracias/ .post");
	
  $(".si-button").click(function(e) {
	e.preventDefault();
	var name=$("input#si_contact_name5").val();
	var apellido=$("input#si_contact_apellido5").val();
	var email=$("input#si_contact_email5").val();
	var tel_p=$("input#si_contact_phone_p").val();
	var tel_a=$("input#si_contact_phone_a").val();
	var tel=$("input#si_contact_phone").val();
	var message=$("textarea#si_contact_message5").val();
	var licencia=$("input#si_contact_ex_field5_2").val();
	
	
	var dataString = 'name=' + name + '&apellido=' + apellido + '&email=' + email + '&telp=' + tel_p + '&tela=' + tel_a +'&tel=' + tel + "&message=" + message + "&licencia="+licencia;
	
	if( getParameter("geoIP")!= '' && getParameter("geoIP")!= null )
		var geoIP = "?geoIP="+getParameter("geoIP");
	else
		var geoIP = '';
	
	procesando();
	
	$.post("/serviciosphp/contactforms/renovacion_validar.php", dataString ,function(data){
		if(data.resultado=="OK")
		{
			
			$("div#contact_form").html('<b>Aguarde mientras se procesa su informaci&oacute;n...</b>');
			
			$.post("/serviciosphp/contactforms/renovacion_grabar.php", dataString ,function(data1){
				if(data1.resultado=="OK")
				{
					$.post("/serviciosphp/avgapi/get_renewal.php", dataString ,function(data3){
						
						//mail asincrónico
						$.post("/serviciosphp/contactforms/renovacion_mail.php"+geoIP, dataString);
						
								
								if(data3.error==1)
								{
									respuesta();
								}
								try{
									_gaq.push(['_trackPageview', '/renovar-licencia/form-saved/']);
								} catch (error){}
								
								if(data3.error==0)
								{
									if(data3.cat_name=="avg-pc-tune-up-2011")
										window.location = "/productos/seguridad-para-hogar/"+data3.cat_name+"/?cantidad="+data3.cantidad+"&id="+data3.id;
									else if(data3.trial==1)
										window.location = "/productos/"+data3.parent_name+"/"+data3.cat_name+"/";
									else
										window.location = "/productos/renovaciones/"+data3.cat_name+"/?cantidad="+data3.cantidad+"&id="+data3.id;
								}

					},"json");
				} else {
					$("div#contact_form").html(data1.texto);
					procesando_out();					
				}
			},"json");
		} else {
			if(data.resultado=="ERROR GENERAL")
				$("div#contact_form").html(data.mensaje);
			else {
				if(data.resultado=="ERROR"){
					errorValidacion("div#si_contact_error_name",data.name);
					errorValidacion("div#si_contact_error_apellido",data.apellido);
					errorValidacion("div#si_contact_error_email",data.email);
					errorValidacion("div#si_contact_error_phone",data.tel);
					errorValidacion("div#si_contact_error_message",data.message);
					errorValidacion("div#si_contact_error_ex_field2",data.licencia);
				} else
					$("div#contact_form").html('<b>No se pudo procesar, intente nuevamente m&aacute;s tarde.</b><br />Error: ' + data.mensaje);	
				
			}
			procesando_out();
		}
	},"json");
  return false;
 });
});



