// open window functions
var objWin;

function newWindow(url,winType){
	win_name = "ec_popup";

	win_size = "resizable,scrollbars,status=0,top=140,left=140,width=400,height=400";

	if(winType==0){
		win_name = "ec_popup0";
		win_size = "resizable,scrollbars,status=0,top=40,left=40,width=400,height=550";
	}

	if(winType==1){
		win_name = "ec_popup1";
		win_size = "resizable,scrollbars,status=0,top=40,left=40,width=670,height=500";
	}

	if(winType==2){
		win_name = "ec_popup2";
		win_size = "resizable,scrollbars,status,top=40,left=40,width=500,height=400";
	}

	if(winType==3){
		win_name = "ec_popup3";
		win_size = "toolbar,location,resizable,scrollbars,status,top=40,left=40,width=600,height=400";
	}

	if(winType==4){
		win_name = "ec_popup4";
		win_size = "menubar,resizable,scrollbars,status,top=40,left=40,width=600,height=400";
	}

	if(winType==5){
		win_name = "ec_popup5";
		win_size = "menubar,toolbar,resizable,scrollbars,status,top=40,left=40,width=600,height=400";

	}
	if(winType==6){
		//for chatroom
		win_name = "ec_popup6";
		win_size = "status=0,top=40,left=40,width=640,height=352";
	}
	if(winType==7){
		//for email
		win_name = "ec_popup7";
		win_size = "resizable,scrollbars,status=0,top=40,left=40,width=670,height=440";
	}
	if(winType==8){
		//for login info
		win_name = "ec_popup9";
		win_size = "resizable=1,scrollbars,status=0,top=40,left=40,width=440,height=400";
	}
	if(winType==9){
		//detach page
		win_name = "ec_popup9";
		win_size = "menubar, resizable, scrollbars, status=0,top=40,left=40,width=800,height=600";
	}
	if(winType==10){
		//for customer support page
		win_name = "ec_popup10";
		win_size = "resizable,scrollbars,status,top=40,left=40,width=800,height=600";
	}
	if(winType==11){
		//for equation editor page
		win_name = "ec_popup11";
		win_size = "status,top=40,left=150,width=720,height=520";
	}
	if(winType==12){
		//for adding attachment
		win_name = "ec_popup12";
		win_size = "top=120,left=200,width=400,height=300";
	}
	if(winType==13){
		//for ELP profile records
		win_name = "ec_popup13";
		win_size = "resizable,scrollbars,status=0,top=40,left=40,width=670,height=540";
	}
	if(winType==17){
		//for invisible update if cannot find free frameset to use
		var intWidth = screen.width;
		var intHeight = screen.height;
		win_name = "ec_popup17";
		win_size = "top="+(intWidth+30)+",left="+intHeight+",width=10,height=10";
	}
	if(winType==18){
		//for HTML editor popup : marking code
		win_name = "ec_popup18";
		win_size = "resizable,scrollbars,status=0,top=200,left=150,width=400,height=300";
	}
	if(winType==19){
		//for HTML editor popup : grading comment
		win_name = "ec_popup19";
		win_size = "resizable,scrollbars,status=0,top=50,left=150,width=400,height=450";
	}
	if(winType==20){
		//for HTML editor popup : insert sticker
		win_name = "ec_popup20";
		win_size = "resizable,scrollbars,status=0,top=50,left=50,width=720,height=350";
	}
	if(winType==21){
		//for HTML editor page
		win_name = "ec_popup21";
		win_size = "status,top=30,left=130,width=720,height=520";
	}
	if(winType==22){
		win_name = "ec_popup22";
		win_size = "resizable,scrollbars,status=0,top=30,left=30,width=680,height=500";
	}
	if(winType==23){
		// for printing
		win_name = "ec_popup23";
		win_size = "menubar,resizable,scrollbars,top=40,left=40,width=640,height=480";
	}

	if(winType==90){
		win_name = "ec_popup90";
		win_size = "resizable,scrollbars=no,status,top=40,left=40,width=780,height=550";
	}
	
	if(winType==91){
		// for eClass Community
		win_name = "ec_popup91";
		win_size = "toolbar,resizable,scrollbars,top=40,left=40,width=460,height=340";
	}

	if(winType==91){
		// for eClass Community
		win_name = "ec_popup91";
		win_size = "toolbar,resizable,scrollbars,top=40,left=40,width=460,height=340";
	}

	if(winType==92){
		// for eClass Community
		win_name = "ec_popup92";
		win_size = "resizable,scrollbars,top=40,left=40,width=417,height=200";
	}


	if (newWindow.arguments.length>3) {
	    win_name = "GivenSizeWin";
		var is_scroll = 0;
		var is_resizable = 0;
		if (newWindow.arguments.length>4) {
		    is_scroll = newWindow.arguments[4];
		}
		if (newWindow.arguments.length>5) {
		    is_resizable = (newWindow.arguments[5]==1) ? "1" : "0";
		}
		win_size = "resizable="+is_resizable+",scrollbars="+is_scroll+",status=0,top=40,left=40,width="+newWindow.arguments[2]+",height="+newWindow.arguments[3];
	}
	if (navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion >="4")

	// Yuen modified the following code on 18th Jul
		if (objWin)
			objWin.close();

	objWin = window.open (url, win_name, win_size);
	if (navigator.appName=="Netscape" && navigator.appVersion >= "3")
		objWin.focus();

	return;
}



