var in_popcount = 0;

function unix_gettime(){
	now = new Date();
	return now.getTime();
}

function OpenPicture(in_url){
	in_popcount = in_popcount + 1;
	window.open('/popup_picture.php?picture=' + encodeURIComponent(in_url), "foniks_popup_" + in_popcount, 'width=100, height=100, left=100, top=50');
}

function OpenPop(in_url){
	in_popcount = in_popcount + 1;
	open('/iframe.php?show=' + in_url, 'foniks_popup_' + in_popcount, 'width=400, height=600, left=100, top=50, toolbar=no, scrollbars=yes');
}

function OpenAdvPop(in_url, in_width, in_height){
	in_popcount = in_popcount + 1;
	open('/iframe.php?show=' + in_url, 'foniks_popup_' + in_popcount, 'width=' + in_width + ', height=' + in_height + ', left=40, top=40, toolbar=no, scrollbars=yes');
}

function OpenAdvPop2(in_url, in_width, in_height){
	in_popcount = in_popcount + 1;
	open(in_url, "foniks_popup_" + in_popcount, "width=" + in_width + ", height=" + in_height + ", left=40, top=40, toolbar=no, scrollbars=yes");
}

function OpenVare(product_id){
	location.href = "/?show=products_show&product_id=" + product_id;
}

function OpenVareEdit(product_id){
	location.href = "/?show=products_edit&product_id=" + product_id;
}

