var preLoad = new Array();
var bPopup = false;
var resultcount = -1;
var bType;
var menuitem = '';

document.all?bType = false:bType = true;

try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}

function submitForm() {
   if (window.event.keyCode == 13 && window.event.srcElement.name == 'frmLoginLosenord') {
 	  document.login.submit();
   }
}

if (document.all) {
	document.onkeydown = submitForm;
}

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 toggleAdress(obj) {
	var element = document.getElementById(obj);
	if (element.style.display == 'none') { element.style.display = 'block' }
	else { element.style.display = 'none' }
}

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 givefocus(field) {
	var objField = document.getElementById(field);
	objField.focus();
	if ((objField.type == "text" || objField.type == "password" || objField.type == "textarea") && (document.all)) { 
  		objField.blur();
  		objField.select();
	}
}

function setFocus() {
	if (document.forms[1]) {
		var x = document.forms[1].length;
		if (document.forms[1].id == 'login') { 
			for(i = 0; i < x; i++ ) {
				if ((document.forms[1].elements[i].type == 'text' || document.forms[1].elements[i].type == 'password') && !document.forms[1].elements[i].disabled && !document.forms[1].elements[i].readOnly) {
					if (document.forms[1].elements[i].value.length == 0) {
						document.forms[1].elements[i].focus();
						break;
					}
				}
			}
		}
	}
}

function changeState(id) {
	if (tree.getOpenState(id) == 1) { tree.closeItem(id); }
	else { tree.openItem(id); }
}

function checkSearch() {
	if (document.getElementById('frmSearch').value.length == 0) {
		alert('Du har inte angivit något sökord');
		document.getElementById('frmSearch').focus();
		return false;
	}
}

function flipLogin() {
	if (document.getElementById('box').style.display == '') {
		document.getElementById('box').style.display = 'block';
		givefocus('frmLoginEpost');
	}
	else {
		document.getElementById('box').style.display = '';
	}
}

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 selectMenu(id) {
	menuitem = id;
}

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 = '#00315C';
			document.getElementById(aData[1]).style.fontWeight = 'bold';
			//document.getElementById(aData[1]).style.color = '#c00000';
			//document.getElementById(aData[1]).style.textDecoration = 'underline';
			menuitem = aData[1];
		}
	}
}

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 - 155 + 'px'; }
	else { document.getElementById('myTree').style.height = document.body.clientHeight - 153 + 'px'; }
	document.getElementById('myTree').style.overflow = 'auto';
	preloadPics();
	setFocus();
}

function preloadPics() {
	if (document.images) {
		if (document.getElementById('preload')) {
			var links = document.getElementById('preload').getElementsByTagName('a');
			for (i = 0; i < links.length; i++) {
				links[i].onclick = function() { previewImg(this.name); return false; };
				preLoad[i] = new Image();
				preLoad[i].src = links[i].href;
			}
		}
	}
}

function previewImg(i) {
	var oImage = document.getElementById('bigimg');
	var oAnchor = oImage.parentNode;
	if (document.images) { oImage.src = preLoad[i].src; }
	oAnchor.href = preLoad[i].src;
}

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 iterateList() {
	
	var anchors = document.getElementById('resultlist').getElementsByTagName('a');
	
	if (window.event.keyCode == 40) {
		if (document.getElementById('result' + (resultcount+1))) {
			for(i = 0; i < anchors.length;i++) { anchors[i].className = 'listunfocus'; }
			resultcount++;
			document.getElementById('result' + resultcount).className = 'listfocus';
			document.getElementById('result' + resultcount).focus();
		}
	}
	else if (window.event.keyCode == 38) {
		if (document.getElementById('result' + (resultcount-1))) {
			for(i = 0; i < anchors.length;i++) { anchors[i].className = 'listunfocus'; }
			resultcount--;
			document.getElementById('result' + resultcount).className = 'listfocus';
			document.getElementById('result' + resultcount).focus();
		}
	}
}

function addToCartEnter(artikelid, row) {
	if (document.all) { if (window.event.keyCode == 13) { addToCart(artikelid, row); } }
}

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")); }
		resultcount = -1;
	}
	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';
		}
		if (document.all) {
			if ((window.event.keyCode != 40) && (window.event.keyCode != 38)) {
				loadResult(document.getElementById("frmSearch").value);
			}
			iterateList();
		}
		else { loadResult(document.getElementById("frmSearch").value); }
	}
}