// trim functions

function LTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}

	return s;
}

function RTrim(str){
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;       // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}

	return s;
}

// Trim a string
function Trim(myText){
	var tempText = myText

	// cut space in the beginning
	var firstChar = tempText.charAt(0);
	while (firstChar == " "){
		tempText = tempText.substr(1, tempText.length-1);
		firstChar = tempText.charAt(0);
	}

	// cut "\n" in the beginning
	var firstTwo = parseInt(tempText.charCodeAt(0));
	while (firstTwo == 13){
		tempText = tempText.substr(2, tempText.length-1);
		firstTwo = parseInt(tempText.charCodeAt(0, 2));
	}

	// cut space in the end
	var lastChar = tempText.charAt(tempText.length-1);
	while (lastChar == " "){
		tempText = tempText.substr(0, tempText.length-1);
		lastChar = tempText.charAt(tempText.length-1);
	}

	return tempText;
}


function abortAction() {
    alert(globalAlertMsg15);
	return false;
}

// form validation functions

function check_text(f, msg){
	if(Trim(f.value)==""){
		alert(msg);
		f.value="";
		f.focus();
		return false;
	}else{
		return true;
	}
}

function check_text_arr(obj, element_name, msg){
	var len = obj.elements.length;
	for (var i=0; i<len; i++)
	{
		if (obj.elements[i].name==element_name && Trim(obj.elements[i].value)=="")
		{
			alert(msg);
			obj.elements[i].value = "";
			obj.elements[i].focus();
			return false;
		}
	}

	return true;
}

function check_number(f, msg){
	if(parseInt(f.value)!=f.value){
		alert(msg);
		f.focus();
		return false;
	}else{
		return true;
	}
}

function check_select(f, msg, g){
	if(Trim(f.options[f.selectedIndex].value)==g)
	{
		alert(msg);
		f.focus();
		return false;
	}else{
		return true;
	}
}

function setChecked(val, obj, element_name){
	len=obj.elements.length;
	var i=0;

	for( i=0 ; i<len ; i++) {
		if (obj.elements[i].name==element_name)
		obj.elements[i].checked=val;
	}
}

function setObjValue(val, obj, element_name){
	len=obj.elements.length;
	var i=0;

	for( i=0 ; i<len ; i++) {
		if (obj.elements[i].name==element_name)
		obj.elements[i].value=val;
	}
}

function countChecked(obj, element_name){
	len=obj.elements.length;
	var i=0;
	var j=0;
	for( i=0 ; i<len ; i++) {
		if (obj.elements[i].name==element_name && obj.elements[i].checked)
		j=j+1;
	}
	return j;
}

function countSelected(obj, element_name){
	var j=0;

	objSel = obj.elements[element_name];
	for (k=0; k<objSel.length; k++)
	{
		if (objSel.options[k].selected)
		{
			j++;
		}
	}

	return j;
}

function returnChecked(obj, element_name){
	len=obj.elements.length;
	var i=0;
	for( i=0 ; i<len ; i++) {
		if (obj.elements[i].name==element_name && obj.elements[i].checked)
		return obj.elements[i].value;
	}
	return null;
}

