function submitForm(formID,extraValuesToSet,returnFormValues,showHTMLTargetContainer,extraLoadFunc)
{
    //extraValuesToSet (if exists) is a string of fields to be updated before submission. E.g: mode=updateSummink,targetID=4
    if (extraValuesToSet)
    {
        extraValuesToSet = extraValuesToSet.split("!!!") ;
        for (i=0;i<extraValuesToSet.length;i++)
        {
            tempArray = extraValuesToSet[i].split("=") ;
            $("#" + formID + " input[name=" + tempArray[0] + "]").attr("value",tempArray[1]) ;
        }
    }
    
    //frmResults = $("#" + formID).serialize() ;
    frmResultsArray = $("#" + formID).serializeArray() ;
    targetURL  = $("#" + formID).attr("action") ;
    
    currentArrayName = "" ;
    frmResults       = "" ;
    
    jQuery.each(frmResultsArray, function(i, field){
        if (currentArrayName != field.name)
        {
            if (currentArrayName!="") {frmResults += "&" ;}
            frmResults   += field.name + "=" + field.value ;
            currentArrayName = field.name ;
        }
        else {frmResults   += "," + field.value ;}
      });
    
    if (returnFormValues) {return frmResults ;}
    else {
        $.post(targetURL,{ results: frmResults },function(returnInfo)
        {
            if (targetURL) {pageTracker._trackPageview("/" + targetURL & "?" + frmResults);} //Google Analytics Tracking
            if (showHTMLTargetContainer)
            {
                $("#" + showHTMLTargetContainer).html(returnInfo) ;
            }
            else
            {
                returnInfo = returnInfo.split(":") ;
                //0 = success/error
                //1 = message to show
                //2 = function to call afterwards
                if (returnInfo[2]) {eval(returnInfo[2]);}
                if (returnInfo[0]=="success")
                {
                    //$("#dialog").html(returnInfo[1]) ;
                    //$("#dialog").dialog({ title: "Success!", height:"200px", width:"300px", resizable: false, draggable: false, modal: true, overlay: { opacity: 0.7, background: "black" } });
                }
                else if (returnInfo[0]=="error")
                {
                    //showError(returnInfo[1]) ;
                    //$("#dialog").html(returnInfo[1]) ;
                    //$("#dialog").dialog({ title: "Error!", height:"200px", width:"300px", titleBarClass:"ui-dialog-titlebarError", resizable: false, draggable: false, modal: true, overlay: { opacity: 0.7, background: "black" } });
                }
            }
            if (extraLoadFunc) {eval(extraLoadFunc) ;}
        });
    }
}

function getMyBag() {
    $.ajax({
        type: "GET",
        url: "http://" + top.location.host + "/" + "getMyBag.asp",
        data: "",
        cache: false,
        success: function(html) { $("#MyBag").html(html); }
    });
}


function showError(errorMsg)
{
    errorHTML = "<h3 class='error'>" + cnt_siteErrHeader + "</h3>" ;
    errorHTML += "<div class='info'>" + errorMsg + "</div>" ;
    errorHTML += "<div class='btnBox2'><div class='top'><div class='bottom'><div class='right'><div class='topLeft'><div class='topRight'><div class='bottomLeft'><div class='bottomRight'>" ;
    errorHTML += "<a href='javascript:$.unblockUI();' title='" + cnt_siteCloseWinLnk + "'>" + cnt_siteCloseWinLnk + "</a>" ;
    errorHTML += "</div></div></div></div></div></div></div></div><div class='clear'>&nbsp;</div><br />" ;
    
    $('#infoMsg').html(errorHTML);
    $.blockUI({ message: $('#infoMsg'), css: { width: '400px' } }); 
}

function loadChildPage(wotURL,targetContainer,dataToPass,successFunc)
{
    $.ajax({
        type: "GET",
        url: wotURL,
        data: dataToPass,
        cache: false,
        success: function(html) 
                 {
                     $("#" + targetContainer).html(html) ;
                     if (successFunc) {eval(successFunc) ;}
                     pageTracker._trackPageview("/" + wotURL & "?" + dataToPass); //Google Analytics Tracking
                 }
    })
}

