/*
 * Copyright by Ricky.Chiang
 * 2004.03.25
 * For ActionForm JavaScript Object
 */
function ActionForm() {}

	ActionForm._FormName = null;
	ActionForm._ActionInfo = new Array();

	ActionForm.SetForm = function() {
	if (arguments.length>0) {
		ActionForm._FormName = arguments[0];
	}
	else {
		ActionForm._FormName = null;
	}
};

ActionForm.GetForm = function() {
	return document.forms[ActionForm._FormName];
};

/*
	ActionForm.SetAction = _ActionForm_SetAction;
	ActionForm.SetActionPopup = _ActionForm_SetActionPopup;
	//2006-09-07 By Daniel  add window.dialog
	ActionForm.SetActionDialog = ActionForm.DialogActionEvent;
	ActionForm.ChangeAction = _ActionForm_changeAction;
	ActionForm.PopupAction = _ActionForm_popupAction;
*/
ActionForm.prototype.toString = function() { return "Copyright by Ricky.Chiang"; };

/*
	Base action setter
	arguments[0] = objId
	arguments[1] = actionName
	arguments[2] = alertMessage
*/
ActionForm.SetAction = function() {
	if (arguments[1] != null) {
		try {
			var relaObj = document.getElementById(arguments[0]);
			//relaObj.onclick = ((arguments[2]==null || arguments[2]==false)? ActionForm.ChangeAction: ActionForm.PopupAction);
			//arguments.previousClick = relaObj.onclick;
			ActionForm._ActionInfo[relaObj.id] = arguments;
			ActionForm._ActionInfo[relaObj.id].previousClick = relaObj.onclick;
			
			relaObj.onclick = ActionForm.ChangeAction;
		}
		catch(E) { 
			alert("Can't find element with ["+arguments[0]+"]");
			return false;
		}
	}
	else {
		alert("action name is null!!!");
		return false;
	}
};

/*
	arguments[0] = objId
	arguments[1] = actionName
	arguments[2] = width
	arguments[3] = height
	arguments[4] = alertMessage
	arguments[5] = status
	arguments[6] = scrollbars
*/
ActionForm.SetActionPopup = function() {
	if (arguments[1] != null) {
		try {
			var relaObj = document.getElementById(arguments[0]);
			//relaObj.onclick = ((arguments[2]==null || arguments[2]==false)? ActionForm.ChangeAction: ActionForm.PopupAction);
			//arguments.previousClick = relaObj.onclick;
			ActionForm._ActionInfo[relaObj.id] = arguments;
			ActionForm._ActionInfo[relaObj.id].previousClick = relaObj.onclick;

			relaObj.onclick = ActionForm.PopupActionEvent;
		}
		catch(E) { 
			alert("Can't find element with ["+arguments[0]+"]");
			return false;
		}
	}
	else {
		alert("action name is null!!!");
		return false;
	}
};

/*
	arguments[0] = objId
	arguments[1] = actionName
	arguments[2] = width
	arguments[3] = height
	arguments[4] = status
	arguments[5] = scrollbars
	arguments[6] = help
	arguments[7] = resize
*/
ActionForm.SetActionDialog = function() {
	if (arguments[1] != null) {
		try {
			var relaObj = document.getElementById(arguments[0]);
			
			ActionForm._ActionInfo[relaObj.id] = arguments;
			ActionForm._ActionInfo[relaObj.id].previousClick = relaObj.onclick;

			relaObj.onclick = ActionForm.DialogActionEvent;
		}
		catch(E) { 
			alert("Can't find element with ["+arguments[0]+"]");
			return false;
		}
	}
	else {
		alert("action name is null!!!");
		return false;
	}
};

