var IE = (document.all) ? 1 : 0;
var NS = (document.layers) ? 1 : 0;

// WINDOW STATUS FUNCTIONS
window.defaultStatus = "";
function winStatus( msg )
{
	window.status = msg;
}

var pictureWindow = null;
function showPicture(url, width, height)
{
  pictureWindow=window.open(url, 'picture', "width="+width+",height="+height+",scrollbars=1,resizable=1");
  if (pictureWindow.focus != null)
    pictureWindow.focus();
  pictureWindow.resizeTo(width+50, height+70);
}

function showCarPicture(url)
{
  pictureWindow=window.open(url, 'picture', "width="+752+",height="+560+",scrollbars=1,resizable=1");
  if (pictureWindow.focus != null)
    pictureWindow.focus();
}
function showPictureBrowser(url)
{   
    pictureWindow=window.open(url, 'picture', "width=770,height=623,scrollbars=1,resizable=0");
    if (pictureWindow.focus != null)
        pictureWindow.focus();
}

function OpenWindow(val, title, width, height, scrollbars)
{
  win1=open(val,title,"width="+width+",height="+height+",scrollbars="+scrollbars);
  if (win1.focus != null)
    win1.focus();
}

function openExitPopup(width,height,file,id,interval)
{
	var nyhedsbrev = getCookie(id);
	if(nyhedsbrev==null)
	{
		if(document.layers)
		{
			width+=80;
			height+=30;
		}
		win = window.open(file,id,'width=5,height=5,left=-10,top=-10');
		window.focus();
		win.blur();
		window.focus();
		var tomorrow = new Date();
		tomorrow.setTime(tomorrow.getTime()+interval*24*60*60*1000);
		document.cookie = id+"=autocom; path=/ ;expires=" + tomorrow.toGMTString();

		win.moveTo((window.screen.width-width)/2, (window.screen.height-height)/2);
		win.resizeTo(width, height);
	}
}





function getCookie(Name)
{
	var search = Name + "=";
	if (document.cookie.length > 0)
	{
	 	offset = document.cookie.indexOf(search);
	 	if (offset != -1)
	 	{
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
				end = document.cookie.length
			return unescape(document.cookie.substring(offset, end));
		}
	}
	return null;
}





function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

function validate(string)
{
    if (!string)
    	return false;
    var Chars = "0123456789";

    for (var i = 0; i < string.length; i++)
    {
       if (Chars.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}

function updateVatCalc(input,outfield,factor)
{
	if(validate(input))
		outfield.value=""+Math.round((parseInt(input)*parseFloat(factor)));
	else
		outfield.value="";
}


function onlyDigits(e,decReq)
{
	var isIE = document.all?true:false;
	var isNS = document.layers?true:false;
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	var isNum = (key > 47 && key < 58) ? true:false;
	var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
	window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
	e.which = (!isNum && !dotOK && isNS) ? 0:key;
	return (isNum || dotOK);
}

// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
function onlyAlphanumeric(e)
{
	var key;
	var keychar;

	if (window.event)
   key = window.event.keyCode;
	else if (e)
   key = e.which;
	else
   	return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// control keys
	if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
  return true;

	// alphas and numbers
	else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) > -1))
  	 return true;
	else
  	 return false;
}


function clearcontrols(controls,listboxes,textareas)
{
	for(var i in controls)
	{
		controls[i].checked = false;
	}
	for(var i in listboxes)
	{
		listboxes[i].selectedIndex = 0;
	}
	for(var i in textareas)
	{
		textareas[i].value = "";
	}
}

function setControlFromSelect(select,radio)
{
  if(select.selectedIndex != 0)
    radio.checked = true;
}

function setControlFromCheckbox(checkbox,radio)
{
  if(checkbox.checked)
    radio.checked = true;
}

function setControlFromTextarea(textarea,radio)
{
  if(textarea.value!="")
    radio.checked = true;
}

function onlyDigitsEnterSubmits(e,decReq,form)
{
	var isIE = document.all?true:false;
	var isNS = document.layers?true:false;
	var key = (isIE) ? window.event.keyCode : e.which;
	var obj = (isIE) ? event.srcElement : e.target;
	if (key == 13)
    form.submit();
	var isNum = (key > 47 && key < 58) ? true:false;
	var dotOK = (key==46 && decReq=='decOK' && (obj.value.indexOf(".")<0 || obj.value.length==0)) ? true:false;
	window.event.keyCode = (!isNum && !dotOK && isIE) ? 0:key;
	e.which = (!isNum && !dotOK && isNS) ? 0:key;
	return (isNum || dotOK);
}

