String.prototype.Trim = function()
{
	var regEx = /(^\s+)|(\s+$)/;
	return this.replace(regEx,'');
}
String.prototype.html = function() {
	return this.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;").replace(/\n/g, "<br>");
};
String.prototype.json = function() {
	return this.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\r/g, "\\r");
};
function $(id){
	return window.document.getElementById(id);
}

function createXMLHttpRequest()
{
	var IEXMLHttpVersion = ["MSXML2.XMLHTTP.6.0","MSXML2.XMLHTTP.5.0","MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0","MSXML2.XMLHttp","Microsoft.XMLHttp"]; 
	var xRequest = null;
  
	if(window.ActiveXObject)
	{
		// ...otherwise, use the ActiveX control for IE5.x , IE6, IE7ActiveX
	    for(var i= 0 ;i < IEXMLHttpVersion.length; i++)
		{
			try
			{
				xRequest = new ActiveXObject(IEXMLHttpVersion[i]);
				break;
			}
			catch(err){;} 
		}
	}
	else if(window.XMLHttpRequest)
	{ 
		// If IE7, Mozilla, Safari, etc: Use native object
		xRequest = new XMLHttpRequest();
	}

	return xRequest; 
}

function check_form()
{
	var boo1,bool2;
	bool1 = check_userName_filled();
	bool2 = check_password_filled();
	return bool1&&bool2;
}


function getPosition(obj, toElement)
{
	var point = new Object();
	var Left = obj.offsetLeft;
	var Top  = obj.offsetTop;
	var objParent = obj.offsetParent;
	while (objParent && objParent != toElement)
	{
		Left += objParent.offsetLeft;
		Top  += objParent.offsetTop;
		objParent = objParent.offsetParent;
	}
	point.x = Left;
	point.y = Top;
	return point;
}

function isIE()
{
	var sUserAgent = navigator.userAgent;
	var isOpera = sUserAgent.indexOf("Opera") > -1;
	var isIE = sUserAgent.indexOf("compatible")>-1 && sUserAgent.indexOf("MSIE")>-1 && !isOpera;

	if(isIE)
	{
		return true;
	}
	else{
		return false;
	}
}

function stopPropagation(event)
{
	if (isIE()){
		window.event.cancelBubble = true;
	}
	else{		
		event && event.stopPropagation();
	}
}

function displayClick(obj)
{
	obj = document.getElementById(obj);
	if (obj.style.display=="none"){
		obj.style.display="";
	}
	else{
		obj.style.display="none";
	}
}

function swap_image(imageID,newImage)
{
	document.getElementById(imageID).src = newImage;
}

//get some argument from the URL for now
function getArgumentInURL(url, keyword)
{
	keyword = keyword+"=";
	var len = keyword.length;

	var i = url.indexOf(keyword);
	var j = url.indexOf("&",eval(i+len));
	var from="";
	if(i>=0){
		from = j>i?url.substring(i+len,j):url.substring(i+len);
	}
	return from;
}

function checkFileFormat(fformat, url)
{
	var s = url.split(".");
	var ftype = s[s.length-1]
	ftype.toLowerCase();
	if(fformat.indexOf(ftype)==-1){
		return false;
	}
	return true;
}


function json()
{
	var xmlHttp = createXMLHttpRequest();
	this.debug = false;
	if (typeof this._initial == "undefined"){
		if(xmlHttp == null){
			alert("Sorry, but your browser donot support XMLHttpRequest object enough.");
			return false;
		}
		this._initial = true;
	}

	this.doFunction = function(){
		if (arguments.length==0){
			return;
		}
		var url = arguments[0];
		var handFunction = arguments[1];
		url += (url.indexOf("?") != -1) ? "&" : "?";
		url += "__=" +uniqueID();
		if (this.debug) this._debug(url,null);
		xmlHttp.open("get",url,true);
		
		xmlHttp.onreadystatechange=function()
		{
			if(4==xmlHttp.readyState)
			{
				if(200==xmlHttp.status)
				{
					handFunction(xmlHttp.responseText);
				}
			}
		}
		xmlHttp.send(null);
	},
	this._debug = function(url, data){
		var frm = document.createElement("FORM");
		frm.method = "post";
		frm.action = url;
		frm.target = "_new_" + uniqueID();
		document.body.appendChild(frm);
		if(data != null && data != "")
		{
			var arr = data.split("&");
			
			for(i=0; i<arr.length; i++)
			{
				var sItem = arr[i];
				var idx = sItem.indexOf("=");
				var oInput = document.createElement("TEXTAREA");
				oInput.rows = 5;
				oInput.cols = 80;
				oInput.name = sItem.substring(0, idx);
				oInput.value = decodeURIComponent(sItem.substr(idx + 1));
				frm.appendChild(oInput);
			}
		}
		frm.submit();
	}
}

