// ##################################################
// ##                                              ##
// ##  Basale JavaScript Funktioner                ##
// ##  Copyright 2003-2004 BI Holding A/S          ##
// ##                                              ##
// ##  Oprettet        : 03.09.2003                ##
// ##  Oprettet af     : FBD, Kommunikation        ##
// ##                                              ##
// ##  Sidst ændret    : 13.12.2004                ##
// ##  Sidst ændret af : SFS, Webudvikling         ##
// ##                                              ##
// ##  Fejlhåndtering  : udviklet af PLU           ##
// ##################################################


// browsercheck...
var IE5 = false;
var IE55 = false;
var IE6 = 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;
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;


function doNothing() {
}

function getTimestamp() {
	tmp = new Date;
	strTime = tmp.getYear() + '' + tmp.getMonth() + '' + tmp.getDate() + '' + tmp.getHours() + '' + tmp.getMinutes() + '' + tmp.getSeconds();
	return strTime;
}

function handleError(wherefrom){
	makePop(-1,-1,'errorWin','errormessage.aspx?wherefrom='+wherefrom,'popError',400,250,'scrollbars=yes,titlebar=no');
}

// i xhtml 1.1 strict er target på a ikke tilladt - i stedet bruges denne simple function
function doBlank(url){
	makePop(-1,-1,'blank',url,'_blank',600,400,'scrollbars=yes,titlebar=yes,resizable,toolbar=yes,menubar=yes,location=yes,status=yes');
}

// Login
function popLogin(email){
	url = (email!=null) ? "login.aspx?email=" + email : "login.aspx";
	makePop(-1,-1,'BankInvest',url,'Login',500,400,'scrollbars=no,titlebar=no');
}

// Print Guide
function popPrintGuide(){
	makePop(-1,-1,'BankInvest','popup.aspx?pageid=1064','PrintGuide',600,350,'scrollbars=yes,titlebar=no');
}

// Ansvarsfraskrivelse
function popDisclaimer(){
	makePop(-1,-1,'BankInvest','popup.aspx?pageid=1358','Disclaimer',600,350,'scrollbars=yes,titlebar=no');
}

// Behandling af personoplysninger
function popPersonoplysninger(){
	makePop(-1,-1,'BankInvest','popup.aspx?pageid=809','Personoplysninger',600,350,'scrollbars=yes,titlebar=no');
}

// Ordbog
function popHelp(article){
	var lookup = (article!=null) ? "#" + article : "";
	makePop(-1,-1,'BankInvest','popup.aspx?pageid=808'+lookup,'popDictionary',512,450,'scrollbars=yes,titlebar=no');
}


// pageID mangler på url'en
function popQuestion(q){
	var lookup = (q!=0) ? "&q=" + q : "";
	makePop(-1,-1,'qaWin','popup.aspx?modul=faq'+lookup,'popQuestion',600,350,'scrollbars=yes,titlebar=no');
}


// pageID mangler på url'en
function popPage(which){
	makePop(-1,-1,'popWin','popup.aspx?modul='+which,'popWindow',600,350,'scrollbars=yes,titlebar=no');
}


// pageID mangler på url'en
function popImage(imgLocation,imgName){
	makePop(100,100,'imgWin','popup.aspx?modul=image&img='+imgLocation+'&name='+imgName,'imgWindow',50,50);
}


// 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 + "')");
}

// window.open function... Vidue åbnes som modal
function makeModalPop(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.showModalDialog('" + url + "','" + name + "','" + str + "')");
}

// close popup and focus opener
function closepopup(){
	window.close();
	opener.focus();
}


// shows a layer...
function showLayer(which) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	objA.style.visibility = 'visible';
}

// hides a layer...
function hideLayer(which) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	objA.style.visibility = 'hidden';
}

// displays a layer...
function displayLayer(which) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	objA.style.display = 'block';
}

// hides a layer...
function removeLayer(which) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	objA.style.display = 'none';
}

// moves a layer to a given position...
function moveLayerTo(which,x,y) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	objA.style.left = x;
	objA.style.top = y;
}

// writes to a layer...
function writeLayer(which, content) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	objA.innerHTML = content;
}

function doCloseLayer(which) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	objA.style.visibility = 'hidden';
}

function doToggleRow(theRow,theColor) {
	var objA = (document.all) ? document.all[theRow] : document.getElementById(theRow) ;
	theColor = 'rowcolor_'+theColor;
	theCheckboxName = 'check_'+theRow.substring(3,theRow.length);
	doToggleCheckbox(theCheckboxName);
	if (document.forms[0][theCheckboxName].checked == true) {
		objA.className = theColor;
	}
	else {
		objA.className = 'rowcolor_white';
	}
}

// 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) {
	var objA = (document.all) ? document.all[which] : document.getElementById(which) ;
	theState = objA.checked;
	if (theState) {
		objA.checked = false;
	}
	else {
		objA.checked = true;
	}
}	

function doToggleRadio(whichRadio,whichIndex) {
	document.forms[0][whichRadio][whichIndex].checked = true;
}	

// placér markør i tekst-indtastnings-felt
function doFocus(which){
	document.forms[0][which].focus();
}

// VALIDATION STUFF...
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;
}

var suppressErrorMessage = true; // Denne variable sættes til false når man ønsker at se fejlene i browser.
var showErrorMessage = false;    // Denne variable sættes til true, hvis der skal vises fejlbeskeder ved clientside fejl.

// Denne funktion åbner en ny usynlig iframe med den givne url.
// dette bruges primært til at give serveren besked om hændelser på klientsiden.
var iframe_counter = 0;
function openInNewIFrame(url) {
	var myiframe;
	var name = 'NewIFrameNumber' + iframe_counter;
			
	myiframe = document.createElement('iframe');
	myiframe.id = name;
	myiframe.src = url;
	myiframe.width = 0;
	myiframe.height = 0;
	document.body.appendChild(myiframe);
	
	iframe_counter = iframe_counter + 1;
}

// Denne funktion URL encoder en almindelig tekst.
function URLEncode(plaintext)
{
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} 
	return encoded;
}

// Denne funktion håndterer selve fejl.
function errorHandler(desc,page,line,chr) {
		// Brug openInNewIFrame('/DK/Error/ErrorHandling.aspx?ClientError=True&Desc=' + URLEncode(desc) + '&Page=' + URLEncode(page) + '&Line=' + line); på udvikler maskiner
		openInNewIFrame('/DK/Error/ErrorHandling.aspx?ClientError=True&Desc=' + URLEncode(desc) + '&Page=' + URLEncode(page) + '&Line=' + line);

		if (showErrorMessage) {
			alert("Der er sket en fejl, den ansvarlige er blevet adviseret og vil udbedre problemmet hurtigst muligt");
		}

		return suppressErrorMessage;
}
window.onerror = errorHandler;