/**
 * Popup a new window for operatearea input.
 * @param popupUrl  The url of popupUrl jsp file (since webRoot may varies)
 */
function popupOperateArea(popupUrl)
{
	openBrWindow(popupUrl, '', 'width=700,height=250,left=200,top=150');
}

/**
 * Popup a new window for operatearea input.
 * @param popupUrl  The url of popupUrl jsp file (since webRoot may varies)
 */
function popupOperateArea2(popupUrl)
{
	openBrWindow(popupUrl, '', 'width=700,height=400,left=200,top=150');
}


/**
 * Popup a new window for operatearea input.
 * @param popupUrl  The url of popupUrl jsp file (since webRoot may varies)
 */
function popupOperateArea3(popupUrl)
{
	openBrWindow(popupUrl, '', 'width=610,height=500,left=200,top=150,status=yes');
}


function popupSelfArea(popupUrl)
{
	openBrWindow(popupUrl, '_self');
}

/**
 * Popup a new window for operatearea input.
 * @param popupUrl  The url of popupUrl jsp file (since webRoot may varies)
 */
function popupOperateArea4(popupUrl)
{
	openBrWindow(popupUrl, '', 'width=650,height=410,left=200,top=150');
}

/**
 * Popup a new window for operatearea input.
 * @param popupUrl  The url of popupUrl jsp file (since webRoot may varies)
 */
function popupOperateArea5(popupUrl)
{
	openBrWindow(popupUrl, '', 'width=700,height=500,left=200,top=150');
}

/**
 * Popup a new window for textarea input.
 * @param inputId   The Id of the textarea element in the parent window
 * @param action    Should be either "edit" or "view"
 * @param popupUrl  The url of popupTextArea.jsp (since webRoot may varies)
 */
function popupTextArea(inputId, action, popupUrl)
{
//    newWin = openBrWindow(popupUrl+'?inputId='+inputId+'&action='+action, '', 'width=200,height=100,left=400,top=350');
	openBrWindow(popupUrl+'?inputId='+inputId+'&action='+action, '', 'width=200,height=100,left=400,top=350');
}

/**
 * Get the inner text of specified element.
 */
function getInnerText(inputId)
{
    return document.getElementById(inputId).innerText;
}

/**
 * Set the inner text of specified element.
 */
function setInnerText(inputId, value)
{
    document.getElementById(inputId).innerText = value;
}

/**
 * Get the defined selected checkboxes number
 * @param selectElementArray the checkbox array
 */
function getSelectedNum (selectElementArray)
{
    var total = 0;

    if (selectElementArray != null)
	{
    	var max = selectElementArray.length;
    	
    	if (max==null)
    	{
    		if (selectElementArray.checked == true)
				total = 1;
    	}
    	
    	if (max > 1)
		{
	    	for (var index = 0; index < max; index++) 
       			if (selectElementArray[index].checked == true)
                	total += 1;
		}
    	else
		{
    		if (selectElementArray.checked == true)
				total = 1;
		}
    }

    return total;
}

/**
 *  Chang all the selection's status by changing first checkbox's status
 */
function	changeSelect (element, selectName)
{
	if (element.checked)
	{
		selectAllSelection (eval ("element.form." + selectName));
	}
	else
	{
		clearAllSelection (eval ("element.form." + selectName));
	}
}

/**
 *  Chang all the selection's status by clicking the image
 */
function changeSelect2(element, selectName)
{
	if (element.value=="true")
	{
		selectAllSelection (eval ("element.form." + selectName));
		element.value = "false";
	}
	else
	{
		clearAllSelection (eval ("element.form." + selectName));
		element.value = "true";
	}
}

/**
 * Clear all the selection for the given selection name
 * @param selectElementArray
 */
function clearAllSelection (selectElementArray)
{
    if (selectElementArray != null)
	{
    	var numberOfChoice = selectElementArray.length;
    	
    	if (numberOfChoice==null)
    		selectElementArray.checked = false;
    		
    	if (numberOfChoice > 1)
	    	for (var index = 0; index < numberOfChoice; index++) 
       			if (selectElementArray[index].checked == true)
          			selectElementArray[index].checked=false
    	else
    		selectElementArray.checked = false;
    }
}

