/*
 *
 * Copyright (c) 2004-2005 by Zapatec, Inc.
 * http://www.zapatec.com
 * 1700 MLK Way, Berkeley, California,
 * 94709, U.S.A.
 * All rights reserved.
 *
 *
 */
Zapatec.formPath = Zapatec.getPath("Zapatec.Form");

Zapatec.L10n=function(objArgs){Zapatec.L10n.SUPERconstructor.call(this,objArgs);};Zapatec.L10n.id='Zapatec.L10n';Zapatec.inherit(Zapatec.L10n,Zapatec.Widget);Zapatec.L10n.prototype.init=function(objArgs){Zapatec.L10n.SUPERclass.init.call(this,objArgs);this.langStr=this.config.lang;if(this.config.langCountryCode&&this.config.langCountryCode.length>0){this.langStr+="_"+this.config.langCountryCode;}
if(this.config.langEncoding&&this.config.langEncoding.length>0){this.langStr+="-"+this.config.langEncoding;}
if(this.config.lang&&this.config.lang.length>0&&!(Zapatec.Langs[this.config.langId]&&Zapatec.Langs[this.config.langId][this.langStr])){Zapatec.Log({description:"No language data founded for language "+
this.config.lang+(this.config.langCountryCode?" and country code "+this.config.langCountryCode:"")+(this.config.langEncoding?" and encoding "+this.config.langEncoding:"")});this.config.lang=null;this.config.langCountryCode=null;this.config.langEncoding=null;this.langStr=null;}};Zapatec.L10n.prototype.reconfigure=function(objArgs){Zapatec.L10n.SUPERclass.reconfigure.call(this,objArgs);if(objArgs.lang||objArgs.langCountryCode||objArgs.langEncoding){this.langStr=this.config.lang;if(this.config.langCountryCode&&this.config.langCountryCode.length>0){this.langStr+="_"+this.config.langCountryCode;}
if(this.config.langEncoding&&this.config.langEncoding.length>0){this.langStr+="-"+this.config.langEncoding;}}
if(this.config.lang&&this.config.lang.length>0&&!(Zapatec.Langs[this.config.langId]&&Zapatec.Langs[this.config.langId][this.langStr])){Zapatec.Log({description:this.config.lang+(this.config.langCountryCode?" and country code "+this.config.langCountryCode:"")+(this.config.langEncoding?" and encoding "+this.config.langEncoding:"")});this.config.lang=null;this.config.langEncoding=null;this.langStr=null;}};Zapatec.L10n.prototype.configure=function(objArgs){this.defineConfigOption('langId');this.defineConfigOption('lang',"eng");this.defineConfigOption('langCountryCode');this.defineConfigOption('langEncoding');Zapatec.L10n.SUPERclass.configure.call(this,objArgs);};Zapatec.L10n.prototype.getMessage=function(key){if(arguments.length==0){return null;}
if(!Zapatec.Langs[this.config.langId]||!Zapatec.Langs[this.config.langId][this.langStr]){return key;}
var res=Zapatec.Langs[this.config.langId][this.langStr][key];if(arguments.length>1&&typeof(res)=="string"){for(var ii=1;ii<arguments.length;ii++){var re=new RegExp("(^|([^\\\\]))\%"+ii);res=res.replace(re,"$2"+arguments[ii]);}}
return res;}
Zapatec.L10n.prototype.sprintf=function(){if(!Zapatec.Langs[this.config.langId]||!Zapatec.Langs[this.config.langId][this.langStr]){return null;}
return Zapatec.Utils.sprintf(arguments,this.config.lang);}
Zapatec.Utils.sprintf=function(args,lang){if(!args||args.length<1||!RegExp){return;}
var str=args[0];var re=/([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;var a=b=[];var numSubstitutions=0;var numMatches=0;while(a=re.exec(str)){var leftpart=a[1];var pPad=a[2];var pJustify=a[3];var pMinLength=a[4];var pPrecision=a[5];var pType=a[6];var rightPart=a[7];var subst="";numMatches++;if(pType=='%'){subst='%';}else{numSubstitutions++;if(numSubstitutions>=args.length){Zapatec.Log({description:'Error! Not enough function args ('+(args.length-1)+', excluding the string)\nfor the number of substitution parameters in string ('+numSubstitutions+' so far).'});}
var param=args[numSubstitutions];var pad='';if(pPad&&pPad.substr(0,1)=="'"){pad=leftpart.substr(1,1);}else if(pPad){pad=pPad;}
var justifyRight=true;if(pJustify&&pJustify==="-"){justifyRight=false;}
var minLength=-1;if(pMinLength){minLength=parseInt(pMinLength);}
var precision=-1;if(pPrecision&&pType=='f'){precision=parseInt(pPrecision.substring(1));}
subst=param;if(pType=='b'){subst=parseInt(param).toString(2);}else if(pType=='c'){subst=String.fromCharCode(parseInt(param));}else if(pType=='d'){subst=parseInt(param)?parseInt(param):0;}else if(pType=='u'){subst=Math.abs(param);}else if(pType=='f'){subst=(precision>-1)?Math.round(parseFloat(param)*Math.pow(10,precision))/Math.pow(10,precision):parseFloat(param);if(lang&&Zapatec.Langs.conversionRules&&Zapatec.Langs.conversionRules[lang]&&Zapatec.Langs.conversionRules[lang].floatSeparator){subst=subst.toString().replace(/\./,Zapatec.Langs.conversionRules[lang].floatSeparator);}}else if(pType=='o'){subst=parseInt(param).toString(8);}else if(pType=='s'){subst=param;}else if(pType=='x'){subst=(''+parseInt(param).toString(16)).toLowerCase();}else if(pType=='X'){subst=(''+parseInt(param).toString(16)).toUpperCase();}}
str=leftpart+subst+rightPart;}
return str;}
Zapatec.Utils.createNestedHash(Zapatec,["Langs","Zapatec.Form","eng"],{'initializeError':'Form already initialized!','loadingForm':'loading form','noFormError':"Couldn't find form",'submitOneError':'There is 1 error.','submitManyErrors':'There are %1 errors.','isRequiredError':'This field is required','maskNotFilledError':'Does not conform to mask %1','noSuchAutoCompleteValueError':'No such value','invalidURLError':'Invalid URL','invalidEmailError':'Invalid email address','invalidCreditCardError':'Invalid credit card number','invalidUSPhoneError':'Invalid US phone number','invalidInternationalPhoneError':'Invalid international phone number','invalidUSZipError':'Invalid US zip code','invalidDateError':'Invalid date','invalidIntError':'Not an integer','invalidFloatError':'Not a float','ajaxDebugSeparator':'-----------------------','ajaxDebugSubmitTitle':'Sending request for AJAX submit form.','ajaxDebugValidateTitle':'Sending request for AJAX validate field %1.','ajaxDebugFillTitle':'Sending request for AJAX fill for field %1.','ajaxDebugQuery':'Query: %1','ajaxDebugResponse':'Response received: %1','ajaxDebugResponseError':'Error response received: %1','ajaxSubmitCantParseError':"Can't parse received JSON: %1",'ajaxSubmitNoResponseError':'No response','ajaxValidateCantParseError':"Can't parse received JSON: %1",'ajaxValidateNoResponseError':'No response','ajaxValidateValidationError':'This field is not valid','ajaxFillCantParseError':"Can't parse received JSON: %1",'ajaxFillNoResponseError':'No response','ajaxFillGeneralError':"Can't retrieve fill values",'ajaxSuggestCantParseError':"Can't parse received JSON: %1",'ajaxSuggestNoResponseError':'No response','ajaxSuggestGeneralError':"Can't retrieve value"});Zapatec.Form=function(){var objArgs={};switch(arguments.length){case 1:objArgs=arguments[0];break;case 2:objArgs=arguments[1];objArgs.form=arguments[0];break;}
Zapatec.Form.SUPERconstructor.call(this,objArgs);}
Zapatec.Form.id="Zapatec.Form";Zapatec.inherit(Zapatec.Form,Zapatec.L10n);Zapatec.Form.prototype.init=function(objArgs){this.container=null;this.changeHandlers=[];Zapatec.Form.SUPERclass.init.call(this,objArgs);if(this.config.form.zpForm!=null){Zapatec.Log({description:this.getMessage('initializeError')});return null;}
this.container=this.config.form;this.createProperty(this.container,"zpForm",this);this.addCircularRef("container");this.container.className=this.container.className.replace(/\bzpForm.*?\b/,"")+" "+this.getClassName({prefix:"zpForm"});if(this.config.hideUntilThemeLoaded){this.showLoader(this.getMessage('loadingForm'));}
var self=this;var oldOnSubmit=this.container.onsubmit||function(){return true};if(typeof(this.config.asyncSubmitFunc)=='function'){if(this.config.serverCallback){var targetName="form-iframe-"+this.id;var tmp=document.createElement("span");tmp.innerHTML='<iframe name="'+targetName+'" style="display: none"></iframe>';var iframe=tmp.firstChild;this.container.parentNode.insertBefore(iframe,this.container);this.container.setAttribute("target",targetName);var hidden=document.createElement("input");hidden.setAttribute("type","hidden");hidden.setAttribute("name",this.config.serverCallback);hidden.setAttribute("value","window.parent.Zapatec.Widget.getWidgetById("+this.id+").processAsyncResponse");this.container.appendChild(hidden);}
this.container.onsubmit=function(ev){return self.asyncSubmit(ev)&&oldOnSubmit()};}else{this.container.onsubmit=function(ev){return self.submit(ev)&&oldOnSubmit()};}
this.addCircularRef(this.container,"onsubmit");this.container.onreset=function(){setTimeout(function(){self.reset()},1);};this.addCircularRef(this.container,"onreset");var focusedFlag=false;var els=[];for(var ii=0;ii<this.container.elements.length;ii++){els.push(this.container.elements[ii]);}
for(var ii=0;ii<els.length;ii++){var el=els[ii];if(!Zapatec.Form.Utils.ignoreField(el)){var zpField=new Zapatec.Form.Field({form:this,field:el,langId:this.config.langId,lang:this.config.lang,langCountryCode:this.config.langCountryCode,langEncoding:this.config.langEncoding});if(this.config.startupFocusPosition){if(el.zpFormField!=null&&focusedFlag==false&&typeof(el.focus)!='undefined'&&(typeof(el.type)=='undefined'||typeof(el.type)!='undefined'&&el.type.toLowerCase()!='hidden')&&!el.disabled&&!el.readOnly){if(this.config.startupFocusPosition=="firstField"||(this.config.startupFocusPosition=="firstRequiredField"&&zpField.hasFeature("zpFormRequired"))||(this.config.startupFocusPosition=="firstIncorrectField"&&zpField.validate(true)!=null&&zpField.validate(true).length>0)){var tmp=el.getAttribute("autocomplete");try{el.setAttribute('autocomplete','off');el.focus();focusedFlag=true;}catch(e){}
el.setAttribute('autocomplete',tmp);}}}}}
var childElements=this.container.all?this.container.all:this.container.getElementsByTagName("*");for(var ii=childElements.length-1;ii>=0;ii--){var el=childElements[ii];Zapatec.Form.Utils.initMultipleField(el,true,this);this.initConditionalField(el);}
if(Zapatec.windowLoaded){this.formLoaded();}else{Zapatec.Utils.addEvent(window,"load",new Function("Zapatec.Widget.callMethod("+this.id+", 'formLoaded')"));}}
Zapatec.Form.prototype.configure=function(objArgs){this.defineConfigOption('langId',"Zapatec.Form");this.defineConfigOption('form',null);this.defineConfigOption('statusImgPos','beforeField');this.defineConfigOption('showErrors',null);this.defineConfigOption('showErrorsOnSubmit',true);this.defineConfigOption('submitErrorFunc',Zapatec.Form.submitErrorFunc);this.defineConfigOption('submitValidFunc',null);this.defineConfigOption('asyncSubmitFunc',null);this.defineConfigOption('strict',false);this.defineConfigOption('asyncTheme',true);this.defineConfigOption('theme',"alternate");this.defineConfigOption('ajaxDebugFunc',null);this.defineConfigOption('dropDownTheme',"default");this.defineConfigOption('hideUntilThemeLoaded',false);this.defineConfigOption('putTabIndexesOnError',true);this.defineConfigOption('startupFocusPosition',"firstField");this.defineConfigOption('displayErrorWhileTyping',true);this.defineConfigOption('multipleCallback',Zapatec.Form.Utils.generateMultipleId);this.defineConfigOption('serverCallback');this.defineConfigOption('disableButtonsWhenAsyncSubmit',true);this.defineConfigOption('conditionalEvents');Zapatec.Form.SUPERclass.configure.call(this,objArgs);this.config.form=Zapatec.Widget.getElementById(this.config.form);if(this.config.form==null||!this.config.form.nodeName||this.config.form.nodeName.toLowerCase()!="form"){Zapatec.Log({description:this.getMessage('noFormError')});throw this.getMessage('noFormError');return null;}}
Zapatec.Form.prototype.reconfigure=function(objArgs){if(objArgs.theme){Zapatec.Utils.removeClass(this.container,this.getClassName({prefix:"zpForm"}));}
Zapatec.Form.SUPERclass.reconfigure.call(this,objArgs);Zapatec.Utils.addClass(this.container,this.getClassName({prefix:"zpForm"}));};Zapatec.Form.prototype.asyncSubmit=function(){var self=this;if(this.processing==true){return false;}
if(this.config.showErrorsOnSubmit&&typeof(this.config.submitErrorFunc)=='function'&&!this.submit()){return false;}
var strUrl=this.container.action;if(!strUrl){return false;}
if(this.config.disableButtonsWhenAsyncSubmit){this.toggleSubmits(true);this.processing=true;}
if(this.config.serverCallback){return true;}
var arrContent=[];var objFormElements=this.container.elements;for(var iElm=0;iElm<objFormElements.length;iElm++){var formEl=objFormElements[iElm];if(!formEl.name||formEl.disabled){continue;}
if(formEl.nodeName.toLowerCase()=='input'&&(formEl.type.toLowerCase()=='radio'||formEl.type.toLowerCase()=='checkbox')&&!formEl.checked){continue;}
arrContent.push(formEl.name+'='+escape(formEl.value));}
var strMethod=this.container.method.toUpperCase();var strContent=arrContent.join('&');if(strMethod!='POST'){if(strMethod===''){strMethod="GET";}
strUrl+='?'+strContent;strContent=null;}
if(this.config.ajaxDebugFunc){this.config.ajaxDebugFunc(this.getMessage('ajaxDebugSeparator'));this.config.ajaxDebugFunc(this.getMessage('ajaxDebugSubmitTitle'));this.config.ajaxDebugFunc(strMethod+" "+strUrl);this.config.ajaxDebugFunc(this.getMessage('ajaxDebugQuery',strContent));}
Zapatec.Transport.fetch({url:strUrl,method:strMethod,contentType:this.container.encoding,content:strContent,onLoad:function(objText){if(self.config.ajaxDebugFunc){self.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponse',objText.responseText));}
if(self.config.disableButtonsWhenAsyncSubmit){self.processing=false;self.toggleSubmits(false);}
if(objText.responseText==null){Zapatec.Log({description:self.getMessage('ajaxSubmitNoResponseError',objText.responseText)});return null;}
var objResponse=Zapatec.Transport.parseJson({strJson:objText.responseText});if(objResponse==null){Zapatec.Log({description:self.getMessage('ajaxSubmitCantParseError',objText.responseText)});return null;}
return self.processAsyncResponse(objResponse);},onError:function(objError){if(self.config.disableButtonsWhenAsyncSubmit){self.processing=false;self.toggleSubmits(false);}
var strError='';if(objError.errorCode){strError=objError.errorCode+' ';}
strError+=objError.errorDescription;if(self.config.ajaxDebugFunc){self.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponseError',strError));}
if(self.config.showErrorsOnSubmit&&typeof(self.config.submitErrorFunc)=='function'){self.config.submitErrorFunc({serverSide:true,generalError:strError});}}});return false;}
Zapatec.Form.prototype.processAsyncResponse=function(objResponse){if(this.config.disableButtonsWhenAsyncSubmit){this.processing=false;this.toggleSubmits(false);}
if(objResponse){if(objResponse.success){this.config.asyncSubmitFunc(objResponse.callbackArgs);}else if(this.config.showErrorsOnSubmit){var arrFieldErrors=[];var boolFocusSet=false;if(objResponse.fieldErrors){for(var strFieldName in objResponse.fieldErrors){for(var iElm=0;iElm<this.container.elements.length;iElm++){var objField=this.container.elements[iElm];if(objField.name&&objField.name==strFieldName){arrFieldErrors.push({field:objField,errorMessage:objResponse.fieldErrors[strFieldName],validator:''});if(objField.zpFormField!=null){objField.zpFormField.setImageStatus(objResponse.fieldErrors[strFieldName]);}
break;}}}}
if(typeof(this.config.submitErrorFunc)=='function'){this.config.submitErrorFunc({serverSide:true,generalError:objResponse.generalError||'',fieldErrors:arrFieldErrors});}}}else if(this.config.showErrorsOnSubmit&&typeof(this.config.submitErrorFunc)=='function'){this.config.submitErrorFunc({serverSide:true,generalError:this.getMessage('ajaxSubmitNoResponseError')});}}
Zapatec.Form.prototype.toggleSubmits=function(disable){var inputs=this.container.getElementsByTagName("input");for(var ii=0;ii<inputs.length;ii++){if(inputs[ii].type=="submit"){inputs[ii].disabled=disable==true;}}}
Zapatec.Form.prototype.reset=function(){for(var ii=0;ii<this.container.elements.length;ii++){var field=this.container.elements[ii].zpFormField;if(field!=null){field.setValueFromField();field.firstRun=true;field.blur();}}
this.runChangeHandlers();this.fireEvent("reset");}
Zapatec.Form.prototype.submit=function(){var errors=this.validate(false);if(errors!=null&&errors.length>0&&this.config.showErrorsOnSubmit&&typeof(this.config.submitErrorFunc)=='function'){this.config.submitErrorFunc({serverSide:false,generalError:errors.length==1?this.getMessage('submitOneError'):this.getMessage('submitManyErrors',errors.length),fieldErrors:errors});try{errors[0].field.focus();}catch(e){}
return false;}
if(typeof(this.config.submitValidFunc)=='function'){this.config.submitValidFunc();}
this.fireEvent("submit");return true;}
Zapatec.Form.prototype.validate=function(onlyValidate){if(typeof(onlyValidate)=="undefined"){onlyValidate=true;}
var valid=true;var tabIndex=1;var errors=[];for(var ii=0;ii<this.container.elements.length;ii++){var el=this.container.elements[ii];if(el.zpFormField==null){continue;}
if(!onlyValidate){el.zpFormField.firstRun=false;}
var validate=el.zpFormField.validate(onlyValidate);var fieldValid=(validate==null||validate.length==0);if(this.config.putTabIndexesOnError){if(fieldValid){el.removeAttribute("tabIndex");el.tabIndex=null;}else{el.tabIndex=tabIndex;el.setAttribute("tabIndex",tabIndex++);}}
if(!fieldValid){for(var jj=0;jj<validate.length;jj++){errors.push(validate[jj])}}
valid=valid&&fieldValid;}
if(errors.length==0){errors=null;}
return errors;}
Zapatec.Form.prototype.initConditionalField=function(field){var md=null;if(field.className&&(md=field.className.match(/zpForm(Display|Visible)When=([^\s]+)/))){var func=eval(md[2]);if(typeof(func)!="function"){return null;}
var handler=null;var self=this;if(md[1]=='Display'){handler=function(){Zapatec.Form.Utils.toggleFormElements(field,func(),false);if(self.config.strict){self.toggleSubmits(self.validate()!=null)}};}else if(md[1]=='Visible'){handler=function(){Zapatec.Form.Utils.toggleFormElements(field,func(),true);if(self.config.strict){self.toggleSubmits(self.validate()!=null)}};}
if(handler!=null){handler();if(this.config.conditionalEvents&&this.config.conditionalEvents.length>0){for(var ii=0;ii<this.config.conditionalEvents.length;ii++){this.addEventListener(this.config.conditionalEvents[ii],handler);}}else{this.addChangeHandler(handler);}}}}
Zapatec.Form.prototype.addChangeHandler=function(func){if(typeof(func)=='string'){func=eval(func);}
if(typeof(func)=='function'){this.changeHandlers.push(func);this.runChangeHandlers();if(this.config.strict){this.toggleSubmits(this.validate()!=null);}
return true;}
return false;}
Zapatec.Form.prototype.runChangeHandlers=function(){for(var ii=0;ii<this.changeHandlers.length;ii++){this.changeHandlers[ii]();}}
Zapatec.Form.prototype.formLoaded=function(){for(var ii=0;ii<this.container.elements.length;ii++){var zpField=this.container.elements[ii].zpFormField;if(zpField!=null){zpField.setValueFromField(true);}}
this.runChangeHandlers();}
Zapatec.Form.prototype.destroy=function(){for(var ii=0;ii<this.container.elements.length;ii++){var field=this.container.elements[ii];if(field.zpFormField){field.zpFormField.destroy();}}
this.discard();}
Zapatec.Form.setupAll=function(params){var forms=document.getElementsByTagName('form');if(!params){params={};}
if(!params.startupFocusPosition){params.startupFocusPosition=null;}
if(forms&&forms.length){for(var ff=forms.length-1;ff>=0;ff--){if(forms[ff].zpForm){continue;}
var arrMatch=forms[ff].className.match(/zpForm(\S*)/);if(arrMatch){var strThemeName=arrMatch[1];var objConfig=Zapatec.Utils.clone(params);if((objConfig.theme==null||objConfig.theme=="")&&strThemeName){objConfig.theme=strThemeName;}
objConfig.form=forms[ff];new Zapatec.Form(objConfig);}}}};Zapatec.Form.submitErrorFunc=function(objErrors){var message=objErrors.generalError+'\n';if(objErrors.fieldErrors&&objErrors.fieldErrors.length){for(var ii=0;ii<objErrors.fieldErrors.length;ii++){message+=(ii+1)+': Field '+objErrors.fieldErrors[ii].field.name+' '+objErrors.fieldErrors[ii].errorMessage+"\n";}
message=message.substr(0,message.length-1);objErrors.fieldErrors[0].field.focus();}
alert(message);}
Zapatec.Form.ignoreMark="zpFormInternalEl";Zapatec.Form.Field=function(objArgs){Zapatec.Form.SUPERconstructor.call(this,objArgs);}
Zapatec.Form.Field.id="Zapatec.Form.Field";Zapatec.inherit(Zapatec.Form.Field,Zapatec.L10n);Zapatec.Form.Field.prototype.configure=function(objArgs){this.defineConfigOption('theme',null);this.defineConfigOption('form',null);this.defineConfigOption('field',null);this.defineConfigOption('langId',"Zapatec.Form");Zapatec.Form.SUPERclass.configure.call(this,objArgs);this.config.theme=null;this.config.form=Zapatec.Widget.getElementById(this.config.form);this.config.field=Zapatec.Widget.getElementById(this.config.field);}
Zapatec.Form.Field.prototype.reconfigure=function(objArgs){Zapatec.Form.SUPERclass.reconfigure.call(this,objArgs);};Zapatec.Form.Field.prototype.init=function(objArgs){if(!objArgs||Zapatec.Form.Utils.ignoreField(objArgs.field)){return null;}
this.autoCompleteOptions=[];this.form=null;this.state={};this.validators={};this.keyPressCounter=0;this.firstRun=true;this.chars=null;this.enteredValue=null
this.dropDown=null;this.isBooleanField=false;this.isEditing=false;Zapatec.Form.SUPERclass.init.call(this,objArgs);this.form=this.config.form;this.field=this.config.field;this.validators=Zapatec.Form.Utils.getTokens(this.field.className," ");this.isBooleanField=(this.field.nodeName.toLowerCase()=='input'&&(this.field.type.toLowerCase()=='radio'||this.field.type.toLowerCase()=='checkbox'));var md=null;if(md=this.field.className.match(/zpFormAllowed-(\S+)/)){if(this.validators['zpFormAllowedChars']){this.validators['zpFormAllowedChars']+='\\'+(md[1]).split('').join('\\');}else{this.validators['zpFormAllowedChars']='\\'+(md[1]).split('').join('\\');}}
if(typeof(this.validators['zpFormAllowedChars'])!='undefined'&&this.getFeature('zpFormAllowedChars')==null){this.validators['zpFormAllowedChars']="";}
if((this.hasFeature("zpFormAutoComplete")||this.hasFeature("zpFormAutoCompleteStrict"))&&this.field.nodeName.toUpperCase()=="SELECT"){var input=document.createElement('input');for(var ii=0;ii<this.field.attributes.length;ii++){var attr=this.field.attributes[ii];if(attr.name=='class'){input.className=this.field.getAttribute(attr.name);}else{input.setAttribute(attr.name,this.field.getAttribute(attr.name));}}
for(var ii=0;ii<this.field.options.length;ii++){this.autoCompleteOptions.push(this.field.options[ii].innerHTML);}
if(this.field.selectedIndex!=null){if(this.field.options[this.field.selectedIndex].value!=null&&this.field.options[this.field.selectedIndex].value!=""){val=this.field.options[this.field.selectedIndex].value;}else{val=this.field.options[this.field.selectedIndex].innerHTML;}
input.value=val;input.setAttribute("value",val);}
Zapatec.Utils.insertAfter(this.field,input);Zapatec.Utils.destroy(this.field);input.type='text';this.field=input;}
if(this.hasFeature("zpFormAutoComplete")||this.hasFeature("zpFormAutoCompleteStrict")||this.hasFeature("zpFormSuggest")){this.field.setAttribute("autocomplete","off");this.field.autoComplete="off";}
this.createProperty(this.field,"zpFormField",this);var oldOnKeyDown=this.field.onkeydown||function(){return true;};Zapatec.Utils.createProperty(this.field,"onkeydown",function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;var res=oldOnKeyDown();return zpField.keydown(ev)&&res;});var oldOnKeyPress=this.field.onkeypress||function(){return true;};Zapatec.Utils.createProperty(this.field,"onkeypress",function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;var res=oldOnKeyPress();return zpField.keypress(ev)&&res;});Zapatec.Utils.addEvent(this.field,'keyup',function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;return zpField.keyup(ev);});Zapatec.Utils.addEvent(this.field,'focus',function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;return zpField.focus(ev);});Zapatec.Utils.addEvent(this.field,'blur',function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;return zpField.blur(ev);});if(this.field.nodeName.toLowerCase()=='select'){Zapatec.Utils.addEvent(this.field,'change',function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;return zpField.valueChanged(ev);});}
var changeEvent=null;if(Zapatec.is_ie){changeEvent='paste';}else if(Zapatec.is_gecko){changeEvent='input';}else{changeEvent='change';}
Zapatec.Utils.addEvent(this.field,changeEvent,function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;return zpField.valueChanged();});var self=this;if(this.isBooleanField){var onChangeFunc=function(ev){var zpField=Zapatec.Utils.getTargetElement(ev).zpFormField;return zpField.booleanChanged();}
Zapatec.Utils.addEvent(this.field,'change',onChangeFunc);Zapatec.Utils.addEvent(this.field,'click',onChangeFunc);}
this.statusImg1=Zapatec.Utils.createElement('span');this.statusImg1.className=Zapatec.Form.ignoreMark+
(this.hasFeature("zpFormRequired")?' zpIsRequired':' zpNotRequired');this.statusImg1.id="zpFormField"+this.id+"StatusImg1";this.statusImg2=this.statusImg1.appendChild(Zapatec.Utils.createElement('span'));this.statusImg2.className=Zapatec.Form.ignoreMark;this.statusImg2.id="zpFormField"+this.id+"StatusImg2";this.statusImg3=this.statusImg2.appendChild(Zapatec.Utils.createElement('span'));this.statusImg3.className=Zapatec.Form.ignoreMark;this.statusImg3.id="zpFormField"+this.id+"StatusImg3";this.statusImg4=this.statusImg3.appendChild(Zapatec.Utils.createElement('span'));this.statusImg4.className=Zapatec.Form.ignoreMark;this.statusImg4.id="zpFormField"+this.id+"StatusImg4";this.statusImg5=this.statusImg4.appendChild(Zapatec.Utils.createElement('span'));this.statusImg5.className=Zapatec.Form.ignoreMark;this.statusImg5.id="zpFormField"+this.id+"StatusImg5";this.statusImg=this.statusImg5.appendChild(Zapatec.Utils.createElement('span'));this.statusImg.className=Zapatec.Form.ignoreMark+' zpStatusImg';this.statusImg.id="zpFormField"+this.id+"StatusImg";this.addCircularRef(this,"statusImg");this.addCircularRef(this,"statusImg5");this.addCircularRef(this,"statusImg4");this.addCircularRef(this,"statusImg3");this.addCircularRef(this,"statusImg2");this.addCircularRef(this,"statusImg1");this.errorText=Zapatec.Utils.createElement('span');this.errorText.id="zpFormField"+this.id+"ErrorText";this.errorText.className=Zapatec.Form.ignoreMark+' zpFormError';if(this.isBooleanField){if(this.field.type.toLowerCase()=="checkbox"){this.field.className+=" zpFormCheckbox";this.statusImg.className=Zapatec.Form.ignoreMark+" zpCheckboxStatusImg"}else if(this.field.type.toLowerCase()=="radio"){this.field.className+=" zpFormRadio";this.statusImg.className=Zapatec.Form.ignoreMark+" zpRadioStatusImg"}}
var lastNode=this.field;if(this.form.config.statusImgPos=='afterField'){Zapatec.Utils.insertAfter(this.field,this.statusImg1);lastNode=this.statusImg1;}else if(this.form.config.statusImgPos=='beforeField'){this.field.parentNode.insertBefore(this.statusImg1,this.field);}
if(this.form.config.showErrors=='afterField'){Zapatec.Utils.insertAfter(this.field,this.errorText);lastNode=this.errorText;}else if(this.form.config.showErrors=='beforeField'){this.field.parentNode.insertBefore(this.errorText,this.field);}
if(this.hasFeature("zpFormMask")){var mask=this.getFeature("zpFormMask");var maskChars=mask.split('');this.chars=[];this.enteredValue=[]
for(var ii=0;ii<maskChars.length;ii++){var tmp=null;switch(maskChars[ii]){case"0":tmp="[0-9]";break;case"L":tmp="[a-zA-Z]";break;case"A":tmp="[0-9a-zA-Z]";break;case"&":tmp=".";break;case"\\":i++;if(i>=maskChars.length)
break;default:this.chars.push(maskChars[ii]);this.enteredValue.push(maskChars[ii]);}
if(tmp!=null){var re=new RegExp("^"+tmp+"$");this.chars.push(re);this.enteredValue.push(null);}}}
if(this.hasFeature("zpFormMultiple")){this.createProperty(this.field,"zpLastNode",lastNode);}
if(this.hasFeature("zpFormSuggest")||this.hasFeature("zpFormAutoComplete")||this.hasFeature("zpFormAutoCompleteStrict")){if(typeof(Zapatec.DropDown)=='undefined'){Zapatec.Transport.loadJS({module:'dropdown',async:true,onLoad:function(){self.initDropDown();}});}else{this.initDropDown();}}
this.setValueFromField(true);}
Zapatec.Form.Field.DELAYED_INTERVAL=1000;Zapatec.Form.Field.prototype.initDropDown=function(){var self=this;var arrow=Zapatec.Utils.createElement("span");arrow.className=Zapatec.Form.ignoreMark+" dropDownArrow";arrow.id="zpFormField"+this.id+"DropDownArrow";this.createProperty(arrow,"onclick",function(){self.autoCompleteValue(self.getAutoCompleteOptions(true));self.suggestValue(true)});Zapatec.Utils.insertAfter(this.field,arrow);this.dropDown=new Zapatec.DropDown({element:this.field,hook:arrow,onselect:function(objSource){self.setValue(objSource.join(" "));if(self.field.onchange!=null){self.field.onchange.call();}
self.validate();self.isEditing=true;self.keyup();self.blur();},theme:this.form.config.dropDownTheme,themePath:Zapatec.zapatecPath+"../zpextra/themes/dropdown/"})}
Zapatec.Form.Field.prototype.valueChanged=function(ev){if(this.hasFeature("zpFormAllowedChars")||this.hasFeature("zpFormMask")){this.setValueFromField();}else{this.validate();}
this.config.form.fireEvent("valueChanged");this.fireEvent("valueChanged");this.form.runChangeHandlers()
return true;};Zapatec.Form.Field.prototype.booleanChanged=function(ev){var elements=this.form.container.elements;for(var ii=0;ii<elements.length;ii++){if(elements[ii].name==this.field.name&&elements[ii].zpFormField!=null){elements[ii].zpFormField.validate();}}
this.config.form.fireEvent("valueChanged");this.fireEvent("valueChanged");this.config.form.fireEvent("booleanValueChanged");this.fireEvent("booleanValueChanged");this.form.runChangeHandlers()
return this.blur();}
Zapatec.Form.Field.prototype.keydown=function(evt){if(!this.isEditing){return false;}
this.form.runChangeHandlers()
if(!evt){evt=window.event;}
this.config.form.fireEvent("keydown",evt);this.fireEvent("keydown",evt);this.state.lastSelectionStart=this.getSelectionStart();this.state.lastSelectionEnd=this.getSelectionEnd();if(Zapatec.is_ie&&(this.hasFeature('zpFormAllowedChars')||this.hasFeature("zpFormMask"))){var tmpArr=Zapatec.Utils.getCharFromEvent(evt);var charCode=tmpArr.charCode;var newChar=tmpArr.chr;if(Zapatec.Form.Utils.isSpecialKey(charCode,newChar)||this.processFunctionalKeys(evt)==true){return true;}
if(this.hasFeature("zpFormMask")){if(this.processCustomKeys(charCode)==true){return true;}
return false;}
if(this.hasFeature('zpFormAllowedChars')){this.setValueFromField();}}
if(this.dropDown){this.dropDown.hide();}
return true;}
Zapatec.Form.Field.prototype.keypress=function(evt){if(!this.isEditing){return false;}
if(typeof(evt)=='undefined'){evt=window.event;}
this.config.form.fireEvent("keypress",evt);this.fireEvent("keypress",evt);if(this.hasFeature('zpFormAllowedChars')){if(this.processFunctionalKeys(evt)==true){return true;}
var tmpArr=Zapatec.Utils.getCharFromEvent(evt)
var charCode=tmpArr.charCode;var newChar=tmpArr.chr;if((Zapatec.Form.Utils.isSpecialKey(charCode,newChar)||charCode==8||charCode==46)){return true;}
var allowed=new RegExp('['+this.getFeature('zpFormAllowedChars')+']');this.setValueFromField();if(!(allowed.test(newChar))){Zapatec.Utils.addClass(this.field,"zpWrongValue");this.field.readonly=true;var self=this;setTimeout(function(){Zapatec.Utils.removeClass(self.field,"zpWrongValue");self.field.readonly=false;},100);return false;}
return true;}
if(this.hasFeature("zpFormMask")){var self=this;var tmpArr=Zapatec.Utils.getCharFromEvent(evt)
var charCode=tmpArr.charCode;var newChar=tmpArr.chr;if(this.processFunctionalKeys(evt)==true){return true;}
this.setValueFromField();var pos=this.getSelectionStart();if(charCode==null&&newChar==null){return false;}
if(!Zapatec.is_ie){if(Zapatec.Form.Utils.isSpecialKey(charCode,newChar)){return true;}
if(this.processCustomKeys(charCode)==false){return false;}}
if(typeof(this.chars[pos])=='string'){var newPos=this.getNextAvailablePosition(pos);if(newPos==null||newPos==pos)
return false;this.setCaretPosition(newPos);pos=newPos;}
if(pos>=this.chars.length||typeof(this.chars[pos])!='string'&&!newChar.match(this.chars[pos])||typeof(this.chars[pos])=='string'&&newChar!=this.chars[pos]){Zapatec.Utils.addClass(this.field,"zpWrongValue");this.field.readonly=true;setTimeout(function(){Zapatec.Utils.removeClass(self.field,"zpWrongValue");self.field.readonly=false;},100);this.setValue();this.setCaretPosition(pos)}else{this.enteredValue[pos]=newChar;this.setValue();var newPos=this.getNextAvailablePosition(pos);if(newPos==null){newPos=pos+1;}
this.setCaretPosition(newPos)}
if(evt&&evt.preventDefault){evt.preventDefault();}
return false;}}
Zapatec.Form.Field.prototype.keyup=function(evt){if(!this.isEditing){return false;}
this.form.runChangeHandlers()
if(evt){var tmp=Zapatec.Utils.getCharFromEvent(evt);if(Zapatec.Form.Utils.isSpecialKey(tmp.charCode,tmp.chr)||((tmp.charCode==8||tmp.charCode==46)&&this.state.lastSelectionStart!=this.state.lastSelectionEnd)){return true;}}
this.validate();if(this.hasFeature("zpFormAutoComplete")||this.hasFeature("zpFormAutoCompleteStrict")){this.autoCompleteValue(this.getAutoCompleteOptions());}
this.keyPressCounter++;var self=this;setTimeout(function(){self.runDelayedActions()},Zapatec.Form.Field.DELAYED_INTERVAL);return true;};Zapatec.Form.Field.prototype.focus=function(evt){if(!evt){evt=window.event;}
if(this.field.readOnly){return;}
this.isEditing=true;this.firstRun=false;if(this.hasFeature("zpFormMask")){if(this.isEmpty()){this.setValue();this.setCaretPosition(0);}}
this.config.form.fireEvent("focus",evt);this.fireEvent("focus",evt);this.validate();this.form.runChangeHandlers()};Zapatec.Form.Field.prototype.blur=function(evt){if(!evt){evt=window.event;}
if(this.hasFeature("zpFormMask")&&!this.isFilled()){Zapatec.Form.Utils.setValue(this.field,"");}
if(!this.isEditing){return;}
this.isEditing=false;if(this.hasFeature("zpFormAllowedChars")){this.setValueFromField(true);}
this.config.form.fireEvent("blur",evt);this.fireEvent("blur",evt);this.validate();this.form.runChangeHandlers()};Zapatec.Form.Field.prototype.validate=function(onlyValidate){if(!this.field.className||this.field.disabled){return null;}
var validators=Zapatec.Form.dataTypes;var message=null;var errors=[];var isRequired=this.hasFeature("zpFormRequired");var isEmpty=null;if(this.isBooleanField){if(isRequired){isEmpty=this.isEmpty();}else{isEmpty=this.field.checked;}}else{isEmpty=this.isEmpty();}
if(this.firstRun&&!isEmpty){this.firstRun=false;}
var validatorUsed=isRequired;if(isEmpty){validatorUsed=true;if(isRequired){message=this.hasFeature("zpFormRequiredError")?this.getFeature("zpFormRequiredError"):this.getMessage('isRequiredError');errors.push({field:this.field,errorMessage:message,validator:'zpFormRequired'});}}else{for(var validatorName in this.validators){if(validatorName=='zpFormMask'){validatorUsed=true;if(!this.isMaskFullyFilled()){var mask=this.getFeature("zpFormMask");message=this.hasFeature("zpFormMaskError")?this.getFeature("zpFormMaskError"):this.getMessage('maskNotFilledError',mask.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/\>/g,'&gt;'));errors.push({field:this.field,errorMessage:message,validator:"zpFormMask"});}}
if(this.hasFeature('zpFormMask')&&!this.isMaskFullyFilled()){continue;}
if(validatorName=='zpFormAutoCompleteStrict'){validatorUsed=true;var founded=false;var currVal=this.getValue();for(var ii=0;ii<this.autoCompleteOptions.length;ii++){if(currVal==this.autoCompleteOptions[ii]){founded=true;break;}}
if(!founded){message=this.hasFeature("zpFormAutoCompleteStrictError")?this.getFeature("zpFormAutoCompleteStrictError"):this.getMessage('noSuchAutoCompleteValueError');errors.push({field:this.field,errorMessage:message,validator:"zpFormAutoCompleteStrict"});}}
if(typeof(validators[validatorName])!='undefined'){validatorUsed=true;var validator=validators[validatorName];var validatorPassed=true;if(validator.regex){validatorPassed=validator.regex.test(this.getValue());}else if(validator.func){validatorPassed=validator.func(this.getValue(),this.getFeature(validatorName));}
if(!validatorPassed){if(this.hasFeature(validatorName+"Error")){message=this.getFeature(validatorName+"Error");}else{message=validator.error;if(typeof(this.getMessage(validator.error))!='undefined'){message=this.getMessage(validator.error);}}
errors.push({field:this.field,errorMessage:message,validator:validator});}}}
if(this.ajaxError!=null){validatorUsed=true;message=this.ajaxError;if(typeof(this.getMessage(this.ajaxError))!='undefined'){message=this.getMessage(this.ajaxError);}
errors.push({field:this.field,errorMessage:message,validator:"zpFormValidate"});}}
if(!onlyValidate&&validatorUsed){this.setImageStatus(message,isEmpty);}
return errors;};Zapatec.Form.Field.prototype.setImageStatus=function(status,isEmpty){var isRequired=this.hasFeature("zpFormRequired");if(typeof(isEmpty)=='undefined'){isEmpty=this.isEmpty();}
this.statusImg1.className=Zapatec.Form.ignoreMark+(isRequired?' zpIsRequired':' zpNotRequired');this.statusImg2.className=Zapatec.Form.ignoreMark;this.statusImg3.className=Zapatec.Form.ignoreMark;this.statusImg4.className=Zapatec.Form.ignoreMark;this.errorText.innerHTML="";if(this.form.config.strict){if(status==null){this.form.toggleSubmits(this.form.validate()!=null)}else{this.form.toggleSubmits(true);}}
if(!this.firstRun&&(isRequired&&isEmpty||!isEmpty)){this.statusImg2.className+=(this.isEditing?' zpIsEditing':' zpNotEditing');this.statusImg3.className+=(isEmpty?' zpIsEmpty':' zpNotEmpty');this.statusImg4.className+=(!status?' zpIsValid':' zpNotValid');if((isRequired&&isEmpty&&this.isEditing)||(this.isEditing&&!this.config.form.config.displayErrorWhileTyping)){status=null;}
if(!status){if(typeof(this.form.config.showErrors)=='function'){this.form.config.showErrors(this.field);}}else if(status){if(this.form.config.showErrors=='beforeField'||this.form.config.showErrors=='afterField'){this.errorText.innerHTML=status;}else if(typeof(this.form.config.showErrors)=='function'){this.form.config.showErrors(this.field,status);}else if(this.form.config.showErrors=='tooltip'&&Zapatec.Tooltip){if(!this.tooltip){var tt=Zapatec.Utils.createElement('div',document.body);this.tooltip=new Zapatec.Tooltip({target:this.statusImg,tooltip:tt});}
this.tooltip.tooltip.innerHTML=status?status:'';}else{this.statusImg.title=status;}}}}
Zapatec.Form.Field.prototype.isEmpty=function(){if(!this.hasFeature("zpFormMask")){if(this.isBooleanField){var elements=this.form.container.elements;for(var ii=elements.length-1;ii>=0;ii--){var element=elements[ii];if(element.name==this.field.name&&element.checked){return false;}}
return true;}else{var currVal=this.getValue();return(currVal==null||currVal.length==0);}}else{for(ii=0;ii<this.enteredValue.length;ii++){if(typeof(this.chars[ii])!='string'&&this.enteredValue[ii]!=null){return false;}}
return true;}}
Zapatec.Form.Field.prototype.isFilled=function(){if(this.hasFeature("zpFormMask")){for(ii=0;ii<this.enteredValue.length;ii++){if(typeof(this.chars[ii])!='string'&&this.enteredValue[ii]!=null){return true;}}
return false;}else{var currVal=this.getValue();return(currVal!=null&&currVal.length>0);}}
Zapatec.Form.Field.prototype.isMaskFullyFilled=function(){if(this.hasFeature("zpFormMask")){for(ii=0;ii<this.enteredValue.length;ii++){if(typeof(this.chars[ii])!='string'&&this.enteredValue[ii]==null){return false;}}
return true;}else{return this.isFilled();}}
Zapatec.Form.Field.prototype.hasFeature=function(feature){if(typeof(feature)=='undefined'||feature==null||typeof(this.validators[feature])=='undefined'){return false;}
return true;}
Zapatec.Form.Field.prototype.getFeature=function(feature){if(!this.hasFeature(feature)){return false;}
return this.validators[feature]}
Zapatec.Form.Field.prototype.getNextAvailablePosition=function(pos){if(pos+1>=this.enteredValue.length){return null;}
if(typeof(this.chars[pos+1])=='string'){return this.getNextAvailablePosition(pos+1);}
return pos+1;}
Zapatec.Form.Field.prototype.getPrevAvailablePosition=function(pos){if(pos-1<0){return null;}
if(typeof(this.chars[pos-1])=='string'){return this.getPrevAvailablePosition(pos-1);}
return pos-1;}
Zapatec.Form.Field.prototype.setCaretPosition=function(startPos,endPos){var valLength=this.getValue().length;if(!this.isSelectionAppliable()||!this.isEditing){return null;}
if(isNaN(parseInt(startPos))){return false;}else{startPos=parseInt(startPos);if(startPos<0){startPos=0}else if(startPos>valLength){startPos=valLength;}}
if(endPos==null||isNaN(parseInt(endPos))||parseInt(endPos)<startPos){endPos=startPos;}else{endPos=parseInt(endPos);if(endPos<0){endPos=0;}else if(endPos>valLength){endPos=valLength;}}
if(typeof(this.field.createTextRange)=="object"){var range=this.field.createTextRange();range.moveEnd("character",endPos-this.getValue().length);range.moveStart("character",startPos);range.select();return true;}else if(typeof(this.field.setSelectionRange)=='function'){this.field.setSelectionRange(startPos,endPos)
return true;}
return false;}
Zapatec.Form.Field.prototype.getSelectionStart=function(){if(this.field.disabled||!this.isSelectionAppliable()||!this.isEditing){return 0;}
if(document.selection){return Math.abs(document.selection.createRange().moveStart("character",-1000000));}else if(typeof(this.field.selectionStart)!="undefined"){var selStart=this.field.selectionStart;if(selStart==2147483647){selStart=0;}
return selStart;}
return 0;}
Zapatec.Form.Field.prototype.getSelectionEnd=function(){if(this.field.disabled||!this.isSelectionAppliable()||!this.isEditing){return 0;}
if(document.selection){return this.field.value.length-Math.abs(document.selection.createRange().moveEnd("character",1000000));}else if(typeof(this.field.selectionEnd)!="undefined"){return this.field.selectionEnd;}
return 0;}
Zapatec.Form.Field.prototype.processCustomKeys=function(charCode){var selStart=this.getSelectionStart();var selEnd=this.getSelectionEnd();if(selStart==selEnd){if(charCode==8){var newPos=this.getPrevAvailablePosition(selStart);if(newPos==null||newPos==selStart){return false;}
this.enteredValue[newPos]=null;this.setValue();this.setCaretPosition(newPos+(Zapatec.is_opera?1:0));return false;}
if(charCode==46){if(typeof(this.chars[selStart])=='string'){return false;}
this.enteredValue[selStart]=null;this.setValue();this.setCaretPosition(selStart)
return false;}}else{if(charCode==8||charCode==46){for(var ii=selStart;ii<selEnd;ii++){if(typeof(this.chars[ii])!='string'){this.enteredValue[ii]=null;}}
this.setValue();this.setCaretPosition(selStart+(Zapatec.is_opera?1:0));return false;}}
return true;}
Zapatec.Form.Field.prototype.processFunctionalKeys=function(evt){var tmpArr=Zapatec.Utils.getCharFromEvent(evt)
var charCode=tmpArr.charCode;var newChar=tmpArr.chr;if(evt.ctrlKey||(typeof(evt.metaKey)!='undefined'&&evt.metaKey)){if(charCode==8){this.setCaretPosition(0,this.getSelectionStart());return false;}else if(charCode==46){this.setCaretPosition(this.getSelectionStart(),this.getValue().length);return false;}else if(newChar=='v'||newChar=='V'){this.setValueFromField();return true;}
return true;}else if(evt.shiftKey){if(charCode==37||charCode==39){return true;}else if(charCode==45){this.setValueFromField();return true;}}else if(evt.altKey){return true;}
return false;}
Zapatec.Form.Field.prototype.setValueFromField=function(runImmediately){if(!runImmediately){var self=this;setTimeout(function(){self.setValueFromField(true);},1);return;}
var selStart=this.getSelectionStart();var selEnd=this.getSelectionEnd();var editMode=this.isEditing;this.isEditing=true;this.setValue(Zapatec.Form.Utils.getValue(this.field));if(this.isBooleanField){this.booleanChanged();}
this.isEditing=editMode;this.validate();if(!this.isEditing){this.blur();}else{this.setCaretPosition(selStart,selEnd);}}
Zapatec.Form.Field.prototype.getValue=function(){return Zapatec.Form.Utils.getValue(this.field);}
Zapatec.Form.Field.prototype.setValue=function(value){if(value==null){value="";}
if(this.hasFeature('zpFormAllowedChars')){var notallowed=new RegExp('[^'+this.getFeature('zpFormAllowedChars')+']','g');value=value.replace(notallowed,"");}
if(this.hasFeature('zpFormMask')){var val="";if(this.isEditing||this.isFilled()){for(ii=0;ii<this.chars.length;ii++){if(ii<value.length){if(typeof(this.chars[ii])!="string"){if(this.chars[ii].test(value.charAt(ii))){this.enteredValue[ii]=value.charAt(ii);val+=value.charAt(ii);}else{this.enteredValue[ii]=null;val+="_";}}else{this.enteredValue[ii]=this.chars[ii];val+=this.chars[ii];}}else if(arguments.length>0){if(typeof(this.chars[ii])=='string'){val+=this.chars[ii];}else{this.enteredValue[ii]=null;val+="_";}}else{if(typeof(this.chars[ii])=='string'){val+=this.chars[ii];}else{val+=this.enteredValue[ii]==null?"_":this.enteredValue[ii];}}}}
value=val;}
var oldScrollTop=null;var oldScrollLeft=null;if(this.field.nodeName.toLowerCase()=='textarea'&&typeof(this.field.scrollTop)!='undefined'){oldScrollTop=this.field.scrollTop;oldScrollLeft=this.field.scrollLeft;}
var retVal=Zapatec.Form.Utils.setValue(this.field,value);if(this.field.nodeName.toLowerCase()=='textarea'&&oldScrollTop!=null){this.field.scrollTop=oldScrollTop;this.field.scrollLeft=oldScrollLeft;}
return retVal;}
Zapatec.Form.Field.prototype.runDelayedActions=function(){this.keyPressCounter--;if(this.keyPressCounter!=0){return null;}
this.ajaxValidate();this.suggestValue();this.ajaxFill();}
Zapatec.Form.Field.prototype.ajaxValidate=function(){if(!this.hasFeature("zpFormValidate")){return null;}
var valid=this.validate();if(!(valid==null||valid!=null&&(valid.length==0||valid.length==1&&valid[0].validator=="zpFormValidate"))){return null;}
var submitUrl=this.getFeature("zpFormValidate");var submitMethod=this.getFeature("zpFormValidateMethod");var submitParam=this.getFeature("zpFormValidateParam");var submitQuery=this.getFeature("zpFormValidateQuery");if(typeof(submitMethod)!='string'){submitMethod="GET"}
if(typeof(submitParam)!='string'){submitParam=this.field.name;}
if(typeof(submitQuery)!='string'){submitQuery="";}
submitQuery+="&"+escape(submitParam)+"="+escape(this.getValue());if(submitUrl.indexOf("?")<0){submitUrl+="?";}
submitUrl+="&"+Math.random();if(submitMethod=='GET'){submitUrl+="&"+submitQuery;}
this.statusImg5.className="zpIsFetching "+Zapatec.Form.ignoreMark;var self=this;if(this.form.config.ajaxDebugFunc){this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugSeparator'));this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugValidateTitle',this.field.name));this.form.config.ajaxDebugFunc(submitMethod+" "+submitUrl);this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugQuery',("GET"?"":submitQuery)));}
Zapatec.Transport.fetch({url:submitUrl,content:submitMethod=="GET"?null:submitQuery,method:submitMethod,onLoad:function(objText){if(self.form.config.ajaxDebugFunc){self.form.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponse',objText.responseText));}
self.statusImg5.className=Zapatec.Form.ignoreMark+"zpNotFetching";if(objText.responseText==null){Zapatec.Log({description:self.getMessage('ajaxValidateNoResponseError',objText.responseText)});return null;}
var objResponse=Zapatec.Transport.parseJson({strJson:objText.responseText});if(objResponse==null){Zapatec.Log({description:self.getMessage('ajaxValidateCantParseError',objText.responseText)});return null;}
if(!objResponse.success){self.ajaxError=typeof(objResponse.generalError)!='string'||objResponse.generalError.length==0?self.getMessage('ajaxValidateValidationError'):objResponse.generalError;}else{self.ajaxError=null;}
self.validate();},onError:function(objError){var strError='';if(objError.errorCode){strError+=objError.errorCode+' ';}
strError+=objError.errorDescription;self.statusImg5.className=Zapatec.Form.ignoreMark+" zpNotFetching";alert(strError);self.ajaxError=null;if(self.form.config.ajaxDebugFunc){self.form.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponseError',strError));}}});}
Zapatec.Form.Field.prototype.ajaxFill=function(){if(!this.hasFeature("zpFormFillUrl")){return null;}
var submitUrl=this.getFeature("zpFormFillUrl");var submitMethod=this.getFeature("zpFormFillMethod");var submitParam=this.getFeature("zpFormFillParam");var submitQuery=this.getFeature("zpFormFillQuery");if(typeof(submitMethod)!='string'){submitMethod="GET"}
if(typeof(submitParam)!='string'){submitParam=this.field.name;}
if(typeof(submitQuery)!='string'){submitQuery="";}
submitQuery+="&"+escape(submitParam)+"="+escape(this.getValue());if(submitUrl.indexOf("?")<0){submitUrl+="?";}
submitUrl+="&"+Math.random();if(submitMethod=='GET'){submitUrl+="&"+submitQuery;}
this.statusImg5.className="zpIsFetching "+Zapatec.Form.ignoreMark;var self=this;if(this.form.config.ajaxDebugFunc){this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugSeparator'));this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugFillTitle',this.field.name));this.form.config.ajaxDebugFunc(submitMethod+" "+submitUrl);this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugQuery',("GET"?"":submitQuery)));}
Zapatec.Transport.fetch({url:submitUrl,content:submitMethod=="GET"?null:submitQuery,method:submitMethod,onLoad:function(objText){if(self.form.config.ajaxDebugFunc){self.form.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponse',objText.responseText));}
self.statusImg5.className=Zapatec.Form.ignoreMark+" zpNotFetching";if(objText.responseText==null){Zapatec.Log({description:self.getMessage('ajaxFillNoResponseError',objText.responseText)});return null;}
var objResponse=Zapatec.Transport.parseJson({strJson:objText.responseText});if(objResponse==null){Zapatec.Log({description:self.getMessage('ajaxFillCantParseError',objText.responseText)});return null;}
if(!objResponse.success){self.ajaxError=typeof(objResponse.generalError)!='string'||objResponse.generalError.length==0?self.getMessage('ajaxFillGeneralError'):objResponse.generalError;}else{self.ajaxError=null;var formObject=self.form;var fillData=objResponse.fillData;if(fillData.length==0){return null;}else{var fields=fillData[0];for(var ii=0;ii<fields.length;ii++){var field=formObject.container.elements[fields[ii]['fieldName']];if(!field){continue;}
Zapatec.Form.Utils.setValue(field,fields[ii]['fieldValue']);if(field.zpFormField){field.zpFormField.setValueFromField(true);}}}}
self.validate();},onError:function(objError){var strError='';if(objError.errorCode){strError+=objError.errorCode+' ';}
strError+=objError.errorDescription;self.statusImg5.className=Zapatec.Form.ignoreMark+" zpNotFetching";alert(strError);self.ajaxError=null;if(self.form.config.ajaxDebugFunc){self.form.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponseError',strError));}}});}
Zapatec.Form.Field.prototype.suggestValue=function(showAll){if(!this.hasFeature("zpFormSuggest")||!showAll&&this.isEmpty()){return null;}
var suggestUrl=this.getFeature("zpFormSuggest");var suggestMethod=this.getFeature("zpFormSuggestMethod");var suggestParam=this.getFeature("zpFormSuggestParam");var suggestQuery=this.getFeature("zpFormSuggestQuery");if(typeof(suggestMethod)!='string'){suggestMethod="GET"}
if(typeof(suggestParam)!='string'){suggestParam=this.field.name;}
if(typeof(suggestQuery)!='string'){suggestQuery="";}
suggestQuery+="&"+escape(suggestParam)+"="+(showAll?"":escape(this.getValue()));if(suggestUrl.indexOf("?")<0){suggestUrl+="?";}
suggestUrl+="&"+Math.random();if(suggestMethod=='GET'){suggestUrl+="&"+suggestQuery;}
this.statusImg5.className="zpIsFetching "+Zapatec.Form.ignoreMark;var self=this;if(this.form.config.ajaxDebugFunc){this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugSeparator'));this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugSuggestTitle',this.field.name));this.form.config.ajaxDebugFunc(suggestMethod+" "+suggestUrl);this.form.config.ajaxDebugFunc(this.getMessage('ajaxDebugQuery',("GET"?"":suggestQuery)));}
Zapatec.Transport.fetch({url:suggestUrl,content:suggestMethod=="GET"?null:suggestQuery,method:suggestMethod,onLoad:function(objText){if(self.form.config.ajaxDebugFunc){self.form.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponse',objText.responseText));}
self.statusImg5.className=Zapatec.Form.ignoreMark+" zpNotFetching";if(objText.responseText==null){Zapatec.Log({description:self.getMessage('ajaxSuggestNoResponseError',objText.responseText)});return null;}
var objResponse=Zapatec.Transport.parseJson({strJson:objText.responseText});if(objResponse==null){Zapatec.Log({description:self.getMessage('ajaxSuggestCantParseError',objText.responseText)});return null;}
if(!objResponse.success){alert(typeof(objResponse.generalError)!='string'||objResponse.generalError.length==0?self.getMessage('ajaxSuggestGeneralError'):objResponse.generalError);}else{self.autoCompleteValue(objResponse);}
self.validate();},onError:function(objError){var strError='';if(objError.errorCode){strError+=objError.errorCode+' ';}
strError+=objError.errorDescription;self.statusImg5.className=Zapatec.Form.ignoreMark+" zpNotFetching";alert(strError);if(self.form.config.ajaxDebugFunc){self.form.config.ajaxDebugFunc(self.getMessage('ajaxDebugResponseError',strError));}}});}
Zapatec.Form.Field.prototype.getAutoCompleteOptions=function(showAll){var opts={body:[]};var currVal=this.getValue();if(this.hasFeature("zpFormAutoComplete")||this.hasFeature("zpFormAutoCompleteStrict")){for(var ii=0;ii<this.autoCompleteOptions.length;ii++){if((this.hasFeature("zpFormAutoCompleteStrict")?this.autoCompleteOptions[ii].substring(0,currVal.length):this.autoCompleteOptions[ii].substring(0,currVal.length).toLowerCase())==(this.hasFeature("zpFormAutoCompleteStrict")?currVal:currVal.toLowerCase())||showAll){opts.body.push([this.autoCompleteOptions[ii]]);}}}
return opts;}
Zapatec.Form.Field.prototype.autoCompleteValue=function(opts){if(typeof(opts)=='undefined'||opts.body==null||opts.body.length==0||(opts.body.length==1&&opts.body[0][0]=="")){return;}
var currValue=this.getValue();var retrValue=null;var firstValue=opts.body[0][0];if(firstValue.substring(0,currValue.length).toLowerCase()==currValue.toLowerCase()){retrValue=firstValue.substring(currValue.length);this.setValue(currValue+retrValue);this.setCaretPosition(currValue.length,this.getValue().length);}
this.validate();if(this.dropDown&&opts.body.length>1){this.dropDown.setContent(opts)
this.dropDown.show();this.dropDown.setWidth(this.field.clientWidth);if(this.dropDown.WCH){this.dropDown.WCH.style.width=this.field.clientWidth+"px";}}}
Zapatec.Form.Field.prototype.isSelectionAppliable=function(){var nodeName=this.field.nodeName.toLowerCase();var inputType=nodeName=='input'?this.field.type.toLowerCase():null;return(nodeName=="body"||nodeName=="button"||nodeName==="textarea"||nodeName=="input"&&(inputType=="button"||inputType=="hidden"||inputType=="password"||inputType=="reset"||inputType=="submit"||inputType=="text"))}
Zapatec.Form.Field.prototype.destroy=function(){this.discard();}
Zapatec.Form.dataTypes={};Zapatec.Form.Validator=[];Zapatec.Form.Validator.addDataType=function(zpName,name,regex,error,help,func){Zapatec.Form.dataTypes[zpName]={zpName:zpName,name:name,regex:regex,error:error,help:help,func:func};}
Zapatec.Form.addDataType=Zapatec.Form.Validator.addDataType;Zapatec.Form.Validator.isDomainValid=function(domain){if(typeof(domain)!='string'){return false;}
for(i=0;i<domain.length;i++){if(domain.charCodeAt(i)>127){return false;}}
var ipDigit="(0?0?\\d|[01]?\\d\\d|2[0-4]\\d|25[0-6])";var ipRE=new RegExp("^"+ipDigit+"\\."+ipDigit+"\\."+ipDigit+"\\."+ipDigit+"$");if(ipRE.test(domain)){return true;}
var domains=domain.split(".");if(domains.length<2){return false;}
for(i=0;i<domains.length-1;i++){if(!(/^[a-zA-Z0-9\-]+$/).test(domains[i])){return false;}}
if(domains[domains.length-2].length<2){return false;}
if(!(/^[a-zA-Z]{2,}$/).test(domains[domains.length-1])){return false;}
return true;}
Zapatec.Form.Validator.isUrlValid=function(url){if(typeof(url)!='string'){return false;}
var domain=url;var protocolSeparatorPos=url.indexOf("://");var domainSeparatorPos=url.indexOf("/",protocolSeparatorPos+3);if(protocolSeparatorPos==0){return false;}
domain=url.substring((protocolSeparatorPos>0?protocolSeparatorPos+3:0),(domainSeparatorPos>0?domainSeparatorPos:url.length));var portSeparatorPos=domain.indexOf(":");if(portSeparatorPos>0){var port=domain.substring(portSeparatorPos+1);if(!port.match(/\d+/)){return false;}
domain=domain.substring(0,portSeparatorPos);}
return Zapatec.Form.Validator.isDomainValid(domain);}
Zapatec.Form.Validator.isEmailValid=function(email){if(email==null){return false;}
var atPos=email.indexOf("@");if(atPos<1||email.indexOf(".",atPos)==-1){return false}
var login=email.substring(0,atPos);var domain=email.substring(atPos+1,email.length);var atom="\[^\\s\\(\\)><@,;:\\\\\\\"\\.\\[\\]\]+";var word="("+atom+"|(\"[^\"]*\"))";var loginRE=new RegExp("^"+word+"(\\."+word+")*$");for(i=0;i<login.length;i++){if(login.charCodeAt(i)>127){return false;}}
if(!login.match(loginRE)){return false;}
return Zapatec.Form.Validator.isDomainValid(domain);}
Zapatec.Form.Validator.isCreditCardValid=function(cardNumber){if(cardNumber==null){return false;}
var cardDigits=cardNumber.replace(/\D/g,"");var parity=cardDigits.length%2;var sum=0;for(var ii=0;ii<cardDigits.length;ii++){var digit=cardDigits.charAt(ii);if(ii%2==parity)
digit=digit*2;if(digit>9)
digit=digit-9;sum+=parseInt(digit);}
return((sum!=0)&&(sum%10==0))}
Zapatec.Form.Validator.isDateValid=function(str,fmt){if(fmt==null||fmt==""){fmt="%m/%d/%y"}
var separator=" ";var nums=fmt.split(separator)
if(nums.length<3){separator="/"
nums=fmt.split(separator)
if(nums.length<3){separator="."
nums=fmt.split(separator)
if(nums.length<3){separator="-"
nums=fmt.split(separator)
if(nums.length<3){separator=null;}}}}
if(separator==null){return false;}
var y=null;var m=null;var d=null;var a=str.split(separator);if(a.length!=3){return false;}
var b=fmt.match(/%./g);var nlDays=[31,28,31,30,31,30,31,31,30,31,30,31];var lDays=[31,29,31,30,31,30,31,31,30,31,30,31];for(var i=0;i<a.length;++i){if(!a[i])
continue;switch(b[i]){case"%d":case"%e":d=parseInt(a[i],10);if(d<0||d>31)
d=-1
break;case"%m":m=parseInt(a[i],10)-1;if(m>11||m<0)
m=-1;break;case"%Y":case"%y":y=parseInt(a[i],10);(y<100)&&(y+=(y>29)?1900:2000);break;}}
if(y==null||m==null||d==null||isNaN(y)||isNaN(m)||isNaN(d)){return false;}
if(m!=-1){if((y%4)==0){if((y%100)==0&&(y%400)!=0){if(d>nlDays[m]){d=-1;}}
if(d>lDays[m]){d=-1;}}else{if(d>nlDays[m]){d=-1;}}}
if(y!=0&&m!=-1&&d!=-1){return true;}
return false;}
Zapatec.Form.Validator.addDataType('zpFormUrl','A URL -- web address',null,'invalidURLError',"Valid URL needs to be in the form http://www.yahoo.com:80/index.html or just www.yahoo.com",Zapatec.Form.Validator.isUrlValid);Zapatec.Form.Validator.addDataType('zpFormEmail','An Email Address',null,'invalidEmailError',"Valid email address need to be in the form of nobody@example.com",Zapatec.Form.Validator.isEmailValid);Zapatec.Form.Validator.addDataType('zpFormCreditCard','Credit card number',null,'invalidCreditCardError',"Please enter valid credit card number",Zapatec.Form.Validator.isCreditCardValid);Zapatec.Form.Validator.addDataType('zpFormUSPhone','A USA Phone Number',/^((\([1-9][0-9]{2}\) *)|([1-9][0-9]{2}[\-. ]?))(\d[ -]?){6}\d *(ex[t]? *[0-9]+)?$/,'invalidUSPhoneError',"Valid US Phone number needs to be in the form of 'xxx xxx-xxxx' For instance 312 123-1234. An extention can be added as ext xxxx. For instance 312 123-1234 ext 1234",null);Zapatec.Form.Validator.addDataType('zpFormInternationalPhone','An international Phone Number',/^\+\d{1,3}[ -]\d{2,3}[ -](\d[ -]?){6}\d *(ex[t]? *[0-9]+)?$/,'invalidInternationalPhoneError',"Valid internation phone number needs to be in the form of '+x xxx xxx-xxxx' For instance +1 234 567-9012. An extention can be added as ext xxxx. For instance +1 234 567-9012 ext 1234",null);Zapatec.Form.Validator.addDataType('zpFormUSZip','A USA Zip Number',/(^\d{5}$)|(^\d{5}-\d{4}$)/,'invalidUSZipError',"Valid US Zip number needs to be either in the form of '99999', for instance 94132 or '99999-9999' for instance 94132-3213",null);Zapatec.Form.Validator.addDataType('zpFormDate','A Valid Date',null,'invalidDateError',"Please enter a valid date",Zapatec.Form.Validator.isDateValid);Zapatec.Form.Validator.addDataType('zpFormInt','An Integer',null,'invalidIntError',"Please enter an integer",function(number){return/^\d+$/.test(number)});Zapatec.Form.Validator.addDataType('zpFormFloat','A Floating Point Number',null,'invalidFloatError',"Please enter a Floating Point Number",function(number){var parsed=parseFloat(number);return(parsed==number);});Zapatec.Form.Utils=[];Zapatec.Form.Utils.getTokens=function(className,separator){if(typeof(separator)!='string'||separator.length==0){separator=" ";}
var arr={};if(className!=null&&className.length>0){var isInQuotes=false;var quoteChar=null;var key="";var value="";var isInValue=false;for(var ii=0;ii<className.length;ii++){var currChar=className.charAt(ii);if(currChar=="\\"){ii++;currChar=className.charAt(ii);}else if(!isInValue&&currChar=="="){isInValue=true;var nextChar=className.charAt(ii+1);if(nextChar=="'"||nextChar=='"'){quoteChar=nextChar;ii++;}
continue;}else if(currChar==" "){if(key.length==0){continue;}
if(quoteChar!=null){if(quoteChar==value.charAt(value.length-1)){quoteChar=null;value=value.substr(0,value.length-1);}else{value=quoteChar+value;}}
arr[key]=value.length==0?null:value;isInValue=false;key="";value="";quoteChar=null;continue;}
if(ii<className.length){if(isInValue){value+=currChar;}else{key+=currChar;}}}
if(key.length>0){if(quoteChar!=null){if(quoteChar==value.charAt(value.length-1)){quoteChar=null;value=value.substr(0,value.length-1);}else{value=quoteChar+value;}}
arr[key]=(value.length==0?null:value)}}
return arr;}
Zapatec.Form.Utils.toggleFormElements=function(field,show,useVisibility){field=Zapatec.Widget.getElementById(field);if(field==null){return null;}
var inputs=Zapatec.Form.Utils.getFormElements(field);for(var ii=0;ii<inputs.length;ii++){inputs[ii].disabled=show==false;if(show&&inputs[ii].zpFormField!=null){inputs[ii].zpFormField.validate();}}
if(useVisibility){field.style.visibility=(show?'visible':'hidden');}else{field.style.display=(show?'':'none');}}
Zapatec.Form.Utils.getFormElements=function(el){el=Zapatec.Widget.getElementById(el);if(el==null){return null;}
var inputs=[];var childs=el.all?el.all:el.getElementsByTagName("*");for(var ii=0;ii<childs.length;ii++){if(Zapatec.Form.Utils.isInputField(childs[ii])){inputs.push(childs[ii]);}}
return inputs;}
Zapatec.Form.Utils.getValue=function(element){element=Zapatec.Widget.getElementById(element);if(element==null||typeof(element.tagName)=='undefined'){return null;}
switch(element.tagName.toLowerCase()){case"select":var option=element.options[element.selectedIndex];if(option!=null){return option.value;}else{return"";}
case"input":return element.value;case"textarea":return element.value;}
return null;}
Zapatec.Form.Utils.setValue=function(element,value){element=Zapatec.Widget.getElementById(element);if(element==null||typeof(element.tagName)=='undefined'){return null;}
switch(element.tagName.toLowerCase()){case"input":if(element.type.toLowerCase()!="file"){element.value=value;}
break;case"textarea":element.value=value;break;case"select":for(var i=0;i<element.options.length;i++){if(element.options[i].value==value){element.selectedIndex=i;break;}}}
return value;}
Zapatec.Form.Utils.isInputField=function(el){if(el.nodeType!=1){return false;}
var nodeName=el.nodeName.toLowerCase();return(nodeName=='input'||nodeName=='textarea'||nodeName=='select');}
Zapatec.Form.Utils.ignoreField=function(field){field=Zapatec.Widget.getElementById(field);if(!field||field.nodeType!=1||(field.className&&/\bzpIgnoreField\b/.test(field.className))||!Zapatec.Form.Utils.isInputField(field)||(field.nodeType==1&&field.nodeName.toLowerCase()=='fieldset')){return true;}
var type=field.type.toLowerCase();var ignoreList=['submit','reset','button'];for(var ii=0;ii<ignoreList.length;ii++){if(type.toLowerCase()==ignoreList[ii]){return true;}}
return false;}
Zapatec.Form.Utils.isSpecialKey=function(charCode,newChar){return((newChar==null&&charCode!=8&&charCode!=46)||charCode==9||charCode==13||charCode==16||charCode==17||charCode==18||charCode==20||charCode==27||charCode==33||charCode==34||charCode==35||charCode==36||charCode==37||charCode==38||charCode==39||charCode==40||charCode==45||charCode==144||charCode>256)}
Zapatec.Form.Utils.initMultipleField=function(currEl,firstRun,form){var md=null;if(!currEl.className||!(md=currEl.className.match(/zpFormMultiple(Inside|Outside)?/))||currEl.zpRelatedElements!=null){return null;}
var outside=true;if(md[1]=="Inside"||currEl.nodeName.toLowerCase()=="td"||currEl.nodeName.toLowerCase()=="th"||currEl.nodeName.toLowerCase()=="tr"){outside=false;}
if(currEl.nodeName.toLowerCase()=="input"||currEl.nodeName.toLowerCase()=="textarea"||currEl.nodeName.toLowerCase()=="select"||currEl.nodeName.toLowerCase()=="image"){outside=true;}
var appendEl=currEl;if(currEl.nodeName.toLowerCase()=="tr"){function findParentTable(el){if(el.parentNode!=null&&el.parentNode.nodeType==1&&el.parentNode.tagName.toLowerCase()!="table"){return findParentTable(el.parentNode);}
return el.parentNode;}
var table=findParentTable(currEl);for(var jj=table.rows.length-1;jj>=0;jj--){var td=document.createElement('td');td.className=Zapatec.Form.ignoreMark;td.innerHTML="&nbsp;";if(jj==currEl.rowIndex||table.rows[jj]==currEl){appendEl=td;}
if(firstRun||jj==currEl.rowIndex){table.rows[jj].appendChild(td);}}}
var button=Zapatec.Utils.createElement('input');button.type="button"
button.className=Zapatec.Form.ignoreMark+" multipleButton"
Zapatec.Utils.createProperty(button,"zpMultipleElement",currEl)
if(currEl.zpOriginalNode==null){Zapatec.Utils.createProperty(currEl,"zpMultipleChilds",[]);button.value="+";button.onclick=function(){if(!this.disabled){Zapatec.Form.Utils.cloneElement(currEl,form);}}}else{button.value="-";var parent=currEl.zpOriginalNode
parent.zpMultipleChilds[parent.zpMultipleChilds.length]=currEl;button.onclick=function(){if(!this.disabled){Zapatec.Form.Utils.removeClonedElement(currEl);}}}
if(!currEl.zpOriginalNode){if(outside){Zapatec.Utils.insertAfter(appendEl,button);}else{appendEl.appendChild(button);}}
Zapatec.Utils.createProperty(currEl,"zpRelatedElements",[button,currEl]);Zapatec.Utils.createProperty(currEl,"zpMultipleButton",button);var tokens=Zapatec.Form.Utils.getTokens(currEl.className);if(typeof(tokens['zpFormMultipleLimit'])!='undefined'&&!isNaN(parseInt(tokens['zpFormMultipleLimit']))){Zapatec.Utils.createProperty(currEl,"zpFormMultipleLimit",parseInt(tokens['zpFormMultipleLimit'])-2);if(isNaN(currEl.zpFormMultipleLimit)){currEl.zpFormMultipleLimit=-1;}}else{Zapatec.Utils.createProperty(currEl,"zpFormMultipleLimit",-1);}
if(currEl.zpFormField!=null){currEl.zpRelatedElements=[currEl.zpFormField.statusImg1,currEl.zpFormField.statusImg2,currEl.zpFormField.statusImg3,currEl.zpFormField.statusImg4,currEl.zpFormField.statusImg,currEl.zpFormField.errorText].concat(currEl.zpRelatedElements);}else{Zapatec.Utils.createProperty(currEl,"zpLastNode",(outside?button:currEl));}}
Zapatec.Form.Utils.cloneElement=function(field,form){if(field.zpFormMultipleLimit>=0&&field.zpMultipleChilds!=null&&field.zpMultipleChilds.length>field.zpFormMultipleLimit){return false;}
var insertAfterNode=field.zpLastNode;if(field.zpMultipleChilds!=null&&field.zpMultipleChilds.length>0){insertAfterNode=field.zpMultipleChilds[field.zpMultipleChilds.length-1].zpLastNode;}
var clone=field.cloneNode(true);Zapatec.Utils.createProperty(clone,"zpOriginalNode",field);if(form&&typeof(form.config.multipleCallback)=='function'){form.config.multipleCallback(field,clone,clone,field.zpMultipleChilds);}
Zapatec.Utils.insertAfter(insertAfterNode,clone);var childElements=[clone];var tmpArr=clone.all?clone.all:clone.getElementsByTagName("*");for(var ii=0;ii<tmpArr.length;ii++){childElements.push(tmpArr[ii]);}
for(var ii=0;ii<childElements.length;ii++){var currEl=childElements[ii];if(currEl.className.indexOf(Zapatec.Form.ignoreMark)>=0){Zapatec.Utils.destroy(currEl);continue;}
if(Zapatec.Form.Utils.isInputField(currEl)){Zapatec.Form.Utils.setValue(currEl,"");if(currEl.form&&currEl.form.zpForm){var zpForm=currEl.form.zpForm;new Zapatec.Form.Field({form:zpForm,field:currEl,langId:zpForm.config.langId,lang:zpForm.config.lang,langCountryCode:zpForm.config.langCountryCode,langEncoding:zpForm.config.langEncoding});}}
currEl.zpMultipleElement=null;currEl.zpMultipleChilds=null;currEl.zpRelatedElements=null;currEl.zpMultipleButton=null;currEl.zpFormMultipleLimit=null;Zapatec.Form.Utils.initMultipleField(currEl,false,form);if(form&&typeof(form.config.multipleCallback)=='function'){form.config.multipleCallback(field,clone,currEl,field.zpMultipleChilds);}}
if(field.zpFormMultipleLimit>=0&&field.zpMultipleChilds!=null&&field.zpMultipleChilds.length>field.zpFormMultipleLimit){field.zpMultipleButton.style.visibility='hidden';field.zpMultipleButton.disabled=true;}
return clone;}
Zapatec.Form.Utils.removeClonedElement=function(field){if(field==null||field.zpOriginalNode==null){return false;}
var childs=field.zpOriginalNode.zpMultipleChilds;for(var ii=0;ii<childs.length;ii++){if(childs[ii]==field){var original=field.zpOriginalNode;original.zpMultipleChilds=childs.slice(0,ii).concat(childs.slice(ii+1));if(original.zpFormMultipleLimit>=0&&original.zpMultipleChilds.length<=original.zpFormMultipleLimit){original.zpMultipleButton.style.visibility='visible';original.zpMultipleButton.disabled=false;}
break;}}
if(field.zpRelatedElements!=null&&field.zpRelatedElements.length>0){for(var ii=0;ii<field.zpRelatedElements.length;ii++){if(typeof(field.zpRelatedElements[ii])!='undefined'&&field.zpRelatedElements[ii]!=null){Zapatec.Utils.destroy(field.zpRelatedElements[ii]);}}}}
Zapatec.Form.Utils.generateMultipleId=function(original,cloneParent,cloned,childs){if(!original.zpMultipleCounter){original.zpMultipleCounter=0;}
if(!cloneParent.zpIsCloned){original.zpMultipleCounter++;cloneParent.zpIsCloned=true;}
cloned.zpMultipleCounter=null;if(typeof(cloned.id)!='undefined'&&cloned.id!=null&&cloned.id!=""){cloned.id+="-"+original.zpMultipleCounter;}
if(typeof(cloned.name)!='undefined'&&cloned.name!=null&&cloned.name!=""){cloned.name+="-"+original.zpMultipleCounter;}}