function disableBtn(targetID)
{
    $("#" + targetID).attr("disabled","true") ;
    $("#" + targetID).html(cnt_siteLoading) ;
}


function toggleBlockContent(blockIt)
{
    if (blockIt) {/*$("#siteContent").block({ overlayCSS:{ backgroundColor:"#000",opacity:"0.35" }, message: null })*/;isContentBlocked = true ;}
    else {/*$("#siteContent").unblock() ;*/isContentBlocked = false ;}
}

function updateNumProductsFound(result)
{
    selectedArr        = $("#frmChoiceSelection [name=selectedColourIDList]").serializeArray() ;
    selectedCheckedArr = $("#frmChoiceSelection [name=selectedColourIDList]:checked").serializeArray() ;
    
    if ((selectedCheckedArr.length==$("#frmChoiceSelection [name=selectedColourIDList]").length)||(selectedCheckedArr.length==0)) {theHtml = cnt_siteAllColours ;}
    else
    {
        theHtml = "" ;
        jQuery.each(selectedArr, function(i, field){
            theTitle = $("#" + field.name + "_" + field.value + "_title").text() ;
            theHtml += theTitle + "<br />" ;
        });
    }
    $("#selectedColours").html(theHtml) ;
    
    selectedArr        = $("#frmChoiceSelection [name=selectedTypeIDList]").serializeArray() ;
    selectedCheckedArr = $("#frmChoiceSelection [name=selectedTypeIDList]:checked").serializeArray() ;
    
    if ((selectedCheckedArr.length==$("#frmChoiceSelection [name=selectedTypeIDList]").length)||(selectedCheckedArr.length==0)) {theHtml = cnt_siteAllTypes ;}
    else
    {
        theHtml = "" ;
        jQuery.each(selectedArr, function(i, field){
            theTitle = $("#" + field.name + "_" + field.value + "_title").text() ;
            theHtml += theTitle + "<br />" ;
        });
    }
    $("#selectedTypes").html(theHtml) ;
    
    selectedArr        = $("#frmChoiceSelection [name=selectedPatternIDList]").serializeArray() ;
    selectedCheckedArr = $("#frmChoiceSelection [name=selectedPatternIDList]:checked").serializeArray() ;
    
    if ((selectedCheckedArr.length==$("#frmChoiceSelection [name=selectedPatternIDList]").length)||(selectedCheckedArr.length==0)) {theHtml = cnt_siteAllPatterns ;}
    else
    {
        theHtml = "" ;
        jQuery.each(selectedArr, function(i, field){
            theTitle = $("#" + field.name + "_" + field.value + "_title").text() ;
            theHtml += theTitle + "<br />" ;
        });
    }
    $("#selectedPatterns").html(theHtml) ;
    
    $("#postIt").html("<a href=\"javascript:searchTerm='';doBestSeller='';searchProducts();\" title=\"" + result + cnt_siteProductsWereFound + "\">" + result + cnt_siteProductsWereFound + "</a>") ;
}
        
        function closeChoiceWindow()
        {
            $("#choiceBox_selection").hide() ;
            $("#" + activeChoiceMenu + "_box").slideUp("fast") ;
            toggleBlockContent(false) ;
            activeChoiceMenu = "" ;
        }
        
        function doSearchCloud(linkNum)
        {
            $("#frmChoiceSelection").attr("action","products.asp") ;
            $("#frmChoiceSelection input[name=mode]").val("searchTerm") ;
            $("#frmChoiceSelection input[name=searchTerm]").val($("#searchCloud_" + linkNum).html()) ;
            $("#frmChoiceSelection").submit() ;
        }
        
        
        function showLoading(loadingWot)
        {
            loadingBtn = "<div class=\"pagingBtnLoading\"><div class=\"left\"><div class=\"right\"><a href=\"javascript:void(0);\" class=\"\" title=\"" + cnt_sitePleaseWait + "\">" + cnt_siteLoadingPW + "</a></div></div></div><div class=\"clear\">&nbsp;</div>" ;
            if (loadingWot=="products")
            {
                $("#pagingControls").html(loadingBtn) ;
                $("#productResults_screen" + currentScreenNum + " a").attr("href","javascript:void(0);") ;
            }
            else if (loadingWot=="productSearch")
            {
                $("#productResults_screen" + currentScreenNum).html("<div id=\"productPagingControls\">" + loadingBtn + "</div>") ;
            }
            else if (loadingWot=="popup")
            {
                $("#popupWindowDetails").html("<div id=\"pagingControls\" style=\"margin:55px 200px;\">" + loadingBtn + "</div>") ;
            }
            else if (loadingWot=="productView")
            {
                if ($("#productDetails").height() > 200){$("#productImgContainer").html("<div id=\"pagingControls\" style=\"width:196px;margin:140px 0 0;\">" + loadingBtn + "</div>") ;}
                else {$("#productDetails").html("<div id=\"pagingControls\" style=\"margin:55px 200px;\">" + loadingBtn + "</div>") ;}
            }
        }
        
        
        function selectAllChoices(checkAll)
        {
            findWot = activeChoiceMenu.split("_")[1] ;
            findWot = findWot.substr(0,1).toUpperCase() + findWot.substr(1,findWot.length-1) ;
            formArr = $("#frmChoiceSelection input[name=selected" + findWot + "IDList]") ;
            jQuery.each(formArr, function(i, field){
                field.checked = checkAll ;
            });
            submitForm("frmChoiceSelection");
        }
        
        var theOffsetArr ;
        function updateCart(wotMode,productOption_id,productPrice,checkQty,lnkObj,parseQty) {
            theOffsetArr = $("#siteContainer").offset() ;
            if (wotMode == "updateFromCart" | wotMode == "removeFromCart")
            {
                $("#frmCart input[name=mode]").val(wotMode);
                $("#frmCart input[name=poid]").val(productOption_id);
                $("#frmCart").submit() ;
            }
            else {
                var qty = checkQty ? $("#numQty").attr("value").match(/^\d+$/) == null ? "" : $("#numQty").attr("value") : "" ;
                qty = qty != "" ? "!!!numQty=" + qty : "" ;
                parseQty = parseQty ? "!!!parseQty=" + parseQty : "";
                submitForm("frmCartProcess", "mode=" + wotMode + "!!!productOption_id=" + productOption_id + qty + "!!!decPrice=" + productPrice + parseQty);
            }
            setTimeout("getMyBag()", 100);
            //getMyBag();
        }
        
        
        var currentAnimNum = 0 ;
        function animCartItem() {
            currentAnimNum ++ ;
            $("#animCart").append("<div id=\"animCart_" + currentAnimNum + "\" class=\"animCartItem\">&nbsp;</div>") ;
            $("#animCart_" + currentAnimNum).css({top:(mouseY-theOffsetArr.top)-0 + "px",left:((mouseX-theOffsetArr.left)+100) + "px",opacity:0.4});
            $("#animCart_" + currentAnimNum).animate({ 
                height: "20px",
                width: "20px",
                top: "175px",
                left: "1020px",
                opacity: 0.4
              }, 1000 ).animate({
                height: "10px",
                width: "6px",
                opacity: 0,
                top: "175px",
                left: "1020px"
                    },1000);
        }
        
        function returnPrice(wotPrice)
        {
            var newPrice ;
            if (site_currencySign == "pound") {newPrice = "&pound;" + wotPrice ;} //wotPrice.toFixed(2) <-- not needed?
            if (site_currencySign == "euro")
            {
                //newPrice = (wotPrice * site_currencyRate).toFixed(2) ;
                newPrice = "" + wotPrice.replace(".",",") + " &euro;" ;
            }
            return newPrice ;
        }
        
        function updateVisualCart(numOfItems,totalCost) {
            animCartItem();
            $("#user_cart_previewNumOfItems").html("My Bag (" + numOfItems + " " + cnt_siteCartItems + " - " + returnPrice(totalCost) + ")");            
            $("#frmCartProcess input[name=numQty]").attr("value","") ;
            $("#frmCartProcess input[name=decPrice]").attr("value","") ;
        }
        
        
        function positionPopup(popupID)
        {
            if (isProductViewed)
            {
                boxHeight    = $("#productDetails").height() ;
                boxWidth     = $("#productDetails").width() ;
                windowHeight = $(window).height() - 50 ; //minus summink
                windowWidth  = $(window).width() - 20 ; //minus summink
                
                if (windowHeight<boxHeight) {targetTop = $(window).scrollTop() ;}
                else
                {
                    targetTop = (Math.round((windowHeight/2) - (boxHeight/2)) + $(window).scrollTop()) ;
                    targetTop = targetTop < 0 ? 0 : targetTop ;
                }
                
                if (windowWidth > $("#siteContainer").width())
                {targetLeft = Math.round(($("#siteContainer").width()/2) - (boxWidth/2)) - 10 + $(window).scrollLeft() ;}
                else
                {targetLeft = Math.round((windowWidth/2) - (boxWidth/2)) + $(window).scrollLeft() ;}
                
                targetTop  = targetTop < 0 ? 0 : targetTop ;
                targetLeft = targetLeft < 0 ? 0 : targetLeft ;
                
                $("#modalContainer").css({ visibility:"visible", left:targetLeft + "upcpx", top:targetTop + "px"})
            }
            else if (isPopupWindowShowing)
            {
                boxHeight    = $("#popupWindowDetails").height() ;
                boxWidth     = $("#popupWindowDetails").width() ;
                windowHeight = $(window).height() - 50 ; //minus summink
                windowWidth  = $(window).width() - 20 ; //minus summink
                
                if (windowHeight<boxHeight) {targetTop = $(window).scrollTop() ;}
                else
                {
                    targetTop = (Math.round((windowHeight/2) - (boxHeight/2)) + $(window).scrollTop()) ;
                    targetTop = targetTop < 0 ? 0 : targetTop ;
                }
                
                if (windowWidth > $("#siteContainer").width())
                {targetLeft = Math.round(($("#siteContainer").width()/2) - (boxWidth/2)) - 10 ;}
                else
                {targetLeft = Math.round((windowWidth/2) - (boxWidth/2)) ;}
                
                targetTop  = targetTop < 0 ? 0 : targetTop ;
                targetLeft = targetLeft < 0 ? 0 : targetLeft ;
                
                $("#modalContainer").css({ visibility:"visible", left:targetLeft + "px", top:targetTop + "px"}) ;
            }
        }
        
        
        var isPopupWindowShowing = false ;
        function popupPage(targetPage,hideIt,overrideCurrentPage,extraLoadFunc)
        {
            if (hideIt)
            {
                $.modal.close() ;
                //$.unblockUI();
                isPopupWindowShowing = false ;
                $("#popupWindowDetails").html("") ;
            }
            else
            {
                if ((!isPopupWindowShowing)||(overrideCurrentPage))
                {
                    isPopupWindowShowing = true ;
                    $('#popupWindowContainer').modal({
                        close:false,
                        overlay: 45
                    });
                    
                    //$.blockUI({ message: $("#popupWindowContainer"), overlayCSS:  { opacity:"0.35"}, centerY:false, centerX:false, css: { position:"absolute", border:0,padding:0,top:0,left:0, visibility:"hidden"} }) ;
                    showLoading("popup") ;
                    loadChildPage(targetPage,"popupWindowDetails","","positionPopup();" + extraLoadFunc) ;
                }
                else
                {
                    showLoading("popup") ;
                    loadChildPage(targetPage,"popupWindowDetails","mode=success") ;
                }
            }
        }
        
        function setPageHeader(wotPage,targetScreenNum)
        {
            if (wotPage == "productSearch")
            {
                targetScreenNum = targetScreenNum ? targetScreenNum : currentScreenNum ;
                $("#pageHeader").html($("#numProductsFound").html() + "<div id=\"pagingControls\"></div>");
                $("#pagingControls").html($("#productResults_screen" + targetScreenNum +" .tempPagingControls").html()) ;
            }
        }
        
        function cart_proceedToPayment()
        {
            self.location = $("#infoMsg").html() ;
        }
        
        function changeProductImg(wotImg,subDir)
        {
            if (subDir) {imgSrcPrefix = "../" ;}
            else {imgSrcPrefix = "" ;}
            $("#productImg img").attr("src",imgSrcPrefix + "images/Products/" + wotImg) ;
        }
            
    
