/**
 * ocsp js listPlugin
 *
 * @since pk-08-01-17
 * 
 * @version $Id: jOCSP_form.js,v 1.35 2012/01/24 12:34:12 pitlinz Exp $
 *
 */
 
 if (!window.jOCSP)
 {
	alert('Error jOCSP not loaded');
 } else { 	
 	if (typeof(jOCSP.dom) == "undefined")
 	{
 		jOCSP.requestScript(jOCSP.systemUrl + 'jOCSP/jOCSP_dom.js',false);
 	}
 	
 	if (typeof(jQuery.fn.ajaxSubmit) == "undefined")
 	{
 		jOCSP.requestScript(jOCSP.systemUrl + 'javascript/jquery/jquery.form.js',true);
 	}
 	
 	if (typeof(jOCSP.ocspFormList) == "undefined")
 	{
 		jOCSP.ocspFormList = {};
 		jOCSP.frmSubmitLog = new Array();
 	}
 	
 	if (typeof(jOCSP.ocspFormObj) == "undefined")
 	{
 		
		jOCSP.FRM_MODE_READONLY=0;
		jOCSP.FRM_MODE_NEW=1;
		jOCSP.FRM_MODE_EDIT=2;
    	jOCSP.FRM_MODE_HIDDEN=3;
    	jOCSP.FRM_MODE_LIST=4;
    	jOCSP.FRM_MODE_SEARCH=5;
    	jOCSP.FRM_MODE_DELETE=6;
    	jOCSP.FRM_MODE_COPY=7; 		
 		
 		
 		jOCSP.initForm = function(aFrmHash,aFrmDesc)
 		{
 			try { 				
 				obj_form = jOCSP.ocspFormList[aFrmHash] = new jOCSP.ocspFormObj(aFrmHash);	 			 			
	 			if (typeof(aFrmDesc) == 'object')
	 			{
					for (str_attrName in aFrmDesc)
					{
						obj_form[str_attrName] = aFrmDesc[str_attrName];
					}
					
					if (aFrmDesc.myAction)
					{
						obj_form.action=aFrmDesc.myAction;
					}
	 			}
	 			if (obj_dom = obj_form.getDomObj())
	 			{
	 				obj_dom.jOCSP_frmHash = aFrmHash;
	 			}
	 		} catch(e) {
	 			alert('jOCSP.initForm: ' + e);
	 		}
	 		
	 		//alert('jOCSP_form.js: jOCSP.initForm: done');
 		}
 		
 		/**
 		 * returns the form object in jOCSP.ocspFormList or null
 		 *
 		 * @param string aFrmHash
 		 *
 		 * @return jOCSP.ocspFormObj
 		 */
 		jOCSP.getFormByHash = function(aFrmHash)
 		{
 			return jOCSP.ocspFormList[aFrmHash];
 		}
 		 	
 		/**
 		 * returns the field with id aFldId in form aFrmHash
 		 * 
 		 * @param string aFrmHash
 		 * @param string aFldId
 		 * 
 		 * @return jOCSP_FLDOBJ
 		 * 
 		 * @since pk-09-02-17
 		 */
 		jOCSP.getFormFieldByHash = function(aFrmHash,aFldId)
 		{
 			if (aForm = jOCSP.getFormByHash(aFrmHash))
 			{
 				return aForm.getFieldByDomId(aFldId)
 			}
 			return null;
 		}	
 		
 		/**
 		 * returns the form of a DOM object
 		 */
 		jOCSP.getFormFromDOMField = function(aField,asDOMObj)
 		{
         	if (typeof(aField.offsetParent))
        	{
        		var obj_form = null;
        		
        		obj_dom=aField.parentNode;        		
        		while ((typeof(obj_dom)=='object'))
        		{
        			tagName=obj_dom.tagName.toUpperCase();
        			
        			if (tagName=='FORM')
        			{
        				obj_form=obj_dom;
        				obj_dom=false; // exit while 
        			} else if (tagName=='BODY') {
        				//alert('could not find form of the field');
        				return null;
        			} else if (typeof(obj_dom.parentNode)=='object') {
                        obj_dom=obj_dom.parentNode;
                    } else {
                    	//alert('else');
                    	return null;
                    }
        		} 
        		if (asDOMObj)
        		{
        			return obj_form;
        		} else {
        			if (obj_form.jOCSP_frmHash)
        			{
        				jOCSP.getFormByHash[obj_form.jOCSP_frmHash]
        			}
        		}       		
        	} 
        	return null;
        				
 		}
 		
 		
 		/**
 		 * adds a form field to a form
 		 * 
 		 * @param string aFrmHash
 		 * @param object aJsonObj
 		 *  
 		 */
 		jOCSP.addFieldObjToFormByHash = function(aFrmHash,aJsonObj)
 		{
 			if (obj_form = jOCSP.getFormByHash(aFrmHash))
 			{
				obj_form.addField(aJsonObj); 				
 			}
 		} 
 		
 		/**
 		 * sets jOCSPFldObj to the fields dom object
 		 * 
 		 * @param string aFrmHash
 		 * @param string aDomId
 		 *
 		 */  
 		jOCSP.formFieldSetToDom = function(aFrmHash,aDomId)
 		{
 			if (obj_form = jOCSP.getFormByHash(aFrmHash))
 			{
 				if (obj_field = obj_form.getFieldByDomId(aDomId))
 				{
 					if (obj_dom = document.getElementById(aDomId))
 					{
 						obj_dom.jOCSPFldObj = obj_field;
 					} 
 				} else {
 					alert('field ' + aDomId + ' not found');
 				} 
 			} else {
				alert('form ' + aFrmHash + ' not found');
 			}
 		}

 		
 		/**
 		 * logs a form submit via ajax
 		 * @param string script ($_SERVER['REQUEST_URI'])
 		 * @param array formArray 
 		 */
 		jOCSP.logFormSubmit = function(script,formArray)
 		{
 			var logEntry = {};
 			logEntry.script 	= script;
 			logEntry.formArray  = formArray;
 			jOCSP.frmSubmitLog.push(logEntry);
 			if (jOCSP.frmSubmitLog.length > 10)
 			{
 				jOCSP.frmSubmitLog.shift();	
 			} 
 		}
 		
 		/**
 		 * 
 		 */
 		jOCSP.formFieldChanged = function(aFrmHash,aDomId,aValue)
 		{
 			if (obj_form = jOCSP.getFormByHash(aFrmHash))
 			{
 				obj_form.fieldChanged(aDomId,aValue)
 			} else {
 				alert('form ' + aFrmHash + ' not found');
 			} 			
 		}
 	 		
 		
 		jOCSP.formSetContent = function(aFrmHash,aContent)
 		{
			try {
				jOCSP.getFormByHash(aFrmHash).setFormHTML(aContent);
			} catch (e) {
				alert('jOCSP.formSetContent: ' + e);
			}
 		}
 		 		
 		/**
 		 * class for opencsp forms
 		 *
 		 * @param string afrmHash (unique identifier of the OCSP_FORM instance)
 		 */
		jOCSP.ocspFormObj = function(afrmHash)
		{
			/**
			 * @var string myRpcUrl
			 */
			this.myRpcUrl = jOCSP.systemUrl+"jOCSP/formRPC.php";
		
			/**
			 * @var string frmHash
			 */
			this.frmHash	= afrmHash;
		
			this.frmId	= 0;
			this.domId	= null;
			
			/**
			 * @var string backUrl
			 */
			this.backUrl	= "history.goBack()";
			
			/**
			 * @var string nextUrl
			 */
			this.nextUrl	= "";
			
			/**
			 * @var associative_array myFields
			 */
			this.myFields	= {}; 
				
			/**
			 * @var boolean isChanged
			 */
			this.isChanged	= false;
			
			this.debug		= false;
			
			// ---------------------------------------------
			// dom methods
			// ---------------------------------------------

			/**
			 * returns the domobject of a form
			 * 
			 * @return DOM 
			 */
			this.getDomObj = function()
			{
				try {
					if (this.domId && (obj_frmDom = document.getElementById('frm_' + this.domId)))
					{
						return obj_frmDom;
					} else if (this.myName && (obj_form = document.forms[this.myName] )) {
						return obj_form;
					}
				} catch(e) {
					return null;
				}
			}
			
			/**
			 * replaces the content of the outer fram with aContent
			 * 
			 * @param string aContent (html <form ....</form>
			 */
			this.setFormHTML = function(aContent)
			{
				try {
					document.getElementById('frmOuter_' + this.domId).innerHTML = aContent;
				} catch(e) {
					//if (this.debug)
					{
						alert('jOCSP_form.js jOCSP.ocspFormObj.setForm: ' + e + '\ndomId: ' + this.domId);
					}
				}
			}			
			
			/**
			 * method called when a form is reinitialized
			 */
			this.clear = function()
			{
				// dummy do nothing overwrite if needed
			}
			
			this.disableWithShadow = function()
			{
				return jOCSP.dom.shadowLayer(this.getDomObj().id);
			}

			// ---------------------------------------------
			// rpc methods
			// ---------------------------------------------
			
			this.getRPCUri = function()
			{
				str_url  = this.myRpcUrl + '?SecReq=' + jOCSP.getSecReq();
				str_url += '&frmHash=' + this.frmHash;
				str_url += '&frmId=' + this.frmId; 
				str_url += '&frmDId=' + this.domId;	
				
				return str_url;			
			} 
						

			// ---------------------------------------------
			// field methods
			// ---------------------------------------------

			
			/**
			 * field methods
			 */
			this.getFieldByDomId = function(aDomId)
			{
				return this.myFields[aDomId];
			}
			
			/**
			 * adds a field to the form
			 * 
			 * @param object fldObj
			 * 
			 * @return boolean 
			 */
			this.addField = function(fldJson)
			{
				if (fldJson && fldJson.domId && fldJson.fldName)
				{
					if (fldJson.fldClass)
					{
						try {
							
							if (fldJson.debug) 
							{
								var str_debug = "";
								for (j in fldJson)
								{
									str_debug += j +':' + fldJson[j] + '\n';
								}
								alert(str_debug);
							}	
											
							if (typeof(jOCSP[fldJson.fldClass]) == "undefined")
							{
								if (fldJson.debug) 
								{
									alert(fldJson.fldClass + ' not loaded');
								}
								
								if (fldJson.fldClassSrc) // <pk-08-06-02>
								{
									if (fldJson.debug) alert('Including: ' + fldJson.fldClassSrc);
									jOCSP.requestScript(fldJson.fldClassSrc,true,1000);
								} else {
									if (fldJson.debug) alert('Including: ' + jOCSP.systemUrl + 'jOCSP/formFields/jOCSP_' + fldJson.fldClass + '.js');
									jOCSP.requestScript(jOCSP.systemUrl + 'jOCSP/formFields/jOCSP_' + fldJson.fldClass + '.js',true,1000);
								}
							}
							str_fldCmd = "var obj_fld = new jOCSP." + fldJson.fldClass + "(fldJson);";
														
							try {
								eval(str_fldCmd);
							} catch(e) {
								alert('jOCSP.ocspFormObj.addField: \n' + e);
							}
						
							this.myFields[fldJson.domId] = obj_fld;
														
						} catch(e) {	
							if (fldObj.debug)
							{
								alert('Error adding field: \n' + e)
							}
							return false;
						}
					} else {
						this.myFields[fldJson.domId] = fldJson;
						if ((this.myFields[fldJson.domId].inLineLabel) && (this.myFields[fldJson.domId].inLineLabel.length))
						{
							if (obj_dom = document.getElementById(fldJson.domId))
							{								
								obj_dom.jOCSP_inLineLabel = this.myFields[fldJson.domId].inLineLabel;
								
								if (typeof(obj_dom.onfocus) == "function")
								{
									obj_dom.jOCSP_onFocus = obj_dom.onfocus;
								}
								
								obj_dom.onfocus = function()
								{
									if (this.value == this.jOCSP_inLineLabel)
									{
										this.value = "";
										if (typeof(this.jOCSP_onFocus) == "function")
										{
											this.jOCSP_onFocus();
										}
									}
									
									this.className = this.className.replace(/inLineLabel/,"").superTrim();
									this.className = this.className.replace(/fldError/,"").superTrim();
								}
								
								if (typeof(obj_dom.onblur) == "function")
								{
									obj_dom.jOCSP_onBlur = obj_dom.onblur;
								} else {
									obj_dom.jOCSP_onBlur = function() {
										if (this.value != this.jOCSP_inLineLabel)
										{
											str_newValue = this.value;
										} else {
											str_newValue = "";
										}
										
										if (str_newValue != this.defaultValue)
										{
											if (this.type != 'button')
											{
												try {
													// alert(this.value + ' inline ' + this.jOCSP_inLineLabel + ' new: ' + str_newValue + ' default: ' + this.defaultValue);
													this.ocspFormField.myForm.fieldChanged(this.ocspFormField.domId,str_newValue);
												} catch(e) {
													alert('Inline Label: ' + this.type + ' ' + e);
												}
											}
										}
									}
								}
								
								obj_dom.onblur = function()
								{
									if (this.value == '')
									{
										this.value = this.jOCSP_inLineLabel;
										if (!(this.className))
										{
											this.className = 'inLineLabel';
										} else {
											this.className = this.className + ' inLineLabel'
										}
									}
									
									if (typeof(this.jOCSP_onBlur) == "function")
									{
										this.jOCSP_onBlur();
									}									
								}
								
								obj_dom.onblur();								
							} // if (obj_dom = document.getElementById(fldJson.domId))
						} else {
							if (obj_dom = document.getElementById(fldJson.domId))
							{
								if (typeof(obj_dom.onfocus) == "function")
								{
									obj_dom.jOCSP_onFocus = obj_dom.onfocus;
								}
								
								obj_dom.onfocus = function()
								{
									if (typeof(this.jOCSP_onFocus) == "function")
									{
										this.jOCSP_onFocus();
									}								
									this.className = this.className.replace(/fldError/,"").superTrim();
								}
							}
						}
					}
					
					this.myFields[fldJson.domId].myForm  = this;
										
					if (typeof(this.myFields[fldJson.domId].init) == "function")
					{
						if (fldJson.debug) 
						{
							alert('addField: ' + fldJson.fldName + '.init()');
						}
						try {
							this.myFields[fldJson.domId].init(fldJson);
						} catch(e) {
							return false;
						}
					}
					
					if (typeof(this.myFields[fldJson.domId].getDomObj) != "function")
					{
						this.myFields[fldJson.domId].getDomObj = function()
						{
							if (obj_ret = document.getElementById(fldJson.domId))
							{
								return obj_ret;
							} else {
								return null;
							}
						}
					}					
					
					if (typeof(this.myFields[fldJson.domId].onChanged) != "function")
					{
						this.myFields[fldJson.domId].onChange = function(newValue)
						{
							this.isChanged=true,
							this.myForm.onChanged(this.domId,newValue)							
						}   
					}
					
					if (typeof(this.myFields[fldJson.domId].getValue) != "function")
					{
						this.myFields[fldJson.domId].getValue = function()
						{
							try {
								if (str_val = document.getElementById(this.domId).value)
								{
									if ((this.inLineLabel) && (str_val == this.inLineLabel))
									{
										return '';
									}
								}
								return str_val;
							} catch(e) {
								if (this.debug)
								{
									alert('getValue: ' + e);
								}			
							}
						}
					}
					
					if (typeof(this.myFields[fldJson.domId].setValue) != "function")
					{
						if (fldJson.debug) alert('generating setValue function');
						this.myFields[fldJson.domId].setValue = function(aValue)
						{
							try {
								document.getElementById(this.domId).value=aValue;
							} catch(e) {
								if (this.debug)
								{
									alert('setValue: ' + e);
								}			
							}
						}
					}	
									
					if (typeof(this.myFields[fldJson.domId].getValue) != "function")
					{
						if (fldJson.debug) alert('generating getValue function');
						this.myFields[fldJson.domId].getValue = function()
						{
							try {
								return document.getElementById(this.domId).value;
							} catch(e) {
								if (this.debug)
								{
									alert('getValue: ' + e);
								}			
							}
						}
					}	
					
					if (obj_dom = document.getElementById(fldJson.domId))
					{
						obj_dom.ocspFormField = this.myFields[fldJson.domId];
					}		
					
					return true;
				} else {
					return false;
				}			 
			}		

			// ---------------------------------------------
			// field watch
			// ---------------------------------------------

			this.fieldChanged = function(fldDomId,newValue)
			{
				this.isChanged = true;
				
				try {
					if (obj_dom = document.getElementById(fldDomId))
					{
						if (str_className = obj_dom.className)
						{
							if (str_className.indexOf('fldError') != -1)
							{
								obj_dom.className = str_className.replace(/fldError/,"").superTrim();
							}
						}
					}
				} catch(e) {
					alert(e);
				}
				
				// alert('fieldChanged: ' + fldDomId + ' value: ' + newValue);
				
				if (!newValue)
				{
					try {
						newValue = jQuery('#' + fldDomId).fieldValue();
					} catch(e) {
						try 
						{ 
							newValue = document.getElementById('#' + fldDomId).value;
						} catch(e) { alert(e); }
					}
				}
				
				if (obj_fld = this.getFieldByDomId(fldDomId))
				{
					if (obj_fld.debug)
					{
						alert('Field: ' + obj_fld.fldName + ' has changed');
					}
					if (typeof(obj_fld.onchange) == "function")
					{
						obj_fld.onchange(newValue);
					}
					

					
				} else {
					if (this.debug) {
						alert('obj_fld ' + fldDomId + ' not found');
					}
				}
				
				if (typeof(this.onchange) == "function")
				{
					this.onchange();
				}
			}
			
			this.showFieldErrors = function(fldErrors,debug)
			{
    			var l=fldErrors.length;
    			for (i = 0; i < l; i++)
    			{
    				if (obj_dom = document.getElementById(fldErrors[i].DOMId))
    				{
    					if (str_className = obj_dom.className)
    					{
    						if (str_className.indexOf('fldError') == -1)
    						{
    							obj_dom.className = str_className + ' fldError'; 
    						}
    					} else {
    						obj_dom.className = 'fldError';
    					}
    				}
    				
    				if (fldErrors[i].DOMId)
    				{
    					try {
	    					if (obj_fld = this.getFieldByDomId(fldErrors[i].DOMId))
	    					{
	    						if (typeof(obj_fld.showError) == "function")
	    						{
	    							try {
	    								obj_fld.showError(fldErrors[i]);
	    							} catch(e) {
	    								if (debug) alert('showFieldErrors function:' + e);
	    							}
	    						} else if (obj_fld.groupFldObj || (obj_fld.grpFldDomId)) {
	    							try {
		    							if (!obj_fld.groupFldObj)
		    							{
		    								obj_fld.groupFldObj = this.getFieldByDomId(obj_fld.grpFldDomId);
		    							}
		    							obj_fld.groupFldObj.showFldError(fldErrors[i]);
	    							} catch(e) {
	    								if (debug) alert('showFieldErrors group field:' + e);
	    							}
	    						} else if (obj_dom = document.getElementById('error_'+fldErrors[i].DOMId)) {
									obj_dom.innerHTML = fldErrors[i].msg;
									obj_dom.style.display	 ='block';
									obj_dom.style.visibility = 'visible';
		    						
		    						if (obj_dom = document.getElementById('fldName_'+fldErrors[i].DOMId))
		    						{
		    							obj_dom.className='fldNameError';
		    						}    							
	    						}
	    					} else if (obj_dom = document.getElementById('error_'+fldErrors[i].DOMId)) {
	    						try {
									obj_dom.innerHTML = fldErrors[i].msg;
									obj_dom.style.display	 ='block';
									obj_dom.style.visibility = 'visible';
		    						
		    						if (obj_dom = document.getElementById('fldName_'+fldErrors[i].DOMId))
		    						{
		    							obj_dom.className='fldNameError';
		    						}
	    						} catch(e) {
	    							if (debug) alert('showFieldErrors dom:\n' + e);
	    						}
	    					} else {
								if (jOCSP.debug) {
									alert(fldErrors[i].label+" "+fldErrors[i].msg);    						
								}
	    					}
    					} catch(e) {
    						if (debug) alert('showFieldErrors ' + fldErrors[i].label + ':\n' + e);
    					}
    				} else {
    					alert(fldErrors[i].label+":\n "+fldErrors[i].msg);
    				}
    			}
			}
			
			this.clearFieldErrors = function ()
			{
				for (i in this.myFields)
				{
					if (obj_dom = document.getElementById('error_' + i))
					{
						obj_dom.style.display = 'none';
						obj_dom.style.visibility = 'hidden';
					}
					
					if (obj_dom = document.getElementById('fldName_' + i))
					{
						obj_dom.className = 'fldName';
					}
				}
			}
	
		} // jOCSP.ocspFormObj	 
		
		
		
		
		jOCSP.ocspFormFldEvents = new function()
		{
		
			// ---------------------------------------------
			// field events
			// ---------------------------------------------
				
			/**
			 * @param event e
			 * @param boolean asInteger
			 * @return number or NULL
			 * 
			 * usage: 
			 * <code>
			 *  &lt;input type="text" onkeypress="return jOCSP.ocspFormFldEvents.keyNumber(event,[true|false]);" /&gt;
			 * </code>
			 */
			this.keyNumber = function (e,asInteger)
			{
			    var int_key = window.event ? e.keyCode : e.which;
			    
			    if (int_key < 28)
			    {
			    	return String.fromCharCode(int_key);
			    }
			            
			    var cha_key = String.fromCharCode(int_key);	
				
			    if (asInteger)
			    {
			    	reg_exp=/[0-9\-]/;
			    } else {
			    	reg_exp=/[0-9\.,\-]/;
			    }
			    return reg_exp.test(cha_key);
			}			
		}
		
		// ------------------------------------------
		// field prototypes
		// ------------------------------------------
		
 	}
}


