// Globase LRM basic functionality
// This file is included on all pages
// Copyright 2001 Globase A/S
//
// Authors: Kim Jensen, Frank B. Daugaard
// Date created: 14.06.2001
// Date modified: 07.11.2001 (fbd)



// browsercheck...
var IE5 = false;
var IE55 = false;
var IE6 = false;
var IE7 = false;
var IE5plus = false;
var IEX = false;
var Mac = false;
var Win = false;


IE5 = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.indexOf('MSIE 5.0')>-1)) ? true : false;
IE55 = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.indexOf('MSIE 5.5')>-1)) ? true : false;
IE6 = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.indexOf('MSIE 6.0')>-1)) ? true : false;
IE7 = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.indexOf('MSIE 7.0')>-1)) ? true : false;
IE5plus = ((navigator.appName.indexOf('Microsoft')>-1) && (navigator.appVersion.slice(navigator.appVersion.indexOf('MSIE')+5,navigator.appVersion.indexOf('MSIE')+6)) >= 5) ? true : false;
IEX = (navigator.appName.indexOf('Microsoft')>-1) ? true : false;

Win = (navigator.platform.indexOf('Win32')>-1) ? true : false;
Mac = (navigator.platform.indexOf('Mac')>-1) ? true : false;

// include stylesheet...
if (parseInt(navigator.appVersion)>3) {
	if (Mac) {document.write('<link href="/css/mac.css" rel="stylesheet" type="text/css">');}
	if (Win) {document.write('<link href="/css/win.css" rel="stylesheet" type="text/css">');}
}

function doLegalNotice() {
	makePop(-1,-1,'LegalNoticeWindow','/legal_notice.asp','windowLegalNotice',400,450);
}

// kj 24.07.2001 - not used but kept here for history + reuse in the future...
//var leadinfosheetOn = false; // boolean to track if leadinfosheet popup is on or off... used in situations where we want to close the LIS from the main page...
//var userinfosheetOn = false; // boolean to track if userinfosheet popup is on or off... used in situations where we want to close the UIS from the main page...

function doLaunchInfobox() {
	makePop(-1,-1,'window_infobox','/info_login.asp','infoboxWindow',400,208);
}

function doInfoboxOn() {
//	theX = xsPos('infobox_beacon');
//	theY = ysPos('infobox_beacon');
//	moveLayerTo('infobox',theX-82,theY+15);
//	showLayer('infobox');
}

function doInfoboxOff() {
//	hideLayer('infobox');
}

function doHelp(theID) {
	theURL = '/help/new_uni_help_fset.asp?helpID='+theID; //NEW! Help is implemented so that help texts are loaded from database..
//	theURL = '/help/uni_help_fset.asp?helpID='+theID; //temporary disabled in order to guide user to index pane in help pop-up.
//	theURL = '/help/uni_help_fset.asp?helpID=show_index'; //temporary. read above. !!Return to this if not new help has finished implementation before release!!
	makePop(10,10,'systemhelp',theURL,'windowSystemHelp',400,450);
}

function doLogout() {
	document.location.href = '/logout.asp';
}

function doNothing() {
}

function getTimestamp() {
	tmp = new Date;
	tmp2 = tmp.getYear() + '' + tmp.getMonth() + '' + tmp.getDate() + '' + tmp.getHours() + '' + tmp.getMinutes() + '' + tmp.getSeconds();
	return tmp2;
}

// window.open function...
function makePop(windowPosX,windowPosY,windowName,url,name,w,h,extra){
	var titlebarHeight = 28; // titlebar + chrome at bottom on a standard IE5 pop-frame on a Win2kPro installa. // Used for correct centering of pop...

	// on the mac space for the scrollbar is added to the popup window size; this we correct here
	var Width, Height;
	if (Mac){Width = w-16; Height = h-16;}
	if (Win){Width = w; Height = h;}
	
	str="height="+Height+",width="+Width+","+extra;
	if(parseInt(navigator.appVersion)>3)
		if (windowPosX == -1) {
			str+=",left="+(screen.width -Width)/2+",top="+parseInt(((screen.height -Height)-titlebarHeight)/2);
		}
		else {
			str+=",left="+windowPosX+",top="+windowPosY;
		}
	eval(windowName + "=window.open('" + url + "','" + name + "','" + str + "')");
}

// shows a layer...
function showLayer(which) {
	document.all[which].style.visibility = 'visible';
	document.all[which].style.display = 'block';
}

// hides a layer...
function hideLayer(which) {
	document.all[which].style.visibility = 'hidden';
	document.all[which].style.display = 'none';
}

