/*reviewed 2nd time to support opera*/

var mabvalid={
    nvdinput:['checkbox','radio','img'],//in oder to set up types that arent valid with respect to form elements
    nvdbutton:['submit','button','reset'],//in oder to set up types that arent valid with respect to form elements
    noclass:['find','button'],
    keys:[8,9,37,38,39,40,46,13],//in oder to set up types that arent valid with respect to form elements
    bordernormal:'1px solid #3664a1',
    bordervalid:'1px solid #B30000',
    border1:'1px',
    border2:'2px',
    backnormal:'#e9f0f5',
    backvalid:'#ffeaea',
    backcommon:'#ffffff',
    marginon:'1px',
    marginoff:'0px',
    allowaction:'',
    allowfield:'',
    init:function(obj,btn){
        var mabobj=document.getElementById(obj)||obj;
        if(mabobj.tagName=="FORM"){
            this.elements=mabobj.elements;
            if(mabobj.className.match("valid")){
                this.addEvent(mabobj,"keypress",this.setEvent);
                this.addEvent(mabobj,"submit",this.setEvent);
            }
        }
        else{
	         
            var input= mabobj.getElementsByTagName("input");
            var textarea= mabobj.getElementsByTagName("textarea");
            var select  = mabobj.getElementsByTagName("select");
            this.elements=new Array();
            var j=0;
            for(var i=0; i<input.length; i++){
                this.elements[j]=input[i];j++;
            }
            for(var i=0; i<textarea.length; i++){
                this.elements[j]=textarea[i];j++;
            }
            for(var i=0; i<select.length; i++){
                this.elements[j]=select[i];j++;
            }
            if(mabobj.className.match("valid")){
                var btnobj=document.getElementById(btn)||btn;
                if(btnobj.type.match('button')){
                    if(this.temp==undefined){
                        this.temp=btnobj.onclick
                        btnobj.onclick='';
                    }
                    this.addEvent(btnobj,"click",this.setEvent);
                }
            }
        }
        for(var i=0;i<this.elements.length;i++){
            this.element=this.elements[i];
            if(!this.element.tagName.match("FIELDSET")){
                if(!this.inarray(this.element.type,this.nvdinput) && !this.inarray(this.element.type,this.nvdbutton) && !this.inarray(this.element.className,this.noclass)){  ///inputs textareas

                    if(this.element.className.match("valid")){
                        if(this.element.parentNode.className.match("dhx_combo_box")){
                            this.element.parentNode.style.backgroundColor=this.backvalid;
                            this.element.parentNode.style.border=this.bordervalid;
                            this.element.parentNode.firstChild.style.background='transparent';
                        }
                        /*else
                if(this.element.type.match("file"))
                {
                  this.element.style.backgroundColor='';
                        this.element.style.border='';	                
                }*/
                        else{
                            this.element.style.backgroundColor=this.backvalid;
                            this.element.style.border=this.bordervalid;
                        }
                    }
                    else{

                        // if(!this.element.className.match('dhx_combo_input')){
                        if(this.element.type!="hidden"){
                            if(this.element.parentNode.className.match("dhx_combo_box")){
                                this.element.parentNode.style.backgroundColor=this.backnormal;
                                this.element.parentNode.style.border=this.bordernormal;
                                this.element.parentNode.firstChild.style.background='transparent';
                            }
                            else{
                                this.element.style.backgroundColor=this.backnormal;
                                this.element.style.border=this.bordernormal;
                            }
                        }
                    //   }
                    }
                    if(this.element.className.match("valid|format")){
                        this.messageCreate(this.element);
                        if(this.element.className.match("date")){
                            this.removeEvent(this.element,"dblclick",this.setEvent);
                            this.addEvent(this.element,"dblclick",this.setEvent);
                        }
                    }
                    this.element.style.margin=this.marginon;
                    this.addEvent(this.element,"keypress",this.setEvent);
                    this.addEvent(this.element,"focus",this.setEvent);
                    this.addEvent(this.element,"blur",this.setEvent);
                }
                else if(this.inarray(this.element.type,this.nvdbutton)){
                    this.element.className="elementbutton";
                    this.addEvent(this.element,"mouseover",this.setEvent);
                    this.addEvent(this.element,"mouseout",this.setEvent);
                }
            }
        }
    },
    inarray:function(element,arraystack, strict){//this.inarray('input',['submit','button','radio'])
        var found = false, key, strict = !!strict;
        for (key in arraystack) {
            if ((strict && arraystack[key] === element) || (!strict && arraystack[key] == element)){
                found = true;
                break;
            }
        }
        return found;
    },
    addEvent:function(element,evType, fn){
        if (element.addEventListener){
            element.addEventListener(evType, fn, false);
            return true;
        }
        else if (element.attachEvent){
            return element.attachEvent("on"+evType, fn);
        }
        else{
            return false;
        }
    },
    removeEvent:function(element,evType, fn){
        if (element.addEventListener){
            element.removeEventListener(evType, fn, false);
        }
        else if (element.attachEvent){
            element.detachEvent("on"+evType, fn);
            element[evType+fn] = null;
            element["e"+evType+fn] = null;
        }
    },
    setEvent:function(event){
        //return false;
        /*
        if(window.addEventListener){ //firefox
            var  target= event.target;
            var  elementvalue= target.value;
            var  keycode= event.keyCode;
            var  charcode= event.charCode;
            var  keystrock=String.fromCharCode(charcode);
            var  stop='event.preventDefault()';
        }
        if(window.attachEvent){ //IE
            var  target= window.event.srcElement ;
            var  elementvalue= target.value;
            var  keycode  = window.event.keyCode;
            var  charcode = window.event.charCode;
            var  keystrock =String.fromCharCode(keycode);
            var  stop='false';

        }*/
        if(navigator.appName=="Microsoft Internet Explorer"){
            var  target= window.event.srcElement ;
            var  elementvalue= target.value;
            var  keycode  = window.event.keyCode;
            var  charcode = window.event.charCode;
            var  keystrock =String.fromCharCode(keycode);
            var  stop='false';
        }
        else{
            var  target= event.target;
            var  elementvalue= target.value;
            var  keycode= event.keyCode;
            var  charcode= event.charCode;
            var  keystrock=String.fromCharCode(charcode);
            var  stop='event.preventDefault()';
        }

        var validate=target.className.split("-");

        if(event.type.match("focus")){
            if(!mabvalid.inarray(target.type,mabvalid.nvdinput) && !mabvalid.inarray(target.type,mabvalid.nvdbutton) && !mabvalid.inarray(target.className,mabvalid.noclass)){
                target.style.backgroundColor=mabvalid.backcommon;
                if(target.tagName!="SELECT"&&target.type!="file"){
                    if(target.parentNode.className.match("dhx_combo_box")){
                        target.parentNode.style.backgroundColor=mabvalid.backcommon;
                        target.style.background='transparent';
                        target.parentNode.style.borderWidth=mabvalid.border2;
                        target.parentNode.style.margin=0+'px';
                    }
                    else{
                        target.style.margin=mabvalid.marginoff;
                        target.style.borderWidth=mabvalid.border2;
                    }
                }
                if(target.className.match("valid|format")&& target.className.match("date")){
                    target.value=target.value.replace(/-/g,"")
                }
            }
        }
        if(event.type.match("blur")){
            this.vbool=1
            if(!mabvalid.inarray(target.type,mabvalid.nvdinput) && !mabvalid.inarray(target.type,mabvalid.nvdbutton) && !mabvalid.inarray(target.className,mabvalid.noclass)){
                target.style.backgroundColor=target.className.match("valid") ? mabvalid.backvalid : mabvalid.backnormal;
                if(target.tagName!="SELECT"){
                    if(target.parentNode.className.match("dhx_combo_box")){
                        if(target.parentNode.getElementsByTagName('input')[1].className.match("valid")){
                            target.parentNode.style.backgroundColor=mabvalid.backvalid

                                    
                        }
                        else{
                            target.parentNode.style.backgroundColor=mabvalid.backnormal
                        }
                        target.style.background='transparent';
                                    
                        target.parentNode.style.margin=1+'px';
                        target.parentNode.style.borderWidth=mabvalid.border1;
                    }
                    else{
                        target.style.margin=mabvalid.marginon;
                        target.style.borderWidth=mabvalid.border1;
                    }
                }
                if(target.className.match("valid|format")){
                    if(target.className.match("int")){
                        if(!mabvalid.digit(elementvalue)){
                            this.vbool*=0
                        }

                        try{
                            if(mabvalid.digit(validate[2])){
                                if(elementvalue.length!=validate[2])
                                    this.vbool*=0
                            }
                        }
                        catch(err){}
                        if(target.className.match("date")){
                            this.value=this.year=this.month=this.date=this.datevalue='';
                            for(var i=0;i<elementvalue.length;i++){
                                if((i==3||i==5)&&elementvalue.length==8)
                                    this.value +=elementvalue.charAt(i)+"-";
                                else
                                    this.value +=elementvalue.charAt(i);
                            }
                            target.value=this.value;
                            this.datevalue=this.value.split("-");
                            this.date= this.datevalue[2];
                            this.month= this.datevalue[1];
                            this.year= this.datevalue[0];
                            this.vbool*=mabvalid.checkDate(this.year,this.month,this.date)?1:0;
                        }
                    }
                    if(target.className.match("email")){
                        this.vbool*=mabvalid.checkMail(elementvalue)?1:0;
                    }
                    document.getElementById(target.name+"div").style.display=this.vbool?"none":"";
                }
            }
        }
        if(event.type.match("keypress")){
            if(mabvalid.inarray(keycode,mabvalid.keys)){
                if(keycode=='13'){
                    if(mabvalid.inarray(target.type,mabvalid.nvdbutton) || target.tagName.toLowerCase()=='textarea'){
                        return true;
                    }
                    else{
                        return eval(stop);
                    }
                }
                else{
                    return true;
                }
            }
            else{
                if(target.className.match("valid|format")){
                    if(target.className.match("int")){
                        if(!mabvalid.digit(keystrock)){
                            return eval(stop);
                        }
                        try{
                            if(mabvalid.digit(validate[2])){
                                if(elementvalue.length>validate[2]-1)
                                    return eval(stop);
                            }
                        }
                        catch(err){}
                    }
                    
                    if(target.className.match("float")){
                        var float_var=elementvalue.split('.');
                        if(float_var.length>1 && keystrock=='.')
                            return eval(stop);
                        if(!mabvalid.digit(keystrock) && keystrock!='.')
                            return eval(stop);
                        
                        try{
                            if(float_var[1].length>validate[2]-1)
                              return eval(stop);
                        }
                        catch(err){}
                    }
                }
            }
        }
        if(event.type.match("mouseover")){
            if(mabvalid.inarray(target.type,mabvalid.nvdbutton))
                target.className="elementbuttonover"
        }
        if(event.type.match("mouseout")){
            if(mabvalid.inarray(target.type,mabvalid.nvdbutton))
                target.className="elementbutton"
        }
        if(event.type.match("dblclick")){
            if(target.className.match("date")){
                displayCalendar(target,'yyyy-mm-dd',target);
                return false;
            }
        }
        if(event.type.match("submit")||event.type.match("click")){
            this.vbool=1;
            for(var i=0;i<mabvalid.elements.length;i++){
                this.element=mabvalid.elements[i];
                this.elebol=1;
                this.validate=this.element.className.split("-");
                if(this.element.className.match("file")){
                    var exe = this.element.value.split(".");
                    if(exe[1])
                    {
                        this.vbool*=(this.element.className.match(exe[1].toLowerCase()))?1:0;
                        this.elebol*=(this.element.className.match(exe[1].toLowerCase()))?1:0;
                        document.getElementById(this.element.name+"div").style.display=this.elebol?"none":"";
                        document.getElementById(this.element.name+"div").innerHTML = 'unallowed extension';
                    }
                                   
                }
                if(!mabvalid.inarray(this.element.type,mabvalid.nvdinput) && !mabvalid.inarray(this.element.type,mabvalid.nvdbutton) && !mabvalid.inarray(target.className,mabvalid.noclass)){  ///inputs textareas
                    if(this.element.className.match("valid")){
                        this.elebol*=(mabvalid.trim(this.element.value)!="")?1:0;
                        if(this.element.className.match("int")){
                            this.elebol*=(mabvalid.digit(this.element.value))?1:0;
                            try{
                                this.elebol*=(mabvalid.digit(this.validate[2]))?1:0;
                                this.elebol*=(this.element.value.length<=this.validate[2])?1:0;
                            }
                            catch(err){}

                            if(this.element.className.match("date")){
                                this.elebol=1;
                                this.datevalue=this.element.value.split("-");
                                this.date= this.datevalue[2];
                                this.month= this.datevalue[1];
                                this.year= this.datevalue[0];
                                this.elebol*=mabvalid.checkDate(this.year,this.month,this.date)?1:0;
                            }

                        }
                        if(this.element.className.match("float")){
                            float_submit=this.element.value.split('.');
                            this.elebol*=float_submit.length>2?0:1;
                            try{
                                if(float_submit[1]){
                                    this.elebol*=float_submit[1].length>validate[2]?0:1;
                                    digit_check=this.element.value.replace('.','');
                                    this.elebol*=(!mabvalid.digit(digit_check))?0:1;
                                }
                            }
                            catch(err){}

                        }

                        if(this.element.className.match("email")){
                            this.elebol*=mabvalid.checkMail(this.element.value)?1:0;
                        }
                        document.getElementById(this.element.name+"div").style.display=this.elebol?"none":"";
                        this.vbool*=this.elebol;
                    }
                }
            }
          
       
         

            if(this.vbool==0){
                if(mabvalid.allowfield!=undefined && mabvalid.allowaction!=undefined){
                    try{
                        var allowsubmit=eval('document.'+target.name+'.'+mabvalid.allowfield+'.value');
                    }
                    catch(err){
                        allowsubmit=undefined
                        };
                    if(allowsubmit!=undefined && allowsubmit.toLowerCase()==mabvalid.allowaction.toLowerCase()){
                        if(mabvalid.temp)
                            mabvalid.temp();
                        try{
                            parent.$("#message_div").empty().attr("class","message_loading");parent.$("#message_obj_div").show();
                        }catch(err){}
                        return true;
                    }
                    else{
                        return eval(stop);
                    }
                }
                else
                    return eval(stop);
            }
            else{
                if(mabvalid.temp)
                    mabvalid.temp();
                try{
                    parent.$("#message_div").empty().attr("class","message_loading");parent.$("#message_obj_div").show();
                }catch(err){}
                return true;

            }
        }

    },
    setallowaction:function(field,value){
        this.allowfield=document.getElementById(field)||field;
        this.allowaction=value;
    },
    
    trim:function trim(str, chars){
        function ltrim(str, chars) {
            chars = chars || "\\s";
            return str.replace(new RegExp("^[" + chars + "]+", "g"),"");
        }
        function rtrim(str, chars) {
            chars = chars || "\\s";
            return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
        }
        return ltrim(rtrim(str, chars), chars);
    },
    digit:function(valobj){
        var isdigit=true;
        for(var i=0;i<valobj.length;i++ ){
            if(!(valobj.charAt(i)>=0 || valobj.charAt(i)<=9)){
                isdigit=false;
            }
        }
        return isdigit;
    },
    checkDate:function(year, month, day){
        function getYear(d){
            return (d < 1000) ? d + 1900 : d;
        }
        month = month - 1;  // javascript month range : 0- 11
        var tempDate = new Date(year,month,day);
        if ((getYear(tempDate.getYear()) == year)&&(month == tempDate.getMonth())&&(day == tempDate.getDate()))
            return true;
        else
            return false
    },
    checkMail:function(valMail){
        var ismail=true;
        var filter =/^.+@.+..{2,3}$/;
        if (!filter.test(valMail))
            ismail=false
        return ismail;
    },
    messageCreate:function(element){
        if(!document.getElementById(element.name+"div")){
            var validdiv = document.createElement("div");
            validdiv.id=element.name+"div";
            validdiv.style.color="#B30000";
            //validdiv.style.textAlign="left";
            validdiv.style.display="none";
            validdiv.innerHTML = element.title;
            if(element.parentNode.className.match("dhx_combo_box"))
                element.parentNode.parentNode.appendChild(validdiv);
            else
                element.parentNode.appendChild(validdiv);
        }
    }
}
