function ShowHideTree(command, url, menutree) { 
	if (url!= undefined) {
		//Always refresh page_content panel with the body logo
		ajaxGetpage('page_content',url+'home/logo',false);
	}
	switch (command) { 
	case 1:
		if ( ajaxGetpage('school_tree',url+'menu/menu_ict',false) != false ) {
			shrinkMenuDiv("general_tree"); 
			expandMenuDiv('school_tree');
		}
		break;
	case 2:
		if ( ajaxGetpage('school_tree',url+'menu/mapping',false) != false ) {
			shrinkMenuDiv("general_tree"); 
			expandMenuDiv('school_tree');
		}
		break;
	case 3:
		if ( ajaxGetpage('school_tree',url+'menu/help',false) != false ) {
			shrinkMenuDiv("general_tree"); 
			expandMenuDiv('school_tree');
		}
		break;
	case 4:
		if ( ajaxGetpage('school_tree',url+'menu/menu_admin',false) != false ) {
			shrinkMenuDiv("general_tree"); 
			expandMenuDiv('school_tree');
		}
		break;	
	case 5:
		if ( ajaxGetpage('school_tree',url+'menu/menu_goal',false) != false ) {
			shrinkMenuDiv("general_tree"); 
			expandMenuDiv('school_tree');
		}
		break;
	case 9:
		if (menutree != undefined) {
			url += menutree;
		}
		if ( ajaxGetpage('school_tree',url,false) != false ) {
			shrinkMenuDiv("general_tree"); 
			expandMenuDiv('school_tree');
		}
		break;
	default:
		shrinkMenuDiv('school_tree');
		expandMenuDiv('general_tree');  
	}
}

function doMenuTree() {
	var dept_div = getDivobj('dept_tree');
	var grade_div = getDivobj('grade_tree');
	//mydiv.style.height = 10;
	if ( showDept==true ) {
		showDept=false;
		shrinkMenuDiv('dept_tree');
		expandMenuDiv('grade_tree');
		document.menus.switch_menu.value = deptButton;
	}
	else {
		showDept=true;
		shrinkMenuDiv('grade_tree');
		expandMenuDiv('dept_tree');
		document.menus.switch_menu.value = gradeButton;
	}
	return false;
}

function LoadUnitPage(url,divid) {
	var retcode = ajaxGetpage(divid, url, false);
	/* if ( retcode ) {
		setupCalendar();
	} */
     return false;
}

function LoadUnitEditPage(url,divid,courseid) {
	var retcode = ajaxGetpage(divid, url, false);
	if ( retcode ) {
		/*
		Calendar.setup({
	 	   inputField : "startdate", // ID of the input field
	 	   ifFormat : "%Y-%m-%d", // the date format
	 	   //ifFormat : "%m-%d-%Y",
	 	   button : "startdatebutton" // ID of the button
	 	   });
	     Calendar.setup({
	 	   inputField : "enddate", // ID of the input field
	 	   ifFormat : "%Y-%m-%d", // the date format
	 	  //ifFormat : "%m-%d-%Y",
	 	   button : "enddatebutton" // ID of the button
	 	   });
	 	   */
	     //refresh_unitnode(courseid);
		refresh_menutree();
	}
     return false;
}

function updateUnit(formname, url, courseid) { 
	if ( ajaxPostform('page_content', url, formname, false) ){
		//refresh_unitnode(courseid);
		refresh_menutree();
	}
}

function undoUnitEdit(url, courseid, msg) {
	if ( confirm(msg) ) {
		if ( ajaxPostform('page_content', url, '', false) ){
			//refresh_unitnode(courseid);
			refresh_menutree();
		}
	}
}

function confirmUnit(url, courseid, msg) {
	if ( confirm(msg) ) {
		if ( ajaxPostform('page_content', url, '', false) ){
			//refresh_unitnode(courseid);
			refresh_menutree();
		}
	}
}
//for refresh menu tree
function confirmUnits(url, courseids, msg) {
	if ( confirm(msg) ) { 
		if ( ajaxPostform('page_content', url, '', false) ) { 
			//refresh_menutree(courseids);
			refresh_menutree(); 
		}
	}
}
/*
//for refresh menu tree
function refresh_menutree() {
	refresh_unitnode();
	// now menutree will refresh the whole tree
	var str = courseids.split(" "); 
	//alert(courseids);
	var dup = ""; 
	if (str != undefined && str.length>0) { 
		for (var i = 0; i < str.length; i++) {  
			//var pos = dup.indexOf(str[i]+'|'); 
			//alert(str[i]);
			//if (pos < 0) {
				
				//dup += str[i]+'|'; 
				refresh_unitnode(str[i]);
			//}
		} 
	}
}
*/
function removeUnit(url, courseid, msg) {
	if ( confirm(msg) ) {
		if ( ajaxPostform('page_content', url, '', false) ){
			//refresh_unitnode(courseid);
			refresh_menutree();
		}
	}
}

function refresh_menutree() {
	// get name list of menutrees from globalvars
	var trees = gMenutreenames;
	if ( typeof trees != 'undefined' ) {
		$(trees).jstree('refresh');
	}
	totalunit_refresh();
	return false;
}
// TODO: (issue: 211) delete after cleanup
function refresh_unitnode(courseid){
	return refresh_menutree();
}
/* No loger used
function refresh_unitnode1(thistree, prefix, courseid) {
	var nodeid=prefix + 'courseunit' + courseid;
	var node = thistree.getNode(nodeid);
	try {
	if ( typeof node == 'undefined' || !node.isExpanded())
		return;
	}
	catch(e){
		return;
	}
	var nodetext = node.getText();
	var nodeimgsrc = node.getImageSrc();
	var parentnodeid = node.getParentId();
	var parentnode = thistree.getNode(parentnodeid);
	
	thistree.removeNode(nodeid);
	parentnode.addChildNode(nodeid, nodetext, nodeimgsrc);
	var newnode = thistree.getNode(nodeid);
	
	//alert(courseid +'\n'+ nodeid +'\n'+ appHomeUrl + 'menu/menu/unit/' + courseid + '/' + prefix);
	newnode.loadSubTree(appHomeUrl + 'menu/menu/unit/' + courseid + '/' + prefix);
}
*/
/* No loger used
function refresh_coursenode1(thistree, prefix, courseid,imgeurl,hidden,coursename) {
	var nodeid=prefix + 'course' + courseid;
	var node = thistree.getNode(nodeid);
	try {
	if ( typeof node == 'undefined' || !node.isExpanded())
		return;
	}
	catch(e){
		return;
	}

	var color = '';
	if (hidden=='1')
		color='show';
	else
		color='disable_course';
	
	var obj = document.getElementById(coursename);
	if (obj != undefined) {
		coursename = obj.value;
	}
	node.setText('<span class="'+ color +'">'+ coursename +'</span>');

	
	node.setImageSrc(imgeurl);
	
}
*/
function shrinkMenuDiv(name) {
	var divobj = getDivobj(name);
	hideDiv(name);
	if (divobj != undefined && divobj.style != undefined) {
		divobj.style.height = '1px'; //0px won't work in IE7
		divobj.style.width = '1px';
		divobj.style.overflow = 'hidden';
	}
}