function switchProduct(intArticle) {
	
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { returnProduct(http_request) };
	http_request.open('GET','ajax_showarticle_inc.asp?a=' + intArticle + '&m=' + intVAT + '&random=' + Math.random(), bType);
	http_request.send(null);
}

function returnProduct(http_request) {
	
	var string;
	
	if (http_request.readyState == 4) {
   		if (http_request.status == 200) {
			string = http_request.responseText;
			document.getElementById('product').innerHTML = string;
     	}
	 	else {
        	alert('Ett problem uppstod artikeln skulle bytas');
     	}
    }
}

function changeVAT() {
	if (document.getElementById('articlelist')) { switchListMode(); }
	
	else if (document.getElementById('articleprice')) { switchArticlePrice(); }
	else if (document.getElementById('checkout')) { switchCheckoutMode(); }
	else if (document.getElementById('ordrar')) { changeOrderView(); }
	else if (document.getElementById('specialoffers')) { changeSpecialoffers(); }
	else {
		var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
		http_request = createXMLHttpRequest();
		http_request.onreadystatechange = function() { updateCart(http_request) };
		http_request.open('GET','changecart_inc.asp?m=' + intVAT + '&popup=0&random=' + Math.random(), bType);
		http_request.send(null);
	}

}

function notifyPopup(val) {
	http_request = createXMLHttpRequest();
	if (val == true) { var str = 1; } else { var str = 0; }
	bPopup = val;
	http_request.open('GET','notify_cookie_inc.asp?str=' + str + '&random=' + Math.random(), bType);
	http_request.send(null);
}

function chooseOrder(obj, row) {

	if (obj.checked == false) {  
		if (document.getElementById('added' + row)) document.getElementById('orderlista').deleteRow(document.getElementById('added' + row).rowIndex);
	}
	else {
		var currentRow = document.getElementById('tr' + row).rowIndex;
		var oRow = document.getElementById('orderlista').insertRow(currentRow+1);
		var oCell = oRow.insertCell(0);
		var oDiv = document.createElement('div');
		oCell.className = 'selected';
		oRow.id = 'added' + row;
		oCell.colSpan = '7';
	}
	
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { updateHistorik(http_request, oCell) };
	http_request.open('GET','orderrader_inc.asp?ordernr=' + obj.value, bType);
	http_request.send(null);
}

function updateHistorik(http_request, obj) {
	
	var string;
	
	if (http_request.readyState == 4) {
   		if (http_request.status == 200) {
			string = http_request.responseText;
			obj.innerHTML = string;
     	}
	 	else {
        	alert('Ett problem uppstod när orderhistoriken skulle uppdateras');
     	}
    }
}

function makulera(ordernr) {
	if (confirm('Är du säker på att du vill makulera denna order?')) {
		var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
		var stat = document.getElementById('frmStatus').options[document.getElementById('frmStatus').selectedIndex].value;
		http_request = createXMLHttpRequest();
		http_request.onreadystatechange = function() { updateOrderHistorik(http_request) };
		http_request.open('GET','orderhuvud_inc.asp?makulera=' + ordernr + '&m=' + intVAT + '&s=' + stat + '&random=' + Math.random(), bType);
		http_request.send(null);
	}
}

function changeOrderView(nr) {
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	var stat = document.getElementById('frmStatus').options[document.getElementById('frmStatus').selectedIndex].value;
	
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { updateOrderHistorik(http_request) };
	http_request.open('GET','orderhuvud_inc.asp?m=' + intVAT + '&s=' + stat + '&nr=' + nr + '&random=' + Math.random(), bType);
	http_request.send(null);
	
	http_request2 = createXMLHttpRequest();
	http_request2.onreadystatechange = function() { updateCart(http_request2) };
	http_request2.open('GET','changecart_inc.asp?m=' + intVAT + '&popup=0&random=' + Math.random(), bType);
	http_request2.send(null);
}