function hideShowDiv(divName)
{
	var divPlus1 = "plus" + divName;
  var div = document.getElementById(divName);
  var divPlus = document.getElementById(divPlus1);
  if (div.style.display=='block')
	{
	  div.style.display='none';
    divPlus.innerHTML=("+");
  }
	else
	{
    div.style.display='block';
    divPlus.innerHTML=("-");
  }
}

function popUp(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=0,menubar=1,resizable=1,width=800,height=600');");
}
var _G_CountDown_limit;
var _G_CountDown_eventMethod;
var tu = new Date();

function clock_tick()
{
	if (tu <= _G_CountDown_limit)
	{
    _G_CountDown_eventMethod();
	}
	else
	{
		tu--;
		if(tu==-1)
    {
      setTimeout("clock_tick()", 1000);
      return;
    }
		if (tu < _G_CountDown_limit)
		{
			_G_CountDown_eventMethod();
		}
		else
		{
			var d = Math.floor(tu / 86400);
			document.forms.timer.days.value = d;
			var h = Math.floor((tu-d*86400) / 3600);
			document.forms.timer.hours.value = h;
		  var m = Math.floor(tu / 60) % 60;
			document.forms.timer.minutes.value = m;
			var s =   Math.floor(tu % 60);
			document.forms.timer.seconds.value = s;
		  if(d==0 && h == 0 && m<=0 && s<=0)
		  {
				document.forms.timer.days.value = '?';
				document.forms.timer.hours.value = '?';
				document.forms.timer.seconds.value = '?';
	    	document.forms.timer.minutes.value = '?';
	  	}
	  }
		setTimeout("clock_tick()", 1000);
	}
}


function timer_start(_limit, _eventMethod, _reloadTime, _now, _timer)
{
		var reloadTime = _reloadTime;
		var now = _now;
		_G_CountDown_limit = _limit;
		_G_CountDown_eventMethod = _eventMethod;
		tu = (reloadTime - now)/1000;
		if (tu > 0)
			clock_tick();
}


function callActionChangeOneDiv(urlString, hideDivId, showDivId, errorMessage)
{
  callActionChangeTwoDivs(urlString, hideDivId, null, showDivId, null, errorMessage);
}

