
// UtilFunc.js  JavaScript utility functions
// general utility functions


// Global variables
// Global variables
var isN, isN6, isM
if (navigator.appVersion.charAt(0) == "4") 
{
	if (navigator.appName == "Netscape") 
	{
		isN = true;
		isM = false;
	} 
	else 
	{
		isM = true;
	}
}
else
{
	if(document.getElementById){
			isN6 = true;
	}
}


// Global funtions


function SetCookie(n,v) 
{
	var expire = new Date();
	expire.setMonth(expire.getMonth() + 6);
	var expireGMT=expire.toGMTString();
	document.cookie = n + "=" + escape(v) + ";expires=" + expireGMT;
}
function GetCookie(name) 
{    
	var start = document.cookie.indexOf(name+"=");
	var len = start+name.length+1;
	if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
	if (start == -1) return null;    var end = document.cookie.indexOf(";",len);
	if (end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len,end));
}
function ltrim(str){
	while ( str.charAt(0) == " "){
		str = str.substr(1);
	}
	return str;
}

function isAlphaNum(str)
{
	for (var i = 0; i < str.length; i++) 
	{
		if (!((str.charCodeAt(i) >= 48 && str.charCodeAt(i) <= 57)   ||	
		     (str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90)   ||	
		     (str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122))
			)	
		{
			return false;
		}
	}
	return true;
}

function isValidStr(str)
{
	for (var i = 0; i < str.length; i++) 
	{
		if (str.charCodeAt(i) < 32 || str.charCodeAt(i) > 122)	
		{
			return false;
		}
	}
	return true;
}

function isValidPasswordStr(str, strInvalidChars)
{
	var chars = new String(strInvalidChars);
	for (var i = 0; i < chars.length; i++) {
		if (str.indexOf(chars.charAt(i)) != -1)
			return false;
	}
	if (str.indexOf("\"") != -1)
		return false;

	if (str.indexOf(" ") != -1)
		return false;

	return true;
}

function isValidLoginStr(str, strInvalidChars)
{
	// Just do the same checking as Password
	return isValidPasswordStr(str, strInvalidChars);
}

function open_help(url){
	var width, height, scrollbars;
	width=640;height=480;
	popupWin=window.open(url, "help", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+width+",height="+height);
	if(!popupWin.closed)
		popupWin.focus();
}
function open_window(url) 
{
	var width, height, scrollbars;
	var lUrl = url.toLowerCase();
	var baseUrl = lUrl.substr(0, 8);
	var name = "help";
	scrollbars="scrollbars=0"
	var bGiveFocus=false;

	switch(baseUrl){
		case "aaddedre":
			width = 600;height = 400;bGiveFocus=true;
			break;
		case "cedprfle":
		case "auserpic":
			width = 500;height = 420;bGiveFocus=true;
			name="userPic";
			break;
		case "awizclas":
		case "awizclan":
			name = "claswizpop"
			width = 525;height = 420;bGiveFocus=true;
			break;
		case "aclsfram":
			width = 500;height = 450;bGiveFocus=true;
			break;
		case "alcnspop":
			width = 370;height = 500;bGiveFocus=true;
			name = "popup";
			break;
		case "cdismsg.":
			scrollbars="scrollbars=1"
		case "cdismsgn":
		case "cdismsgc":
			width = 500;height = 240;bGiveFocus=true;
			break;
		case "areorder":
			width = 400;height = 270;bGiveFocus=true;
			break;
		case "cpasswd.":
		case "aaddgrp.":
		case "aaddfldr":
			width = 450;height = 260;bGiveFocus=true;
			break;
		case "aselects":
		case "aselectf":
		case "aselectg":
		case "aselectm":
		case "aselectc":
			scrollbars="scrollbars=1"
			width = 500;height = 400;bGiveFocus=true;
			break;
		case "cvwsch.a":
			scrollbars="scrollbars=1";
			width = 785;height = "200";bGiveFocus=true;
			break;
		case "csuppdes":
			width = 300;height = "300";bGiveFocus=true;
			break;
		case "csuppmat":
			width = 650;height = "400";bGiveFocus=true;
			break;
		case "asendurl":
		case "csuppurl":
			width = 550;height = "220";bGiveFocus=true;
			break;
		case "cwaitlis":
			width = 520;height = "250";bGiveFocus=true;
			break;
		case "awaitlis":
			scrollbars="scrollbars=1";
			width = 640;height = "500";bGiveFocus=true;
			break;
		case "agrpasig":
		case "cmatview":
			scrollbars="scrollbars=1";bGiveFocus=true;
			break;
		case "aconfig.":
			width = 550;height = "500";
			scrollbars="scrollbars=1";bGiveFocus=true;
			break;
		case "ausagese":
			width = 750;height = "500";
			scrollbars="scrollbars=1";bGiveFocus=true;
			break;
		default:
			scrollbars="scrollbars=1";
			width = 800;height = 600;
	}
	// is this a testlinc window
	if(lUrl.indexOf("tlframeset.asp") != -1){
		scrollbars="scrollbars=1";width = 800;height = 600;bGiveFocus=true;
	}
	//popupWin=window.open(url, "help", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width="+width+",height="+height);
	popupWin=window.open(url, name, "toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=1,"+scrollbars+",width="+width+",height="+height);
	if(-1 == navigator.appVersion.indexOf("MSIE 4.0") && bGiveFocus)
		if(!popupWin.closed){
			popupWin.focus();
		}

}
function IsBrowserOK()
{
	// *************************************
	// test the browser
	// *************************************
	if(parseInt(navigator.appVersion) >= 4)
	{
		if (navigator.appName.indexOf("Microsoft") != -1 &&
			navigator.appVersion.indexOf("MSIE 4.0") != -1                                                    )
		{
			var minorver=""+clientInformation.appMinorVersion;
			if (minorver.indexOf("SP")!=-1)
			{
				if (parseFloat(navigator.appVersion) >= 4)
				{
					return true;
				}
			}
			else
			{
				// its bad - we have ie4 without the service pack
				return false;
			}
		}
		else
		{
			// 4.x version of Netscape is ok
			// and any IE > 4 is also ok
			return true;
		}
	}

	return false;
}