// moves a layer to a given position...
function moveLayerTo(theLayer,x,y) {
	document.all[theLayer].style.left = x;
	document.all[theLayer].style.top = y;
}

// writes to a layer...
function writeLayer(which, content) {
	document.all[which].innerHTML = content;
}


// Ask for image-position:
function ysPos(what){
	return yIE(eval('document.'+what));
}
function xsPos(what){
	return xIE(eval('document.'+what));
}
function yIE(what) {
	if(what.offsetParent) {
		return parseInt((what.offsetTop + yIE(what.offsetParent)));
	} else {
		return parseInt(what.offsetTop);
	}
}
function xIE(what) {
	if(what.offsetParent) {
		return parseInt((what.offsetLeft + xIE(what.offsetParent)));
	} else {
		return parseInt(what.offsetLeft);
	}
}
// ------


function doWarning(warningtext)	{
	if (confirm(warningtext)) {
		return true;
	}
	else {
		return false;
	}
}

function doToggleCheckbox(which) {
	theState = document.all[which].checked;
	if (theState) {
		document.all[which].checked = false;
	}
	else {
		document.all[which].checked = true;
	}
}	

function doToggleRadio(whichRadio,whichIndex) {
	document.form[whichRadio][whichIndex].checked = true;
}	

function doCloseLayer(which) {
	document.all[which].style.visibility = 'hidden';
}

function doToggleRow(theRow,theColor) {
	theColor = 'rowcolor_'+theColor;
	theCheckboxName = 'check_'+theRow.substring(3,theRow.length);
//	alert(theCheckboxName);
	doToggleCheckbox(theCheckboxName);
		if (document.all[theCheckboxName].checked == true) {
			document.all[theRow].className = theColor;
		}
		else {
			document.all[theRow].className = 'rowcolor_white';
		}
}

// launch the SaveBox (if existing in the document...)
function doSaveOk(theHeader,theBody,offsetImg,xOffset,yOffset,stayTime) {
	if (document.all.ok_box) {

		theHeader = (theHeader) ? theHeader=theHeader : theHeader='System Information';
		theBody = (theBody) ? theBody=theBody : theBody='The information has been saved successfully.';
		offsetImg = (offsetImg) ? offsetImg=offsetImg : offsetImg='zeropoint';
		xOffset = (xOffset) ? xOffset=xOffset : xOffset=false;
		yOffset = (yOffset) ? yOffset=yOffset : yOffset=250;
		stayTime = (stayTime) ? stayTime=stayTime : stayTime=2000;

		document.all.ok_box_header.innerHTML = theHeader;
		document.all.ok_box_body.innerHTML = theBody;
		theX = xsPos(offsetImg) + xOffset;
		theY = ysPos(offsetImg) + yOffset;
		if (!xOffset) { // if no xOffset is specified then center the layer...
			theX = Math.round((document.body.clientWidth/2) - (ok_box.clientWidth/2));
		}
		moveLayerTo('ok_box',theX,theY);
		showLayer('ok_box');
		setTimeout("hideLayer('ok_box')",stayTime);
	}
}

// launch the SaveBox2 (if existing in the document...)
function doSaveOk2(theHeader,theBody,offsetImg,xOffset,yOffset,stayTime) {
	if (document.all.ok_box) {

		theHeader = (theHeader) ? theHeader=theHeader : theHeader='System Information';
		theBody = (theBody) ? theBody=theBody : theBody='The information has been saved successfully.';
		offsetImg = (offsetImg) ? offsetImg=offsetImg : offsetImg='zeropoint';
		xOffset = (xOffset) ? xOffset=xOffset : xOffset=false;
		yOffset = (yOffset) ? yOffset=yOffset : yOffset=250;
		stayTime = (stayTime) ? stayTime=stayTime : stayTime=2000;

		document.all.ok_box_header.innerHTML = theHeader;
		document.all.ok_box_body.innerHTML = theBody;
		theX = 0 + xOffset;
		theY = 0 + yOffset;
		if (!xOffset) { // if no xOffset is specified then center the layer...
			theX = Math.round((document.body.clientWidth/2) - (ok_box.clientWidth/2));
		}
		moveLayerTo('ok_box',theX,theY);
		showLayer('ok_box');
		setTimeout("hideLayer('ok_box')",stayTime);
	}
}


