window.onload = function()
{
    if ( document.forms )
    {
        for (var i = 0; i < document.forms.length; i++)
        {
            if( document.forms[i] )
            {
                var validate = GetAttribute(document.forms[i], 'fckvalidate', 'false');

                if(validate == 'true')
                {
                    document.forms[i].onsubmit = function()
                    {
                        return validateForm( this );
                    }
                }

                if( document.forms[i].elements )
                {
                    for (var j = 0; j < document.forms[i].elements.length; j++)
                    {
                        var el = document.forms[i].elements[j];
                        if( el )
                        {
                            if( GetAttribute(el, 'fcktype', '') == 'numerico' )
                            {
                                el.onkeypress = CheckIsDigit;
                            }
                        }
                    }
                }
            }
        }
    }
}

function CheckIsDigit(e)
{
    if (!e) var e = window.event;
    if (e.keyCode) iCode = e.keyCode;
    else if (e.which) iCode = e.which;

    e.returnValue =
        (
            ( iCode >= 48 && iCode <= 57 )      // Numbers
            || (iCode >= 37 && iCode <= 40)     // Arrows
            || iCode == 8                       // Backspace
            || iCode == 9                       // Tab
            || iCode == 46                      // Delete
        ) ;

    return e.returnValue ;
}

function validateForm(frm)
{
    if( frm.method == 'email' ) {
        frm.method = 'post';
    }

    erro = false;
    if( frm.elements )
    {
        if( GetAttribute(frm, 'fckvalidate', 'false') == 'true' )
        {
            for (var j = 0; j < frm.elements.length; j++)
            {
                var el = frm.elements[j];

                if( el )
                {
                    if( GetAttribute(el, 'fckmandatory', 'false') == 'true' || (el.value != '') )
                    {
                        if( GetAttribute(el, 'fckmandatory', 'false') == 'true' && (el.value == '') )
                        {
                            alert( GetAttribute(el, 'fckmensagem', 'O preenchimento do campo ' + el.name + ' é obrigatório!') );
                            return false;
                        }

                        if( GetAttribute(el, 'fcktype', '') == 'email' )
                        {
                            if( !( validarEmail(el.value) ) ) { erro = true; }
                        }
                        else if( GetAttribute(el, 'fcktype', '') == 'cep' )
                        {
                            if( !( validarCEP(el.value) ) ) { erro = true; }
                        }
                        else if(GetAttribute(el, 'fcktype', '') == 'data')
                        {
                            if( !( validarData(el.value) ) ) { erro = true; }
                        }
                        else if(GetAttribute(el, 'fcktype', '') == 'numerico')
                        {
                            if( !( validarNumerico(el.value) ) ) { erro = true; }
                        }

                        if( erro )
                        {
                            alert( GetAttribute(el, 'fckmensagem', 'Formato do campo ' + el.name + ' é inválido!') );
                            el.focus();
                            return false;
                        }
                    }
                }
            }
        }
    }
    
    try {
	    var bnts = document.getElementsByTagName('input');
	    for (var j = 0; j < bnts.length; j++)
		{	
			var o = bnts[j];
		    if(GetAttribute(o, 'type', '') == 'submit')
		    {
			    if(GetAttribute(o, 'fckcheck', '') == 'true')
			    {
			    	o.disabled = true;
			    }	    	
		    }
	    }
    }
    catch( e ) { 
    	alert(e);
    }
    
    return true;
}

function GetAttribute( element, attName, valueIfNull )
{
    var oAtt   = element.attributes[attName] ;
    var oValue = null;

    try
    {
        if ( !( oValue = element.getAttribute( attName ) ) )
            oValue = oAtt.nodeValue ;
    }
    catch(e) {}

    return ( oValue == null ? valueIfNull : oValue ) ;
}

function validarEmail(emailStr) {
    var emailPat=/^(.+)@(.+)$/;

    var matchArray=emailStr.match(emailPat);
    if (matchArray==null) {
        return false;
    }

    return true;
}

function validarCEP(cepStr) {
    var cepPat=/^\d{5}-\d{3}/;

    var matchArray=cepStr.match(cepPat);
    if (matchArray==null) {
        return false;
    }

    return true;
}

function validarNumerico( Str ) {
    var numPat=/^\d+/;

    var matchArray=Str.match(numPat);
    if (matchArray==null) {
        return false;
    }

    return true;
}

function validarData( dateStr ) {
    var datePat=/^\d{2}\/\d{2}\/\d{4}/;

    var matchArray=dateStr.match(datePat);
    if (matchArray==null) {
        return false;
    }

    return true;
}
