﻿$(document).ready(function () {
// Beginning of Homepage tabs
    $("div#hometabs .t_tabs a").click(function () {
        $("div#hometabs .t_tabs a").removeClass("active");
        $(this).addClass("active");
    });

    var innerMenu = null;

    var tidMenu = null;
    $(".t_tabs a").click(function () {
        $("div.snav").each(function () {
            if ($(this) != innerMenu) {
                $(this).hide();
            }
        });
        innerMenu = $("div." + $(this).attr("id")).show();
    })

    // End of Homepage tabs
    $("div#bannerHolder").bannerRotator(20000, "/modules/rs3BannerManager.aspx");
    $("div.loginInputs").css("height","80px");
    pageload();
    quests();
    depots();
    $("a#ma_reports").attr("target","_blank");
    $("button, div.rsButton").css("opacity",0.7);
    $(".mediabox").mediabox({ boxTitle: "Sportron Media", w: 0, h: 0 });
    setupRS3Selects();
    $("div.registerImage").hide();
    $("div.RegisterInto").hide();
    $("div.loginLanding button").click(function(){
        $("div.loginLanding").hide();
        $("div.LoginInto").hide();
        $("div.RegisterInto").show();
        var rndImg = Math.floor(3*Math.random()) + 1;
        $("div#registerImage img").attr("src","../images/registerimage_0" + rndImg + ".jpg"); 
        $("div#registerImage").fadeIn("slow");
    });
    $("div.LoggedOut button").click(function(){
        doLogin();      
    }).css("cursor","pointer"); 
    $("div.logoutNow button").click(function(){
        doLogout();      
    }).css("cursor","pointer"); 
    $("input[@name=search]").click(function(){
    //TODO Search Icons
    });
    if($("div#menuHolder1 span#login").text() == "Login")
    {
        $("div.LoggedIn").hide();
        $("div.loginLanding").show();
        $("div.LoggedOut").show();
    }
    else
    {
        $("div.loginLanding").hide();
        $("div.LoggedIn").show();
        $("div.LoggedOut").hide();
        if(_customer.MAType != undefined)
        {
            if(_customer.MAType == "D")
            {
                //debugger;
                if(isCheckOut())
                {
                    $("div.checkOut, div.loginSignup").slideDown("normal"); 
                }
                else
                {
                    $("div.loginSignup").slideDown("normal");  
                }             
            }
            else
            {
                if(_customer.UserID == "0")
                {
                    $("div#wizardHolder").load("/includes/sportron/updateCustomer.aspx",{IDNum : _customer.IDNum, cache:newid()},function(){
                    setupRS3Selects();
                    setupWizard();
                    setupValidation(); 
                    $("div.login").slideUp("normal");
                    }); 
                }
                else
                {
                    if(_customer.MASiteID == "0")
                    {
                        setupURLs(_customer);
                        $("div.loginMysite").slideDown("normal");
                        $("div.partnerOrders").slideDown("normal");
                        if(isCheckOut())
                        {
                            $("div.checkOut").slideDown("normal"); 
                        }                        
                        $("div.LoggedIn").show();
                        $("div.LoggedOut").hide();                             
                    }
                    else
                    {
                        if(_customer.MAStatusID == "1")
                        {
                            $("div.manageMysite").slideDown("normal");
                            if(isCheckOut())
                            {
                                $("div.checkOut").slideDown("normal"); 
                            }                              
                            $("div.LoggedIn").show();
                            $("div.LoggedOut").hide(); 
                        }
                        if(_customer.MAStatusID == "4" || _customer.MAStatusID == "5")
                        {
                            setupURLs(_customer);
                            $("div.loginMysite").slideDown("normal");
                            $("div.loginMysite button").html("Re-Activate");
                            $("div.partnerOrders").slideDown("normal");
                            if(isCheckOut())
                            {
                                $("div.checkOut").slideDown("normal"); 
                            }                              
                            $("div.LoggedIn").show();
                            $("div.LoggedOut").hide();  
                        }                       
                    } 
                }            
            }
        }
 
    } 
        
});

function depots()
{
    $("div.depot div.header, div.depotopen div.header").click(
        function(){
            if($(this).parent().attr("class").indexOf("depotopen") > -1){
                $(this).parent().removeClass("depotopen").addClass("depot");
            }
            else{
                $(this).parent().removeClass("depot").addClass("depotopen");
                if($(this).next().find("iframe").attr("src") == undefined)
                {
                    $(this).next().find("iframe").attr("src",$(this).next().find("iframe").attr("title"));   
                }
            }
        }
    );
}

