$(document).ready(function(){
	$("#ShopLayOut_Topmenu_TD a").each(function(){
		$(this).wrap('<div class="topmenuLinks"></div>');
	});
	
	$(".topmenuLinks").hover(function(){
		$(this).css("border-top-color", "#e71c02");
	},function(){
		if($(this).attr("class").indexOf("topmenuActive") == -1){
			$(this).css("border-top-color", "transparent");
		}
	});
	if(location.href.indexOf("/shop/frontpage.html") != -1){
		setTopmenu(0);
		$(".prices td").each(function(){
	   		key= $(this).html().replace(/\s+/g," ");
			key = key.replace(/  /g, " ");
			key = key.replace(/&nbsp;&nbsp;/g, " ");
	    	$(this).html(key);
		})
		$(".prices td strike").remove();
	}
	html = ""
	$("#ShopMainLayOutTable .SubCats_Prodlink_TD").each(function(){
		html += '<div class="SubCats_Prodlink_TD">'+$(this).html()+'</div>';
	});
	$("#ShopMainLayOutTable .SubCats_Prodlink_TD:first").parent().parent().parent().parent().html('<div>'+html+'</div>');
	$("#ShopMainLayOutTable .SubCats_Prodlink_TD:first").parent().parent().css("width", "100%");
	$("#ShopMainLayOutTable .SubCats_Prodlink_TD:first").parent().parent().attr("width", "100%");
	if($(".SubMenu_Productmenu_Table").length > 0){
		$(".SubMenu_Productmenu_Table").parent().css("background", "none");
	}
	
	if($(".Activ_Productmenu").length > 0){
		$(".Activ_Productmenu").parent().css("background-image", "url('/images/design/da/productmenu/bulletActive.jpg')")
	}
	
	if(location.href.indexOf("news-ALL-") != -1){
		$(".Deactiv_Productmenu[href='/shop/news-ALL-1.html']").parent().css("background-image", "url('/images/design/da/productmenu/bulletActive.jpg')")
	}
	if(location.href.indexOf("specialoffer-ALL-") != -1){
		$(".Deactiv_Productmenu[href='/shop/specialoffer-ALL-1.html']").parent().css("background-image", "url('/images/design/da/productmenu/bulletActive.jpg')")
	}
	
	$(".CmsPageStyle").closest("table").height("auto");
	
	$(".Prices_Custom_TBL td").removeAttr("align");
	
	
	
	$(".ProductImage_ShowBasket").attr("align", "center");
	
	$(".BackgroundColor1_Orderstep1").attr("cellspacing", "0");
	$("#ShopMainLayOutTable .TableLines_Orderstep1 table:first").attr("cellspacing", "0");
	$("#ShopMainLayOutTable .TableLines_Orderstep2 table:first").attr("cellspacing", "0");
	$("#ShopMainLayOutTable .TableLines_OrderStep3 td:first table:first").attr("cellspacing", "0");
	$("#ShopMainLayOutTable .BackgroundColor1_CreditCardPayment table:first").attr("cellspacing", "0");
	$("#ShopMainLayOutTable .BackgroundColor1_CreditCardPayment table:first").attr("cellpadding", "0");
	
	$("#ShopMainLayOutTable .BackgroundColor1_CreditCardPayment table:first tr").each(function(index){
		if(index == 2 || index == 3 || index == 5 || index == 6){
			$(this).find("td").css("padding", "0px 0px 0px 20px");
		}
		if(index == 4){
			$(this).find("td").css("padding", "0px 0px 8px 20px");
		}
		
		if(index < 7){
			$(this).find("td").css("background-color", "#eeeeee");
		}
	});
	
	submitBtn = $(".SubmitButton_Pay").parent().html();
	$("#ShopMainLayOutTable .SubmitButton_Pay").parent().parent().next().next().children("td").attr("colspan", "1");
	$("#ShopMainLayOutTable .SubmitButton_Pay").parent().parent().next().next().children("td").css("vertical-align", "middle");
	$("#ShopMainLayOutTable .SubmitButton_Pay").parent().parent().next().next().children("td").css("padding", "20px 0px 0px 0px;");
	$("#ShopMainLayOutTable .SubmitButton_Pay").parent().parent().next().next().append('<td style="padding-top:20px;padding-left:20px;" align="left" colspan="2">'+submitBtn+'</td>');
	$("#ShopMainLayOutTable .SubmitButton_Pay").parent().parent().next().hide();
	$("#ShopMainLayOutTable .SubmitButton_Pay:first").parent().parent().hide();
	
	$("#Field11_0 .TextInputField_Orderstep1").after("<div style='font-size:10px;color:#817e83;margin-bottom:8px;'>Må varerne stilles i garage, hos nabo el. lign. hvis ingen hjemme?</div>")
	
	selectBox = $("#Field15_0").html();
	
	$("#Field15_0").hide();
	$(".Heading_Orderstep1_TBL").after('<table cellspacing="0" id="forhandlerBox" cellpadding="0" border="0" width="880" style="margin:0px 10px 0px 10px;border-top:1px solid #000000;"><tr><td style="padding:10px 0px 10px 0px;" colspan="3"><b>Vælg hvilken forhandler du vil have din levering fra</b></td></tr><tr><td><table cellspacing="0" cellpadding="0" border="0" width="432px"><tr>'+selectBox+'</tr></table></td></tr></table>')

	//$("#ShopMainLayOutTable  #forhandlerBox .OptionSelect_Orderstep1 option:first").attr("selected", "selected")
	
	
	if($("#Field15_0").length > 0){
		$("#Field15_0 .OptionSelect_Orderstep1 option").each(function (index) {
			if($(this).attr("selected")){
				myVal = index//$("#Field15_0 .OptionSelect_Orderstep1").val();
			}
		   });
		$("#ShopMainLayOutTable  #forhandlerBox .OptionSelect_Orderstep1 option:eq("+myVal+")").attr("selected", "selected")
	}
	$("#ShopMainLayOutTable  #forhandlerBox .OptionSelect_Orderstep1 option:selected").each(function () {
                val = $(this).val();
     });		
		
	$("#ShopMainLayOutTable .OrderStep1_Next_TD .SubmitButton_Orderstep1").click(function(){
		//alert(val)
		if(val != "Vælg forhandler"){
			return true;
		} else {
			alert("Vælg en forhandler");
			return false;
		}
	});
	
	$("#ShopMainLayOutTable .OrderStep1_Next_TD .SubmitButton_Orderstep1:first").click(function(event){
			if(val != "Vælg forhandler"){
				$("#Field15_0 .OptionSelect_Orderstep1 option").each(function () {
		                if($(this).val() == val){
							var CookieVal = $(this).val().replace("ø", "oe")
							//alert(CookieVal)
							createCookie("Store", CookieVal, "1");
							//alert("Store "+ $(this).val() +" 1")
							$(this).attr("selected", "selected");
							return true;
						} 
		        });
				return true;
			} else {
				alert("Vælg en forhandler");
				return false;
			}
		});
	

	$("#ShopMainLayOutTable #forhandlerBox .OptionSelect_Orderstep1").change(function(){
		$("#ShopMainLayOutTable  #forhandlerBox .OptionSelect_Orderstep1 option:selected").each(function () {
                val = $(this).val();
        });
			
		$("#Field15_0 .OptionSelect_Orderstep1 option").each(function () {
			//alert($(this).val()+" == "+val)
                if($(this).val() == val){
					var CookieVal = $(this).val().replace("ø", "oe")
					createCookie("Store", CookieVal, "1");
					//alert("Store "+ $(this).val() +" 1")
					$(this).attr("selected", "selected")
				}
        });
		
		
		
		
	});
	
	$(".Frontpage_Custom_TBL .infos").each(function(){
		var $this = $(this);
		var $priceTable = $this.find(".prices");
		var url = $this.find("a").attr("href") + "?rawxml=1";
		$.ajax({
			url:url,
			success:function(xml) {
				$xml = $(xml);
				$xml.find("UnitPrice").each(function () {
					var $unitPriceNode = $(this);
					var amount = $unitPriceNode.find("Amount").text();
					amount = parseInt(amount,10);
					if (amount > 1 ) {
						var price = $unitPriceNode.find("Price").text();
						var currency = $unitPriceNode.find("Currency").text();
						$priceTable.find("tbody").append("<tr class='bulk'><td style='color:#817E83;'>v/"+amount+" flasker</td><td>"+price+" DKK</td></tr>")
					}
				});
				
				$(".prices").each(function(){
					$(this).find("tr:first").find("td:last").css("font-size", "12px");
				});
				
				
				$(".bulk").each(function(){
					$(this).find("td:last").css("font-size", "14px");
				}); 
				
				$(".prices td").css("vertical-align", "middle");
			}
		});
	});
	
	$(".relatedPRods").each(function(){
		var $this = $(this);
		var $priceTable = $this.find(".relatedPRices");
		var url = $this.find(".relatedProdname a").attr("href") + "?rawxml=1";
		$.ajax({
			url:url,
			success:function(xml) {
				$xml = $(xml);
				$xml.find("UnitPrice").each(function () {
					var $unitPriceNode = $(this);
					var amount = $unitPriceNode.find("Amount").text();
					amount = parseInt(amount,10);
					if (amount > 1 ) {
						var price = $unitPriceNode.find("Price").text();
						var currency = $unitPriceNode.find("Currency").text();
						$priceTable.append("<div style='color:#817E83;white-space:nowrap;'>v/"+amount+" flasker&nbsp;"+price+"DKK</div>")
					}
				});
			}
		});
	});
	
	$(".Heading_TipFriend").attr("colspan", "2");
	
	if($(".Prices_Custom_TBL").length > 0){
		$(".infoBox").each(function(){
			if($(this).find(".Prices_Custom_TBL > tbody > tr").length > 1){
				$(this).find(".Prices_Custom_TBL > tbody > tr:last-child").addClass("sale");
			}
			$(this).find(".Prices_Custom_TBL").find("tr:first").find("table td:first").text("Pr. flaske");
			//$(this).find("tr:first").find("table td:last").css("text-align", "right")
			//$(this).find(".sale").find("table td:last").css("text-align", "right")
			if($("#ProductList_TD").length > 0){
				$(this).find(".Prices_Custom_TBL").find("tr:first").find("table td:first").css("padding-left", "0px");
				$(this).find(".Prices_Custom_TBL").find("tr:first").find("td:first").css("padding-left", "0px")
				if($(this).find(".Prices_Custom_TBL > tbody > tr").length > 1){	
					$(this).find(".priceFor6Container").html($(this).find(".sale").find("table td:last").text());
					$(this).find(".priceFor6Txt").html($(this).find(".sale").find("table td:first").text());
					$(this).find(".sale").remove();
				} else {
					$(this).find(".priceFor6").parent().remove();
				}
			}
			if($(this).find("strike").length > 0){
				if($(this).find("strike").html().length < 2){
					$(this).find("strike").parent().remove();
				} else {
					$(this).find("strike").append(" DKK")
				}
			}
		});
	}

	if($(".Specialoffer_Overlay_IMG").length > 0){
		$(".Specialoffer_Overlay_IMG").attr("class", "SpecialofferOverlayIMG");
		$(".SpecialofferOverlayIMG").wrap("<div class='SpecialofferOverlayCon'></div>")
		if($.browser.msie){
			$(".SpecialofferOverlayIMG").css({
				"position" : "relative",
				"top" : "-5px",
				"left" : "2px"
			});
			$("#ProductList_TD .product .SpecialofferOverlayIMG").css({
				"position" : "relative",
				"top" : "-5px",
				"left" : "-10px"
			});
		}
	}
	
	if($(".ProductInfo_MagixZoomPlus_MainImage_TD").length > 0){
		html = $(".ProductInfo_MagixZoomPlus_MainImage_TD").html();
		html2 = "";
		if($(".newsImage").length > 0){
			html2 = $(".newsImage").html();
			$(".newsImage").remove();
		}
		$(".ProductInfo_MagixZoomPlus_MainImage_TD").html('<div style="position:relative;"><div class="newsImage2">'+html2+'</div>'+html+'</div>');
	}
	
	counter = 0;
	if($("#ShopMainLayOutTable .newsImage").length > 0){
		$("#ShopMainLayOutTable .image").each(function(index){
			if($(this).find(".newsImage").html().length > 0){
				if($(this).find(".SpecialofferOverlayCon").length > 0){
					$(this).find(".SpecialofferOverlayCon").after("<div class='newsImage2'><img border='0' src='/images/design/da/new.png'></div>");
				} else {
					$(this).find(".newsImage").prev().children("img").wrap('<div style="position:relative;" class="prodImgWrapper"></div>');
					$(this).find(".prodImgWrapper img").wrap('<div></div>');
					$(this).find(".prodImgWrapper img").parent().after("<div class='newsImage2'><img border='0' src='/images/design/da/new.png'></div>");
				}
				$(this).find(".newsImage").remove();
				counter++;
			}
		});
		if($.browser.msie){
			$(".newsImage2 img").css({
				"position" : "relative",
				"top" : "-5px",
				"left" : "2px"
			});
			$("#ProductList_TD .product .newsImage2 img").css({
				"position" : "relative",
				"top" : "-5px",
				"left" : "-10px"
			});
		}
		
	}	
	if($.browser.msie){
		$(".breadcrumbArrow	").css({
			"margin" : "0 5px",
		    "position" : "relative",
		    "top" : "0px",
		    "vertical-align" : "middle"
		});
	}
	
	

});