/**
 * Clear the first selection for the given selection name
 * @param selectElementArray
 */
function clearFirstSelection (selectElementArray)
{
    if (selectElementArray != null)
	{
    	var numberOfChoice = selectElementArray.length;
    	
    	if (numberOfChoice==null)
    		selectElementArray.checked = false;
    		
    	if (numberOfChoice > 1)
	    	for (var index = 0; index < numberOfChoice; index++) 
       			if (selectElementArray[index].checked == true)
				{
          			selectElementArray[index].checked=false;
					break;
				}
    	else
    		selectElementArray.checked = false;
    }
}

/**
 * Select all the selection for the given selection name
 * @param selectElementArray
 */
function selectAllSelection (selectElementArray)
{
    if (selectElementArray != null)
	{
    	var numberOfChoice = selectElementArray.length;
    
    	if (numberOfChoice==null)
    		selectElementArray.checked = true;
    	
   		if (numberOfChoice > 1)
	    	for (var index = 0; index < numberOfChoice; index++) 
       			if (selectElementArray[index].checked == false)
        	  		selectElementArray[index].checked=true
		else
	    	selectElementArray.checked = true;
	}
}

/**
 * Get the first selected checkboxes value
 * @param selectElementArray the checkbox array
 */
function getFirstSelectedValue(selectElementArray)
{
	if (selectElementArray != null)
	{
		var max = selectElementArray.length;
		
		//	the selectElementArray is not a array, only a select object
		if (max==null)
		{
			if (selectElementArray.checked == true)
			{
				return selectElementArray.value;
			}
		}
		
		for (var index = 0; index < max; index++) 
    	{
    		if (selectElementArray[index].checked == true)
    		{
				return selectElementArray[index].value;
            }
    	}
    }
    
    return null;
}

/**
 * Get the first selected checkboxes value
 * @param selectElementArray the checkbox array
 */
function getAllSelectedValue (selectElementArray)
{
	var	array = new	Array ();

	if (selectElementArray != null)
	{
		var max = selectElementArray.length;
		
		//	the selectElementArray is not a array, only a select object
		if (max==null)
		{
			if (selectElementArray.checked == true)
			{
				arraypush (array, selectElementArray.value);
				return	array;
			}
		}
		
		for (var index=0; index<max; index++) 
    	{
    		if (selectElementArray[index].checked == true)
    		{
				arraypush (array, selectElementArray[index].value);
            }
    	}
    }
    
    return array;
}

/**
 *	Clear all the options under the specified select element
 */
function clearSelect(select)
{
	if (null==select.options) return;

	for (i=select.options.length-1;i>=0;i--)
	{
		var	option = select.options[i];
		select.removeChild(option);
	}
}

/**
 *	Get the specified select element value.
 */
function getSelectedValue(select)
{
	if (null==select.options) return null;
	
	for (i=0;i<select.options.length;i++)
	{
		if (select.options[i].selected == true)
		{
			return select.options[i].value;
		}
	}

	return null;
}

/**
 * Submit a form, together with following types of elements contained in every sub iframe.
 *
 * @param form   a form object
 */
function submitWithIFrame(form)
{
	preSubmitWithIFrame(form);
	form.submit();
}

/**
 * Submit a form, together with following types of elements contained in every sub iframe.
 *
 * @param form   a form object
 */
function preSubmitWithIFrame(form)
{
    var i;
    
	var iframes = form.getElementsByTagName("iframe");

	//	prepare a string array, contained the name of the elements 
	//	which should be removed from the given form
	var removeArray = new Array();
	
	for (i=0;i<iframes.length;i++)
	{
		getRemoveArrayFormIFrame(removeArray, document.frames(iframes[i].name));
	}

	//	remove the elements which's name contains in the removeArray
	for (i=0;i<removeArray.length;i++)
	{
		//	get the elements by given name in the removeArray
		if (removeArray[i]=='') continue;
		var elements = document.getElementsByName(removeArray[i]);
		
		for (j=elements.length-1;j>=0;j--)
		{
			//	if the form contains the element, remove it
			if (form.contains(elements[j]))
			{
				form.removeChild(elements[j]);
			}
		}
	}

	//	append the new elements from inner iframes
    for (i=0;i<iframes.length;i++)
    {
    	appendElementFromIFrame(document, form, document.frames(iframes[i].name));
	}
}

