$(document).ready(function()
{
	$.each($("input[type=text], input[type=password], textarea"), function(i, n)
	{
		$(n).attr("rel", (($(n).attr("id") != "") && ( !$(n).is("textarea") )) ? document.getElementById($(n).attr("id")).getAttribute("value") : $(n).val() );
		$(n).focus( function()
		{
			if($(this).val() == $(this).attr("rel")){ $(this).val(""); }
		});
		$(n).blur( function()
		{
			if($(this).val() === ""){ $(this).val($(this).attr("rel")); }
		});
	});
	
	
	if( $.browser.msie )
	{
		$("#accesoClientes").insertBefore("#branding");
	}
	
	$("#accesoClientes ol").hide();
	$("#accesoClientes legend").css("cursor", "pointer").hover
	(
		function()
		{
			$(this).addClass("hover");
		},
		function()
		{
			$(this).removeClass("hover");
		}
	);
	
	
	$("#accesoClientes legend").bind("click", function()
	{
		$(this).fadeOut("fast", function()
		{
			$("#accesoClientes ol").fadeIn("slow");
		});
	});
	
	
	
	
	// css hacks
	if( $.browser.msie )
	{
		$("#globalNav li:last-child").css("border-right", "none").css("padding-right", "0px");
	}
});


var formValidator = {

    errors          : Array,
    erroneousFields : Array,
    returnVal       : true,
    
    init : function()
    {
        formValidator.errors          = [];
        formValidator.erroneousFields = [];
        formValidator.returnVal       = true;
    },
    
    checkRequired : function(el)
    {
        if( (el.val() === "") || (el.val() === null) )
        {
            formValidator.errors.push("El campo " + String(el.attr("id")).replace(/^f/,"") + " es obligatorio");
            formValidator.returnVal = false;
            formValidator.erroneousFields.push(el);
        }
    },
  
    checkMaxLength : function(els, length)
    {
        for(var i=0; i<els.length; i++)
        {
            if(String(els[i].val()).length > length)
            {
                 formValidator.errors.push("El campo " + String(els[i].attr("id")).replace(/^f/,"") + " no puede contener mas de " + length + " dígitos");
                 formValidator.returnVal = false;
                 formValidator.erroneousFields.push(els[i]);
            }
        }
    },
    
    checkValidMail : function(el)
    {
        var pattern  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!pattern.test(el.val()))
        {  
            formValidator.errors.push("Debe introducir una direccion de mail correcta en el campo " + String(el.attr("id")).replace(/^f/,""));
            formValidator.returnVal = false;
            formValidator.erroneousFields.push(el);
        }
    },
    
    checkNumeric : function(el)
    {
        if(isNaN(el.val()))
        {
            formValidator.errors.push("El campo " + String(el.attr("id")).replace(/^f/,"") + " solo puede contener números");
            formValidator.returnVal = false;
            formValidator.erroneousFields.push(el);
        }
    },
    
    checkRange : function(el, minValue, maxValue)
    {
        var val = el.val();
        if(isNaN(val) || !( (val >= minValue) && (val <= maxValue) ) )
        {
            formValidator.errors.push("El campo " + String(el.attr("id")).replace(/^f/,"") + " debe contener un valor numérico emprendido entre " + minValue + " y " + maxValue);
            formValidator.returnVal = false;
            formValidator.erroneousFields.push(el);
        }
    },
    
    checkCp : function(el)
    {
        var cp = el.val();
        if( (cp.length > 5) || (isNaN(cp)) )
        {
            formValidator.errors.push("El campo " + String(el.attr("id")).replace(/^f/,"") + " debe contener un código postal válido, formado por 5 núneros");
            formValidator.returnVal = false;
            formValidator.erroneousFields.push(el);
        }
    },

    checkFileInputExtension : function(el, ext)
    {
        if(String(el.val()).toLowerCase().substr(String(el.val()).lastIndexOf('.')) != ext)
        {
            formValidator.errors.push("El campo " + String(el.attr("id")).replace(/^f/,"") + " solo admite archivos con la extensión " + ext);
            formValidator.returnVal = false;
            formValidator.erroneousFields.push(el);
        }
    }
  
};
