﻿/// <reference path="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.1-vsdoc.js" />
var loadProductCode;

$(document).ready(function() {
    var groupDDL = $("div.columns select.ddlGroup");
    groupDDL.change(function() {
        GetProductOptions(this);
    });

    if (groupDDL.val() != "-1") {
        GetProductOptions(groupDDL);
    }

    $("input#accsubmit").click(function() {
        var href = GetAccesoriesUrl();
        if (href != null) {
            location.href = href;
        }
    });
	
	$("select#ddlBrochure").change(function() {
		window.open($(this).val(),'');
	});
});

function GetProductOptions(ddl) {
    if ($(ddl).val() != "-1") {
    	// DL database uses 'UK' instead of 'GB'...
    	var localCountryCode = countrycode;
    	if(localCountryCode == "GB") localCountryCode = "UK";
    	
        $.get(siteurl + "/handlers/ajaxhandler.ashx",
					{
					    action: "accgroupproduct",
					    group: $(ddl).val(),
					    countrycode: localCountryCode,
					    languagecode: languagecode
					},
					function(data) {
					    var productDDL = $("div.columns select.ddlProduct");
					    if (data != '') {
					        productDDL.html(data);

					        if (loadProductCode) {
					            productDDL.val(loadProductCode);
					            productDDL.change();
					        }
					    }
						else {
							productDDL.html("<option value=\"-1\"></option>");
						}
						
						var productOptions = $("div.innbrowseform .ddlProduct option");
						productOptions.each(function() {
							if($(this).attr('class') == "indent1") $(this).text("" + $(this).text());
							if($(this).attr('class') == "indent2") $(this).text("    " + $(this).text());
							if($(this).attr('class') == "indent3") $(this).text("        " + $(this).text());
						})

	                }
				);
    }
};

function GetAccesoriesUrl() {
    var productGroup = $("div.columns select.ddlGroup option:selected");
    var productOption = $("div.columns select.ddlProduct option:selected");
    var productOptionValue = productOption.val();

    if (productGroup.val() != "-1") {
        var productCode = productGroup.val();

        if (productOption.val() != "-1") {
            productCode = productOptionValue.split("$")[1];
        }
        var href = accessoriesurl.replace("!PC!", escape(productCode));
        return href;
    };
    return null;
}

// Loads the accessories from hash variables. e.g. "M01/125ccXTR///"
function LoadAccessories(hash) {
    var arrHash = hash.split("/");
    if (arrHash.length > 0) {

        var groupDDL = $("div.columns select.ddlGroup");
        groupDDL.val(arrHash[0]);

        if (arrHash.length > 1) {
            loadProductCode = arrHash[1];
        }

        groupDDL.change();
    }

}

function LoadUrl(url) {
    document.location = siteurl + url;
}
