// Window functions
function newWin(url,width,height,win_name)
{
	if (win_name == null || win_name == "") win_name = "win1";
    var win = window.open(url, win_name, "width=" + width + ",height=" + height + ",resizable=yes");
	win.focus();
}

// Change language functions
function chgLang(lang) {
	if (lang != 'en' && lang != 'tc') newLang = 'en'; else newLang = lang;

	link = document.URL;
	linkArray = link.split("/");
	remainingURL = "";

	// Check if it is called from HTML or JSP
	if (link.indexOf("home/apps") == -1) {
		if (linkArray[3] != 'en' && linkArray[3] != 'tc' && linkArray[3] != 'kr') { // old version of the site
			currentLang = "en";
			oldSite = 1;
			for (i = 3; i < linkArray.length; i++) remainingURL = remainingURL + "/" + linkArray[i];
		} else {
			currentLang = linkArray[3];
			oldSite = 0;
			for (i = 4; i < linkArray.length; i++) remainingURL = remainingURL + "/" + linkArray[i];
		}

		if (currentLang != lang) {
			if (oldSite == 1) {
				if (newLang == 'tc') newURL = "/" + newLang + remainingURL; else newURL = remainingURL;
			} else {
				newURL = "/" + newLang + remainingURL;
			}
			window.location.href = newURL;
		}
	} else {
		if (newLang == 'tc' && link.indexOf("lang=2") == -1) {
			// Change from ENG to CHI, replace "lang=1" to "lang=2", append "lang=2" if it doesn't exist
			if (link.indexOf("lang=1") != -1) {
				re = /lang=1/gi;
				newURL = link.replace(re, "lang=2");
			} else {
				if (link.indexOf("?") == -1) {
					newURL = link + "?lang=2";
				} else {
					newURL = link + "&lang=2";
				}
			}
			window.location.href = newURL;
		} else if (newLang == 'en' && link.indexOf("lang=2") != -1) {
			// Change from CHI to ENG, replace "lang=2" to "lang=1"
			re = /\?lang=2/gi;
			re1 = /\&lang=2/gi;
			newURL = link.replace(re, "?lang=1");
			newURL = newURL.replace(re1, "&lang=1");
			window.location.href = newURL;
		}
	}
}

// Form checking functions
function exists(userEntry)
{
	var aCharExists = false;
	var entry = userEntry;
	
	if(entry) {
		for(var i=0;i<entry.length;i++) {
			//space doesn't count
			if(entry.charAt(i) != "") {
				aCharExists = true;
				break;
			}
		}
	}
	return aCharExists;
}

function isAnEmail(entry)
{
	var re = /^\w[\w\.\-\_]+@[\w\.\-\_]+(\.\w+)+$/;
	
	if(!(re.test(entry)))
		return false;
	
	return true;
}

function isANumber(number) 
{
	var answer = true;

	if(!parseFloat(number)) {
		//the first digit wasn't numeric
		answer = false;
	}
	else {
		//the first digit was numeric, so check the rest
		for(var i=0; i<number.length; i++) {
			if((number.charAt(i) != "0") && 
			  (!parseFloat(number.charAt(i)))) {
				answer = false;
				break;
			}
		}
	}
	return answer;	
}

function isAlphaNumeric(str)
{
	var objRegExp = /^[aA-zZ0-9 ]*$/i; 
	return objRegExp.test(str);
}

function checkDate(strDate)
{
    if(strDate.length > 0)
    {
        var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
        var match=strDate.match(dateregex);

        if (match) {
            var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
            if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) &&
				(tmpdate.getMonth()+1)==parseInt(match[1],10))
			{ 
				return true;
			}
		}
		return false;
	}
	else
	{
		return true;	
	}
}

function allChecked(radio)
{
	for(i=0; i<radio.length; i++)
		if(radio[i].checked)
			return true;
	return false;
}

function selboxChosen(selbox)
{
	if(selbox.value == "")
		return false;
	else 
		return true;
}

