//-----------------GET WHAT BROWSER
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

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;
}
//----------------END OF GET WHAT BROWSER

function showHideIt(id) {
  if (browser == "Internet Explorer")
	{
		if (document.getElementById(id).style.display=='block'){
			  document.getElementById(id).style.display='none';
			  document.getElementById(id+'img').src='/grafik/ikoner/hidden.gif';
	
		} else {
			  document.getElementById(id).style.display='block';
			  document.getElementById(id+'img').src='/grafik/ikoner/shown.gif';
	
		}
	}
	else
	{
		if (document.getElementById(id).style.display=='table-row'){
			  document.getElementById(id).style.display='none';
			  document.getElementById(id+'img').src='/grafik/ikoner/hidden.gif';
	
		} else {
			  document.getElementById(id).style.display='table-row';
			  document.getElementById(id+'img').src='/grafik/ikoner/shown.gif';
	
		}
	}
}

function showHideId(id) {
		if (document.getElementById(id).style.display=='block'){
			  document.getElementById(id).style.display='none';	
		} else {
			  document.getElementById(id).style.display='block';	
		}
}

function showTrId(id) {
  if (browser == "Internet Explorer")
  {
		document.getElementById(id).style.display='block';
	}
	else
	{
    document.getElementById(id).style.display='table-row';
	}
}

function showId(id) {
  document.getElementById(id).style.display='block';
}

function showIdInline(id) {
  document.getElementById(id).style.display='inline';
}

function hideId(id) {
  document.getElementById(id).style.display='none';
}