function expandMenuDiv(name) {
	var divobj = getDivobj(name);
	if (divobj != undefined && divobj.style != undefined) {
		divobj.style.height = 'auto';
		divobj.style.width = 'auto';
		divobj.style.overflow = 'auto';
	}
	showDiv(name);
}

function shrinkDiv(name, margin) {
	var divobj = getDivobj(name);
	hideDiv(name);
	if (divobj != undefined && divobj.style != undefined) {
		divobj.style.height = '1px'; //0px won't work in IE7
		divobj.style.width = '1px';
		divobj.style.overflow = 'hidden';
		if (typeof margin != 'undefined')
			divobj.style.margin = margin;
	}
}

function expandDiv(name, margin) {
	var divobj = getDivobj(name);
	if (divobj != undefined && divobj.style != undefined) {
		divobj.style.height = 'auto';
		divobj.style.width = 'auto';
		divobj.style.overflow = 'auto';
		if ( typeof margin != 'undefined' ) {
			divobj.style.margin = margin;
		}
	}
	showDiv(name);
}

//This will work for any checkboxes on any form.
//The name_pattern (default is gradeid_) will separate multiple groups of checkboxes.
function allowOneCheckBox(checkBox,name_pattern) {
	var theform = checkBox.form;
	var elementname,elementtype,matched_str;
	if ( typeof(name_pattern) == 'undefined' ) {
		name_pattern='gradeid_';
	}
	var regexobj=new RegExp(name_pattern,'gi');
	for(var i=0; i < theform.elements.length; i++) {
		elementname=theform.elements[i].name;
		if ( typeof(elementname) == 'undefined' || elementname == '' )
			elementname = theform.elements[i].id;
		if ( typeof(elementname) == 'undefined' || elementname == '' )
			continue;
		elementtype = theform.elements[i].tagName.toLowerCase();
		if (elementtype == 'input' && theform.elements[i].type == 'checkbox' && theform.elements[i] != checkBox) {
			matched_str=elementname.match(regexobj);
			if ( matched_str == name_pattern ) {
				theform.elements[i].checked=false;
			}
		}
	}
	return true;
}

function  saveCourseDesc(divid,url,formname)
{
	ajaxPostform(divid,url,formname);
}

function setNewCourseOwner(divid) {
	index=document.editorsel_form.editor_sel.selectedIndex;
	if ( index >= 0 ) {
		name = document.editorsel_form.editor_sel.options[index].text;
		id = document.editorsel_form.editor_sel.options[index].value;
		//document.coursedesc_form.teachername.value = name;
		document.getElementById('editornames').innerHTML = name;
		document.editcouresdes_form.editorid.value = id;
		closeDhtmlWindow('changeteacher_div');
	}
	else {
		printErrorWindow('Please select a user.');
	}
	return false;
} 

//java script for Integrated Unit, Unified Unit    
function setNewUnitOwner(url_refresh, formname, closediv, lang_del,flag_name) { 
	ask_id = 0;
	removeflag = document.getElementById(flag_name);
	if (removeflag != undefined ) {
		ask_id = parseInt(removeflag.value);
	}
	if (ask_id > 0) { //remove
		var answer = confirm(lang_del);
	    if (answer) { 
	    	ajaxPostform('page_content', url_refresh, formname, true); 
			closeDhtmlWindow(closediv);
	    } 
	} else { // normal add 
		ajaxPostform('page_content', url_refresh, formname, true); 
		closeDhtmlWindow(closediv);
	}
	return false; 
}

//java script for Integrated Unit, Unified Unit 
function setEditUnitOwner( url_check_mode, url_insert, url_delete, url_refresh, formname, lang_del, closediv) {   
	var rethtml = ajaxPostform('', url_check_mode, formname, false);   
	var str = rethtml.split("|");  
	if (str != undefined) { 
		var ask_id = str[0]; 
		//var ref_id = str[1];
		if (ask_id==1) {  
			var answer = confirm(lang_del);
		    if (answer) { 
		    	var rethtml_insert = ajaxPostform('', url_insert, formname, false);
		    	
		    	var rethtml_delete = ajaxPostform('', url_delete, formname, false);  
		    	ajaxPostform('page_content', url_refresh, formname, false); 
		    	//refresh_menutree(ref_id);
		    	refresh_menutree();
		    	closeDhtmlWindow(closediv); 
		    }
		} else { // normal add 
			var rethtml_insert = ajaxPostform('', url_insert, formname, false);  
			ajaxPostform('page_content', url_refresh, formname, false);  
			closeDhtmlWindow(closediv);
		} 
	}    
 
	return false; 
} 

//java script for Integrated Unit, Unified Unit  // multi-select and check duplicated
function show_select(obj, targetids, targetnames) {
	var str_id = '';
	var str_name = ''; 
	var str_name_div = '';  
	if (obj != undefined ) {   
		var row = 40; 
		var color = 'black';
		for (var i = 0; i < obj.length; i++) { 
			if (obj[i].selected) {
				
				var name = obj[i].text;
				var id = obj[i].value;    
				str_id += id+' '; 
				str_name += name+'\|'; 
				
				str_name_div += '<font color="'+color+'">'+ name+'</font>,';
				if (color == 'black') { color = 'black'; } else { color = 'black'; }  
				
				if (str_name.length >= row) {
					str_name_div +=  '<br />';
					row += 60; 
				}
			}  
		}
	} 
	
	var pos = str_name_div.lastIndexOf(','); 
	str_name_div = str_name_div.substring(0, pos); 
	document.getElementById(targetids).value = str_id;
	document.getElementById(targetnames).innerHTML = str_name_div;  
	return false; 
}

