function ShowImage(pic, width, height, varenr, text) {
	width = new Number(width);
	height = new Number(height);
	width = width + 10;
	height = height + 105;
	html =  '<html><head><title>Varenummer: ' + varenr + '</title></head>' +
	'<body topmargin=0 leftmargin=0 onClick=\'self.close()\' onLoad="focus();resizeTo('+width+','+height+')">' +
	'<img src="webshop/images/upload/' + pic + '"><br><font face="Verdana,Geneva,Arial,Helvetica,sans-serif" size="1"><div align="center">' + text + '</div></font></body></html>';
	
	if (height > 800) {
		width = width + 50;
		var settings = 'width=' + width + ',height=' + height + ',scrollbars=yes,status=no,location=no,resizable=yes,menubar=no';
	} 
	else {
		var settings = 'width=' + width + ',height=' + height + ',scrollbars=no,status=no,location=no,resizable=yes,menubar=no';
	}
	   
	var imgWin = window.open('', 'imgWindow', settings);

	imgWin.document.open();
	imgWin.document.write(html);
	imgWin.document.close();
}

function showBasket(){
    xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	    alert ("The browser doesn't support HTTP Requests");
		return;
    }
	
    param = "loadbasket=1";	
				  						
    var url="webshop/_include/addToBasket.asp" + "?" + param;
    xmlHttp.onreadystatechange=function() { fillBasket() };
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
}	

function fillBasket(){
    if (xmlHttp.readyState == 1){
        document.getElementById("basket").innerHTML = '<div style="padding:10px; margin:0 auto; text-align:center;"><img src="images/loading.gif"></div>';
    }   
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){         
        document.getElementById("basket").innerHTML = xmlHttp.responseText;	    
    } 		
}

function fillData(vareID, sprogID, elementID, variantGroupID, listToFill, firstSelectName, isLastSelect, custGr, b2b, restrCat, restrProd) {
    var param;
    var selectedItem = document.getElementById('VariantGroup' + elementID).value;
    var formName = document.bestil;
    var variantValueStr = "";
    var tmpCounter = 0;
    var variantGroupBox;
    var variantGroupBoxID;
    var boxLength;
    var i;

    for (i = 0; i < formName.length; i++) {
        if (formName.elements[i].type == "select-one") {

            variantGroupBox = formName.elements[i].name

            if (variantGroupBox.indexOf("VariantGroup") != -1) {

                variantGroupBoxID = variantGroupBox.replace("VariantGroup", "")

                if (variantGroupBoxID >= listToFill) {
                    clearCombo(variantGroupBoxID, firstSelectName)
                }

                if (formName.elements[i].value != "") {
                    if (tmpCounter > 0) {
                        variantValueStr = variantValueStr + ","
                    }
                    variantValueStr = variantValueStr + formName.elements[i].value
                    tmpCounter = tmpCounter + 1;
                }
            }
        }
    }

    param = "vareID=" + vareID + "&sprogID=" + sprogID + "&variantGroupID=" + variantGroupID + "&listToFill=" + listToFill + "&strVariantID=" + variantValueStr + "&firstOption=" + firstSelectName + "&isLastSelect=" + isLastSelect + "&custGr=" + custGr + "&B2B=" + b2b + "&restrCat=" + restrCat + "&restrProd=" + restrProd;
    //alert(param)

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("The browser doesn't support HTTP Requests");
        return;
    }

    var url = "/webshop/advancedVariant.asp" + "?" + param;
    xmlHttp.onreadystatechange = function () { stateChanged(listToFill) };
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function stateChanged(listToFill) {
    var returnedArray, returnedItemArray;

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {

        returnedArray = xmlHttp.responseText.split("#product#");

        if (listToFill != 0) {
            for (var i = 0; i < (returnedArray.length); i++) {
                if (returnedArray[i] != "") {
                    returnedItemArray = returnedArray[i].split("#item#")
                    document.getElementById("VariantGroup" + listToFill).options[i] = new Option(convertText(convertText(returnedItemArray[1])), returnedItemArray[0]);
                }
            }
        }
    }
}

function clearCombo(listToFill, firstSelectName) {
    var comboToClear = document.getElementById('VariantGroup' + listToFill);
    var boxLength = comboToClear.options.length;

    if (boxLength != 0) {
        for (var i = boxLength - 1; i >= 0; i--) {
            comboToClear.options[i] = null;
        }
    }

    document.getElementById("VariantGroup" + listToFill).options[0] = new Option(firstSelectName, "");

}

function convertText(str) {
    var txt;
    str = str.replace("&#229;", "å")
    str = str.replace("&#230;", "æ")
    str = str.replace("&#230;", "æ")
    str = str.replace("&#248;", "ø")
    return str;
}

