// JavaScript Document
<!--
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 trim(inputString) {
	if (typeof inputString != "string") { 
		return inputString; 
	}
	var retValue = inputString;
	var ch = retValue.substring(0, 1);		
	while (ch == " ") {
		retValue = retValue.substring(1, retValue.length);
		ch = retValue.substring(0, 1);
	}		
	ch = retValue.substring(retValue.length-1, retValue.length);		
	while (ch == " ") {
		retValue = retValue.substring(0, retValue.length - 1);
		ch = retValue.substring(retValue.length - 1, retValue.length);
	}		
	while (retValue.indexOf(" ") != -1) {
		retValue = retValue.substring(0, retValue.indexOf(" ")) + retValue.substring(retValue.indexOf(" ") + 1, retValue.length);
	}		
	return retValue;
};

function validaMail(email) {
	var res = true;		
	var iChars = "! #$%^&*()+=[]\\\';,/{}|\":<>?'";		
	for (var i = 0; i < email.length; i++) {
		if (iChars.indexOf(email.charAt(i)) != -1)
			res = false;
	}		
	if(res == true) {
		res = false;
		if(email.indexOf("@")>0 && email.indexOf("@") == email.lastIndexOf("@"))
		{
			if(email.lastIndexOf(".")>email.indexOf("@") && email.lastIndexOf(".") < email.length-1)
				res = true;
		}	
	}	
	return res;
};

var mensajes = '';

function borraCamposContacto() {
	$("#nombre_txt").val("");
	$("#correo_txt").val("");
	$("#telefono_txt").val("");
	$("#comentario_txt").val("");
};

/****************************************************************************/
function validaTexto(IDitem, CSSclass, ERRmsj) {
	var correcto = false;
	if(trim($(IDitem).val()) == "") {
	  $(IDitem).addClass(CSSclass);
	  mensajes += "<li>" + ERRmsj + "</li>";
	  correcto = false;
	} else {
	  $(IDitem).removeClass(CSSclass);
	  correcto = true;
	}
	if(correcto) return 0; else return 1;
};
function validaCorreo(IDitem, CSSclass, ERRmsj) {
	var correcto = false;
	if(trim($(IDitem).val()) == "" || !validaMail($(IDitem).val())) {
	  $(IDitem).addClass(CSSclass);
	  mensajes += "<li>" + ERRmsj + "</li>";
	  correcto = false;
	} else {
	  $(IDitem).removeClass(CSSclass);
	  correcto = true;
	}
	if(correcto) return 0; else return 1;	
};
/****************************************************************************/

function validaContacto() {
	var total = 0;	
	$("#mensaje_error").html();
	
	total += validaTexto("#nombre_txt", "InputText_Contacto_Obligatorio", "");
	total += validaCorreo("#correo_txt", "InputText_Contacto_Obligatorio", "");
	total += validaTexto("#comentario_txt", "TextArea_Contacto_Obligatorio", "");
	
	if(total > 0) $("#mensaje_error").html('Por favor llene los campos marcados en rojo');
	else contactoSubmit();
};

function contactoSubmit() {
	var datos = "nombre_val=" + $("#nombre_txt").val() + "&correo_val=" + $("#correo_txt").val() + "&telefono_val=" + $("#telefono_txt").val() + "& comentario_val=" + $("#comentario_txt").val();
	$.ajax ({
		type: "POST",
		url: "http://boxer-motors.com/_ajax/contactoAjax.php",
		data: datos,
		success: function(response) {
			if(response == "wrong")
				$("#mensaje_error").html('Por favor llene todos los campos');
			else if(response == "correct") {					
				$("#mensaje_error").html('Gracias, hemos recibido su mensaje');
				borraCamposContacto();
			}
			else
				$("#mensaje_error").html('Error al enviar el mensaje, por favor intente más tarde');
		}
	});
};

function doLogin() {
	alert("Nombre de usuario y/o contraseña incorrecta");
	$("#contrasena_txt").val("");
};

function newsletterSubmit() {
	var total = 0;	
	$("#mensaje_error").html();
	
	total += validaCorreo("#newsletter_txt", "", "");
	
	if(total > 0) alert('Por favor introduzca su nombre y su correo electrónico');
	else {	
		var datos = "correo_val=" + $("#newsletter_txt").val() + "&nombre_val=" + $("#nombreNews_txt").val();
		$.ajax ({
			type: "POST",
			url: "http://boxer-motors.com/_ajax/newsletterAjax.php",
			data: datos,
			success: function(response) {
				if(response == "wrong")
					alert('Por favor introduzca su correo electrónico');
				else if(response == "correct") {					
					alert('Gracias, su correo electrónico fue añadido a nuestro newsletter');
					$("#nombreNews_txt").val("");
					$("#newsletter_txt").val("");
				}
				else
					alert('Error al contactar al servidor, por favor intente más tarde');
			}
		});
	}
};

