var db_id = 0;
var DB_LOAN_NEW = 1;
var DB_ENVIRON = 2;
var DB_EQUITY = 3;
var DB_RESCHEDULE = 4;
var DB_EXPENSES = 5;
var DB_QUOTATIONS = 6;
var LAST_TYPE=0;

//allow js to add functions to window.onload
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
//added g to name, dont want to redefine the $( if its already defined on client site

function $g(str_id){
	return document.getElementById(str_id);
}
//globals for browser detection
var detect = navigator.userAgent.toLowerCase();
//alert(detect);
var OS,browser,version,total,thestring;

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/
function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 	= "Opera"
	else if (checkIt('webtv')) browser 	= "WebTV";
	else if (checkIt('icab')) browser 	= "iCab"
	else if (checkIt('msie')) browser 	= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 	= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS = "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}


function activateBox(db_type){
	//cleanup after last activate
	if(LAST_TYPE > 0 && LAST_TYPE != db_type) if ($g('tkcontainer')!=null) $g('tkoverlaycontent').removeChild($g('tkcontainer'));
	LAST_TYPE=db_type;
	
	if (browser == 'Internet Explorer'){
		getScroll();
		prepareIE('100%', 'auto');
		setScroll(0,0);
		hideSelects();
	}
	if ($g('tkcontainer')==null){
		
		var host = 'http://www.totalkredit.dk';
		var base_url = '/TKdk/page?action=[action]&extid=[id]&refresh='+Math.floor(Math.random()*10000);
		var id = db_id > 0 ? db_id : 0;
		var action = "", img = "";
		switch (db_type) {
		case DB_LOAN_NEW:
			action = "loan_new";
			img = "http://banner.totalkredit.swdc.dk/calc/images/newloan.png";
			break;
		case DB_ENVIRON:
			action = "ne.calculation";
			img = "http://banner.totalkredit.swdc.dk/calc/images/mb_header.png";
			break;
		case DB_QUOTATIONS:
			action = "quotations";
			img = "http://banner.totalkredit.swdc.dk/calc/images/quotations.png";
			break;
		}
		
		var url = host + base_url.replace("[action]",action).replace("[id]", id);
		
		//$g('tkoverlaycontent').appendChild(createIFrame('tkcontainer',url,510,3700));
		
		//make div with loader
		
		var loaderimg=createImg('tkloadimg','http://banner.totalkredit.swdc.dk/calc/images/loader.gif',16,16,'margin-left:247px;margin-top:18px');
		var loaderstyle="width:100%;height:60px;float:left;background-color:white";
		var loaderdiv=createDiv('tkloader',loaderstyle,loaderimg);
		$g('tkoverlaycontent').appendChild(loaderdiv);
		$g('tkoverlaycontent').appendChild(createIFrame('tkcontainer',url,510,3700));
		var loaderonload=function(){var e = document.getElementById('tkloader'); e.parentNode.removeChild(e);};
		$g('tkcontainer').onload=loaderonload;
		setTimeout('removeLoader()',6000);		

		$g('tkmblogoimg').src = img;
	}
	$g('tkoverlay').style.display = 'block';
	$g('tkoverlaycontent').style.display = 'block';
	
	return false;
}
function removeLoader(){
	if ($g('tkloader')){
		var e = document.getElementById('tkloader'); 
		e.parentNode.removeChild(e);
	}
}

function deactivateBox(){
	if (browser == 'Internet Explorer'){
		setScroll(0,yPos);
		prepareIE('auto', 'auto');
		showSelects();
	}
	$g('tkoverlay').style.display = 'none';
	$g('tkoverlaycontent').style.display = 'none';
}
function doScroll(){
	$g('tkclose').style.top=getScrollPos()+'px';
	
}