//used to check userName field in registration page, updating profile page, login page
function checkUserName(username)
{
	var regName = /^[_a-zA-Z0-9-]+(.[_a-zA-Z0-9-]+)*$/;///^(\w|\_|\.){3,18}$/;

	if(!regName.test(username)){
		return false;
	}
	return true;
}

//used to check password field in registration page, updating profile page, login page
function checkPassword(password)
{
	var regPassword = /^(\w|\-|\?){6,16}$/;

	if(!regPassword.test(password)){
		return false;
	}
	return true;
}

function CheckLogin(form)
{
	if (!checkUserName(form.username.value)){
		alert("But we don't think your username field is filled correctly!");
		form.username.focus();
		return false;
	}

	var regPassword = /^(\w|\-|\?){1,16}$/;

	if(!regPassword.test(form.password.value)){
		alert("But we don't think your password field is filled correctyly!");
		form.password.focus();
		return false;
	}
	return true;
}

//used to check firstName field and lastName field when login and registration, updating profile.
function check_firstName_and_lastName(name)
{
	var regName = /^(\w|\.){1,50}$/;

	if(!regName.test(name)){
		return false;
	}
	return true;
}

function check_userTitle(title)
{
	var regName = /^(\w|\.){1,10}$/;

	if(!regName.test(name)){
		return false;
	}
	return true;
}

function DrawImage(ImgD,imageWidth,imageHeight) {
	var image=new Image();
	image.src=ImgD.src;
	if(image.width>0 && image.height>0 && (image.width!=imageWidth || image.height!=imageHeight)){
		if(image.width/image.height>= imageWidth/imageHeight){
			if(image.width>imageWidth){
				ImgD.width=imageWidth;
				ImgD.height=(image.height*imageWidth)/image.width;
			}else{
				ImgD.width=image.width;
				ImgD.height=image.height;
			}
		}
		else{
			if(image.height>imageHeight){
				ImgD.height=imageHeight;
				ImgD.width=(image.width*imageHeight)/image.height;
				}else{
					ImgD.width=image.width;
					ImgD.height=image.height;
				}
		}
	}
}

//check if the string is null
function checkIfNull(theString)
{
	if (theString==undefined){
		return "";
	}
	return theString;
}

//decode "<" and ">"
function decodeDigits1(theString)
{
	if (theString==undefined){
		return "";
	}

	var temp = theString.replace(/\&lt\;/g, "<").replace(/\&gt\;/g, ">").replace(/\&quot\;/g, "\"");
	return temp;
}

function getFilenameByPath(path)
{
	if (path==null||path==""){
		return "";
	}
	var temp;
	temp = path.split("/");
	var len = temp.length;
	return temp[len-1];
}

function decodeTextareaContent(content)
{
	return content.replace("<br />", "\n");
}

function encodeTextareaContent(content)
{
	return content.replace(/\n\r/g, "<br />").replace(/\r\n/g, "<br />").replace(/\r/g, "<br />").replace(/\n/g, "<br />");
}

function changeBackgroundColor(element, newColor)
{
	element.style.backgroundColor = newColor;
}

var uniqueID = (function() {
    var id = (new Date()).getTime();
    return function() { return id++; };
})();

function addEvent(obj, evname, func)
{
    if(document.all) {
        obj.attachEvent("on"+evname, func);       
    }
    else {
        obj.addEventListener(evname, func, false);
    }                
}

function window_open(link)
{
	var sFeatures="resizable=1,scrollbars=1,status=1,width=600,height=600";
	window.open(link,"THC_Help",sFeatures);
}

function html(value){
	return value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\"/g, "&quot;").replace(/\n/g, "<br>");
}

function DownloadFile(url)
{
	//var sFeatures = "width="+(screen.availWidth*0.50)+",height="+(screen.availHeight*0.60)+",scrollbars=1,resizable=1";
	if (url.indexOf("act=student") > 0)
	{
		self.location.href = url;
	}
	else{
		window.open(url,"_blank");
	}
	
}