// Convert RGB css values to HEX
var HexLookup = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
function RGB2Hex(rgb) {
    var Hex = "";
    while (rgb > 0) {
        Hex = HexLookup[rgb % 16] + Hex;
        rgb -= (rgb % 16);
        rgb /= 16;
    }
    return Hex;
}
function RGBcss2Hex(str) {
	var hex = str;
	// Is this an RGB string?
	if (str.indexOf("rgb(", 0) == 0) {
	    var vals = str.match(/\d+/g);
	    hex = "";
	    for (var i = 0; i < vals.length; i++) {
	        hex += RGB2Hex(vals[i]);
	    }
		
		hex = "#" + hex;
	}
    return hex;
}


//IE background image bug fix
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(err) {}


function AjaxLoad(url,Element,ReloadAds,AjaxResult,ConfirmLoad,Module) {
	var confirmed = 1; //default
	
	if(ConfirmLoad == 1) {
		if(!confirm("Er du sikker?")) 
			confirmed = 0;
	}
	
	if(confirmed == 1) {
		if(Element == 'alertbox')
			show_alertbox("","stay");
				
		document.getElementById(Element).innerHTML = "<img src='/_grafik/reading.gif' height='15'>";
		
		if(AjaxResult == '' || AjaxResult == undefined) {
			AjaxResult = "AjaxResult";
		}
		
		var	eDate = new Date();
		
		if(url.indexOf("?")==-1) {
			VarSeparator='?';}
		else {
			VarSeparator='&';}
			
		var NoCacheUrl = url  + VarSeparator + 'ajaxmodule=' + Module + '&t=' + eDate.getTime();
		setTimeout("MakeNewAJAXCall('" + NoCacheUrl + "', " + AjaxResult + ", 'GET', AjaxStatus, '" + Element + "', '" + eDate.getTime() + "', AjaxTimeout, 60)",400);
	   
	}
}


function AjaxResult(in_text, Element, in_param2) {
	if(Element == 'alertbox')
		show_alertbox(in_text,"hide");
	else
		document.getElementById(Element).innerHTML = in_text;
}

	
function AjaxTimeout(Element, in_param2, in_index) {
	//alert("Timed out loading element: " + Element);
}


function AjaxStatus(in_stage, in_text, Element, in_param2, in_index){
	//var	eDate = new Date();
	//var time_spent = parseInt ((eDate.getTime() - in_param2) / 100) / 10;
	//document.getElementById(Element).innerHTML = "<img src='/_grafik/reading.gif' height=15> " + time_spent + " seconds";
}


function fader(opac,ThisObject,opac_stop,step,fadeout,fadeinfo) {
	if(opac!=opac_stop) {
		opac = opac + step;
		set_opacity(opac,ThisObject);
		setTimeout("fader(" + opac + ",'" + ThisObject + "'," + opac_stop + "," + step + "," + fadeout + ",'" + fadeinfo + "')",70);
	}
	if(fadeinfo!='')
		document.getElementById(fadeinfo).value=opac;
	
	if(opac==opac_stop&&fadeout==1)
		setTimeout("fader(" + opac_stop + ",'" + ThisObject + "',0,-" + step + ",0,'" + fadeinfo + "')",1);
	
	if(opac==opac_stop&&fadeout==2) //lukker div'en efter fadeout
		document.getElementById(ThisObject).style.display='none';
}


function set_opacity(opac,ThisObject) {
	if(document.all){	// IE rules
		document.getElementById(ThisObject).style.filter = 'alpha(opacity=' + opac + ')';
	}
	else {
		document.getElementById(ThisObject).style.opacity = opac/100;
	}
}


function show_pic(id_pic,h,w) {
	OpenWindow = window.open('/show_product_pic.asp?id_pic='+id_pic, 'vis', 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width='+w+',height='+h);
}


function display_timeleft(timeleft,objectname) { 
	if(timeleft>0) {
		var days=Math.floor(timeleft / 86400); 
		var hours = Math.floor((timeleft - (days * 86400 ))/3600);
		var minutes = Math.floor((timeleft - (days * 86400 ) - (hours *3600 ))/60);
		var seconds = Math.floor((timeleft - (days * 86400 ) - (hours *3600 ) - (minutes*60)));
		window.top.document.getElementById(objectname).innerHTML = days + 'd ' + hours + 't ' + minutes + 'm ' + seconds + 's';
		timeleft-=1;
		setTimeout("display_timeleft(" + timeleft + ",'" + objectname + "')",1000);
	}
	else
		window.top.document.getElementById(objectname).innerHTML="Afsluttet"; 
} 


function getTimeLeft(ends,objectname) { 
	now = new Date('{{website_time_jsformat}}');
	ends = new Date(ends);
	var timeleft = (ends.getTime() - now.getTime())/1000;
	display_timeleft(timeleft,objectname);
}


function box_open_close(id) {
	if(document.getElementById(id).style.display == 'block'){
		document.getElementById(id).style.display = 'none';
	}
	else{
		document.getElementById(id).style.display = 'block';
	}
}


function show_messagebox(mode,ThisObject,html) {

	document.getElementById(ThisObject).innerHTML = html;
	if(mode==true) {
		window.parent.set_opacity(30,'page_container');
		document.getElementById(ThisObject).style.display = "block";
		}
	else {
		window.parent.set_opacity(100,'page_container');
		document.getElementById(ThisObject).style.display = "none";
		}
}


