// -------------------------------------------------------------------- //
// --------------- 將 form 的值, 轉成GET 形式後回傳 ------------------- //
// -------------------------------------------------------------------- //
function postvalue2get(theform)
{

  var i=0;
  var getstr = '';

  while(theform.elements[i])
  {
	if(theform.elements[i].name)
	{
	  if(theform.elements[i].type == 'radio' || theform.elements[i].type == 'checkbox')
	  {
		if(theform.elements[i].checked == true)
		{
		  getstr = getstr+'&'+theform.elements[i].name+'='+encodeURI(theform.elements[i].value);
		}
	  }
	  else
	  {
		getstr = getstr+'&'+theform.elements[i].name+'='+encodeURI(theform.elements[i].value);
      }
    }
  i++;
  }
  return getstr;
}
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// --- 將 form or 畫面上所有的 checkbox or radio, 全選 or 取消 -------- //
// -------------------------------------------------------------------- //
//用於資料量少時
function select_all(theform, attribute, method)
{
    var i=0;
    //var Msec1 = (new Date()).getTime();
    //alert(Msec1);
    while(theform.elements[i])
    {
        if(theform.elements[i].type == 'radio' || theform.elements[i].type == 'checkbox')
        {
            var this_id = theform.elements[i].id;
            //alert(theform.elements[i].attribute);

            if(theform.elements[i].attribute == attribute)
            {
                theform.elements[i].checked = method;
                //alert('1: '+theform.elements[i].attribute);
            }
            else if(this_id.search(attribute) >= 0)
            {
                theform.elements[i].checked = method;
                //alert('2: '+theform.elements[i].id.search(attribute));
            }
            else
            {
                //alert('3: '+theform.elements[i].id);
            }
        }
    i++;
    }

    //var Msec2 = (new Date()).getTime();
    //alert(Msec2);
    //alert(Msec2 - Msec1);
    return true;
}
//用於資料量大時, 1. 可不受 form 中的欄位數量限制, 2. 處理速度快約 68%
function select_all_2(prefix, id_string, method)
{
  var i=0;
  //var Msec1 = (new Date()).getTime();
  //alert(Msec1);
  //alert(prefix);
  id_array = id_string.split(",");
  while(id_array[i] >= 1 || id_array[i] >= 'a')
  {
	//alert(id_array[i]);
    this_id = prefix + '' + id_array[i];
    //alert(this_id);
	this_obj = document.getElementById(this_id);
	if(this_obj.type == 'radio' || this_obj.type == 'checkbox')
    {
      this_obj.checked = method;
    }
  i++;
  }

  return true;
}
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// --------------- URI 編碼 ------------------------------------------- //
// -------------------------------------------------------------------- //
function for_URIencode(str)
{
	str = encodeURIComponent(str);
	while(str.search("'") != -1)
		str = str.replace(/'/, "%27");

	return str;
}

// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// --------------- 物件顯示與隱藏 ------------------------------------- //
// -------------------------------------------------------------------- //
function be_display(obj)
{
  if(obj.style.display == "none")
  {
  	obj.style.display = "";
    return true;
  }
  else
  {
  	obj.style.display = "none";
  	return false;
  }
}

function display_be_hidden_absolute(bt_obj, obj)
{
  if (obj.style.display == 'none') {
    obj.style.position = '';
    obj.style.display = '';
    bt_obj.style.display = 'none';
  } else {
    obj.style.display = 'none';
    bt_obj.style.display = '';
  }
  return false;
}

function hidden_a_display(obj, img, img_open, img_close)
{
  if (obj.style.display == 'none') {
    obj.style.display = '';
    img.src =img_close;
  } else {
    obj.style.display = 'none';
    img.src =img_open;
  }
  return false;
}

function hidden_a_display_2(obj, checkbox, str1)
{
  var i=0;
  DIV_obj = document.getElementsByTagName("DIV");
  while(DIV_obj[i])
  {
    thisobj = DIV_obj[i];
    if(thisobj.id.match(str1)) {
      thisobj.style.display = 'none';
    }
  i++;
  }

  if(checkbox.checked == true) { obj.style.display = ''; }
  else {  obj.style.display = 'none';  }
  return false;
}
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// --------------- 滑鼠經過表格時變色 --------------------------------- //
// -------------------------------------------------------------------- //
function sbar(obj, color){obj.style.backgroundColor=color;}
// -------------------------------------------------------------------- //


// -------------------------------------------------------------------- //
// --------------- 關閉視窗相關 --------------------------------------- //
// -------------------------------------------------------------------- //
function CloseClicked()
{
  window.returnValue = '';
  window.close();
}
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// --------------- 字串取代 ------------------------------------------- //
// -------------------------------------------------------------------- //
function ReplaceAll(strSource, strFind, strRepl) {
    var str5 = new String(strSource);

	while (str5.indexOf(strFind) != -1) {
          str5=str5.replace(strFind, strRepl);
    }
   return str5;
}
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// --------------- Select 選單 ---------------------------------------- //
// -------------------------------------------------------------------- //
var dropShow=false
var currentID
function dropdown(el){
if(dropShow){
dropFadeOut()
}else{
currentID=el
el.style.visibility="visible"
dropFadeIn()
}
}
function dropFadeIn(){//選單淡入的效果
if(currentID.filters.alpha.opacity<100){
currentID.filters.alpha.opacity+=20
fadeTimer=setTimeout("dropFadeIn()",50)
}else{
dropShow=true
clearTimeout(fadeTimer)
}
}
function dropFadeOut(){//選單淡出的效果
if(currentID.filters.alpha.opacity>0){
clearTimeout(fadeTimer)
currentID.filters.alpha.opacity-=20
fadeTimer=setTimeout("dropFadeOut()",50)
}else{
dropShow=false
currentID.style.visibility="hidden"
}
}
function dropdownHide(){
if(dropShow){
dropFadeOut()
dropShow=false
}
}
function hiLight(el){//高亮度顯示指標位置
if(dropShow){
for(i=0;i<el.parentElement.childNodes.length;i++){
el.parentElement.childNodes(i).className="link_record0"
}
el.className="link_record1"
}
}
function CheckMe(el){//替換顯示內容
el.parentElement.parentElement.childNodes(0).childNodes(0).childNodes(0).childNodes(0).childNodes(0).childNodes(0).innerHTML=el.innerHTML
}
document.onclick=dropdownHide
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// ---------------------- Wait ---------------------------------------- //
// -------------------------------------------------------------------- //
function waitSecond(second)
{
    var date = new Date();
    var curDate = null;
    second = second * 1000;
    do { curDate = new Date(); }
    while(curDate-date < second);
}
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// ---------------  兼容FireFox/IE的insertAdjacentHTML方法 ------------ //
// -------------------------------------------------------------------- //
   function insertHtml(where, el, html){
        where = where.toLowerCase();
        if(el.insertAdjacentHTML){
            switch(where){
                case "beforebegin":
                    el.insertAdjacentHTML('BeforeBegin', html);
                    return el.previousSibling;
                case "afterbegin":
                    el.insertAdjacentHTML('AfterBegin', html);
                    return el.firstChild;
                case "beforeend":
                    el.insertAdjacentHTML('BeforeEnd', html);
                    return el.lastChild;
                case "afterend":
                    el.insertAdjacentHTML('AfterEnd', html);
                    return el.nextSibling;
            }
            throw 'Illegal insertion point -> "' + where + '"';
        }
  var range = el.ownerDocument.createRange();
        var frag;
        switch(where){
             case "beforebegin":
                range.setStartBefore(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el);
                return el.previousSibling;
             case "afterbegin":
                if(el.firstChild){
                    range.setStartBefore(el.firstChild);
                    frag = range.createContextualFragment(html);
                    el.insertBefore(frag, el.firstChild);
                    return el.firstChild;
                }else{
                    el.innerHTML = html;
                    return el.firstChild;
                }
            case "beforeend":
                if(el.lastChild){
                    range.setStartAfter(el.lastChild);
                    frag = range.createContextualFragment(html);
                    el.appendChild(frag);
                    return el.lastChild;
                }else{
                    el.innerHTML = html;
                    return el.lastChild;
                }
            case "afterend":
                range.setStartAfter(el);
                frag = range.createContextualFragment(html);
                el.parentNode.insertBefore(frag, el.nextSibling);
                return el.nextSibling;
            }
            throw 'Illegal insertion point -> "' + where + '"';
    }
// -------------------------------------------------------------------- //

// -------------------------------------------------------------------- //
// ----------------------  tinyMCE 啟動函式 --------------------------- //
// -------------------------------------------------------------------- //
function init_tiny(id_textarea)
{
    tinyMCE.init({
		// General options
		mode : "exact",
        elements : id_textarea,
		theme : "advanced",
        plugins : "pagebreak,style,layer,table,save,advhr,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist,autosave",
        setup : function(ed) {
            // Display an alert onclick
            //ed.onDblClick.add(function(ed) {
            //    alert(ed.id);
            //});

            // Add a custom button
            /*
            ed.addButton('mySavebutton', {
                title : 'Save Button',
                image : './images/save.gif',
                onclick : function() {
                    //ed.selection.setContent('<strong>Hello world!</strong>');
                    //alert(obj_id+' - '+module+' - '+sql_layer+' - '+t_id+' - '+c_id+' - '+main_value+' - '+format+' - '+return_method);
                    //dataUpdate(obj_id, module, sql_layer, t_id, c_id, main_value, encodeURIComponent(ed.getContent()), format, return_method)
                } 
            }); */

        },

		// Theme options
		theme_advanced_buttons1 : "mySavebutton,forecolor,backcolor,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "bullist,numlist,|,outdent,indent,|,undo,redo,|,link,unlink,image,|,sub,sup,hr,|,charmap,iespell,media,|,code,preview,fullscreen",
		theme_advanced_buttons3 : "tablecontrols,",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example content CSS (should be your site CSS)
		//content_css : "/css/content.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js",

		// Style formats
		style_formats : [
			{title : 'Bold text', inline : 'b'},
			{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
			{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
			{title : 'Example 1', inline : 'span', classes : 'example1'},
			{title : 'Example 2', inline : 'span', classes : 'example2'},
			{title : 'Table styles'},
			{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
		],

		// Replace values for the template plugin
		template_replace_values : {
			username : "Some User",
			staffid : "991234"
		}


	});
}