/**
 * Append all the input values from iframe to current document and form recurvely.
 * If iframe has sub iframes, invoke this function first with iframe's document 
 * and subframe of this iframe. 
 *
 * These type inputs will be hidden values in current document:
 * 
 * type 1: text input
 * type 2: textarea input
 * type 3: password input
 * type 4: checkbox input which is checked
 * type 5: radio input witch is checked
 * type 6: select option which is selected
 * type 7: hidden input
 * 
 * @param document		the top level document
 * @param form			the top level form which all inputs in sub iframe will be append to
 * @param iframe		the sub iframe of current document
 */
function appendElementFromIFrame(document, form, iframe)
{
	var i, j, k;
	
	//	get all sub iframe of this iframe
	var iframes = iframe.document.getElementsByTagName("iframe");

	if (iframes!=null)
	{
		for (i=0;i<iframes.length;i++)
		{
			appendElementFromIFrame(document, form, iframe.document.frames(iframes[i].name));
		}
	}

	var inputs, selects, options, areas;
    var itype;
    var hidden;
        
	//	append the inputs from iframe to current document
	inputs = iframe.document.getElementsByTagName("input");
	
    for (j = 0; j < inputs.length; j++)
    {
        itype = inputs[j].type.toLowerCase();
        if ((itype == "checkbox" && inputs[j].checked) ||
            (itype == "radio"    && inputs[j].checked) ||
            (itype == "text")                          ||
            (itype == "hidden")                        ||
            (itype == "password"))
        {
            hidden = document.createElement('<input type="hidden" name="'+inputs[j].name+'" value="'+inputs[j].value+'">');
            form.appendChild(hidden);
        }
		// add by xjma	2002.02.01
        else if (itype == "file")
		{
//alert ('<input type="file" name="'+inputs[j].name+'" value="'+inputs[j].value+'">');
            hidden = document.createElement('<input type="file" name="'+inputs[j].name+'" value="'+inputs[j].value+'">');
            form.appendChild(hidden);
//alert ('form.' + inputs[j].name + '.setAttribute ("value", "' + inputs[j].value + '")');
			eval ('form.' + inputs[j].name + '.setAttribute ("value", "' + inputs[j].value + '")');
		}
    }
    //	append the selects from iframe to current document
    selects = iframe.document.getElementsByTagName("select");
    
    for (j = 0; j < selects.length; j++)
    {
        options = selects[j].getElementsByTagName("option");
        for (k = 0; k < options.length; k++)
        {
            if (selects[j].type=='select-multiple' || options[k].selected)
            {
                hidden = document.createElement('<input type="hidden" name="'+selects[j].name+'" value="'+options[k].value+'">');
                form.appendChild(hidden);
            }
        }
    }

    //	append the textarea from iframe to current document
    areas = iframe.document.getElementsByTagName("textarea");
    for (j = 0; j < areas.length; j++)
    {
        hidden = document.createElement('<input type="hidden" name="'+areas[j].name+'" value="'+areas[j].innerText+'">');
        form.appendChild(hidden);
    }
}

/**
 *	Build the string array, which contain the element name that should be 
 *	removed from top level form. 
 */
