function CommonUtils() {
	
	this.readCookie = readCookie;
	this.getLanguageCode = getLanguageCode;
	this.setLanguageCode =  setLanguageCode;
	this.getQueryString = getQueryString;
	this.includeCSS = includeCSS;
	this.includeJavaScript = includeJavaScript;
	this.getBrowserUrl = getBrowserUrl;
	this.getCurrentPage = getCurrentPage;
	this.setSrc = setSrc;
	this.generateImageButton = generateImageButton;
	this.generateImageButtonWithFunction = generateImageButtonWithFunction;
	
	
	
}
 CommonUtils = new CommonUtils(); 
//System Specific Utils ####################
function getLanguageCode () {
	
	var strLang = readCookie("COOKIE_LANG")

	if (strLang != null) {
	    if ((strLang != "") && (strLang == "en" || strLang == "zh")) {
			return strLang;
		} else {
		    setLanguageCode("en");
			return "en";
		}
	} else {
		setLanguageCode("en");
		return "en";
	}
	//return ("test")

}

function checkLanguage(strQuery) {
    try {
        if (strQuery != null) {
            if ((strQuery == "zh") || (strQuery == "en")) {
                setLanguageCode(strQuery);
            } else {
                strQuery = getLanguageCode();
            }

        } else {
            strQuery = getLanguageCode();
        }
    } catch (err) {
        strQuery = getLanguageCode();
    }

    return strQuery;

}

function setLanguageCode(strLang) {
    createCookie("COOKIE_LANG", strLang, 30);
}

//##########################################


function getQueryString() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for(var i = 0; i < pairs.length; i++) {
	var pos = pairs[i].indexOf('=');
	if (pos == -1) continue;
	var argname = pairs[i].substring(0,pos);
	var value = pairs[i].substring(pos+1);
	args[argname] = unescape(value);
	}
	return args;
}
		
function setSrc(name, src) {
	document[name].src = src;

}

function generateImageButton(name, image, image_over, link) {
		
		strImageFunction = "CommonUtils.setSrc('" + name + "', '" + image + "')"; 
		strImageOverFunction = "CommonUtils.setSrc('" + name + "', '" + image_over + "')"; 
	
		document.write ('<a HREF="' + link + '" onmouseover="' + strImageOverFunction + '"  onmouseout="' + strImageFunction + '" >');
		document.write ('			<IMG SRC="' + image + '" NAME="' + name + '" BORDER="0"/>');
		document.write ('		</a>');
	}
	
function generateImageButtonWithFunction(name, image, image_over, link, js_function) {
		
		strImageFunction = "CommonUtils.setSrc('" + name + "', '" + image + "')"; 
		strImageOverFunction = "CommonUtils.setSrc('" + name + "', '" + image_over + "')"; 
	
		document.write ('<a HREF="' + link + '" onmouseover="' + strImageOverFunction + '"  onmouseout="' + strImageFunction + ' onClick="' + js_function + '" >');
		document.write ('			<IMG SRC="' + image + '" NAME="' + name + '" BORDER="0"/>');
		document.write ('		</a>');
	}



function includeCSS(cssFile) {
	
    document.write('<link rel="stylesheet" type="text/css" href="' + cssFile + '"/>');
}

function includeJavaScript(jsFile) {
    document.write('<script type="text/javascript" src="' + jsFile + '"></script>');
}

function getBrowserUrl() {
 	return window.location.href;

}

function getCurrentPage() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	return sPage;

}


function readCookie(name) {
             var nameEQ = name + "=";
             var ca = document.cookie.split(';');
             for (var i = 0; i < ca.length; i++) {
                 var c = ca[i];
                 while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
             }
             return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}