function newLocation(url){
	window.location=url;
}

function goHome() {
	window.location=appHomeUrl;
}

function changeMenuHeight() {
	menudiv = document.getElementById('menu');
	menudiv.style.height=window.innerHeight - 145;
}

//Debugging function
function printObject(obj) {
	var str = '';
	for (prop in obj) {
		str+= "****" + prop + ' : ' + obj[prop] + ' <br /><br /> ';
	}
	document.write (str);
}

function nosession_handle(sender, arg) {
	var node = sender.getNode(arg.NodeId);
	var ids = node.getChildIds();
	if ( ids.length > 0 ) {
		childid = ids[0];
		if ( childid == 'nosession' ) {
			sender.removeNode(childid);
			if ( typeof sessionErrorMsg != 'undefined' && sessionErrorMsg != '' )
				errormsg = sessionErrorMsg;
			else
				errormsg = "Your session has timedout, you wil be redirected in 10 seconds";
			printErrorWindow(errormsg, 350, 250, true);
			return true;
		}
	}	
	return true;
}

function cancelInput(divid, url) {
	var ret = confirm(cancelInputMessage);
	if ( ret ) {
		ajaxGetpage(divid, url);
	}
	return false;
}

function cancelIframe(divid) {
	var ret = confirm(parent.cancelInputMessage);
	if ( ret ) {
		parent.closeDhtmlWindow(divid);
	}
	return false;
}

function submitIframe(url, formname, stageurl, divid, closediv) {   
	var obj = document.getElementById('text1');
	if ( obj != undefined ) {
		var str = tinyMCE.get('text1').getContent();
		//obj.value = tinyMCE.get('text1').getContent();
		//fix default font style & font size
		//alert(str);
		str = str.replace('<span style="font-family: verdana,arial,helvetica,sans-serif;"><span style="font-size: 11px;">&nbsp;</span></span>','');
		str = str.replace('<span style="font-family: verdana,arial,helvetica,sans-serif;"></span>','');
		obj.value = str; 
	}
	if (closediv == undefined || closediv == '')
		closediv = 'resource';
	var ret = ajaxPostform('', url, formname, false); 
	if ( ret != false  || typeof ret == 'string' ) {
		if ( typeof divid != 'undefined' && typeof stageurl != 'undefined') {
			parent.ajaxGetpage(divid, stageurl, false);
		}
		parent.closeDhtmlWindow(closediv);
	}
	return false;
}

function delUnitItemNorefresh(url, msg, iwin) {
	var ret = confirm(msg);
	if ( ret ) {
		ajaxGetpage('', url, false);
	}
	parent.closeDhtmlWindow(iwin);
	return false;
}

function delUnitItem(url, stageurl, divid, msg) {
	var ret = confirm(msg);
	if ( ret ) {
		ajaxGetpage('', url, false);
		ajaxGetpage(divid, stageurl);
	}
	return false;
}

function downloadWindow(url) {
	var obj = document.getElementById('hiddeniframesystemdownload');
	if ( obj == undefined || obj == null ) {
		//Only create a iframe once for the duration of the session
		obj = document.createElement("iframe");
		obj.setAttribute('id', 'hiddeniframesystemdownload');
		document.body.appendChild(obj);
		hiddenOBJ('hiddeniframesystemdownload');
	}
	if ( obj != undefined && obj != null ) {
		obj.src = url;
	}
	else {
		//Fall back to old style browser window mode if somehow iframe does not work
		mywindow = window.open(url,'download','width=1,height=1');
		mywindow.blur();
		mywindow.moveTo(600,500);
	}
	return false;
}

function cancelDhtml(divid) {
	var ret = confirm(parent.cancelInputMessage);
	if ( ret ) {
		closeDhtmlWindow(divid);
	}
	return false;
}

function submitDhtml(url, formname, stageurl, divid, dhtmlname) {
	var ret = ajaxPostform('', url, formname, false);
	// question why orelse || if ( ret != false  || typeof ret == 'string' || ret=='' || ret=='success') {
	if ( ret != false || (typeof ret == 'string' && (ret=='' || ret=='success')) ) { 	
		if (stageurl!=false || stageurl!='')
		ajaxGetpage(divid, stageurl, false);
		if ( typeof dhtmlname == 'undefined' )
			dhtmlname = 'select';
		closeDhtmlWindow(dhtmlname);
	}
	return false;
}

function updateTarget(url, formname, divid, closediv) {
	if ( ajaxPostform(divid, url, formname, false) )
		closeDhtmlWindow(closediv);
	return false;
}

function delTarget(url, divid, msg) {
	if ( confirm(msg) ) {
		ajaxGetpage(divid, url, false);
	}
	return false;
}


function writeSpan(objid, msg) {
	var obj = document.getElementById(objid)
	if ( obj != 'undefined' ) {
		obj.innerHTML = msg; 
	} 
}
function print_flashchart(swfurl,divid,width,height,dataurl) {
	var newurl = dataurl + '/' + new Date().getTime();
	var encodedurl = encodeURIComponent(newurl);
	//wmode: opaque, transparent, window
	//swfobject.embedSWF(swfurl,divid,width,height,"9.0.0","expressInstall.swf",{"data-file":encodedurl,"wmode":"opaque"});
	swfobject.embedSWF(swfurl,divid,width,height,"9.0.0","expressInstall.swf",{"data-file":encodedurl});
}

//Mapping search initial window
function searchWindow(url,title,width,height,divid,formname,chartdata) {
	var rethtml=ajaxPostform('', url, formname, false);
	if ( rethtml != false ) {
		newDhtmlWindow_text(title, width, height, divid, rethtml);
		if ( typeof chartdata == 'object') {
			var prefetch = ajaxGetpage('',chartdata[4],false);
			if (prefetch != '{}') {
				print_flashchart(chartdata[0],chartdata[1],chartdata[2],chartdata[3],chartdata[4]);
			}
		}
		/* else {
			doJscriptProcess(rethtml);
		} */
	}
	return false;
}

