// For translation, as js is shared by all themes, path for image need to be changed
// Globals
if (!path){
	 var path ="/default/default";
}
//alert(path);
function launchWin(URL, w, h, toolbar,scrollbars,location,statusbar,menubar,resizable) {

	if (w == "" || w == null){
		w=800;
	}

	if (h == "" || h == null){
		h=600;
	}

	if(toolbar == "" || toolbar == null){
		toolbar = 0;
	}

	if(scrollbars == "" || scrollbars == null){
		scrollbars = 0;
	}

	if(location == "" || location == null){
		location = 0;
	}

	if(statusbar == "" || statusbar == null){
		statusbar = 0;
	}

	if(menubar == "" || menubar == null){
		menubar = 0;
	}

	if(resizable == "" || resizable == null){
		resizable = 0;
	}

	ww = screen.width;
	wh = screen.height;
	newtop = Math.round((wh-h)/2);
	newleft = Math.round((ww-w)/2);
	
	p = window.open(URL, "win", 'toolbar='+toolbar+',scrollbars='+scrollbars+',location='+location+',status='+statusbar+',menubar='+menubar+',resizable='+resizable+',width='+w+',height='+h+',left='+newleft+',top='+newtop);
	p.resizeTo(w,h);
	p.moveTo(newleft, newtop);
	p.focus();

	//return p;
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function update_email() {
	URL = "/Elearning_Platform/xfiles/CRM.modcgi?act=CRM&action_type=update_user_email&type=default";
	launchWin(URL,460,200);
}

function change_password() {
	URL = "/Elearning_Platform/xfiles/CRM.modcgi?act=CRM&action_type=edit_user_password";
	launchWin(URL,400,360);
}

function change_language() {
	URL = "/Elearning_Platform/xfiles/mui.modcgi?act=MUI&action_type=change_language";
	launchWin(URL,360,225);
}

function change_photo(userid_photo) {
	URL = "/Elearning_Platform/xfiles/CRM.modcgi?act=CRM&action_type=edit_user_photo&userid_photo=" + userid_photo;
	launchWin(URL,360,380,0,1,0,1,0,0);
}

function change_contact_number() {
    URL = "/Elearning_Platform/xfiles/CRM.modcgi?act=CRM&action_type=edit_contact_number";
    launchWin(URL,360,240);
}

function change_dp_group_photo(group_id) {
	URL = "/Elearning_Platform/xfiles/CRM.modcgi?act=CRM&action_type=edit_dp_group_photo&group_id=" + group_id;
	launchWin(URL,360,380,0,1,0,1,0,0);
}

function show_calculator(){
	URL="/Elearning_Platform/help/resource/calculator/index.html";
	launchWin(URL,320,500);
}

function launch_flash_lesson_builder() {
	var rnd = Math.floor(Math.random() * 1000);
	URL="/Elearning_Platform/modcgi/moelc_lesson_builder/LessonBuilder.html" + "?rnd=" + rnd;
	launchWin(URL,805,680);
}


/*    function launchLesson(theme) {
        if(theme == "mol" || theme == "hktec_sec"){
            width=800;
            height=600;
        } else if (theme == "drseries" || theme == "drseries_snp"){
            width=760;
            height=463;
        }

        URL = "/Elearning_Platform/Lesson/index.html";
        p = window.open(URL, "win", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width='+width+',height='+height);
    }

    function launchCourse(lom_id,execution_mode,width,height) {
        if (width == "" || width == null){
            width=800;
            height=600;
        }
        URL = "/Elearning_Platform/Lesson/index.html?lom_id="+lom_id+"&execution_mode="+execution_mode;
        p = window.open(URL, "win", 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width='+width+',height='+height);
    }
*/


//function to check for invalid characters in a textbox
function checktextbox(obj,objname){
   // var regex = new RegExp("[^0-9a-zA-Z_\(\)\-]");
    var mystr=obj.value;

	var i=0;
    var mycode=0;
	var result=true;

    if(mystr){
        //loop thru the string
        for(i=0;i<mystr.length;i++){
            mycode=mystr.charCodeAt(i);
            if(mycode>32 && mycode <=39){
                result=false;
            }
            if(mycode>=0 && mycode <=31){
                result= false;
            }
			if(mycode==42 || mycode==47 || mycode==60 || mycode==62 || mycode==92 || mycode==94 || mycode==96){
                result= false;
            }
        }
    }else{
        result=false;
    }

    if(result==false){
                alert("The "+objname+" should not contain symbols. Please amend.");
                obj.focus();
                return false;
    }else{
        return true;
    }

}


// convert to utf-8 
// http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_21060604.html
function convert(str){
	strArr=str.split('')
	for (i=0;i<str.length;i++)
	{
		  strArr[i]=conChar(strArr[i]);
	}
	return strArr.join('')
}

function conChar(ch){
   if (/[-_.!~*'()=?&:]/.test(ch)) return ch
//   if (ch==' ') return '+'
   return escape(ch)
}


// Wilson Chicardo - MouseOver with Caching
var imageURL = new Array("gbar-homework-icon.gif","gbar-homework-icon1.gif","gbar-task-icon.gif","gbar-task-icon1.gif","gbar-notices-icon.gif","gbar-notices-icon1.gif", "gbar-workbin-icon.gif", "gbar-workbin-icon1.gif");
var imageInfo = new Array();

for (var i=0;i<imageURL.length;i++) {
	imageInfo[i] = new Image();
	imageInfo[i].src = "/Elearning_Platform/images"+path+"/schedule/gbar/"+imageURL[i];
}

function mouseEvent(xid,imgnum) {
	document[xid].src = eval("imageInfo["+imgnum+"].src");
}

// Wilson Chicardo - MouseOver with Caching for Top Bar
var TopimageURL = new Array("home.gif","home-b.gif","help.gif","help-b.gif","logout.gif","logout-b.gif");
var TopimageInfo = new Array();

for (var i=0;i<TopimageURL.length;i++) {
	    TopimageInfo[i] = new Image();
		TopimageInfo[i].src = "/Elearning_Platform/images"+path+"/common/"+TopimageURL[i];
}

function TopmouseEvent(xid,imgnum) {
	    document[xid].src = eval("TopimageInfo["+imgnum+"].src");
}


//checking for Browser version
// free to use just leave this copyright notice intact
// eddie traversa etraversa@dhtmlnirvana.com
// http://dhtmlnirvana.com/ 	
function IsBrowser() {
  agent       = navigator.userAgent.toLowerCase();
  this.major  = parseInt(navigator.appVersion);
  this.minor  = parseFloat(navigator.appVersion);

  this.ns     = ((agent.indexOf('mozilla')   != -1) &&
                (agent.indexOf('spoofer')    == -1) &&
                (agent.indexOf('compatible') == -1) &&
                (agent.indexOf('opera')      == -1) &&
                (agent.indexOf('webtv')      == -1) &&
                (agent.indexOf('hotjava')    == -1));

  this.ns2    = (this.ns && (this.major      ==  2));
  this.ns3    = (this.ns && (this.major      ==  3));
  this.ns4    = (this.ns && (this.major      ==  4));
  this.ns6    = (this.ns && (this.major      >=  5));

  this.ie     = ((agent.indexOf("msie")      != -1) &&
                (agent.indexOf("opera")      == -1));

  this.ie3    = (this.ie && (this.major      <   4));

  this.ie4    = (this.ie && (this.major      ==  4) &&
                (agent.indexOf("msie 4")     != -1));

  this.ie5    = (this.ie && (this.major      ==  4) &&
                (agent.indexOf("msie 5.")    != -1) &&
                (agent.indexOf("msie 5.5")   == -1) &&
                (agent.indexOf("mac")        == -1));

  this.iem5   = (this.ie && (this.major      ==  4) &&  
  				(agent.indexOf("msie 5.")    != -1) && 
				(agent.indexOf("mac")        != -1));

  this.ie55   = (this.ie && (this.major      ==  4) &&  
  				(agent.indexOf("msie 5.5")   != -1));

  this.ie6    = (this.ie && (this.major      ==  4) && 
  				(agent.indexOf("msie 6.")    != -1));
  this.ie7    = (this.ie && (this.major      ==  4) &&  
  				(agent.indexOf("msie 7.0b")    != -1));
  this.nsdom  = (this.ns4 || this.ns6);
  this.ie5dom = (this.ie5 || this.iem5   || this.ie55);
  this.iedom  = (this.ie4 || this.ie5dom || this.ie6);
  this.w3dom  = (this.ns6 || this.ie6 ||this.ie7);
}

var isthisbrowser = new IsBrowser();

function checkBrowser () {
	var istrue=0;
	if (isthisbrowser.ie7) {
		alert ('ie7')
	}else if(isthisbrowser.ie6){
		alert('ie6');
	}else if(isthisbrowser.ns){
		alert('ns type');
	}
}

function isIE7(){
	if (isthisbrowser.ie7){
		return true;
	}else{
		return false;
	}
}

function netscapeKeyPress(e) {
    if (e.which == 13){
		inheritTitle();
	}
}

function microsoftKeyPress() {
    if (window.event.keyCode == 13){
		inheritTitle();
	}
}

if (navigator.appName == 'Netscape') {
    window.captureEvents(Event.KEYPRESS);
    window.onKeyPress = netscapeKeyPress;
}


function goto_ByteMe(school_ident) {
//URL, w, h, toolbar,scrollbars,location,statusbar,menubar,resizable
        URL = "http://"+school_ident+".byte-me.tv:8080";
        launchWin(URL,600,500,0,1,0,1,0,1);
}


//=======================================================
//Christophe Javascript
function winContent(url){var content=window.open(url,'content')}