function updateOrderHistorik(http_request) {
	
	var string;
	
	if (http_request.readyState == 4) {
   		if (http_request.status == 200) {
			string = http_request.responseText;
			document.getElementById('ordrar').innerHTML = string;
     	}
	 	else {
        	alert('Ett problem uppstod när ordrar skulle uppdateras');
     	}
    }
}

function switchListMode() {
	var str = document.getElementById('listmode').action;
	if (document.getElementById('frmView').checked) { var intView = 1; } else { var intView = 0; }
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	if (document.getElementById('frmFilter').value.length > 0) {
		str = str.substring(0,str.indexOf('q=')+2);
		loadArticlegroups(str + document.getElementById('frmFilter').value + '&v=' + intView + '&m=' + intVAT);
	}
	else {
		loadArticlegroups(str + '&v=' + intView + '&m=' + intVAT);
	}
	
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { updateCart(http_request) };
	http_request.open('GET','changecart_inc.asp?m=' + intVAT + '&popup=0&random=' + Math.random(), bType);
	http_request.send(null);
}

function delFromCart(str) {
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { updateCheckout(http_request) };
	http_request.open('GET','change_checkout_inc.asp?delete=' + str + '&random=' + Math.random(), bType);
	http_request.send(null);
	
	http_request2 = createXMLHttpRequest();
	http_request2.onreadystatechange = function() { updateCart(http_request2) };
	http_request2.open('GET','changecart_inc.asp?m=' + intVAT + '&random=' + Math.random(), bType);
	http_request2.send(null);
}

function emptyCheckout() {
	if (confirm('Vill du tömma kundvagnen?')) {
		var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
		document.getElementById('frmCart').options.length = 0;
		document.getElementById('frmCart').options[document.getElementById('frmCart').options.length] = new Option('Kundvagnen är tom', '', true, true);
		http_request = createXMLHttpRequest();
		http_request.onreadystatechange = function() { updateCheckout(http_request) };
		http_request.open('GET','change_checkout_inc.asp?deleteall=1&random=' + Math.random(), bType);
		http_request.send(null);
		
		http_request2 = createXMLHttpRequest();
		http_request2.onreadystatechange = function() { updateCart(http_request2) };
		http_request2.open('GET','changecart_inc.asp?m=' + intVAT + '&random=' + Math.random(), bType);
		http_request2.send(null);
	}
}

function switchCheckoutMode() {
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { updateCheckout(http_request) };
	http_request.open('GET','change_checkout_inc.asp?m=' + intVAT + '&random=' + Math.random(), bType);
	http_request.send(null);
	
	http_request2 = createXMLHttpRequest();
	http_request2.onreadystatechange = function() { updateCart(http_request2) };
	http_request2.open('GET','changecart_inc.asp?m=' + intVAT + '&popup=0&random=' + Math.random(), bType);
	http_request2.send(null);
}

function switchArticlePrice() {
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	var strArticleNr = document.getElementById('frmArticleNr').value
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { updateArticlePrice(http_request) };
	http_request.open('GET','change_price_inc.asp?a=' + strArticleNr + '&m=' + intVAT + '&random=' + Math.random(), bType);
	http_request.send(null);
	
	http_request2 = createXMLHttpRequest();
	http_request2.onreadystatechange = function() { updateCart(http_request2) };
	http_request2.open('GET','changecart_inc.asp?m=' + intVAT + '&popup=0&random=' + Math.random(), bType);
	http_request2.send(null);
}

function recalcCheckout() {
	
	var bVal = false;
	var str = '';
	var row = document.getElementsByName('frmRad');
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	
	for (var i = 0; i < row.length;i++) {
		if (isNaN(document.getElementById('frmAntal' + i).value)) { alert('Antal måste vara ett heltal'); givefocus('frmAntal' + i); bVal = true; }
		else { str = str + row[i].value + '|' + document.getElementById('frmAntal' + i).value + ']['; }
	}
	
	if (str.length == 0) {
		alert('Ingenting att uppdatera');
	} else if (bVal == false) {
		http_request = createXMLHttpRequest();
		http_request.onreadystatechange = function() { updateCheckout(http_request) };
		http_request.open('GET','change_checkout_inc.asp?update=' + str + '&random=' + Math.random(), bType);
		http_request.send(null);
		
		http_request2 = createXMLHttpRequest();
		http_request2.onreadystatechange = function() { updateCart(http_request2) };
		http_request2.open('GET','changecart_inc.asp?m=' + intVAT + '&random=' + Math.random(), bType);
		http_request2.send(null);
	}
}

