﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

var REMOTE_SERVER = "https://secure.intelligentdirect.com/members/";
//var REMOTE_SERVER = "http://localhost:50790/";
//var REMOTE_SERVER = "http://betasecure.intelligentdirect.com/members/";
var REMOTE_LOGIN = REMOTE_SERVER + "mapsales/account/Login.aspx";
var SAVE_QUOTE_LOGIN = REMOTE_SERVER + "mapsales/account/savequote.aspx";
var BENEFITS_LOGIN = REMOTE_SERVER + "mapsales/account/benefits.aspx";
var MY_ACCOUNT = REMOTE_SERVER + "mapsales/MyAccount.aspx";
var SAVED_QUOTES = REMOTE_SERVER + "mapsales/SavedQuotes.aspx";

/***** Javascript Guid *****/

function Guid(options) {
    this.options = options || {};
    this.chars = this.options.chars || Guid.constants.alphanumerics;
    this.epoch = this.options.epoch || Guid.constants.epoch1970;
    this.counterSequenceLength = this.options.counterSequenceLength || 1;
    this.randomSequenceLength = this.options.randomSequenceLength || 2;
}

Guid.prototype.generate = function() {
    var now = (new Date()).getTime() - this.epoch;
    var guid = this.baseN(now);

    this.counterSeq = (now == this.lastTimestampUsed ? this.counterSeq + 1 : 1);
    guid += this.counterSeq;

    for (var i = 0; i < this.randomSequenceLength; i++) {
        guid += this.chars.charAt(Math.floor(Math.random() * this.chars.length));
    }

    this.lastTimestampUsed = now;

    return guid;
}

Guid.prototype.baseN = function(val) {
    if (val == 0) return "";
    var rightMost = val % this.chars.length;
    var rightMostChar = this.chars.charAt(rightMost);
    var remaining = Math.floor(val / this.chars.length);
    return this.baseN(remaining) + rightMostChar;
}

Guid.constants = {};
Guid.constants.numbers = "0123456789";
Guid.constants.alphas = "abcdefghijklmnopqrstuvwxyz";
Guid.constants.lowerAlphanumerics = "0123456789abcdefghijklmnopqrstuvwxyz";
Guid.constants.alphanumerics = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// http://tools.ietf.org/html/rfc1924
Guid.constants.base85 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&()*+-;<=>?@^_`{|}~";

Guid.constants.epoch1970 = (new Date(0));
Guid.constants.epoch = function(year) { return (new Date("Jan 1 " + year)).getTime(); }


/***** Cookies *****/

function SetCookie(sName, sValue, numMinutesToExpire) {
    date = new Date();
    date = new Date(date.getTime() + numMinutesToExpire * 60 * 1000);
    document.cookie = sName + "=" + escape(sValue) + "; expires=" + date.toGMTString();
}

function GetCookie(sName) {
    var aCookie = document.cookie.split("; ");
    for (var i = 0; i < aCookie.length; i++) {
        var aCrumb = aCookie[i].split("=");
        if (sName == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    // a cookie with the requested name does not exist
    return null;
}

function DelCookie(sName, sValue) {
    document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}


/***** jQuery Extentions *****/

jQuery.fn.defaultButton = function(buttonID) {
    /// <summary>
    /// Sets the default button to be clicked when the user hits the 'enter' button.
    /// </summary>
    /// <param name="buttonID" type="String">The button ID to be clicked.</param>
    
    this.keypress(
        function(e) {
            if(e.keyCode == 13) {
                $("#" + buttonID).click();
                return false;
            }
        }
    );
};

jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});
	
	return this;
};

function CheckCartForItems()
{
    /// <summary>
    /// Checks the cart for the number of items. Updates the 'View Cart' link in the header if there are any.
    /// </summary>

    MemberService.GetCartItemCount(
        function(itemCount)
        {
            $("#headerViewCart").html("View Cart (" + itemCount + ")");

            SetCartItemCookieInMembersArea();
        }
    );
}

function SetCartItemCookieInMembersArea()
{
    /// <summary>
    /// Makes a request to the members area that will set the cart quantity there in a cookie.
    /// This will ensure that if the user decides to goto the members area the number of items will display correctly.
    /// </summary>

    if($("#savecartupdate").length > 0)
    {
        var quantity = "0";

        if(GetCookie("CartQuantity") != null)
        {
            quantity = GetCookie("CartQuantity");
        }

        $("#savecartupdate").attr('src', REMOTE_SERVER + 'connect.aspx?request=savecartquantity&site=mapsales&quantity=' + quantity + '&rand=' + Math.random());
    }
}

function SetLoginUrl()
{
    /// <summary>
    /// Sets the login url for the members area.
    /// </summary>

    $("#memberlogin").attr('href', REMOTE_LOGIN);
    $("#membershipLinks > a").attr('href', MY_ACCOUNT);
}

function SetLeftNavMemberLink()
{
    if($("#becomeamemberlink").length > 0)
    {
        $("#becomeamemberlink").wrap('<a href="' + BENEFITS_LOGIN + '"></a>');
    }
}

$(function()
{
    CheckCartForItems();

    SetLoginUrl();

    SetLeftNavMemberLink();
});
