/**
 * <p>Description: </p>
 * 
 * @author tianyu * Modification&Comments on 2005-7-28
 * 
 * @version 1.50
 * 
 * <p>Copyright: Copyright (c) 2004 2005</p>
 * <p>Company: 清华同方科技有限公司</p>
 */
 
 
/* Helper for the ezFramework*/

function EzUtility() {}

function EzUtility_getElementById(strid,objdoc) {
  var p,i,x;  
  if(!objdoc) objdoc=document; 
  if( (p=strid.indexOf('?'))>0 && parent.frames.length ) {
    objdoc=parent.frames[strid.substring(p+1)].document; 
    strid=strid.substring(0,p);
    }
  if( !(x=objdoc[strid]) && objdoc.all ) x=objdoc.all[strid]; 
  for (i=0; !x&&i<objdoc.forms.length; i++) x=objdoc.forms[i][strid];
  for(i=0; !x&&objdoc.layers&&i<objdoc.layers.length; i++) 
  	x=EzUtility_getElementById(strid,objdoc.layers[i].document);
  if(!x && document.getElementById) 
  	x=document.getElementById(strid); 
  return x;
}

function EzUtility_setElementVisibility(obj,vis){
	if (obj.style) { obj=obj.style; vis=(vis=='show')?'visible':(vis='hide')?'hidden':vis; }
	obj.visibility=vis; 	
}

function EzUtility_setElementVisibilityById() {
	//three args ,on group
	//there can be any groups
	//first arg: element id
	//second arg: visability - hide or show
	//third arg:some other info, no decided yet
  var obj,vis;
  var i;
  var args=EzUtility_setElementVisibilityById.arguments;

  for (i=0; i<(args.length-2); i+=3) 
  	if ((obj=EzUtility_getElementById(args[i]))!=null) { 
  		vis=args[i+1];
  		//alert(obj);
    	EzUtility_setElementVisibility(obj,vis);
    }
}

function EzUtility_getEnclosingElementByTagName(node,str) {
	// search the enclosing element which embbeds this node and has the specific tag name
	var parent = node.parentNode;
	
	if (null == parent) return null;
	
	if (parent.nodeName == str ) {
		return parent;
	} else {
		return arguments.callee(parent,str);
	}
}

function EzUtility_getEnclosingForm(node) {
	// search the form wich embbeds the Element
	var parent = node.parentNode;
	
	if (null == parent) return null;

	if (parent.nodeName == 'FORM') {
		return parent;
	} else {
		return arguments.callee(parent);
	}
}
function EzUtility_submitEnclosingForm(node) {

	var form = this.getEnclosingForm(node);
	
	if (null == form) {
		// form not specified -> do nothing
		return false;
	} else {
		form.submit();
	}
}
function EzUtility_resetEnclosingForm(node) {

	var form = this.getEnclosingForm(node);
	
	if (null != form) {
		form.reset();
	}

	// do not submit the form
	return false;
}
function EzUtility_createHidden(fldName, fldValue) {
	var input = null;

	// check if the hidden field already exist
	input = document.getElementById(fldName);

	if (null != input) {
		input.value=fldValue;
		return input;
	} else {
		var input=document.createElement('INPUT');
		input.type='hidden';
		input.id=fldName;
		input.name=fldName;
		input.value=fldValue;
		return input;
	}
}
/**
 * node......: Current node which generates the event
 * param.....: action used by the control to identify the event
 * formId....: optional id of the form, if null the enclosing form is searched by the script
 * userscript: optional jscript which must return true before the form is sumbitted
 */
function EzUtility_crtCtrla(node, param, formId, userscript) {
	var form = null;

	// if a user script was defined execute the script
	if (null != userscript) {
		var rtc = true;
		var obj = eval(userscript);

		if (typeof obj == 'boolean') {
			rtc = new Boolean(obj);
		}

		if (!rtc.valueOf()) {	
			return false;
		}
	}

	// first check if the formId was specified
	if (null != formId && '' != formId) {
		form = document.getElementById(formId);
	} else {
		// try to find the enclosing form
		form = this.getEnclosingForm(node);
	}

	if (null != form) {
		form.appendChild(this.createHidden('ctrla', param));
		form.submit();
	} else {
		// not found. Do nothing
	}
}

//should be called inline the button's onclick event
function EzUtility_resetSubmitState(node){
			if(null == node)return;
			it=node.getElementsByTagName('input');
			if(null != it)it[0].value='';
			return;
}


new EzUtility();
EzUtility.getEnclosingForm     = EzUtility_getEnclosingForm;
EzUtility.submitEnclosingForm  = EzUtility_submitEnclosingForm;
EzUtility.resetEnclosingForm   = EzUtility_resetEnclosingForm;
EzUtility.createHidden         = EzUtility_createHidden;
EzUtility.crtCtrla             = EzUtility_crtCtrla;
EzUtility.getEnclosingElementByTagName = EzUtility_getEnclosingElementByTagName;
EzUtility.setElementVisibilityById = EzUtility_setElementVisibilityById;
EzUtility.resetSubmitState = EzUtility_resetSubmitState;



/*Helper for the ListControl object*/
function ListHelp() {
}
/**
 * Unchecks all Checkboxes specified in the argument
 * param: items An array of input fields
 */
function ListHelp_uncheckAll(items) {

	for (var i=0; i < items.length; i++) {
		if (items[i].type == 'checkbox' ) {

			// uncheck checkbox
			items[i].checked = false;
		}
	}
}
function ListHelp_checkAll(items) {
	for (var i=0; i < items.length; i++) {
		if (items[i].type == 'checkbox' ) {

			// uncheck checkbox
			items[i].checked = true;
		}
	}
}
new ListHelp();
ListHelp.uncheckAll   = ListHelp_uncheckAll;
ListHelp.checkAll     = ListHelp_checkAll;


/*HTTPUtil*/
function HTTPUtil() {
}
function HTTPUtil_getParameters(location) {
	var url = new String(location);
	
	if (null == url) {
		return null;
	}
	var qString = url.split('?');

	if (null == qString || qString.length == 1) {
		return null;
	}
	
	var params = qString[1].split('&');
	return params;
}
function HTTPUtil_getParameter(key, params) {
	
	if (null == params || null == key) {
		return '';
	}
	
	for (var i=0; i < params.length; i++) {
		var arr = params[i].split('=');
		
		if (arr[0].toUpperCase() == key.toUpperCase()) {
			return arr[1];
		}
	}
}
function HTTPUtil_isSecure() {
	return ('https:' == window.location.protocol);
}
new HTTPUtil();
HTTPUtil.getParameters  = HTTPUtil_getParameters;
HTTPUtil.getParameter   = HTTPUtil_getParameter;
HTTPUtil.isSecure       = HTTPUtil_isSecure