function setTopmenu(index){
	$("#ShopMainLayOutTable .topmenuLinks").each(function(){
		$(this).removeClass("topmenuActive");
	});
	$("#ShopMainLayOutTable .topmenuLinks:eq("+index+")").addClass("topmenuActive")
}

function createCookie(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 readCookie(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);
}

function setShippingAndPayment(){	
	//alert(readCookie("Store"))
	switch (readCookie("Store")){
		case "Middelfart":
			var paymentID = new Array("78","59","60")
			var shippingID = new Array("70","53","54")		
			break
			
		case "Vejle":
			var paymentID = new Array("86")
			var shippingID = new Array("55","56","57")
			break
		
		case "Horsens":
			var paymentID = new Array("68","69","70")
			var shippingID = new Array("61","62","63")
			break
		
		case "Aalborg":
			var paymentID = new Array("71","72","73")
			var shippingID = new Array("64","65","66")
			break
			
		case "Hjoerring":
			var paymentID = new Array("74","75","76")
			var shippingID = new Array("67","68","69")
			break
		
		case "Greve":
			var paymentID = new Array("80","81","82")
			var shippingID = new Array("53","54","69")
			break
		
		case "Haderslev":
			var paymentID = new Array("83","84","85")
			var shippingID = new Array("53","54","69")
			break
	}
	
	$("input[id*='PaymethodID']").each(function(){
		currentID = $(this).attr("id").split("_")[1]+'';
		//alert(readCookie("Store"))
		if($.inArray(currentID, paymentID) == -1){
			$(this).parent().prev().parent().hide();
		}
	});
	/*
	$("tr[id*='ShippingMethod']").each(function(){
		currentID = $(this).attr("id").split("_")[1];
		if($.inArray(currentID, shippingID) == -1){
			$(this).hide();
		}
	});
	*/
	/*$(".OrderStep2_PayMethods_TD td").show();
	$(".OrderStep2_ShippingMethods_TD td").show();*/
	$("#ShopMainLayOutTable .OrderStep2_PayMethods_TD input").each(function(){	

		$(this).attr("onclick", "");
		$(this).click(function(event) {
  			//event.preventDefault();
			//id = $(this).attr("id").split("_")[1];
			//GetMethods2('ShippingMethods', id, document.getElementById('ShippingMethodsContainer'), '', '0');
			//setTimeout("setShippingAndPayment()", 210);
		});	
	});
	
	$("#ShopMainLayOutTable .OrderStep2_ShippingMethods_TD input").each(function(){	

		$(this).attr("onclick", "");
		$(this).click(function(event) {
  			//event.preventDefault();
			//id = $(this).attr("id").split("_")[1];
			//GetMethods2('ShippingMethods', id, document.getElementById('ShippingMethodsContainer'), '', '0');
			//setTimeout("setShippingAndPayment()", 210);
		});	
	});
	
	$(".OrderStep2_ShippingMethods_TD input").click(function(){
		setTimeout("setShippingAndPayment()", 210);
	});
}