function check_int(f,d,msg){

	if(isNaN(parseInt(f.value)) || parseInt(f.value)==0){

		alert(msg);

		f.value=d;

		f.focus();

		return false;

	}else{

		f.value=parseInt(f.value);

		return true;

	}

}

function charVerify(x,y){

	for (var i=0; i<x.length; i++){

		var ch;

		ch= x.substring(i,i+1);

		if ( y.indexOf(ch)<0 ){

			alert(globalAlertMsg1+"\n\n"+y)

			return false;

		}

	}

	return true;

}

function strReplaceAll(s, t, f){
	var tmp1=s;
	var tmp2="";

	while (tmp1.indexOf(t)!=-1)
	{
		tmp2=tmp2+tmp1.substring(0,tmp1.indexOf(t))+f;
		tmp1=tmp1.substring(tmp1.indexOf(t)+t.length);
	}
	tmp2=tmp2+tmp1;

	return tmp2;
}


function recurReplace(exp, reby, txt) {
    while (txt.search(exp)!=-1) {
        txt = txt.replace(exp, reby);
    }
        return txt;
}


function check_date(x,msg){
	var err=0
	a = x.value;
	if (a.length != 10) err=1
	b = parseInt(a.substring(0, 4),10)// year
	c = a.substring(4, 5)// '-'
	d = parseInt(a.substring(5, 7),10)// month
	e = a.substring(7, 8)// '-'
	f = parseInt(a.substring(8, 10),10)// day

	//basic error checking
	if (b<0 || b>3000 || isNaN(b)) err = 1
	if (c != '-') err = 1
	if (d<1 || d>12 || isNaN(d)) err = 1
	if (e != '-') err = 1
	if (f<1 || f>31 || isNaN(f)) err = 1
	//advanced error checking
	// months with 30 days
	if (d==4 || d==6 || d==9 || d==11){
		if (f==31) err=1
	}

	// february, leap year
	if (d==2){ // feb
		var g=parseInt(b/4)
		if (isNaN(g)) {
			err=1
		}

		if (f>29) err=1
		if (f==29 && ((b/4)!=parseInt(b/4))) err=1
	}

	if (err==1){
		alert(msg);
		x.focus();
		return false;
	}else{
		return true;
	}
}

function check_time(tObj, msg) {
	//format: t = 00:00
	var t = tObj.value;
	var tArr = t.split(":");
	var hour = parseInt(tArr[0]);
	var minute = parseInt(tArr[1]);

	if (hour<0 || hour>23 || minute<0 || minute>59) {
		alert(msg);
		tObj.focus();
	    return false;
	}
	return true;
}

function compareTime(date_s, hr_s, min_s, date_e, hr_e, min_e) {
	var time1 = toTimeStr(date_s, hr_s, min_s);
	var time2 = toTimeStr(date_e, hr_e, min_e);
	if (time1>time2) {
	    return false;
	}else {
		return true;
	}
}

function toTimeStr(date_o, hr_o, min_o) {
    var hr_v = parseInt(hr_o.options[hr_o.selectedIndex].value);
	if (hr_v<10) {
	    hr_v = "0"+hr_v;
	}
	min_v = parseInt(min_o.options[min_o.selectedIndex].value);
	if (min_v<10) {
	    min_v = "0"+min_v;
	}
	var time1 = date_o.value + hr_v + min_v;
	return time1;
}

// page functions

function gopage(page, obj){

	obj.pageNo.value=page;

	obj.submit();

}

function sortPage(a, b, obj){

	obj.order.value=a;

	obj.field.value=b;

	obj.pageNo.value=1;

	obj.submit();

}



// menu bar functions

function checkNew(page){

	self.location.href=page;

}

function checkPost(obj,url){
	var tmp = obj.action;
	obj.action=url;
	obj.submit();
	obj.action = tmp;
}

function checkPostTarget(obj,url){
	obj.action=url;
	var tmp = obj.target;
	obj.target="ec_pop_up";
	obj.submit();
	obj.target = tmp;
}

function checkEdit(obj,element,page){
	if(countChecked(obj,element)==0)
		alert(globalAlertMsg2);
	else if(countChecked(obj,element)>1)
		alert(globalAlertMsg3);
	else{
		obj.action=page;
		obj.submit();
	}
}

