Array.prototype.getUniqueValues = function () {
	var hash = new Object();
	for (j=0; j<this.length; j++) 
		hash[this[j]] = true;

	var array = new Array();
	for(value in hash) 
		array.push(value)

	return array;
}

// var str = str.trim();
// alert(str.trim());
String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/g, "");
}

function getObject(id){

	if(document.getElementById){
		return document.getElementById(id);
	}
	else if(document.all){
		return document.all[id];
	}
	return null;
}

function getElement(formObj, name)
{
	for(i=0; i<formObj.elements.length; i++) {
		e = formObj.elements[i];
		if(e.name==name)
			return e;
	}
}

//remove duplciated values and split into correct lines
function normalizeInput(obj)
{
	ret = obj.value.trim().split(/\s|[^A-Za-z0-9_.@-]/).getUniqueValues();
	ret.sort();
	obj.value = ret[0];
	for(i=1; i<ret.length; i++)
		obj.value += "\n" + ret[i];
}

function clearForm(formObj)
{
	for(i=0; i<formObj.elements.length; i++) {
		e = formObj.elements[i];
		if(e.type=='hidden' || e.type=='button')
			continue;
		if(e.type=='select-one')
			e.selectedIndex = 0;
		else if(e.type=='checkbox' || e.type=='radio')
			e.checked = false
		else {
			e.value = '';
		}
	}
}

function checkAll(formObj, v) 
{
	for(i=0; i<formObj.elements.length; i++) {
		e = formObj.elements[i];
		if(e.type=='checkbox')
			e.checked = v;
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}


function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

function MM_preloadImages() { //v3.0
	var d=document; 
	if(d.images){ 
		if(!d.MM_p) 
			d.MM_p=new Array();

		var i,j = d.MM_p.length, a = MM_preloadImages.arguments; 
		for(i=0; i<a.length; i++)
			if (a[i].indexOf("#")!=0) { 
				d.MM_p[j]=new Image; 
				d.MM_p[j++].src=a[i];
			}
	}
}

function openWin(url, w, h, name)
{
	if(!w)                  w = screen.width - 50;
	else if(w>screen.width)	w = screen.width;

	if(!h)                    h = screen.height - 100;
	else if(h>screen.height)  h = screen.height;

	if(!name)
		name = 'newwin';

	win = window.open(url, name, "width=" + w + ",height=" + h + ",resizable=1,scrollbars=1");
	win.focus()
}


function showHideLayer(obj, status) 
{
	var subobj = getObject(obj);
//	var subobj = document.getElementById(obj);

	if(status=='block' || status=='none') {
		subobj.style.display = status;
		return;
	}

	if( subobj.style.display == "block") {
		subobj.style.display = "none";
	} else {
		subobj.style.display = "block";
	}
}


//http://techpatterns.com/downloads/javascript_cookies.php
function Set_Cookie(name, value, expires, path, domain, secure) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	//if the expires variable is set, make the correct 
	//expires time, the current script below will set 
	//it for x number of days, to make it for hours, 
	//delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	var cookieStr = name + "=" +escape( value ) + ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + ( ( path ) ? ";path=" + path : "" ) +  ( ( domain ) ? ";domain=" + domain : "" ) + ( ( secure ) ? ";secure" : "" );
//	alert(cookieStr);
	document.cookie = cookieStr;

}

function changeLang(l)
{
	var url = self.location.href;
	if(l>0 && l<4) {
		pos = url.indexOf("LANG=");
		if(pos>0)
			self.location.href = url.substr(0, pos) + 'LANG=' + l + url.substr(pos+6);
		else {
			pos = url.indexOf("?");
			if(pos>0)
				self.location.href = url + '&LANG=' + l;
			else
				self.location.href = url + '?LANG=' + l;
		}
	}
	return;
}


/******** general function for fetching dynamic result by HTTP *********/
function httpConnect(url, objInnerHTML, objValue, alertString)
{
	var xmlhttp = null;
	if (window.XMLHttpRequest)
		xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject)
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 
	//does not support XMLHTTP, use popup instead
	if (xmlhttp == null)
	{
		alert("請使用 IE 6.0 或更高版本 (1)");
	}
	else {
		xmlhttp.open("GET", url, true);
		xmlhttp.onreadystatechange = function(){ 

			if (xmlhttp.readyState != 4)
				return; 
			if (xmlhttp.status == 200)
			{
				if(objInnerHTML!=null)
					objInnerHTML.innerHTML = xmlhttp.responseText;
				else if(objValue!=null) 
					objValue.value = xmlhttp.responseText;
				else if(alertString!=null) 
					alert(alertString + xmlhttp.responseText);
			} 
			else
			{
				alert("請使用 IE 6.0 或更高版本 (2)");
			}
		}
		xmlhttp.send(null);
	}
}