function updateArticlePrice(http_request) {
	
	var string;
	
	if (http_request.readyState == 4) {
   		if (http_request.status == 200) {
			string = http_request.responseText;
			document.getElementById('articleprice').innerHTML = string;
     	}
	 	else {
        	alert('Ett problem uppstod när artikelpris skulle uppdateras');
     	}
    }
}

function updateCheckout(http_request) {
	
	var string;
	
	if (http_request.readyState == 4) {
   		if (http_request.status == 200) {
			string = http_request.responseText;
			document.getElementById('checkout').innerHTML = string;
     	}
	 	else {
        	alert('Ett problem uppstod när kundvagnen skulle uppdateras');
     	}
    }
}

function changeCart(str) {
	
	var orderid = document.getElementById('frmCart').options[document.getElementById('frmCart').selectedIndex].value;
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	
	if (orderid.length > 0) {
		http_request = createXMLHttpRequest();
		http_request.onreadystatechange = function() { updateCart(http_request) };
		http_request.open('GET','changecart_inc.asp?m=' + intVAT + '&orderid=' + orderid + '&antal=' + str + '&random=' + Math.random(), bType);
		http_request.send(null);
		
		if (document.getElementById('checkout')) {
			http_request2 = createXMLHttpRequest();
			http_request2.onreadystatechange = function() { updateCheckout(http_request2) };
			http_request2.open('GET','change_checkout_inc.asp?random=' + Math.random(), bType);
			http_request2.send(null);
		}
	}
	else { alert('Du kan inte ändra antal eftersom kundvagnen är tom'); }
	return false;
}

function showNotify() {
	if (document.getElementById('notify')) {
		if (bPopup == false) {
			var coords = {x: 0, y: 0};
			coords = findPos(document.getElementById('frmCart'));
			document.getElementById('notify').style.left = (coords.x + 120) + 'px';
			document.getElementById('notify').style.top = (coords.y - 160) + 'px';
			document.getElementById('notify').style.visibility = 'visible';
		}
	}
}

function addToCart(artikelid, row) {
	
	var frmAntal = document.getElementById('frmAntal' + row);
	
	if (frmAntal.value.length == 0) { alert('Du måste ange antal'); frmAntal.focus(); frmAntal.select(); }
	else if (isNaN(frmAntal.value)) { alert('Antal måste vara ett numeriskt värde'); frmAntal.focus(); frmAntal.select(); }
	else if (frmAntal.value == '0') { alert('Antal måste vara ett eller mer'); frmAntal.focus(); frmAntal.select(); }
	else {
		http_request = createXMLHttpRequest();
		http_request.onreadystatechange = function() { updateCart(http_request) };
	  	http_request.open('GET','updatecart_inc.asp?artikelid=' + artikelid + '&antal=' + frmAntal.value + '&random=' + Math.random(), bType);
	  	http_request.send(null);
	}
}

function updateCart(http_request) {
	
	var string = '';
	var aRows;
	var aValues;
   
    if (http_request.readyState == 4) {
   		if (http_request.status == 200) {
			string = http_request.responseText;
			if (string.length > 0) {
				document.getElementById('frmCart').options.length = 0;
				aCart = string.split('[]');
				aRows = aCart[0].split('][');
				for (var i = 0; i < aRows.length - 1; i++) {
					aValues = aRows[i].split('|');
					if (parseInt(aCart[1]) == aValues[0]) { document.getElementById('frmCart').options[document.getElementById('frmCart').options.length] = new Option(aValues[1], aValues[0], true, true); }
					else { document.getElementById('frmCart').options[document.getElementById('frmCart').options.length] = new Option(aValues[1], aValues[0]); }
				}
				if (aCart[4].length > 0) { alert(aCart[4]); }
				document.getElementById("summa").innerHTML = aCart[2];
				if (aCart[3] != '0')  { showNotify(); }
			}
     	}
	 	else {
        	alert('Ett problem uppstod när kundvagnen skulle uppdateras');
     	}
    }
}