function quests() {
    $("div.questions div.header, div.questionopen div.header").click(
			function () {
			    if ($(this).parent().attr("class").indexOf("questionopen") > -1) {
			        $(this).parent().removeClass("questionopen").addClass("questions");
			    }
			    else {
			        $(this).parent().removeClass("questions").addClass("questionopen");
			    }
			}
		);
}

function newid()
{
    var d = new Date();
    var s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    return s.charAt(d.getSeconds()) +  ((d.getSeconds()+ d.getMinutes())%13)+ s.charAt((d.getHours() + d.getMinutes() + d.getSeconds()) % 60) + d.getMilliseconds(); 
}


function isCheckOut()
{
    var querystring = location.search;
    if(querystring)
    {
        if(querystring.indexOf("checkOut") >= 0)
        {
            return true;   
        }
    } 
    return false;     
}

function underAge(idnum)
{
    var idyear = idnum.toString().substr(0,1) == "0" ? "20" + idnum.toString().substr(0,2) : "19" + idnum.toString().substr(0,2);
    var birthdate = new Date(idyear, parseInt(idnum.toString().substr(2,2),10)-1, parseInt(idnum.toString().substr(4,2),10));
    var today = new Date();
    if(dateDiff("d", birthdate, today, 1, 1) < 5844)
    {
        alert("You need to be at least 16 years of age to register as a Sportron Partner")
        return true;
    }
    else
        return false;
}