// for help debugging .. dump the properties of an object
function showProps(o) 
{
	var result = ""
	count = 0
	for (var i in o) 
	{
		result += o + "." + i + "=" + o[i] + "\n"
		count++
		if (count == 25) 
		{
			alert(result)
			result = ""
			count = 0
		}
	}
	alert(result)
}

// Convert object name string or object reference
// into a valid object reference
function getObject(obj) 
{
	var theObj
	if (typeof obj == "string") 
	{
		if(isM)
			theObj = eval("document.all." + obj + ".style")
		else {
			if(isN6){
				// alert("NS6 getting object "+obj);
				theObj = eval("document.getElementById('" + obj + "');")
				// showProps(theObj);
			}
			else
				theObj = eval("document." + obj)
		}
	} 
	else 
	{
		theObj = obj
	}
	return theObj
}

// Setting the visibility of an object to visible
function show(obj) 
{
	var theObj = getObject(obj)
	if(isN6)
		theObj.style.visibility = "visible"
	else
		theObj.visibility = "visible"
}

// Setting the visibility of an object to hidden
function hide(obj) 
{
	var theObj = getObject(obj)
	if(isN6)
		theObj.style.visibility = "hidden"
	else
		theObj.visibility = "hidden"
}
// Setting the z-order of an object
function setZIndex(obj, zOrder) 
{
	var theObj = getObject(obj)
	theObj.zIndex = zOrder
}
// set the new frame text
function setFrameText(obj, text)
{
	var theObj
	if (typeof obj == "string") 
	{
		if(isM)
			theObj = eval("document.all." + obj)
		if(isN)
			theObj = eval("document." + obj)
		if(isN6)
			theObj = eval("document.getElementById('" + obj + "');")
	} 
	else 
	{
		theObj = obj
	}
	if(isM||isN6)
	{
		theObj.innerHTML = text
	}
	if(isN)
	{
		theObj.document.write(text)
		theObj.document.close()
	}
}

