ns = (document.layers)? true:false 
ie = (document.all)? true:false 

/////////////////  Scripts for handling Radio Buttons

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

/////////////////  Scripts for handling CheckBoxes

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

function CheckAll(CbList)
{
    for (i = 1; i < CbList.length; i++) {
          CbList[i].checked = CbList[0].checked;
    }
}

function setCheckboxGroupValue(CbList)
{
    var out = "" ;

    for (i = 1; i < CbList.length; i++) {
	if (CbList[i].checked && CbList[i].value != "")
	{
          out += CbList[i].value + ",";
	}	
    }
    if (out.charAt(out.length-1) == ",") out = out.substring(0, out.length-1); 
    CbList[0].value = out;
}

function confirmSubmitCheckboxGroup(form, CbList, action)
{
 if(confirm("Are you sure to "+action+" the selected items ?"))
  {
     setCheckboxGroupValue(CbList);
     if (CbList[0].value == "") 
       alert("No item selected !"); 
     else
       form.submit();
  }
}

/////////////////  Scripts for handling List/Menu

function getSelectedListValue(CONTROL) {
	var list_values = "";
	for (var i=0;i<CONTROL.length;i++) {
		if (CONTROL.options[i].selected) {
			if (list_values!="") list_values += ",";
			list_values += CONTROL.options[i].value;
		}
	}
	return list_values;
}

function getListValue(CONTROL) {
	var list_values = "";
	for (var i=0;i<CONTROL.length;i++) {
			if (list_values!="") list_values += ",";
			list_values += CONTROL.options[i].value;
	}
	return list_values;
}

function selectAllList(CONTROL){
   for(var i = 0;i < CONTROL.length;i++){
      CONTROL.options[i].selected = true;
   }
}

function deselectAllList(CONTROL){
   for(var i = 0;i < CONTROL.length;i++){
      CONTROL.options[i].selected = false;
   }
}

function confirmSubmit(form, action)
{
  if(confirm("Are you sure to "+action+" the selected items ?")) form.submit();
}

/////////////////  Other Scripts

function gotoAnchor(sAnchor) {
 if (sAnchor.length > 0) {
 	if (ns) window.scrollTo(0, document.anchors[sAnchor].y);

 	if (ie) eval('document.all.' + sAnchor + '.scrollIntoView()');

 }

}

function popupResize(url, width, height, scrollbars)
{
     scrollbars = (scrollbars == 1) ? "yes" : "no";
     var d = new Date();
     var win = window.open(url,d.getHours()+d.getMinutes()+d.getSeconds(),"resizable=yes,height="+height+",width="+width+",scrollbars="+scrollbars);
	 win.moveTo((window.screen.width-width)/2,(window.screen.height-height)/2);
     win.focus();
}

function popupFix(url, width, height)
{
     popupFix(url, width, height, 1)
}

function popupFix(url, width, height, scrollbars)
{
     scrollbars = (scrollbars == 1) ? "yes" : "no";
     var d = new Date();
     var win = window.open(url,d.getHours()+d.getMinutes()+d.getSeconds(),"resizable=no,height="+height+",width="+width+",scrollbars="+scrollbars);
	 win.moveTo((window.screen.width-width)/2,(window.screen.height-height)/2);
     win.focus();
}

function replaceString(Sstring,Cstring,Dstring) {
	  var blength=Cstring.length;
	  var firstbyte=Sstring.indexOf(Cstring,0);
	  for (var i=0;i<=Sstring.length-blength;i++){
		tstring=Sstring.substring(i,i+blength);
		if (tstring==Cstring){
		  Sstring=Sstring.substring(0,i)+Dstring+Sstring.substring(i+blength,Sstring.length);
		}
	  }
	  return Sstring; 
} // Ends the "replaceSubstring" function

function checknumber(x){
var anum=/(^\d+$)|(^\d+\.\d+$)/
if (anum.test(x))
testresult=true
else{
testresult=false
}
return (testresult)
}

function checkChin(str) {
      hasChin = false;
      i = 0;
      while (((letter=str.charAt(i))!="")&&(!hasChin)) {
            num=escape(letter);
      	    if ((num1=num.substring(1,3)) > '7F' ) {
               hasChin = true;
            }
            i++;
      }
      return hasChin;
}


function extractFilename(filepath, seperator) {
   var temp=filepath.split(seperator);
   return temp[temp.length-1];
}

function compare(aText, bText) { 
    i = 0; 
    aString = aText.toString(); aString.toLowerCase(); 
    bString = bText.toString(); bString.toLowerCase(); 
    if (aString < bString) { return -1; } 
    else if (aString > bString) { return 1; } 
    return 0; 
} 

function removeOption(mylist) {
	var index=0;
	while (index<mylist.length) {
		if (mylist.options[index].selected) {
			mylist.options[index]=null;
			//mylist.options.remove(index);
		} else {
			index++;
		}
	}
}