var oSAIDValidate = new SAIDValidate();
function doLogin()
{
    //debugger;
    var frm = $("div.login form")[0];
    if(validate(frm))
    {
    
        //if starting up
        var butCaption = $("div.LoggedOut button").html().trim();
        if(butCaption == "Start")
        {
            //debugger;
            if(underAge($("div.loginForm input[@name=MAID]").val().trim()))
                return;    
            busy(true);
            var parms0 = {IDNum: $("div.loginForm input[@name=MAID]").val().trim(), cache:newid()};
            $.getJSON("/includes/sportron/getDataByID.aspx",parms0,function(customer)
            {
                 //debugger;
                 busy(false);
                if(typeof customer == "undefined")
                {
                    if(oSAIDValidate.SAIDCheck($("div.loginForm input[@name=MAID]").val().trim()) == false)
                    {
                        alert("Please enter a valid South African Identity Number.\nInitially we will only be handling retail shopping for South African residents.\nIn later phases we will be introducing international shopping and delivery.");    
                    }
                    else
                    {
                        alert("We couldn't find you in our database, please \"Register to Shop\" \n\nor \n\n\"Sign Up as a Sportron Partner and enjoy lower prices!\"!");
                        $("div.registerImage").hide();
                        $("div.loginLanding").hide();
                        $("div.loginNow").hide();
                        $("div.loginRegister, div.loginSignup").slideDown("normal");
                        $("div#registerSignUp").css("float","right").fadeIn("slow");
                    }                
                }
                else
                {
                    if(oSAIDValidate.SAIDCheck($("div.loginForm input[@name=MAID]").val().trim()) == false)
                    {
                        alert("Please enter a valid South African Identity Number.\nInitially we will only be handling retail shopping for South African residents.\nIn later phases we will be introducing international shopping and delivery.");    
                        return;
                    }
 
                    //MA with password and Email Address
                    if((customer.MAType == "M") && (customer.Password == "Yes") && (customer.Email))
                    {
                        if("BC".indexOf(customer.PayMeth)== -1)
                        {
                            alert("Unfortunately according to our records your Sportron account type currently does'nt qualify for a MySite, please contact Customer Services on (011) 317 8500.");
                            return;
                        }                    
                        MAPasswordEmail();
                    }
                    //MA with without password and Email Address
                    if(((customer.MAType == "M") && (customer.Password == "Yes") && (!customer.Email)) || ((customer.MAType == "M") && (customer.Password == "No") && (!customer.Email)) || ((customer.MAType == "M") && (customer.Password == "No") && (customer.Email)))
                    {
                        if("BC".indexOf(customer.PayMeth)== -1)
                        {
                            alert("Unfortunately according to our records your Sportron account type currently does'nt qualify for a MySite, please contact Customer Services on (011) 317 8500.");
                            return;
                        }                    
                        MANoPasswordEmail();
                    }
                    //Direct Customer with password and Email Address
                    if((customer.MAType == "D") && (customer.Password == "Yes") && (customer.Email))
                    {
                        DCPasswordEmail();
                    }
                    //Direct Customer with without password and Email Address
                    if(((customer.MAType == "D") && (customer.Password == "Yes") && (!customer.Email)) || ((customer.MAType == "D") && (customer.Password == "No") && (!customer.Email)) || ((customer.MAType == "D") && (customer.Password == "No") && (customer.Email)))
                    {
                        DCNoPasswordEmail();
                    }                  
                    setupValidation();                         
                }
                 return false; 
            });        
        }
        //if logging in with ID Number & Password (assumes Password and Email exist)
        if (butCaption == "Login")
        { 
            //debugger;
            busy(true);
            var parms1 = {MAID: $("div.loginForm input[@name=MAID]").val().trim(), Password: isBlank($("div.loginForm input[@name=Password]").val().trim(),"NULL"), MktAssocNum : getMktAssocNum(), cache:newid()};
            $.getJSON("includes/sportron/loginWork.aspx",parms1,function(customer)
            {
                //debugger;
                busy(false);
                if(customer.status == "failed")
                {
                    alert("Invalid Identity Number and Password combination! \n\nSorry we cannot log you in. Please try again!"); 
                    $("div.loginForm input[@name=MAID]")[0].readOnly = false;
                    $("div.loginForm input[@name=Password]")[0].focus();                 
                }
                else
                {
                    $("div.loginForm input[@name=MAID]")[0].readOnly = true;
                    $("div.registerImage").hide();
                    _customer = customer;
                    if(customer.MAType == "M")
                    {
                        if(customer.StatusCode == "ACT" || customer.StatusCode == "GRC" || customer.StatusCode == "RTO")
                        {
                            if(customer.UserID == "0")
                            {
                                $("div#wizardHolder").load("/includes/sportron/updateCustomer.aspx",{IDNum : _customer.IDNum, cache:newid()},function(){
                                setupRS3Selects();
                                setupWizard();
                                setupValidation(); 
                                //ensure that MAType is set to "M"
                                $("input#MAType").val("M");
                                $("div.login").slideUp("normal");
                                }); 
                            }
                            else
                            {
                                if(customer.MASiteID == "0")
                                {
                                    setupURLs(customer);
                                    $("div.loginMysite").slideDown("normal");
                                    $("div.partnerOrders").slideDown("normal");
                                    $("div.LoggedIn").show();
                                    if(isCheckOut())
                                    {
                                        $("div.checkOut").slideDown("normal"); 
                                    }  
                                    $("div.LoggedOut").hide();                             
                                }
                                else
                                {
//                                    var curURL = window.location.href.Replace("https","http");
//                                    setTimeout(function(){location.href=curURL},2000);
                                    if(customer.MAStatusID == "1")
                                    {
                                        $("div.manageMysite").slideDown("normal");
                                        $("div.LoggedIn").show();
                                        if(isCheckOut())
                                        {
                                            $("div.checkOut").slideDown("normal"); 
                                        }                                    
                                        $("div.LoggedOut").hide(); 
                                    }
                                    if(customer.MAStatusID == "4" || customer.MAStatusID == "5")
                                    {
                                        setupURLs(customer);
                                        $("div.loginMysite").slideDown("normal");
                                        $("div.loginMysite button").html("Re-Activate");
                                        $("div.partnerOrders").slideDown("normal");
                                        if(isCheckOut())
                                        {
                                            $("div.checkOut").slideDown("normal"); 
                                        }
                                        $("div.LoggedIn").show();
                                        $("div.LoggedOut").hide();  
                                    }                                                            
                                } 

                            }
                        }
                        else
                        {
                            alert("Unfortunately according to our records your Sportron account is no longer active, please contact Customer Services on (011) 317 8500.");
                            doLogout();
                        }                        
                    }
                    else
                    {
                        if(customer.Password == "Yes")
                        {                           
                            if(isCheckOut())
                            {
                                $("div.checkOut, div.loginSignup").slideDown("normal"); 
                            }
                            else
                            {
                                $("div.loginSignup").slideDown("normal");  
                            }  
                            $("div.LoggedIn").show();
                            $("div.LoggedOut").hide();                         
                        }
                        else
                        {
                            $("div.loginRegister, div.loginSignup").slideDown("normal"); 
                            $("div.LoggedIn").show();
                            $("div.LoggedOut").hide(); 
                        }
                    }
                    $("div#menuHolder1 span#login").text(customer.FirstName.trim() + " " + customer.SurName.trim());
                    $("span#subnavInfo").text(customer.MAType == "M" ? "Marketing Associate" : "Direct Customer");              
                }
            });
        }
    }
    $("body")[0].style.height = "auto";
}

