// JavaScript Document
// Author : Nick Cyrus Lemus Duque



(function ($) {
	
	
	
	defaults = {
		classKO : false,
		divError : '',
		msgError : 'Error en el campo',
		collback : function(){
		}
	};
	
	$.fn.validaciones = function (options, callback) {
			
			var property = $.extend({}, defaults, options);	
			
			$(this).submit(function(e){
				var error = false;
					$(this+'[class*=obligatorio]').each(function(){
							
							if ($(this).is(':visible')){
								
								campo = $(this);
								
								var value = this.value;
								
								if (!value)
								{
									error = true;
									if (property.classKO){
										if (property.divError){
											$(property.divError).html(property.msgError);
											
																											
																	$(property.divError).animate({
																								 	opacity:0.1
																								 },100)
																						.animate({
																								 	opacity:1
																								 },100)

										}
										campo.addClass(property.classKO);
									}									
									campo.focus();	
									return false;
								}else{
									
									$case  = $(this).attr('alt');
									
									switch($case)
									{
										case 'email':
												if (!isEmailAddress(value)){
													error = true;
													if (property.classKO){
														if (property.divError){
																$(property.divError).html(property.msgError);
														}
														campo.addClass(property.classKO);
													}									
													campo.focus();
													return false;
												}else
														error = false;
										break
									}
									
									if (property.classKO){
										campo.removeClass(property.classKO);
									}
								}
								
							}
					})
			
			
			
			if (!error) sendContact();
			e.preventDefault();
			return false;
		})
	}
	
	$.fn.validaciones.Send = function(){
		$(this).submit();
	}
	
}(jQuery));

function isEmailAddress(correo) 
{
	var s = correo;	
	var filter=/^[A-Za-z-_.][A-Za-z0-9_.-]*@[A-Za-z0-9_-]+\.[A-Za-z0-9_.-]+[A-za-z]$/;	
	if (s.length == 0) return false;		
	if (filter.test(s)) return true;
	else return false;
}

/******************************************************/

	function sendContact(){
		
		var nombre, compania , email, conmment
		nombre   = $('#nombre').val(); 
		compania = $('#compania').val(); 
		email    = $('#email').val(); 
 		comment = $('#comment').val(); 
		
		$.ajax({
		   type: "POST",
		   url: "../ajax.php",
		   data: "opc=sendContact&nombre="+nombre+"&compania="+compania+"&email="+email+"&comment="+comment,
		   success: function(msg){
			  $('#RespuestaSend').html(msg);
			  $('#contact').fadeOut(500,function(){
				  	$('#RespuestaSend').fadeIn(100);
			  })
			}
	})

		
	}

/*********************************************************/