function OrderProduct(product_id){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=iframehelper&choice=orderproduct&product_id=" + product_id,
		cache: false,
		async: true,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") != "1"){
				if (jqXHR.getResponseHeader("ErrorMsg")){
					alert(jqXHR.getResponseHeader("ErrorMsg"));
				} else {
					alert("Der opstod en fejl:\n\n" + data);
				}
			} else {
				modal({width: "500px", height: "300px", url: "/index_clean.php?show=products_inbasketchoice"});
			}

			updateBasket();
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function OrderProductDemo(product_id){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=iframehelper&choice=orderproduct&demo=true&product_id=" + product_id,
		cache: false,
		async: true,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") != "1"){
				if (jqXHR.getResponseHeader("ErrorMsg")){
					alert(data);
					alert(jqXHR.getResponseHeader("ErrorMsg"));
				} else {
					alert("Der opstod en fejl:\n\n" + data);
				}
			} else {
				modal({width: "500px", height: "300px", url: "/index_clean.php?show=products_inbasketchoice"});
			}

			updateBasket();
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function OrderBundle(bundle_id) {
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=iframehelper&choice=orderbundle&bundle_id=" + bundle_id,
		cache: false,
		async: true,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") != "1"){
				if (jqXHR.getResponseHeader("ErrorMsg")){
					alert(jqXHR.getResponseHeader("ErrorMsg"));
				}else{
					alert("Der opstod en fejl:\n\n" + data);
				}
			}else{
				modal({width: "500px", height: "300px", url: "/index_clean.php?show=products_inbasketchoice"});
			}

			updateBasket();
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function OrderBundleProds(bundle_id, prods){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=iframehelper&choice=orderbundle&bundle_id=" + bundle_id + "&prods=" + prods,
		cache: false,
		async: true,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") != "1"){
				if (jqXHR.getResponseHeader("ErrorMsg")){
					alert(jqXHR.getResponseHeader("ErrorMsg"));
				}else{
					alert("Der opstod en fejl:\n\n" + data);
				}
			}else{
				modal({width: "500px", height: "300px", url: "/index_clean.php?show=products_inbasketchoice"});
			}

			updateBasket();
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function productChoosePriceGroup(classname, object_id, pgroup_id)
{
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=products_pricegroups_choose&choice=dosavegroup&classname=" + classname + "&object_id=" + object_id + "&pgroup_id=" + pgroup_id,
		async: false,
		cache: false,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") != "1"){
				alert("Der opstod en fejl:\n\n" + data);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function productRemovePriceGroup(link_id){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=products_pricegroups_choose&choice=doremovegroup&link_id=" + link_id,
		async: false,
		cache: false,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") != "1"){
				alert("Der opstod en fejl:\n\n" + data);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function productTitle(product_id){
	return $.ajax({
		type: "GET",
		url: "/index_clean.php?show=products_ajax&choice=info&product_id=" + product_id,
		async: false,
		cache: false
	}).getResponseHeader("Title");
}

function product_pictures_viewer(args){
	urlstr = "/index_clean.php?show=products_pictures_viewer";

	for(key in args){
		urlstr += "&" + key + "=" + args[key];
	}

	modal({title: locale_strings["view_pictures"], url: urlstr, width: "800px", height: "660px"});
}

function getSelected(opt) {
	var selected = new Array();
	var index = 0;

	for (var intLoop = 0; intLoop < opt.length; intLoop++){
		if ((opt[intLoop].selected)) {
			index = selected.length;
			selected[index] = new Object;
			selected[index].value = opt[intLoop].value;
			selected[index].index = intLoop;
		}
	}

	return selected;
}

function MakeVar(opt) {
	var sel = getSelected(opt);
	var strSel = "";
	var in_first = true;

	for (var item in sel){
		if (in_first == true){
			in_first = false;
		}else{
			strSel += ";";
		}

		strSel += sel[item].value;
	}

	return strSel;
}

function select_makevar(selectbox){
	var sel_count = 0;
	var in_var = "";

	while(sel_count < selectbox.options.length){
		if (selectbox.options[sel_count].selected == true){
			if (in_var){
				in_var += ";";
			}

			in_var += selectbox.options[sel_count].value;
		}

		sel_count++;
	}

	return in_var;
}

function number_format(number, decimals, csep, tsep){
	number = parseFloat(number);

	if (decimals == 1){
		multi = 10;
	}

	if (decimals == 2){
		multi = 100;
	}

	if (decimals == 3){
		multi = 1000;
	}

	if (decimals == 0){
		multi = 1;
	}

	number = Math.round(number * multi) / multi;

	if (number > 999 || number < -999){
		var number = "" + number + "";
		var lengthfrom = number.indexOf(".");

		if (lengthfrom == -1){
			var lengthfrom = number.length;
		}

		var newnumber = "";
		var count = 0;

		while(lengthfrom > 0){
			onechar = number.substr(lengthfrom - 1, 1);

			if (count >= 3){
				if (onechar != "-"){
					newnumber = tsep + newnumber;
					count = 0;
				}
			}

			newnumber = onechar + newnumber;

			lengthfrom--;
			count++;
		}
	}else{
		var number = "" + number + "";

		lengthto = number.indexOf(".");

		if (lengthto == -1){
			lengthto = number.length;
		}

		newnumber = number.substr(0, lengthto);
	}

	if (decimals > 0){
		commanumb_offset = number.indexOf(".");

		if (commanumb_offset != -1){
			var commanumb = number.substr(commanumb_offset + 1);
		}else{
			var commanumb = "";
		}

		while(commanumb.length < decimals){
			commanumb += "0";
		}

		number = newnumber + csep + commanumb;
	}else{
		number = newnumber;
	}

	return number;
}

function validate_email(emailstring){
	if (emailstring.indexOf(".") > 0 && emailstring.indexOf("@") > 0){
		return true;
	}else{
		return false;
	}
}

function str_replace(str_replace, str_with, tha_string){
	var i = tha_string.indexOf(str_replace);

	while(i > -1){
		tha_string = tha_string.replace(str_replace, str_with);
		i = tha_string.indexOf(str_replace);
	}

	return tha_string;
}

/** Handels graphical effect for some menus. */
function changemenu(element, method){
	if (method == "over"){
		if (element.style.backgroundColor.toLowerCase() != "#c0c0c0"){
			element.style.backgroundColor='#6B656B';
		}
	}

	if (method == "out"){
		if (element.style.backgroundColor.toLowerCase() != "#c0c0c0"){
			element.style.backgroundColor='#000000';
		}
	}
}

/** Draws a popup-box. */
var varedata = new Array();

function PopupBox(paras){
	if (!paras["width"]){
		paras["width"] = "500px";
	}

	if (!paras["height"]){
		paras["height"] = "";
	}

	$("#popupbox").css("width", paras["width"]);
	$("#popupbox").css("height", paras["height"]);
	$("#popupbox_content").html(paras["content"]);
}

function PopupBoxActivate(event){
	$("#popupbox").stop();
	$("#popupbox").css("display", "block");
	$("#popupbox").fadeTo(250, 1);
}

function PopupBoxActivateQuick(event){
	$("#popupbox").stop();
	$("#popupbox").css("display", "block");
	$("#popupbox").fadeTo(0, 1);
}

/** Hides the popup-box. */
function PopupBoxHide(){
	$("#popupbox").stop();
	$("#popupbox").fadeTo(250, 0, function(){
		$("#popupbox").css("display", "none");
		$("#popupbox").css("top", "1px");
		$("#popupbox").css("left", "1px");
	});
}

function PopupBoxHideQuick(){
	$("#popupbox").css("display", "none");
	$("#popupbox").css("top", "1px");
	$("#popupbox").css("left", "1px");
	$("#popupbox").stop();
	$("#popupbox").fadeTo(0, 0);
}

function PopupBoxShow(boxmode, id){
	$("#popupbox").css("height", "");

	if (boxmode == "text"){
		$("#popupbox_content").html(id);
	}else if(boxmode == "order"){
		$("#popupbox_content").html(locale_strings["loading"] + "…");
		$.ajax({
			type: "GET",
			url: "/iframe.php?show=iframehelper&choice=loadorder&order_id="  + id,
			async: false,
			cache: false,
			success: function(data, textStatus, jqXHR) {
				$("#popupbox_content").html(data);
			},
			complete: function(jqXHR, textStatus) {
				return false;
			},
			error: function(jqXHR, textStatus, errorThrown) {
				if (textStatus = 'abort') {
					return false;
				}
				alert("Der opstod en fejl:\n\n" + textStatus);
			}
		});
	}else if(boxmode == "pproduct"){
		$("#popupbox_content").html(locale_strings["loading"] + "…");
		$.ajax({
			type: "GET",
			url: "/iframe.php?show=iframehelper&choice=loadpproduct&pproduct_id=" + id,
			async: false,
			cache: false,
			success: function(data, textStatus, jqXHR) {
				$("#popupbox_content").html(data);
			},
			complete: function(jqXHR, textStatus) {
				return false;
			},
			error: function(jqXHR, textStatus, errorThrown) {
				if (textStatus = 'abort') {
					return false;
				}
				alert("Der opstod en fejl:\n\n" + textStatus);
			}
		});
	}else{
		alert("Undefined boxmode: " + boxmode);
	}
}

/** Updates the popupbox. */
function PopupBoxUpdate(event){
	if (in_browser == "chrome" || in_browser == "safari" || in_browser == "ie" && (in_browser_v == "6" || in_browser_v == "5")){
		scroll_top = document.body.scrollTop;
		scroll_left = document.body.scrollLeft;
	}else{
		scroll_top = document.documentElement.scrollTop;
		scroll_left = document.documentElement.scrollLeft
	}

	popbox_width = $("#popupbox").width();
	popbox_height = $("#popupbox").height();

	pos_top = event.clientY + scroll_top + 17;
	pos_left = event.clientX + scroll_left + 10;

	if (in_browser == "ie"){
		sh = document.body.offsetHeight + scroll_top;
		sw = document.body.offsetWidth + scroll_left;
	}else{
		sh = window.innerHeight + scroll_top;
		sw = window.innerWidth + scroll_left;
	}

	popbox_bottom_pos = pos_top + popbox_height + 30;
	popbox_right_pos = pos_left + popbox_width + 35;

	if (popbox_bottom_pos > sh){
		pos_top = event.clientY + scroll_top - popbox_height - 25;
	}

	if (popbox_right_pos > sw){
		pos_left = event.clientX + scroll_left - popbox_width - 25;
	}

	$("#popupbox").css("left", pos_left + "px");
	$("#popupbox").css("top", pos_top + "px");
}


/** Handels the event when the cursor goes over an item in the popup-menu. */
function popmenu_over(poptd){
	poptd.style.backgroundImage = "url('/images/menu/menu_bg_active.jpg')";

	if (in_browser == "ie" && in_browser_v == "6"){
		doSelects("hide");
	}
}

/** Handels the event when the cursor leaves an popmenu-item. */
function popmenu_out(poptd){
	poptd.style.backgroundImage = "url('/images/menu/menu_bg.jpg')";

	if (in_browser == "ie" && in_browser_v == "6"){
		doSelects("show");
	}
}

/** Handels the event when the cursor goes over a FC-button. */
function fcbtn_over(fcbtn, color){
	if (color == "black"){
		fcbtn.style.backgroundImage = "url('/images/fcbtn_bg_active.jpg')";
	}else if(color == "blue"){
		fcbtn.style.backgroundImage = "url('/images/fcbtn_bg_active_blue.jpg')";
	}else{
		alert("Unsupported color: " + color);
	}
}

/** Handels the event when the cursor leaves a FC-button. */
function fcbtn_out(fcbtn, color){
	if (color == "black"){
		fcbtn.style.backgroundImage = "url('/images/fcbtn_bg.jpg')";
	}else if(color == "blue"){
		fcbtn.style.backgroundImage = "url('/images/fcbtn_bg_blue.jpg')";
	}else{
		alert("Unsupported color: " + color);
	}
}

/** Hides all select-boxes on the site (to fix IE6-bug - Microsoft sucks???). */
function doSelects(inDo){
	if (inDo == "hide"){
		for (var i = 0; i < document.getElementsByTagName("select").length; i++) {
			document.getElementsByTagName("select")[i].style.visibility = "hidden";;
		}
	}

	if (inDo == "show"){
		for (var i = 0; i < document.getElementsByTagName("select").length; i++) {
			document.getElementsByTagName("select")[i].style.visibility = "visible";;
		}
	}
}

function systemChangeView(system_id, view){
	product_div = document.getElementById("divsystem_product_" + system_id);
	spec_div = document.getElementById("divsystem_spec_" + system_id);
	descr_div = document.getElementById("divsystem_descr_" + system_id);

	product_show = "none";
	spec_show = "none";
	descr_show = "none";

	product_weight = "normal";
	spec_weight = "normal";
	descr_weight = "normal";

	if (view == "product"){
		product_show = "block";
		product_weight = "bold";
	}else if(view == "spec"){
		spec_show = "block";
		spec_weight = "bold";
	}else if(view == "descr"){
		descr_show = "block";
		descr_weight = "bold";
	}

	product_div.style.display = product_show;
	spec_div.style.display = spec_show;
	descr_div.style.display = descr_show;

	document.getElementById("divsystitle_product_" + system_id).style.fontWeight = product_weight;
	document.getElementById("divsystitle_spec_" + system_id).style.fontWeight = spec_weight;
	document.getElementById("divsystitle_descr_" + system_id).style.fontWeight = descr_weight;
}

function CreateFakeProduct(pricelist_product_id){
	ifrupd.src = "/iframe.php?show=pricelists_newproducts_control&choice=docreatefakeproduct&pprod_id=" + pricelist_product_id;
}

function orderSystem(system_id){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=iframehelper&choice=ordersystem&system_id=" + system_id,
		cache: false,
		async: false,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") != "1"){
				alert("Der opstod en fejl.\n\n" + data);
			}

			updateBasket();
			modal({url: "/index_clean.php?show=products_inbasketchoice"});
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function updateBasket(){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=iframehelper&choice=updatebasket",
		cache: false,
		async: false,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("Success") == "1"){
				div_basket = $("#div_baskettekst");
				div_basket.slideUp("fast", function(){
					div_basket.html(data);
					div_basket.slideDown("fast");
				});
			} else {
				alert("Der opstod en fejl ved opdatering af kurven:\n\n" + data);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function openRecommendWindow(product_id, type_string){
	modal({width: "500px", height: "330px", url: "/index_clean.php?show=products_recommend&type=" + type_string + "&product_id=" + product_id});
}

function openEvaluateWindow(product_id, type_string){
	modal({width: "500px", height: "330px", url: "/index_clean.php?show=products_evaluate&type="  + type_string + "&product_id=" + product_id})
}

function addShoppingList(product_id){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=products_shoppinglist&addproduct=" + product_id,
		cache: false,
		async: false,
		success: function(data, textStatus, jqXHR) {
			if (jqXHR.getResponseHeader("everythingok") == "1"){
				alert("Produktet blev tilføjet til indkøbslisten.");
			}else{
				alert("Der gik noget galt.\n\n" + data);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function ShowHide(tha_element){
	if (tha_element.style.display == "none"){
		tha_element.style.display = "block";
	}else{
		tha_element.style.display = "none";
	}
}

function ShowHideSpec(tha_element, showhide){
	$(tha_element).stop();

	if (showhide == "show"){
		if (tha_element.style.display != "block"){
			$(tha_element).fadeTo(0, 0);
			tha_element.style.display = "block";
			$(tha_element).fadeTo(250, 1);
		}
	}else if(showhide == "hide"){
		$(tha_element).fadeTo(250, 0, function(){
			tha_element.style.display = "none";
		});
	}else{
		alert("Wrong: " + showhide);
	}
}

function ShowHideVisSpec(tha_element, showhide){
	$(tha_element).stop();

	if (showhide == "show"){
		if (tha_element.style.visibility != "visible"){
			$(tha_element).fadeTo(0, 0);
			tha_element.style.visibility = "visible";
			$(tha_element).fadeTo(150, 1);
		}
	}else if(showhide == "hide"){
		$(tha_element).fadeTo(150, 0, function(){
			tha_element.style.visibility = "hidden";
		});
	}else{
		alert("Wrong: " + showhide);
	}
}

var fctabs = new Array();
function fctabs_show(tabs_id, tha_id){
	func_name = "fctabs_" + tha_id + "_load";
	if (eval("typeof(" + func_name + ")") == "function"){
		eval(func_name).call();
	}

	if (fctabs[tabs_id] && fctabs[tabs_id]["prev"]){
		tha_tab = document.getElementById("fctabs_" + fctabs[tabs_id]["prev"]);
		tha_tab_header = document.getElementById("fctabs_header_" + fctabs[tabs_id]["prev"]);

		if (tha_tab_header.className == "fctabs_header_active"){
			tha_tab_header.className = "fctabs_header_inactive";
		}else if(tha_tab_header.className == "fctabs_header_active_first"){
			tha_tab_header.className = "fctabs_header_inactive_first";
		}

		$(tha_tab).stop();
		$(tha_tab).fadeOut(250, function(){
			$(tha_tab).css("display", "none");

			tha_tab = document.getElementById("fctabs_" + tha_id);
			tha_tab_header = document.getElementById("fctabs_header_" + tha_id);

			if (tha_tab_header.className == "fctabs_header_inactive"){
				tha_tab_header.className = "fctabs_header_active";
			}else if(tha_tab_header.className == "fctabs_header_inactive_first"){
				tha_tab_header.className = "fctabs_header_active_first";
			}

			$(tha_tab).fadeIn(250);

			fctabs[tabs_id]["prev"] = tha_id;
		});
	}else{
		tha_tab = document.getElementById("fctabs_" + tha_id);
		tha_tab_header = document.getElementById("fctabs_header_" + tha_id);

		if (!tha_tab || !tha_tab_header){
			alert("Tab not found: " + tha_id);
		}

		if (tha_tab_header.className == "fctabs_header_inactive"){
			tha_tab_header.className = "fctabs_header_active";
		}else if(tha_tab_header.className == "fctabs_header_inactive_first"){
			tha_tab_header.className = "fctabs_header_active_first";
		}

		$(tha_tab).stop();
		$(tha_tab).css("display", "block");

		fctabs[tabs_id]["prev"] = tha_id;
	}
}

function modalOpen(tha_subject, tha_content){
	modalParas({"title": tha_subject, "content": tha_content})
}

function modalClose(){
	$.modal.close();
}

function modal(paras){
	if (!paras["width"]){
		paras["width"] = "640px";
	}

	if (!paras["height"]){
		paras["height"] = "480px";
	}

	modal_opts = {
		overlay: 80,
		overlayCss: {backgroundColor: "#000000"},
		containerId: "simplemodal_box",
		onOpen: function(dialog){
			dialog.overlay.fadeIn(400, function(){
				dialog.container.fadeIn(0, function(){
					dialog.data.fadeIn(400, function(){

					});
				});
			});
		},
		onClose: function(dialog){
			$("#flowplayer").html("&nbsp;");

			dialog.data.fadeOut(400, function(){
				dialog.container.fadeOut(0, function(){
					dialog.overlay.fadeOut(400, function(){
						$.modal.close();
					});
				});
			});
		}
	};

	if (paras["url"]){
		$.ajax({
			type: "GET",
			url: paras["url"],
			cache: false,
			async: false,
			success: function(data, textStatus, jqXHR) {
				paras["content"] = data;
			},
			complete: function(jqXHR, textStatus) {
				return false;
			},
			error: function(jqXHR, textStatus, errorThrown) {
				if (textStatus = 'abort') {
					return false;
				}
				alert("Der opstod en fejl:\n\n" + textStatus);
			}
		});
	}

	tha_style = "";
	if (paras["width"]){
		tha_style += "width: " + paras["width"] + ";";

		if (in_browser == "ie"){
			xpos = (document.documentElement.clientWidth / 2) - (parseInt(paras["width"]) / 2);
		}else{
			xpos = (window.innerWidth / 2) - (parseInt(paras["width"]) / 2);
		}
	}
	if (paras["height"]){
		tha_style += "height: " + paras["height"] + ";";

		if (in_browser == "ie"){
			ypos = (document.documentElement.clientHeight / 2) - (parseInt(paras["height"]) / 2);
		}else{
			ypos = (window.innerHeight / 2) - (parseInt(paras["height"]) / 2);
		}

		modal_opts["position"] = [ypos, xpos];
	}

	realcontent = "<div class=\"simplemodal_box\" style=\"" + tha_style + "\">";

	if (paras["title"]){
		realcontent += "<div class=\"simplemodal_header\">";
		realcontent += "<div style=\"float: right; font-size: 9px; font-weight: normal; padding-top: 5px;\"><a href=\"javascript: modalClose();\">[" + locale_strings["close"] + "]</a></div>";

		realcontent += paras["title"] + "</div>";
	}

	realcontent += paras["content"];
	realcontent += "</div>";

	$(realcontent).modal(modal_opts);
}

function getidfromurl(url){
	string = "asd" + url;
	tha_match = string.match(/id=([0-9]+)/);
	return tha_match[1]
}

function addToWorkspace(object_class, object_ids){
	$.ajax({
		type: "POST",
		url: "/index_clean.php?show=workspace&choice=addtoworkspace&object_class=" + object_class,
		data: "ids=" + object_ids,
		async: false,
		cache: false,
		success: function(data, textStatus, jqXHR) {
			if (!jqXHR.getResponseHeader("WorkspaceSuccess")){
				alert(data);
				alert(locale_strings["not_added_to_workspace"]);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function workspaceRemove(object_ids){
	$.ajax({
		type: "POST",
		url: "/index_clean.php?show=workspace&choice=remove",
		data: "ids=" + object_ids,
		async: false,
		cache: false,
		success: function(data, textStatus, jqXHR) {
			if (!jqXHR.getResponseHeader("WorkspaceSuccess")){
				alert(data);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function product_compare_add(product_id){
	$.ajax({
		type: "GET",
		url: "/index_clean.php?show=products_ajax&choice=compare_add&product_id=" + product_id,
		cache: false,
		async: true,
		success: function(data, textStatus, jqXHR) {
			if (data.replace(/\s+/,"").length > 0){
				alert(data);
			}else{
				alert(locale_strings["product_compare_added"]);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function product_new_comment(product_id){
	modal({
		title: locale_strings["new_comment"],
		url: "/index_clean.php?show=comments_writehtml&object_class=fcom_product&object_id=" + product_id + "&backurl=" + escape("/?show=products_edit&section=comments&product_id=" + product_id)
	});
}

function product_ordernote(product_id){
	modal({
		title: locale_strings["new_ordernote"],
		url: "/index_clean.php?show=products_ordernotes_edit&product_id=" + product_id
	});
}

function product_ordernote_edit(onote_id){
	modal({
		title: locale_strings["edit_ordernote"],
		url: "/index_clean.php?show=products_ordernotes_edit&onote_id=" + onote_id
	});
}

function pbTextChange(texele){
	newamount = texele.value;
	newamount = parseInt(newamount.replace(".", "").replace(",", ".").replace(" kr.", ""));

	tha_width = parseInt($("#pb_fullbar").css("width"));
	perc = newamount / pb_price_max;

	newleft = tha_width * perc;

	if (newleft < 0){
		newleft = 0;
	}else if(newleft > pb_max_left){
		newleft = pb_max_left;
	}

	if (texele.id == "texpricestart"){
		pb_bar = $("#pb_bar_start");
		pb_bar.css("left", newleft + "px");
		$("#texpricemin").val(newamount);
	}else if(texele.id == "texpriceend"){
		pb_bar = $("#pb_bar_end");
		pb_bar.css("left", newleft + "px");
		$("#texpricemax").val(newamount);
	}

	reloadProducts();
}

function pbDown(event, barele){
	if (barele.id == "pb_bar"){
		pb_bar = $("#pb_bar");
		pb_bartype = "start";
	}else{
		pb_bar = $("#pb_bar_end");
		pb_bartype = "end";
	}

	pb_down = true;
	pb_start_x = event.clientX;
	pb_old_left = parseInt(pb_bar.css("left"));

	return false;
}

function pbUp(event){
	pb_down = false;
	reloadProducts();
	return false;
}

function pbMove(event){
	if (pb_down){
		newx = event.clientX;
		newleft = pb_old_left + (newx - pb_start_x);

		if (pb_bartype == "end"){
			if (newleft < 5){
				newleft = 5;
			}
		}else{
			if (newleft < 0){
				newleft = 0;
			}
		}

		if (newleft > pb_max_left){
			newleft = pb_max_left;
		}

		if (pb_bartype == "start" && newleft > (pb_end_left - pb_bar_width)){
			newleft = pb_end_left - pb_bar_width;
		}else if(pb_bartype == "end" && newleft < (pb_start_left + pb_bar_width)){
			newleft = pb_start_left + pb_bar_width;
		}

		pb_bar.css("left", newleft + "px");

		if (pb_bartype == "start"){
			pb_start_left = newleft;
		}

		if (pb_bartype == "end"){
			pb_end_left = newleft;
		}

		pbCalcPrice();
	}

	return false;
}

function pbCalcPrice(){
	bar_left = parseInt(pb_bar.css("left"));
	perc = bar_left / (pb_fullbar_width - 6);
	if (perc == 0){
		min_price = 0;
	}

	newprice = parseInt(pb_price_max * perc);

	if (pb_bartype == "start"){
		$("#texpricestart").val(number_format(newprice, 0, ",", ".") + " kr.");
		$("#texpricemin").val(newprice);
	}else{
		$("#texpriceend").val(number_format(newprice, 0, ",", ".") + " kr.");
		$("#texpricemax").val(newprice);
	}
}

function setPage(newpage){
	if (in_pages_max > 0){
		if (newpage + 1 > in_pages_max){
			newpage = 0;
		}else if(newpage < 0){
			newpage = in_pages_max;
		}

		$("#currentpage").val(newpage);
		reloadProducts();
		syncBottomPages();
		$("html, body").animate({scrollTop: $("#hardware_productlist").offset().top}, "slow");
	}
}

function hardware_setPages(in_pages_max, curpage){
	html = "";
	var lastPage = 0;
	var showMin = curpage - 2;
	if (showMin < 0) {
		showMin = 0;
	} else if (showMin > in_pages_max - 5) {
		showMin = in_pages_max - 5;
	}
	var showMax = showMin + 4;
	for(i = 0; i < in_pages_max; i++){
		if (i != 0
			&& (i < showMin || i > showMax)
			&& i != in_pages_max - 1
		) {
			continue;
		}

		if (i > 0 && i != lastPage + 1) {
			html += '<div style="float: left; padding-right: 5px; font-weight: bold;">…</div>';
		}

		astyle = ""
		if (i == curpage){
			astyle = ' style="color: #0280b6;"';
		}

		html += '<div style="float: left; padding-right: 5px; font-weight: bold;"><a ' + astyle + 'href="#" onclick="setPage(' + i + '); return false;">' + (i + 1) + '</a></div>';

		lastPage = i;
	}

	$("#divpages").html(html);
	syncBottomPages();
}

function hardware_setResults(results){
	$("#divresultscount").html(results);
}

function setNextPage(){
	curpage = parseInt($("#currentpage").val()) + 1;
	setPage(curpage);
	syncBottomPages();
}

function syncBottomPages(){
	$("#divpagesbottom").html($("#divpagestop").html());
}

function reloadProducts(){
	post_demands = "pricemin=" + $("#texpricemin").val() + "&pricemax=" + $("#texpricemax").val();

	count_details = new Array();
	$(document).find(".link.demands").each(function(){
		ele_checkbox = $(this).find(".details_checkbox")[0];

		if (ele_checkbox.checked){
			demands_type = $(this).find(".demands_type");

			if (demands_type && demands_type[0]){
				demands_type = demands_type[0].value;

				if (demands_type == "interval" || demands_type == "equal"){
					from_text = $(this).find(".interval_from")[0].value;
					to_text = $(this).find(".interval_to")[0].value;
					type_text = $(this).find(".interval_type")[0].value;

					if (!count_details[type_text]){
						count_details[type_text] = 1;
					}else{
						count_details[type_text]++;
					}

					post_demands += "&d[" + type_text + "_type_" + count_details[type_text] + "]=" + demands_type;
					post_demands += "&d[" + type_text + "_from_" + count_details[type_text] + "]=" + escape(from_text);
					post_demands += "&d[" + type_text + "_to_" + count_details[type_text] + "]=" + escape(to_text);
				}else if(demands_type == "manufacturer"){
					manu_id = $(this).find(".hidden_manufacturer")[0].value;
					post_demands += "&manus[]=" + manu_id;
				}else if(demands_type == "producttype"){
					ptype_id = $(this).find(".hidden_producttype")[0].value;
					post_demands += "&types[]=" + ptype_id;
				}else if(demands_type == "store"){
					store_id = ele_checkbox.id.substring(9);
					post_demands += "&stores[]=" + store_id;
				}else{
					alert("Unknown type: " + demands_type);
				}
			}
		}
	});

	if ($("#chedemo").attr("checked")){
		demoval_out = "1";
	}else if($("#demoval").val() == "on"){
		demoval_out = "1";
	}else{
		demoval_out = "0";
	}

	url = "/index_clean.php?show=hardware_newdesign&type_id=" + pb_type_id + "&prods=" + pb_prods + "&page=" + $("#currentpage").val() + "&showresults=" + $("#selshowresults").val() + "&sortby=" + $("#selsortby").val() + "&demo=" + demoval_out + "&" + post_demands;

	$.ajax({
		type: "GET",
		url: url,
		cache: false,
		async: true,
		success: function(data, textStatus, jqXHR) {
			in_pages_max = parseInt(jqXHR.getResponseHeader("Pages"));
			$("#currentpage").val(jqXHR.getResponseHeader("CurrentPage"));
			hardware_setResults(jqXHR.getResponseHeader("Products"));
			hardware_setPages(in_pages_max, $("#currentpage").val());

			$("#hardware_productlist").html(data);
			reloadJS();

			curpage = parseInt(jqXHR.getResponseHeader("CurrentPage"));
			if (curpage + 1 > in_pages_max){
				setPage(0);
			}
		},
		complete: function(jqXHR, textStatus) {
			return false;
		},
		error: function(jqXHR, textStatus, errorThrown) {
			if (textStatus = 'abort') {
				return false;
			}
			alert("Der opstod en fejl:\n\n" + textStatus);
		}
	});
}

function reloadJS(){
	if (typeof(employee_reload_js) != "undefined"){
		employee_reload_js.call();
	}

	// Intialize the hover for the HTML-box for frontpage-products.
	$(".frontpage_newproduct > .picture > a > img").hover(function(){
		tha_div_id = this.id.substring(20);
		tha_div = document.getElementById("divproductoverdescr_" + tha_div_id);

		if (tha_div){
			ShowHideVisSpec(tha_div, "show");
		}
	}, function(){
		tha_div_id = this.id.substring(20);
		tha_div = document.getElementById("divproductoverdescr_" + tha_div_id);

		if (tha_div){
			ShowHideVisSpec(tha_div, "hide");
		}
	});

	$(".popupbox").mousemove(function(event){
		PopupBoxUpdate(event);
	});
	$(".popupbox").mouseover(function(event){
		PopupBoxActivate(event);
	});
	$(".popupbox").mouseout(function(){
		PopupBoxHide();
	});
	$(".popupboxquick").mouseover(function(event){
		PopupBoxActivateQuick(event);
		PopupBoxUpdate(event);
	});
	$(".popupboxquick").mouseout(function(){
		PopupBoxHideQuick();
	});
	$("#popupbox").fadeTo(0, 0);
	$(".hoverinfo").slideUp(0);
}

$(document).ready(function(){
	$.contextMenu.defaults({
		menuStyle: {
			width: "185px"
		}
	});
	$.ajaxSetup({error: function(XMLHttpRequest,textStatus, errorThrown){
		alert(textStatus);
		alert(errorThrown);
		alert(XMLHttpRequest.responseText);
	}});
	reloadJS();
});

function stripAlphaChars(pstrSource) {
	var m_strOut = new String(pstrSource);
	m_strOut = m_strOut.replace(/[^0-9]/g, '');
	return m_strOut;
}

function check(name) {
	$('input[name="' + name + '"]').attr('checked', true);
	return false;
}
function uncheck(name) {
	$('input[name="' + name + '"]').attr('checked', false);
	return false;
}


