var FormValidator={errorFocused:false,errorsText:'',sender:null,forms:new Hash(),fields:new Hash()}
FormValidator.addForm=function(formName,todoVar,autoFocus){var myForm,formId;if(myForm=$(formName)){if(formId=this.readAttr(myForm,'id'))this.forms.set(formId,{form:myForm,todoVar:todoVar,todoNext:null,autoFocus:autoFocus,fields:new Array()});myForm.addEvent('submit',this.onSubmit.bind(this));}}
FormValidator.initActiveFields=function(){$$('TD.nullSave INPUT[type=text]').each(function(el){el.addEvent('blur',function(ev){if($defined(this.oldClass))this.oldClass=this.className;this.className=this.value.length>0?'haveVal':this.oldClass;});el.fireEvent('blur');});$$('TR.mtDataRow').addEvents({'mouseover':function(e){if(!$defined(this.oldClass))this.oldClass=$defined(this.className)?this.className:'';this.className='rowOver';},'mouseout':function(e){this.className=this.oldClass;}});}
FormValidator.initToggleRad=function(){$$('DIV.toggleRad').each(function(div){div.getElements('INPUT[type=radio]').each(function(input){if(input.checked)div.set('class','toggleRad'+input.value);input.addEvent('click',function(ev){div.set('class','toggleRad'+this.value);});});});}
FormValidator.initToggleCheck=function(chkName){$$('DIV.markSel').each(function(span){var input=span.getElement('INPUT');var label=span.getElement('LABEL');if(input&&label){input.spanEl=span;input.addEvent('click',function(e){this.spanEl.className=this.checked?'checkOn':'checkOff';});input.fireEvent('click');}});$$('DIV.toggleChks').each(function(div){div.getElements('A').each(function(link){var info=link.className.split('-');if(info.length==3)link.addEvent('click',function(ev){FormValidator.toggleCheck(info[2],parseInt(info[1]));});});div.getElements('INPUT[type=checkbox]').each(function(input){var info=input.name.split('-');if(info.length==2)input.addEvent('click',function(ev){FormValidator.toggleCheck(info[1],this.checked);});});});return false;}
FormValidator.toggleCheck=function(chkName,newStatus){$$('input[name="'+chkName+'[]"]').each(function(el){el.checked=newStatus;el.fireEvent('click');});}
FormValidator.addFields=function(mainForm,checkFunc){var myForm=this.forms.get(mainForm);if(myForm)myForm.fields.push(checkFunc.bind(this));}
FormValidator.sendForm=function(sender,todoValue,doSubmit){if(!$chk(doSubmit))doSubmit=false;var myForm=this.findForm(sender);if(myForm){if($chk(todoValue))myForm.todoNext=todoValue;return!doSubmit||this.go(myForm,true);}}
FormValidator.go=function(myForm,doSubmit){if(myForm.todoNext)eval('myForm.form.'+myForm.todoVar+".value = '"+myForm.todoNext+"';");this.showProgress(myForm.form,true);if(doSubmit)myForm.form.submit();return true;}
FormValidator.findForm=function(sender){var myForm;if(sender.form){this.sender=(sender.type=='submit')||(sender.type=='button')?$(sender):null;myForm=sender.form;}else if(sender.tagName=='FORM'){myForm=sender;this.sender=null;}else{myForm=false;this.sender=null;}
return myForm?this.forms.get(myForm.id):false;}
FormValidator.goUrl=function(sender,url){var myForm=this.findForm(sender);if(myForm)this.showProgress(myForm.form,false);location.href=url;}
FormValidator.showProgress=function(myForm,disable){if($chk(this.sender)||(this.sender=myForm.getElement('INPUT[type=submit]'))){if(this.readAttr(myForm,'target')=='_blank')return;else{if(disable)this.sender.set('disabled',true);this.sender.addClass('btnSubmited');this.sender=null;}}}
FormValidator.onSubmit=function(event){var formId,myForm,formName;var formObj=event.target;if((formId=this.readAttr(formObj,'id'))&&(myForm=this.forms.get(formId))){myForm.fields.each(function(checkFunc){checkFunc();});if(this.errorsText.length>0){alert("Please check the following errors:\n\n"+this.errorsText);this.errorFocused=false;this.errorsText='';return false;}else if(formName=this.readAttr(myForm.form,'name')){myForm.todoNext=formName.substr(formId.length+1);return this.go(myForm,false);}}
return true;}
FormValidator.readAttr=function(el,name){if(el.getAttributeNode){var attrNode=el.getAttributeNode(name);return $type(attrNode)=='object'?attrNode.nodeValue:false;}else if(el.getAttribute){return el.getAttribute(name);}else{return false;}}
FormValidator.autoFocus=function(){var forms,elements,i,j;if(forms=this.forms.getValues())
for(i=0;i<forms.length;i++)
if(forms[i].autoFocus&&(elements=forms[i].form.elements))
for(j=0;j<elements.length;j++)
if((elements[j].type.indexOf('text')>=0)&&!elements[j].disabled&&!elements[j].readOnly&&(elements[j].value.length==0)){elements[j].focus();return true;}}
FormValidator.checkField=function(field,fieldDesc,dataType,allowNull,minLen,maxLen,minValue,maxValue){var myField;if((myField=this.findField(field))&&(myField.value!=undefined)){fieldValidator=new fieldClass(this,myField,fieldDesc,dataType,allowNull,minLen,maxLen,minValue,maxValue);return fieldValidator.check();}else{}}
FormValidator.checkGroup=function(groupName,fieldDesc){var found=false;var oneEnabled=false;var group=$$('INPUT[name="'+groupName+'"]');if(group.length>0){for(var i=0;i<group.length;i++){if(!group[i].disabled){oneEnabled=true;if(group[i].checked){found=true;break;}}}
if(!found&&oneEnabled){this.addError('You have to select at least one option from field \''+fieldDesc+'\'');}}}
FormValidator.checkTime=function(group,fieldDesc,allowNull){var timeFields;if((timeFields=$$('SELECT[name="'+group+'"]'))&&(timeFields.length==2)){var hours=timeFields[0].value;var minutes=timeFields[1].value;if((hours.length==0)&&(minutes.length==0)){if(allowNull)return true;else this.addError('\''+fieldDesc+'\' can not be empty');}else if(isNaN(hours)||(hours<0)||(hours>23)||isNaN(minutes)||(minutes<0)||(minutes>59)){this.addError('\''+fieldDesc+'\' have a wrong format');}}}
FormValidator.addError=function(message,field){this.errorsText+="- "+message+".\n";if((!this.errorFocused)&&(field!=undefined)&&(field.type!='hidden')){this.errorFocused=true;field.focus();}}
FormValidator.findField=function(field){var foundField=$(field);if(!foundField)foundField=document.getElement('INPUT[name="'+field+'"],SELECT[name="'+field+'"],TEXTAREA[name="'+field+'"]');return foundField;}
FormValidator.findFields=function(fields){return $$('INPUT[name="'+fields+'"]','SELECT[name="'+fields+'"]','TEXTAREA[name="'+fields+'"]');}
FormValidator.countText=function(field,maxLength){var textArea=$(field);if(textArea)textArea.addEvent('keyup',function(e){var charLeft=maxLength-textArea.value.length;if(charLeft>=0){this.showCount(textArea,'You have '+charLeft+' chars left','help');}else{this.showCount(textArea,'You have entered '+Math.abs(charLeft)+' more chars than allowed','error');}
if(textArea.countTimer)clearTimeout(textArea.countTimer);textArea.countTimer=this.hideCount.delay(2000,this,textArea);}.bind(this));}
FormValidator.showCount=function(textArea,msg,className){var newName=textArea.name+'_counter';var msgBox=document.id(newName);if(!msgBox)msgBox=new Element('div',{id:newName,name:newName}).inject(textArea,'after');if(className!=undefined)msgBox.set('class',className);if(msg!=undefined)msgBox.set('text',msg);}
FormValidator.hideCount=function(textArea){var msgBox=document.id(textArea.name+'_counter');if(msgBox)msgBox.destroy();}
function fieldClass(form,field,desc,dataType,allowNull,minLen,maxLen,minValue,maxValue){this.form=form;this.field=field;this.desc=desc;this.dataType=dataType;this.allowNull=allowNull;this.minLen=minLen;this.maxLen=maxLen;this.minValue=minValue;this.maxValue=maxValue;var newValue=this.trim(field.value);if(newValue!=field.value)field.value=newValue;this.value=field.value;}
fieldClass.prototype.trim=function(value){if(value.length>0){var trimed=value;while((trimed.length>0)&&(trimed.indexOf(' ')==0))
trimed=trimed.substr(1,trimed.length-1);while((trimed.length>0)&&(trimed.lastIndexOf(' ')==trimed.length-1))
trimed=trimed.substr(0,trimed.length-1);return trimed;}else
return value;}
fieldClass.prototype.check=function(){if(this.field.disabled)return true;if(this.dataType=="DATE"){return this.checkDate();}else if(this.checkValue()){if((this.dataType=="INT")||(this.dataType=="TINYINT")||(this.dataType=="SMALLINT")){if(!this.checkInt())return this.wrongType();else return this.checkRange();}else if(this.dataType=="DECIMAL"||this.dataType=="FLOAT"||this.dataType=="REAL"||this.dataType=="CURRENCY"){if(!this.checkFloat())return this.wrongType();else return this.checkRange();}else if(((this.dataType=="EMAIL")&&!this.isEmail())||((this.dataType=="DIGITS")&&!this.isDigit())){return this.wrongType();}
return true;}}
fieldClass.prototype.checkDate=function(){if(this.value==this.field.dateFormat)this.value='';if(this.checkValue()){if(this.isDate())return true;else return this.wrongType();}else return false;}
fieldClass.prototype.isDate=function(year,month,day){theDate=Date.parseDate(this.value,'%d/%m/%Y');if((values=this.value.split('/'))&&((values.length==3))){day=parseInt(values[0],10);month=parseInt(values[1],10);year=parseInt(values[2],10);return(day==theDate.getDate())&&(month==(theDate.getMonth()+1))&&(year==theDate.getFullYear());}}
fieldClass.prototype.checkValue=function(){if(this.value.length==0){if(!this.allowNull){this.addError('\''+this.desc+'\' can not be empty');}
return false;}else if(((this.minLen!=null)&&(this.value.length<this.minLen))||((this.maxLen!=null)&&(this.value.length>this.maxLen))){if(this.minLen==this.maxLen)
this.addError('\''+this.desc+'\' must have '+this.minLen+' characters');else
this.addError('\''+this.desc+'\' must have between '+this.minLen+' and '+this.maxLen+' characters');return false;}
return true;}
fieldClass.prototype.addError=function(message){this.form.addError(message,this.field);return false;}
fieldClass.prototype.wrongType=function(){this.addError('\''+this.desc+'\' have a wrong format');return false;}
fieldClass.prototype.checkRange=function(){if((this.minValue!=undefined)&&(this.maxValue!=undefined)){if(this.value<this.minValue||this.value>this.maxValue){return this.addError('\''+this.desc+'\' must be a number between '+this.minValue+' and '+this.maxValue+'');}}
return true;}
fieldClass.prototype.checkInt=function(){var num=parseInt(this.value,10);if(this.value==''+num){this.value=parseInt(this.value,10);return true;}}
fieldClass.prototype.checkFloat=function(){var frm_num=this.value;if(frm_num.charAt(0)=='.')frm_num='0'+frm_num
var lastPoint=frm_num.lastIndexOf('.');if(lastPoint>=0){var otherPoint=frm_num.lastIndexOf('.',lastPoint-1);if(otherPoint>=0)return false;frm_num=this.trimZeros(frm_num);}
var str_num=''+parseFloat(this.value);for(var i=str_num.length;i<frm_num.length;i++)str_num+='0';if(frm_num==str_num){this.value=parseFloat(this.value);return true;};}
fieldClass.prototype.trimZeros=function(str_num){var last_num=str_num.charAt(str_num.length-1);if(last_num=='.'){return str_num.substr(0,str_num.length-1);}else if(last_num!='0'){return str_num;}else{var new_num=str_num.substr(0,str_num.length-1);return this.trimZeros(new_num);}}
fieldClass.prototype.isEmail=function(){var pos=this.value.indexOf('@');return!(pos<1||pos==(this.value.length-1));}
fieldClass.prototype.isDigit=function(){var valid_digits=new String("01234567890");for(i=0;i<this.value.length;i++){if(valid_digits.indexOf(this.value.charAt(i))==-1)return false;}
return true;}
