$(document).ready(function(){
	$('#cotizacionProductos input[type=text]').each(function(index, value){
					
			if($(this).val() != 0)
			{
				$(this).addClass('full');
			}
			
			$(this).focus(function(){			
					if($(this).val() == 0)
					{
						$(this).val('');
					} 
					$(this).addClass('focus');
				}
			);
			$(this).focusout(function(){
					if($(this).val() == '' || $(this).val() == 0 || isNaN($(this).val()))
					{
						$(this).val(0);
						$(this).removeClass('full');
					}
					else
					{ 
						$(this).addClass('full');
					}
					$(this).removeClass('focus');
				}
			);									 
		}
	);
});

function checkContacto(theForm)
{
    var why = "";
    why += isEmpty(theForm.txtnombre.value, 'nombre');
    why += checkEmail(theForm.txtemail.value);
	
    if (why != "")
	{
       alert("Se encontraron los siguientes errores:\n\n" + why + "\nPor favor revisa el formulario de contacto y corrige los errores");
       return false;
    }
	return true;
}

function checkEmpty (inputId, options)
{
	var error = "";
	if ($('#' + inputId).val().length == 0) 
	{
		error = "Campo vacío";
	}
	
	if(error.length > 0)
	{
		if(typeof(options) !== 'undefined' && options.messageBox)
		{
			$('#' + inputId).addClass('error');
			$('#' + options.messageBox).html(error);
			$('#' + options.messageBox).slideDown();
		}
		else
		{
			alert(error);
		}
		return false;
	}
	else
	{
		if(typeof(options) !== 'undefined' && options.messageBox)
		{
			$('#' + inputId).removeClass('error');
			$('#' + options.messageBox).html('');
			$('#' + options.messageBox).slideUp();
		}
		return true;
	}
}

function checkEmail (inputId, options)
{
	var error = "";
	
	if ($('#' + inputId).val().length == 0) 
	{
		error = "Dirección de correo faltante.";	   
	}
	else
	{
		var emailFilter = /^.+@.+\..{2,3}$/;
		if (!(emailFilter.test($('#' + inputId).val())))
		{ 
		   error = "Dirección de correo no valida.";
		}
		else
		{
			var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;
			if ($('#' + inputId).val().match(illegalChars))			 
			{
				error = "La dirección de correo contiene caracteres no permitidos.";
			}
		}    
	}
	
	if(error.length > 0)
	{
		if(typeof(options) !== 'undefined' && options.messageBox)
		{
			$('#' + inputId).addClass('error');
			$('#' + options.messageBox).html(error);
			$('#' + options.messageBox).slideDown();
		}
		else
		{
			alert(error);
		}
		return false;
	}
	else
	{
		if(typeof(options) !== 'undefined' && options.messageBox)
		{
			$('#' + inputId).removeClass('error');
			$('#' + options.messageBox).html('');
			$('#' + options.messageBox).slideUp();
		}
		return true;
	}
}

function isEmpty(strng, nombreCampo) {
var error = "";
  if (strng.length == 0) {
     error = " - El campo de " + nombreCampo + " esta vacío.\n"
  }
return error;	  
}