//Moved the calculation of window height and graph height here
//so we can get the accurate height even if the number of units changed.
function unitcalendar(url,title,width,divid,formname,chartdata,heighturl) {
	var total = ajaxGetpage('', heighturl, false);
	height = 170 + (total*35);
	chartdata[3] = 75 + (total*35);
	searchWindow(url,title,width,height,divid,formname,chartdata);
	return false;
}

//The new window resulted from clicking on a falshchart URL link.
//The parentid should be the id of the original flashchart id so we can hide it
//Ignore parentid or supply '' or false if you don't need to hide the parent flashchart window
function showChartUnitlist(url, title, width, height, id, parentid) {
	var hasParentid = ( typeof parentid != 'undefined' && parentid != '' && parentid != false );
	if ( hasParentid ) {
		hideDhtmlWindow(parentid);
	}
	var obj = newDhtmlWindow(url, title, width, height, id);
	//parentid is without the dhtml_ prefix
	//We hide parent window because Safari on Windows messes up the parent window
	//So, we hide parent window for all browsers
	if ( hasParentid ) {
		prepareDhxwinFlipDisplay(obj, parentid);
	}
}

//Function to hide parent window whenever child window is shown on the screen
function prepareDhxwinFlipDisplay(obj, parentid) {
	if ( isDhtmlxObject(obj) ) {
		obj.cms_parent_dhxwinid = parentid;
		obj.attachEvent('onClose', 'unhideParentDhxwin');
		obj.attachEvent('onParkUp', 'unhideParentDhxwin');
		obj.attachEvent('onParkDown', 'hideParentDhxwin');
		obj.attachEvent('onShow', 'hideParentDhxwin');
		obj.attachEvent('onHide', 'unhideParentDhxwin');
	}
}

function hideParentDhxwin(winobj) {
	if ( isDhtmlxObject(winobj) && typeof winobj.cms_parent_dhxwinid != 'undefined' ) {
		hideDhtmlWindow(winobj.cms_parent_dhxwinid);
	}
	return true;
}

function unhideParentDhxwin(winobj) {
	if ( isDhtmlxObject(winobj) && typeof winobj.cms_parent_dhxwinid != 'undefined' ) {
		showDhtmlWindow(winobj.cms_parent_dhxwinid);
	}
	return true;
}

//Fix this 2 ajax call code
function ajaxPostIframe(url,title,width,height,divid,formname) {
	var rethtml=ajaxPostform('', url, formname, false); 
	if ( rethtml != false ) {
	 	winobj = newDhtmlWindow(url+rethtml,title,width,height,divid);
	}
	return false;
}

//for open and post data to newDhtml 
function ajaxPostWindow(url,title,width,height,divid,formname) {
	var rethtml=ajaxPostform('', url, formname, false);
	if ( rethtml != false && rethtml != '' ) {
		newDhtmlWindow_text(title, width, height, divid, rethtml);
	}
	return false;
}
//checkbox checkd to textbox by parseInt +1 when checked and -1 when unchecked
function remove_flag(obj,flag_name) {
	if (obj != undefined ) { 
		removeflag = document.getElementById(flag_name);
		if (removeflag != undefined ) {   
			if (removeflag.value=='') 
				removeflag.value = 0;
			if (obj.checked == 1) 
				removeflag.value = parseInt(removeflag.value)+1;
			else
				removeflag.value = parseInt(removeflag.value)-1;
		}
	} 
}
//For mapping EQ
function ajax_select(obj , url, target_div , formname) {
	var str_id = '';       
	if (obj != undefined ) {   
		 if (obj=='1'){
			 str_id =obj;
		 }
		 else{
			 index  = obj.selectedIndex;  
			str_id = obj.options[index].value;
		 }
	}    
	if (str_id != '' && target_div != '' ) {   
		document.getElementById(target_div).innerHTML = '<img src="'  + app_image_folder + 'ajax_arrow.gif' + '" />'; 
		ajaxPostform(target_div, url+str_id, formname, true);    
	}
	return false; 
}

function sortUnitItem(url, stageurl, divid) {   
	if  (stageurl != undefined && stageurl != '') {
		ajaxGetpage('', url, false);
		ajaxGetpage(divid, stageurl, true); 
	} else {
		ajaxGetpage(divid, url, false);
	} 	 
	return false;
}

function init_tinymce(base_url, svgimg, svgfile, svgcss) {    
	tinyMCE.init({
		mode : "textareas",
	    theme : "advanced",
	    //comment font format: formatselect,
	    theme_advanced_buttons1 : "fontselect,fontsizeselect,bold,italic,underline,strikethrough,separator,sub,sup,separator,cut,copy,paste,undo,redo,pastetext,pasteword,selectall",
	    theme_advanced_buttons2 : "justifyleft,justifycenter,justifyright,justifyfull,separator,numlist,bullist,outdent,indent,separator,forecolor,backcolor,separator,hr,link,unlink,image,table,code,separator,asciimath,asciimathcharmap,asciisvg",
	    theme_advanced_buttons3 : "",
	    theme_advanced_fonts : "Arial=arial,helvetica,sans-serif,Courier New=courier new,courier,monospace,Georgia=georgia,times new roman,times,serif,Tahoma=tahoma,arial,helvetica,sans-serif,Times=times new roman,times,serif,Verdana - Default=verdana,arial,helvetica,sans-serif",
	    theme_advanced_toolbar_location : "top",
	    theme_advanced_toolbar_align : "left",
	    theme_advanced_statusbar_location : "bottom",
	    plugins : 'paste,safari,asciimath,asciisvg,table,inlinepopups',
	    //fix new line <p> to <br/>
	    forced_root_block : false,
	    force_br_newlines : true,
	    convert_newlines_to_brs : false, 
	    //fix select font size
	    font_size_style_values : "8,9,10,11,12,14,16",
	    theme_advanced_font_sizes : "1,2,3,4,5,6,7",   
	    paste_create_paragraphs : false,
		paste_create_linebreaks : true,
		paste_use_dialog : true,
		paste_auto_cleanup_on_paste : true,
		paste_convert_middot_lists : true,
		paste_retain_style_properties : true,
		paste_unindented_list_class : "unindentedList",
		paste_convert_headers_to_strong : true,
		handle_event_callback : "update_parent_session_event",
	    /*
	    paste_use_dialog : false,
	    paste_auto_cleanup_on_paste : true,
	    paste_convert_headers_to_strong : false,
	    paste_strip_class_attributes : "all",
	    paste_remove_spans : true,
	    paste_remove_styles : true,*/
	    AScgiloc : base_url + svgimg, //'misc/php/svgimg.php',
	    ASdloc : base_url + svgfile, //'include/js/tiny_mce/plugins/asciisvg/js/d.svg',
	    content_css : base_url + svgcss, //'include/css/content.css'
	    gecko_spellcheck : true,
	    oninit : "postInitWork"
	});
}
function postInitWork() {
	if ( tinymce_backgroundcolor != '' ) {
		var resource_text = tinyMCE.getInstanceById('text1');
		resource_text.getBody().style.backgroundColor = tinymce_backgroundcolor;
	}
}
function hiddenOBJ(objid) { 
	if (objid =='' || objid==false )
		objid = 'mathplayer';
	var obj = document.getElementById(objid);
	if (obj != undefined ) { 
		obj.style.visibility="hidden";
		obj.style.height = '0px';
		obj.style.margin = '0px';
		obj.style.border = '0px';
		obj.style.width = '0px';
		obj.style.padding = '0px';
		obj.style.cssFloat = 'right';
		obj.style.display = 'none';
	}
}

