
var JSBalloonPath="/dgtech/JSBalloon/";

function addEvent(obj, evType, fn){  
	if (obj.addEventListener){    
		obj.addEventListener(evType, fn, true);    return true;  
	} 
	else if (obj.attachEvent){    
		var r = obj.attachEvent('on'+evType, fn);    return r;  
	} else {    
		return false;  
	} 
}

function setCropIcon(icon, img) {    
	var aTag = img;      
	var leftPos = 0;      
	var topPos = 0;     
	do { 	
		aTag = aTag.offsetParent; 	
		leftPos += aTag.offsetLeft; 	
		topPos += aTag.offsetTop;     } 
	while (aTag.tagName != 'BODY');    
	icon.style.display = 'block';     
	icon.style.top = topPos;     
	icon.style.left = leftPos + img.width - 20; 
}

function setPopIcon(icon, img) {    
	var aTag = img;      
	var leftPos = 0;      
	var topPos = 0;     
	do { 	
		aTag = aTag.offsetParent; 	
		leftPos += aTag.offsetLeft; 	
		topPos += aTag.offsetTop;     } 
	while (aTag.tagName != 'BODY');    
	icon.style.display = 'none';     
	icon.style.position = 'absolute';
	icon.style.top = topPos;     
	icon.style.left = leftPos + 250 ; 
}

