

addToWindowLoad("load", runOnLoad); // Run stuff on window.onload:

blnHideDivFakePopup = true;
blnAlreadyPoppedFakePopup = false;

if ( typeof( bitShowFakePopup ) == "undefined" ) bitShowFakePopup = 0;
if ( typeof( blnAlreadyPoppedFakePopup ) == "undefined" ) blnAlreadyPoppedFakePopup = true;
if ( typeof( autoUpdateFieldsForm ) == "undefined" ) autoUpdateFieldsForm = null;
if ( typeof( arrKeyValue ) == "undefined" ) arrKeyValue = new Array();



function runOnLoad()
{
	appendCloneToBody( document.getElementById("divPopup"), true );
	enableInputDefalutValues();
	autoUpdateFields();
	fixPage_TakForHenvendelsen();

	loc = "" + window.location.href;
	query = "" + window.location.search;
	if (
		bitShowFakePopup == 1
		&& blnAlreadyPoppedFakePopup == false
		&& (
			loc.substring( loc.length - "robinhus.dk/".length) == "robinhus.dk/"
			|| loc.substring( loc.length - "robinhus.dk/default.asp".length) == "robinhus.dk/default.asp"
			|| query.indexOf("m=popup") != -1
		)
	)
	{
		addEventToElement(document, "click", function() { setTimeout('checkIfFakeDropDownShouldBeClosed()', 50); } );
		addEventToElement(document, "click", function() { setTimeout('hideDivPopup()', 125); } );

		if (document.getElementById("divOverlayGlobal"))
		{
			document.getElementById("divOverlayGlobal").onclick = function() { setTimeout('hideDivPopup()', 125); };
		}
	}
	

	// Debugging
	//document.onmousemove = function() { document.title = "blnHideDivFakePopup = " + blnHideDivFakePopup; };
}

blnDebugMode = false;
function setDocTitle(strNewTitle, blnAppend)
{
	if (blnDebugMode == true)
	{
		if (blnAppend)
		{
			document.title += strNewTitle;
		} else {
			document.title = strNewTitle;
		}
	}
}

function addEventToElement(obj, eventname, newcode, optional_phase)
{
	eventPhase = false; if (optional_phase) eventPhase = optional_phase;

	if (obj.attachEvent)
	{
		// IE
		obj.attachEvent(eventname, newcode);
	} else {
		// all other browsers
		obj.addEventListener(eventname, newcode, eventPhase );
	}

	
}

function addToWindowLoad(eventname, functionname)
{
	evt_1 = "";
	evt_2 = "";
	func = functionname;
	if (typeof(func) == "string") func = eval(func);

	if (eventname == "load" || eventname == "onload")		{ evt_1 = "load"; evt_2 = "onload" }
	if (eventname == "resize" || eventname == "onresize")	{ evt_1 = "resize"; evt_2 = "onresize" }

	if (evt_1 == "" || evt_2 == "") return;

	if( window.addEventListener ) {
	  window.addEventListener(evt_1,func,false);
	} else if( document.addEventListener ) {
	  document.addEventListener(evt_1,func,false);
	} else if( window.attachEvent ) {
	  window.attachEvent(evt_2,func);
	}
}

//Bruges til at fjerne ikke-numeriske værdier fra indtastningsfelter
function parseNumber(el) {

	el.value = parseInt(el.value.replace(/[^0-9]/g,""));
	if (isNaN(el.value)) el.value = '';

}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y) {
		curtop += obj.y;
	}
	return curtop;
}

layersatskjule=new Array();

layersatskjule[0]="selectdummy";

function skjulLayers() {
	for(i=0; i<layersatskjule.length; i++) {
		if (document.getElementById(layersatskjule[i])) document.getElementById(layersatskjule[i]).style.visibility="hidden";
	}
}

//document.onclick = function() { setTimeout('checkIfFakeDropDownShouldBeClosed()',50); };

blnHideFakeDropdown = false;

