﻿var previousCount;

function AddToCart(ProductId, Count){
    var newCount = '0' + Count.value;
    if (isNaN(newCount))
    {
        alert(msgErrorQuantity);
        Count.value = '';
        Count.focus();
        return;
    }
    else
    {
        newCount = Number.parseInvariant(newCount);
        if (newCount < 0)
        {
            alert(msgErrorQuantity);
            Count.value = '';
            Count.focus();
            return;
        }
    }
    WebClient.UpdateCart(ProductId, newCount, Result, wsErrores);
}
function RemoveFromCart(TextBox)
{
    var textBox = $get(TextBox);
    textBox.value = '';
    textBox.onchange();
}
function Result(returnedResult, eventArgs){
    var actual;
    if ($get(lblCount).innerText != undefined)
    {
        actual = $get(lblCount).innerText;
        $get(lblCount).innerText = returnedResult[0];
        $get(lblTotal).innerText = returnedResult[1];
    } else {
        actual = $get(lblCount).textContent;
        $get(lblCount).textContent = returnedResult[0];
        $get(lblTotal).textContent = returnedResult[1];
    }
    if (actual == 0 || returnedResult[0] == 0)
    {
        if (returnedResult[0] == 0)
            $get(btnViewCart).src = "Images/TopEmptyCart.gif";
        else
            $get(btnViewCart).src = "Images/TopFullCart.gif";
    }
}
function wsErrores(error){
    alert(msgWSError);
}
function CheckCursorKeys(evt, ProductId, obj)
{
    var newCount = '0' + obj.value;
    if (isNaN(newCount))
        newCount = 0;
    else
        newCount = Number.parseInvariant(newCount);
        
    var keyCode = document.layers?evt.which:evt.keyCode;
    switch(keyCode)
    {
        case 40:
            if (newCount > 0)
                setTimeout("SetObjectValue('" + obj.id + "'," + (newCount-1) + ");", 1);
            break;
        case 38:
            if (newCount < 999)
                setTimeout("SetObjectValue('" + obj.id + "'," + (newCount+1) + ");", 1);
            break;
        case 13:
            MoveNextProduct(obj);
            break;
        case 9:
            if (evt.shiftKey)
                MovePreviousProduct(obj);
            else
                MoveNextProduct(obj);
            break;
        default:
            return true;
    }
    return false;
}
function SetObjectValue(ObjectId, Value) {
    $get(ObjectId).value = Value;
}
function ItemFocus(obj)
{
    previousCount = obj.value;
    obj.parentNode.parentNode.className = 'rowFocus';
    var nextTR=obj.parentNode.parentNode.nextSibling;
    while(nextTR.nodeType == 3)
        nextTR = nextTR.nextSibling;
    nextTR.className = 'rowFocus';
}
function ItemBlur(obj)
{
    if (previousCount != obj.value);
        obj.onchange();
    obj.parentNode.parentNode.className = 'rowNoFocus';
    var nextTR=obj.parentNode.parentNode.nextSibling;
    while(nextTR.nodeType == 3)
        nextTR = nextTR.nextSibling;
    nextTR.className = 'rowNoFocus';
}
function MoveNextProduct(obj)
{
    var count = 3;
    var nextTR=obj.parentNode.parentNode.nextSibling;
    try
    {
        while (count-- > 0)
            do
                nextTR = nextTR.nextSibling;
            while(nextTR.nodeType == 3);
        nextTR.getElementsByTagName("INPUT")[2].focus();
    } catch(ex) {
        nextTR = obj.parentNode.parentNode.parentNode.firstChild;
        nextTR.getElementsByTagName("INPUT")[2].focus();
    }
}
function MovePreviousProduct(obj)
{
    var count = 3;
    var prevTR=obj.parentNode.parentNode.previousSibling;
    try
    {
        while (count-- > 0)
            do
                prevTR = prevTR.previousSibling;
            while(prevTR.nodeType == 3);
        prevTR.getElementsByTagName("INPUT")[2].focus();
    } catch(ex) {
        prevTR = obj.parentNode.parentNode.parentNode.lastChild.previousSibling.previousSibling;
        while(prevTR.nodeType == 3)
            prevTR = prevTR.previousSibling;
        prevTR.getElementsByTagName("INPUT")[2].focus();
    }
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
