/* This file is used in gavefinder page and productNotFound

History of changes:
    20110412 - ENE added buybutton to products.
*/

function updateFormElements(data, shoptexts, initrun, direction, pagenumber){
	// Update the checkboxes in the form and return true or false to the result array if it is checked or not
	var result=new Array();
	result[0] = updateFormCheckbox(data[0].csbe_product_gift_finder_agerange_1, 'csbe_product_gift_finder_agerange_1');
	result[1] = updateFormCheckbox(data[0].csbe_product_gift_finder_agerange_2, 'csbe_product_gift_finder_agerange_2');
	result[2] = updateFormCheckbox(data[0].csbe_product_gift_finder_agerange_3, 'csbe_product_gift_finder_agerange_3');
	result[3] = updateFormCheckbox(data[0].csbe_product_gift_finder_agerange_4, 'csbe_product_gift_finder_agerange_4');
	result[4] = updateFormCheckbox(data[0].csbe_product_gift_finder_agerange_5, 'csbe_product_gift_finder_agerange_5');
	result[5] = updateFormCheckbox(data[0].csbe_product_gift_finder_agerange_6, 'csbe_product_gift_finder_agerange_6');
	result[6] = updateFormCheckbox(data[0].csbe_product_gift_finder_gender_m, 'csbe_product_gift_finder_gender_m');
	result[7] = updateFormCheckbox(data[0].csbe_product_gift_finder_gender_f, 'csbe_product_gift_finder_gender_f');
	result[8] = updateFormCheckbox(data[0].csbe_product_gift_finder_maternity, 'csbe_product_gift_finder_maternity');
	result[9] = updateFormCheckbox(data[0].csbe_product_gift_finder_christening, 'csbe_product_gift_finder_christening');
	result[10] = updateFormCheckbox(data[0].csbe_product_gift_finder_toy, 'csbe_product_gift_finder_toy');
	result[11] = updateFormCheckbox(data[0].csbe_product_gift_finder_interior, 'csbe_product_gift_finder_interior');
	result[12] = updateFormCheckbox(data[0].csbe_product_gift_finder_equipment, 'csbe_product_gift_finder_equipment');
	result[13] = updateFormCheckbox(data[0].csbe_product_gift_finder_accessory, 'csbe_product_gift_finder_accessory');
	/* Set price range checkboxes when not using a selectbox for price range */
	result[14] = updateFormCheckbox(data[0].pricerange_0_100, 'pricerange_0_100');
	result[15] = updateFormCheckbox(data[0].pricerange_100_250, 'pricerange_100_250');
	result[16] = updateFormCheckbox(data[0].pricerange_250_500, 'pricerange_250_500');
	result[17] = updateFormCheckbox(data[0].pricerange_500_1000000, 'pricerange_500_1000000');
	
	// Check if the result array has any true values, if it does this is not an intital run and we should display the products.
	for (var i = 0; i < result.length; i++){
        if (result[i] == true) {
            initrun = false;
        }
    }
	
	// Write number of products found in the result window
	$('numproducts').innerHTML = data[0].trueCountID;
	
	// Clear the product list
	$('productlist').innerHTML = '';
	
	// Add an anchor link to the debug output if were in debug mode
	if (data[0].debug) {
		$('productlist').innerHTML += "<a href=\"#debugSection\">Go to Debug Output</a>";
	}
	
    if (initrun == false) {
    
        // Pagination
        /*if (direction == 'next') {
          $('pageNumber').value = parseInt(pagenumber) + 1;
          startindex = parseInt($('startindex').value) + (parseInt(pagenumber) * parseInt(itemsperpage));
          endindex = parseInt(itemsperpage) + (parseInt(pagenumber) * parseInt(itemsperpage));
        }
        else if (direction == 'prev') {
          $('pageNumber').value = parseInt(pagenumber) - 1;
          startindex = parseInt($('startindex').value) - (parseInt(pagenumber) * parseInt(itemsperpage));
        endindex = parseInt(itemsperpage) - (parseInt(pagenumber) * parseInt(itemsperpage));
        }

        
        if (endindex < data[0].trueCountID) {
          $('next_page').style.display = 'block';
        }
        else {
          $('next_page').style.display = 'none';
        }
        if (startindex > 0) {
          $('previous_page').style.display = 'block';
        }
        else {
          $('previous_page').style.display = 'none';
        }
        $('startindex').value = startindex;
        $('endindex').value = endindex;
        $('itemsperpage').value = itemsperpage;*/

        var productList = "";
        productList = "<h2 class=\"popularProducts\">" + shoptexts['ProductListTitle'] + "</h2>";
        productList += "<div class=\"productsContainer\">";
        productList += "<input type=\"hidden\" id=\"productFormID\" value='' />";
        
        for(var i=0; i < 200 && i < data[0].trueCountID; i++) {
            if (i > 0 && i % 3 == 0) {
                productList += "<div class=\"clear\"></div>";
            }
            if (i == 0  || i % 3 == 0) {
                var productbox_class = 'productbox_first';
            }
            else {
                var productbox_class = 'productbox';
            }
            if (data[0].id[i].annotation.price_offer != '0') {
                var overlay = "<span id=\"overlay_" + data[0].id[i].value + "\" style=\"width:201px;\">TILBUD</span>";
                var overlayStyling = "productImageWithOverlay";
                var overlayOnClick = "onMouseOver=\"hightlightoverlay('overlay_" + data[0].id[i].value + "', 1)\" onMouseOut=\"hightlightoverlay('overlay_" + data[0].id[i].value + "', 0)\"";
                var priceNow = formatNumber(data[0].id[i].annotation.price_offer);
                var priceBefore = formatNumber(data[0].id[i].annotation.price);
                var pricetagNow = "<div class=\"priceNow\">Nu " + shoptexts['CurrencySymbolLeft'] + " " + priceNow + " " + shoptexts['CurrencySymbolRight'] + "</div>";
                var pricetagBefore = "<div class=\"priceBefore\" style=\"margin-right: 0px;\" >F&oslash;r " + shoptexts['CurrencySymbolLeft'] + " " + priceBefore + " " + shoptexts['CurrencySymbolRight'] + "</div>"; 
            }
            else {
                var overlay = "";
                var overlayStyling = "";
                var overlayOnClick = "";
                var priceNow = "";
                var priceBefore = formatNumber(data[0].id[i].annotation.price);
                var pricetagNow = "<div class=\"priceNow\" align=\"right\">&nbsp;</div>"; 
                var pricetagBefore = "<div class=\"priceOrg\" >" + shoptexts['CurrencySymbolLeft'] + " " + priceBefore + " " + shoptexts['CurrencySymbolRight'] + "</div>";
            }

            var key = data[0].id[i].value;
            var ProductLink = data[0].id[i].annotation.url_product;
            var buybuttonSubmit = "javascript:document.buyprod" + key + ".submit()";            
            /*Form with product info and buybutton*/
            productList += "<form name=\"buyprod" + key + "\" action=\"javascript:addProductToBasket(this, " + key + ", " + key + ", 0);\" method=\"post\">";
            productList += "<input type=\"hidden\" name=\"amount\" id=\"amount_" + key + "\" value=\"1\"   />";
            productList += "<input type=\"hidden\" name=\"option\" value=\"&defaultOptionTitle0=Gratis%20gaveindpakning&defaultOption0=%C3%98nskes%20ikke\" id=\"option_" + key + "\" />";		
            productList += "<input type=\"hidden\" name=\"weight\" value=\"0\" id=\"weight\" />"; 
            
                productList += "<a name=\"catprod" + data[0].id[i].value + "\"></a>" +
                "<div class=\"c210 " + productbox_class + "\">" +
                    "<div class=\"productImage " + overlayStyling + "\"  " + overlayOnClick + ">" +
                        "<a href=\"" + ProductLink + "\" style=\"text-decoration: none;\">" + overlay +
                            "<img alt=\"" + data[0].id[i].annotation.title + "\" title=\"" + data[0].id[i].annotation.title + "\" border=\"0\" src=\"" + data[0].id[i].annotation.url_image + "RESIZED_category" + data[0].id[i].annotation.ext_image + "\" />" +
                        "</a>" +
                    "</div>" +
                    "<div class=\"productName\">" + data[0].id[i].annotation.title + "</div>" +
                    "<div class=\"productDetails\">" +
                  
                        "<div class=\"productStockInfo\">";
                            if (data[0].id[i].annotation.availability == "low_stock") {
                                productList += "<img border=\"0\" title=\"" + shoptexts['LimitedAmountImageAlt'] + "\" alt=\"" + shoptexts['LimitedAmountImageAlt'] + "\" src=\"" + shoptexts['ImagePath'] + shoptexts['LimitedAmountImage'] + "\"/>" + shoptexts['LimitedAmountImageAlt'];
                            }
                            else if (data[0].id[i].annotation.availability == "in_stock") {
                                productList += "<img border=\"0\" title=\"" + shoptexts['ProductInStockImageAlt'] + "\" alt=\"" + shoptexts['ProductInStockImageAlt'] + "\" src=\"" + shoptexts['ImagePath'] + shoptexts['ProductInStockImage'] + "\"/>" + shoptexts['ProductInStockImageAlt'];
                            }
                            else if (data[0].id[i].annotation.availability == "out_of_stock" || data[0].id[i].annotation.availability == "recurring") {
                                productList += "<img border=\"0\" title=\"" + shoptexts['NoAmountImageAlt'] + "\" alt=\"" + shoptexts['NoAmountImageAlt'] + "\" src=\"" + shoptexts['ImagePath'] + shoptexts['NoAmountImage'] + "\"/>" + shoptexts['NoAmountImageAlt'];
                            }
                        productList += "</div>";
                        productList += "<div class=\"productPriceInfo\">" + pricetagNow + pricetagBefore + "</div>";
                        
                        productList += "<div class=\"clear\"></div>";
                        productList += "<div class=\"productReadMore\" style=\"margin-top: 3px;\"><a href=\"" + ProductLink + "\">" + shoptexts['ReadMoreText'] + "&nbsp;&raquo;</a></div>";   
                        productList += "<div class=\"productBuyButton\" style=\"margin-top: 3px;\">";
                        
                            if (data[0].id[i].has_product_variants == true){
                                productList += "<a href=\"" + ProductLink + "/none/vaelg_variant\" id=\"BuyButton_"+ key +"\">" + shoptexts['BuyButtonText'] + "</a>";                                
                            }
                            else {                                
                                productList += "<a href=\"" + buybuttonSubmit + ";\" id=\"BuyButton_"+ key +"\">" + shoptexts['BuyButtonText'] + "</a>";
                            }
                        productList += "</div>";
                        productList += "<div class=\"clear\"></div>";
                        productList += "<span id=\"loadContainer_" + key + "\" style=\"display:none; font-weight: bold;\">" + shoptexts['AddToBasketLoadingText'] + "</span>";
                        productList += "<span id=\"errorContainer_" + key + "\" style=\"width: 765px; margin-top:10px; display:none; font-weight: bold; font-size: 12px; color: #D00847; text-align:center;\">";
                            /* ENE - NOTE: Here can be list of errors according to templates with buybutton, but it is probably not necessary to have it here, but the id=errorContainer is used in script_add_to_basket.tpl*/
                        productList += "</span>";
                        
                    productList += "</div>";
                productList += "</div>";
            productList += "</form>";
  
        }
        productList += "</div><div class=\"clear\"></div>";
        $('productlist').innerHTML += productList;
    }
			
	if (data[0].product_count < 50) {
		for(var i=0; i < data[0].product_count; i++){
			$('productlist').innerHTML += "<b>data[0].id[" + i + "] is </b>=>" + data[0].id[i].annotation.title + "<br>"; //" (" + data[0].id[i].annotation.product_number + ")<br>";
		}
	}
	if (data[0].debug) {
		$('productlist').innerHTML += "<a name=\"debugSection\"></a>";
		$('productlist').innerHTML += "<br><b>Debug output:</b><br>\n";
		$('productlist').innerHTML += "<pre>" + data[0].debug + "</pre>";
	}
	
	// Clear the price range selector box
	$('csbe_product_gift_finder_price_range').options.length = 0;
	// Insert the price ranges
	updatePriceRangeOption(0, '-1', '-1', 'Vælg prisniveau her');
	updatePriceRangeOption(1, data[0].pricerange_0_100, 'pricerange_0_100', '0 - 100 kr.');
	updatePriceRangeOption(2, data[0].pricerange_100_250, 'pricerange_100_250', '100 - 250 kr.');
	updatePriceRangeOption(3, data[0].pricerange_250_500, 'pricerange_250_500', '250 - 500 kr.');
	updatePriceRangeOption(4, data[0].pricerange_500_1000000, 'pricerange_500_1000000', 'Over 500 kr.');
	
	// Clear the brand selector box
	$('csbe_product_gift_finder_brand').options.length = 0;
	// Insert the initial option
	var elOptNew = document.createElement('option');
    elOptNew.text = 'Søg blandt vores mange mærker';
    elOptNew.value = '-1';
    var elSel = $('csbe_product_gift_finder_brand');
    try {
      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
    }
    catch(ex) {
      elSel.add(elOptNew); // IE only
    }
    // Fill in the available brands as options
	for (i=0;i<=data[0].brand.length;i++) {
	  if (data[0].brand[i].state == true) {
	    var elOptNew = document.createElement('option');
	    elOptNew.text = data[0].brand[i].value; //.substring(5);
	    elOptNew.value = data[0].brand[i].index;
	    var elSel = $('csbe_product_gift_finder_brand');

	    try {
	      elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
	    }
	    catch(ex) {
	      elSel.add(elOptNew); // IE only
	    }
	  }
	}
}