//update_div is for Dhtmlx window, set it to '' if not using it
//notwin_div - normal div and ignore param if not using
function updateWindow(url, formname, title, update_div, notwin_div) {
	if (typeof notwin_div == 'undefined' || notwin_div == '' || notwin_div == false) {
		var obj = getDhtmlWindowById(update_div);
		if (obj != null ) {
			//obj.contentarea.innerHTML = wait;
			var rethtml = ajaxPostform('', url, formname, false);
			/* if ( winobjAttachHtml(obj, rethtml) ) {
				doJscriptProcess(rethtml);
			} */
			winobjAttachHtml(obj, rethtml);
		}
	}
	else {
		//Updates a normal div and not a Dhtmlx window
		/*var wait_html = '<div style="margin-top:30%;text-align:center;border:solid 0px">'+
        		   '<img src="' + app_image_folder + 'wait30.gif" /></div>'+
        		   '<div style="margin-bottom:30%;text-align:center;border:solid 0px"></div>'; */
		var obj = document.getElementById(notwin_div);
		if (obj != undefined ) {  
			//obj.innerHTML = wait_html;
			//var rethtml = 
			ajaxPostform(notwin_div, url, formname, true); 
			//obj.innerHTML = rethtml; 
			//doJscriptProcess(rethtml);
		}
	}
	return false;
}

function select_listitem(obj, url, formname, update_div, type) {   
	if (update_div == 'page_content') {
		if (type=='')
			type='0';
		if (obj.name == 'filters') {
			ajaxPostform(update_div,  url + obj.value+'/'+type+'/'+update_div , formname, true);
		}
		else {
			ajaxPostform(update_div,  url  , formname, true);
		}
	} else  {
		if (obj.name == 'filters') {
			updateWindow(url + obj.value+'/'+type+'/'+update_div, formname, '', update_div);
		}else{
			updateWindow(url, formname, '', update_div);
		}		
	} 
	
	return false; 
}
function refresh_page(obj, url) { 
	formname = 'resource_form';
	var rethtml = ajaxPostform('', url, formname, false);
	document.getElementById('dhtmlwin_resource').innerHTML = rethtml;
	return false; 
}
function reset_page(obj, url) { 
	var ret = confirm(parent.cancelInputMessage);
	if ( ret ) {
		refresh_page(obj, url);
	}
	
	return false; 
}
//Used to check for unique username when creating new user but this feature is obsolete
/*
var username = null;
function checkusername(url) {  
	var obj = document.getElementById('username');
	if (obj != undefined && username != obj.value)
		username = obj.value;
	else
		return false;  

	formname = 'resource_form';
	var rethtml = ajaxPostform('', url, formname, false);
	document.getElementById('dhtmlwin_checkuser').innerHTML = rethtml;
	return false; 
}
*/

function showhidediv(obj,id) { 
	var style_show = 'block';
	var style_hide = 'none';
	if (obj.value != undefined) {
	var object = document.getElementById(id);
			if (obj.value!=1 && obj.value!=0){
				object.style.display = style_hide;
			}else{
				object.style.display = style_show;
			}
		}    
}
 
function display(showflag, hidebutton, showbutton, showdiv ) { 
	if (showflag == undefined) {
		return false;
	}
	var style_show = 'none';
	var style_hide = 'block';
	
	if (showflag == false) {
		style_show = 'block';
		style_hide = 'none';
	}
	if (hidebutton != undefined) {
		var obj = document.getElementById(hidebutton);
		if (obj != undefined)
			obj.style.display = style_show;
	} 
	if (showbutton != undefined) {
		if (showflag == false)  
			hideDiv(showbutton);
		else 
		    showDiv(showbutton);
		var obj = document.getElementById(showbutton);
		if (obj != undefined)
			obj.style.display = style_hide;
	} 
	if (showdiv != undefined) {
		if (showflag == false)  
			hideDiv(showdiv);
		else 
		    showDiv(showdiv); 
		var obj = document.getElementById(showdiv);
		if (obj != undefined)
			obj.style.display = style_hide;
	}
}
 
function refresh_select(obj,url,reset) { 
	if (reset != '' || reset != undefined) {
		reset = '/'+reset;
	}
	else {
		reset = '';
	}
	ajaxGetpage('page_content', url+'/'+obj.value+reset, false); 	
	return false; 
}

function submitUser(url, formname, stageurl, divid, dhtmlname) {
	var savepass = document.getElementById('password');
	var showpass = document.getElementById('showpass');
	savepass.value = des_encrypt(showpass.value);
	
	submitDhtml(url, formname, stageurl, divid, dhtmlname);
	return false;
}

function showbutton(obj,id) { 
	
	var style_show = 'block';
	var style_hide = 'none';
	if (obj.value != undefined) {
	var object = document.getElementById(id);
			if (obj.value != ''){
				object.style.disabled=true;
				object.style.display = style_show;
			}else{
				object.style.display = style_hide;
			}
		}    
}

