$(document).ready(function() {
	
	$('#name').focus();
	
	$('#frm_contact').submit(function() {
		var name		= jQuery.trim($('#name').val());
		var email		= jQuery.trim($('#email').val());
		var phone		= jQuery.trim($('#phone').val());
		var msg			= jQuery.trim($('#msg').val());
		var code		= jQuery.trim($('#code').val());
		
		//Product Included
		
		var pro_proid		= jQuery.trim($('#pro_proid').val());
		var pro_venid		= jQuery.trim($('#pro_venid').val());
		var pro_code		= jQuery.trim($('#pro_code').val());
		var pro_price1		= jQuery.trim($('#pro_price1').val());
		var pro_tit1		= jQuery.trim($('#pro_tit1').val());
		var pro_des1		= jQuery.trim($('#pro_des1').val());
		var pro_size		= jQuery.trim($('#pro_size').val());
		var pro_pkg			= jQuery.trim($('#pro_pkg').val());
		var pro_pic			= jQuery.trim($('#pro_pic').val());
		var pro_updated		= jQuery.trim($('#pro_updated').val());
		var pro_qty			= jQuery.trim($('#pro_qty').val());
		var pro_isact		= jQuery.trim($('#pro_isact').val());
		
		var red			= "#CE0E09";
		var black		= "#000000";
		
		//Validate name
		if(name.length <= 2) {
			alert("Escriba su Nombre Completo!");
			$('#name').focus();
			highLightInput('name', red, 3);
			
			return false;
		}
		else {
			highLightInput('name', black, 1);
		}
		
		//Validate name
		if(phone.length <= 5) {
			alert("Escriba su Numero de Telefono para Contactarlo!");
			$('#phone').focus();
			highLightInput('phone', red, 3);
			
			return false;
		}
		else {
			highLightInput('phone', black, 1);
		}		
		
		//Validate email
		if( (email.length <= 9) || (email.indexOf("@")<0) ) {
			alert("Ingrese un Email Valido!");
			$('#email').focus();
			highLightInput('email', red, 3);
			
			return false;
		}
		else {
			highLightInput('email', black, 1);
		}
		
		//Validate Question
		if(msg.length <= 5) {
			alert("Detalle mas su mensaje por favor");
			$('#msg').focus();
			highLightInput('msg', red, 3);
			
			return false;
		}
		else {
			highLightInput('msg', black, 1);
		}
		
		//Validate code
		if(code.length != 5) {
			alert("Digite y Verifique el Numero de Seguridad (Numero con Fondo Rojo)");
			$('#code').focus();
			highLightInput('code', red, 3);
			
			return false;
		}
		else {
			highLightInput('code', black, 1);
		}
		
		
		//Send Data
		var prod_params		= "";
		var request_data 	= "";
		
		if(pro_proid.length > 1){
			prod_params		= "&pro_proid="+pro_proid
								+"&pro_venid="+pro_venid
								+"&pro_code="+pro_code
								+"&pro_price1="+pro_price1
								+"&pro_tit1="+pro_tit1
								+"&pro_des1="+pro_des1
								+"&pro_size="+pro_size
								+"&pro_pkg="+pro_pkg
								+"&pro_pic="+pro_pic
								+"&pro_updated="+pro_updated
								+"&pro_qty="+pro_qty
								+"&pro_isact="+pro_isact;
		}
		
		request_data = "name="+name
					 +"&email="+email
					 +"&phone="+phone
					 +"&msg="+msg
					 +"&code="+code
					 +prod_params;
					 
		
		 $.ajax({
		   type: "POST",
		   url: "http://www.construcr.com/contactar/doaction/",
		   data: request_data,
		   beforeSend: function() {
		   		$("#contactar_main").css({"display":"none"});
		   		$("#loading").attr({"style":""});
		   },
		   success: function(result){
		   		$("#loading").attr({"style":"display:none;"});
		   		result	= jQuery.trim(result);
		   		
		     	if(result == 'error-code') {
		     		$("#contactar_main").attr({"style":""});
		     		alert("Wrong code Number.");
		     		$('#code').focus();
					highLightInput('code', red, 3);
		     	}
		     	else {
		     		$("#contactar_main").attr({"style":""});
		     		var success_msg	 = '<table width="100%" border="0" cellspacing="0" cellpadding="5"><tr><td align="center"><img src="/construcr/skin/default/image/contact/ok.gif" width="73" height="75" /></td></tr><tr><td align="center"><strong>Mensaje Enviado Correctamente!</strong><br />Pronto nos comunicaremos con usted. <br /><br /><em>Att: El Equipo de ConstruCR.com</em> </td></tr></table>';
		   			$("#contactar_main").html(success_msg);
		     	}
		   }
		 });

		
		
		return false;
	});
	
	
	/*
	* function: highLightInput
	* desc: Highlights the Input
	*/
	function highLightInput(id, color, border) {
		$('#'+id).css({"border": ""+border+"px solid "+color+""});
	}
	
});