function busy(state)
{
    if(state)
    {
        $("div#idLabel").html("<img alt=\"Processing please wait ...\" style=\"margin-top:6px; width:190px;\" src=\"/images/loadingAnimation.gif\" />");
    }
    else
    {
        $("div#idLabel").html("Identity Number :");
    }
}

function getMktAssocNum()
{
    var MANum = $("input[@name=MANum]").val();
    var MANames = $("select[@name=MANames]").val();
    if(MANum == "" ||MANum == null)MANum = MANames;
    if(MANum == "" ||MANum == null)MANum = "0";
    return MANum;
}

function isBlank(val, replaceVal)
{
    if(val.toString().length == 0){
        val = replaceVal;}
    return val;
}

function MAPasswordEmail()
{
    $("div.registerImage").hide();
    $("div.loginLanding").hide();
    $("div.LoginInto").show();
    $("div.RegisterInto").hide();     
    $("div.loginForm input[@name=MAID]")[0].readOnly = true;
    $("div.LoggedOut button").html("Login"); 
    $("div.validator").hide();
    $("div#registerImage img").attr("src","../images/registerimage_02.jpg");    
    $("div#registerImage").fadeIn("slow");
    $("div.loginNow div.registerImage").hide();    
    $("div.loginInputs").css("height","160px");
    $("div#valPassword").show();
    $("div.loginForm input[@name=Password]")[0].focus();
}

function MANoPasswordEmail()
{
    $("div.registerImage").hide();
    $("div.loginLanding").hide();
    $("div.LoginInto").show();
    $("div.RegisterInto").hide();     
    $("div.loginForm input[@name=MAID]")[0].readOnly = true;
    $("div.LoggedOut button").html("Login"); 
    $("div.validator").hide();
    $("div.loginInputs").css("height","160px");
    $("div#registerImage img").attr("src","../images/registerimage_02.jpg");
    $("div#registerImage").fadeIn("slow");
    $("div.loginNow div.registerImage").hide();      
    $("div#valMANumber").show();
    $("div.loginForm input[@name=MANum]")[0].focus();
}

function DCPasswordEmail()
{
    $("div.registerImage").hide();
    $("div.loginLanding").hide();
    $("div.LoginInto").show();
    $("div.RegisterInto").hide();     
    $("div.loginForm input[@name=MAID]")[0].readOnly = true;
    $("div.LoggedOut button").html("Login"); 
    $("div.validator").hide();
    $("div.loginInputs").css("height","160px");
    $("div#registerImage img").attr("src","../images/registerimage_03.jpg");
    $("div#registerImage").fadeIn("slow");
    $("div.loginNow div.registerImage").hide();      
    $("div#valPassword").show();
    $("div.loginForm input[@name=Password]")[0].focus();
}

function DCNoPasswordEmail()
{
    $("div.registerImage").hide();
    $("div.loginLanding").hide();
    $("div.LoginInto").show();
    $("div.RegisterInto").hide();     
    $("div.loginForm input[@name=MAID]")[0].readOnly = true;
    $("div.LoggedOut button").html("Login"); 
    setRS3Select($("select#xml_MANames"), $("input#MAID")); 
    $("div.validator").hide();
    $("div.loginInputs").css("height","160px");
    $("div#registerImage img").attr("src","../images/registerimage_03.jpg");
    $("div#registerImage").fadeIn("slow");
    $("div.loginNow div.registerImage").hide();      
    $("div#valNames").show();
}

function setupURLs(customer)
{
    $("p#mysiteURLs").load("/includes/sportron/siteURLs.aspx",{cache:newid()});
}

function doLogout()
{
    busy(true);
    var parms = {logout: "true", cache:newid()};
    _customer = {};
    $.getJSON("includes/sportron/loginWork.aspx",parms,function(resp)
    {
        busy(false);
        if(resp.status == "loggedOut")
        {
            //alert("You have been logged out!");                  
            $("div#menuHolder1 span#login").html("Login");
            $("span#subnavInfo").html("");
            $("div.LoggedOut button").html("Start"); 
            $("div.loginForm input[@name=Password]").val("");
            $("div.loginForm input[@name=Password]").attr("title","MA Numbers cannot be blank");
            $("div.loginForm input[@name=Password]").removeAttr("validationRule").removeAttr("validationMessage");
            $("div.loginForm input[@name=MAID]")[0].readOnly = false;
            $("div.loginForm input[@name=MAID]").val("");
            $("div.actions").hide();
            $("div.LoggedIn").hide();
            $("div.validator").hide();
            $("div.LoggedOut").show();
            $("div.loginInputs").css("height","80px");
            $("div#registerImage").fadeIn("slow"); 
            setupValidation(); 
        }       
    });
    $("body")[0].style.height = "auto";
}