function updatePosition(){
	
	var contentwidth = 510;
	var startpos = (document.documentElement.clientWidth-contentwidth)/2;
	var closepos=startpos+contentwidth-5;
	$g('tkoverlaycontent').style.left=startpos+'px';
	$g('tkclose').style.left=closepos+'px';
}
function addOverlay() {
	//function to build the dom needed for overlay(s)
	var bod = document.getElementsByTagName('body')[0];
	var contentwidth = 510;
	var w = document.body.clientWidth || document.documentElement.clientWidth;
	var startpos = (w-contentwidth)/2;
	
	//styles
	var ostyle = 'display:none;top:0;left:0;width:100%;z-index:1000;background-color:#333;-moz-opacity: 0.8;opacity:.80;filter: alpha(opacity=80)';
	var ocstyle = 'color: #333;position:absolute;display: none;top: 25px;left: '+startpos+'px;width: 510px;text-align: left;z-index:1001;overflow: hidden;';
	var closepos=startpos+contentwidth-5;
	var closestyle='z-index:1002;background-image: url(http://banner.totalkredit.swdc.dk/calc/images/mb_flap.png);top:25px;left:'+closepos+'px;cursor:pointer;width:149px;height:98px;-moz-opacity: 1;opacity:1;';
	var logostyler='';	

	//Special case for IE6
	if (checkIt('msie 6.0') || (document.compatMode == "BackCompat" && checkIt('msie 7.0'))) {
		ostyle+=';height:3800px;position:absolute';
		closestyle+=';position:absolute';
		logostyler=';margin-left:-3px;';
	}else{
		ostyle+=';position:fixed;height:100%';
		closestyle+=';position:fixed';
	}
	
	var overlay = createDiv('tkoverlay',ostyle,null);
	var overlaycontents = createDiv('tkoverlaycontent',ocstyle,null);
	var toplink =createLink('tkcloselink','#tkoverlaycontent','Til toppen','font-family:Verdana;font-size:12px;font-weight:bold;color:#000000;margin-left:10px',null);
	
	//dom for fixed div in right corner
	var closelink = createLink('tkcloselink','#','Luk dette vindue','font-family:Verdana;font-size:12px;font-weight:bold;color:#000000;margin-left:10px',null);
	var closebutton = createDiv('tkclose',closestyle,null);
	closebutton.appendChild(document.createElement('br'));
	closebutton.appendChild(closelink);
	closebutton.appendChild(document.createElement('br'));
	closebutton.appendChild(toplink);
	closelink.onclick = function(){deactivateBox();return false;};
	
	
	//extra dom for logos 
	var tklogodiv = createDiv('tkmblogo','float:left;width:'+contentwidth+'px;background-color:white;height:113px'+logostyler,null);
	var imglogo=createImg('tkmblogoimg','http://banner.totalkredit.swdc.dk/calc/images/mb_header.png',510,113,'float:left');	
	tklogodiv.appendChild(imglogo);
	
	//attach to body
	bod.appendChild(overlay);
	bod.appendChild(overlaycontents);
	overlay.appendChild(closebutton);
	overlaycontents.appendChild(tklogodiv);
}


function hideSelects(){
	var selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) selects[i].style.visibility = 'hidden';
}

function showSelects(){
	var selects = document.getElementsByTagName('select');
	for(i = 0; i < selects.length; i++) selects[i].style.visibility = 'visible';
}

var yPos = 0;

function getScroll(){
	yPos=getScrollPos();
}

function getScrollPos(){
	var temp;
	if (self.pageYOffset) {
		temp = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){
		temp = document.documentElement.scrollTop; 
	} else if (document.body) {
		temp= document.body.scrollTop;
	}
	return temp;
}
	
function setScroll(x, y){
	window.scrollTo(x, y); 
}

//DOM factories, more or less specialized for their intended use
function createDiv(id,style,childnode){
	var thediv=document.createElement('div');
	thediv.id=id;
	thediv.style.cssText=style;
	if (childnode!=null) thediv.appendChild(childnode);
	return thediv;
}
function createIFrame(id,url,width,height){
	var theframe=document.createElement('iframe');
	theframe.id=id;
	if (width!=null) theframe.width=width+'px';
	if (height!=null) theframe.height=height+'px';
	if (url!=null) theframe.src=url;
	theframe.name=id;
	theframe.allowTransparency=true;
	theframe.frameBorder=0;
	theframe.scrolling='no';
	return theframe;
}
function createLink(id,url,txt,styletxt,target){
	var thelink=document.createElement('a');
	thelink.id=id;
	thelink.href=url;
	
	if (styletxt!=null) thelink.style.cssText=styletxt;
	thelink.innerHTML=txt;
	if (target!=null) thelink.target=target;
	return thelink;
}
function createImg(id,src,width,height,styletxt){
	var theimg=document.createElement('img');
	theimg.id=id;
	theimg.src=src;
	theimg.width=width;
	theimg.height=height;
	if (styletxt!=null) theimg.style.cssText=styletxt;
	return theimg;
}


//prepare IE
function prepareIE(height, overflow){
	bod = document.getElementsByTagName('body')[0];
	bod.style.height = height;
	bod.style.overflow = overflow;
  
	htm = document.getElementsByTagName('html')[0];
	htm.style.height = height;
	htm.style.overflow = overflow; 
}


//attaches itself to window onload
addLoadEvent(getBrowserInfo);
addLoadEvent(addOverlay);