function show_select_local(obj, targetids, targetnames) {
	
	var str_id = '';
	var str_name = ''; 
	var str_name_div = '';  
	if (obj != undefined ) {   
		var row = 40; 
		var color = 'red';
		for (var i = 0; i < obj.length; i++) { 
			if (obj[i].selected) {
				
				var name = obj[i].text;
				var id = obj[i].value;    
				str_id += id+' '; 
				str_name += name+'\|'; 
				
				str_name_div += '<font color="'+color+'">'+ name+'</font>,';
				if (color == 'red') { color = 'red'; } else { color = 'red'; }  
				
				if (str_name.length >= row) {
					str_name_div +=  '<br />';
					row += 60; 
				}
			}  
		}
	} 

	var pos = str_name_div.lastIndexOf(','); 
	str_name_div = str_name_div.substring(0, pos); 
	//document.getElementById(targetids).value = str_id;
	alert(str_name_div);
	document.getElementById(targetnames).innerHTML = str_name_div;  
	return false; 
}
function refresh_unitcopy(url,courseid) { 
	formname = 'resource_form';
	var rethtml = ajaxPostform('', url, formname, false);
	document.getElementById('dhtmlwin_resource').innerHTML = rethtml;
	//refresh_unitnode(courseid);
	refresh_menutree();
	return false; 
}
function showhide_course_unit(showflag, hidebutton, showbutton ,url ) { 
	formname = 'resource_form';
	ajaxPostform('', url, formname, false);
	display(showflag, hidebutton, showbutton, '' ) ;
	return false; 
}

//For admin section, searches using the user input search text
//function search_filter(url,starturl,formname,div) {
function search_filter(url,formname,div) { 
	//var rethtml = ajaxPostform('', url, formname, false);
	//if (div == 'page_content') {
		//ajaxGetpage(div, starturl + rethtml, true);
		ajaxPostform(div, url, formname, false);
	//}
	//else {
		//don't think this else part is ever executed...
		//url	     = starturl + rethtml;
		//updateWindow(url, formname, '', div);
	//}
	return false; 
}

function LoadPdfPage(url, pdf, divid) {   
	var obj = document.getElementById(divid);
	if (obj != undefined) { 
		if (pdfWaitHtml != undefined && pdfWaitHtml != '')  {
			obj.innerHTML = pdfWaitHtml;
		}
		ajaxGetpage(divid, url+pdf, true); 
	} 
	return false;
}

function getRadioButton(objid, divid, courseids, post_url, formname, back_url) {   
	var obj = document.getElementsByName(objid);
	if (obj != undefined) { 
		for( i = 0; i < obj.length; i++ ) {	 
			if (obj[i].checked == true) {  
				if (obj[i].alt == '') {   
					closeDhtmlWindow(divid);
					return false;
				}
				else if (confirm(obj[i].alt)) {  
					var rethtml = ajaxPostform('', post_url, formname, false);     
					if (rethtml =='success' ) {
						parent.closeDhtmlWindow(divid);
						//parent.refresh_menutree(courseids);
						parent.refresh_menutree();
						parent.ajaxGetpage('page_content', back_url, true);
						return true;
					}  
				} 
			} 
		} 
	}    
}
function del_listitem(url, divid, msg,formname) {
	if ( confirm(msg) ) {
		if (divid == 'page_content') {
			ajaxGetpage(divid, url, false);
		}else{
			var rethtml = ajaxPostform('', url, formname, false);
			updateWindow(rethtml, formname, '', divid);
		}
	}
	return false;
}
function submit_input(url, formname, stageurl, divid, dhtmlname) {
	var ret = ajaxPostform('', url, formname, false);
	if ( ret != false || (typeof ret == 'string' && (ret=='' || ret=='success')) ) { 	
		if (stageurl!=false || stageurl!='')
			if (divid == 'page_content') {
				ajaxGetpage(divid, stageurl, true);
			}else{
				updateWindow(stageurl, divid, '', divid)
			}
		if ( typeof dhtmlname == 'undefined' )
			dhtmlname = 'select';
		closeDhtmlWindow(dhtmlname);
		
	}
	return false;
}
function compare_histoty(url, formname, update_div) {   
		updateWindow(url, formname, '', update_div);
	return false; 
}
function histoty_version(href,url,formname,title, width, height, div) {  
	var rethtml = ajaxPostform('', href, formname, false); 
	if(rethtml!=false){
		//closeDhtmlWindow(formname);
		newDhtmlWindow(url+rethtml,title, width, height, div);
	}
return false; 
}

function change_button(divid, imagesrc, title, urlclick, formname, update_div) {
	if (title == undefined || title == '') {
		title = ''; 
	}
	var htmlimg = "<img src='"+imagesrc+"' title='"+title+"' border='0' />";
	if (update_div == undefined)
		update_div = "page_content"; 
	if (urlclick !== undefined && urlclick !== ''  && urlclick !== null) {
		var onclick = 'ajaxPostform("'+update_div+'", "'+urlclick+'", "'+formname+'",false); return false;';
		htmlimg = "<a href='"+urlclick+"' onClick='"+onclick+" ' >"+htmlimg+"</a>";	
	}
	var obj = document.getElementById(divid);
	if (obj != undefined) { 
		obj.innerHTML = htmlimg; 
	}
	return false; 
}

//function user_course(href_save, href_refresh, href_desc, formname,descid) {
function user_course(href_save, href_refresh, formname) {
	var rethtml = ajaxPostform('', href_save, 'resource_form', true)	
	if (rethtml == 'success') {
		/*
		if(descid!=''){
			ajaxGetpage("page_content", href_desc, true);
		} */
		updateWindow(href_refresh, formname, 'title', 'course_input')
	}
	/*
	var obj = document.getElementById(divid);
	if (obj != undefined) { 
		obj.innerHTML = htmlimg; 
	}*/
	return false; 
}
function course_editor(href_save, href_refresh, href_desc, formname,descid) {
	var rethtml = ajaxPostform('', href_save, 'resource_form', true)	
	if (rethtml == 'success') {
		ajaxGetpage("page_content", href_desc, true);
		updateWindow(href_refresh, formname, 'title', 'course_input')
	}
	return false; 
}

/*
function unlock_editorunit(url,url_unlock,courseid,divbutton) {	
	ajaxGetpage('page_content',url, false);
	ajaxPostform('', url_unlock, 'resource_form', false)
    refresh_unitnode(courseid);
	var style_hide = 'none';
	var obj = document.getElementById(divbutton);
	if (obj != undefined )
		obj.style.display = style_hide;

    return false; 
}
*/
function updateDivConfirm(divid, url, msg) {
	if ( confirm(msg) ) {
		ajaxGetpage(divid, url, false);
	}
}