/*
  Add AJAX Action By Daniel At 2006-05-18
  
	Base action setter
	arguments[0] = objId
	arguments[1] = actionName
	arguments[2] = method
*/
ActionForm.SetAjaxAction = function() {

	if (arguments[1] != null) {
		try {
			var relaObj = document.getElementById(arguments[0]);
			//relaObj.onclick = ((arguments[2]==null || arguments[2]==false)? ActionForm.ChangeAction: ActionForm.PopupAction);
			//arguments.previousClick = relaObj.onclick;
			ActionForm._ActionInfo[relaObj.id] = arguments;
			ActionForm._ActionInfo[relaObj.id].previousClick = relaObj.onclick;
			
			relaObj.onclick = ActionForm.AjaxAction;
		}
		catch(E) { 
			alert("Can't find element with ["+arguments[0]+"]");
			return false;
		}
	}
	else {
		alert("action name is null!!!");
		return false;
	}
};

ActionForm.OnChangeActionSession=false;

ActionForm.ChangeAction = function() {
/*
	try {
*/
		var actionInfo = ActionForm._ActionInfo[this.id];
		if (actionInfo==null) {
			alert("No action be setted!!!");
			return false;
		}
		else {
			if (ActionForm.OnChangeActionSession) {
				alert("Do not submit again!!!");	
				return false;
			}
			ActionForm.OnChangeActionSession=true;
			// execute previous click function
			try {
				if (actionInfo.previousClick) {
					if (actionInfo.previousClick.apply()==false) {
						ActionForm.OnChangeActionSession=false;
						return false;
					}
				}
			}
			catch(E) { }
			
			var formObj = ActionForm.GetForm();

			if (actionInfo[1]!="") {
				formObj.action = actionInfo[1];
			}
		
			// 20060913 fix bug
			// cancel confirm, can't submit again			
			ActionForm.OnChangeActionSession = (ActionForm.Submit(formObj, this, actionInfo[2]));

			return ActionForm.OnChangeActionSession;
		}
/*
	 }
	 catch(E) {
	 	alert("Set ActionForm Name first!!!");
	 	return false;
	 }
*/
};
	
ActionForm.PopupActionEvent = function() {
	var popwin = false;	
/*
	try {
*/
		if (openCenteredWindow) {
			var actionInfo = ActionForm._ActionInfo[this.id];
			if (actionInfo==null) {
				alert("No action be setted!!!");
				return false;
			}
			else {
				// execute previous click function
				try {
					if (actionInfo.previousClick) {
						if (actionInfo.previousClick.apply()==false) {
							return false;
						}
					}
				}
				catch(E) { }
			
				var formObj = ActionForm.GetForm();
				actionInfo[7] = new Array();
				// ar paramInfo = (actionInfo[1].indexOf("?")>=0)?"":"?";

				var paramInfo = (actionInfo[1].indexOf("?")>=0)?actionInfo[1].substr(actionInfo[1].indexOf("?")+1):"";
				if (paramInfo!="") {
					// Split parameter into array
					pmArray = paramInfo.split("&");
					for(var i=0; i<pmArray.length; i++) {
						peerParam = pmArray[i].split("=");
						actionInfo[7][ peerParam[0] ] = peerParam[1];
					}				
				}

				for (var i=0; i<formObj.elements.length; i++) {
					//if (paramInfo!="?") { paramInfo += "&"; }
					if (formObj.elements[i].name!="") {
						if (formObj.elements[i].tagName.toLowerCase()=='input') {
							if (formObj.elements[i].type=='checkbox' && !formObj.elements[i].checked) {
								continue;	
							}
							if (formObj.elements[i].type=='radio' && !formObj.elements[i].checked) {
								continue;	
							}
						}
						//paramInfo += formObj.elements[i].name+"="+formObj.elements[i].value;
						if (actionInfo[7][ formObj.elements[i].name ]) {
							actionInfo[7][ formObj.elements[i].name ] = actionInfo[7][ formObj.elements[i].name ] + "," + formObj.elements[i].value;
						}
						else {
							actionInfo[7][ formObj.elements[i].name ] = formObj.elements[i].value;
						}
					}
				}
				
				popwin = ActionForm.Popup( "actionPopup.htm?"+this.id, actionInfo[2], actionInfo[3], actionInfo[4], actionInfo[5], actionInfo[6] );
				//popwin = ActionForm.Popup( actionInfo[1]+paramInfo, actionInfo[2], actionInfo[3], actionInfo[4], actionInfo[5], actionInfo[6] );
				//popwin = openCenteredWindow( actionInfo[1], "ActionFormPopup", actionInfo[2], actionInfo[3], false, false, "", "" );
			}

		}
		else {
			alert("Need include window.js!!!");	
		}
/*
	}
	catch(E) {
		alert("Need include window.js!!!"+E);
	}
*/	
	return popwin;
};