function form2JSON(frm)
{
    var parms = new Object();
    for(var i=0; i < frm.elements.length; i++)
    {
        if(frm.elements[i].name)
        {
            if ($(frm.elements[i]).is(".null") && frm.elements[i].value == "")
            {
                parms[frm.elements[i].name] = "NULL";
            }
            else
            {
                parms[frm.elements[i].name] = frm.elements[i].value.trim();
            } 
        }
    }
    return parms;
}

function setupOrder(MktAssocNum, MktAssocType)
{
    checkDelivery();
    var mon = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var prodInfo = getProducts4Order();
    $("input#customerid").val(MktAssocNum);
    $("input#paymentauthid").val("0");
    $("input#orderitems").val(prodInfo[0]);
    $("input#deliveryid").val("0");
    var d = new Date();
    $("input#MDate").val(d.getDate() + " " + mon[d.getMonth()] + " " + d.getFullYear());
    $("input#Amount").val(prodInfo[1]);
    $("input#Vat").val($("span.mod_cart_vat_value").text());
    var deliveryExVAT = (parseFloat($("span.mod_cart_del_value").text())-(parseFloat($("span.mod_cart_del_value").text())* vatcalc));
    deliveryExVAT = deliveryExVAT.toFixed(2);    
    $("input#Ship").val(deliveryExVAT);
    $("input#CoupRed").val("0");
    $("input#CoupPoints").val("0");
    $("input#CoupRP").val("0");
    $("input#NewBV").val("0");
    $("input#BranchCD").val("JHB");
    $("input#prmReleaseType").val("I");
    $("input#ReceiveAmt").val($("span.mod_cart_total_incdel").text());
    $("input#MktAssocType").val(MktAssocType);
    $("input#OrdHeadKey").val("0");
    $("input#WebOrderID").val("0"); 
}