function update_checkbox(href_save,value) {
	var rethtml = ajaxPostform('', href_save, 'itstd_form', true);
}

function course_lnk(divid, imagesrc, title, urlclick, formname) {
	if (title == undefined || title == '') {
		title = ''; 
	}
	htmlimg = "<img src='"+imagesrc+"' title='"+title+"' border='0' />";	
	if (urlclick != undefined && urlclick != '') {
		updateWindow(urlclick,formname, "title","course_input");
		htmlimg = "<a href='"+urlclick+"' onClick='"+onclick+"' >"+htmlimg+"</a>";
	}
	return false; 
}

function user_goal(href_save, href_refresh, href_goal, formname,divname) {
	var rethtml = ajaxPostform('', href_save, formname, true);	
	if (rethtml == 'success') {
		ajaxGetpage("page_content", href_goal, true);
		updateWindow(href_refresh, formname, 'title', divname);
	}
	return false; 
}
function select_unit(url, formname, update_div, massage) {
	var rethtml = ajaxPostform(update_div,  url , formname, true);
	countdownvalue('massage', '&nbsp;');
	return false; 
}
function select_item_goal(obj, url, formname, update_div) {  
	ajaxPostform(update_div,  url + obj.value , formname, true);
	return false; 
}
function add_goal(url,href_refresh,formname, massge) { 
	var rethtml = ajaxPostform("page_content", url, formname, false);
	if (rethtml == true) {
		var obj = document.getElementById('massage');
		if (obj != undefined) {
			obj.innerHTML = massge; 
		} 
		//timer_goal = start_goal; //initial timer
		countdownvalue('massage', '&nbsp;');
	}
	else{
		var obj = document.getElementById('title');
		var obj_input = document.getElementById('txttitle');
		if (obj != undefined && obj_input != undefined) {
			obj_input.value = obj.value; 
		} 
		else {
			//alert('object lost'); //please change text
		}
	}
	return false; 
}
function update_datetime(url,formname) { 
	ajaxPostform('',  url , formname, true);
	return false; 
}

function countdownvalue(divid, message, timeout){
	var obj = document.getElementById(divid);
	if (obj != undefined) {
		if (timeout == undefined) {
			var func_name = 'countdownvalue("'+divid+'","'+message+'",true);'; 
			setTimeout(func_name,5000);
		}
		else {
			obj.innerHTML = '&nbsp;';
		}
	}
}

var mouse_click = false;
var session_inactive_time = 0;
var ajax_skip_count = 0;
var system_timeout_value = 10*60*1000;
var system_max_inactive_time = 2*3600*1000;

function force_update_session() {
	ajax_is_running++;
	ajaxPostform('',appHomeUrl + 'session_update/force_update','',false);
	ajax_is_running--;
}

function update_session() {
	var logout_url = appHomeUrl + 'login/logout';
	var skip_timeout = 60000;
	var max_skip_count = 8;
	if ( ajax_is_running > 0 ) {
		ajax_skip_count++;
		if ( ajax_skip_count > max_skip_count ) {
			ajax_skip_count = 0;
			ajax_is_running = 0;
		}
		var func_name = 'countdownpost(' + skip_timeout + ',' + system_max_inactive_time + ',true);';
		setTimeout(func_name,skip_timeout);
		return false;
	}
	
	if ( ajax_is_running < 0 )
		ajax_is_running = 0;
	
	session_inactive_time = 0;
	mouse_click =  false;
	result = ajaxPostform('',appHomeUrl + 'session_update','',false,false);
	if ( result == 'failed' ) {
		window.location = logout_url;
		return false;
	}
	return true;
}

function countdownpost(timeout_value, max_inactive_time, timeout){
	var func_name = 'countdownpost(' + timeout_value + ',' + max_inactive_time + ',true);';
	var logout_url = appHomeUrl + 'login/logout';
/*var obj = document.getElementById('debug')
if (obj != undefined) { 
	obj.innerHTML = ajax_is_running + ' - ' + session_inactive_time + ' - ' +
	max_inactive_time + ' - ' + new Date().getTime(); 
}*/
	if ( timeout == undefined) {
		system_timeout_value = timeout_value;
		system_max_inactive_time = max_inactive_time;
		session_inactive_time += timeout_value;
		setTimeout(func_name,timeout_value);
		return;
	}
	if(mouse_click != undefined && mouse_click) {  
		if ( update_session() == false )
			return;
	} else {
		result = ajaxPostform('',appHomeUrl + 'session_update/check_sess','',true,false);
		if ( result == 'failed' ) {
			window.location = logout_url;
			return false;
		}
		session_inactive_time += timeout_value;
		if ( session_inactive_time >= max_inactive_time ) {
			window.location = logout_url;
			return;
		}
	}
	setTimeout(func_name,system_timeout_value);
}

function update_parent_session_event(e) {
	if ( e == '1' || e.type == 'keydown' )
		parent.mouse_click = true;
	return true;
}

function totalunit_refresh() {
	ajaxGetpage('total_unit', appHomeUrl+'menu/menu/totalunit', false); 
}
function find_filter(url, formname, update_div, type) { 
	ajaxPostform(update_div,  url , formname, true);
	return false; 
}

function update_radio(url) {
	var ret_code;
	ret_code = ajaxGetpage('', url, false);
	if ( ret_code != 'success' ) {
		alert(raido_update_error_msg);
		return false;
	}
	return true; 
}

function checked_all_radios(formname) {
	var theform = document.forms[formname];
	var checked_array=new Array(0);
	var elementtype;
	for(var i=0; i < theform.elements.length; i++) {
		elementname = theform.elements[i].name;
		if ( typeof(elementname) == 'undefined' || elementname == '' )
			elementname = theform.elements[i].id;
		if ( typeof(elementname) == 'undefined' || elementname == '' )
			continue;
		elementtype = theform.elements[i].tagName.toLowerCase();
		if (elementtype == 'input' && theform.elements[i].type == 'radio') {
			if ( checked_array[elementname] === undefined ) {
				checked_array[elementname] = theform.elements[i].checked;
			}
			else if ( theform.elements[i].checked ) {
				checked_array[elementname] = true;
			}
		}
	}
	var retcode = true;
	for (var i in checked_array) {
		if ( !checked_array[i] ) {
			retcode = false;
			break;
		}
	}
	return retcode;
}

