$(document).ready(function(){
	// GESTION DEL BOTON REGISTRO
	$('#btn_enviar_contacto').click(function(){											
		
		// LEER DATOS DEL FORM
		var nombres = encodeURI($('#nombres').val());
		var apellidos = encodeURI($('#apellidos').val());
		var email = $('#mail').val();
		var cod = $('#codigo_area').val();
		var celular = $('#celular').val();
		var consulta = encodeURI($('#consulta').val());
		var postData = false;
		
		// VALIDACION DE FORMULARIO
		if( trim(nombres) == "")
		{
			formAlert("Por favor ingresa tus nombres", "nombres")
			
			return;
		}			
		if( trim(apellidos) == "")
		{
			formAlert("Por favor ingresa tus apellidos", "apellidos")
			return;
		}			
		if( trim(email) == "")
		{
			formAlert("Por favor ingresa tu mail de contacto", "mail")
			return;
		}			
		if( !validarEmail( email ) )
		{
			formAlert("Por favor ingresa un mail de contacto v\u00e1lido", "mail")
			return;			
		}
		if( trim(celular) == "")
		{
			formAlert("Por favor ingresa tu n\u00famero de contacto", "movil")
			return;
		}	
		if( trim(consulta) == "")
		{
			formAlert("Por favor ingresa tu consulta", "consulta")
			return;
		}	
		
		celular = cod + "-" + celular;

		var postData = 
			"nombres="+nombres
			+"&apellidos="+apellidos
			+"&email="+email
			+"&celular="+celular
			+"&consulta="+consulta;
		
		$.ajax(
		{
			type: "POST",
			url: "process/act_send_contact.asp",
			data: postData,
			success: function(serverMsg)
			{
				var respuesta = new String(serverMsg);

				if ( respuesta == "ERROR_SENDING_MAIL" )
				{
					alert("En estos momentos no es posible procesar su solicitud. Por favor intentelo m\u00e1s tarde.");					
				}
				else if ( respuesta == "SUCCESS" )
				{					
					alert("Tu mail de contacto fu\u00e9 recibido por nuestros administrativos y pronto ser\u00e1 atendido. Muchas gracias.");
					document.location.href= "index.asp";
				} 
				else {
					alert("En estos momentos el servidor se encuentra ocupado. Por favor intentelo m\u00e1s tarde");
				}					
			}
		});
	});

});

function trim(variable)
{
	if (variable != "")
	{
	frase = variable.split("%");
	//alert("dd" + frase[0]);
	
	return frase[0];
	}else
	return variable;
}


function rTrim(sStr){

     while (sStr.charAt(sStr.length - 1) == "%20")

      sStr = sStr.substr(0, sStr.length - 1);
	  alert ("-"+sStr+"-");

     return sStr;

    }