function resize_screen()
{
	var targetWidth = screen.availWidth;
	var targetHeight = screen.availHeight;
	window.moveTo(0,0);
	window.resizeTo(targetWidth,targetHeight);

	resize_flash();
}

 function resize_flash() {
	// get the usable screen area of the browser
	if (document.all) { // IE
		tmpW = document.body.offsetWidth-5;
		tmpH = document.body.offsetHeight-5;
	} else { // Netscape & Firefox
		tmpW = window.innerWidth-5;
		tmpH = window.innerHeight-5;
	}

	msg = "Screen Size: " + tmpW + 'x' + tmpH;

	// calculate the new size of the flash movie
/*	resize_unit = 20;
	border_size = 20;
	wh_ratio = 780 / 560;
	scale_factor = 1;

	// calculate the new width of the flash movie
	wW = Math.floor(tmpW * scale_factor / resize_unit ) * resize_unit;

	// readjust the flash width if it is bigger than the usable screen area
	while (wW + border_size > tmpW) {
		wW = wW - resize_unit;
	}

	// calculate the new height of the flash movie based on the width
	wH = Math.floor(wW / wh_ratio);

	// check if the new height is bigger than the usable screen area
	if (wH + border_size > tmpH) {
		// readjust the flash height if it is bigger that the usable screen area
		while (wH + border_size > tmpH) {
			wH = wH - 1;
		}

		// recacluate the flash width based on the new height
		wW = Math.floor(wH * wh_ratio / resize_unit) * resize_unit;
	}
*/
	var flashobj = document.passto;
	if (!flashobj)
	{
		return;
	}

	origW = flashobj.width;
	origH = flashobj.height;
	if (tmpW/tmpH > origW/origH)
	{
		wH = tmpH;
		wW = tmpH * origW/origH;
	}
	else
	{
		wW = tmpW;
		wH = tmpW * origH/origW;
	}

	// resize the flash
	document.passto.width = wW;
	document.passto.height = wH;

	// output the debug info to the status bar
	window.status = msg + " / " + "Flash Size: " + document.passto.width + 'x' + document.passto.height;
 }

function getLang()
{
	var langList = [];
	langList["en"] = 1;
	langList["tc"] = 2;
	langList["kr"] = 3;

	var link = document.URL;
	var linkArray = link.split("/");
	var currentLang = 1;

	// Check if it is called from HTML or JSP
	if (link.indexOf("home/apps") == -1)
	{
		if (langList[linkArray[3]])
		{
			currentLang = langList[linkArray[3]];
		}
	}
	else
	{
		var bLangPos = link.indexOf("lang=");
		if (bLangPos != -1)
		{
			bLangPos += 5;
			var eLangPos = link.indexOf("&", bLangPos);
			if (eLangPos != -1)
			{
				currentLang = link.substring(bLangPos, eLangPos);
			}
			else
			{
				currentLang = link.substring(bLangPos);
			}
		}
	}
	return currentLang;
}

function openWindow(url, name, features)
{
	var win = window.open(url, name, features);
	if (!win)
	{
		alert("A Popup Window from Planetii is blocked! Please unblock the window!");
	}
	else
	{
		win.focus();
	}
	return win;
}

function loadScript(url)
{
	var http = getXmlhttpObject();

	http.open('GET', url, false);
	if (http.readyState == 0)
	{
		alert("my god b!:"+http.open);
		http.open('GET', url, false);
		alert("my god a!:"+http.open);
	}

	try {
		http.send(null);
	} catch (e) {
		return null;
	}

	if (window.eval)
	{
		window.eval(http.responseText);
	}
	else
	{
		eval(http.responseText);
	}
}
/*
function loadScript()
{
	if (typeof loadedobjects == "undefined")
	{
		loadedobjects = "";
	}
	if (!document.getElementById)
		return;
	for (i=0; i<arguments.length; i++)
	{
		var file=arguments[i];
		var fileref="";
		if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
			if (file.indexOf(".js")!=-1){ //If object is a js file
				fileref=document.createElement('script');
				fileref.setAttribute("type","text/javascript");
				fileref.setAttribute("src", file);
			}
			else if (file.indexOf(".css")!=-1){ //If object is a css file
				fileref=document.createElement("link");
				fileref.setAttribute("rel", "stylesheet");
				fileref.setAttribute("type", "text/css");
				fileref.setAttribute("href", file);
			}
		}
		if (fileref!=""){
			document.getElementsByTagName("head").item(0).appendChild(fileref);
			loadedobjects+=file+" "; //Remember this object as being already added to page
		}
	}
}*/

function getXmlhttpObject(){
	if (typeof DJ_XMLHTTP_PROGIDS == 'undefined')
	{
		DJ_XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
	}

    var http = null;
	var last_e = null;
	try{ http = new XMLHttpRequest(); }catch(e){}
    if(!http){
		for(var i=0; i<DJ_XMLHTTP_PROGIDS.length; ++i){
			var progid = DJ_XMLHTTP_PROGIDS[i];
			try{
				http = new ActiveXObject(progid);
			}catch(e){
				last_e = e;
			}

			if(http){
				DJ_XMLHTTP_PROGIDS = [progid];  // so faster next time
				break;
			}
		}

		/*if(http && !http.toString) {
			http.toString = function() { "[object XMLHttpRequest]"; }
		}*/
	}

	return http;
}