function checkIfFakeDropDownShouldBeClosed()
{
	if (blnHideFakeDropdown != false)
	{
		skjulLayers();
	} else {
		blnHideFakeDropdown = true;
	}
}

function toggleVisibilitySelect(mom,hvilken) {
	if (document.getElementById(hvilken))
	{
		if(document.getElementById(hvilken).style.visibility=="hidden")
			{
			blnHideFakeDropdown = false;
		
			if (String(window.location).indexOf('infopaq.dk/') != -1) {
				document.getElementById(hvilken).style.left=(findPosX(mom)+mom.offsetLeft-130)+"px";
				document.getElementById(hvilken).style.top=(findPosY(mom)+mom.offsetHeight-91)+"px";
			} else { 
				document.getElementById(hvilken).style.left=findPosX(mom)+mom.offsetLeft+"px";
				document.getElementById(hvilken).style.top=findPosY(mom)+mom.offsetHeight+"px";		
			}
			
			document.getElementById(hvilken).style.visibility="visible";
			if (document.getElementById("selectdummy"))
			{
				document.getElementById("selectdummy").style.visibility="visible";
				document.getElementById("selectdummy").style.width=document.body.clientWidth+"px";
				document.getElementById("selectdummy").style.height=document.body.clientHeight+"px";
			}
			setTimeout("blnHideFakeDropdown = true", 200);
		}
		else {
			blnHideFakeDropdown = true;
			skjulLayers();
		}
	}
}

function skiftVaerdi() {
	for(i=0; i<arguments.length; i++) {
		hvad=document.getElementById(arguments[i]);
		i++;
		hvad.value=arguments[i];
	}
}

function overSelect(hvad) {
	hvad.style.backgroundColor='#002589';
	hvad.style.color='#ffffff';
	hvad.style.cursor='hand';
}

function outSelect(hvad) {
	hvad.style.backgroundColor='#F2F4FC';
	hvad.style.color='#002589';
}

function sendTilVen(urlen,doc) {
	vin=window.open('/includes/mailven.asp?url='+urlen+'&dokument='+doc,'sendven','width=400,height=400,left=100,top=100');
	vin.focus();
}

function getDocumentHeight()
{
	return "100%";

	if (document.body.scrollHeight && navigator.appVersion.indexOf("Win") != -1) {
	// body.scrollHeight gets the correct value on WIN IE6, but non on MAC
		return document.body.scrollHeight;
		}
	else if (document.documentElement.scrollHeight) {
		return document.documentElement.scrollHeight;
		}
	else if (document.documentElement.offsetHeight) {
		return document.documentElement.offsetHeight;
		}
	else{
		return 0;
	}
}

function getDocumentWidth()
{
	//return "100%";

	if (document.body.scrollWidth && navigator.appVersion.indexOf("Win") != -1) {
	// body.scrollHeight gets the correct value on WIN IE6, but non on MAC
		return document.body.scrollWidth;
		}
	else if (document.documentElement.scrollWidth) {
		return document.documentElement.scrollWidth;
		}
	else if (document.documentElement.offsetWidth) {
		return document.documentElement.offsetWidth;
		}
	else{
		return 0;
	}
}

function showOverlayGlobal()
{
	_divOverlayGlobal = document.getElementById("divOverlayGlobal");
	if (_divOverlayGlobal)
	{
		w = "" + (getDocumentWidth()-1);
		if (w.indexOf("%") == -1) w += "px";

		h = "" + getDocumentHeight();
		if (h.indexOf("%") == -1) h += "px";
		
		_divOverlayGlobal.style.display = "block";
		_divOverlayGlobal.style.visibility = "visible";
		_divOverlayGlobal.style.width = w;
		_divOverlayGlobal.style.height = h;

		/*
		_popup = document.getElementById("divPopup");
		if (_popup)
		{
			_popupClone = _popup.cloneNode(true);
			_popup.parentNode.removeChild(_popup);
			document.body.appendChild(_popupClone);
		}
		*/
	}
}