function survey_done(url, homeurl, formname, success_msg, failed_msg, incomplete_msg) {
	var ret_code='';
	if ( !checked_all_radios(formname) ) {
		alert(incomplete_msg);
		return false;
	}
	ret_code = ajaxGetpage('', url, false);
	if ( ret_code == 'success' ) {
		alert(success_msg);
	}
	else {
		alert(failed_msg);
	}
	window.location = homeurl;
	return false;
}

function survey_check_session() {
	var result = ajaxPostform('',appHomeUrl + 'session_update/check_sess','',false);
	var func_name = 'survey_check_session()';
	if ( result == 'failed' ) {
		window.location = surveyHomeUrl;
		return false;
	}
	setTimeout(func_name,5*60*1000);
}

function survey_result(url,width,height) {
	//ajaxGetpage('page_content', url, false);
	ajaxPostform('page_content', url, 'surveyresult', false);
	var index_text = ajaxGetpage('', appHomeUrl+'survey/result/chartindexes', false);
	var waittime = 0;
	if ( index_text.length > 1 ) {
		var indexes = eval(index_text);
		for (var i in indexes) {
			//Flash object is async so use settimeout to slow down server requests
			var chartfunction = 'print_flashchart("' + appIncludePath + 'flashchart.swf","surveychart_' +
				indexes[i] + '",' + width + ',' + height + ',"' + appHomeUrl +
				'survey/result/chart/' + indexes[i] + '");';
			var chartfunction_teacher = 'print_flashchart("' + appIncludePath + 'flashchart.swf","surveychart_' +
				indexes[i] + 't",' + width + ',' + height + ',"' + appHomeUrl +
				'survey/result/chart/' + indexes[i] + 't");';
			//Some timing issue with browser and flash
			if ( waittime == 0 ) {
				setTimeout(chartfunction, 50);
				setTimeout(chartfunction_teacher, 1600);
				waittime=2600;
			}
			else {
				waittime += 400;
				setTimeout(chartfunction, waittime);
				waittime += 400;
				setTimeout(chartfunction_teacher, waittime);
			}
		}
	}
	return false;
}

var current_tree_id;
var current_reloadnode_id;
function get_unit_id(){
	$("a#addunit_html, a#otherunitnode").live("click", function() { 	
		current_reloadnode_id = $(this).closest("li.jstree-last").attr("id");
		current_tree_id = $(this).closest("div.jstree-focused").attr("id");
	});	
	$("a#copyunit_html").live("click", function() {
		current_reloadnode_id = $(this).parents("li.jstree-last:eq(1)").attr("id");
		current_tree_id = $(this).closest("div.jstree-focused").attr("id");
	});
}
function get_current_id(){
	$("a#course_desc_html").live("click", function() {
		//use -1 to refresh whole tree in desc_edit_view
		current_reloadnode_id = '-1';
		current_tree_id = $(this).closest("div.jstree-focused").attr("id");
	});
}
function change_course(courseid, isHidden){
	var prefix_mycourse = 'myu' + courseid;
	var prefix_department = 'deptucourse' + courseid;
	var prefix_grad = 'gradeucourse' + courseid;
	var show = 'show';
	var hide = 'disable_course';
	// must does refresh before change id
	refresh_menutree();
	if ( isHidden == "1" ) {
		if ( current_tree_id == "course_tree" ){
			var prefix = prefix_mycourse;
		}
		else if ( current_tree_id == "dept_tree" ){
			var prefix = prefix_department;
		}
		else if ( current_tree_id == "grade_tree"  ){
			var prefix = prefix_grad;
		}
		var getspanid = document.getElementById(prefix).getElementsByTagName('span')[0];
		getspanid.className = show;
	}
	if ( isHidden == "0" ) {
		if ( current_tree_id == "course_tree" ){
			var prefix = prefix_mycourse;
		}
		else if ( current_tree_id == "dept_tree" ){
			var prefix = prefix_department;
		}
		else if ( current_tree_id == "grade_tree"  ){
			var prefix = prefix_grad;
		}
		var getspanid = document.getElementById(prefix).getElementsByTagName('span')[0];
		getspanid.className = hide;
	}
	return false;
}

function confirmDBUpgrade(url,title,width,height,divid,msg) {
	var ret = confirm(msg);
	if ( ret ) {
		newIframeWindow(url,title,width,height,divid);	
	}
	return false;
}

function openBrowserWindow(url, width, height, window_name) {
	if ( typeof width == 'undefined' ) {
		width=1000;
		height=750;
	}
	else if ( typeof height == 'undefined' ) {
		height=750;
	}
	if ( typeof window_name == 'undefined' || window_name == '' ) {
		window_name = 'pdf'+new Date().getTime();
	}
	var top = (screen.height-height)/2 - 60;
	if ( top < 0 ) {
		top = 0;
	}
	var left = (screen.width-width)/2;
	if ( left < 0 ) {
		left = 0;
	}
	var option = 'status=no,toolbar=no,menubar=no,location=no,resizable=yes';
	option = option + ',top=' + top + ',left=' + left + ',width=' + width + ',height=' + height;
	var new_winobj = window.open(url,window_name,option);
	new_winobj.focus();
	return false;
}

function noEnterKey(event) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	}
	else if (event) {
		keycode = event.which;
	}
	else {
		return true;
	}
	if (keycode == 13) {
		return false;
	}
}

//Course selection window for read-only teachers in the admin user page
function course_read(id,enable) {
	var url = appHomeUrl + 'admin/course/select/' + id + '/0/' + enable + '/user';
	new newDhtmlWindow(url, ' ', 600, 470, 'course_input');
	return false;
}
function course_edit(id,enable) {
	var url = appHomeUrl + 'admin/course/select/' + id + '/1/' + enable + '/user';
	new newDhtmlWindow(url, ' ', 600, 470, 'course_input');
	return false;
}
//Can delete any DOM element by ID. Not used yet but keep it here since
//it may be helpful in the future
/*
function removeElement(element_id) {
	var thisnode = document.getElementById(element_id);
	if ( thisnode != undefined ) {
		thisnode.parentNode.removeChild(thisnode);
		return true;
	}
	return false;
}
*/
