function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

FormManipulation = function(formId, requiredFileds, textAreas){

    this.formId = formId;
    this.requiredFileds = requiredFileds;
    this.textAreas = textAreas;
    this.isValid = false;

    this.ShowClientErrorArea = function(){
        $('#'+this.textAreas[0]).fadeIn('fast');
    }

    this.HideClientErrorArea = function(onElementId){
        if(typeof onElement == typeof underfined){
           this.SwitchOffRequiredSign(onElementId);
        }
        $('#'+this.textAreas[0]).fadeOut('fast');
    }

    this.ShowServerErrorArea = function(){        
        $('#'+this.textAreas[2]).removeClass('no_display');
    }

    this.HideServerErrorArea = function(){       
        $('#'+this.textAreas[2]).addClass('no_display');
    }

    this.ShowClientMessageArea = function(){
        $('#'+this.textAreas[1]).fadeIn('fast');
    }

    this.HideClientMessageArea = function(){
        $('#'+this.textAreas[1]).fadeOut('fast');
    }

    this.SwitchOffRequiredSign = function(element){
        $('#'+element+'_required').removeClass('error');
        $('#'+element+'_required').addClass('normal');        
    }
    this.SwitchOnRequiredSign = function(element){
        $('#'+element+'_required').removeClass('normal');
        $('#'+element+'_required').addClass('error');
    }

    this.ClientValidate = function(){
        this.HideServerErrorArea();

        var formValid = true;

        for(var i=0; i<this.requiredFileds.length; i++){            
            var currentElement = document.getElementById(this.requiredFileds[i]);

            this.SwitchOffRequiredSign(this.requiredFileds[i]);
            
            if((currentElement.tagName.toUpperCase() == 'INPUT' || currentElement.tagName.toUpperCase() == 'TEXTAREA') && trim(currentElement.value) == ''){
                this.SwitchOnRequiredSign(this.requiredFileds[i]);
                formValid = false;
            }
        }
        
        if(formValid == false){
            this.ShowClientErrorArea();
            return false;
        } else {
            $('#'+this.formId).submit();
        }

        return true;
    }

    this.ClientCancel = function(message, back_url){
        var answer = confirm(message);        
        if(answer == true){            
            window.location.href = back_url;
        }
    }
}