/* Cookie functions from http://www.w3schools.com/JS/js_cookies.asp */
function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
		{ 
		c_start=c_start + c_name.length+1 ;
		c_end=document.cookie.indexOf(";",c_start);
		if (c_end==-1) c_end=document.cookie.length
		return unescape(document.cookie.substring(c_start,c_end));
		} 
	  }
	return "";
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toUTCString());
}

function deleteCookie(c_name)
{
	setCookie(c_name,'',-1);
}

function hideDivPopup()
{
	
	if (skipHideDivPopup) return;

	
	if (blnHideDivFakePopup == true)
	{
		if (document.getElementById("Table1")) document.getElementById("Table1").style.display = "none";
		if (document.getElementById("divPopup")) document.getElementById("divPopup").style.display = "none";
		if (document.getElementById("divOverlayGlobal")) document.getElementById("divOverlayGlobal").style.display = "none";

		if (blnAlreadyPoppedFakePopup == false)		
		{
			blnAlreadyPoppedFakePopup = true;
			window.open('/popup/rhm.asp?layout=' + layoutToShow, 'RHM', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=' + layoutToShowWidth + ',height=' + layoutToShowHeight);
		}
	}
	/*
	document.title = layoutToShow + "," + layoutToShowWidth + "," + layoutToShowHeight;
	document.title += "|blnHideDivFakePopup = " + blnHideDivFakePopup;
	document.title += "|bitShowFakePopup = " + bitShowFakePopup;
	document.title += "|blnAlreadyPoppedFakePopup = " + blnAlreadyPoppedFakePopup;
	*/

	setDocTitle(layoutToShow + "," + layoutToShowWidth + "," + layoutToShowHeight, false);
	setDocTitle("|blnHideDivFakePopup = " + blnHideDivFakePopup, true);
	setDocTitle("|bitShowFakePopup = " + bitShowFakePopup, true);
	setDocTitle("|blnAlreadyPoppedFakePopup = " + blnAlreadyPoppedFakePopup, true);

}

function enableInputDefalutValues()
{
	c = 0;
	inps = document.getElementsByTagName("input");
	for(i = 0, len = inps.length; i < len; i ++)
	{
		inp = inps[i];
		if (inp.className.indexOf("jsToggleDefaultValue") != -1)
		{
			c++;
			v = inp.value;
			inp.defaultValue = v;
			inp.onfocus = function() {
				if (this.value == this.defaultValue)
				{
					this.value = "";
					this.style.color = "#000000";
				}
			};
			inp.onblur  = function() {
				if (this.value.replace(/ /g, "") == "")
				{
					this.value = this.defaultValue;
					this.style.color = "#999999";
				} else {
					this.style.color = "#000000";
				}
			};
		}
	}

	autoUpdateFields();
}

function autoUpdateFields()
{
	autoUpdateFieldsForm = document.getElementById('frmDivPopup');
	//alert(autoUpdateFieldsForm);
	if (autoUpdateFieldsForm)
	{

		for (elm in arrKeyValue)
		{
			if ( autoUpdateFieldsForm.elements[elm] )
			{
				//alert( " element: " + autoUpdateFieldsForm.elements[elm].name + "\n værdi: " + arrKeyValue[elm] );
				autoUpdateFieldsForm.elements[elm].value = arrKeyValue[elm];
				autoUpdateFieldsForm.elements[elm].style.color = "#000000";
			}
		}
	}
}

function delayedExecutionEval(delay, code)
{
//	setTimeout("eval('" + code.replace(/\'/g, "\'") + "')", delay);
}

function validatePopup_BestilGratisInformation(f)
{
	alert("* Hvilke felter SKAL man udfylde?\n\n*Skal man kunne submitte formen ved at trykke på ENTER tasten?");
	return false;
}

function _ajaxGetXMLHttpRequest()
{
	obj = null;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		obj=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		obj=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return obj;
}

function ajaxCall(url, handler, params)
{
	xmlhttp = _ajaxGetXMLHttpRequest();
	if (xmlhttp == null) return;

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			handler(xmlhttp.responseText, params);
		}
	}
	xmlhttp.open("GET",url,true);
	xmlhttp.send();
}