function updateDeliveryAddress()
{
    if ($("#selUseBillingDetails").attr("checked"))
    {
        $("#txtDeliveryTitle").val($("#txtBillingTitle").val()) ;
        $("#txtDeliveryFirstName").val($("#txtBillingFirstName").val()) ;
        $("#txtDeliverySurname").val($("#txtBillingSurname").val()) ;
        $("#txtDeliveryAddress1").val($("#txtBillingAddress1").val()) ;
        $("#txtDeliveryAddress2").val($("#txtBillingAddress2").val()) ;
        $("#txtDeliveryCity").val($("#txtBillingCity").val()) ;
        $("#txtDeliveryCounty").val($("#txtBillingCounty").val()) ;
        $("#txtDeliveryPostcode").val($("#txtBillingPostcode").val()) ;
        $("#numDeliveryCountry_id").val($("#numBillingCountry_id").val()) ;
    }
}

function checkForEndFunc()
{
    eval($("#infoMsg").html())
}

function prepareLinks(objID,actionToUse,isJS)
{
    newHref = isJS ? "javascript:" + actionToUse : actionToUse
    $("#" + objID).attr("href",newHref) ;
    /*if (!document.getElementById) return false;
    if (!document.getElementById(containerId)) return false;
    if (!document.getElementById(canvasId)) return false;
    var links = document.getElementById(containerId).getElementsByTagName("a");
    for (var i=0; i<links.length; i++) {
        links[i].canvasId = canvasId;
        links[i].file = file;
        links[i].onclick = function() {
            var data = this.getAttribute("href").split("?")[1];
            return (!sendData(data,this.file,this.canvasId));
        };
    }*/
}

