/*
 * ieWeb.js Javascript Framework v0.2
 * by ieWebDesigns (www.inlandempirewebdesigns.com)
 * Requires jQuery 1.3.2 or newer.
 */
ieWeb = {
    /**
     * Validate a form.
     * @param formID the ID of the form to validate, without the # prefix.
     * @param callback a function called after validation. function(bool){}
     */
    Validate: function(formID, callback)
    {
        var form = $('#'+formID);
        var doSubmit = true;
        /* Show Notice */
        var showNotice = function(msg, target){
            var xy = $(target).offset();
            var notice = $('#'+formID+' .ieWebValidationNotice');
            var left = xy.left - notice.outerWidth()/2 + 10;
            if( left < 5 ) left = 5;
            notice.html(msg).fadeIn(500)
            .css({
                'top': xy.top + $(target).outerHeight(),
                'left': left
            });
            $(target).select().keyup(function(){
                notice.hide();
            });
            self.scrollTo(0, xy.top-50);
            target.focus();
            doSubmit = false;
        };
        /* OnSubmit */
        var onSubmit = function(){
            doSubmit = true;
            $('#'+formID+' .ieWebValidationNotice').hide();
            $('#'+formID+' input, #'+formID+' textarea, #'+formID+' select').each(function(){
                if( $(this).attr('disabled') )
                    return doSubmit;
                
                var attribute = $(this).attr('ieweb-val');
                var res = $(this).attr('ieweb-alert');

                if( attribute ){
                    if( attribute.charAt(0) == '_' && this.value.length == 0 )
                        return doSubmit;
                    else if(  attribute.charAt(0) == '_' )
                        attribute = attribute.substr(1);

                    switch( attribute ){
                        case 'required':
                            if( this.value.length == 0 )
                                showNotice(res?res:'This field is required.', this);
                            break;
                        case 'email':
                            if( !/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test( this.value ) )
                                showNotice(res?res:'Please enter a valid email.', this);
                            break;
                        case 'alpha':
                            if( !/^([a-zA-Z ]+)$/.test( this.value ) )
                                showNotice(res?res:'Letters only, please.', this);
                            break;
                        case 'phone':
                            if( !/^(([0-9]{1})*[- .(]*([0-9a-zA-Z]{3})*[- .)]*[0-9a-zA-Z]{3}[- .]*[0-9a-zA-Z]{4})+$/.test( this.value ) )
                                showNotice(res?res:'Please enter a valid phone.', this);
                            break;
                        case 'numeric':
                            if( !/^([0-9.]+)$/.test( this.value ) )
                                showNotice(res?res:'Numbers only, please.', this);
                            break;
                        case 'alphanumeric':
                            if( !/^([a-zA-Z0-9 .]+)$/.test( this.value ) )
                                showNotice(res?res:'Only letters and numbers, please.', this);
                            break;
                        case 'filename':
                            if( !/^[^\\\/\:\*\?\"\<\>\|\.]+$/.test( this.value ) )
                                showNotice(res?res:'Please enter a valid filename.', this);
                            break;
                        case 'url':
                            if( !/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/.test( this.value ) )
                                showNotice(res?res:'Please enter a valid URL.', this);
                            break;
                        default:
                            if( attribute && this.value != $('#'+attribute).val() )
                                showNotice(res?res:'This field doesn\'t match the above.', this);
                    };
                }
                attribute = $(this).attr('ieweb-min');
                if( attribute && this.value.length < attribute )
                    showNotice(res?res:'This field requires a minimum of '+attribute+' characters.', this);
                attribute = $(this).attr('ieweb-max');
                if( attribute && this.value.length > attribute )
                    showNotice(res?res:'This field has a maximum of '+attribute+' characters.', this);
                attribute = $(this).attr('ieweb-gt');
                if( attribute && this.value <= attribute )
                    showNotice(res?res:'This number must be greater than '+attribute+'.', this);
                attribute = $(this).attr('ieweb-lt');
                if( attribute && this.value >= attribute )
                    showNotice(res?res:'This number must be less than than '+attribute+'.', this);

                return doSubmit;
            });
            if( $.isFunction(callback) ){
                callback(doSubmit);
                return false;
            }
            return doSubmit;
        };
        form.submit(onSubmit);
        form.append('<div class="ieWebValidationNotice"></div>');
    },
    readCookie : function(name)
    {
        name = name + '=';
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
        }
        return null;
    },
    saveCookie : function(name, value, days)
    {
        var expires = '';
        if(days){
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            expires = '; expires=' + date.toGMTString();
        }
        document.cookie = name + '=' + escape(value) + expires + '; path=/';
    }
}