/**
 * 2006-09-06 By Daniel For showModalDialog
 */
ActionForm.DialogActionEvent = function() {
	var popwin = false;	
	var actionInfo = ActionForm._ActionInfo[this.id];
	if (actionInfo==null) {
		alert("No action be setted!!!");
		return false;
	}
	else {
		// execute previous click function
		try {
			if (actionInfo.previousClick) {
				if (actionInfo.previousClick.apply()==false) {
					return false;
				}
			}
		}
		catch(E) { }

		var urlMVC = ActionForm._ActionInfo[this.id][1];
		var width = ActionForm._ActionInfo[this.id][2];
		var height = ActionForm._ActionInfo[this.id][3];
		var status = ActionForm._ActionInfo[this.id][4];
		var scrollbars = ActionForm._ActionInfo[this.id][5];
		var help = ActionForm._ActionInfo[this.id][6];
		var resize = ActionForm._ActionInfo[this.id][7];
		
		if(typeof width == 'undefined')	width = 800;
		if(typeof height == 'undefined')	height = 600;		
		
		var x, y = 0;
		if (screen) {
	      x = (screen.availWidth - width) / 2;
		  y = (screen.availHeight - height) / 2;
	    }
		var properties = 'dialogWidth:' + width + 'px;' + 'dialogHeight:' + height + 'px;' +  'center: yes' + '; status:' + ((status) ? 'yes' : 'no') + '; scroll:' + ((scrollbars) ? 'yes' : 'no') + ';help:' + ((help) ? 'yes':'no') + ';resizable :' + ((resize) ? 'yes':'no')+ ';';
		//alert(properties );
		popwin = ActionForm.Dialog( urlMVC,ActionForm.GetForm(),properties );
	}

	return popwin;
};

/*
	Add New Method By Daniel At 2006-05-18
*/
ActionForm.AjaxAction = function() {

	var actionInfo = ActionForm._ActionInfo[this.id];
	if (actionInfo==null) {
		alert("No action be setted!!!");
		return false;
	}

	var urlMVC = ActionForm._ActionInfo[this.id][1];
	var paras = Form.serialize(ActionForm.GetForm());
	
	//alert("urlMVC:"+urlMVC);
	//alert("paras:"+paras);
	//alert(ActionForm._ActionInfo[this.id][2]);
	
	var myAjax ;
	myAjax = new Ajax.Request(urlMVC,
							                {method: 'post', parameters: paras , onComplete : ActionForm._ActionInfo[this.id][2]});

};

ActionForm.Submit = function(formObj, element, message) {
	if (!(message==null || ""==message)) {
		if (!confirm(message)) {
			//ActionForm.OnChangeActionSession=true;
			return false;
		}
	}
	// if element is not form submit button, submit form
	if (!(element.tagName.toUpperCase()=="INPUT" && element.type.toUpperCase()=="SUBMIT")) {
		formObj.submit();
	}
};


ActionForm.Popup = function(name, width, height, message, status, scrollbars) {
	if (!(message==null || ""==message)) {
		if (!confirm(message)) {
			//ActionForm.OnChangeActionSession=true;
			return null;
		}
	}
	return openCenteredWindow( name, (new Date()).getTime(), width, height, status, scrollbars, "", "" );
};

ActionForm.Dialog = function(url, form,properties) {
	return window.showModalDialog(url,form,properties);
};

/**
 * ???????????????? ComboBox 
 */
ActionForm.clearField = function() {
	var allElements = ActionForm.GetForm().elements;
	
	for (var i=0; i<allElements.length; i++) {
		var element = ActionForm.GetForm().elements[i];
		if (['text', 'password'].contains(element.type)) {
			element.value = '';
		}
		else if (['radio', 'checkbox'].contains(element.type)) {
			element.checked = false;
		}
		else {
			if (element.tagName=='select') {
				element.selectedIndex = 0;
			}
		}
	}
};