function sortOptions(mylist) { 
  var copyOption = new Array(); 

  for (var i=0;i<mylist.options.length;i++) 
        copyOption[i] = new Array(mylist[i].value, mylist[i].text); 

  copyOption.sort(function(a,b) { return compare(a[1],b[1]); }); 

  for (var i=mylist.options.length-1; i>-1; i--) 
        mylist.options[i] = null; // this may bomb in Netscape 

  for (var i=0; i<copyOption.length; i++) { 
    mylist.options[mylist.length] = new Option( copyOption[i][1], 
    copyOption[i][0], false, false ); 
    mylist.options[mylist.length-1].selected = false; 
  } 
}

function addRow(table, row) {
	var tbl = document.getElementById(table);
	var rows = tbl.rows;
	var ori_row = rows[0];
	var lastRow = tbl.rows.length;
	var new_row = tbl.insertRow(lastRow);
	var new_row_id = row + "_" + new Date().getTime();;
	new_row.id = new_row_id
	var cell = new_row.insertCell(0);
	var sHTML=replaceString(ori_row.innerHTML, row, new_row_id);
	cell.innerHTML=sHTML;
}    

function getRowPosition(rows, row_id) {
	for (i=0; i<rows.length; i++) {
		if (rows[i].id==row_id)
			return i;
    }
}

function removeRow(table, row_id) {	
	var tbl = document.getElementById(table);
	var rows = tbl.rows;
	if (rows.length>1)
		tbl.deleteRow(getRowPosition(rows, row_id));
}

function toggleDisplayRow(row_id) {
 if( document.getElementById(row_id).style.display=='none' ){
   document.getElementById(row_id).style.display = '';
 }else{
   document.getElementById(row_id).style.display = 'none';
 }
}

function getImgHTML(url, alt, border, align, width, height, vspace, hspace) {
	var html = "";
	if (url.indexOf("swf")>0) {
		if (width.length==0) {
			width = "100%";
		}
		html = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0' width='" + width + "'"
		if (height.length>0) {
			html = html + " height='" + height + "'";
		}
		html = html + ">";
       	html = html + "<param name='movie' value='" + param.f_url + "'>"
       	html = html + "<param name='quality' value='high'>"
		html = html + "<param name='menu' value='false'>"
		html = html + "<param name='scale' value='noborder'>"
		html = html + "<embed scale='noborder' src='" + param.f_url + " quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' menu='false' width='" + width + "'"
		if (height.length>0) {
			html = html + " height='" + height + "'";
		}
		html = html + "></embed></object>"
	} else {
  		html = '<img src="' + url + '"';
  		if (alt.length>0) {
   			html += ' alt="' + alt + '" title="' + alt + '"';
		}
	  	if (border.length>0) {
		    html += ' border="' + border + '"';
		}
		if (width.length>0) {
    		html += ' width="' + width + '"';
		}
  		if (height.length>0) {
    		html += ' height="' + height + '"';
		}
  		if (align.length>0) {
    		html += ' align="' + align + '"';
		}
  		if (vspace.length>0) {
    		html += ' vspace="' + vspace + '"';
		}
  		if (hspace.length>0) {
    		html += ' hspace="' + hspace + '"';
		}
		html += '>';
	}
	return html;
}

function centerWindow(width, height) {
	window.resizeTo(width,height);
	window.moveTo((window.screen.width-width)/2,(window.screen.height/2)-(height/2));			
}
////////////////////////////////////////////////

function submitForm(targetForm) {
	targetForm.submit();
}

function changePage(targetForm, component, page){
	component.value=page;
	//component.selectedIndex=page-1;
	targetForm.submit();
}

function performAction(targetForm, action, actionComponent, idComponent, idsComponent){
	var ids = getSelectedCheckboxValue(idComponent);
	if (ids=="") {
			alert("沒有已選的項目!");
	} else {
			if (confirm("確定執行?")) {
				actionComponent.value = action;
				idsComponent.value = ids;
				submitForm(targetForm);
			}
	}
}

function cleanHTML(obj) {
	obj.value = stripTagsAttrs(obj.value, "p");
	obj.value = removeTag(obj.value, "span");
	obj.value = removeTag(obj.value, "div");
	obj.value = removeTag(obj.value, "font");
}

function removeTag(html, tagName) {
	tag = "<"+tagName;
	endtag = "</" + tagName;
	spos1 = html.indexOf(tag);
	while (spos1>=0) {
		spos2 = html.indexOf(">",spos1+1);
		epos1 = html.indexOf(endtag, spos2);
		epos2 = html.indexOf(">",epos1+1);		
		html = html.substring(0, spos1) + html.substring(spos2+1, epos1) + html.substring(epos2+1, html.length);
		spos1 = html.indexOf(tag, spos1);
	}
	return html;
}

function stripTagsAttrs(html, tagName) {
	tag = "<"+tagName;
	spos = html.indexOf(tag);
	while (spos>=0) {
		epos = html.indexOf(">",spos+1);
		html = html.substring(0, spos+tag.length) + html.substring(epos,html.length);
		spos = html.indexOf(tag, spos+tag.length);
	}
	return html;
}