// Utility function returns rendered height of object content in pixels
function getObjHeight(obj) {
	if (isN) {
		return obj.clip.height
	} else {
		return obj.clientHeight
	}
}
// Utility function returns rendered width of object content in pixels
function getObjWidth(obj) {
	if (isN) {
		return obj.clip.width
	} else {
		return obj.clientWidth
	}
}
// Utility function returns the available content width space in browser window
function getInsideWindowWidth() {
	if (isN||isN6) {
		return window.innerWidth
	} else {
		return document.body.clientWidth
	}
}
// Utility function returns the available content height space in browser window
function getInsideWindowHeight() {
	if (isN) {
		return window.innerHeight
	} else {
		return document.body.clientHeight
	}
}
// Utility function to position an element at a specific x,y location
function shiftTo(obj, x, y) {
	if (isN) {
		obj.moveTo(x,y)
	} else {
		obj.pixelLeft = x
		obj.pixelTop = y
	}
}

// Center an element named banner in the current window/frame, and show it
function centerIt(name) {

	var obj, contentObj
	if(isN){
		obj = eval("document." + name )
		contentObj = eval("document." + name)
	}
	if(isM){
		obj = eval("document.all." + name + ".style")
		contentObj = eval("document.all." + name)
	}
	if(isN6){
		obj = eval("document.getElementById('" + name + "');")
		obj = obj.style
		contentObj = obj
	}
	var x = Math.round((getInsideWindowWidth()/2) - (getObjWidth(contentObj)/2))
	var y = Math.round((getInsideWindowHeight()/2) - (getObjHeight(contentObj)/2))
	shiftTo(obj, x, y)
	obj.visibility = "visible"
}

function getClientVersion()
{
	var bRetValue = "";
	var bPlayerInstalled = false;
	var bOlderClientInstalled = false;
	if(isM && bIEClientInstalled)
		return IEversionOfLLClient;

	// for Netscape check the mimeType ..
	if(navigator.appName == "Netscape")
	{
		for(var i=0; i < navigator.mimeTypes.length; i++)
		{
			var desc = navigator.mimeTypes[i].description.toLowerCase()

			if(desc.indexOf("learnlincclient") >=0)
			{
				//alert("type: " + navigator.mimeTypes[i].type + 
				//	"\ndescription: '" + navigator.mimeTypes[i].description + 
				//	"'\nextension: " + navigator.suffixes)
				bRetValue = navigator.mimeTypes[i].description.slice(15);
			}
			if(desc.indexOf("llclient file") >=0)
			{
				bOlderClientInstalled = true;
			}
			if(desc.indexOf("learnlinc recorded classroom") >=0)
			{
				bPlayerInstalled = true;
				//alert("type: " + navigator.mimeTypes[i].type + 
				//	"\ndescription: '" + navigator.mimeTypes[i].description + 
				//	"'\nextension: " + navigator.suffixes)
			}
		}
		// if we did not get a 5x version then 
		if(bRetValue == "")
		{
			// it could be even older than 4.0 but thats ok
			if(bOlderClientInstalled)
				bRetValue = "4.0.0.239"
			if(bPlayerInstalled && bOlderClientInstalled)
				bRetValue = "4.5.0.321"
			
		}

	}
	else
	{
		if(document.cookie != "" && document.cookie.indexOf("LearnLincVersion=") != -1)
		{
			var version = GetCookie("LearnLincVersion")
			if(null != version)
			{
				bRetValue = version;
			}
		}
		else
		{
			if(acceptTypes.indexOf("LL4.") != -1)
			{
				var start = acceptTypes.indexOf("LL4.")
				var end = acceptTypes.indexOf(",", start)
				if(end == -1)
					bRetValue = acceptTypes.slice(start + 2);
				else
					bRetValue = acceptTypes.slice(start + 2, end);
				
			}
			if(acceptTypes.indexOf("LL5.") != -1)
			{
				var start = acceptTypes.indexOf("LL5.")
				var end = acceptTypes.indexOf(",", start)
				if(end == -1)
					bRetValue = acceptTypes.slice(start + 2);
				else
					bRetValue = acceptTypes.slice(start + 2, end);
				
			}
		}
	}

	return bRetValue;
}

function confirmDownload(str){
	if(confirm(str)){
		location = "cdownld.asp"
	} else {
		location = "chome.asp?TestClient=false"
	}

}