function changeProduct(vareID, sprogID, lastSelectID, pageID, pageName, sNumber, sGroup, sStr, sSupplier) {
    var variantProductId = document.getElementById("VariantGroup" + lastSelectID).value
    if (variantProductId != "") {
        if (pageName == "soegeside") {
            document.location.href = "/webshop/variant_redirect.asp?ID=" + pageID + "&vareid=" + variantProductId + "&name=" + pageName + "&sNumber=" + sNumber + "&sGroup=" + sGroup + "&sStr" + sStr + "&sSupplier=" + sSupplier;
        }
        else {
            document.location.href = "/webshop/variant_redirect.asp?ID=" + pageID + "&vareid=" + variantProductId + "&name=" + pageName;
        }
    }
}

function MouseOver(RwNm) {
    RwNm.style.background = RowMouseOverColor;    
}

function MouseOut(RwNm) {
    RwNm.style.background = RowMouseOutColor;    
}
		
function compareproductItems(ID){			
	var tmpStr = "";
	var row = 0;
	if (document.products.compareproduct.length > 1){
		for (i=0;i<document.products.compareproduct.length;i++){
		if (document.products.compareproduct[i].checked)
			if (row > 0){
				tmpStr = tmpStr + "," + document.products.compareproduct[i].value;
			}
			else {
				tmpStr = document.products.compareproduct[i].value;
				row = 1;
			}
		}				
	}
	else {
		if (document.products.compareproduct.checked){
			tmpStr = document.products.compareproduct.value;
		}				
	}				
	document.getElementById("compareproductitems").value = tmpStr;
	if (tmpStr != ""){
		win_compare = window.open("webshop/sammenligninger.asp?ID="+ID,"Produktsammenligning","width=800,height=600,location=0,status=0,menubar=0,toolbar=0,resizable=yes,scrollbars=1");			 
	}
	else {
		alert("Der er ikke valgt nogen varer der skal sammenlignes");
	}
}
		
function putInBasket(ID,ProdID,GroupID,Page,MinBuy,MaxBuy){
	if ((MinBuy > 1) && (MaxBuy == 0)){
		if (confirm(txtMinimumBuy + ' ' + MinBuy)){
			addToBasket(ID,ProdID,GroupID,Page)
		}
	}
	else if ((MinBuy > 1) && (MaxBuy > 0)){
		if (confirm(txtMinimumBuy + ' ' + MinBuy + '\n' + txtMaximumBuy + ' ' + MaxBuy)){
			addToBasket(ID,ProdID,GroupID,Page)
		}
	}
	else if ((MinBuy = 1) && (MaxBuy > 0)){
		if (confirm(txtMaximumBuy + ' ' + MaxBuy)){
			addToBasket(ID,ProdID,GroupID,Page)
		}				
	}
	else {			    			    	    
		addToBasket(ID,ProdID,GroupID,Page)
	}
}
		
function addToBasket(ID,ProdID,GroupID,Page){		    
	var numberOfProducts = 1;
	if (document.getElementById(ProdID+"_numberofproducts")){
		numberOfProducts = document.getElementById(ProdID+"_numberofproducts").value;
	}
		    
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("The browser doesn't support HTTP Requests");
		return;
	}
    		
    param = "ID="+ID+"&show=varekurv&func=add&vareid="+ProdID+"&varegrp="+GroupID+"&page="+Page+"&name=vareliste&numberof="+numberOfProducts;
    					  						
 	var url="webshop/_include/addToBasket.asp" + "?" + param;
 	xmlHttp.onreadystatechange=function() { updateBasket() };
  	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);	
}	
		
function updateBasket(txtSuccess,txtLimitReached){
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        if ((xmlHttp.responseText != "error") && (xmlHttp.responseText != "0")) {
            if (document.getElementById("basket")) {	        			        			   		    
                document.getElementById("basket").innerHTML = xmlHttp.responseText;
            }
            alert(txtAddToBasketSuccess);
        }
        else if (xmlHttp.responseText != "0") {
            alert(txtMaximumLimitReached);
        }	
    }
}

function showAvailableVariants(id) {
    if (document.getElementById(id)) {
        document.getElementById(id).style.display = 'block';
    }
}

function hideAvailableVariants(id) {
    if (document.getElementById(id)) {
        document.getElementById(id).style.display = 'none';
    }
}

 function TjekFelt(){
    if (SearchBoxStyle == 1) {
        if ((document.forms.soegboks.VareNavn.value == "") || (document.forms.soegboks.VareNavn.value.length < 2)){
		    alert(txtNoSearchTextInput);
		    return false;
	    }
    }
	if (SearchBoxStyle == 2) {
	    if (document.forms.soegboks.VareNavn.value == txtDefaultSearchText) {
	        document.forms.soegboks.VareNavn.value = "";
	    }
	    if (((document.forms.soegboks.VareNavn.value == "") || (document.forms.soegboks.VareNavn.value.length < 2))  && (document.forms.soegboks.Producent.value == "") && (document.forms.soegboks.VareGruppe.value == "")){
		    alert(txtNoSearchInput);
		    return false;
	    }
	}
}