function checkPick(obj,element,page,type){
	if(countChecked(obj,element)==0)
	alert(globalAlertMsg2)
	else{
		if (page == 'public.php') {
			switch (type) {
			    case 1 :
			        msg = globalAlertMsg8; break;
				case 2 :
			        msg = globalAlertMsg12; break;
				case 3 :
			        msg = globalAlertMsg13; break;
				case 4 :
			        msg = globalAlertMsg14; break;
				case 5 :
			        msg = globalAlertMsg23; break;
			    default :
			        msg = "Are you sure?" ; break;
			}
			if(confirm(msg)){
				obj.action=page;
				obj.submit();
			}
		} else {
			obj.action=page;
			obj.submit();
		}
	}
}

function checkRemove(obj,element,page){

	if(countChecked(obj,element)==0)
	alert(globalAlertMsg2)
	else{
		msg = globalAlertMsg4;
		if( page == 'suspend.php' ) msg = globalAlertMsg5;
		if( page == 'activate.php' ) msg = globalAlertMsg6;
		if( page == 'autoMark.php' || page == 'mark_auto.php' ) msg = globalAlertMsg10;
		if( page == 'move.php' || page == 'copy.php') msg = globalAlertMsg6;
		if( page == 'download.php' ) msg = globalAlertMsg7;
		if( page == 'handinRedo.php' ) msg = globalAlertMsg24;
		if( page == 'handinCorrection.php' ) msg = globalAlertMsg25;
		if( page == 'handinCorrectionAccept.php' ) msg = globalAlertMsg26;
		if(confirm(msg)){
			obj.action=page;
			obj.submit();
		}
	}
}


function clickMark(obj, page){
	if(confirm(globalAlertMsg11)){
		obj.action=page;
		obj.submit();
	}
}

function checkRegEx(str,msg){
	var RE1 = new RegExp("[^0-9a-zA-Z]");
	if (RE1.test(str)){
		alert(msg);
		return false;
	}else{
		return true;
	}
}

function validateEmail(obj,msg){
	var re = /^[\w_.-]+@[\w-]+(\.\w+)+$/;
	if (re.test(obj.value)) {
		return true;
	}else{
		alert(msg);
		obj.focus();
		return false;
	}
}