$(document).ready(function() {
	$('#newsletter_txt').focus(function() {
		if($("#newsletter_txt").val() == "Ingresa tu correo")
			$("#newsletter_txt").val("");
	});
	$('#newsletter_txt').blur(function() {
		if(trim($("#newsletter_txt").val()) == "")
			$("#newsletter_txt").val("Ingresa tu correo");
	});
	
	$('#nombreNews_txt').focus(function() {
		if($("#nombreNews_txt").val() == "Ingresa tu nombre")
			$("#nombreNews_txt").val("");
	});
	$('#nombreNews_txt').blur(function() {
		if(trim($("#nombreNews_txt").val()) == "")
			$("#nombreNews_txt").val("Ingresa tu nombre");
	});
});

/* Suscripción */
function validaSuscripcion() {
	var total = 0;	
	var subtotal = 0;
	$("#mensaje_error").html();
	
	total += validaTexto("#nombre_txt", "InputText_SuscripcionLG_Obligatorio", "");
	total += validaTexto("#apellidos_txt", "InputText_SuscripcionLG_Obligatorio", "");
	total += validaTexto("#direccion_txt", "InputText_SuscripcionLG_Obligatorio", "");
	total += validaTexto("#colonia_txt", "InputText_SuscripcionLG_Obligatorio", "");
	total += validaTexto("#cp_txt", "InputText_SuscripcionLG_Obligatorio", "");
	total += validaCorreo("#correo_txt", "InputText_SuscripcionLG_Obligatorio", "");	
	
	subtotal += validaTexto("#telefono_txt", "InputText_SuscripcionLG_Obligatorio", "");
	subtotal += validaTexto("#celular_txt", "InputText_SuscripcionLG_Obligatorio", "");
	
	if(subtotal == 2) {
		alert("Por favor introduzca su teléfono o celular");	
		total += subtotal;
	}	
	
	if(total > 0) $("#mensaje_error").html('Por favor llene los campos marcados en rojo');
	else suscripcionSubmit();
};

function suscripcionSubmit() {
	var datos = "nombre_val=" + $("#nombre_txt").val() + "&apellidos_val=" + $("#apellidos_txt").val() + "&direccion_val=" + $("#direccion_txt").val() + "&colonia_val=" + $("#colonia_txt").val() + "&cp_val=" + $("#cp_txt").val() + "&telefono_val=" + $("#telefono_txt").val() + "&celular_val=" + $("#celular_txt").val() + "&correo_val=" + $("#correo_txt").val() + "&empresa_val=" + $("#empresa_txt").val() + "&puesto_val=" + $("#puesto_txt").val() + "&direccionEmp_val=" + $("#direccionEmp_txt").val() + "&telefonoEmp_val=" + $("#telefonoEmp_txt").val() + "&nombre1_val=" + $("#nombre1_txt").val() + "&telefono1_val=" + $("#telefono1_txt").val() + "&nombre2_val=" + $("#nombre2_txt").val() + "&telefono2_val=" + $("#telefono2_txt").val() + "&sangre_val=" + $("#sangre_txt").val() + "&alergias_val=" + $("#alergias_txt").val() + "&otros_val=" + $("#otros_txt").val() + "&seguro_val=" + $("#seguro_txt").val() + "&poliza_val=" + $("#poliza_txt").val() + "&telefonoSeg_val=" + $("#telefonoSeg_txt").val() + "&dia_val=" + $("#dia_txt").val() + "&mes_val=" + $("#mes_txt").val() + "&anio_val=" + $("#anio_txt").val() + "&intMoto=" + $("input[name='intMoto']:checked").val(); + "&marca_val=" + $("#marca_txt").val() + "&modelo_val=" + $("#modelo_txt").val() + "&anioMoto_val=" + $("#anioMoto_txt").val();
	$.ajax ({
		type: "POST",
		url: "http://boxer-motors.com/_ajax/suscripcionAjax.php",
		data: datos,
		success: function(response) {
			if(response == "wrong")
				$("#mensaje_error").html('Por favor llene todos los campos');
			else if(response == "correct") {					
				$("#mensaje_error").html('Gracias, hemos recibido su mensaje');
				borraCamposSuscripcion();
			}
			else
				$("#mensaje_error").html('Error al enviar el mensaje, por favor intente más tarde');
		}
	});
};

function borraCamposSuscripcion() {
	$("#nombre_txt").val(""); 	$("#apellidos_txt").val(""); 	$("#direccion_txt").val(""); 	$("#colonia_txt").val("");
	$("#cp_txt").val(""); 		$("#telefono_txt").val(""); 	$("#celular_txt").val(""); 		$("#correo_txt").val("");
	$("#empresa_txt").val(""); 	$("#puesto_txt").val(""); 		$("#direccionEmp_txt").val(""); $("#telefonoEmp_txt").val("");
	$("#nombre1_txt").val(""); 	$("#telefono1_txt").val(""); 	$("#nombre2_txt").val(""); 		$("#telefono2_txt").val("");
	
	$("#sangre_txt").val(""); 	$("#alergias_txt").val(""); 	$("#otros_txt").val(""); 		$("#seguro_txt").val("");
	$("#poliza_txt").val(""); 	$("#telefonoSeg_txt").val(""); 	$("#dia_txt").val(""); 			$("#mes_txt").val("");
	$("#anio_txt").val(""); 	$("#marca_txt").val(""); 		$("#modelo_txt").val(""); 		$("#anioMoto_txt").val("");
};
//-->