function GetMethods2(MethodType,ReqMethodID,ContentContainer,Debug,ServiceCOD)
{

//alert("MethodType: "+MethodType+"\nReqMethodID: "+ReqMethodID+"\nContentContainer: "+ContentContainer+"\nDebug: "+Debug+"\nServiceCOD: "+ServiceCOD)

    var objReq ;
    var requestpage;
    var ParamStr

    if(MethodType=='ShippingMethods') ParamStr = "&PayMethodID="+ReqMethodID;
    if(MethodType=='PayMethods') ParamStr = "&ShippingMethodID="+ReqMethodID;

    ParamStr = ParamStr + "&Debug=" + Debug
    
    requestpage = '/shop/order2.asp?GetMethods=' + MethodType + ParamStr

    // Create XMLhttp object
    objReq = CreateXmlHttpObj();

    objReq.onreadystatechange = function() {
        if (objReq.readyState == 4) {
			if(ContentContainer.innerHTML != objReq.responseText){
            ContentContainer.innerHTML = objReq.responseText
			}
			

            // Indicate that a paymethod using COD is selected - this will disable som shipping method services
            if (ServiceCOD != undefined) {
                CodPayMethodSelected(ServiceCOD)
            }

        }
    };
	objReq.open("GET", requestpage,true);
	objReq.send(null);

	

	//GLS Parcel shops
	if (MethodType == 'PayMethods') {
	    try {

	        var GlsParselShops_TR = document.getElementById('GLS_ParselShops_' + ReqMethodID);
	        
	        // Start by hiding all GLS parcel shops
	        try {
	            var ParselShopRows = document.getElementsByClassName('GLS_ParselShops_TR');
	            for (var i = ParselShopRows.length - 1; i >= 0; --i) {
	                ParselShopRows[i].style.display = 'none';
	            }
	        }
	        catch (e) {
	        }
             
            // Show parcels shops for the active selected shipping method
	        GlsParselShops_TR.style.display = '';
	    }
	    catch (e) {}
	}

}


