﻿// ------------------------------------------------------------------------------------------
// Copyright AspDotNetStorefront.com, 1995-2010.  All Rights Reserved.
// http://www.aspdotnetstorefront.com
// For details on this license please visit  the product homepage at the URL above.
// THE ABOVE NOTICE MUST REMAIN INTACT.
// ------------------------------------------------------------------------------------------
function $bindMethod(object, method) {
  return function() {
    return method.apply(object, arguments);
  };
}

function $window_addLoad(handler) {
    if (window.addEventListener) { 
        window.addEventListener('load',handler,false);
    }
    else if (document.addEventListener) {
        document.addEventListener('load',handler,false);
    }
    else if (window.attachEvent) { 
        window.attachEvent('onload',handler);
    }
    else {
        if (typeof window.onload=='function') {
            var oldload=window.onload;
            window.onload = function(){
                oldload();
                handler();
            }
        } 
        else { window.onload=init; }
    }

}

function $getElement(id, handler) {
    var el = document.getElementById(id);
    return el;
}

var Keys = {
    Enter: 13
}

function $handleSearchEnterKey(id, handler) {
    var el = $getElement(id);    
    if(el) {
        var delKeypress = function(e) {

            var keyCode;
            if (e && e.which) {
                keyCode = e.which;
            }
            else if (typeof (event) != 'undefined') {
                keyCode = event.keyCode;
            }

            // we must manually invoke the Page_ClientValidate
            // method here since relying on the normal behavior
            // is not guaranteed to set the appropriate validation flags
            // to stop the postback.
            if (keyCode == Keys.Enter) {
                if (typeof (Page_ClientValidate) != 'undefined') {
                    if (Page_ClientValidate() == false) {
                        return;
                    }
                }

                handler();

                return false;
            }
        }
        
        el.onkeypress = delKeypress;
    }

}


//LA Amends:090309: Added a bit of JS to append genre id to end of url
function appendURL() {
    var newURL;
    var oldURL;
    // var strmanid;
    // var arymanid;
    // var ismainid;
    var URL1;
    var URL;
    var pagenumber;
    //var mylist = document.getElementById("selGenreID");
    //var val = mylist.options[mylist.selectedIndex].value;
    var mylist2 = document.getElementById("selPriceID");
    var val2 = mylist2.options[mylist2.selectedIndex].value;

    //if (val != "") {
    //    newURL = 'genreid=' + val;

    //}
    if (val2 != "") {
        newURL = 'sprice=' + val2;

    }
    oldURL = document.location.href;

    var indexstr = oldURL.indexOf("?");
    //alert("indexstr" + indexstr );
    //if genre id already set in url then
    //split url on ? and then reassign genre id with selected id from 
    //drop down.
    if (indexstr != -1) {
        //alert("matched");
        URL = oldURL.split("?");
        //alert("0 minus querystrings: "+URL[0]);//main url minus querystring variables
        //alert("1 with querystrings: "+URL[1]);//querystring variables
      //  alert("url array length: "+URL.length);
        var test;

        test = URL[1].split("&");
      //  alert("test: " + test[0]);
        //alert("test array multi = : "+test.length);
        // alert("test1 genre :"+test[0]);
        // alert("test2 price :"+test[1]);
        // alert("test3 something :"+test[3]);

        strmainid = test[0].split("=")
        //alert("strGenre ="+test[0]);
        //alert("test length: "+test.length);
     //   alert(strmainid);
        //alert(test.length);

        if (test.length > 1)
        //more than one variable in query string
        {
            //  alert("test > 1");

            //strmainid = test[1];
            //get first variable
            //arymanid = strmainid.split("=");
            // alert("test[0]" + test[0]); //pagenum
            // alert("test[1]" + test[1]); //price
            //  alert("strmain: " + strmainid[0]); //pagenum



            if (strmainid[0] == "pagenum") {
                //    alert(test[0]);
                //    alert(test[1]);
                // alert("MY PAGENUMBER ");
                if (test[1] != "") {
                    if (test[0] != "pagenum=1") {
                        pagenumber = "pagenum=1";
                    }
                    else {
                        pagenumber = test[0];
                    }
                    //URL = URL[0] + "?" + pagenumber + "&genreid=" + val + "&sprice=" + val2;
                    URL = URL[0] + "?" + pagenumber + "&sprice=" + val2;
                }
                else {
                    //alert("test1: ");
                    // URL = URL[0] + "?genreid=" + val + "&sprice=" + val2;
                    URL = URL[0] + "?sprice=" + val2;
                }
            } //if pagenum



            else {
                //  alert("ELSE");
                //URL = URL[0] + "?genreid=" + val + "&sprice=" + val2;
                URL = URL[0] + "?sprice=" + val2;


            }

        } //test.length
        else {
           //  alert("ELSE 2 ");
           //  alert("strmainid[0]" + strmainid[0]);
           // if (strmainid[0] == "manid") {
                // alert("yes");
           //     URL = URL[0] + "?" + test[0] + "&" + newURL;
           // }
            if (strmainid[0] == "pagenum") 
 {
                URL = URL[0] + "?" + test[0] + "&" + newURL;
            }
            else {
                URL = URL[0] + "?" + newURL;
            //    alert(URL);
            }

        }
    }
    else {
       // alert("ELSE 3 ");
       // alert(oldURL);
       // alert(newURL);
        //no variables at the moment so add genreid to the end of url
        URL = oldURL + "?" + newURL
    }

    //alert(URL);
    document.location.href = URL;

} //function
