﻿/****************************************** Global Variables ******************************************/
var httpProtocol = document.location.protocol == 'https:' ? 'https://' : 'http://';

/****************************************** Global Functions ******************************************/

//Resize iFrame Height
function resizeIframeHeight(frameName, height) {
	var myFrame = $('iframe.' + frameName + 'Iframe');
	if (myFrame.length) {
		myFrame.css({ height: height });
	}
}

//Get Cookies Array
function getCookiesArray(myCookie) {
	var cookies = {};
	if (myCookie != null && myCookie != '') {
		var split = myCookie.split('&');
		for (var i = 0; i < split.length; i++) {
			var name_value = split[i].split("=");
			name_value[0] = name_value[0].replace(/^ /, '');
			cookies[decodeURIComponent(name_value[0])] = decodeURIComponent(name_value[1]);
		}
	}
	return cookies;
}

//Validate User Login
function validateLogin() {
	var isLogin = false;
	var userCookies = getCookiesArray($.cookie('User'));
	var customerCookies = getCookiesArray($.cookie('sfCustomer'));
	//var smallWidth = '<li class="smallWidth"><img src="/images/top_seperator.jpg" width="2" height="20" alt="seperator" border="0" /></li>';
	if (userCookies['CustID'] != undefined && userCookies['CustID'] != '') {
		//var custName = customerCookies['custName'].split("+")[0].length == 0 ? "My Account" : customerCookies['custName'].split("+")[0] + "'s Account";
		var custName = "My Account";

		var accountNameLI = '<a href="/ssl/myaccount/MyAccountHome.asp">' + custName + '</a>';
		//$('.accountNameLI').html(accountNameLI);
		$('.headerLoginSelectedTab').hide();
		//$('.accountNameLI').css('width', '');
		//$('.accountNameLI').css({"width": "80px", "text-align": "center" });
		//if ($.trim($('#liveChatHeaderDiv').html()) == '') {
		//    $('.restaurantEquipmentLI').width('611px');
		//} else {
		//    $('.restaurantEquipmentLI').width('531px');
		//}
		$('.signOutLI').show();
		$('.signInLI').hide();
	}
	else {
	    //$('.accountNameLI').html('&nbsp;');
	    //$('.accountNameLI').css({ "width": "0px !important", "text-align": "right !important" });
	    // If live chat is not enabled and they've signed out make the width larger
	    //if ($.trim($('#liveChatHeaderDiv').html()) == '') {
	    //    if ($.trim($('.liveChatHeaderLI').text()) == "") {
	    //        $('.liveChatHeaderLI').css("display", "none");
	    //        $('.headerContactUsLI').css("border-right", "1px #ccc solid");
	    //        $('.restaurantEquipmentLI').width('681px');
	    //    } else $('.restaurantEquipmentLI').width('611px');
	    //} else {
	    //    $('.restaurantEquipmentLI').width('611px');
	    //}
	    $('.signOutLI').hide();
		$('.signInLI').show();
	}
}

//Load SignIn and SignOut Li
function callLogIn() {
    var signInLi = '<li class="signInLI borderGrayLeft"><a href="#" class="linkToShowHeaderLoginBox">Sign&nbsp;In</a></li>';
	var signOutLi = '<li class="signOutLI borderGrayLeft"><a href="/ssl/myaccount/MyAccountHome.asp" class="linkToMyAccount">My&nbsp;Account</a></li>';
	$('.headerContactUsLI').after(signInLi + signOutLi);
}

//Redirect Parent Window
function openParentUrl(url) {
	parent.window.location = url;
}

//Shipping Policy Popup
function shippingPolicyPopup(target) {
	var url = (target.length > 0) ? "/shippingpolicy_popup.asp#" + target : "/shippingpolicy_popup.asp";
	window.open(url, "ShippingPolicy", "height=450,width=600,scrollbars=yes");
}

function addToShoppingCart(productCode, quantityClientID, isCloseOut, closeoutQuantity, quantityOnHand, isInFeed) {
    var result = true;
    var message = "";
    var quantityTextboxAvailable = quantityClientID.length > 0 ? $('input#' + quantityClientID).length : 0;
    var Quantity;
    var CloseoutQuantity = Number(closeoutQuantity);
    var QuantityOnHand = Number(quantityOnHand);

    if (quantityTextboxAvailable > 0) {
        if (isNaN($('input#' + quantityClientID).val())) {
            message += "Quantity must be a whole number\n";
            result = false;
        }
        else {
            Quantity = parseInt($('input#' + quantityClientID).val());
            $('input#' + quantityClientID).val(Quantity);
        }
    }
    else {
        Quantity = 1;
    }

    if (isCloseOut == 1) {
        if ((CloseoutQuantity - Quantity) < 0) {
            if (CloseoutQuantity < 0) {
                CloseoutQuantity = 0;
            }
            message += " Desired Quantity is greater than what is available. Your quantity has been adjusted accordingly. Quantity Available: " + CloseoutQuantity;
            $('input#' + quantityClientID).val(CloseoutQuantity);
            result = false;
        }
    }
    if (!result) {
        alert(message);
    }
    else {
        var url = "/inc_shoppingcart.asp";
        $.post(url,
        {
            prodID: productCode,
            Quantity: Quantity,
            IsCloseoutItem: isCloseOut,
            Inventory: CloseoutQuantity,
            QtyOnHand: QuantityOnHand,
            IsFeedVendor: isInFeed,
            cartAction: "ADD"
        },
        function (data) {
            $(location).attr('href', "/order.asp?sc=add");
        });
    }
}

var cookieList = function (cookieName) {
    var cookie = $.cookie(cookieName);
    var items = cookie ? cookie.split(/,/) : new Array();
    return {
        "add": function (val) {
            //Add to the items.
            items.push(val);
            //Save the items to a cookie.
            $.cookie(cookieName, items, { domain: '.' + document.domain, expires: 30 });
        },
        "clear": function () {
            //clear the cookie.
            $.cookie(cookieName, null);
        },
        "items": function () {
            //Get all the items.
            return items;
        }
    }
}