function loadArticlegroups(str) {
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { showArticlegroups(http_request) };
	str = str.substring(str.indexOf('?')+1,str.length);
  	http_request.open('GET','ajax_showarticlegroup_inc.asp?r=' + Math.random() + '&' + str, bType);
  	http_request.send(null);
}

function showArticlegroups(http_request) {
	
	var string = '';
   
    if (http_request.readyState == 4) { 
   		if (http_request.status == 200) {
			string = http_request.responseText;
			if (string.length > 0) {
				document.getElementById("articlelist").innerHTML = string;
			}
     	}
	 	else {
			document.write(http_request.responseText);
        	alert('Ett problem uppstod när artikellistan skulle visas'); 
     	} 
    }
}

function loadResult(str) {
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { showResult(http_request) };
  	http_request.open('GET','searchresult.asp?str=' + str + '&random=' + Math.random(), true);
  	http_request.send(null);
}

function showResult(http_request) {
	
	var string = '';
   
    if (http_request.readyState == 4) { 
   		if (http_request.status == 200) {
			string = http_request.responseText;
			if (string.length > 0) {
				resultcount = -1;
				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 checkOrg(str) {
	if (str.length > 0) {
		http_request = createXMLHttpRequest();
		http_request.onreadystatechange = returnOrg;
	  	http_request.open('GET','checkorg_inc.asp?str=' + str, bType);
	  	http_request.send(null);
	}
}

function returnOrg(http_request) {
	
	var string = '';
   
    if (http_request.readyState == 4) { 
   		if (http_request.status == 200) {
			string = http_request.responseText;
			if (string == 1) { document.getElementById('customerexist').style.display = 'block'; }
			else { document.getElementById('customerexist').style.display = 'none'; }
     	}
	 	else {
        	alert('Ett problem uppstod när data skulle hämtas'); 
     	} 
    }
}

function loadForm(str) {
	document.getElementById('customerexist').style.display = 'none';
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { showForm(http_request) };
  	http_request.open('GET','registerform_inc.asp?type=' + str, bType);
  	http_request.send(null);
}

function showForm(http_request) {
	
	var string = '';
   
    if (http_request.readyState == 4) { 
   		if (http_request.status == 200) {
			string = http_request.responseText;
        	document.getElementById("regform").innerHTML = string;
     	}
	 	else {
        	alert('Ett problem uppstod när formulär skulle hämtas'); 
     	} 
    }
}

function changeSpecialoffers() {
	var intVAT = document.getElementById('frmVAT').options[document.getElementById('frmVAT').selectedIndex].value;
	http_request2 = createXMLHttpRequest();
	http_request2.onreadystatechange = function() { updateSpecialoffers(http_request2) };
	http_request2.open('GET','change_specialoffers_inc.asp?m=' + intVAT + '&random=' + Math.random(), bType);
	http_request2.send(null);
	
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { updateCart(http_request) };
	http_request.open('GET','changecart_inc.asp?m=' + intVAT + '&popup=0&random=' + Math.random(), bType);
	http_request.send(null);
}

function updateSpecialoffers(http_request) {
	
	var string = '';
   
    if (http_request.readyState == 4) { 
   		if (http_request.status == 200) {
			string = http_request.responseText;
        	document.getElementById("specialoffers").innerHTML = string;
     	}
	 	else {
        	alert('Ett problem uppstod när kampanj 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")); }
	resultcount = -1;
}

function checkLogin() {

	var form = document.login;
	
	if (form.frmLoginEpost.value.length == 0) 
  	{
		alert('Du har inte angivit någon e-postadress');
		givefocus('frmLoginEpost');
		return false;
  	}
	
	if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.frmLoginEpost.value))
    {
	    alert("Du har fyllt i en ogiltig e-postadress");
	    givefocus('frmLoginEpost');
		return false;
    }
	
	if (form.frmLoginLosenord.value.length == 0) 
  	{
		alert('Du har inte angivit något lösenord');
		givefocus('frmLoginLosenord');
		return false;
  	}
}

function checkConfirm() {
	
	var form = document.confirm;
	var postnr = "-0123456789 ";
	
	if (form.frmLevNamn.value.length == 0) 
  	{
		alert('Du har inte angivit något namn');
		givefocus('frmLevNamn');
		return false;
  	}
	
	if (form.frmLevPostNr.value.length == 0) 
  	{
		alert('Du har inte angivit något postnummer');
		givefocus('frmLevPostNr');
		return false;
  	}
	
	if (form.frmLevPostNr.value.length != 0) {
		for (var i=0;i < form.frmLevPostNr.value.length;i++)
		{
			if (postnr.indexOf(form.frmLevPostNr.value.substring(i,i+1))==-1)
			{
				alert('Postnummer ska vara numeriskt');
				givefocus('frmLevPostNr');
				return false;
			}
		}
	}
	
	if (form.frmLevStad.value.length == 0) 
  	{
		alert('Du har inte angivit någon ort');
		givefocus('frmLevStad');
		return false;
  	}
}

function saveLevAdress() {
	
	var form = document.register;
	var postnr = "-0123456789 ";
    
	if (form.frmAdressA1.value.length == 0) 
  	{
		alert('Du har inte angivit någon adress');
		givefocus('frmAdressA1');
		return false;
  	}
	
	if (form.frmPostNr.value.length == 0) 
  	{
		alert('Du har inte angivit något postnummer');
		givefocus('frmPostNr');
		return false;
  	}
	
	if (form.frmPostNr.value.length != 0) {
		for (var i=0;i < form.frmPostNr.value.length;i++)
		{
			if (postnr.indexOf(form.frmPostNr.value.substring(i,i+1))==-1)
			{
				alert('Postnummer ska vara numeriskt');
				givefocus('frmPostNr');
				return false;
			}
		}
	}
	
	if (form.frmOrt.value.length == 0) 
  	{
		alert('Du har inte angivit någon ort');
		givefocus('frmOrt');
		return false;
  	}
	
	var str = 'frmAdressA1=' + encodeURI(document.getElementById("frmAdressA1").value) +
			  '&frmAdressA2=' + encodeURI(document.getElementById("frmAdressA2").value) +
			  '&frmPostNr=' + encodeURI(document.getElementById("frmPostNr").value) +
			  '&frmOrt=' + encodeURI(document.getElementById("frmOrt").value) +
			  '&frmLand=' + encodeURI(document.getElementById("frmLand").options[document.getElementById("frmLand").selectedIndex].value) +
			  '&frmAdressB1=' + encodeURI(document.getElementById("frmAdressB1").value) +
              '&frmAdressB2=' + encodeURI(document.getElementById("frmAdressB2").value) +
			  '&frmPostNrB=' + encodeURI(document.getElementById("frmPostNrB").value) +
			  '&frmOrtB=' + encodeURI(document.getElementById("frmOrtB").value) +
			  '&frmCustomerID=' + encodeURI(document.getElementById("frmCustomerID").value);
	
	http_request = createXMLHttpRequest();
	http_request.onreadystatechange = function() { returnSave(http_request) };
	http_request.open('POST','save_adress_inc.asp', true);
	http_request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    http_request.setRequestHeader('Content-length', str.length);
    http_request.setRequestHeader('Connection', 'close');
  	http_request.send(str);
	return false;
}

function returnSave() {
	
	var string = '';
	
    if (http_request.readyState == 4) { 
   		if (http_request.status == 200) {
			string = http_request.responseText;
			document.getElementById('accountsaved').innerHTML = string;
        	document.getElementById('accountsaved').style.display = 'block';
     	}
	 	else {
        	alert('Ett problem uppstod när data skulle sparas'); 
     	} 
    }
}

function checkRegistration() {
	
	var form = document.register;
	var telefon = "-()0123456789 ";
	var postnr = "-0123456789 ";
	var namn;
	
	if (document.getElementById('frmTyp1').checked) { namn = 'företagsnamn'; } else { namn = 'namn'; } 
	
	if (form.frmOrgnr) {
		if (form.frmOrgnr.value.length == 0) 
	  	{
			alert('Du har inte angivit något Org/VAT-nummer');
			givefocus('frmOrgnr');
			return false;
	  	}
		
		if (form.frmOrgnr.value.length != 0) {
			for (var i=0;i < form.frmOrgnr.value.length;i++)
			{
				if (postnr.indexOf(form.frmOrgnr.value.substring(i,i+1))==-1)
				{
					alert('Org/VAT-nummer innehåller otillåtna tecken');
					givefocus('frmMobil');
					return false;
				}
			}
		}
	}
	
	if (form.frmNamn.value.length == 0) 
  	{
		alert('Du har inte angivit något ' + namn);
		givefocus('frmNamn');
		return false;
  	}
	
	if (form.frmPostNr.value.length == 0) 
  	{
		alert('Du har inte angivit något postnummer');
		givefocus('frmPostNr');
		return false;
  	}
	
	if (form.frmPostNr.value.length != 0) {
		for (var i=0;i < form.frmPostNr.value.length;i++)
		{
			if (postnr.indexOf(form.frmPostNr.value.substring(i,i+1))==-1)
			{
				alert('Postnummer ska vara numeriskt');
				givefocus('frmPostNr');
				return false;
			}
		}
	}
	
	if (form.frmStad.value.length == 0) 
  	{
		alert('Du har inte angivit någon ort');
		givefocus('frmStad');
		return false;
  	}
	
	if(form.frmLand)
	{
	if (form.frmLand.options[form.frmLand.selectedIndex].value.length == 0) {
		alert('Du har inte angivit något land');
		givefocus('frmStad');
		return false;
	}
	}
	
	if (form.frmTelefon.value.length == 0) 
  	{
		alert('Du har inte angivit något telefonnummer');
		givefocus('frmTelefon');
		return false;
  	}
	
	if (form.frmTelefon.value.length != 0) {
		for (var i=0;i < form.frmTelefon.value.length;i++)
		{
			if (telefon.indexOf(form.frmTelefon.value.substring(i,i+1))==-1)
			{
				alert('Telefonnummer innehåller otillåtna tecken');
				givefocus('frmTelefon');
				return false;
			}
		}
	}
	
	if (form.frmMobil.value.length != 0) {
		for (var i=0;i < form.frmMobil.value.length;i++)
		{
			if (telefon.indexOf(form.frmMobil.value.substring(i,i+1))==-1)
			{
				alert('Mobilnummer innehåller otillåtna tecken');
				givefocus('frmMobil');
				return false;
			}
		}
	}
	
	if (form.frmEpost.value.length == 0) 
  	{
		alert('Du har inte angivit någon e-post');
		givefocus('frmEpost');
		return false;
  	}
	
	 else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.frmEpost.value))
    {
	    alert("Du har fyllt i en ogiltig e-postadress");
	    givefocus('frmEpost');
		return false;
    }
	
	if (form.frmLosenord.value.length == 0) 
  	{
		alert('Du har inte angivit något lösenord');
		givefocus('frmLosenord');
		return false;
  	}
	
	if (form.frmLosenord.value != form.frmLosenord2.value) 
  	{
		alert('Det upprepade lösenordet matchar inte lösenordet');
		givefocus('frmLosenord2');
		return false;
  	}

}

function addKeyHandler(element) {

	element._keyObject = new Array();
	element._keyObject["keydown"] = new Array();
	element._keyObject["keyup"] = new Array();
	element._keyObject["keypress"] = new Array();
	
	element.addKeyDown = function (keyCode, action) {
		element._keyObject["keydown"][keyCode] = action;
	}
	
	element.removeKeyDown = function (keyCode) {
		element._keyObject["keydown"][keyCode] = null;
	}

	element.addKeyUp = function (keyCode, action) {
		element._keyObject["keyup"][keyCode] = action;
	}
	
	element.removeKeyUp = function (keyCode) {
		element._keyObject["keyup"][keyCode] = null;
	}
	
	element.addKeyPress = function (keyCode, action) {
		element._keyObject["keypress"][keyCode] = action;
	}
	
	element.removeKeyPress = function (keyCode) {
		element._keyObject["keypress"][keyCode] = null;
	}
	
	function handleEvent() {
		var type = window.event.type;
		var code = window.event.keyCode;
		
		if (element._keyObject[type][code] != null) 
			element._keyObject[type][code]();
	}
	
	element.onkeypress = handleEvent;
	element.onkeydown = handleEvent;
	element.onkeyup = handleEvent;
} 

addEvent(window,'load', initPage, false);
addEvent(window,'resize', initPage, false);