function getProducts4Order()
{
    var oi = [];
    var price = 0;
    var out = [];
    var exvatexshippingtotal = 0;
    $("div.product").each(function(){
        price = (parseFloat($(this).find("div.price span").text())-((parseFloat($(this).find("div.price span").text())* vatcalc)).toFixed(2));
        price = price.toFixed(2);
        var qty = parseInt($(this).find("div.qty span.value").text(),10);
        exvatexshippingtotal += (parseFloat(price) * qty);
        oi.push("'" + $(this).find("span.prdCode").text() + "'," + $(this).find("div.qty span.value").text() + "," + price);       
    });
    out.push(oi.join(";"));
    out.push(exvatexshippingtotal.toFixed(2));
    return out;
}
var actionMAType = "M";
function pageload()
{   
    //Call ShowDetails Function
    showDetails();
    var _id
    //Sign-Up Button Click
    $("div.loginSignup button").click(function(){
        $("div#registerSignUp").hide();
        actionMAType = "M";
        _id = _customer.IDNum || $("input#MAID").val();
        $("div#wizardHolder").load("/includes/sportron/signup.aspx",{IDNum : _id, cache:newid()},function(){
            setupRS3Selects();
            setupWizard();
            setupValidation(); 
            ppp = $("div.signUp").attr("id");
            if(ppp == undefined)ppp=0;
            $("div.login").fadeOut("fast", function(){
                $.get("/includes/sportron/arf.htm", function(data){
                    if($("div#prdARF").length == 0)
                    {
                        $("div#cart").append(data);
                    }
                    $("div.plus, div.minus, div.delete").hide();
                    reLoadCart();
                    if(recalc() == false)
                    {
                        linkTo("/products");
                    }                    
                });                
            });
        });    
    }).css("cursor","pointer");
    
    //Register Button Click
    $("div.loginRegister button").click(function(){
        $("div#registerSignUp").hide();
        actionMAType = "D";
        _id = _customer.IDNum || $("input#MAID").val();
        $("div#wizardHolder").load("/includes/sportron/register.aspx",{IDNum : _id, cache:newid()},function(){
            setupRS3Selects();
            setupWizard();
            setupValidation();
            ppp = undefined; 
            $("div.login").slideUp("normal", function(){
                reLoadCart();
            });
        });            
    }).css("cursor","pointer");  
    
    //Partner Ordering
    $("button#managePartners").click(function(){
        $("div.partnerOrders").hide();
        linkTo("/rscms/admin");
    });
    
    //Manage, Activate, De-Activate, Re-Activate Button Click
    $("div.loginMysite button, div.manageMysite button").click(function(){
        switch($(this).text())
        {
            case "Manage":
                linkTo("/rscms/admin");
                break;              
            default: 
                var activeHost = $("div.loginMysite input[@type=radio][@checked]").val() == null ? "" : $("div.loginMysite input[@type=radio][@checked]").val();
                if(activeHost == "")
                {
                    alert("You must select a URL to activate first");
                    break;
                }              
                var siteParms = {UserID: _customer.UserID, ActiveHost : activeHost, RequestedHost :  $("input[@name=customURL]").val(), Action: $(this).text(), cache:newid() };           
                $.getJSON("/includes/sportron/setMASite.aspx",siteParms,function(MASite)
                {
                    _customer.MASiteID = MASite.MASiteID;
                    _customer.MAStatusID = MASite.MAStatusID;
                    switch(MASite.Action)
                    {
                        case "Activate" :
                            $("div.loginMysite").slideUp("normal");
                            $("div.manageMysite").slideDown("normal");
                            //location.href = "http://" + activeHost + ".sportron.co.za";
                            break;
                        case "De-Activate" :
                            $("div.manageMysite").slideUp("normal");
                            $("div.loginMysite").slideDown("normal");
                             setupURLs(_customer);                        
                            $("div.loginMysite button").html("Re-Activate");
                            break;
                        case "Re-Activate" :
                            $("div.loginMysite").slideUp("normal");
                            $("div.manageMysite").slideDown("normal"); 
                            $("button#actionMysite").html("De-Activate");
                            break;                                                        
                    }               
                });
                break;
        }  
    }).css("cursor","pointer"); 
            
    //Password Reminder
    $("a#remindMe").click(function(){ 
        var ID = $("input#MAID").val();                      
        var parms = {IDNum: ID, cache:newid() };           
        $.getJSON("/includes/sportron/remindMe.aspx",parms,function(resp)
        {
            alert(resp.msg);            
        });
    }).css("cursor","pointer"); 
        
    //Checkout Button Click
    $("div.checkOut button").click(function(){
        if(_customer.MAType == "M"){
            if(confirm("Please Note - We see that you are a Sportron Partner!\n\nPartner Prices are only available via Partner Ordering or MySite Admin!\n\nContinue with this Direct Price purchase?")){
                $("div.login").fadeOut("normal", function(){});
                $("div#wizardHolder").load("/includes/sportron/checkOut.aspx",{IDNum : _customer.IDNum, cache:newid()},function(){
                    setupRS3Selects();
                    setupWizard();
                    setupValidation();
                    ppp = undefined; 
                    reLoadCart();
                });
            }
        }
        else
        {
            $("div.login").fadeOut("normal", function(){
            });
            $("div#wizardHolder").load("/includes/sportron/checkOut.aspx",{IDNum : _customer.IDNum, cache:newid()},function(){
                setupRS3Selects();
                setupWizard();
                setupValidation();
                ppp = undefined; 
                reLoadCart();
            });        
        }            
    }).css("cursor","pointer");       

    //System Guide
    $("div#mod_SG img").css("opacity", 0.7);
    $("#mod_SG_icon_desc").css("opacity", 0.7);
    $("div#mod_SG img").hover(
        function(){
            $(this).css("opacity", 1.0);
            $("#mod_SG_icon_desc").css("opacity", 1.0);
            $("#mod_SG_icon_desc").css("backgroundColor", $(this).css("backgroundColor"));
            $("#mod_SG_icon_desc").text($(this).attr("alt"));
        },
        function(){
            $(this).css("opacity",  0.7);
            $("#mod_SG_icon_desc").css("opacity",  0.7);
        }
    );
    $("div#mod_SG img").click(function()
        {
            //$.blockUI("<img alt=\"Processing please wait ...\" style=\"margin-top:6px; width:190px;\" src=\"/images/loadingAnimation.gif\" />"); 
            $("body").css("cursor", "progress");               
            $("div.right_content, div.left_content").load("/includes/sportron/productSummaryLive.aspx",{ClassName:$(this).attr("id"),searchBy:$(this).attr("alt"),displayType:"summary", cache:newid()}, function(){
                //$.unblockUI();
                showDetails();
                //$(this).fadeIn("fast");
                window.scrollTo(0,0);
                $("body").css("cursor", "default"); 
            });    
        }
    );
    
    //Collapse Module
    $("div.mod_header_icon_up").css("cursor","pointer").toggle(
        function(){
            $(this).removeClass("mod_header_icon_up").addClass("mod_header_icon_down").parent().next().slideUp("normal")
        },
        function(){
            $(this).removeClass("mod_header_icon_down").addClass("mod_header_icon_up").parent().next().slideDown("normal")
        }        
    );
      
    //Product Tree
    $("#prodtree").Treeview({speed:"normal"});
    $("#prodtree li span.category").css("cursor","pointer").click(function(){
        $(this)
        .prev(".hitarea")
        .trigger("click");
        //$.blockUI("<img alt=\"Processing please wait ...\" style=\"margin-top:6px; width:190px;\" src=\"/images/loadingAnimation.gif\" />");        
        $("body").css("cursor", "progress");
        $("div.right_content, div.left_content").load("/includes/sportron/productSummaryLive.aspx",{ProductCategoryID:$(this).attr("id").replace("p","").replace("c","").replace("a","").replace("b",""),ParentID:$(this).attr("id").replace("p","").replace("c","").replace("a","").replace("b",""),searchBy:$(this).text(),displayType:"summary", cache:newid()}, function(){
            //$.unblockUI();
            showDetails();
            //$(this).fadeIn("fast");
            $("body").css("cursor", "default"); 
            window.scrollTo(0,0);
         });
    });
    $("#prodtree ul li span.product").css("cursor","pointer").click(function(){
        //$.blockUI("<img alt=\"Processing please wait ...\" style=\"margin-top:6px; width:190px;\" src=\"/images/loadingAnimation.gif\" />");            
        $("body").css("cursor", "progress");
        $("div.right_content, div.left_content").load("/includes/sportron/productDetailLive.aspx",{ProductID:$(this).attr("id").replace("p","").replace("c","").replace("a","").replace("b",""),searchBy:$(this).text(),displayType:"detail", cache:newid()}, function(){
            //$.unblockUI();
            showDetails();
            //$(this).fadeIn("fast");
            $("body").css("cursor", "default");
            window.scrollTo(0,0);
         });
    }); 
    
    //Ailment Tree
    $("#ailtree").Treeview({speed:"normal"});
    $("#ailtree li span.category").css("cursor","pointer").click(function(){
        $(this)
        .prev(".hitarea")
        .trigger("click");
    });
    $("#ailtree ul li span.product").css("cursor","pointer").click(function(){
        //$.blockUI("<img alt=\"Processing please wait ...\" style=\"margin-top:6px; width:190px;\" src=\"/images/loadingAnimation.gif\" />");            
        $("body").css("cursor", "progress");
        $("div.right_content, div.left_content").load("/includes/sportron/productSummaryLive.aspx",{AilmentID:$(this).attr("id").replace("p","").replace("c","").replace("a","").replace("b",""),searchBy:$(this).text(),displayType:"summary", cache:newid()}, function(){
            //$.unblockUI();
            showDetails();
            //$(this).fadeIn("fast");
            $("body").css("cursor", "default");
            window.scrollTo(0,0);
        });
    }); 
    
    //Module Config
    if(moduleConfig)
    {
        var modules = $("div.modules");
        for(var i=0; i < modules.length; i++)
        {
            if(moduleConfig[modules[i].id])
            {
                if(!moduleConfig[modules[i].id].visible)
                    $("div#" + modules[i].id).hide();
            }
            if(moduleConfig[modules[i].id])
            {
                if(!moduleConfig[modules[i].id].open)
                    $("div#" + modules[i].id).find("div.mod_header_icon_up").trigger("click");
            }
        }
    } 
    
    //Fade In Sidebar
    $("div#sidebar").fadeIn("fast"); 
    
    //Proceed to Login
    $("span#login").css("cursor", "pointer").click(function()
        {
            linkTo("/Login");
        }
    ); 
    
    //Other Links to Login
     $("div.bopSignup button, div.bopRegister button").css("cursor", "pointer").click(function()
        {
            linkTo("/Login");
            return false;
        }
    );    
    
    //Proceed to checkout
    $("div.mod_cart_action").css("cursor", "pointer").click(function()
        {
            if(parseFloat($("span.mod_cart_total_incdel").text()) == 0)
            {
               alert("Please add a few items to the basket first... \n\nRedirecting you to the Products page!");
               linkTo("/products");
            }
            else
            {
                linkTo("/main.aspx?ID=726&checkOut=true");
            }
        }
    ); 
        
  
    //Setup FAQ
    $("div.answer").hide();
    $("h2.question").toggle(function(){
        $(this).next("div.answer").slideDown("normal");
    }, function(){
        $(this).next("div.answer").slideUp("normal");
    }).css("cursor","pointer");
    
   
   //Call Wizard Setup
    setupWizard();    
}