/*
	arguments[0] = objId
	arguments[1] = actionName
	arguments[2] = width
	arguments[3] = height
	arguments[4] = status
	arguments[5] = scrollbars
	arguments[6] = help
	arguments[7] = resize
*/
ActionForm.SetActionDialog = function() {
	if (arguments[1] != null) {
		try {
			var relaObj = document.getElementById(arguments[0]);
			ActionForm._ActionInfo[relaObj.id] = arguments;
			ActionForm._ActionInfo[relaObj.id].previousClick = relaObj.onclick;
			relaObj.onclick = ActionForm.DialogActionEvent;
		}
		catch(E) { 
			alert("Can't find element with ["+arguments[0]+"]");
			return false;
		}
	}
	else {
		alert("action name is null!!!");
		return false;
	}
};

/**
 * 2006-09-06 By Daniel For showModalDialog
 */
ActionForm.DialogActionEvent = function() {
	var popwin = false;	
	var objId = this.id.substring(0, this.id.lastIndexOf("_"))
	var actionInfo = ActionForm._ActionInfo[this.id];
	if (actionInfo==null) {
		alert("No action be setted!!!");
		return false;
	}
	else {
		// execute previous click function
		try {
			if (actionInfo.previousClick != null) {
				if (actionInfo.previousClick.apply()==false) {
					return false;
				}
			}
		}
		catch(E) { }
		var urlMVC = ActionForm._ActionInfo[this.id][1];
		var width = ActionForm._ActionInfo[this.id][2];
		var height = ActionForm._ActionInfo[this.id][3];
		var status = ActionForm._ActionInfo[this.id][4];
		var scrollbars = ActionForm._ActionInfo[this.id][5];
		var help = ActionForm._ActionInfo[this.id][6];
		var resize = ActionForm._ActionInfo[this.id][7];
		
		if(typeof width == 'undefined')	width = 800;
		if(typeof height == 'undefined')	height = 600;		
		var x, y = 0;
		if (screen.availWidth) {
	    x = (screen.availWidth - width) / 2;
		  y = (screen.availHeight - height) / 2;
	  }
		var properties = 'dialogWidth:' + width + 'px;' + 'dialogHeight:' + height + 'px;' +  'center: yes' + '; status:' + ((status) ? 'yes' : 'no') + '; scroll:' + ((scrollbars) ? 'yes' : 'no') + ';help:' + ((help) ? 'yes':'no') + ';resizable :' + ((resize) ? 'yes':'no')+ ';';
		//alert(properties );
		popwin = ActionForm.Dialog( urlMVC,ActionForm.GetForm(),properties );
	}
	try{
		returnValues(popwin + "-" + objId);		
	}catch(E){}
	

	return popwin;
};

ActionForm.Dialog = function(url, form,properties) {
	return window.showModalDialog(url,form,properties);
};

// Begin Added by Jack Lee on 2007/08/05
ActionForm.PutPreviousCondition = function() {
  var condition = document.getElementById("__PREVIOUS_CONDITION");
  if (condition == null) {
    return;
  }
  condition = condition.value;
  if (condition == null || condition.length == 0) {
    return;
  }
  var nameValues = condition.split(";");
  var name;
  var value;
  var values;
  var index;
  for (var i = 0; i < nameValues.length; i++) {
    index = nameValues[i].indexOf("=");
    if (index > 0) {
      name = nameValues[i].substring(0, index);
      if (document.getElementById(name) == null) {
        value = nameValues[i].substring(index + 1);
        if (value.length > 0) {
          if (value.indexOf(",") > 0) {
            values = value.split(",");
            if (values.length > 0) {
              for (var j = 0; j < values.length; j++) {
                if (values[j].length > 0) {
                  document.forms[0].appendChild(document.createElement("<input type='hidden' name='" + name + "' value='" + values[j] + "' />"));
                }
              }
            }
          }
          else {
            document.forms[0].appendChild(document.createElement("<input type='hidden' name='" + name + "' value='" + value + "' />"));
          }
        }
      }
    }
  }
};
// End Added by Jack Lee on 2007/08/05
