// TODO: http://groups.yahoo.com/group/wdf-dom/message/4531
var oForms = { // <form class="form">

		aForms : [],
	
		mInit : function (parent) { // initialize
			return function() {
				oForms.aForms=oIThelp.getAllElementsByClassName(oIThelp.getElementById(parent), 'ith_form');
				oTools.mMap(oForms.aForms, oForms.mSetupForm);
			}
		},

		mValidateItemFunc : function(oField) {
			return function(e) {
				var oLabel=oForms.mGetFormElementLabel(oField);
				//var oElement=oForms.mGetFormElement(oField);
				var userDefValidate=oIThelp.mGetAttribute(oField, 'userdefvalidate');
				var sValidateString = unescape(escape(oIThelp.mGetAttribute(oField, 'validate').trim()).replaceAll('%A0', '%20'))
				var aValidate=sValidateString.split(' ');
				var message='';
				for (var i=0;i<aValidate.length;i++) {
					//alert(oForms.mCheck(oField.oElement, aValidate[i]));
					if ((message=oForms.mCheck(oField.oElement, aValidate[i]))!='') break;
				}
				if (message!='') {
					//alert('sd'+oLabel.className);
					oIThelp.mAddClass(oField.oLabel, 'invalid');
					//alert(oLabel.className);
					oField.oLabel.title=message;
				}
				else {
					if (oField.oLabel!=null) {
						oIThelp.mRemoveClass(oField.oLabel, 'invalid');				
						oField.oLabel.title='';
					}
				}
				return message;
			}
		},
		mCheck : function (oElement, validateStr) {
			var retVal='';
			var sType = oElement.tagName.toUpperCase() == 'TEXTAREA' ? 'text' : oElement.type;
			//alert(oElement.type+' '+oElement.tagName);
			if (oElement.tagName=='FIELDSET') {
				retVal='';
			}
			else {
				switch ( sType ) {
					case "text" :
						switch ( validateStr ) {
							//case "number"			: oForms.mTest ( /^[0-9]+(\,[0-9]+)?$/ ); break;
							//case "currency"			: oForms.mTest ( /^[0-9]{1,3}(\.[0-9]{3})*(\,[0-9]{1,2})?$/ ); break;
							case "valid_variable"	: if (oElement.value!='' && !/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(oElement.value)) retVal=oIThelp.mGetLocalName('VALIDATION_VARIABLE'); break;
							case "email"			: if (oElement.value!='' && !/^([a-zA-Z0-9_\-])+(\.([a-zA-Z0-9_\-])+)*@((\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\]))|((([a-zA-Z0-9])+(([\-])+([a-zA-Z0-9])+)*\.)+([a-zA-Z])+(([\-])+([a-zA-Z0-9])+)*))$/.test(oElement.value)) retVal=oIThelp.mGetLocalName('VALIDATION_EMAIL'); break;
							case "url"				: 
								var re = new RegExp('^(((ht|f)tp(s?))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$');
								if (oElement.value!='' && !oElement.value.match(re))
									retVal=oIThelp.mGetLocalName('VALIDATION_URL'); 
								break;
							case "colorcode" 		: if (oElement.value!='' && !/^#?([a-f]|[A-F]|[0-9]){3}(([a-f]|[A-F]|[0-9]){3})?$/.test( oElement.value )) retVal=oIThelp.mGetLocalName('VALIDATION_COLORCODE'); break;

							case "int"			: if (oElement.value!='' && !/^[0-9]+$/.test( oElement.value )) retVal=oIThelp.mGetLocalName('VALIDATION_INT'); break;
							case "required"		: if (oElement.value=='') retVal=oIThelp.mGetLocalName('VALIDATION_REQUIRED'); break;
							case "decimal"		: 
								oElement.oParentForm.aValidators[oElement.id]=[1, '', oElement.parentField];
								var fPostBackValidateDecimal=function(oElement) {
									return function (oRequest) {
										var beginContent=oRequest.responseText.indexOf('&lt;content&gt;');
										var endContent=oRequest.responseText.indexOf('&lt;/content&gt;');
										var resp=oRequest.responseText.substring(beginContent+15, endContent);
										resp=resp.replaceAll('&lt;','<').replaceAll('&gt;','>');
									
										if (!resp.contains('SUCCESS')) {
											oIThelp.mAddClass(oElement.parentField.oLabel, 'invalid');
											oElement.parentField.oLabel.title=oIThelp.mGetLocalName('VALIDATION_DECIMAL');
											//oElement.oParentForm.aValidators[oElement.id]=[0, 'Must be a decimal'];
											oElement.oParentForm.aValidators[oElement.id]=[0, resp, oElement.parentField];
											retVal = 'Must be a decimal';
										}
										else {
											oIThelp.mRemoveClass(oElement.parentField.oLabel, 'invalid');				
											oElement.parentField.oLabel.title='';
											oElement.oParentForm.aValidators[oElement.id]=[0, '', oElement.parentField];
											oElement.value = resp.substring(7);
										}
										
									}
								}
								//oXMLLib.mPostHTTPRequestByParam(oIThelp.mGetApplicationPath()+'/BackEnd.aspx?xslname=admin&mode=validate_decimal', 'decimalValue='+oElement.value, fPostBackValidateDecimal(oElement), null);
								oXMLLib.mPostHTTPRequestByParam(oIThelp.mGetAttribute(oElement.parentField, 'wsurl'), 'Admin', 'mode=validate_decimal;decimalValue='+oElement.value, fPostBackValidateDecimal(oElement), null);
								//oIThelp.mWaitTill(new function() {return function() { return oElement.processing==""}}, function(bool) {alert("p: "+oElement.processing);});
								break;
							case "spsite"		: 
								oElement.oParentForm.aValidators[oElement.id]=[1, '', oElement.parentField];
								oElement.processing = 'process';
								var fPostBackValidateSpSite=function(oElement) {
									return function (oRequest) {
										var beginContent=oRequest.responseText.indexOf('&lt;content&gt;');
										var endContent=oRequest.responseText.indexOf('&lt;/content&gt;');
										var resp=oRequest.responseText.substring(beginContent+15, endContent);
										resp=resp.replaceAll('&lt;','<').replaceAll('&gt;','>');
									
										if (!resp.contains('SUCCESS')) {
											oIThelp.mAddClass(oElement.parentField.oLabel, 'invalid');
											oElement.parentField.oLabel.title=oIThelp.mGetLocalName('VALIDATION_SPSITE');
											oElement.oParentForm.aValidators[oElement.id]=[0, resp, oElement.parentField];
											retVal = 'Sharepoint Site does not exist';
										}
										else {
											oIThelp.mRemoveClass(oElement.parentField.oLabel, 'invalid');				
											oElement.parentField.oLabel.title='';
											oElement.oParentForm.aValidators[oElement.id]=[0, '', oElement.parentField];
											//oElement.value = resp.substring(7);
										}
										oElement.processing = '';
									}
								}
								oXMLLib.mPostHTTPRequestByParam(oIThelp.mGetAttribute(oElement.parentField, 'wsurl'), 'Admin', 'mode=validate_spsite;url='+oElement.value, fPostBackValidateSpSite(oElement), null);
								//oIThelp.mWaitTill(new function() {return function() { return oElement.processing==''}}, function(bool) {alert("p: "+oElement.processing);});
								break;
							default					: try { retVal=eval(validateStr); } catch ( exception ) {}; break;
						}
						if (validateStr.toUpperCase().contains('USERDEF')) {
							oElement.oParentForm.aValidators[oElement.id]=[1, '', oElement.parentField];
							var func=null;
							eval('func=oIThelp.oUserDef.'+validateStr.substring(8));
							eval('func.oField=oElement.parentField');
							
							var fPostBackUserDef=function(oElement) {
								return function (oRequest) {
									var resp=oRequest.responseText.substring(9,oRequest.responseText.length-10);
									
									if (!resp.contains('SUCCESS')) {
										oIThelp.mAddClass(oElement.parentField.oLabel, 'invalid');
										oElement.parentField.oLabel.title=resp;
										oElement.oParentForm.aValidators[oElement.id]=[0, resp, oElement.parentField];
									}
									else {
										oIThelp.mRemoveClass(oElement.parentField.oLabel, 'invalid');				
										oElement.parentField.oLabel.title='';
										oElement.oParentForm.aValidators[oElement.id]=[0, '', oElement.parentField];
									}
									
								}
							}
							
							oXMLLib.mPostHTTPRequestByParam(oIThelp.mGetApplicationPath()+'/BackEnd.aspx?xslname='+func.xslname+'&mode='+func.mode, func.getParams(), fPostBackUserDef(oElement), null);
						}
						break;
					case "select-one" :
						switch ( validateStr ) {
							case "required"	: if (oElement[oElement.selectedIndex].value=='-1') retVal='Required'; break;
							default		: try { retVal=eval(validateStr); } catch ( exception ) {}; break;
						}
						break;
					case "select-multiple" :
						switch ( validateStr ) {
							case "required"	: if (oElement.options.length == 0) retVal='Required'; break;
						}
						break;
				}
			}
			return retVal;
		},
		
		mRemoveFields : function(oForm, oNode){
			var aFs=oIThelp.getAllElementsByClassName(oNode, 'ith_field');
			for (var o = 0; o < aFs.length; o++){
				if(oForm.aElements[aFs[o].oElement.id]) oForm.aElements[aFs[o].oElement.id] = null;
				if(oForm.aFields[aFs[o].fieldCount]) oForm.aFields[aFs[o].fieldCount] = null;
			}	
			
		},
		// oForm: ith_form, oNode: node within this function parameter the fields
		// This function is created to make the possibility to add further form elements to the form.
		mSetupFields : function (oForm, oNode) { 
			var aFs=oIThelp.getAllElementsByClassName(oNode, 'ith_field');
			//oForm.aFields=oIThelp.getAllElementsByClassName(oNode, 'ith_field');
			if(!oForm.aElements) oForm.aElements={};
			var oPopup=oIThelp.getParentByClassName(oForm, 'popped');
			var i=0;
			while ((oField=aFs[i++])!=null) {

				oField.fieldCount = oForm.aFields.length;
				oGod.mPush(oForm.aFields, oField);
				// Initialize Fields and Elements
				oField.oControlFrame = oField.getElementsByTagName('SPAN')[0];
				oField.oElement=oForms.mGetFormElement(oField.oControlFrame);
				oForm.aElements[oField.oElement.id]=oField.oElement;
				oField.oElement.parentField=oField;
				oField.oElement.oParentForm=oForm;
				if (oIThelp.containClassName(oField.className, 'ith_colorpicker')) {
					
					oIThelp.mAddClass(oField.oElement,'readonly');
					oField.oElement.setAttribute('disabled', 'disabled');
					
					var origWidth=oField.oElement.offsetWidth;
					oField.oElement.style.width=(origWidth-24)+'px';
					//alert(oField.oElement.value+' '+oField.oElement.value.charAt(0)+' '+oField.oElement.value.length);
					if (oField.oElement.value!='' && oField.oElement.value.charAt(0)=='#' && oField.oElement.value.length==7) {
						oField.oElement.style.backgroundColor=oField.oElement.value;
						oField.oElement.style.color=oIThelp.mGetInverseColor(oField.oElement.value);
					}
					
					// Setup oA
					var oA=document.createElement('a');
					oA.style.position='absolute';
					oA.style.top='15px';
					oA.style.left=(origWidth-22)+'px';
					oA.href=oGod.DUMMY_LINK;
					oA.oElement=oField.oElement;
					oA.close=function() {
						oIThelp.mRemoveClass(this.oPalette, "selected");
						oIThelp.mRemoveClass(this.oPalette.oShim, "selected");
					}
					oA.onclick = function ( e ) {
						this.oPalette.oA=this;
						var r=oIThelp.getAbsolutePos(this, oPopup);
						this.oPalette.style.left=r.x+'px';
						this.oPalette.style.top=r.y+'px';
						this.oPalette.oShim.style.left=r.x+'px';
						this.oPalette.oShim.style.top=r.y+'px';
						oIThelp.mAddClass(this.oPalette, "selected");
						oIThelp.mAddClass(this.oPalette.oShim, "selected");
					}
					if ( oClient.bOpera ) {
						// and safari too?
						oA.onclick = oA.focus;
					}
					oField.appendChild(oA);
					// Setup oImg
					var oImg=document.createElement('img');
					oImg.src=oIThelp.mGetApplicationPath()+'/Wendy/Brix/Images/colorpicker.gif';
					oImg.style.height='20px';
					oImg.style.width='20px';
					oImg.style.position='relative';
					oImg.id='img_palette_'+oField.oElement.id;
					oImg.style.cursor='pointer';
					oImg.oElement=oField.oElement;
					oA.appendChild(oImg);
					
					// Setup oPalette and oShim (iframe)
					var oPalette=null;
					var oShim=null;
					for (var j=0;j<oPopup.childNodes.length;j++) {
						if (oPopup.childNodes[j].nodeType!=Node.TEXT_NODE) {
							if (oPopup.childNodes[j].className.contains('palette') && oPopup.childNodes[j].tagName=='DIV') {
								oPalette=oPopup.childNodes[j];
							}
						}
					}
					if (oPalette==null) {
						oPalette=document.createElement('div');
						oPalette.className="palette";
						oPalette.style.zIndex=parseInt(oPopup.style.zIndex)+2;
						oPalette.style.position='absolute';
						var r=oIThelp.getAbsolutePos(oImg, oPopup);
						oPalette.oA=oA;
						oA.oPalette=oPalette;
						oForms.drawPalette(oPalette);
						oPopup.appendChild(oPalette);
						
						var oShim=document.createElement('iframe');
						oShim.src="App/Cuba/Images/spacer.gif";
						oShim.style.zIndex=parseInt(oPopup.style.zIndex)+1;
						oShim.className="palette";
						oShim.style.position='absolute';
						oShim.scrolling='no';
						oShim.frameBorder='0';
						oPopup.appendChild(oShim);
						oPalette.oShim=oShim;
					}
					else {
						oA.oPalette=oPalette;
					}
				}

				// Set date and datetime pickers
				if (oIThelp.containClassName(oField.className, 'ith_date') || oIThelp.containClassName(oField.className, 'ith_datetime')) {
					oIThelp.mAddClass(oField.oElement,'readonly');
					oField.oElement.setAttribute('disabled', 'disabled');
					
					//oDebug.alertA(oField.oElement);
					var origWidth=oField.oElement.offsetWidth;
					
					oField.oElement.style.width=(origWidth-24)+'px';

					// Setup oA
					var oA=document.createElement('a');
					oA.style.position='absolute';
					oA.style.left=(origWidth-22)+'px';
					oA.href=oGod.DUMMY_LINK;
					oA.oElement=oField.oElement;
					
					oImg=document.createElement('img');
					oImg.src=oIThelp.mGetApplicationPath()+'/App/Cuba/Images/calendar.gif';
					oImg.style.width='20px';
					oImg.style.height='20px';
					oImg.id='img_diag_'+oField.oElement.id;
					oImg.style.cursor='pointer';
					oImg.oElement=oField.oElement;
					if (oIThelp.mGetAttribute(oField,'attach')) {
						var ii=0;
						var otherId=oIThelp.mGetAttribute(oField,'attach');
						var oField2=null;
						while ((oField2=oForm.aFields[ii++])!=null) {
							var oElement2=oForms.mGetFormElement(oField2.getElementsByTagName('SPAN')[0]);
							if (oElement2.id==otherId) {
								oImg.oAttachedElement=oElement2;
								oElement2.uniqueFormat=oIThelp.mGetAttribute(oElement2,'format');
								break;
							}
						}
					}
					oA.oImg=oImg;
					oA.appendChild(oImg);
					
					if (oIThelp.containClassName(oField.className, 'ith_datetime')) {
						var sDateFormat = oIThelp.mGetAttribute(oField.oElement,'format') ? oIThelp.mGetAttribute(oField.oElement,'format') : oNames.DATETIME_DEF;
						oImg.onclick=function(e) {
							return oForms.mShowCalendar(this.oElement, this, sDateFormat, '24', true);
						}
						oA.onkeyup=function(e) {
							e = e ? e : window.event;
							if ( e.type == "keyup" && (e.keyCode == 13)) {
								return oForms.mShowCalendar(this.oElement, this.oImg, sDateFormat, '24', true);
							}
						}
					}
					else {
						var sDateFormat = oIThelp.mGetAttribute(oField.oElement,'format') ? oIThelp.mGetAttribute(oField.oElement,'format') : oNames.DATE_DEF;
						oImg.onclick=function(e) {
							return oForms.mShowCalendar(this.oElement, this, sDateFormat, null, true);
						}
						oA.onkeyup=function(e) {
							e = e ? e : window.event;
							if ( e.type == "keyup" && (e.keyCode == 13)) {
								return oForms.mShowCalendar(this.oElement, this.oImg, sDateFormat, null, true);
							}
						}
					}
					
					// Picker could be empty
					if(oIThelp.mGetAttribute(oField,'emptyfunc') == 'true'){
						var oImgEmp=document.createElement('img');
						oImgEmp.src=oIThelp.mGetApplicationPath()+'/App/Cuba/Images/icon_delete.gif';
						oImgEmp.id='img_emp_'+oField.oElement.id;
						oImgEmp.style.cursor='pointer';
						oImgEmp.oElement=oField.oElement;
						oA.oImgEmp=oImgEmp;
						oA.appendChild(oImgEmp);
						oImgEmp.onclick=function(e) {
							this.oElement.value = '';
							if(oA.oImg.oAttachedElement)
								oA.oImg.oAttachedElement.value = '';
						}
					}
					oField.oControlFrame.appendChild(oA);
				}

				// Set general picker
				if (oIThelp.containClassName(oField.className, 'picker')) {
					
					oIThelp.mAddClass(oField.oElement,'readonly');
					oField.oElement.setAttribute('disabled', 'disabled');
					var origWidth=oField.oElement.offsetWidth;
					oField.oElement.style.width=(origWidth-20)+'px';
					oField.validate=oIThelp.mGetAttribute(oField,'validate');
					oImg=document.createElement('img');
					
					oImg.src=oIThelp.mGetApplicationPath()+'/App/Cuba/Images/picker.gif';
					oImg.style.width='20px';
					oImg.style.height='20px';
					//oImg.style.top='14px';
					oImg.style.left=(origWidth-18)+'px';
					oImg.style.position='absolute';
					oImg.id='img_picker_'+oField.oElement.id;
					oImg.style.cursor='pointer';
					oImg.oElement=oField.oElement;
					oImg.oForm=oForm;
					oImg.popperId=oIThelp.mGetAttribute(oField,'popper');
					oImg.xslPath=oIThelp.mGetAttribute(oField,'xslpath');
					oImg.param= oIThelp.mGetAttribute(oField,'param');
					oImg.url= oIThelp.mGetAttribute(oField,'url');
					oImg.paramValidation = oIThelp.mGetAttribute(oField,'paramvalidation');
					
					// Label Edit params
					oImg.isLabelEdit = oIThelp.containClassName(oField.className, 'labeledit');
					oImg.xmlDataObject = oIThelp.mGetAttribute(oField,'xmldataobject');
					oImg.labelXpath = oIThelp.mGetAttribute(oField,'labelxpath');

					oImg.onclick=function(e) { 
						this.param= oIThelp.mGetAttribute(this.oElement.parentField,'param'); // refresh settings
						var bValidation = true;
						if (this.paramValidation) {
							bValidation = eval(this.paramValidation.split(";")[0]);
							if(!bValidation) alert(this.paramValidation.split(";")[1]);
						}
						if (bValidation) {
							var sParam=""; 	var aParams = this.param != null ? this.param.split("@sep") : [];
							for (var i = 0; i < aParams.length; i++) {
								sParam += sParam == "" ? aParams[i] : "@sep"+aParams[i];
							}
							if(this.isLabelEdit){
								sParam += '@sepdata_dom_object@eq' + this.xmlDataObject;
								sParam += '@sepithxml_data@eq' + document.getElementById(this.xmlDataObject).value;
								sParam += '@seplabelxpath@eq' + this.labelXpath;
								sParam += '@seplabel_dom_object@eq' + this.oElement.id;
								var aLanguages = document.getElementById('all_languages').value.split('@sep'), sLangXml = '<languages>';
								for(var la = 0; la < aLanguages.length; la++){
									if(aLanguages[la] != ''){
										sLangXml += '<language>';
										sLangXml += '<id>' + aLanguages[la].split('@eq')[0] + '</id>';
										sLangXml += '<name>' + aLanguages[la].split('@eq')[1] + '</name>';
										sLangXml += '</language>';
									}
								}
								sLangXml += '</languages>';
								sParam += '@sepithxml_all_languages@eq'+sLangXml;
							}
							var popper = document.getElementById ( this.popperId );
							popper.opener = this;
							popper.mPop ( true, e );
							//alert(this.url + ' - ' + this.xslPath + ' - ' + this.popperId + ' - ' + sParam);
							oIThelp.callPopper(this.url, this.xslPath, this.popperId, sParam);
						}
					}
					
					oField.observer=oIThelp.mGetAttribute(oField,'observer');
					if (oField.observer) {
						oIThelp.mAttachObserver(oField.observer, oField, 'mChooser');
						oField.mChooser=function(id, params) {
							if (this.validate) {
								var aValidate=this.validate.split(' ');
								for (var i=0;i<aValidate.length;i++) {
									if (aValidate[i].toUpperCase().contains('USERDEF')) {
										eval('oIThelp.oUserDef.'+aValidate[i].substring(8)+'.params=\''+params+'\'');
									}
								}
							}
							oForms.mValidateItemFunc(this)(null);
						}
					}
					
					oField.appendChild(oImg);
				}
			
				// Hide hidden elements
				if (oField.className.contains('hidden')) {
					oField.style.display='none';
				}
			
				// Set Disabled items
				if (oField.className.contains('disabled')) {
					oForms.mGetFormElement(oField.oControlFrame).setAttribute('disabled', 'disabled');
					oIThelp.mAddClass(oForms.mGetFormElement(oField.oControlFrame),'readonly');
				}
				
				// Set behaviour for search fields
				if(oIThelp.containClassName(oField.oElement.className, 'ith_search')) {
					oField.oElement.onfocus = new function(){return function(){oIThelp.mRemoveClass(this, 'ith_search');}};
					oField.oElement.onblur = new function(){return function(){if(this.value.trim() == '') oIThelp.mAddClass(this, 'ith_search');}};
				}
				
				// Set validation				
				if (oIThelp.mGetAttribute(oField, 'validate')!=undefined) {
					oField.oLabel=oForms.mGetFormElementLabel(oField);
					oField.oLabel.text=oField.oLabel.firstChild.nodeValue;
					oField.oElement.onblur=oForms.mValidateItemFunc(oField);
					if (oIThelp.mGetAttribute(oField, 'validate').contains('required')) {
						oField.oLabel.firstChild.nodeValue=oField.oLabel.firstChild.nodeValue+'*';
					}
				}
				// Set comment
				if (oIThelp.mGetAttribute(oField, 'comment')!=undefined) 
					oForms.mGetFormElement(oField.oControlFrame).title=oIThelp.mGetAttribute(oField, 'comment');
				
				// Set value escaping
				if (oIThelp.mGetAttribute(oField, 'escapeparam')!=undefined)
					oField.escapeParam = true;

				// Set Focus
				if (oField.className.contains('focus')) 
					oIThelp.mFocus(oForms.mGetFormElement(oField.oControlFrame));
			}
			
			var aFieldSets=oIThelp.getAllElementsByClassName(oForm, 'ith_fieldset');
			//oForm.aFieldSets=oIThelp.getAllElementsByClassName(oForm, 'ith_fieldset');
			var k=0;
			while ((oFieldSet=oForm.aFieldSets[k++])!=null) {	
				oGod.mPush(oForm.aFieldSets, oFieldSet);
				var aFields=oForms.mGetFormElements(oFieldSet);
				var l=0;
				while ((oField=aFields[l++])!=null) {
					var relateId=oIThelp.mGetAttribute(oField, 'relate');
					if (relateId!=null) {
						oField.oRelate=document.getElementById(relateId);
						var m=0;
						var aTemp=[];
						while ((oField2=aFields[m++])!=null) {
							if (oField2!=oField)
								aTemp.push(oField2);
						}
						oField.aOthers=aTemp;
						oField.onmouseup=function() {
								this.oRelate.style.display='block';
								for (var i=0;i<this.aOthers.length;i++)
									this.aOthers[i].oRelate.style.display='none';
						}
						oField.onkeyup=function(e) {
								e = e ? e : window.event;
								//alert(e.keyCode);
								if ( e.type == "keyup" && e.keyCode == 9 || e.keyCode == 16 ) return;
								if ( e.type == "keyup" && (e.keyCode == 32 || e.keyCode== 13 || e.keyCode == 39 || e.keyCode == 37)) {
									this.checked=true;
									this.oRelate.style.display='block';
									for (var i=0;i<this.aOthers.length;i++)
										this.aOthers[i].oRelate.style.display='none';
								}
						}
					}
				}
			}
		},
		
		mSetupForm : function(oForm) {
			oForm.notifier=oIThelp.mGetAttribute(oForm, 'notifier');
			oForm.observer=oIThelp.mGetAttribute(oForm, 'observer');
			oForm.aValidators={};
			if (oForm.observer) {
				var aObservers=oForm.observer.split(';');
				for (var iii=0;iii<aObservers.length;iii++) {
					var sSubject=aObservers[iii].trim();
					oIThelp.mAttachObserver(sSubject, oForm);
				}
			}
			oForm.mUpdateObserver = oForms.mUpdateObserver;
			
			oForm.aFields=[];
			oForm.aFieldSets=[];
			
			// Setups fields, the second parameter tells the area within the fields must be initialized. OForm means all field must be initialized under oForm
			oForms.mSetupFields(oForm, oForm);
		
			//Set validation
			oForm.mValidateForm=oForms.mValidateForm; 
			oForm.mEnableValidation=oForms.mEnableValidation;
			
			//Collect field values
			oForm.mCollect=oForms.mCollect;
			oForm.mGetId=oForms.mGetId;
			oForm.parentGrid=oIThelp.mGetAttribute(oForm, 'parentgrid');
			oForm.resultNodeId=oIThelp.mGetAttribute(oForm, 'resultnode');
			
			//Set submit buttons
			if (oIThelp.getAllElementsByClassName(oForm, 'submit').length==1) {
				var oSubmit=oIThelp.getAllElementsByClassName(oForm, 'submit')[0];
				oForms.mSetupSubmit(oSubmit, oForm);
			}
			
			//alert(oForm.id+' finished');
			if (oIThelp.getAssociativeArrayLength(oIThelp.aLoadingIds) > 0)
				oIThelp.mStopLoading('openPopper');
			return oForm;
		},
		
		mUpdateObserver : function(id, params) {
			var aParams = params.split(";");
			var oParams = {};
			for (var j=0;j<aParams.length;j++) {
				var aParam=aParams[j].split("=");
				oParams[aParam[0]]=oIThelp.mUnEscape(aParam[1]);
			}
			for (var oItem in this.aElements) {
				if (oParams[oItem]) {
					this.aElements[oItem].value = oParams[oItem];
				}
			}
		},
		
		mSetupSubmit : function(oSubmit, oForm) {
			var i=0;
			var oNode;
			while ((oNode=oSubmit.childNodes[i++])!=null) {
				if (oNode.nodeType!=Node.TEXT_NODE) {
					if (oNode.className.contains("commandButton")) {
						switch (oNode.id) {
							case 'xmllabelsave' :
								var oA = document.createElement ( "a" );
								oA.href = oGod.DUMMY_LINK;
								oA.appendChild ( document.createTextNode ( oNode.firstChild.nodeValue));
								var sUrl=oIThelp.mGetAttribute(oNode, 'url');
								var sXslPath=oIThelp.mGetAttribute(oNode, 'xslpath');
								var sDataDomObject=oIThelp.mGetAttribute(oNode, 'data_dom_object');
								var sLabelDomObject=oIThelp.mGetAttribute(oNode, 'label_dom_object');
								var sParam=oIThelp.mGetAttribute(oNode, 'param');
								var fSave=function (sXslPath, sParam, sCommand, oForm) {
									return function(e) {
										var aForms = oIThelp.getAllElementsByClassName(oIThelp.getParentByClassName(oForm, 'popped'), 'ith_form');
										var message='';
										var sAllParams ='';
										for (var y = 0; aForms.length > y; y++) {
											oIThelp.getAssociativeArrayLength(aForms[y].aElements);
											sAllParams += sAllParams != '' && oIThelp.getAssociativeArrayLength(aForms[y].aElements) > 0 ? '@sep' : '';
											message += aForms[y].mValidateForm();
											sAllParams += aForms[y].mCollect();
										}
										sAllParams += "@sep"+sParam;
										//alert(sAllParams);
										
										var	labelEscape = function(name) {
											return typeof(name) != 'undefined' ? name.replaceAll(';', '$ith_sc').replaceAll('=', '$ith_eq').replaceAll('@', '$ith_at').replaceAll('>', '$ith_gt').replaceAll('<', '$ith_lt') : name;
										}
										var validFunc=function() {
											for (var y = 0; aForms.length > y; y++) {
												for (var validator in aForms[y].aValidators) {
													if (aForms[y].aValidators[validator][1]!='') message+=(message==''?'':'\n')+aForms[y].aValidators[validator][2].oLabel.text+': '+aForms[y].aValidators[validator][1];
												}
											}
											if (message!='') {
												alert(message);
												return false;
											}
											var sNewLabelXML = '<label>', aLabels = sAllParams.split('@sep'), sDefaultLabel = '';
											for(var l = 0; l < aLabels.length; l++){
												if(aLabels[l].contains('labelfield_')){
													var bIsDefault = aLabels[l].contains('labelfield_default_');
													var aLabelParams = aLabels[l].split('@eq');
													var sLang = aLabelParams[0].substring((bIsDefault ? 19 : 11));
													var sLabelText = labelEscape(aLabelParams[1]);
													if(bIsDefault) sDefaultLabel = sLabelText;
													if(!bIsDefault && sLabelText.trim() == ''){
														sNewLabelXML += '<text lang="'+sLang+'" autogenerated="yes">' + sDefaultLabel + '</text>';
													}
													else{
														sNewLabelXML += '<text lang="'+sLang+'">' + sLabelText + '</text>';
													}
												}
											}
											sNewLabelXML += '</label>';
											//alert(sNewLabelXML);
											sAllParams += '@sepithxml_label@eq'+sNewLabelXML;
											var fPostBackSave=function (oRequest, nodeId) {
												oIThelp.mStopPlainLoading();
												var resp = oIThelp.getCallBackEndContent(oRequest.responseText);
												if(resp.contains('<error>')){ alert(resp); return false;}
												//alert(resp);
												oIThelp.mStopLoading('save');
												document.getElementById(sDataDomObject).value = resp;
												document.getElementById(sLabelDomObject).value = sDefaultLabel;
												var oPopup=oIThelp.getParentByClassName(oForm, 'popped');
												if (oPopup!=undefined) oPopup.mPop ( false );
											}
											oIThelp.mPlainLoading();
											oXMLLib.mCallBackEnd(sUrl, sAllParams, fPostBackSave, oNode);
											//oXMLLib.mPostHTTPRequestByParam(sWSURL, sXslPath, sAllParams, fPostBackSave, null);
										}
										validFunc();
									}
								}
								oA.onclick = fSave(sXslPath, sParam, sCommand, oForm);
								oNode.parentNode.replaceChild ( oA, oNode );
								
								break;
							case 'save' :
								var oA = document.createElement ( "a" );
								oA.href = oGod.DUMMY_LINK;
								oA.appendChild ( document.createTextNode ( oNode.firstChild.nodeValue));
								var sUrl=oIThelp.mGetAttribute(oNode, 'url');
								var sCommand=oIThelp.mGetAttribute(oNode, 'command');
								var sParam=oIThelp.mGetAttribute(oNode, 'param');
								var fSave=function (sXslPath, sParam, sCommand, oForm) {
									return function(e) {
										var aForms = oIThelp.getAllElementsByClassName(oIThelp.getParentByClassName(oForm, 'popped'), 'ith_form');
										var message='';
										var sAllParams ='';
										for (var y = 0; aForms.length > y; y++) {
											oIThelp.getAssociativeArrayLength(aForms[y].aElements);
											sAllParams += sAllParams != '' && oIThelp.getAssociativeArrayLength(aForms[y].aElements) > 0 ? '@sep' : '';
											message += aForms[y].mValidateForm();
											sAllParams += aForms[y].mCollect();
										}
										//sAllParams += sParam ? "@sep" + sParam : '';
										//alert(sAllParams);
										
										var validFunc=function() {
											for (var y = 0; aForms.length > y; y++) {
												for (var validator in aForms[y].aValidators) {
													if (aForms[y].aValidators[validator][1]!='') message+=(message==''?'':'\n')+aForms[y].aValidators[validator][2].oLabel.text+': '+aForms[y].aValidators[validator][1];
												}
											}
											if (message!='') {
												alert(message);
												return false;
											}
											if(sCommand){var s = eval(sCommand);if(s!=''){alert(s); return false;}}
											var fPostBackSave=function (oRequest, nodeId) {
												oIThelp.mStopPlainLoading();
												var resp = oIThelp.getCallBackEndContent(oRequest.responseText);
												//alert(resp);
												if(!oRequest.responseText.contains('<content') && oRequest.responseText.contains('<error>') && oRequest.responseText.contains('<message>')){
													var beginContent=oRequest.responseText.indexOf('<message>');
													var endContent=oRequest.responseText.indexOf('</message>');
													var respTmp=oRequest.responseText.substring(beginContent+9, endContent);
													alert(respTmp); return false;
												}
												else if(!oRequest.responseText.contains('<content')){
													alert('Unexpected error.\n'+oRequest.responseText); return false;
												}
												else if(!resp.toUpperCase().contains('SUCCESS')){alert("Data has not been saved!\nMessage:\n"+resp); return false;}
												if (resp.toUpperCase().contains('SUCCESS')) {
													resp = resp.trim().substring(8);
													//alert(resp);
													var iPointer = resp.contains('@sep') ? resp.indexOf('@sep') : resp.indexOf(";");
													var sId = resp.substring(0, iPointer > -1 ? iPointer : resp.length);
													//alert(sId);
													resp = resp.substring(sId.length);
													//alert(resp);
													var backParams = resp.contains('@sep') ? resp.split('@sep') : resp.split(";");
													for (w = 0; w < backParams.length; w++) {
														sAllParams += sAllParams != "" ? (resp.contains('@sep') ? '@sep' : ';')+oIThelp.mUnEscape(backParams[w]) : oIThelp.mUnEscape(backParams[w]);
													}
													//alert(sAllParams);
													if (sParam.toUpperCase().contains('TYPE=NEW')) {
														if(oForm.notifier){
															var aSubjects=oForm.notifier.split(';');
															for (var ii=0;ii<aSubjects.length;ii++) {
																var sSubject=aSubjects[ii].trim();
																//alert(sSubject + '  -  ' + sAllParams + '  -  ' + sId);
																if (sSubject.toUpperCase().contains("UPDATE"))
																	oIThelp.mUpdateObserver(sSubject, oForm.mGetId().split('=')[1], sAllParams);
																else if (sSubject.toUpperCase().contains("INSERT"))
																	oIThelp.mUpdateObserver(sSubject, sId, sAllParams);
																else oIThelp.mUpdateObserver(sSubject, sId, sAllParams);
															}
														}
													}
													else {
														var aSubjects=oForm.notifier.split(';');
														for (var ii=0;ii<aSubjects.length;ii++) {
															var sSubject=aSubjects[ii].trim();
															//alert(sSubject + '  -  ' + sAllParams);
															if (sSubject.toUpperCase().contains("UPDATE")){
																oIThelp.mUpdateObserver(sSubject, oForm.mGetId().split('=')[1], sAllParams);
															}
															else oIThelp.mUpdateObserver(sSubject, oForm.mGetId().split('=')[1], sAllParams);
														}
													}
													var oPopup=oIThelp.getParentByClassName(oForm, 'popped');
													if (oPopup!=undefined) oPopup.mPop ( false );
												}
											}
											//oXMLLib.mPostHTTPRequestByParam(sWSURL, sXslPath, sAllParams, fPostBackSave, oNode);
											oIThelp.mPlainLoading();
											oXMLLib.mCallBackEnd(sUrl + '&' + sParam, sAllParams, fPostBackSave, oNode);
										}
										oIThelp.mPlainLoading();
										//oIThelp.mLoading('save', oNames['PLEASE_WAIT']);
										
										oIThelp.mWaitTill(
											function() {
												for (var y = 0; aForms.length > y; y++) {
													for (var validator in aForms[y].aValidators) {
														if (aForms[y].aValidators[validator][0]==1) {
															return false;
														}
													}
												}
												oIThelp.mStopPlainLoading();
												//oIThelp.mStopLoading('save');
												return true;
											}, validFunc);
										
									}
								}
								oA.onclick = fSave(sXslPath, sParam, sCommand, oForm);
								oNode.parentNode.replaceChild ( oA, oNode );
							break;
							case 'delete' : 
								var oA = document.createElement ( "a" );
								oA.href = oGod.DUMMY_LINK;
								oA.appendChild ( document.createTextNode ( oNode.firstChild.nodeValue));
								var sWSURL=oIThelp.mGetAttribute(oNode, 'wsurl');
								var sXslPath=oIThelp.mGetAttribute(oNode, 'xslpath');
								var sCommand=oIThelp.mGetAttribute(oNode, 'command');
								var sParam=oIThelp.mGetAttribute(oNode, 'param');
								var fDelete=function (sXslPath, sParam, sCommand, oForm) {
									return function(e) {
										eval(sCommand);
										if (!confirm(oNames['CONFIRM_DELETE_ITEM'])) return;
										var fPostBackDelete=function (oRequest, nodeId) {
											//var resp=oRequest.responseText.substring(9,oRequest.responseText.length-10);
											var beginContent=oRequest.responseText.indexOf('&lt;content&gt;');
											var endContent=oRequest.responseText.indexOf('&lt;/content&gt;');
											var resp=oRequest.responseText.substring(beginContent+15, endContent);
											resp=resp.replaceAll('&lt;','<').replaceAll('&gt;','>');
											var notDel = oRequest.responseText.indexOf("COLUMN REFERENCE");
											if (resp.contains('SUCCESS')) {
												var oPopup=oIThelp.getParentByClassName(oForm, 'popped');
												if (oPopup!=undefined) oPopup.mPop ( false );
												
												var aSubjects=oForm.notifier.split(';');
												for (var ii=0;ii<aSubjects.length;ii++) {
													var sSubject=aSubjects[ii].trim();
													if (sSubject.toUpperCase().contains("DELETE"))
														oIThelp.mUpdateObserver(sSubject, oForm.mGetId().split('=')[1], sParam);
												}
											}
											else if (notDel != -1) alert(oNames["DONOT_DELETE_ROW"]);
											else oIThelp.mMessageBox(resp, 'Error report',1, 0, "FORM_"+oForm.id+"_"+oForm.mGetId().split('=')[1]+"_"+"delete");
										}
										oXMLLib.mPostHTTPRequestByParam(sWSURL, sXslPath, sParam+";"+oForm.mGetId(), fPostBackDelete, oNode);
										//oXMLLib.mPostHTTPRequestByParam(oIThelp.mGetApplicationPath()+'/BackEnd.aspx?xslname='+sXslPath+'&'+sParam, oForm.mGetId(), fPostBackDelete, oNode);
										return false;
									}
								}
								oA.onclick = fDelete(sXslPath, sParam, sCommand, oForm);
								oNode.parentNode.replaceChild ( oA, oNode );
							break;
							case 'choose' : // Only grid & listbox can be notifier
								var oA = document.createElement ( "a" );
								oA.href = oGod.DUMMY_LINK;
								oA.appendChild ( document.createTextNode ( oNode.firstChild.nodeValue));
								var sCommand=oIThelp.mGetAttribute(oNode, 'command');

								var fChoose = function(oForm) {
									return function(e) {
										var oNotifier = oIThelp.getAllElementsByClassName(oForm, 'grid').length > 0 ? oIThelp.getAllElementsByClassName(oForm, 'grid') : oIThelp.getAllElementsByClassName(oForm, 'listbox');
										if (oNotifier.length != 0) {
											var aSelectedRows = oGrids.mGetSelectedRowsToArray(oNotifier[0].id);
											if (aSelectedRows.length == 0) alert(oIThelp.mGetLocalName("PLEASE_SELECT_ROW"));
											else {
												var bIsOk = sCommand ? eval(sCommand) : true;
												if (bIsOk) {
													var oPopup=oIThelp.getParentByClassName(oForm, 'popped');
													if (oPopup!=undefined) oPopup.mPop ( false );
													var aSubjects=oForm.notifier.split(';');
													for (i = 0; i < aSelectedRows.length; i++) {
														var aSubjects=oForm.notifier.split(';');
														for (var ii=0;ii<aSubjects.length;ii++) {
															var sSubject=aSubjects[ii].trim();
															if (sSubject.toUpperCase().contains("UPDATE"))
																oIThelp.mUpdateObserver(sSubject, aSelectedRows[i].rowid, aSelectedRows[i].params);
															if (sSubject.toUpperCase().contains("INSERT"))
																oIThelp.mUpdateObserver(sSubject, aSelectedRows[i].rowid, aSelectedRows[i].params);
															if (sSubject.toUpperCase().contains("CHOOSE"))
																oIThelp.mUpdateObserver(sSubject, aSelectedRows[i].rowid, aSelectedRows[i].params);																
														}
													}
												}
											}
										}
										else alert(oIThelp.mGetLocalName("PLEASE_SELECT_ROW"));
									}
								}
								oA.onclick = fChoose(oForm);
								oNode.parentNode.replaceChild ( oA, oNode );
							break;
							case 'cancel' :
								var oA = document.createElement ( "a" );
								oA.href = oGod.DUMMY_LINK;
								oA.appendChild ( document.createTextNode ( oNode.firstChild.nodeValue));
								var fCancel=function (sCommand, oForm) {
									return function(e) {
										if(sNoQuestion == 'true'){
											eval(sCommand);
											var oPopup=oIThelp.getParentByClassName(oForm, 'popped');
											if (oPopup!=undefined) oPopup.mPop ( false );
										}
										else if (confirm(oNames["CLOSE_FORM"])){
											eval(sCommand);
											var oPopup=oIThelp.getParentByClassName(oForm, 'popped');
											if (oPopup!=undefined) oPopup.mPop ( false );
										}
									return false;
									}
								}
								//oNode.mPop ( false );
								var sCommand=oIThelp.mGetAttribute(oNode, 'command');
								var sNoQuestion=oIThelp.mGetAttribute(oNode, 'noquestion');
								oA.onclick = fCancel(sCommand, oForm);
								oNode.parentNode.replaceChild ( oA, oNode );
								
							break;
							case 'search' : 
								var oA = document.createElement ( "a" );
								oA.href = oGod.DUMMY_LINK;
								oA.appendChild ( document.createTextNode ( oNode.firstChild.nodeValue));
								var sXslPath=oIThelp.mGetAttribute(oNode, 'xslpath');
								var sCommand=oIThelp.mGetAttribute(oNode, 'command');
								var sParam=oIThelp.mGetAttribute(oNode, 'param');
								var fSearch=function (sXslPath, sParam, sCommand, oForm) {
									return function(e) {
										var message='';
										if ((message=oForm.mValidateForm())!='') {
											alert(message);
											return false;
										}
										eval(sCommand);
										if (oForm.resultNodeId!=null) {
											oXMLLib.mPostHTTPRequestByParam(oIThelp.mGetApplicationPath()+'/BackEnd.aspx?xslname='+sXslPath+'&'+sParam, oForm.mCollect(), oIThelp.callBackBase, oForm.resultNodeId);
										}
										return false;
									}
								}
								oA.onclick = fSearch(sXslPath, sParam, sCommand, oForm);
								oNode.parentNode.replaceChild ( oA, oNode );
							break;
						}
					}
				}
			}
		},
		
		mValidateForm : function () { // validate form
			//return ''; // for debugging purposes
			var oFields=oIThelp.getAllElementsByClassName(this, 'ith_field');
			var error='';
			var message='';
			for (var i = 0; i < oFields.length; i++) {
				var oField=oFields[i];
				if(oField == null) continue;
				if (oIThelp.mGetAttribute(oField, 'disablevalidation')==undefined && oIThelp.mGetAttribute(oField, 'validate')!=undefined && (message=oForms.mValidateItemFunc(oField)(null))!='') {
					var label=oForms.mGetFormElementLabel(oField).firstChild.nodeValue;
					if (label.charAt(label.length-1)) label=label.substring(0, label.length+(label.charAt(label.length-1)=='*'?-1:0));
					message=label+(label.substring(label.length-1,label.length)==':'?' ':': ')+message;
					error+=(error==''?message:'\n'+message);
				}
			}
			return error;
		},
		
		mEnableValidation : function (enable, oNode) { // enable or disable validation
			var oFields=oIThelp.getAllElementsByClassName(oNode, 'ith_field');
			var i=0;
			while ((oField=oFields[i++])!=null) {
				if (!enable) oIThelp.mAddAttribute(oField, 'disablevalidation', 'disablevalidation');
				else if (oIThelp.mGetAttribute(oField, 'disablevalidation')!=undefined) oField.removeAttribute('disablevalidation');
			}
		},
			
		mGetFormElement : function(oControlFrame) {
			if (oControlFrame.className.contains('listboxfield')) {
				var aElements=oIThelp.getAllElementsByClassName(oControlFrame, 'listbox');
				return aElements[0];
			}
			for (var i=0;i<oControlFrame.childNodes.length;i++) {
				if ((oControlFrame.childNodes[i].NodeType!=Node.TEXT_NODE) && (oControlFrame.childNodes[i].tagName=='INPUT' || oControlFrame.childNodes[i].tagName=='SELECT')
					|| oControlFrame.childNodes[i].tagName=='TEXTAREA')
				{
					return oControlFrame.childNodes[i];
				}
			}
		},
		
		mGetFormElements : function(oField) {
			//alert('c1: '+oField.childNodes.length);
			arr=[];
			for (var i=0;i<oField.childNodes.length;i++) {
				if ((oField.childNodes[i].NodeType!=Node.TEXT_NODE) && (oField.childNodes[i].tagName=='INPUT' || oField.childNodes[i].tagName=='SELECT'))
				{
					arr.push(oField.childNodes[i]);
				}
			}
			return arr;
		},
		
		mGetFormElementLabel : function(oField) {
			if ((oField.previousSibling.NodeType!=Node.TEXT_NODE) && (oField.previousSibling.tagName=='DIV')) {
				return oField.previousSibling;
			}else if ((oField.previousSibling.previousSibling.NodeType!=Node.TEXT_NODE) && (oField.previousSibling.previousSibling.tagName=='DIV')){
				return oField.previousSibling.previousSibling;
			}
			return null;
		},
		
		mCollect : function () {
			var params='';
			for (var i = 0; i < this.aFields.length; i++) {
				var oField=this.aFields[i];
				if(oField == null) continue;
				params+=(params!=''?'@sep':'')+oField.oElement.id+'@eq';
				// Listbox
				if (oField.oElement.className!=null && oField.oElement.className.contains('listbox')) {
					var aParams= oField.className.contains("selector") ? oListboxes.mGetSelectedRowsId(oField.oElement.id) : oListboxes.mGetAllRowId(oField.oElement.id);
					var param='';
					for (var ii=0;ii<aParams.length;ii++) {
						param+=aParams[ii]+'@sep';
					}
					//alert('param: '+oIThelp.mEscape(param));
					params+=((oField.escapeParam) ? oIThelp.mEscape(param) : param);
				}
				//alert(oField.oElement.id + ' :  ' + oField.oElement.value + '  -  ' + oField.escapeParam + '  -  ' + oIThelp.mEscape(oField.oElement.value));
				switch (oField.oElement.type) {
					case 'hidden' : params+=((oField.escapeParam) ? oIThelp.mEscape(oField.oElement.value) : oField.oElement.value); break;
					case 'text' : params+=((oField.escapeParam) ? oIThelp.mEscape(oField.oElement.value) : oField.oElement.value); break;
					case 'password' : params+=((oField.escapeParam) ? oIThelp.mEscape(oField.oElement.value) : oField.oElement.value); break;
					case 'textarea' : params+=((oField.escapeParam) ? oIThelp.mEscape(oField.oElement.value) : oField.oElement.value); break;
					case 'checkbox' : params+=((oField.oElement.checked?1:0)); break;
					case 'select-one' : 
						params+=oField.oElement[oField.oElement.selectedIndex].value;
						if (oIThelp.mGetAttribute(oField.oElement, 'selectname')!=null) {
							params+='@sep'+oIThelp.mGetAttribute(oField.oElement, 'selectname')+'@eq';
							params+=oField.oElement[oField.oElement.selectedIndex].text;
						}
						break;
				}
			}
			
			var j=0;
			while ((oFieldSet=this.aFieldSets[j++])!=null) {
				var aFields=oForms.mGetFormElements(oFieldSet);
				if (oFieldSet.className.contains('check')) {
					var k=0;
					while ((oField=aFields[k++])!=null) {
						params+=(params!=''?'@sep':'')+oField.id+'='+(oField.checked?1:0);
						
					}
				}
				if (oFieldSet.className.contains('radio')) {
					var l=0;
					params+=(params!=''?'@sep':'')+oFieldSet.id+'=';
					while ((oField=aFields[l++])!=null) {
						if (oField.checked) {
							params+=oField.id;
							break;
						}
					}
				}
			}
			//alert(params);
			return params;
		},
		
		mGetId : function() {
			var aFields=oIThelp.getAllElementsByClassName(this, 'id');
			if (aFields.length==0) return 'ItemId=-1';
			var oElement=oForms.mGetFormElement(aFields[0].oControlFrame);
			return oElement.id+'='+oElement.value;
		},
		
		mShowCalendar : function(oInput, oImg, format, showsTime, showsOtherMonths) {
		  
		  if (_dynarch_popupCalendar != null) {
			// we already have some calendar created
			_dynarch_popupCalendar.hide();                 // so we hide it first.
		  } else {
			// first-time call, create the calendar.
			var cal = new ith_Calendar(1, null, 
				function(cal, date) {
					cal.sel.value=date; 
					if (cal.selAE!=null) cal.selAE.value=cal.date.print(cal.selAE.uniqueFormat); 
					if (cal.dateClicked) cal.callCloseHandler();
				},
				function(cal) {cal.hide(); _dynarch_popupCalendar = null;} 
			);
			// uncomment the following line to hide the week numbers
			// cal.weekNumbers = false;
			if (typeof showsTime == "string") {
			  cal.showsTime = true;
			  cal.time24 = (showsTime == "24");
			}
			if (showsOtherMonths) {
			  cal.showsOtherMonths = true;
			}
			_dynarch_popupCalendar = cal;                  // remember it in the global var
			cal.setRange(1900, 2070);        // min/max year allowed.
			cal.create();
		  }
		  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
		  _dynarch_popupCalendar.parseDate(oInput.value);      // try to parse the text in field
		  _dynarch_popupCalendar.sel = oInput;
		  _dynarch_popupCalendar.selAE = oImg.oAttachedElement;
		  
		  // the reference element that we pass to showAtElement is the button that
		  // triggers the calendar.  In this example we align the calendar bottom-right
		  // to the button.
		  _dynarch_popupCalendar.showAtElement(oImg, 'br');        // show the calendar
		
		  return false;
		},
		
		createColor : function(oPalette, color, left, top) {
			var aColors=['00', '33', '66', '99', 'CC', 'FF'];
			var c='#'+aColors[color[0]]+aColors[color[1]]+aColors[color[2]];
			var oDiv=document.createElement('div');
			oDiv.style.backgroundColor=c;
			oPalette.appendChild(oDiv);
			oDiv.title=c;
			oDiv.style.position='absolute';
			oDiv.style.clip='rect(0,15px,15px,0)';
			oDiv.style.height='15px';
			oDiv.style.cursor='pointer';
			oDiv.style.width='15px';
			oDiv.style.top=(1+top*16)+'px';
			oDiv.style.left=(1+left*16)+'px';
			oDiv.onclick=new function() {
				return function(e) {
					oPalette.oA.close();
					oPalette.oA.oElement.value=c;
					oPalette.oA.oElement.style.backgroundColor=c;
					oPalette.oA.oElement.style.color=oIThelp.mGetInverseColor(c);
				}
			}
		},
		
		drawPalette : function(oPalette) {
			var pattern=[
				[[4],[3],[0]],
				[[5],[2],[1]]
			];
			for (var x=0;x<18;x++) {
				for (var y=0;y<12;y++) {
					var u=x % 6, v=Math.floor(x/6) % 2;
					var w=y % 6, z=Math.floor(y/6);
					oForms.createColor(oPalette, [pattern[Math.floor(y/6)][Math.floor(x/6)], (1-v)*(5-u)+v*u, (1-z)*w+z*(5-w)], x, y);
				}
			}
		}
}



// initialize
oGod.mOnload ( oForms.mInit("worksheet") );
