//K. dec 14-21,2011

function statusMenu(mySelect) {

    var pidValue = getParameter("ProductId");
    
    //bypass product detail page    
    if (pidValue == null || pidValue=="") {
        var e = document.getElementById("select0");
        var selProdValue = e.options[e.selectedIndex].value;
    }

    var Indx = mySelect;   
    with (document.aspnetForm.select2) {
        document.aspnetForm.select2.options.length = 0;
        if (Indx == 0) {
            options[0] = new Option("Select retailer type first", "");
        }

        //online
        if (Indx == 1) {
            options[0] = new Option("Choose an online retailer", "");

            if (pidValue != 37) {
                options[1] = new Option("London Drugs", "http://www.londondrugs.com/Cultures/en-US/Product+Detail/Health.htm?BreadCrumbs=Health;Health;Sexual%20Health%20and%20Wellness;Personal%20Massagers&ProductID=5390893&ProductTab=3");
                //options[1] = new Option("trojanvibrations.ca", "http://www.londondrugs.com/Cultures/en-US/Product+Detail/Health.htm?BreadCrumbs=Health;Health;Sexual%20Health%20and%20Wellness;Personal%20Massagers&ProductID=5390893&ProductTab=3");
            }
            else {
                options[1] = new Option("London Drugs", "http://www.londondrugs.com/Cultures/en-US/Product+Detail/Health.htm?BreadCrumbs=Health;Health;Sexual%20Health%20and%20Wellness;Personal%20Massagers&ProductID=5390901&ProductTab=3");
                //options[1] = new Option("trojanvibrations.ca", "http://www.londondrugs.com/Cultures/en-US/Product+Detail/Health.htm?BreadCrumbs=Health;Health;Sexual%20Health%20and%20Wellness;Personal%20Massagers&ProductID=5390901&ProductTab=3");
            }
        }
        //offline
        if (Indx == 2) {
            options[0] = new Option("Choose an offline retailer", "");
            options[1] = new Option("Brunet", "http://www.brunet.ca/en/search-branches.html");
            options[2] = new Option("Familiprix", "http://www.familiprix.com/SectionCorporative/Succursales/NosSuccursales.aspx");
            options[3] = new Option("Guardian", "http://www.rexall.ca/storelocator");
            options[4] = new Option("IDA", "http://www.rexall.ca/storelocator");
            options[5] = new Option("Loblaws", "http://www.loblaws.ca");
            options[6] = new Option("London Drugs", "http://www.londondrugs.com/Cultures/en-US/StoreLocator.htm?Location=0&Province=0&City=0&Store=0&MSCSProfile=DCCDF22EB27065BE6D1C166B2ADF9B3BD4FDADAF131800CCBD0669A08F9056FA9F5E589AEB9E9EF54E0BAB9C0AEC79A37E0E32237E548395D1F69D08830D2B2D2A6DC2E4F98E2DEAEA4D042649C04105872BD894BE2365394004E77CDEC35C645EC525152FDBF14360C42725D6B1B2FD2F26651500A06D51F56F0BF3714708BA&UserPref=");
            options[7] = new Option("Pharmasave", "http://www.pharmasave.com/storefinder.aspx");
            options[8] = new Option("Real Canadian Superstore", "http://www.realcanadiansuperstore.ca");
            options[9] = new Option("Rexall", "http://www.rexall.ca/storelocator");
            options[10] = new Option("Rexall Pharma-Plus", "http://www.rexall.ca/storelocator");
            options[11] = new Option("Sobeys Atlantic", "http://www.sobeys.com/Choose-Store.aspx?language=en");

            if (pidValue != "37" && selProdValue != "triphria" ) {
                options[12] = new Option("Uniprix", "http://www.uniprix.ca/en/stores");
            }
        }
        aspnetForm.select2.options[0].selected = true;
    }

}

function goToPage() {
    PageIndex2 = document.form2.select2.selectedIndex
    if (document.form2.select2.options[PageIndex2].value != "") {
        location.href = document.form2.select2.options[PageIndex2].value;
    }
} 

function getParameter(paramName) {
  var searchString = window.location.search.substring(1),
      i, val, params = searchString.split("&");

  for (i=0;i<params.length;i++) {
    val = params[i].split("=");
    if (val[0] == paramName) {
      return unescape(val[1]);
    }
  }
  return null;
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

$(document).ready(function() {

    // WTB on Product Detail 
    var pidValue = getParameter("ProductId");

    if (pidValue == "36" || pidValue == "37") {
        //window.alert(pidValue);
        $("#wtb_div").css("visibility", "visible");
    }
    else {
        $("#wtb_div").remove();
        $("#wtb_div_hd").remove();
    }

    // new product rotation
    var dataStore = readCookie("sldata");
    if (dataStore == "triphoria") {
        $("#tbtriphoria").remove();
        eraseCookie("sldata");
    }
    else {
        $("#tbpluse").remove();
        createCookie("sldata", "triphoria", "1")
    }

    //select type ddl change event
    $("#selType").change(function() {
        var seltype = $("#selType").val();
        statusMenu(seltype);
    });
    
    //select store ddl change event
    $("#selectStore").change(function() {
        var selStore = $("#selectStore option:selected").val();
        if (selStore != "") {
            window.open(selStore, "_blank");
        }
    });
});
