var menuitem = '';

try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

function createXMLHttpRequest() {
   try { return new ActiveXObject("MSXML2.XMLHTTP"); } catch(e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return window.createRequest(); } catch(e) {}
   return null;
}

function get_cookie(name) {
  var search = name + '=';
  var returnvalue = '';
  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;
      returnvalue = unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function resetSearch(obj) {
	if (obj.value == 'Sök produkt') { obj.style.color = 'black'; obj.value = ''; }
	else if (obj.value == '') { obj.style.color = 'gray'; obj.value = 'Sök produkt'; }
}

function saveMenuPos() {
	var pos = document.getElementById('myTree').scrollTop;
	document.cookie = 'pos' + top.document.location.host + '=' + pos + '|'+ menuitem;
}

function getMenuPos() {
	if (get_cookie(top.document.location.host) != '') {
		var sValue = get_cookie('pos' + top.document.location.host);
		var aData = sValue.split('|');
		document.getElementById('myTree').scrollTop = aData[0];
		if (aData[1].length > 0) { document.getElementById(aData[1]).style.color = '#c00000'; document.getElementById(aData[1]).style.textDecoration = 'underline'; menuitem = aData[1]; }
	}
}

function selectMenu(id) {
	menuitem = id;
}

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    	obj.addEventListener(evType, fn, useCapture);
    	return true;
  } else if (obj.attachEvent){
    	var r = obj.attachEvent("on"+evType, fn);
    	return r;
  }
}

function initPage() {
	if (document.documentElement && document.documentElement.clientHeight) { document.getElementById('myTree').style.height = document.documentElement.clientHeight - 167 + 'px'; }
	else { document.getElementById('myTree').style.height = document.body.clientHeight - 165 + 'px'; }
	createPopups();
}

function goHome() {
	window.location = 'default.asp';
}

function findPos(obj) {
	var coords = {x: 0, y: 0};
	
	if (obj.offsetParent) {
		do {
			coords.x += obj.offsetLeft;
			coords.y += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return coords;
}

function autoComplete(obj) {
	
	var divWidth = 0;
	var coords = {x: 0, y: 0};
	coords = findPos(obj)
	
	if (obj.value.length < 1) {
		if (document.getElementById("autocomplete")) { document.getElementById("body").removeChild(document.getElementById("autocomplete")); }
	}
	else {
		if (!document.all) { divWidth = obj.offsetWidth + 24; } else { divWidth = obj.offsetWidth + 25; }
		if (!document.getElementById("autocomplete")) { 
			objAutoDiv = document.createElement("div");
			document.getElementById("body").appendChild(objAutoDiv);
			objAutoDiv.setAttribute("id","autocomplete");
			document.getElementById("autocomplete").style.height = '15px';
			document.getElementById("autocomplete").style.display = 'none';
			document.getElementById("autocomplete").style.width = divWidth + 'px';
			document.getElementById("autocomplete").style.left = coords.x + 'px';
			document.getElementById("autocomplete").style.top = (coords.y + 23) + 'px';
		}
		loadResult(document.getElementById("frmSearch").value)
	}
}

function loadResult(str) {
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = showResult;
  	http_request.open('GET','searchresult.asp?str=' + str + '&random=' + Math.random(), true);
  	http_request.send(null);
}

function showResult() {
	
	var string = '';
   
    if (http_request.readyState == 4) { 
   		if (http_request.status == 200) {
			string = http_request.responseText;
			if (string.length > 0) {
				document.getElementById("autocomplete").style.display = 'block';
        		document.getElementById("autocomplete").innerHTML = string;
				if (document.getElementById("resultlist").offsetHeight > 209) {
					document.getElementById("autocomplete").style.height = '209px';
					document.getElementById("autocomplete").style.overflowY = 'auto';
					document.getElementById("autocomplete").style.overflowX = 'hidden';
				}
				else {
					document.getElementById("autocomplete").style.height = document.getElementById("resultlist").offsetHeight + 2 + 'px';
					document.getElementById("autocomplete").style.overflowY = 'hidden';
					document.getElementById("autocomplete").style.overflowX = 'hidden';
				}
			}
			else {
				document.getElementById("autocomplete").style.display = 'none';
			}
     	}
	 	else {
        	alert('Ett problem uppstod när sökresultat skulle hämtas'); 
     	} 
    }
}

function copySearch(obj) {
	document.getElementById("frmSearch").value = obj.innerHTML;
	document.search.submit();
}

function createPopups() {
	var thumbs = document.getElementsByName('thumb');
	for (i = 0; i < thumbs.length; i++) {
		thumbs[i].onclick = function() { openWindow(this.href, this.rel, this.rev); return false; }
	}
}

function callBack() { }

function openWindow(url, width, height) {
	if (window.showModalDialog) {
		var checknow = window.showModalDialog(url,callBack,'dialogHeight: ' + height + 'px; dialogWidth: ' + width + 'px; dialogTop: px; dialogLeft: px; edge: Raised; center: Yes; help: No; resizable: No; status: No;');
	}
	else {
		var xMax = window.outerWidth;
		var yMax = window.outerHeight;
		var xOffset = (xMax - 700)/2, yOffset = (yMax - 540)/2;
		var handle = window.open(url,'dialog', 'width=' + width + ',height=' + height + ',screenX=' + xOffset + ',screenY=' + yOffset + ', top=' + yOffset + ',left=' + xOffset + ',resizable=no,dialog=yes,modal=yes');
	}
}

function closeAutoComplete() {
	if (document.getElementById("autocomplete")) { document.getElementById("body").removeChild(document.getElementById("autocomplete")); }
}

function fixEmail(obj) {
	var str = obj.href.replace('[ta]','@');
	str = str.replace('ftp:','mailto:');
	obj.href = str;
}

addEvent(window,'load', initPage, false);
addEvent(window,'resize', initPage, false);