var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
function specialChars(strengen){
strengen=strengen.replace("&oslash;","ø").replace("&aelig;","æ").replace("&aring;","å").replace("&Oslash;","Ø").replace("&AElig;","Æ").replace("&Aring;","+");
return strengen;
}
function zipToCity(zip){
 xmlhttp.open("GET", "/includes/actions_bg/ziptocity.asp?zip="+zip,true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   if (xmlhttp.responseText.length>2) {
		document.forms['editor'].elements['dbrugere_by'].value=specialChars(xmlhttp.responseText);
	zipToCountry(zip);
	}
  }
 }
 xmlhttp.send(null)
}
function zipToCountry(zip){
 xmlhttp.open("GET", "/includes/actions_bg/ziptocountry.asp?zip="+zip,true);
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   document.forms['editor'].elements['dbrugere_land'].value=xmlhttp.responseText
  }
 }
 xmlhttp.send(null)
}
//loader en adresse
function loadAdresse(url)
{
  var xmlhttpf = false;
  if (!xmlhttpf && typeof XMLHttpRequest!='undefined') {
  	try {
  		xmlhttpf = new XMLHttpRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  if (!xmlhttpf && window.createRequest) {
  	try {
  		xmlhttpf = window.createRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  loadOkay = false;
  xmlhttpf.open("GET", url,true);
  xmlhttpf.onreadystatechange=function() {
  if (xmlhttpf.readyState==4) {

  }
 }
 xmlhttpf.send(null);
}
function loadAdresseToIdValue(url,id){
  document.getElementById(id).value=loadAdresseWaitReturn(url)
  }

var http_request = false;
function makePOSTRequest(url, parameters, alertContentsFunction) {
  http_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
     http_request = new XMLHttpRequest();
     if (http_request.overrideMimeType) {
     	// set type accordingly to anticipated content type
        //http_request.overrideMimeType('text/xml');
        http_request.overrideMimeType('text/html');
     }
  } else if (window.ActiveXObject) { // IE
     try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
        try {
           http_request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {}
     }
  }
  if (!http_request) {
     alert('Cannot create XMLHTTP instance');
     return false;
  }
  
  http_request.onreadystatechange = alertContentsFunction;
  http_request.open('POST', url, true);
  http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http_request.setRequestHeader("Content-length", parameters.length);
  http_request.setRequestHeader("Connection", "close");
  http_request.send(parameters);
}


function loadAdresseToId(url,id){
  var xmlhttpf = false;
  if (!xmlhttpf && typeof XMLHttpRequest!='undefined') {
  	try {
  		xmlhttpf = new XMLHttpRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  if (!xmlhttpf && window.createRequest) {
  	try {
  		xmlhttpf = window.createRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  loadOkay = false;
  xmlhttpf.open("GET", url,true);
  xmlhttpf.onreadystatechange=function() {
  if (xmlhttpf.readyState==4) {
      document.getElementById(id).innerHTML=specialChars(xmlhttpf.responseText);
  }
 }
 xmlhttpf.send(null);
}

function loadOptionToSelectId(url,id){
  var xmlhttpf = false;
  if (!xmlhttpf && typeof XMLHttpRequest!='undefined') {
  	try {
  		xmlhttpf = new XMLHttpRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  if (!xmlhttpf && window.createRequest) {
  	try {
  		xmlhttpf = window.createRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  loadOkay = false;
  xmlhttpf.open("GET", url,true);
  xmlhttpf.onreadystatechange=function() {
  if (xmlhttpf.readyState==4) {
      $('#' + id).html(xmlhttpf.responseText);      
  }
 }
 xmlhttpf.send(null);
}

function loadAdresseWaitReturn(url)
{
  var xmlhttpf = false;
  if (!xmlhttpf && typeof XMLHttpRequest!='undefined') {
  	try {
  		xmlhttpf = new XMLHttpRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  if (!xmlhttpf && window.createRequest) {
  	try {
  		xmlhttpf = window.createRequest();
  	} catch (e) {
  		xmlhttpf=false;
  	}
  }
  loadOkay = false;
  xmlhttpf.open("GET", url,false);

 xmlhttpf.send(null);
 return xmlhttpf.responseText;
}

function setWorker(url)
{
  window.frames.worker.location.href = url;
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
}

function Mid(str, start, len)
{
// Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor)
{
  for (i=0; i < strSearch.length; i++)
  {
        if (charSearchFor == Mid(strSearch, i, charSearchFor.length))
        {
              return i;
        }
  }
  return -1;
}

function submitForm(formId)
{
  var formToSend = document.getElementById(formId);
  try {
    document.getElementById('stylesheetTextarea').value = editAreaLoader.getValue('stylesheetTextarea');
  } catch (e) {}
  formToSend.submit();
}

function URLEncode(plaintext)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

  return encoded;
};

function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};
function isNumeric(elem, helperMsg){
	var numericExpression = /^[0-9]+$/;
	if(elem.value.match(numericExpression)){
		return true;
	}else{
		alert(helperMsg);
		elem.value='0';
		elem.focus();
		return false;
	}
}

function isNumSmaller(elem, smallerThan, helperMsg) {
  if(elem.value <= smallerThan){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isNumLarger(elem, lagerThan, helperMsg) {
  if(elem.value >= lagerThan){
		return true;
	}else{
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function fullscreenWin(url) {
  var mac = navigator.userAgent.indexOf('Mac') > 0; 
  if (mac) { 
    newwin=window.open(location.href, '', 'status=no,location=no,toolbar=no,menubar=no,directories=no,fullscreen=yes, scrollbars=auto,resizable=yes');
    newwin.moveTo(0,0);
    newwin.resizeTo(screen.width,screen.height); 
  } else { 
    window.open(location.href, '', 'status=no,location=no,toolbar=no,menubar=no,directories=no,fullscreen=yes, scrollbars=auto');
  }
}

function get_radio_value(f_field)
{
  if (f_field.type == 'radio') { return f_field.value; };
  for (var i=0; i < f_field.length; i++)
   {
   if (f_field[i].checked)
      {
        return f_field[i].value;
      }
   }
   return '0';
}
function getSelectIdValue(f_selectId) {
  return document.getElementById(f_selectId).options[document.getElementById(f_selectId).selectedIndex].value;
}
function getSelectValue(f_select) {
  return f_select.options[f_select.selectedIndex].value;
}
function getIdValue(f_Id) {
  return document.getElementById(f_Id).value;
}

function loading() {
document.getElementById('topDiv').style.visibility = 'visible';
document.getElementById('topDiv').innerHTML = '<div id="loading" style="position:absolute; width:100%; text-align:center; height: 100%; opacity: 0.8; background-color: #ccc; background-image: url(grafik/loading.gif); background-repeat: no-repeat; background-position: center center;"></div>';
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}
