﻿var http_request = false;
var loadstatustext="<img src='images/loading.gif' /> 讀取資料中 ...";
function makeRequest(url, parameters, target) {
	http_request = false;
	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
	try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
			http_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
		}
	}
	if (!http_request) {
		alert('無法建立 XMLHTTP 需求');
		return false;
	}
	// 97/10/10 加入資料載入中效果
	document.getElementById(target).innerHTML = loadstatustext;
	// for IE Hold 住 cache 問題，加入一個時間傳值
	var extpara = parameters +'&timeStamp='+ new Date().getTime();
	// 97/09/23 debug post echo 406 error change use GET
	// http_request.open('POST', url + extpara, true);
	http_request.open('GET', url + extpara, true);
	http_request.onreadystatechange = function() {alertContents(target);};
	http_request.send(null);
}

function alertContents(target) {
  if (http_request.readyState == 4) {
	if (http_request.status == 200) {
		result = http_request.responseText;
		document.getElementById(target).innerHTML = result;            
    } else {
		document.getElementById(target).innerHTML = '伺服器回傳資料出現問題('+http_request.status+')';
		alert('伺服器回傳資料出現問題('+http_request.status+')');
    }
  }
}

function get(form, phpurl, target) {
		var getstr = "?";
		var fields = form.elements;
		for(j=0; j<fields.length; j++){
			if(fields[j].type == "button" || fields[j].type == "submit" || fields[j].type == "reset" || fields[j].name == "") continue;
			if (fields[j].type == "text" || fields[j].type == "password" || fields[j].type == "hidden" || fields[j].tagName == "TEXTAREA") {
				getstr += fields[j].name + "=" + Url.encode(fields[j].value) + "&";
			}
            if (fields[j].type == "checkbox") {
               if (fields[j].checked) {
                  getstr += fields[j].name + "=" + Url.encode(fields[j].value) + "&";
               } else {
                  getstr += fields[j].name + "=&";
               }
            }
            if (fields[j].type == "radio" && fields[j].checked) {
				getstr += fields[j].name + "=" + Url.encode(fields[j].value) + "&";
            }
           	if (fields[j].tagName == "SELECT") {
	            getstr += fields[j].name + "=" + Url.encode(fields[j].options[fields[j].selectedIndex].value) + "&";
         	}
		}
		makeRequest(phpurl, getstr, target);
}

/**
*  URL encode / decode
*  http://www.webtoolkit.info/
**/
var Url = {
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
		for (var n = 0; n < string.length; n++) {
			var c = string.charCodeAt(n);
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}
		return utftext;
	},
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < utftext.length ) {
			c = utftext.charCodeAt(i);
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

/* checkbox 需點選，某個Button才顯示或可使用
　參數１：checkbox的Id
　參數２：Button的Id 
*/
function Button_Enable(ChkForm,ChkID, BID,todo){
　if(document.getElementById(ChkID).checked){
		if (todo == 0){
　　　　document.getElementById(BID).style.display = "block";
		} else {
			/* 為避免 IE 畫面移位問題 */
			document.getElementById(ChkForm).style.display = "none";
			document.getElementById(BID).disabled = false;
			document.getElementById(ChkForm).style.display = "block";
		}
　　} else {
		if (todo == 0){
　　　　document.getElementById(BID).style.display = "none";
		} else {
			/* 為避免 IE 畫面移位問題 */
			document.getElementById(ChkForm).style.display = "none";
			document.getElementById(BID).disabled = true;
			document.getElementById(ChkForm).style.display = "block";
		}
　}
}

function Height_adjustment(chkid1,chkid2){
	if (document.getElementById(chkid1)){
		var chk1 = document.getElementById(chkid1);
		var chk2 = document.getElementById(chkid2);
		var sh1 = chk1.scrollHeight;
		var sh2 = chk2.scrollHeight;
		if(sh1 <= sh2){
			chk1.style.height = (sh2+2)+'px';
		} else {
			chk2.style.height = (sh1+2)+'px';
		}
	}
	// 讓游標停留在 Form 的 Text 上
	var j=0;
	while (document.forms[j]){
		for(var i=0; i<document.forms[j].elements.length; i++) {
		　var e  = document.forms[j].elements[i];
			var en = document.forms[j].elements[i].name;
		　if (e.type=="text" && e.disabled != true) {
		　　e.focus();
		　　break;
		　}
		}
		j++;
	}
}

// 圖片按比例縮放
function DrawImage(ImgD,w,h){
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0){
		flag=true;
		if(image.width/image.height>= w/h){
			if(image.width>w){
				ImgD.width=w;
				ImgD.height=(image.height*w)/image.width;
			} else {
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
			//ImgD.alt=image.width+"×"+image.height;
		} else {
			if(image.height>h){
				ImgD.height=h;
				ImgD.width=(image.width*h)/image.height;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
			//ImgD.alt=image.width+"×"+image.height;
		}
	}
}

function go_page(page){
	document.write('轉向中 ... 請稍後！');
	setTimeout(window.location=page,3000);
}