function strpos(str, ch) {
for (var i = 0; i < str.length; i++)
if (str.substring(i, i+1) == ch) return i;
return -1;
}



// Popup window opened in the middle of the browser window
function popup_window(url, windowName, w, h) {
	newwindow = window.open(url, windowName, 'top='+((screen.height - h) / 2)+', left='+((screen.width - w) / 2)+', toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h)
	newwindow.focus();
	return newwindow;
}

// Popup window correct size for /kundeservice or /om_billigvvs popup-versions
function popup_info(url, nowrap) {
	if(nowrap == '0' || nowrap == 0) {
		var nowrap_str = '';
	} else {
		var nowrap_str = '?nowrap=1';
	}
	popup_window(url + nowrap_str, 'InfoCenter', 615, 520);
}

function showLarge(fn) {
	window.open('/shop/ViewPicture.asp?picturefilename=' + fn, "LargePicture", "width=520,height=520,scrollbars=yes");
}

// Popup window -> Handelsfinans Ekspres Lån
function popup_handelsfinans() {
	popup_window('/handelsfinans/', 'EkspresLaan', 600, 550);
}

// Print window
function print_window() {
	bV = parseInt(navigator.appVersion)
	if (bV >= 4) window.print()
}

// Change variant
function change_product(prod_sec_id, group_path) {
	if (group_path) {
		document.location.href = escape(group_path) + "--" + prod_sec_id + ".html";
	}
	else {
		document.location.href = prod_sec_id + ".html";
	}
}

// Get body element depending on browser version
function browser_body() {
	if(navigator.appName == 'Opera') {
		return document.body;
	} else {
		return (document.compatMode && document.compatMode!="BackCompat") ? document.documentElement : document.body;
	}
}


//
// Display/hide confirmation of adding products to basket
//
var basketConfirmTimer = 0;

function display_cursor() {
	// Alias for display_confirmAddToBasket()
	display_confirmAddToBasket();
}
function display_confirmAddToBasket() {
	cookie = getCookie('Toplevel_HideBasketConfirm');
	if(cookie == '1') {
		return false;
	}
	clearTimeout(basketConfirmTimer);
	object = document.getElementById('Toplevel_ConfirmAddToBasket');
	checkbox = document.getElementById('disablePopup');
	if(object) {
		width = 570;
		height = 256;
		elem = browser_body();
		checkbox.checked = false;
		object.style.width = width + 'px';
		object.style.height = height + 'px';
		object.style.top = (elem.scrollTop + (elem.clientHeight-height)/2) + 'px';
		object.style.left = ((elem.clientWidth-width)/2) + 'px';
		object.style.display = 'block';
		object.style.visibility = 'visible';
		basketConfirmTimer = setTimeout("hide_confirmAddToBasket();", 5000);
	}


}
function hide_confirmAddToBasket() {
	object = document.getElementById('Toplevel_ConfirmAddToBasket');
	checkbox = document.getElementById('disablePopup');
	cookie = getCookie('Toplevel_HideBasketConfirm');
	if(object) {
		object.style.display = 'none';
		object.style.visibility = 'hidden';
	}
	clearTimeout(basketConfirmTimer);
	if(checkbox.checked == true) {
		setCookie('Toplevel_HideBasketConfirm', '1', 3);
	} else {
		eraseCookie('Toplevel_HideBasketConfirm');		
	}
}
function state_checkboxConfirm() {
	object = document.getElementById('disablePopup');
	if(object.checked == true) {
		object.checked = false;
	} else {
		object.checked = true;
	}
}

// Preload confirmation box background
_i = new Image(570, 256)
_i.src = '/media/toplevel/images/confirmAddToBasket/bg.gif';




// Make a AJAX-call to arg url. Arg handler must be a function
// that takes exactly one (1) argument = ajax request object
function AjaxCall(url, handler) {
	var GetXmlHttpObject = function() {
		var xmlHttp = null;
		try {
			xmlHttp = new XMLHttpRequest();
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		return xmlHttp;
	}
	xmlHttp = GetXmlHttpObject();
	xmlHttp.onreadystatechange = function() {
		handler(xmlHttp);
	}
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}


// ---------------------------------------
//  COOKIES
// ---------------------------------------


function setCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}