function getBynavnPopup(objSource, objDestination)
{
	if (objSource)
	{
		if (typeof(objSource) != "string") objSource = objSource.value;
		if (!objDestination) return;
	}
	ajaxCall("/includes/lookup/zipcode_to_cityname.asp?zipcode=" + objSource, setBynavnPopup, objDestination);
}

function setBynavnPopup(res, objDestination)
{
	objDestination.value = res;
	objDestination.style.color = "#000000";
}

function checkPostnummerPopup(objSource, objDestination)
{
	if (objSource.value.length == 4)
	{
		getBynavnPopup(objSource, objDestination);
	} else {
		objDestination.value = "" + objDestination.defaultValue;
		objDestination.style.color = "#999999";
	}
}

function setInputValues(frm, arrKeyValue)
{
	frm = document.getElementById("Form1");
	if (!frm) frm = document.getElementById("frmDivPopup");

	if (frm)
	{
		for (elm in arrKeyValue)
		{
			if ( frm.elements[elm] )
			{
				frm.elements[elm].value = arrKeyValue[elm];
				frm.elements[elm].style.color = "#000000";
			}
		}
	}
}

function setValuesPopupDialog(headline, message)
{

	_divPopupDialogGlobal = document.getElementById("divPopupDialogGlobal");
	_divSplash = document.getElementById("divOverlayGlobalSplash");
	_divSplashTemplate = document.getElementById("divTemplateSplayEjAktiveret");
	_divDialogClose = document.getElementById("imgPopupDialogTitleClose");

	if (_divPopupDialogGlobal)
	{
		_divPopupDialogGlobalTitle = document.getElementById("divPopupDialogGlobalTitleText");
		_divPopupDialogGlobalTextHeadline = document.getElementById("divPopupDialogGlobalContentTextHeadline");
		_divPopupDialogGlobalTextBody = document.getElementById("divPopupDialogGlobalContentTextBody");

		_divPopupDialogGlobalTitle.innerHTML = headline;
		_divPopupDialogGlobalTextHeadline.innerHTML = headline;
		_divPopupDialogGlobalTextBody.innerHTML = message;

		//_divSplash.innerHTML = _divSplashTemplate.innerHTML;
		showOverlayGlobal();

		_divPopupDialogGlobal.style.display = "block";
		_divPopupDialogGlobal.style.visibility = "visible";
	}

	/*
	if (_divDialogClose) _divDialogClose.onclick = function() { hideOverlayAndSplash_EjAktiveret(); };
	*/
}


function hideDivOverlayGlobal()
{
	if (document.getElementById("divOverlayGlobal")) document.getElementById("divOverlayGlobal").style.display = "none"
}

function appendCloneToBody(objElement, blnRemoveOriginalElement)
{
	if (objElement)
	{
		objElementClone = objElement.cloneNode(true);
		if (blnRemoveOriginalElement) objElement.parentNode.removeChild(objElement);
		document.body.appendChild(objElementClone);
	}
}

function fixPage_TakForHenvendelsen()
{
	var loc = "" + window.location.href;
	if (loc.indexOf("/tak_for_henvendelsen.asp") != -1)
	{
		agent = navigator.userAgent;
		if (agent.toLowerCase().indexOf("applewebkit") != -1)
		{
			if (document.getElementById("postnummer"))
			{
				document.getElementById("postnummer").style.marginLeft = 2 + "px";
			}
			if (document.getElementById("ejendomAdresse"))
			{
				document.getElementById("ejendomAdresse").style.marginLeft = 0 + "px";
			}
			if (document.getElementById("ejendomBoligAreal"))
			{
				document.getElementById("ejendomBoligAreal").style.marginLeft = 0 + "px";
			}
			
		}
	}
}
