/**
 * CommonTools lib
 *
 * @author bGiraffe 24-9-2007
 */

/*prototype function*/

// string trim
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, "");
};
String.prototype.lTrim = function() {
	return this.replace(/^\s+/, "");
};
String.prototype.rTrim = function() {
	return this.replace(/\s+$/, "");
};

// XMLEncode function
String.prototype.XMLEncode = function() {
	return this.trim().replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\u2019", "&apos;").replace("\"", "&quot;");
};

// unify AJAX engine
if (window.ActiveXObject && !window.XMLHttpRequest) {
	window.XMLHttpRequest = function() {
		var msxmls = ["Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
		for(var i = 0; i < msxmls.length; i++) {
			try {
				return new ActiveXObject(msxmls[i]);
			}
			catch(e) {
			}
		}
		return null;
	};
}

/* CommonTools class */
CTs = new function() {
	// get previous element
	this.getPreviousObject = function(target) {
		var p = target;
		do p = p.previousSibling;
		while (p && p.nodeType != 1);
			return p;
	}
	
	// get next element
	this.getNextObject = function(target) {
		var n = target;
		do n = n.nextSibling;
		while (n && n.nodeType != 1);
			return n;
	}
	
	// judge element's class
	this.isClass = function(target, n) {
		var classNames = target.className.split(" ");
		for(var i in classNames)
			if(classNames[i] == n)
				return true;
		return false;
	}
	
	// get the true position of the element
	this.getElementPos = function(obj) {
		var curleft = 0;
		var curtop = 0;
		if(obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while(obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return [curleft, curtop];
	};
	
	// get inner window size
	this.getWindowInnerSize = function() {
		var myWidth = 0, myHeight = 0;
		if(typeof(window.innerWidth) == "number") {
	    // Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else {
			if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
	    // IE 6+ in 'standards compliant mode'
				myWidth = document.documentElement.clientWidth;
				myHeight = document.documentElement.clientHeight;
			} else {
				if(document.body && (document.body.clientWidth || document.body.clientHeight)) {
	    // IE 4 compatible
					myWidth = document.body.clientWidth;
					myHeight = document.body.clientHeight;
				}
			}
		}
		return [myWidth, myHeight];
	};
	
	// get scroll X Y
	this.getScrollXY = function() {
		var scrOfX = 0, scrOfY = 0;
		if(typeof(window.pageYOffset) == "number") {
	    // Non-IE
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else {
			if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
	    // IE 4 compatible
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
			} else {
				if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
	    //IE 6+ in 'standards compliant mode'
					scrOfY = document.documentElement.scrollTop;
					scrOfX = document.documentElement.scrollLeft;
				}
			}
		}
		return [scrOfX, scrOfY];
	};
	
	// get all elements by class in the page
	this.getElementsByClass = function(n) {
		var elements = document.getElementsByTagName("*");
		var targets = new Array();
		var count = 0;
		for(var i = 0; i < elements.length; i++) {
			if (this.isClass(elements[i], n)) {
				targets[count] = elements[i];
				count++;
			}
		}
		return targets;
	};
	
	// add event
	this.addEvent = function(target, eventName, fn) {
		var fnContent = this.getFunctionContent(fn);
		var orgEvent = null;
		eval("orgEvent = target." + eventName);
		if(orgEvent) {
			var orgContent = this.getFunctionContent(orgEvent);
			eval("target." + eventName + " = function(e) {if(!e) e = event;" + orgContent + fnContent + "}");
		} else {
			eval("target." + eventName + " = function(e) {if(!e) e = event;" + fnContent + "}");
		}	
	};

	// get function content
	this.getFunctionContent = function(fn) {
		var temp = fn.toString();
		return temp.substring(temp.indexOf("{"));
	}
	
	// add event for specify class elements
	this.addEventByClass = function(className, eventName, fn) {
		var elements = this.getElementsByClass(className);
		for(var i = 0; i < elements.length; i++) {
			this.addEvent(elements[i], eventName, fn);
		}
	};

	// create element
	this.createElement = function(type, attributes, styles, text) {
		var element = document.createElement(type);
		if(attributes) {
			for(var i in attributes) {
				if(i == "class") {
					element.className = attributes[i];
				} else {
					if(i == "id") {
						element.id = attributes[i];
					} else {
						element.setAttribute(i, attributes[i]);
					}
				}
			}
		}
		if(styles) {
			for(var s in styles) {
				element.style[s] = styles[s];
			}
		}
		if(text) {
			element.appendChild(document.createTextNode(text));
		}
		return element;
	};
	
	// preload image
	this.preloadImg = function(imgUrls) {
		var preloadImgObjs = new Array();
		var temp = this.createElement("div", {"id":"preloadImg"}, {"height":"0px", "width":"0px", "overflow":"hidden", "display":"none"}, "");
		for(var i = 0; i <= imgUrls.length; i++) {
			preloadImgObjs[i] = new Image();
			preloadImgObjs[i].src = imgUrls[i];
			temp.appendChild(preloadImgObjs[i]);
		}
		document.body.appendChild(temp);
	};
};

/* other functions */

// rewrite document.getElementById
function getElement(id) {
	return document.getElementById(id);
}

// simple check form content
function checkForm(formName, txtElements, mailElements) {
	var targetForm = document.getElementById(formName);
	for(var i in txtElements) {
		if(eval("targetForm." + txtElements[i] + ".value") == "") {
			alert("\u8bf7\u586b\u5199\u76f8\u5173\u8baf\u606f.");
			eval("targetForm." + txtElements[i] + ".focus()");
			return false;
		}
	}
	for(var i in mailElements) {
		if(!checkMail(eval("targetForm." + mailElements[i] + ".value"))) {
			alert("\u8bf7\u586b\u5199\u5408\u6cd5\u7535\u90ae.");
			eval("targetForm." + mailElements[i] + ".focus()");
			return false;
		}
	}
}

// validate email address
function checkMail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(filter.test(email)) {
		return true;
	} else {
		return false;
	}
}

// 向服务器获取AJAX内容
/*
function getData(target, args, output, loading) {
	var xmlHttp = new XMLHttpRequest();
	var out = document.getElementById(output);
	xmlHttp.open("POST", target, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			var input = xmlHttp.responseText;
			out.align = "left";
			out.vAlign = "top";
			if(input.substr(0,1) == "@") {
				msg(input.substr(1));
				out.innerHTML = "";
			} else
				out.innerHTML = input;
		} else {
			out.align = "center";
			out.vAlign = "middle";
			out.innerHTML = loading;
		}
	};
	xmlHttp.send(args);
}
*/