function updateVariants(prodID,packageGroupID,normalPrice,offerPrice) {
                    
    if (prodID != "") {
                                            
        var extraPrice = getProductPrice(prodID);

        updatePackageProductPrice(normalPrice, offerPrice);

        prodID = getProductID(prodID);                        

        var arr = new Array(); 
        arr = document.getElementById("divPackageProducts"+packageGroupID).getElementsByTagName("*");
        for (var i = 0; i < arr.length; i++) {
            var tagName = document.getElementById("divPackageProducts"+packageGroupID).getElementsByTagName("*").item(i).nodeName;
            var tagObj = document.getElementById("divPackageProducts"+packageGroupID).getElementsByTagName("*").item(i); 
                            
            if ((tagName == "SELECT") || (tagName == "LABEL") || (tagName == "SPAN")) {
                if (tagObj.name != "PackageProductID") {
                    tagObj.style.display = "none";
                    tagObj.disabled;                                    
                }                          
            }
        }                        

        document.getElementById("PackageProductLink"+prodID).style.display="";    

        if (document.getElementById("variant1"+prodID)) {
            document.getElementById("labelVariant1"+prodID).style.display="";
            document.getElementById("variant1"+prodID).style.display="";
            document.getElementById("variant1"+prodID).enabled;                                                                         
        }
        if (document.getElementById("variant2"+prodID)) {
            document.getElementById("labelVariant2"+prodID).style.display="";
            document.getElementById("variant2"+prodID).style.display="";
            document.getElementById("variant2"+prodID).enabled;                                                             
        }
    }
}

function updatePackageProductPrice(normalPrice, offerPrice) {
    var calculatedSum = 0;     

    if (offerPrice != "") {
        calculatedSum = parseInt(offerPrice) + parseInt(calculatePackageProductPrice());
    }
    else {
        calculatedSum = parseInt(normalPrice) + parseInt(calculatePackageProductPrice());
    }

    calculatedSum = calculatedSum + ((calculatedSum * vat) / 100);

    if (currencyFactor != 0) {
        calculatedSum = ((100 / currencyFactor) * calculatedSum)
    }

    calculatedSum = formatCurrency(calculatedSum) + '';
    calculatedSum = calculatedSum.replace(".", ",");
    calculatedSum = formatNumber(calculatedSum);

    if (document.getElementById("divPackageProductPrice")) {
        document.getElementById("divPackageProductPrice").style.display = "";
        document.getElementById("calculatedPackagePrice").innerHTML = calculatedSum + " " + currencyCode;
    }
}

function formatNumber(num) {
    var val = num.replace(/\./g, '');
    var dec_pos = val.indexOf(',', val);
    var number = (dec_pos != -1) ? val.substr(0, dec_pos) : val;
    var reversed = number.split("").reverse();
    var newNum = '';

    for (var i=0; i<reversed.length; i++) {
        if (i != 0 && (i % 3) == 0) {
            newNum = '.'+newNum;
        }
        newNum = reversed[i]+newNum;;
    }

    if (dec_pos != -1) {
        newNum += val.substr(dec_pos, (val.length-dec_pos));
    }

    return newNum;
}

function getProductID(tmpValue) {
    var prodID = tmpValue.split(";")[0];
    return prodID;
}

function formatCurrency(num) {     
    num = isNaN(num) || num === '' || num === null ? 0.00 : num;
    num = parseFloat(num).toFixed(2);                     
    return num;
} 

function calculatePackageProductPrice() {
    var arr = new Array(); 
    var tmpPrice = 0;
    arr = document.getElementsByName("PackageProductID");
    for (var i = 0; i < arr.length; i++) {
        tmpPrice = tmpPrice + parseInt(getProductPrice(arr[i].value))        
    }    
    return tmpPrice;                
}

function getProductPrice(tmpValue) {
    var extraPrice
    if (tmpValue != "") {
        extraPrice = tmpValue.split(";")[1];
    }
    else {
        extraPrice = 0;
    }    
    return extraPrice;
}

function Currency() {
	if (document.getElementById("ChangeCurrency")) {
	    ValutaID = document.getElementById("ChangeCurrency").value;
	    document.valutaform.valuta.value = ValutaID;
	}
	if (document.getElementById("ChangeTax")) {
	    Moms = document.getElementById("ChangeTax").value;
	    document.valutaform.moms.value = Moms;
	}
	document.valutaform.submit();
}