function openCropImage(query) {  	
	window.open('/Tools/CropImage.aspx?' + query, '', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=800,height=600'); 
}  

function myOnLoad() {
	if (document.all.cropicon) {    
		if (document.all.cropicon.length) {	    
			for (i = 0; i < document.all.cropimage.length; i++)		    
				setCropIcon(document.all.cropicon[i], document.all.cropimage[i]);   
		} 
		else if (document.all.cropicon)	    
			setCropIcon(document.all.cropicon, document.all.cropimage);
	}
}

addEvent(window, 'load', myOnLoad);

function myPopOnLoad() {
	var imgs,i, b;
	imgs=document.getElementsByTagName('img');
	for(i in imgs)
	{
		if(/help/.test(imgs[i].id))
		{
			if (document.all[imgs[i].id.substring(5)]) {
				var p = document.all[imgs[i].id.substring(5)];
				setPopIcon(imgs[i], p);
			}
		}
	}
}

//addEvent(window, 'load', myPopOnLoad);

function showHelp(img) {
	var imgs,i, b;
	imgs=document.getElementsByTagName('img');
	for(i in imgs)
	{
		if(/help/.test(imgs[i].id))
		{
			if (imgs[i].style.display == 'none')
				imgs[i].style.display = 'block';
			else
				imgs[i].style.display = 'none';
			b = (imgs[i].style.display == 'none');
		}
	}
	if (b)
		img.alt = 'Vis hjelp';
	else
		img.alt = 'Skjul hjelp';
};

// Rendering functions
function balloonInfrastructure(body, direction)
{
	var ret;

	switch(direction)
	{
		case 'SE':
			// South East	
			ret ='<table class="JSBalloon" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" >'+
				'  <tr>'+
				'    <td height="1" width="10">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftTop.gif" width="10" height="10"></td>'+
				'    <td height="7" width=100% style="border-top:1px solid #999999; border-left-width:1; border-right-width:1; border-bottom-width:1; background-color:#FFFFEA" colspan="4"></td>'+
				'    <td height="7"  width="10">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightTop.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td valign=top colspan="6" style="border-left: 1px solid #999999; border-right: 1px solid #999999; background-color: #FFFFEA">'+
				body +
				'    </td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td width="10" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftBottom.gif" width="10" height="10"></td>'+
				'    <td height="7" style="background-color: #FFFFEA" colspan="4" width="280"></td>'+
				'    <td width="10" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightBottom.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td width="10" height="10"></td>'+
				'    <td width="1" style="border-top: 1px solid #999999; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1" height="10"></td>'+
				'    <td  height="10">'+
				'    <img border="0" src="'+JSBalloonPath+'images/aSouthEast.gif" width="67" height="18"></td>'+
				'    <td width=100% height="10" style="border-top: 1px solid #999999; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1"></td>'+
				'    <td width="70" height="10" style="border-top: 1px solid #999999; padding-left: 4; padding-right: 4; padding-top: 1; padding-bottom: 1"></td>'+
				'    <td width="10" height="10"></td>'+
				'  </tr>'+
				'</table>'
				break;

		case 'SW':					
			// South West
			ret ='<table class="JSBalloon" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1" >'+
				'  <tr>'+
				'    <td height="1" width="10">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftTop.gif" width="10" height="10"></td>'+
				'    <td height="7" width=179 style="border-top:1px solid #999999; border-left-width:1; border-right-width:1; border-bottom-width:1; background-color:#FFFFEA" colspan="4"></td>'+
				'    <td height="7"  width="11">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightTop.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td valign=top colspan="6" style="border-left: 1px solid #999999; border-right: 1px solid #999999;  background-color: #FFFFEA"">'+
				body +
				'    </td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td width="10" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftBottom.gif" width="10" height="10"></td>'+
				'    <td height="7" style="background-color: #FFFFEA" colspan="4" width="179"></td>'+
				'    <td width="11" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightBottom.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td width="10" height="10"></td>'+
				'    <td width="70" style="border-top: 1px solid #999999; border-left-width:1; border-right-width:1; border-bottom-width:1" height="10"></td>'+
				'    <td  height="10" style="border-left-width: 1; border-right-width: 1; border-top: 1px solid #999999; border-bottom-width: 1" width="100%">'+
				'    </td>'+
				'    <td  align="right">'+
				'    <img border="0" src="'+JSBalloonPath+'images/aSouthWest.gif" width="67" height="18"></td>'+
				'    <td width="1" height="10" style="border-top: 1px solid #999999;"></td>'+
				'    <td width="10" height="10"></td>'+
				'  </tr>'+
				'</table>'
				break;

		case 'NE':	
				// North East
				ret ='<table class="JSBalloon" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1" >'+
				'   <tr>'+
				'    <td width="10" height="9"></td>'+
				'    <td width="1" style="border-bottom:1px solid #999999; " height="9"></td>'+
				'    <td  height="9" valign="bottom">'+
				'    <img border="0" src="'+JSBalloonPath+'images/aNorthEast.gif" width="67" height="18"></td>'+
				'    <td width=100% height="9" style="border-bottom:1px solid #999999; "></td>'+
				'    <td width="70" height="9" style="border-bottom:1px solid #999999;"></td>'+
				'    <td width="10" height="9"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td height="1" width="10">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftTop.gif" width="10" height="10"></td>'+
				'    <td height="7" width=100% colspan="4" bgcolor="#FFFFEA"></td>'+
				'    <td height="7"  width="10">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightTop.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td valign=top colspan="6" style="border-left: 1px solid #999999; border-right: 1px solid #999999; background-color: #FFFFEA">'+
				body +
				'    </td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td width="10" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftBottom.gif" width="10" height="10"></td>'+
				'    <td height="7" style="border-bottom:1px solid #999999; border-left-width:1; border-right-width:1; border-top-width:1" colspan="4" width="280" bgcolor="#FFFFEA"></td>'+
				'    <td width="10" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightBottom.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'</table>'
				break;

		case 'NW':	
				// North West			
				ret ='<table class="JSBalloon" border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" id="AutoNumber1" >'+
				'  <tr>'+
				'    <td width="10" height="10"></td>'+
				'    <td width="70" style="border-bottom:1px solid #999999;  border-left-width:1; border-right-width:1; " height="10"></td>'+
				'    <td  height="10" style="border-bottom:1px solid #999999; border-left-width: 1; border-right-width: 1; " width="100%">'+
				'    </td>'+
				'    <td  align="right" valign="bottom">'+
				'    <img border="0" src="'+JSBalloonPath+'images/aNorthWest.gif" width="67" height="18"></td>'+
				'    <td width="1" height="10" style="border-bottom:1px solid #999999;"></td>'+
				'    <td width="10" height="10"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td height="1" width="10">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftTop.gif" width="10" height="10"></td>'+
				'    <td height="7" width=179 colspan="4" bgcolor="#FFFFEA"></td>'+
				'    <td height="7"  width="11">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightTop.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td valign=top colspan="6" style="border-left: 1px solid #999999; border-right: 1px solid #999999;  background-color: #FFFFEA">'+
				body +
				'    </td>'+
				'  </tr>'+
				'  <tr>'+
				'    <td width="10" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cLeftBottom.gif" width="10" height="10"></td>'+
				'    <td height="7" style="border-bottom:1px solid #999999; border-left-width:1; border-right-width:1; border-top-width:1" colspan="4" width="179" bgcolor="#FFFFEA"></td>'+
				'    <td width="11" height="7">'+
				'    <img border="0" src="'+JSBalloonPath+'images/cRightBottom.gif" width="10" height="10"></td>'+
				'  </tr>'+
				'</table>'
				break;
	}


	return ret;
}
	
function balloonBody(title, icon, body, footer, titleFontStyle, 
					messageFontStyle, footerFontStyle,
					showCloseBox)
{
	var imgShow='none';
	var iconTitle='';
	var ballonBody=body;
	var imgClose='none';
	var headerVisible='block';
	var offsetParent="-7";

	if(title!=undefined)
	{
		iconTitle=title;
	}

	if(showCloseBox)
	{
		imgClose='block';
	}
	else
	{
		imgClose='none';
	}

	if(icon != '')
	{
		imgShow='block';
	}
	else
	{
		imgShow='none';
	}

	if(imgShow=='none' && imgClose=='none' && iconTitle=='')
	{
		headerVisible='none';
		offsetParent="0";
	}
	else
	{
		headerVisible='block';
		offsetParent="-7";
	}

	return '    <table border="0" cellpadding="3" cellspacing="0" style="cursor:default;border-collapse: collapse; position:relative; top: '+offsetParent+';left:3" width="100%">' + 
			'      <tr style="display:'+headerVisible+'">' + 
			'        <td id="BIcon" width="3%" align=left><img id=BIcon src="'+icon+'" style="display:'+imgShow+'"></td>' + 
			'        <td id="BTitle" UNSELECTABLE="on" width="90%" style="'+titleFontStyle+'" align=left>'+iconTitle+'</td>' + 
			'        <td id="BClose" width="3%" valign=top align=right><img src="'+JSBalloonPath+'images/close.jpg" style="position:relative; top: 4;left:-5;display:'+imgClose+'" onmouseover="this.src=\''+JSBalloonPath+'images/closeActive.jpg\'" onmouseout="this.src=\''+JSBalloonPath+'images/close.jpg\'" onmouseup="this.src=\''+JSBalloonPath+'images/closeActive.jpg\'" onmousedown="this.src=\''+JSBalloonPath+'images/closeDown.jpg\'" title="Close">&nbsp;</td>' + 
			'      </tr>' + 
			'      <tr>' + 
			'        <td id="BBody" UNSELECTABLE="on" style="'+messageFontStyle+'" width="100%" colspan="3">' + ballonBody +'</td>' + 
			'      </tr>' + 
			'       <tr>' + 
			'        <td id="BFooter" UNSELECTABLE="on" style="'+footerFontStyle+'" width="100%" colspan="3">' + footer +'</td>' + 
			'      </tr>' + 
			'    </table>'
}							

function doUnescape(msg) {
	var s = new String(msg);  		
	return s.replace(/\&apos;/g, '\'');

}

function showBalloon(title, msg, ref, ctl) {
	var direction='SE';
	var footer = '';
	
	if (ref != '') 
		footer = '<a href="' + ref + '" target="help">Les meir</a>';

	var txtBody = balloonInfrastructure(balloonBody(title, 
										JSBalloonPath+'images/help.ico', 
										doUnescape(msg), 
										footer, 
										'font-family: MS Sans Serif;font-weight: bold; font-size:10pt;',
										'font-family: MS Sans Serif\; font-size:10pt\;',
										'font-family: MS Sans Serif\; font-size:10pt\;',
										false), direction);
	return overlib('<div style="width:200px;">' + txtBody + '</div>', FULLHTML, STICKY, MOUSEOFF, WIDTH, 200, ANCHOR, ctl.id, ANCHORALIGN, 'LL', ANCHORY, -15)
}