function getRemoveArrayFormIFrame(removeArray, iframe)
{
	var i, j, k;
	
	//	get all sub iframe of this iframe
	var iframes = iframe.document.getElementsByTagName("iframe");
	
	if (iframes!=null)
	{
		for (i=0;i<iframes.length;i++)
		{
			getRemoveArrayFormIFrame(removeArray, iframe.document.frames(iframes[i].name));
		}
	}
	
	var inputs, selects, options, areas;
    var itype;
    var hidden;
        
	//	append the inputs from iframe to current document
	inputs = iframe.document.getElementsByTagName("input");
	
    for (j = 0; j < inputs.length; j++)
    {
    	itype = inputs[j].type.toLowerCase();
    	//	changed by heinz, the checkbox and radio should be removed, whether be checked
    //  if ((itype == "checkbox" && inputs[j].checked) ||
    //      (itype == "radio"    && inputs[j].checked) ||
    	if ((itype == "checkbox") ||
    		(itype == "radio") 	  ||
            (itype == "text")     ||
            (itype == "hidden")   ||
            (itype == "password") ||
            (itype == "file")) // add by xjma	2002.02.01
        {
        	if (findInArray(removeArray, inputs[j].name)<0)
        	{
        		arraypush(removeArray, inputs[j].name);
        	}
        }
	}
	
	selects = iframe.document.getElementsByTagName("select");
    
    for (j = 0; j < selects.length; j++)
    {
    	if (findInArray(removeArray, selects[j].name)<0)
    	{
    		arraypush(removeArray, selects[j].name);
    	}
	}

    areas = iframe.document.getElementsByTagName("textarea");
    for (j = 0; j < areas.length; j++)
    {
    	if (findInArray(removeArray, areas[j].name)<0)
    	{
    		arraypush(removeArray, areas[j].name);
    	}
    }
}

/**
* Hide all iframes which has specified name prefix, except the one which has 
* the name "ifname_index". Showing/Hiding is done by setting the height of iframe.
*
* @param ifname     iframes which has "ifname_" as name prefix will be hidden 
*                   except "ifname_index".
* @param index      iframe "ifname_index" will not be hidden.
* @param height     iframe "ifname_index"'s height attribute will be set to this.
*/
function switchIFrame(ifname, index, height)
{
    var iframes = document.getElementsByTagName("iframe");
    var prefix = ifname+"_";
    var i;
    for (i = 0; i < iframes.length; i++)
    {
        if (iframes[i].name.indexOf(prefix) == 0)
        {
            iframes[i].height = 0;
        }
    }
    document.getElementById(prefix+index).height = height;
}

/**
* Make the given iframe jumping to the given url.
* This function copy from the xu's page
* @param ifname     the iframe name which want jump to other url
* @param inputurl   the target url
*/
function jumpto(ifname, inputurl)
{
	var displaymode = 0;

	if (document.getElementById&&displaymode==0)
	{
		document.getElementById(ifname).src=inputurl
	}
	else if (document.all&&displaymode==0)
	{
		document.all.external.src=inputurl
	}
	else
	{       
		if (!window.win2||win2.closed)
		{
			win2=window.open(inputurl)       
		}
			//else if win2 already exists       
		else
		{       
		}       
	}       
}   

/**
 *	Hide the specified iframe page in current documnet.
 *	Why get the iframe so complex, i also didn't know.
 */
function hideFrame(ifname)
{
	var iframes = document.getElementsByTagName("iframe");
	var i;
	for (i=0;i<iframes.length;i++)
    {
		if (iframes[i].name.indexOf(ifname)==0)
        {
			iframes[i].height = 0;
        }
    }
}

/**
 *	Show the specified iframe page in current document with given height.
 *	Why get the iframe so complex, i also didn't know.
 */
function showFrame(ifname, height)
{
	var iframes = document.getElementsByTagName("iframe");
	var i;
	for (i=0;i<iframes.length;i++)
    {
		if (iframes[i].name.indexOf(ifname)==0)
        {
			iframes[i].height = height;
        }
    }
}

/**
 *	Find the specified string in a string array.
 *	
 *	@param	strArray	the string array contains strings
 *	@param	strArray	the specified string
 *	@return				-1 when no found, etherwise return the index
 */
function findInArray(strArray, str)
{
	if (strArray==null)
	{
		return -1;
	}
	
	for (i=0;i<strArray.length;i++)
	{
		if (strArray[i] == str)
		{
			return i;
		}
	}
	
	return -1;
}

/**
 *	Split the specified string, and return to the index's part.
 */
function getSplitString(str, separator, index)
{
	if (str==null) return null;
	
	var arrayOfStrings = str.split(separator);
	return arrayOfStrings[index];
}

/**
 *	push the specified data to array.
 */
function arraypush(array, value)
{
//	in javascript 1.2, support by IE 5.5 and later
//	array.push(value);
//	in javascript 1.1, support by IE 5.0 and lower
	array[array.length] = value;
}

