/**
 * Collection of isolated javascript functions
 */

function doClock(){ 
  var t=new Date();
  var a=doClock.arguments;
  var str="";
  var i,a1;
  var lang="1";
  var month=new Array('January','Jan', 'February','Feb', 'March','Mar', 'April','Apr', 'May','May', 'June','Jun', 'July','Jul', 'August','Aug', 'September','Sep', 'October','Oct', 'November','Nov', 'December','Dec');
  var tday= new Array('Sunday','Sun','Monday','Mon', 'Tuesday','Tue', 'Wednesday','Wed','Thursday','Thr','Friday','Fri','Saturday','Sat');
  for(i=0;i<a.length;i++) {a1=a[i].charAt(1);switch (a[i].charAt(0)) {
  case "M":if  ((Number(a1)==3) && ((t.getMonth()+1)<10)) str+="0";
  str+=(Number(a1)>1)?t.getMonth()+1:month[t.getMonth()*2+Number(a1)];break;
  case "D": if ((Number(a1)==1) && (t.getDate()<10)) str+="0";str+=t.getDate();break;
  case "Y": str+=(a1=='0')?t.getFullYear():t.getFullYear().toString().substring(2);break;
  case "W":str+=tday[t.getDay()*2+Number(a1)];break; default: str+=unescape(a[i]);}}return str;
}

function resendPassword() {
	document.location.href="frontcontroller.lms?dpMethod=com.atl.lms.servicelayer.Service.resendPasswordFromLogin";
}

function login() {
	document.location.href="frontcontroller.lms?dpMethod=com.atl.lms.servicelayer.Service.login";
}

function misc_logout() {
	// course course windows
	
	if (window.top['menu']!=null && window.top['menu'].currentCourseWindow!=null){
		window.top['menu'].menu_currentCourseWindow();
		window.setTimeout("top.location.href=\"frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.logout\";", 1000);	
	}else{
		top.location.href="frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.logout";
	}	
	
}

//call the below function if you want to close the window
function misc_closewindow() {
	misc_logout();
	top.window.close();
}

/**
 * Loads href in popup window. 
 */
function openPageInNewWindow(href) {
	window.open(unescape(href), "", "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=800,height=600");
}

/**
 * Loads course page in popup window.
 */
function openCourseInNewWindow(href) {
	window.open("/courses/" + unescape(href), "", "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes,width=800,height=600");
}


function hideShow(divId) {
	var child = document.getElementById(divId + "Child");
	var parent = document.getElementById(divId + "Parent");
	var state = child.style.display;
	if (state == "none")
	{
		child.style.display = "";
		parent.style.height = 45;
	}
	else {
		child.style.display = "none"
		parent.style.height = 40;
	}
}

/**
 * Removes unwanted characters e.g. + from strings for display on screen
 */
function util_clean(toClean) {
	var cleanString = new String(toClean);
	cleanString = cleanString.replace(/\+/g, " ");
	cleanString = cleanString.replace(/\&amp/g, "&");
	cleanString = cleanString.replace(/\%26/g, "&");
	cleanString = cleanString.replace(/\%AE/g, "?");
	cleanString = cleanString.replace(/\%2C/g, ",");

	return cleanString;
}


/**
 * Finds host that is loading this javascript page. Required by api instance. This gets around a problem, that is caused by the way javascript works.
 * If javascript from another host call the api instance, the instance will think, its is getting hosted on this host. That is way, the instance
 * has to keep a record of its host, when it gets initialized.
 */
function misc_findHost() {

	var url = document.location.href;
	var arr = url.split("/");
	var host = "";
	for (var i = 0; i < arr.length - 1; i++) {
		host += arr[i] + "/";
	}
	
	return host;

}


function misc_goToLms(){
	var url = misc_findHost();
	document.location.href = url;		
}

function checkUncheckAll(theElement) {
    var theForm = theElement.form, z = 0;
	 for(z=0; z<theForm.length;z++){
     if(theForm[z].type == 'checkbox' && theForm[z].name != 'checkall'){
	  theForm[z].checked = theElement.checked;
	  }     
	 }
}

function misc_loadCss(css){
	var fileref = document.createElement("link")
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", css);
	document.getElementsByTagName("head").item(0).appendChild(fileref)
}

function misc_replaceAll(string, currentChar, replacementChar){

	if (string){		
		while (string.match(currentChar)){
			string = string.replace(currentChar, replacementChar);
		}
	}
	return string;

}

function misc_showTestInNewWindow(variable, companyCss, currentLanguage){


	findAPIetc(window, variable, companyCss, currentLanguage)

}

function misc_disableNavigationField (changeField, fieldToDisable){

	if (changeField.value || changeField.value!=""){
		fieldToDisable.disabled=true;
	} else{
		fieldToDisable.disabled=false;
		fieldToDisable.backgroundcolor=changeField.backgroundcolor;
	}
}

function misc_hideDivs(divIds) {

	for (x in divIds ){

			var divId = divIds[x];

			var div = top.parent['menu'].document.getElementById(divId);
		
			div.style.display = "none";
			
	}

}

function misc_showDivs(divIds) {

	for (x in divIds ){

			var divId = divIds[x];

			var div = top.parent['menu'].document.getElementById(divId);

			div.style.display = "";

	}


}	

function courseGroups_getbookings(id) {
	document.location.href="frontcontroller.lms?slMethod=com.atl.lms.servicelayer.Service.getCourseGroup&viewName=trainingPlan&id="+id;
}

function misc_loadName(pageName){

	doLMSSetValue('cmi.core.lesson_location', pageName);	
	window.document.location = pageName;

}