var reg_activeCartIcon = 1 ;
function reg_setCartIcon(arrNum,cartIcon)
{
    $("#cartIcon").val(cartIcon) ;
    $("#cartIcon_" + reg_activeCartIcon).removeClass("reg_cartIconActive") ;
    $("#cartIcon_" + arrNum).addClass("reg_cartIconActive") ;
    reg_activeCartIcon = arrNum ;
}

function prodVote(option_id,voteValue)
{
    submitForm("productOptionVote","option_id=" + option_id + "!!!voteValue=" + voteValue) ;
}

function updateProductOptionVotes(option_id,voteValue)
{
    $("#loveItBox_" + option_id + " .content").html("&nbsp;&nbsp;&nbsp;&nbsp;Thanks for voting.") ;
    currentVotesArr      = $("#loveItBG_" + option_id).html().split(",") ;
    if (currentVotesArr[0]=="") {currentVotesArr[0] = 0 ;}
    totalLoveItVotes     = voteValue ? parseInt(currentVotesArr[0]) + 1 : parseInt(currentVotesArr[0]) ;
    totalVotes           = parseInt(currentVotesArr[1]) + 1 ;
    percentOfLoveItVotes = (totalLoveItVotes / totalVotes) * 100 ;
    numOfHeartPixels     = Math.round((34/100) * percentOfLoveItVotes) ;
    $("#loveItBG_" + option_id).css("height",numOfHeartPixels + "px") ;
    $("#loveItHeartImg_" + option_id).attr("alt","" + Math.round(percentOfLoveItVotes,0) + "% of you Love this Product!") ;
}

function zoomImg(targetObj)
{
    var options = {
        zoomWidth: 300,
    	zoomHeight: 285,
        xOffset: 100,
        yOffset: 0,
        position: "right",
        showPreload: true
    };
    
    $(targetObj).jqzoom(options) ;
}

var activeSubNavNum = 0;
function toggleNav(navNum)
{
    if (activeSubNavNum!=navNum)
    {
        $("#subNav" + activeSubNavNum + "_container").hide("fast") ;
        $("#subNav" + navNum + "_container").show("fast") ;
        activeSubNavNum = navNum
    }
    else {$("#subNav" + activeSubNavNum + "_container").toggle("fast") ;}
}