// JavaScript Document - LAST MODIFICATION: 13-05-2008 v2.1
// MADE BY: RottenEye @ dbwt | www.dbwt.co.uk (Please don't remove this line)
// If you have any doubt referring to this document please don't be afraid, contact me at rotten_eye@iol.pt

if(typeof dbwt=='undefined')dbwt=new Object();dbwt.fvalidation=new Object();dbwt.fvalidation._parent=dbwt;dbwt.fvalidation.validElements=new Array('input','textarea','select');dbwt.fvalidation.validInputTypes=new Array('text','hidden','checkbox','radio','file','password');dbwt.fvalidation.cssForInvalid=' labelInvalidField';dbwt.fvalidation.isValidInputType=function(type){type=type.toLowerCase();for(var k in this.validInputTypes){if(this.validInputTypes[k]==type)return true;}
return false;}
dbwt.fvalidation.isValidElement=function(elem){elem=elem.toLowerCase();for(var k in this.validElements){if(this.validElements[k]==elem)return true;}
return false;}
dbwt.fvalidation.getLabel=function(lblId,returnObj){var label;if(document.getElementById(lblId)&&document.getElementById(lblId).tagName=='LABEL'){if(returnObj)
return document.getElementById(lblId);label=document.getElementById(lblId).innerHTML;label=label.replace(/<\/?[^>]+>/gi,"");if(label.substr(label.length-1,1)==":")
label=label.substring(0,label.length-1);return label;}
if(returnObj)return new Object();else return"?";}
dbwt.fvalidation.getValidationArray=function(vProperty){var props,temp;var porpsOutput=new Array();if(!vProperty)return false;vProperty=vProperty.replace(/\s/g,"");if(vProperty.substr(0,1)!=';'&&vProperty.length<=1)return false;if(vProperty.substr(vProperty.length-1,1)==";")
vProperty=vProperty.substr(0,vProperty.length-1);props=vProperty.split(';');for(var k in props){if(k==0)continue;temp=props[k].split(':');porpsOutput[k-1]=new Array();porpsOutput[k-1]['propName']=temp[0].toLowerCase();porpsOutput[k-1]['propValue']=temp[1].toLowerCase();}
return porpsOutput;}
dbwt.fvalidation.getFieldProperties=function(){var curElem,lblName;var index=0;for(var k=0,len=this.formObj.length;k<len;k++){curElem=this.formObj.elements[k];lblName=curElem.name+'_lbl';if(!this.isValidElement(curElem.tagName))continue;if(curElem.tagName=='INPUT')
if(!this.isValidInputType(curElem.type))continue;this.fieldsProps[index]=new Array();this.fieldsProps[index]['obj']=curElem;this.fieldsProps[index]['label']=lblName;this.fieldsProps[index]['name']=curElem.name;this.fieldsProps[index]['type']=curElem.type;this.fieldsProps[index]['value']=curElem.value;this.fieldsProps[index]['dis']=curElem.disabled;this.fieldsProps[index]['properties']=this.getValidationArray(curElem.getAttribute('validation'));if(this.markLabels)
this.unmarckLabel(this.getLabel(lblName,true));index++;}}
dbwt.fvalidation.marckLabel=function(_label){if(!_label.className)return;_label.className+=this.cssForInvalid;}
dbwt.fvalidation.unmarckLabel=function(_label){if(!_label.className)return;_label.className=_label.className.replace(this.cssForInvalid,'')}
dbwt.fvalidation.addErr=function(langKey,_label,MAXLEN){var tempMsg;if(MAXLEN){tempMsg=$LANG[langKey].replace(/##boxName##/,'"'+this.getLabel(_label)+'"');tempMsg=tempMsg.replace(/##MAXLEN##/,MAXLEN);}else{tempMsg=$LANG[langKey].replace(/##boxName##/,'"'+this.getLabel(_label)+'"');tempMsg=tempMsg.replace(/##MAXLEN##/,'');}
if(this.markLabels)
this.marckLabel(this.getLabel(_label,true));return this.errMsg+=tempMsg;}
dbwt.fvalidation.checkMail=function(mail){if(mail=="")return true;var caract=new Array('@','.','_','-','0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');var len=mail.length;var at_pos=mail.indexOf("@");var point_pos=mail.lastIndexOf(".");var good;if(!((point_pos>0&&at_pos>0)&&((point_pos==(len-3)&&at_pos<(len-4))||(point_pos==(len-4)&&at_pos<(len-5)))))
return false;for(var k=0,len=mail.length;k<len;k++){good=false;for(var i in caract){if(mail.substr(k,1)==caract[i]||mail.substr(k,1)==caract[i].toUpperCase()){good=true;break;}}
if(good==false)break;}
return good;}
dbwt.fvalidation.checkPhone=function(phone){if(phone=="")return true;var allowedChars=" 0123456789+()";var invalidSeq=new Array("  ","((","))");var temp="";for(var k=0,len=phone.length;k<len;k++){temp=phone.substr(k,1)+temp;for(var i in invalidSeq){if(temp.substr(0,invalidSeq[i].length)==invalidSeq[i])
return false;}
if(allowedChars.indexOf(phone.substr(k,1))===-1)return false;}
return true;}
dbwt.fvalidation.check_nNumber=function(number){if(number=="")return true;var allowedChars='0123456789';for(var k=0,len=number.length;k<len;k++){if(allowedChars.indexOf(number.substr(k,1))===-1)
return false;}
return true;}
dbwt.fvalidation.check_zNumber=function(number){if(number=="")return true;var allowedChars='0123456789-';for(var k=0,len=number.length;k<len;k++){if(allowedChars.indexOf(number.substr(k,1))===-1)
return false;}
return true;}
dbwt.fvalidation.check_rNumber=function(number){if(number=="")return true;var allowedChars='0123456789-,.';for(var k=0,len=number.length;k<len;k++){if(allowedChars.indexOf(number.substr(k,1))===-1)
return false;}
return true;}
dbwt.fvalidation.checkZipCode=function(code,format){var curCode,curFormat;code=code.replace(/\s/g,'');if(code=="")return true;if(code.length!=format.length)return false;for(var k=0,len=format.length;k<len;k++){curCode=code.substr(k,1);curFormat=format.substr(k,1);if((!isNaN(curFormat)&&isNaN(curCode))||(isNaN(curFormat)&&!isNaN(curCode))){return false;}else if(!isNaN(curFormat)&&curCode>curFormat){return false;}else if(isNaN(curFormat)&&curCode!==curFormat){return false;}}
return true;}
dbwt.fvalidation.valRequired=function(property,field){if(property['propValue']=='yes'&&field['value']=='')
return this.addErr('formMissingVal',field['label']);return 0;}
dbwt.fvalidation.type=function(property,field){switch(property['propValue']){case"mail":if(!this.checkMail(field['value']))
return this.addErr('formInvMail',field['label']);return 0;break;case"phone":if(!this.checkPhone(field['value']))
return this.addErr('formInvPhone',field['label']);return 0;break;case"n_number":if(!this.check_nNumber(field['value']))
return this.addErr('formInv_n_Numb',field['label']);return 0;break;case"z_number":if(!this.check_zNumber(field['value']))
return this.addErr('formInv_z_Numb',field['label']);return 0;break;case"real":if(!this.check_rNumber(field['value']))
return this.addErr('formInv_r_Numb',field['label']);return 0;break;case"zipcode-pt":if(!this.checkZipCode(field['value'],'9999'))
return this.addErr('formInv_zipCode_pt',field['label']);return 0;break;case"zipcode-pt+3":if(!this.checkZipCode(field['value'],'9999-999'))
return this.addErr('formInv_zipCode_pt3',field['label']);return 0;break;case"zipcode-us":if(!this.checkZipCode(field['value'],'99999'))
return this.addErr('formInv_zipCode_us',field['label']);return 0;break;case"zipcode-us+4":if(!this.checkZipCode(field['value'],'99999-9999'))
return this.addErr('formInv_zipCode_us4',field['label']);return 0;break;default:alert($LANG['scriptFatalErr']);break;}
return 0;}
dbwt.fvalidation.minLen=function(property,field){if(field['value']!=''&&field['value'].length<property['propValue'])
return this.addErr('formInvLength',field['label'],property['propValue']);return 0;}
dbwt.fvalidation.maxLen=function(property,field){if(field['value'].length>property['propValue'])
return this.addErr('formInvLength2',field['label'],property['propValue']);return 0;}
dbwt.fvalidation.maxLines=function(property,field){var counter=0;var lineBreaks=new Array("\n","\r\n","<br>","<br/>","<br />");for(var k=0,len=field['value'].length;k<len;k++){for(var i in lineBreaks){if(field['value'].substr(k,lineBreaks[i].length)==lineBreaks[i])
counter++;}}
if(counter>property['propValue'])
return this.addErr('formInvLineBk',field['label'],' (max='+property['propValue']+')');return 0;}
dbwt.fvalidation.minValue=function(property,field){if(parseInt(field['value'])<parseInt(property['propValue']))
return this.addErr('formMinExceeded',field['label'],property['propValue']);return 0;}
dbwt.fvalidation.maxValue=function(property,field){if(parseInt(field['value'])>parseInt(property['propValue']))
return this.addErr('formMaxExceeded',field['label'],property['propValue']);return 0;}
dbwt.fvalidation.checkPassword=function(property,field){var field2=null;for(var k in this.fieldsProps){if(this.fieldsProps[k]['type']=='password'&&this.fieldsProps[k]['name']==property['propValue']){field2=k;break;}}
if(field['value']===this.fieldsProps[field2]['value'])
return 0;else if(field2===null)
return this.addErr('formPass2NotFound',field['label'],property['propValue']);else
return this.addErr('formPassDifPass2',field['label'],property['propValue']);}
dbwt.fvalidation.validateFields=function(){var curProps,fieldInfo;for(var i in this.fieldsProps){if(this.fieldsProps[i]['properties']===false)continue;if(this.ignoreDisabled&&this.fieldsProps[i]['dis']==true)continue;curProps=new Array();curProps=this.fieldsProps[i]['properties'];fieldInfo=this.fieldsProps[i];fieldInfo.pop();for(var k in curProps){switch(curProps[k]['propName']){case'required':this.valRequired(curProps[k],fieldInfo);break;case'type':this.type(curProps[k],fieldInfo);break;case'maxlen':this.maxLen(curProps[k],fieldInfo);break;case'minlen':this.minLen(curProps[k],fieldInfo);break;case'maxlines':this.maxLines(curProps[k],fieldInfo);break;case'min':this.minValue(curProps[k],fieldInfo);break;case'max':this.maxValue(curProps[k],fieldInfo);break;case'password':this.checkPassword(curProps[k],fieldInfo);break;case"transform":break;default:alert($LANG['scriptFatalErr']);break;}}}}
dbwt.fvalidation.execTransformation=function(){var props,tempField;for(var k in this.fieldsProps){if(this.fieldsProps[k]['properties']===false)continue;properties=this.fieldsProps[k]['properties']
for(var i in properties){if(properties[i]['propName']!='transform')continue;tempField=this.fieldsProps[k]['obj'];switch(properties[i]['propValue']){case'disable':tempField.disabled=true;break;case'enable':tempField.disabled=false;break;case'clean':tempField.value='';break;default:alert($LANG['scriptFatalErr']);break;}}}}
dbwt.fvalidation.reset=function(){this.defaultForm='form1';this.autoSubmit=false;this.showAlert=true;this.execTransform=true;this.formObj=null;this.fieldsProps=new Array();this.errMsg='';this.ignoreDisabled=true;this.supportTinyMCE=true;this.markLabels=true;}
dbwt.fvalidation.validate=function(form_name,confs){this.reset();try{if(!form_name)form_name=this.defaultForm;for(var k in confs)this[k]=confs[k];if(typeof form_name=='string')
this.formObj=eval('window.document.'+form_name);else
this.formObj=form_name
if(!this.formObj.name)return this.invalidForm(form_name);if(typeof tinyMCE=='object'&&this.supportTinyMCE==true)
tinyMCE.triggerSave();this.getFieldProperties();this.validateFields();if(this.errMsg){this.errMsg=$LANG['formTopMsgErr']+this.errMsg;if(this.showAlert)
alert(this.errMsg);return false;}
if(this.execTransform)
this.execTransformation();this.fieldsProps=null;if(this.autoSubmit)
return this.formObj.submit();return true;}catch(err){return false;}}