function callActionChangeTwoDivs(urlString, hideDivId, theOtherHideDivId, showDivId, theOtherShowDivId, errorMessage)
{
  document.getElementById(hideDivId).style.display='none';
  if (theOtherHideDivId != null)
   document.getElementById(theOtherHideDivId).style.display='none';
  document.getElementById(showDivId).style.display='block';
  if (theOtherShowDivId != null)
   document.getElementById(theOtherShowDivId).style.display='block';
  document.getElementById(hideDivId).innerHTML=document.getElementById(hideDivId).innerHTML; // Do not delete this line. It speeds up the process dramatically - there is probably a better way to do this ...
  if (theOtherHideDivId != null)
    document.getElementById(theOtherHideDivId).innerHTML=document.getElementById(theOtherHideDivId).innerHTML; // Do not delete this line. It speeds up the process dramatically - there is probably a better way to do this ...
  var xmlHttp;
  try
  { // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {  // Internet Explorer
    try
    {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        alert("Your browser does not support AJAX");
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {

    if(xmlHttp.readyState==4)
    {
      
      if (xmlHttp.responseText != 'OK')
      {
        document.getElementById(hideDivId).style.display='block';
        if (theOtherHideDivId != null)
          document.getElementById(theOtherHideDivId).style.display='block';
        document.getElementById(showDivId).style.display='none';
        if (theOtherShowDivId != null)
          document.getElementById(theOtherShowDivId).style.display='none';
        document.getElementById(hideDivId).innerHTML=errorMessage;
        if (theOtherHideDivId != null)
          document.getElementById(theOtherHideDivId).innerHTML=errorMessage;
      }
    }
  }
  // rval is added to avoid caching of the result
  urlString += "&rval="+new Date().getTime();
  xmlHttp.open("GET", urlString, true);
  xmlHttp.send(null);
}



function HideLayer(id) {
document.getElementById(id).style.display="none";
}

function ShowLayer(id) {
document.getElementById(id).style.display="block";
}

function ShowHideLayer(showId,hideId)
{
	HideLayer(hideId);
	ShowLayer(showId);
}

function ShowHideLayerTimeout(layer,action,timeout)
{
    var el
    if (action == 'show')
    {
        document.getElementById('filter').style.display = 'block';
        el = document.getElementById(layer)
        el.style.display = 'block';
        if (timeout > 0)
        {
            setTimeout("document.getElementById('" + layer + "').style.display='none';document.getElementById('filter').style.display='none'", timeout);
        }
    } else
    {
        el = document.getElementById(layer)
        el.style.display = 'none';
    }
}

function changeClass(id, newClass) {
 identity=document.getElementById(id);
 identity.className=newClass;

}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function isEventEnter(event)
{
	event = event || window.event;
	var unicode=event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
	if (unicode == 13){
		return true;
	}
	return false;
}

function simulateClick(element)
{
	if(element.click)
		element.click();
	else
	{
		var evt = document.createEvent("MouseEvents"); 
		evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); 
		var el = element;  
		el.dispatchEvent(evt);
	}

}

function toggle_visibility(id) 
{
    var e = document.getElementById(id);
    if(e.style.display == 'block')
       e.style.display = 'none';
    else
       e.style.display = 'block';
 }

function setFocus()
{
	var el = document.getElementById("username");
	if(el.style.visibility == "visible")
		el.focus();
	
}

function toggleDisplay(div,iframe,objpos,pos)
{
	var t=0;
	var l=0;
	var state=false;

	var cNode = document.getElementById(objpos);
	while(cNode.tagName!='BODY'){ 
       		l+=cNode.offsetLeft; 
       		t+=cNode.offsetTop; 
       		cNode=cNode.offsetParent; 
    	}

	var DivRef = document.getElementById(div);
	var IfrRef = document.getElementById(iframe);
   	var ObjPos = document.getElementById(objpos);

	if (DivRef.style.display=="none") {
		state=true;
	} else {
		state=false;
	}

   	if(state)
   	{
    		DivRef.style.display = "block";
    		IfrRef.style.width = DivRef.offsetWidth;
    		IfrRef.style.height = DivRef.offsetHeight;
	
	switch (pos) {
		case "left": 
    			DivRef.style.top = t;
    			DivRef.style.left = (l-ObjPos.offsetWidth);
    			IfrRef.style.top = t;
    			IfrRef.style.left = (l-ObjPos.offsetWidth);
	  		break;
		case "right": 
			DivRef.style.top = t;
			DivRef.style.left = (l+ObjPos.offsetWidth);
    			IfrRef.style.top = t;
    			IfrRef.style.left = (l+ObjPos.offsetWidth);
	  		break;
		case "bottom": 
			DivRef.style.top = (t+ObjPos.offsetHeight);
    			DivRef.style.left = l;
    			IfrRef.style.top = (t+ObjPos.offsetHeight);
    			IfrRef.style.left = l;
	  		break;
		case "top": 
    			DivRef.style.top = (t-ObjPos.offsetHeight);
    			DivRef.style.left = l;
    			IfrRef.style.top = (t-ObjPos.offsetHeight);
    			IfrRef.style.left = l;
	  		break;
        	}


    	IfrRef.style.zIndex = DivRef.style.zIndex - 1;
    	IfrRef.style.display = "block";
    	IfrRef.style.backgroundColor = "black";
   	}
   	else
   	{
    		DivRef.style.display = "none";
    		IfrRef.style.display = "none";
   	}

}

function changeBPMBTW(_enrollId, _currencyCode, _useBPM, _depriciatedBPM, _input, _vatFactor, _groupingSeparator)
{
    if (!_useBPM)
     _depriciatedBPM = 0;
    var price = "";
    var amount = Math.round(parseFloat(_vatFactor) * parseInt(_input)) + parseInt(_depriciatedBPM);
    if (!(amount >= 0) || parseInt(_depriciatedBPM) < 0)
        price = "?";
    else
        price = ""+amount;
    var output = "";
    for (var i=0; i < price.length; i++)
    {
      if (i != 0 && (price.length - i) % 3 == 0) output += _groupingSeparator;
        output += price.charAt(i);
    }
    document.getElementById("bpmbtw_" + _enrollId).innerHTML = _currencyCode + " " + output;
}

function submitOnEnterKey(event, formName)
{

    var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
    if (keyCode == 13)
    {
        document.forms[formName].submit();
    }
}

function limitText(limitField, limitCount, limitNum)
{

    if (limitField.value.length > limitNum)
    {
		limitField.value = limitField.value.substring(0, limitNum);
	}
    else
    {
		if(limitCount)
            limitCount.value = limitNum - limitField.value.length;
	}
}