function inList(url)
{
    var aList = ["login","726","rscms/admin"];//"test","testing"
    url = url.toLowerCase();
    for(var i=0; i < aList.length; i++)
    {
        if(url.indexOf(aList[i]) > -1)
        {
            return true;
        }   
    }
    return false;
} 
  
function getLink(url)
{
    if(inList(url))
    {
        return "https://" + location.hostname + url;
    }
    else
    {
        return "http://" + location.hostname + url;
    }  
}

function linkTo(url)
{
    if(inList(url))
    {
        location.href = "https://" + location.hostname + url;
    }
    else
    {
        location.href = "http://" + location.hostname + url;
    }  
}  

//Function: showDetails <  Products via Tabs
function showDetails()
{
    $(".showCategory").css("cursor","pointer").click(function(){
        //$.blockUI("<img alt=\"Processing please wait ...\" style=\"margin-top:6px; width:190px;\" src=\"/images/loadingAnimation.gif\" />");
        $("body").css("cursor", "progress");
        $("div.right_content, div.left_content").load("/includes/sportron/productSummaryLive.aspx",{ProductCategoryID:$(this).attr("id").replace("p","").replace("c","").replace("a","").replace("b",""),ParentID:$(this).attr("id").replace("p","").replace("c","").replace("a","").replace("b",""),searchBy:$(this).parent().parent().find("div.title").text(),displayType:"summary", cache:newid()}, function(){
        //$.unblockUI();
        showDetails();
        //$(this).fadeIn("fast");
        $("body").css("cursor", "default");
        $("button, div.rsButton").hover(function(){
           $(this).css("opacity",1.0);
         },function(){
           $(this).css("opacity",0.7);
         });         
        });    
    }); 
    //Product Tabs
    $("div.tabBut img").click(
        function(){
            $(this).css("borderBottom","1px solid #fff");
            $(this).siblings().css("borderBottom","1px solid #E5E5E5");
            $(this).parent().next().find("div").hide();
            $(this).parent().next().find("div." + $(this).attr("class")).show();
            return false;
        }
    );
    $("div.tabBut").each(function()
        {
            $(this).find("img").css("cursor","pointer").gt(0).css("borderBottom","1px solid #E5E5E5");
        }
    );
    $("div.tabs").each(function()
    {
        if($(this).find("div.description").text().length < 2)
        {
           $(this).find("div.description").text("No Information Available"); 
        }
        if($(this).find("div.benefits").text().length < 2)
        {
           $(this).find("div.benefits").text("No Information Available"); 
        }
        if($(this).find("div.ingredients").text().length < 2)
        {
           $(this).find("div.ingredients").text("No Information Available"); 
        }
        if($(this).find("div.ailments").text().length < 2)
        {
           $(this).find("div.ailments").text("No Information Available"); 
        }                        
    });
    loadCart(); 
}

if ( window.Node )
Node.prototype.removeNode = function( removeChildren )
{
	var self = this;
	if ( Boolean( removeChildren ) )
	{
		return this.parentNode.removeChild( self );
	}
	else
	{
		var range = document.createRange();
		range.selectNodeContents( self );
		return this.parentNode.replaceChild( range.extractContents(), self );
	}
}

function cleanMySiteContent(jObj)
{
    
    jObj.find(".MsoNormal").each(function(){
        $(this).removeClass("MsoNormal");
    });
    jObj.find("font").each(function(){
        this.removeNode(false);
    });
}
function rgen6()
	{
	var drn = "";
	var rand_no;
	for (x = 0; x < 20; x++)
		{
		rand_no = Math.floor((3)*Math.random()) + 1;
		drn = drn + rand_no + "\n";
		}
	alert(drn);
	}