function openBrWindow (theURL,winName,features)
{
	newWin = window.open (theURL,winName,features);
	newWin.focus ();
	//window.location = theURL;
	return;
}

/**
 *	Change the element value by select value. This function should be
 *	attached on the select element.
 *
 *	@param	select	the select element
 *	@param	element	the element which's value will be changed
 *	@param	names	the name array if the selected value defined in this array
 *	@param	values	the value array which the new value defined in this array
 */
function changeValue(select, element, names, values)
{
	var name = getSelectedValue(select);
	
	var index = findInArray(names, name);
	
	if (index>=0&&index<values.length)
	{
		element.value = values[index];
	}
}


//Change the select_status of all checkbox at one time. 
//Add by lixinqian 2002.2.1
//Usage Demo:
/*
function changeSelect(ctlChkbox,chkbox)
{
  //ctlChkbox: a control checkbox object that decides if all other check box are checked.
  var form1 = document.listForm;// a form object
  var strChkboxName = "selectedIds";//a string of checkbox name for comparison
  chkboxSelect(form1,ctlChkbox,strChkboxName);
}
*/
function chkboxSelect(form1,ctlChkbox,strChkboxName)
{
  for(var i=0;i<form1.elements.length;i++)
  {
    var e=form1.elements[i];
    if((e.name).indexOf(strChkboxName)!=-1) e.checked=ctlChkbox.checked;    
  }
}

	/**
	 *	add by xzheng
	 */
	function	selectAllInSameLine(selectElementArray,compareFieldIndex,delimiter,lineId,lineStatus)
	{
	    if (selectElementArray != null)
		{
   		 	var numberOfChoice = selectElementArray.length;

   			if (numberOfChoice==null)
			{
    			selectElementArray.checked = true;
			}
    	
   			if (numberOfChoice > 1)
	    		for (var index = 0; index < numberOfChoice; index++) 
       				if (isSameLine(selectElementArray[index].value,compareFieldIndex,delimiter,lineId))
					{
						if	(lineStatus.value=="true")
						{
        	  				selectElementArray[index].checked=false;
						}
						else
						{
        	  				selectElementArray[index].checked=true;
						}
					}
			else
			{
				if	(lineStatus.value=="true")
				{
	    			selectElementArray.checked = false;
				}
				else
				{
	    			selectElementArray.checked = true;
				}
			}
			if	(lineStatus.value=="true")
			{
				lineStatus.value="false";
			}
			else
			{
				lineStatus.value="true";
			}
		}
	}

	function	isSameLine(selectValue,compareFieldIndex,delimiter,lineId)
	{
		var pos = 0;
		var tmpStr = selectValue;
		for	(var i=0;i<compareFieldIndex;i++)
		{
			pos = tmpStr.indexOf(delimiter);
			tmpStr = tmpStr.substr(pos+1,tmpStr.length);
		}
		var endPos = tmpStr.indexOf(delimiter);
		if	(endPos==-1)
		{
			endPos=tmpStr.length;
		}
		if	(tmpStr.substr(0,endPos)==lineId)
		{
			return	true;
		}
		else
			return	false;
	}
/**
 * 判断字符串是否为空，
 * @param 要判断的字符串
 * @return 为空true
 * @author zhj
 */
function checkVoidStr(inputStr){
var space = /\s+/g;
var anykey = /\S+/gi;
	if ((inputStr==""||space.test(inputStr))&&!anykey.test(inputStr)){
		return true;
	}
}


//计算checkbox项选中的数目
function checkedCount(checkbox){
if(checkbox==null) return 0;
if(checkbox.length==null) {
	if(checkbox.checked) return 1;
	else return 0;
}else {
	var c = 0;
	for(i=0;i<checkbox.length;i++){
		if(checkbox[i].checked) c++;
	}
	return c;
}

}

function checkAll(checkbox,ifchecked){
	if(checkbox==null) return false;
	if(checkbox.length==null) {
		checkbox.checked=ifchecked;
	}else{
		for(i=0;i<checkbox.length;i++){
			checkbox[i].checked = ifchecked;
		}
	}


}
