/*
   Copyright © 2005 CSM Online HB
*/

// init global variables
var oOnLoad = new Array();

// init events
window.onload = onLoad;
window.onresize = onRezise;
// document.oncontextmenu = function() { return false; }

String.prototype.trim = strTrim;

// functions

function strTrim() {
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

function onLoad()
{
	oLink = document.getElementsByTagName('a');
	for (var i = 0; i < oLink.length; i++)
	{
		oLink[i].onfocus = function() { this.blur(); return true; }
		if (oLink[i].title.length > 0)
		{
			oLink[i].onmouseover = function() { window.status = this.title; return true; }
			oLink[i].onmouseout = function() { window.status = ''; return true; }
		}
	}

	oInput = document.getElementsByTagName('input');
	for (var i = 0; i < oInput.length; i++)
	{
		if (oInput[i].className == 'button')
		{
			oInput[i].onmouseover = function() { this.style.color = '#EEEEEE'; return true; }
			oInput[i].onmouseout = function() { this.style.color = '#FFFFFF'; return true; }
			oInput[i].onfocus = function() { this.blur(); return true; }
		}
	}

	oForm = document.getElementsByTagName('form');
	for (var i = 0; i < oForm.length; i++)
	{
		oForm[i].onsubmitold = oForm[i].onsubmit;
		oForm[i].onsubmit = function() { disableButtons(this); if (this.onsubmitold != null) return this.onsubmitold(); return true; }
	}

	for (i = 0; i < oOnLoad.length; i++)
	{
		oOnLoad[i]();
	}

	onRezise();

	var oUsername = document.getElementById('login_username');
	var oPassword = document.getElementById('login_password');
	if (oUsername != null && oPassword != null)
	{
		var sUsername = getCookie('login');
		if (sUsername != null)
		{
			oUsername.value = sUsername;
			oPassword.focus();
		}
		else
		{
			oUsername.focus();
		}
	}
}

function onRezise()
{
	iFrame = document.documentElement.clientHeight;
	iHeader = document.getElementById('header').clientHeight;
	iFooter = document.getElementById('footer_container').clientHeight;

	document.getElementById('content').style.display = 'block';
	iContainer = document.getElementById('container').clientHeight;

	if (iContainer < (iFrame - iHeader - iFooter - 1))
	{
		document.getElementById('container').style.height = (iFrame - iHeader - iFooter - 1) + 'px';
	}
	else
	{
		document.getElementById('container').style.height = (iContainer) + 'px';
	}

	document.getElementById('container').style.visibility = 'visible';
	document.getElementById('footer_container').style.visibility = 'visible';
}

function addOnLoad(func)
{
	oOnLoad[oOnLoad.length] = func;
}

function leadingZero(iNr)
{
	if (iNr < 10) iNr = '0' + iNr;
	return iNr;
}

function getQueryVar(sVar)
{
	var oVars = window.location.search.substring(1).split('&');
	for (var i = 0; i < oVars.length; i++)
	{
		var oPair = oVars[i].split('=');
		if (oPair[0] == sVar)
		{
			return (oPair.length > 1) ? oPair[1] : false;
		}
	}
	return null;
}

function disableButtons(oForm)
{
	oButtons = oForm.getElementsByTagName('input');
	for (var i = 0; i < oButtons.length; i++)
	{
		if (oButtons[i].type == 'submit' || oButtons[i].type == 'button')
		{
			window.status = 'Loading...';
			oButtons[i].disabled = true;
		}
	}
}


function setCookie(sName, sValue, oExpires)
{
    document.cookie = sName + "=" + escape(sValue) + "; expires=" + oExpires.toGMTString() + "; path=/; domain=cs-manager.com";
}

function getCookie(sName)
{
    var dc = document.cookie;
    var prefix = sName + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = dc.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function launchWindow(sUrl, iWidth, iHeight)
{
	if (iWidth + 10 > screen.availWidth)
		iWidth = screen.availWidth - 10;

	if (iHeight + 59 > screen.availHeight)
		iHeight = screen.availHeight - 59;

	var iLeft = (screen.availWidth - iWidth - 10) / 2;
	var iTop = (screen.availHeight - iHeight - 59) / 2;

	if (sUrl.indexOf('?', 0) > 0)
		sUrl += '&browser=' + ((BrowserDetect.getBrowser() == 'Explorer') ? 'ie' : 'other');

	var oWin = window.open(sUrl, 'csm', 'height=' + iHeight + ', width=' + iWidth + ', left=' + iLeft + ', top=' + iTop + ', location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=no');
	if (!oWin) {
		alert('Your browser blocked the popup you tried to open!');
		return;
	}

	if (oWin.opener == null)
		oWin.opener = window;

	oWin.moveTo(iLeft, iTop);
	oWin.resizeTo(iWidth + 10, iHeight + 59);
	oWin.focus();
}

var BrowserDetect = {
	getOS: function () {
		return this.searchString(this.dataOS) || "unknown";
	},
	getResolution: function () {
		return screen.width + 'x' + screen.height;
	},
	getBrowser: function () {
		return this.searchString(this.dataBrowser) || "unknown";
	},
	getVersion: function () {
		return this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "unknown";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{	// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 	// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};

