﻿function FavoritesAttachImageHandlers(thisImg) {
    thisImg.onclick = function() { FavoritesSelectStar(thisImg, true) };
    thisImg.onmouseover = function() { FavoritesSelectStar(thisImg); return false; };
    thisImg.onmouseout = function() { FavoritesImgMouseOut(thisImg); };
}

function FavoritesDeAttachImageHandlers(thisImg) {
    thisImg.onclick = "";
    thisImg.onmouseover = "";
    thisImg.onmouseout = "";
}


function FavoritesImgMouseOut(img) {
    if (img.src.indexOf('FilledStar.png') >= 0)
        img.src = '/Images/icons/StarUnSelected.gif';
}

function FavoritesSelectStar(img, clicked) {
    if (true == clicked) {
        img.src = '/Images/icons/StarSelected.gif';
        //call webservice to add to favorites		
        var ele = document.getElementById(img.id + '_PONum');
        if (ele.value > "") {
            var ponum = ele.value;
            FavoritesWSAddOrder(ponum, img);
        }

        ele = document.getElementById(img.id + '_InvId');
        if (ele.value > "") {
            var invid = ele.value;
            FavoritesWSAddItem(invid, img);
        }

    }
    else {
        if (img.src.indexOf('StarSelected.gif') < 0)
            img.src = '/Images/icons/FilledStar.png';
    }
}

function FavoritesWSAddItem(invId, img) {
    //var output = Fulfillment.Web.WebServices.HelloWorld(SucceededCallbackWithContext, FailedCallback, null, null);   
    FulFillment.Web.WebServices.AddItemToFavorites(invId, FavoritesSucceededCallback, FailedCallback, img);
}

function FavoritesWSAddOrder(ponumber, img) {
    //var output = Fulfillment.Web.WebServices.HelloWorld(SucceededCallbackWithContext, FailedCallback, null, null);   
    FulFillment.Web.WebServices.AddOrderToFavorites(ponumber, FavoritesSucceededCallback, FailedCallback, img);
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function FavoritesSucceededCallback(result, img) {
    img.src = '/Images/icons/StarSelected.gif';
    FavoritesDeAttachImageHandlers(img);
}

// This is the callback function called if the
// Web service failed. It accepts the error object
// as a parameter.
function FailedCallback(error) {
    alert("Service Error: " + error.get_message());
}

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