function validateFilename(str){
	var re = /[:<>%\#\\\/\|\*\?"]/;
	if (re.test(str))
	{
		alert(globalAlertMsg9);
		return false;
	} else
	{
		return true;
	}
}



// eClass Core Update

// For Accessment

function newImageWindow(url,winWidth,winHeight){

	if (winWidth>600) winWidth=600;

	if (winHeight>400) winHeight=400;



	win_name = "ec_popupImage";

	win_size = "resizable,scrollbars,status,top=80,left=80,width=" + (winWidth+40) + ",height=" + (winHeight+40);

	if (navigator.appName=="Microsoft Internet Explorer" && navigator.appVersion >="4")

	if (objWin!=null)

		objWin.close();

	objWin = window.open (url, win_name, win_size);

	if (navigator.appName=="Netscape" && navigator.appVersion >= "3")

		objWin.focus();

}

// generate title with gradient effect
function gradientTitle(myText) {
	myChar = "";
	newString = "";

	if (myText.indexOf('&#')>-1) {
	    newString = "<font class=gtitle3>"+myText+"</font>";
	} else {
	    for (i=0; i<myText.length; i++){
			cssNum = i%14 + 1;
			myChar = myText.charAt(i);

			if (cssNum > 8)
				switch(cssNum) {
					case 9:		cssNum=7; break;
					case 10:	cssNum=6; break;
					case 11:	cssNum=5; break;
					case 12:	cssNum=4; break;
					case 13:	cssNum=3; break;
					case 14:	cssNum=2; break;
				}

			newString += "<font class=gtitle" + cssNum + ">" + myChar + "</font>";
		}
	}
	document.write(newString);
}

// generate arrow (>>>) with gradient effect
function gradientArrow(myText) {
	myChar = "";
	newString = "";

	for (i=0; i<myText.length; i++){
		cssNum = i%6 + 1;
		myChar = myText.charAt(i);

		if (cssNum > 4)
			switch(cssNum) {
				case 5:		cssNum=3; break;
				case 6:		cssNum=2; break;
			}

		newString += "<font class=garrow" + cssNum + ">" + myChar + "</font>";
	}
	document.write(newString);
}

function checkOption(obj){
	for(i=0; i<obj.length; i++){
		if(obj.options[i].value== '')
		obj.options[i] = null;
	}
}

function check(from,to){
	checkOption(from);
	checkOption(to);
	i = from.selectedIndex;
	while(i!=-1){
		to.options[to.length] = new Option(from.options[i].text, from.options[i].value, false, false);
		from.options[i] = null;
		i = from.selectedIndex;
	}
}


function charEscape(xStr) {
	var fStr = "";
	for (var j=0; j<xStr.length; j++)
	{
		fStr += (xStr.charCodeAt(j)>255) ? xStr.charAt(j) : escape(xStr.charAt(j));
	}

	return fStr;
}


function editEqt(obj, root) {
	var aWin = newWindow("",11);
	var frmTargetOrg = obj.form.target;
	var frmActionOrg = obj.form.action;
	var frmMethodOrg = obj.form.method;

	obj.form.fieldvalue.value = obj.value;
	obj.form.fieldname.value = obj.name;
	obj.form.target = "ec_popup11";
	obj.form.action = root + "/src/tool/misc/eqt_editor/index.php";
	obj.form.submit();

	obj.form.target = frmTargetOrg;
	obj.form.action = frmActionOrg;
	obj.form.method = frmMethodOrg;

    return;
}

function editHTML(obj, root) {
	var aWin = newWindow("",21);
	var frmTargetOrg = obj.form.target;
	var frmActionOrg = obj.form.action;
	var frmMethodOrg = obj.form.method;

	obj.form.fieldvalue.value = obj.value;
	obj.form.fieldname.value = obj.name;
	obj.form.target = "ec_popup21";
	obj.form.action = root + "/src/tool/misc/html_editor/index.php";
	obj.form.submit();

	obj.form.target = frmTargetOrg;
	obj.form.action = frmActionOrg;
	obj.form.method = frmMethodOrg;

    return;
}


function postInstantForm(obj, url, method, winType, winName) {
	winType = (typeof(winType)=="number") ? winType.toString() : winType;
	var aWin = (winType.indexOf(",")!=-1) ? eval("newWindow('',"+winType+")") : newWindow("",winType);
	var frmTargetOrg = obj.target;
	var frmActionOrg = obj.action;
	var frmMethodOrg = obj.method;

	obj.target = winName;
	if (method!="")
	{
		obj.method = method;
	}
	obj.action = url;
	obj.submit();

	obj.target = frmTargetOrg;
	obj.action = frmActionOrg;
	obj.method = frmMethodOrg;

    return;
}


function invisibleUpdate(obj, url) {
    obj.top.eClassHidden.location.replace(url);
	return;
}


function array_indexOf(arr,val) {
	for (var i=0; i<arr.length; i++)
	{
		if (arr[i]==val)
		{
			return i;
		}
	}
	return -1;
}

function windowMax(){
	top.window.moveTo(0,0);
	if (document.all)
	{
		top.window.resizeTo(screen.availWidth,screen.availHeight);
	} else if (document.layers||document.getElementById)
	{
		if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth)
		{
			top.window.outerHeight = screen.availHeight;
			top.window.outerWidth = screen.availWidth;
		}
	}
}

function displayTable(tableID, myStyle){
	//block, none, inline
	var currentStyle = document.all[tableID].style.display;
	var newStyle = "none";
	if (typeof(myStyle)!="undefined")
	{
		newStyle = myStyle;
	} else
	{
		newStyle = (currentStyle=="none") ? "block" : "none";
	}

	document.all[tableID].style.display = newStyle;

	return;
}

function setDivVisible(state, lay)
{
	var DivRef = document.getElementById(lay);
	var IfrRef = document.getElementById('lyrShim');
	if(state)
	{
		DivRef.style.display = 'block';
		IfrRef.style.width = DivRef.offsetWidth;
		IfrRef.style.height = DivRef.offsetHeight;
		IfrRef.style.top = DivRef.style.top;
		IfrRef.style.left = DivRef.style.left;
		IfrRef.style.zIndex = DivRef.style.zIndex - 1;
		IfrRef.style.display = 'block';
	} else
	{
		DivRef.style.display = 'none';
		IfrRef.style.display = 'none';
	}
}
