	function wopen(url, name, w, h) {
	  // Fudge factors for window decoration space.
	  // In my tests these work well on all platforms & browsers.
	  w += 32;
	  h += 96;
	  wleft = (screen.width - w) / 2;
	  wtop = (screen.height - h) / 2;
	  // IE5 and other old browsers might allow a window that is
	  // partially offscreen or wider than the screen. Fix that.
	  // (Newer browsers fix this for us, but let's be thorough.)
	  if (wleft < 0) {
	    w = screen.width;
	    wleft = 0;
	  }
	  if (wtop < 0) {
	    h = screen.height;
	    wtop = 0;
	  }
	  var win = window.open(url,
	    name,
	    'width=' + w + ', height=' + h + ', ' +
	    'left=' + wleft + ', top=' + wtop + ', ' +
	    'location=no, menubar=no, ' +
	    'status=no, toolbar=no, scrollbars=yes, resizable=yes');
	  // Just in case width and height are ignored
	  win.resizeTo(w, h);
	  // Just in case left and top are ignored
	  win.moveTo(wleft, wtop);
	  win.focus();
	}

	function submitNavItem(frmName, controllerPage, queryString,actionCmd) {
		frm = eval('document.' + frmName);
		qString = '';
		if (queryString != '') {
			qString = "?" + queryString + "&command=" + actionCmd;
		} else {
			qString = "?" + "command=" + actionCmd;
		}
		frm.action = controllerPage + qString;
		frm.submit();
	}

	function submitNavItemWithConfirm(msg, frmName, controllerPage, queryString,actionCmd) {
		if (confirm(msg)) {
			return submitNavItem(frmName, controllerPage, queryString,actionCmd);
		} else {
			return false;
		}
	}

	function unhideMessage(msg, elementIdName) {
		textElement = document.getElementById(elementIdName);
		textElement.display = "block";
		textElement.visibility = "visible";
		textElement.innerHTML = msg;
	}

	function hideMessage(msg, elementIdName) {
		textElement = document.getElementById(elementIdName);
		textElement.display = "none";
		textElement.visibility = "hidden";
	}
	
	/* TabNext()
	* Function to auto-tab phone field
	* Arguments:
	*   obj :  The input object (this)
	*   event: Either 'up' or 'down' depending on the keypress event
	*   len  : Max length of field - tab when input reaches this length
	*   next_field: input object to get focus after this one
	* 	<td>Phone No.:</td>
	* 	<td>
	* 	<input type=text name="Phone_1" size=3 maxlength=3 nKeyDown="TabNext(this,'down',3)" onKeyUp="TabNext(this,'up',3,this.form.Phone_2)">
	* 	-
	* 	<input type=text name="Phone_2" size=3 maxlength=3 onKeyDown="TabNext(this,'down',3)" onKeyUp="TabNext(this,'up',3,this.form.Phone_3)">
	* 	-
	* 	<input type=text name="Phone_3" size=4 maxlength=4>
	* 	</td>
	*/
	var phone_field_length=0;
	function TabNext(obj,event,len,next_field) {
		if (event == "down") {
				phone_field_length=obj.value.length;
		} else if (event == "up") {
			if (obj.value.length != phone_field_length) {
				phone_field_length=obj.value.length;
				if (phone_field_length == len) {
					next_field.focus();
				}
			}
		}
	}

	function removeItem() {
		selection = confirm ("Are you sure you want to remove this item");
		return selection;
	}	
	
	function addJavascript(jsname) {
		$(document).ready(function() {
			$("head").append($("<script type='text/javascript' src='"+jsname+"'/>"));
		});
	} 
	
	function addStyleSheet(stylesheetname) {
		$(document).ready(function() {
			$("head").append($("<link rel='stylesheet' href='"+stylesheetname+"' type='text/css' media='screen' />"));
		});
	} 

	/**
	 * This method is useful on Forms for default actions. It sets up all of the required props etc for handling a form in both
	 * ajax and traditional modes
	 * 
	 * @param frmId The ID instance used in the form
	 * @param mode Either insert or update values
	 * @param businessObject The business Object name
	 * @return
	 */
	function defaultFormBasics(frmId,mode,businessObject,sessionId) {
		var use_ajax=true;
		businessObject = (businessObject+'').toLowerCase();
		$.validationEngine.settings={};
		$("#"+frmId).validationEngine({	/* create the form validation */
			inlineValidation: false,
			promptPosition: "centerRight",
			success :  function(){use_ajax=true},	/* if everything is OK enable AJAX */
			failure : function(){use_ajax=false}	/* in case of validation failure disable AJAX */
		 })
	
		 /*
		  * The Standard Non Ajax Actions
		  */
		$("#cancelform").click(function(e){
			if (confirm("Cancelling will undo any changes you made, are you sure?")) {
				$("#"+frmId).submit();
			}
		});
		$("#closeform").click(function(e){
			$("#"+frmId).submit();
		});
		$("#remove").click(function(e){
			$("#"+frmId).submit();
		});
		$("#update").click(function(e){
			$("#"+frmId).submit();
		});
		$("#insert").click(function(e){
			$("#"+frmId).submit();
		});
	
		/*
		 * Ajax Actions
		 */
		$("#ajaxsave").click(function(e){
			if(use_ajax) {
				for ( instance in CKEDITOR.instances )
		            CKEDITOR.instances[instance].updateElement();

				if ( window.area != undefined) {
//					$('#content').val(editAreaLoader.getValue(editor_id));
//					var params = $(this).serialize();
					var values = {};
					$.each($('#'+frmId).serializeArray(), function(i, field) {
					    values[field.name] = field.value;
					    if ($('#'+field.name).is('textarea') ) {
						    var textarea = $('textarea', $('iframe').get(0).contentWindow.document);
						    $('#'+field.name).val(textarea.val());
					      }
					});
				}

				$.jGrowl("<img src='../../core/images/loading.gif' align='left' style='padding : 5px'>Now saving, please wait", {life : 500});
				
				// $.post(businessObject+'handler.php',$("#"+frmId).serialize()+"&"+newMode+"=true&sid="+sessionId,
				$.post(businessObject+'handler.php',$("#"+frmId).serialize()+"&ajaxsave=true&sid="+sessionId,
					function(data){
						if(parseInt(data)==1) {
							$.validationEngine.closePrompt('.formError',true);
							$.jGrowl("<img src='../../images/right.png' align='left' style='padding : 5px'>Save Successful", { life: 5000 });
							$('#ajaxsave').trigger('saveSuccess');
						} else {
							$.jGrowl("<img src='../../images/wrong.png' align='left' style='padding : 5px'><h1>Oops!</h1>There was a problem saving this record", { life: 5000 });
							for (i=0; i<data.jsonValidateReturn.length;i++) {
								$.validationEngine.buildPrompt(data.jsonValidateReturn[i][0],data.jsonValidateReturn[i][1],"error");
							}
							$('#ajaxsave').trigger('saveError');
						}
					},"json");
			}
	
			e.preventDefault();	/* stop the default form submit */
		})
	}
