// JavaScript Document

// URLResolver
// AX

/// js patching
fixArray();
fixXMLHttpRequest();

// add indexOf in Array
function fixArray() {
	a = ["aa"];
	try {
		b = a.indexOf("aa");
	} catch(e) {
		Array.prototype.indexOf = function(aObj) {
			for (var i=0; i<this.length; i++) {
				if (this[i] == aObj) {
					return i;
				}
			}
			return -1;
		}
	}
}

// add XMLHttpRequest
function fixXMLHttpRequest() {
	if (!window.XMLHttpRequest) {
		window.XMLHttpRequest = function() {
			try {
				return new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					return new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					return null;
				}
			}
		}
    }
}

//URL class

function URLResolver() {};

URLResolver.getPara = function(aParaName) {
	var tValuePairA = URLResolver.getQueryString().split("&");
	for (var i=0; i<tValuePairA.length; i++) {
		var tVP = tValuePairA[i].split("=");
		if (tVP.length > 1) {
			if (tVP[0] == aParaName) {
				return tVP[1];
			}
		}
	}		
	return "";
}
	
URLResolver.getQueryString = function() {
	var tQueryA = window.parent.location.href.split("?");
	if (tQueryA.length > 1 && tQueryA[1] != "") {
		return tQueryA[1];
	}
	return "";
}
URLResolver.getFilenameBody = function(aURL) {
	var t = URLResolver.getFilename(aURL);
	var tLocD = t.lastIndexOf(".");
	if (tLocD == -1) {
		return t;
	} else if (tLocD == 0) {
		return "";
	} else {
		return t.substring(0, tLocD);
	}
}
URLResolver.getFilenameExt = function(aURL) {
	var t = URLResolver.getFilename(aURL);
	var tLocD = t.lastIndexOf(".");
	if (tLocD == -1) {
		return "";
	} else {
		return t.substring(tLocD+1);
	}
}
URLResolver.getFilename = function(aURL) {
	if (aURL == undefined) {
		var t = document.URL.split("?")[0].split("#")[0];
	} else {
		var t = aURL.split("?")[0].split("#")[0];
	}
	var tLocS = t.lastIndexOf("/");
	if (tLocS >= 0) {
		var s2 = t.substring(tLocS+1);
		var tLocBS = s2.lastIndexOf("\\");
		if (tLocBS >= 0) {
			return s2.substring(tLocBS+1);
		} else {
			return s2;
		}
	} else {
		return t
	}
}	

// XMLWrapper

function AX() {
	this.request;
	this.status = "";
	this.onLoadDone = function() {};
	this.onPostDone = function() {};
	this.request = new XMLHttpRequest();
}
AX.prototype.getXML = function() {
	if (this.status == "done") {
		return this.request.responseXML;
	}
}
AX.prototype.getText = function() {
	if (this.status == "done") {
		return this.request.responseText;
	}
}
AX.prototype.load = function(aURL) {
	this.status = "loading";
	var ts = this;
	this.request.open("GET", aURL, true);
	this.request.onreadystatechange = function() {
		if (ts.isStatusOK()) {
			ts.status = "done";
			ts.onLoadDone();
		} else {
			ts.status = "error";
		}
	}
	this.request.send(null);
}
AX.prototype.post = function(aFormID, aURL) {
	this.status = "loading";
	var ts = this;
	this.request.open("POST", aURL, true);
	this.request.onreadystatechange = function() {
		if (ts.isStatusOK()) {
			ts.status = "done";
			ts.onPostDone();
		} else {
			ts.status = "error";
		}
	}
	this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	this.request.send(getFormSerial(aFormID));
}
AX.prototype.isStatusOK = function() {
	if (this.request.readyState == 4) {
		if (this.request.status == 200 || this.request.status == 0 || this.request.status == undefined) {
			if (this.request.responseText != null) {
				return true;
			}
		}
	}
	return false;
}

function getFormSerial(aFormID) {
	var tForm = gebi(aFormID);
	var c = tForm.elements.length;
	var tResult = "";
	for (var i=0; i<c; i++) {
		var tName = tForm.elements[i].name;
		var tType = tForm.elements[i].type;
		var tValue = "";
		if (tType == "select-multiple") {
			var tOpt = tForm.elements[i].options
			var tOptString = "";
			for (var j=0; j<tOpt.length; j++) {
				if (tOpt[j].selected) {
					if (tOptString == "") {
						tOptString = String(tOpt[j].value);
					} else {
						tOptString += ","+String(tOpt[j].value);
					}
				}
			}
			tValue = encodeURIComponent(tOptString);
			if (i != 0) {
				tResult += "&";
			}
			tResult += tName + "=" + tValue;
		} else if (tType == "radio") {
			if (tForm.elements[i].checked) {
				tValue = encodeURIComponent(tForm.elements[i].value);
				if (i != 0) {
					tResult += "&";
				}
				tResult += tName + "=" + tValue;
			}
		} else {
			tValue = encodeURIComponent(tForm.elements[i].value);
			if (i != 0) {
				tResult += "&";
			}
			tResult += tName + "=" + tValue;
		}
	}
	return tResult;
}

function getRandomString() {
	return "s"+Math.floor(Math.random()*(99999999+1));
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


// global functions

function getFilenameObj() {
	var s = URLResolver.getFilenameBody();
	var tPos = s.lastIndexOf('_');
	if (tPos != -1) {
		var tLang = s.substr(tPos+1, 2);
		if (tLang != "tc" && tLang != "en" && tLang != "sc") {
			tLang = "";
		}
		var tLocation = s.substring(0, tPos)
	} else {
		var tLang = "";
		var tLocation = s;
	}
	var tLocA = tLocation.split("_");
	var tS = tLocA[0];
	if (tLocA.length > 1) {
		var tSub = tLocA[1];
	} else {
		var tSub = "";
	}
	return {location:tLocation, section:tS, subSection:tSub, lang:tLang};
}
// getElementById shortcut
function gebi(aId) {
	return document.getElementById(aId);
}
function ce(aTagName) {
	return document.createElement(aTagName);
}
function ctn(aText) {
	return document.createTextNode(aText);
}
function br() {
	return document.createElement("br");
}

function updateElement(aId, aInnerHTML) {
	var tDiv = gebi(aId);
	tDiv.innerHTML = aInnerHTML;
}
function switchLang(s) {
	var t = window.location.href;
	var sLang = "_"+s+".";
	if (gLang == "tc") {
		window.location.href =t.replace(/_tc./,sLang);
	} else if (gLang == "en") {
		window.location.href =t.replace(/_en./,sLang);
	} else if (gLang == "sc") {
		window.location.href =t.replace(/_sc./,sLang);
	}
}
function emailBuilding(aType, aID) {
	var tURL = "emailFriend_"+gLang+".asp?type="+aType+"&id="+aID;
	var tWin = window.open(tURL, "emailFriend", "scrollbars=no,resizable=yes,width=480,height=360");
	tWin.focus();
	tWin.moveTo(0,0);	
}
function popUpUnitPrint_init() {
	var e = gebi("unit");
	e.innerHTML = decodeURIComponent(URLResolver.getPara("c"));
	window.print();
	window.close();
}

function login_submit() {
	gebi("login").submit();
}
var gLang = getFilenameObj().lang;




