

/** 
*   camaoFormValidation (c) by camao 2009
*   v1.0 Matthias Friedrich
*/


    /**
     * function to trim whitespaces at the beginning and end of a string
     * call: newString = string.trim();
    */
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g,"");
    }

    function camaoFormValidation(){
        //Define valid regex
        var valid = {
            'text' : /^[a-zA-ZÄÖÜäöüß.,\- :/\n]+$/,
            'numbers' : /^[0-9.,]+$/,
            'integer_numbers' : /^[0-9]+$/,
            'text_numbers' : /^[a-zA-Z0-9ÄÖÜäöüß.,\-:/ \n_]+$/,
            'email' : /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/,
            'phone' : /^[0-9\\ /+-]+$/
        }

        var errorclass = "error";
        var formular;
        var required = new Array();
        var betweenfields = new Array();

        /** 
        *   Initialize the object and all events.
        *   bool init ( dom $Forumular )
        */
        this.init = function(form){
            formular = form;
            formular.select("input").each(function(elm){
                if (!elm.hasClassName("image")){
                    elm.observe("click", this.checkValid.bind(this, elm));
                    elm.observe("keyup", this.checkValid.bind(this, elm));
                }
            }.bind(this));

            formular.select("textarea").each(function(elm){
                if (!elm.hasClassName("image")){
                    elm.observe("keyup", this.checkValid.bind(this, elm));
                }
            }.bind(this));

            formular.select("select").each(function(elm){
                if (!elm.hasClassName("image")){
                    elm.observe("change", this.checkValid.bind(this, elm));
                }
            }.bind(this));

        }

        /** 
        *   Set fields as Required.
        *   none setFieldsAsRequired ( array $reqarr )
        */
        this.setFieldsAsRequired = function(reqarr){
            reqarr.each(function(req){
            required.push(req);
	    }.bind(this));
	}

        this.setBetweenNumbers = function(element, minNumber, maxNumber){
            betweenfield = {
                'element' : element,
                'min' : minNumber,
                'max' : maxNumber
            };

            betweenfields.push(betweenfield);
        }

        this.checkValid = function(elm){
            if (elm.className != ""){
                if (elm.className.indexOf(" ")){
                    firstclasssplit = elm.className.split(" ");
                    firstclass = firstclasssplit[0];
                }else{
                    firstclass = elm.className;
                }
                if (firstclass != "notRequired"){
                    dowork = true;
                    
                    betweenfields.each(function(e){
                        if (e.element == elm){
                            umrech1 = "" + $F(elm) + "";
                            umrech1 = umrech1.replace(",",".");
                            
                            if (umrech1 < e.min || umrech1 > e.max){
                                dowork = false
                                elm.up().addClassName(errorclass);
                            }else{
                                elm.up().removeClassName(errorclass);
                            }
                        }
                    }.bind(this));
                    if (dowork){
                        if (elm.type == "radio"){
                            radios = $$('input[name="' + elm.name + '"]');
                            dowork = false;
                            radios.each(function(e){
                                if (e.checked != false) dowork = true;
                            });
                            if (dowork == false){
                                elm.up().up().removeClassName(errorclass);
                                elm.up().up().addClassName(errorclass);
                            }else{
                                elm.up().up().removeClassName(errorclass);
                            }
                        }else if (elm.tagName.toLowerCase() == "select"){
                            if($F(elm) == -1){
                                elm.up().removeClassName(errorclass);
                                elm.up().addClassName(errorclass);
                            }else{
                                elm.up().removeClassName(errorclass);
                            }
    
                        }else if (firstclass == "relation1"){
                            $("depositDataInputLeasingInfo02").up().removeClassName(errorclass);
                            $("depositDataInputLeasingInfo03").up().removeClassName(errorclass);
    
                        }else if(eval("valid." + firstclass + ".test($F(elm))") == false) {
                            elm.up().removeClassName(errorclass);
                            elm.up().addClassName(errorclass);
                        }else{
                            elm.up().removeClassName(errorclass);
                        }
        
                        if (elm.up(".applicationFormContainer")){
                            checkPart(elm.up(".applicationFormContainer"));
                        }
                    }
                }
            }else{
                elm.up().removeClassName(errorclass);
                return true;
            }
            
        }

        /** 
        *   Checks the Form on errors.
        *   bool getAllowSubmitStatus ( )
        */
        this.getAllowSubmitStatus = function(){
            if ($$("form ." + errorclass).length > 0) return false;
            else return true;
        }

        /** 
        *   Submit the Formular.
        *   bool submit ( )
        */
        this.submit = function(dosubmit){
          
            if (this.getAllowSubmitStatus()){
                dowork = true;
                /*hack*/
                /*if ($("contactWhy")){
                    if ($F("contactWhy") == -1){
                        dowork = false;
                        $("contactWhy").up().addClassName(errorclass);
                    }else{
                        $("contactWhy").up().removeClassName(errorclass);
                    }
                }*/

                if ($("depositDataInputLeasingInfo02")){
                    if ( ($("depositDataInputLeasingInfo02").checked == false) && ($("depositDataInputLeasingInfo03").checked == false)){
                        dowork = false;
                        $("depositDataInputLeasingInfo02").up("div").addClassName(errorclass);
                        $("depositDataInputLeasingInfo03").up("div").addClassName(errorclass);
                    }else{
                        $("depositDataInputLeasingInfo02").up("div").removeClassName(errorclass);
                        $("depositDataInputLeasingInfo03").up("div").removeClassName(errorclass);
                    }
                }


                if ($("contactGenderFemale")){
                    if ( ($("contactGenderFemale").checked == false) && ($("contactGenderMale").checked == false)){
                        dowork = false;
                        $("contactGenderFemale").up("p").addClassName(errorclass);
                    }else{
                        $("contactGenderFemale").up("p").removeClassName(errorclass);
                    }
                }
                /*hacks end*/

                
                
                required.each(function(elm){

                    if (elm.type == "checkbox"){
                        if (!elm.checked) {
                            if (!elm.hasClassName("relation1")){
                                dowork = false;
                            }
                        }
                    }else{
                        gonext = true;
                       
                        if (gonext){
                            if (elm.className.indexOf(" ")){
                                firstclasssplit = elm.className.split(" ");
                                firstclass = firstclasssplit[0];
                            }else{
                                firstclass = elm.className;
                            }
                            if (firstclass != "notRequired"){
                                //elm.value = elm.value.trim();
                                
                                if (elm.type == "text"){
                                    elm.value = elm.value.trim();
                                }
                                
                                if (elm.type == "radio"){
                                    radios = $$('input[name="' + elm.name + '"]');
                                    foundactive = false;
                                    radios.each(function(e){
                                        if (e.checked != false) foundactive = true;
                                    });
                                    if (foundactive == false){
                                        dowork = false;
                                        elm.up().up().removeClassName(errorclass);
                                        elm.up().up().addClassName(errorclass);
                                    }
                                }else if (elm.tagName.toLowerCase() == "select"){
                                    if (($F(elm) == -1) || ($F(elm) == "")){
                                        dowork = false;
                                        elm.up().removeClassName(errorclass);
                                        elm.up().addClassName(errorclass);
                                    }
    
                                }else if ($F(elm) == ""){
                                    dowork = false;
                                    elm.up().removeClassName(errorclass);
                                    elm.up().addClassName(errorclass);
                                }
                            }
                        }

                    }

                    /*hacks*/

                    if ($('managementData')){
                        if ($('managementData').visible() == false){

                            if(elm.up("#managementData")){

                                if (elm.type == "radio"){
                                    elm.up().up().removeClassName(errorclass);
                                }else{ 
                                    elm.up().removeClassName(errorclass);
                                }
                            }
                        }
                    }

                    if (elm.up(".applicationFormContainer")){
                        checkPart(elm.up(".applicationFormContainer"));
                    }
                    /*hacks end*/

                }.bind(this));
                
                if (dowork){
                     if (dosubmit){
                         formular.submit();
                    }else{
                        return true;
                    }
                }else{
                    $("error_message").show();
                }
                return false;
            }else{
                if ($("error_message")) $("error_message").show();
                return false;
            }
        }
    }