// launch the WarningBox (if existing in the document...)
function doWarningBox(theHeader,theBody,offsetImg,xOffset,yOffset) {
	if (document.all.warning_box) {
		// check for header/body in passed vars... and write to layer if any strings are passed from function call..
		theHeader = (theHeader) ? theHeader=theHeader : theHeader='System Warning';
		theBody = (theBody) ? theBody=theBody : theBody='An error has occured.';
		document.all.warning_box_header.innerHTML = theHeader;
		document.all.warning_box_body.innerHTML = theBody;
		// check for offset Image in function call.. if no image is specified just default to zeropoint image (BE AWARE.. this image is not on the pops...so you need to specify another or name one "zeropoint")...
		offsetImg = (offsetImg) ? offsetImg=offsetImg : offsetImg='zeropoint';
		// check for offsetparameters in function call... if no special offset is specified just center on the z-axis and use a default y-offset...
		xOffset = (xOffset) ? xOffset=xOffset : xOffset=false;
		yOffset = (yOffset) ? yOffset=yOffset : yOffset=210;
		// sniff x,y from offsetImg and move the layer there.. and.. show the motherfucker.. :-)
		theX = xsPos(offsetImg) + xOffset;
		theY = ysPos(offsetImg) + yOffset;
		if (!xOffset) { // if no xOffset is specified then center the layer...
			theX = Math.round((document.body.clientWidth/2) - (warning_box.clientWidth/2));
		}
		moveLayerTo('warning_box',theX,theY);
		showLayer('warning_box');
	}
}

// launch the InfoBox (if existing in the document...)
function doInfoBox(theHeader,theBody,offsetImg,xOffset,yOffset) {
	if (document.all.info_box) {
		// check for header/body in passed vars... and write to layer if any strings are passed from function call..
		theHeader = (theHeader) ? theHeader=theHeader : theHeader='System Information';
		theBody = (theBody) ? theBody=theBody : theBody='This is a system information message.';
		document.all.info_box_header.innerHTML = theHeader;
		document.all.info_box_body.innerHTML = theBody;
		// check for offset Image in function call.. if no image is specified just default to zeropoint image (BE AWARE.. this image is not on the pops...so you need to specify another or name one "zeropoint")...
		offsetImg = (offsetImg) ? offsetImg=offsetImg : offsetImg='zeropoint';
		// check for offsetparameters in function call... if no special offset is specified just center on the z-axis and use a default y-offset...
		xOffset = (xOffset) ? xOffset=xOffset : xOffset=false;
		yOffset = (yOffset) ? yOffset=yOffset : yOffset=210;
		// sniff x,y from offsetImg and move the layer there.. and.. show the motherfucker.. :-)
		theX = xsPos(offsetImg) + xOffset;
		theY = ysPos(offsetImg) + yOffset;
		if (!xOffset) { // if no xOffset is specified then center the layer...
			theX = Math.round((document.body.clientWidth/2) - (info_box.clientWidth/2));
		}
		moveLayerTo('info_box',theX,theY);
		showLayer('info_box');
	}
}

function doAltTag(theBody,offsetImg) {


	if (document.all.alt_tag) {
		// check for header/body in passed vars... and write to layer if any strings are passed from function call..
		theBody = (theBody) ? theBody=theBody : theBody='This is an additional help message.';
		document.all.alt_tag_body.innerHTML = theBody;
		// check for offset Image in function call.. if no image is specified just default to zeropoint image (BE AWARE.. this image is not on the pops...so you need to specify another or name one "zeropoint")...
		offsetImg = (offsetImg) ? offsetImg=offsetImg : offsetImg='zeropoint';
		// sniff x,y from offsetImg and move the layer there..
		theX = xsPos(offsetImg) + 15;
		theY = ysPos(offsetImg) + 15;

// get the end positions of x, y
		endX = theX + 300;
//		endY = theY + alt_tag.offsetHeight;

		// ajust theX and theY if needed
		if (endX > document.body.clientWidth)
		{
			moveLeft = endX - document.body.clientWidth;
			theX = theX - moveLeft;
		}
/*
		if (endY > document.body.clientHeight)
		{
			moveUp = endY - document.body.clientHeight;
			theY = theY - moveUp;
		}
*/
		moveLayerTo('alt_tag',theX,theY);
		showLayer('alt_tag');
	}
}

// VALIDATION SHIT...
var defaultEmptyOK = false
var whitespace = " \t\n\r";

// Check whether string s is empty.
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function doLaunchChooseLetterTemplate(parentURL) {
	gotoURL = "/globase_guides/choose_letter_template/step1.asp?parentURL=" + parentURL;
	makePop(-1,-1,'chooseLetterTemplate',gotoURL,'windowChooseTemplate',600,350);
}