// try to detect if the Correct version of the 
// LearnLinc client is installed
function clientTest(str)
{

	// cant detect the client from Netscape 6
	if(isN6)
		return;

	if(isM && !bIEClientInstalled)
		confirmDownload(str);
		// location = "cdownld.asp?reason=NoClient";

	var message = ""
	var version = getClientVersion();
	var clientVersion = version;
	var serverVersionStr = serverVersion;

	if(isM)
		clientVersion=IEversionOfLLClient;

	if("" == version)
	{
		// no client detected
		confirmDownload(str);
		// location = "cdownld.asp?reason=NoClient"
	}
	else
	{
		// break the version strings up
		////////////////////////////////
		// Major version
		////////////////////////////////
		var dotPos = serverVersion.indexOf(".");
		var serverMajorH = parseInt(serverVersion.slice(0,dotPos));
		serverVersion = serverVersion.slice(dotPos + 1);

		dotPos = version.indexOf(".");
		var clientMajorH = parseInt(version.slice(0,dotPos));
		version = version.slice(dotPos + 1);

		dotPos = serverVersion.indexOf(".");
		var serverMajorL = parseInt(serverVersion.slice(0,dotPos));
		serverVersion = serverVersion.slice(dotPos + 1);

		dotPos = version.indexOf(".");
		var clientMajorL = parseInt(version.slice(0,dotPos));
		version = version.slice(dotPos +1);

		//////////////////////////////
		// Minor Version
		//////////////////////////////
		dotPos = serverVersion.indexOf(".");
		var serverMinorH = parseInt(serverVersion.slice(0,dotPos));
		serverVersion = serverVersion.slice(dotPos + 1);

		dotPos = version.indexOf(".");
		var clientMinorH = parseInt(version.slice(0,dotPos));
		version = version.slice(dotPos + 1);

		var serverMinorL = parseInt(serverVersion);

		var clientMinorL = parseInt(version);


		// the client is older than the server
		if(serverMajorH > clientMajorH ||
		  (serverMajorH == clientMajorH && serverMajorL > clientMajorL) ||
		  (serverMajorH == clientMajorH && serverMajorL == clientMajorL && serverMinorH > clientMinorH) ||
		  (serverMajorH == clientMajorH && serverMajorL == clientMajorL && serverMinorH == clientMinorH && serverMinorL > clientMinorL))
		{
			// wrong version of the client
			//location = "cdownld.asp?reason=WrongVer"
			confirmDownload(str);
		}
		else 
		{
			// correct version of the client
			// dont need to do anything
			//alert("client version is ok");
		}
	}
}

// Return the long name for the current day of the week (0=Sunday)
function WeekdayLongName(w)
{
	switch(w) {
	case 0:
		return "Sonntag";
	case 1:
		return "Montag";
	case 2:
		return "Dienstag";
	case 3:
		return "Mittwoch";
	case 4:
		return "Donnerstag";
	case 5:
		return "Freitag";
	default:
		return "Samstag";
	}
}


// Return the long name for the current month (0=January)
function MonthLongName(m)
{
	switch(m) {
	case 0:
		return "Januar";
	case 1:
		return "Februar";
	case 2:
		return "März";
	case 3:
		return "April";
	case 4:
		return "Mai";
	case 5:
		return "Juni";
	case 6:
		return "Juli";
	case 7:
		return "August";
	case 8:
		return "September";
	case 9:
		return "Oktober";
	case 10:
		return "November";
	default:
		return "Dezember";
	}
}

function GetLocalShortDate(year, month, day)
{

	var dateToReturn = "day.month.year";

	dateToReturn = dateToReturn.replace("month", month);
	dateToReturn = dateToReturn.replace("day", day);
	dateToReturn = dateToReturn.replace("year", year);

	return dateToReturn;
}

function GetLocalLongDate(year,month, day)
{
	var dateToReturn = "dayNum. monthName year";

	dateToReturn = dateToReturn.replace("monthNum", month);
	dateToReturn = dateToReturn.replace("monthName", MonthLongName(month));
	dateToReturn = dateToReturn.replace("dayNum", day);
	dateToReturn = dateToReturn.replace("dayName", WeekdayLongName(day));
	dateToReturn = dateToReturn.replace("year", year);

	return dateToReturn;
}
