﻿/*************************************************************************************************/
/*************************************************************************************************/
/***
/***  Web Services
/***
/*************************************************************************************************/
/*************************************************************************************************/

var G_Service = new Array();
var GP = {
    Item: {
        StatusOfAvailability: { Available:0, TBD:1, SoldOut:2, Discontinued:3, Error:4, Unavailable:5 }
    }
}

/**************
/* Method used to add item to wishlist
/***/
function AddItemToWishlist(obj)
{
    if (obj.id)
    {
        var arIds = obj.id.split('_');
        G_Service[arIds[0]] = new Commerce().AddItemToWishlist(arIds[0],arIds[1], _AddItemToWishlist_OnComplete, _MSAjax_OnError, _MSAjax_OnTimeOut);   
    }
}

function _AddItemToWishlist_OnComplete(arg)
{
	var ReturnObj = eval('('+arg+')');

	if (!ReturnObj.Error)
	{
        var Plant = ReturnObj.Item.Plant;
        var imagePath = WG.BasePath + '/images/layout/noimageavailable.gif';
        for (i = 0; i < Plant.MediaFiles.length; i++)
        {
            if (Plant.MediaFiles[i].MediaType.match(/thum/gi))
                imagePath = WG.BasePath + '/images/lib/plants/' + Plant.MediaFiles[i].FileName;
        }
        
        var str = '<div><h1 style="font-size:10pt">' + Plant.Name.AsHtml + ' was added to your wishlist</h1>';        
        str += '<img src="' + imagePath + '" alt="' + Plant.Name.AsText + '" Title="' + Plant.Name.AsText + '"';
        str += ' class="plant" style="width:75px;height:75px;float:left;margin-right:15px;" />';
        
        str += '<div style="float:left; width: 375px;font-size:11px">';
        
        str += '<strong>Common Name: </strong>' + Plant.Common + '<br />';

		if (typeof(PR) == "undefined")
		{
			str += '<strong>Sizes Offered:<br /></strong>';
			for (i = 0; i < Plant.SizesOffered.length; i++)
				str += '&nbsp; ' + Plant.SizesOffered[i].Name + '<br />';
        }

        str += '<strong>Hardiness Zone:</strong> ' + Plant.HardinessZone + '<br />';
        str += '<strong>Height:</strong> ' + Plant.Height + '<br />';
        str += '<strong>Spread:</strong> ' + Plant.Spread + '<br />';
        str += '<strong>Exposure:</strong> ' + Plant.Exposure + '<br />';
        str += '<strong>Bloom Time:</strong> ' + Plant.BloomTime + '<br />';
 
        str += '</div></div>';
        
        str += '<div style="clear:both">';
        str += '<br /><br/ ><div style="padding: 8px 5px 0 0; text-align:right; background: #d7bb98">';
        str += ' <img src="' + WG.BasePath + '/images/buttons/close.gif" onclick="WG.Dialog.Close();" style="cursor:pointer;border:0px none" />'; 
        str += ' &nbsp; <a href="' + WG.BasePath + '/wishlist"><img src="' + WG.BasePath + '/images/buttons/proceedtowishlist.gif"';
        str += ' border="0" /></a></div>';
        
	    WG.Dialog.Create(str);
	    WG.Dialog.ToggleOverlay(true);
	}
}

/**************
/* Method used to located a great plains item by it's full great plains key
/***/
function FindPlantByGreatPlainsId(i, obj)
{
    obj.value = jQuery.trim(obj.value);
    if (obj.value.length >= 7 && WG.QuickOrderForm.Queue.Exists(i,obj))
        return true;
    
    jQuery(obj).parents('table').find('#item-container, #item-substitution-container').empty()
        .end().css('border','0px none').find('img#imgNoToAll').hide();    
    
    if (obj.value.length >= 7)
    {
        WG.QuickOrderForm.Queue.Add(i,obj);
        
        WG.QuickOrderForm.Notice.Worker({load:true,index:i,ctrl:obj});
        G_Service[i] = new Commerce().FindPlantByGreatPlainsId(i, obj.value, 'true', jQuery('#txtRequestedShipDate').val(),
                                                                _FindPlantByGreatPlainsId_OnComplete, _MSAjax_OnError, _MSAjax_OnTimeOut);
        
        WG.QuickOrderForm.Controls.NewRow(i,obj);        
    }
    else
        WG.QuickOrderForm.Notice.Worker({load:false,index:i});
}

/**************
/* Method used to located a great plains item by it's base key and size key
/***/
function FindPlantByPartialGreatPlainsId(gp_id_base, obj)
{
    if (! obj)
        obj = jQuery('select[id$="ddlSize"]').get(0);
    var size_id = jQuery(obj).val();
    
    jQuery('#item-container, #item-substitution-container').empty();
    jQuery('#item-init').css('display','block');    
    
    if (gp_id_base.length >= 5 && size_id.length > 0) {
        jQuery('select[id$=ddlSize]').attr('disabled', 'disabled');
    
        WG.QuickOrderForm.Notice.Worker({load:true,index:0,ctrl:obj});
        G_Service[0] = new Commerce().FindPlantByPartialGreatPlainsId(gp_id_base, size_id, jQuery('#txtRequestedShipDate').val(),
                                                                        _FindPlantByGreatPlainsId_OnComplete, _MSAjax_OnError, _MSAjax_OnTimeOut);
    }
    else
        WG.QuickOrderForm.Notice.Worker({load:false,index:0});
}

/**************
/* Callback for FindPlantByGreatPlainsId
/***/
function _FindPlantByGreatPlainsId_OnComplete(arg)
{
	var ReturnObj = eval('('+arg+')');
	var ControlIndex = ReturnObj.Params[0];
	DisplayGreatPlainsItem(ReturnObj, ControlIndex, 0);
}

function DisplayGreatPlainsItem(ReturnObj, ControlIndex, SubstituteIndex)
{
    var IsMultiForm = false;

    if (!ReturnObj.Error)
	{
	    WG.QuickOrderForm.Notice.Worker({load:false,index:ControlIndex});

	    IsMultiForm = (ReturnObj.Params[1] == 'true' ? true : false);
	    var RequestedShipDate = ReturnObj.RequestedShipDate;
	    var Plant   = ReturnObj.Plant;
        var SelectedSize = WG.QuickOrderForm.ParseSelectedSizeList(Plant.SizesOffered, Plant.GreatPlains.Id);

        // the main reference control to append items to will be deciphered by either the multi form quick-order-items 
        // container or just a single container selector
        var refSelectorContainerId = '';
        
        // if this is the single add to cart page, then remove all child nodes
        if (! IsMultiForm)
            jQuery('#item-init').css('display','none');
        else
            refSelectorContainerId = 'div.quick-order-items:eq('+ControlIndex+') '; // must reference the appropriate index group

        // copy the template item and alter the id then append this item to the item container, then
        // control/values will be populated later
        var refContainerId = Plant.IsSubstitute ? '#item-substitution-container' : '#item-container';
        refSelectorContainerId += refContainerId;
        
        // substitution header, should only be added once and first per substiution grouping
        if (Plant.IsSubstitute && jQuery(refSelectorContainerId+' #substitutions-header').length == 0)
            jQuery(refSelectorContainerId).append('<div id="substitutions-header"><strong>Suggested Substitute(s):</strong></div>');
        // create unique indexed id for referencing. substitutions will have the same index as their parent item.. to maintain
        // uniqueness attach the substituted plant
        var newItemControlId = refContainerId+'-'+ControlIndex+(Plant.IsSubstitute?'-'+SubstituteIndex:''); newItemControlId = newItemControlId.replace('#','');
        var htmlTemplate = jQuery('#item-template').html();
        var newItemControlContainer = jQuery('<div id="'+newItemControlId+'">'+htmlTemplate+'</div>');       

        if ( jQuery(refSelectorContainerId).find('#'+newItemControlId).length > 0 )
            return;
        jQuery(refSelectorContainerId).append(jQuery(newItemControlContainer));

        // the template has an item number/field div attached, for the quickorder form we will remove this as
        // this portion already exists, however, it will not exist on substitute records
        if (!Plant.IsSubstitute && IsMultiForm)
            jQuery(refSelectorContainerId+' #item-number-container').remove();

        var ControlRef = jQuery((IsMultiForm?'div.quick-order-items:eq('+ControlIndex+') #':'#')+newItemControlId);

        // if plant is substitute and the plant is not sold out, then hide substitutions
        if (Plant.IsSubstitute && Plant.HideSubstituteOnLoad) {
            jQuery(refSelectorContainerId).hide();
            WG.QuickOrderForm.Quantity.Disable(jQuery(ControlRef));
        }        
        
        // setup references to new control objects to variables
        var ddlQuantity = jQuery(ControlRef).find('select#ddlQuantity');
        var ddlAvailability = jQuery(ControlRef).find('select#ddlAvailability');
        var txtQuantity   = jQuery(ControlRef).find('input#txtQuantity');
        var lblItemNumber = jQuery(ControlRef).find('span#lblItemNumber');
        var txtItemNumber = jQuery(ControlRef).find('input#txtItemNumber');
        
        // bind events
        jQuery(ddlAvailability).change(function() {
            var selValue = jQuery(this).val();
            var controlRef = jQuery(this).parents('.item-control-container');
            jQuery(ControlRef).find('input[id$="hdnBin"]').val((selValue == 'SUB' ? '' : selValue));
            var substituationContainer = jQuery(this).parents('div#item-container').nextAll('#item-substitution-container');
            if (jQuery(substituationContainer).length > 0) {
                if ('SUB' == selValue) {
                    jQuery(substituationContainer).show();
                    WG.QuickOrderForm.Quantity.Disable(jQuery(ControlRef));
                }
                else {
                    // if we have a substitute item with only one availability and it's auto-selected, since we don't show the dropdown, 
                    // we can't disable the quantity or clear out the hidden bin, because these cannot be re-enabled.. need to visit further
                    jQuery(substituationContainer).find('input[id$=hdnBin]').each(function() {
                        var ddl = jQuery(this).parent().find('select[id$=ddlAvailability]');
                        WG.QuickOrderForm.Quantity.Disable(jQuery(this).parent());
                        if (jQuery(ddl).find('option').length > 0)
                            jQuery(this).val(''); // clear bin
                        jQuery(ddl).val(''); // clear availability ddl
                    });
                    jQuery(substituationContainer).hide();
                    WG.QuickOrderForm.Quantity.Enable(jQuery(ControlRef));
                }
            }
            else
                WG.QuickOrderForm.Quantity.Enable(jQuery(ControlRef));
            var plantId = jQuery(ControlRef).find('input[id$="hdnPlantId"]').val();
            WG.QuickOrderForm.Quantity.Display(ControlIndex, plantId, jQuery(ControlRef), selValue);
        });
        jQuery(ddlQuantity).change(function() { WG.QuickOrderForm.Price.Calculate(ControlIndex, jQuery(ControlRef)); });
        if (Plant.IsSubstitute)
            jQuery(ddlQuantity).change(function() { WG.QuickOrderForm.Reinstate.Toggle(ControlIndex,this); });

        jQuery(txtQuantity).bind('keyup', function() { WG.QuickOrderForm.Price.Adjust(ControlIndex,this); })
            .bind('keypress', function(e) { return validChar('numeric',this,e); })
            .bind('blur', function() { doZeroDefault(this); });
        jQuery(ControlRef).find('input#chkReInstate').click(function() { WG.QuickOrderForm.Reinstate.Toggle(ControlIndex,this); });
        jQuery(ControlRef).parents('table').find('img#imgNoToAll').click(function() {
            var ref = jQuery(this).parents('div.quick-order-items').next('div.quick-order-items');
            jQuery(this).parents('div.quick-order-items').empty();
            WG.QuickOrderForm.Controls.NewRowNeeded();
            if (ref.length == 1)
                jQuery(ref).find('input#txtItemNumber').focus();
        });

        // populate visual entities
        jQuery(lblItemNumber).html(Plant.GreatPlains.Id.TrueId);
        if (Plant.IsSubstitute)
            jQuery(txtItemNumber).val(Plant.GreatPlains.Id.TrueId);
        var str = '';
        for (p = 0; p < Plant.MediaFiles.length; p++) {
            if (Plant.MediaFiles[p].MediaType.match(/thum/gi))
                jQuery(ControlRef).find('img#imgThumb').attr('src',WG.BasePath+'/images/lib/plants/' + Plant.MediaFiles[p].FileName);
        }
        if (Plant.IsSubstitute && !IsMultiForm)
            str += '<a href="'+WG.BasePath+'/plants/view/?plant='+Plant.Id+'"><strong>' + Plant.Name.AsHtml + '</strong></a><br />';
        else
            str += '<strong>' + Plant.Name.AsHtml + '</strong><br />';
        if (SelectedSize) str += SelectedSize.Name + '<br />';

        // set current multiples for current item
        WG.QuickOrderForm.MultiplesInUse[ControlIndex] = {Value:SelectedSize.Multiples,Description:SelectedSize.UnitofMeasure}; //UofM.Multiples;
        // set current price breakdowns for current item
        WG.QuickOrderForm.AvailablePricing[ControlIndex] = Plant.GreatPlains.Item.Prices;
        // set current bins for current item
        WG.QuickOrderForm.AvailableBins[ControlIndex][Plant.Id] = Plant.GreatPlains.Item.Bins;
        WG.QuickOrderForm.BindQuantityList(ControlIndex, jQuery(ddlQuantity));

        /////
        // Builds the quantity/price breakdown list as a table, then tacks on the string to the main
        // description string
        /////

        var Prices = Plant.GreatPlains.Item.Prices;
        if (Prices.length > 0)
        {
            var uom = 'each'; //UofM.EquivalentBase.toLowerCase();
            var strPriceList = '';
            strPriceList += '<tr><td colspan="2" style="padding-top:6px">Price(s):</td></tr>';
            for (var i = 0; i < Prices.length; i++)
            {
                var qfrom_to =  Prices[i].QuantityFrom + ' - ' + Prices[i].QuantityTo;
                if (Prices.length == 1)
                    qfrom_to = SelectedSize.Multiples;
                else if (i == Prices.length-1 && Prices.length != 1)
                    qfrom_to = Prices[i].QuantityFrom + '+';

                strPriceList += '<tr><td align="right">' + qfrom_to + ' plants: &nbsp;</td>';
                strPriceList += '<td>$ ' + formatNumber(Prices[i].Value,2,true,true) + '</td></tr>';
            }
            strPriceList = '<table border="0" cellpadding="0" cellspacing="0">' + strPriceList + '</table>';
            str = str + strPriceList;
        }
        
        // set available status
        var statusOfAvail = Plant.GreatPlains.Item.StatusOfAvailability;
        var SelectedBin = false;
        // if we're able to find the exact bin for the user, then auto-load the available quantity for viewing
        if (Plant.GreatPlains.Item.SelectedBin)
            SelectedBin = Plant.GreatPlains.Item.SelectedBin;
            
        // if status is unavailable, that means an error occurred with loading all required GP object(s)
        if (GP.Item.StatusOfAvailability.Error != statusOfAvail) {
        
            /////
            // Builds the available quantities and dates
            /////

            // 1. if we have an available bin within user ship date and not sold out
            // 2. or plant is sold out
            if (Plant.GreatPlains.Item.SelectedBin && !Plant.GreatPlains.Item.SelectedBin.IsSoldOut ||
                !Plant.IsSubstitute && statusOfAvail == GP.Item.StatusOfAvailability.SoldOut) {
                jQuery(ddlAvailability).parents('tr:first').hide();
            }
            else {
                var Bins = Plant.GreatPlains.Item.Bins;
                var hasValidBin = false;
                for (var i = 0; i < Bins.length; i++) {
                    if (Bins[i].InSeason) hasValidBin = true;
                }
                if (hasValidBin) {
                    jQuery(ddlAvailability).empty();
                    var bin_opt_html = ''; //'<option value="">---</option>';

                    for (var i = 0; i < Bins.length; i++) {
                        // 1. do not show out of season items for all
                        // 2. do not show sold out items for plant..
                        // 3. ..unless a substitute is available
                        // 4. and do not add sold out items to substitutes, only the original item
                        if (Bins[i].InSeason) {
                            if (!Plant.IsSubstitute && !Bins[i].IsSoldOut ||
                                !Plant.IsSubstitute && Bins[i].IsSoldOut && Plant.Substitutions.length > 0 ||
                                !Bins[i].IsSoldOut && Plant.IsSubstitute) {
                                var bin_opt_text = Bins[i].AvailableFromAsString + ' - ' + Bins[i].AvailableToAsString + (!Bins[i].IsSoldOut ? ' available' : ' unavailable, display substitute');
                                var selected = Bins[i].AutoSelect ? 'selected' : '';
                                if (!Plant.GreatPlains.Item.SelectedBin && Bins[i].AutoSelect) {
                                    SelectedBin = Bins[i];
                                }
                                // if the bin is sold out, flag the value with SUB, this will indicate to load applicable substitutes
                                bin_opt_html += '<option value="' + (!Bins[i].IsSoldOut ? Bins[i].Id : 'SUB') + '" ' + selected + '>';
                                bin_opt_html += bin_opt_text + '</option>';
                            }
                        }
                    }
                    jQuery(ddlAvailability).append(bin_opt_html);
                    jQuery(ddlAvailability).removeAttr('disabled');
                }
                else
                    jQuery(ddlAvailability).attr('disabled', 'disabled');
            }

            // set available quantity for selected bin
            var quantityForSelectedBin = 0;
            if (SelectedBin)
                quantityForSelectedBin = WG.QuickOrderForm.Quantity.Display(ControlIndex, Plant.Id, jQuery(ControlRef), SelectedBin.Id);
        }

        if (statusOfAvail > GP.Item.StatusOfAvailability.TBD)
        {
            if (statusOfAvail == GP.Item.StatusOfAvailability.SoldOut)
                message = '<b>This item is currently sold out.</b>';
            else if (statusOfAvail == GP.Item.StatusOfAvailability.Discontinued)
                message = '<b>This item has been discontinued.</b>';
            else if (statusOfAvail == GP.Item.StatusOfAvailability.Error) {
                if (Plant.GreatPlains.IsFound) {
                    var js = '<a href="javascript:FindPlantByPartialGreatPlainsId(\'' + Plant.GreatPlains.Id.BaseId + '\');">Please try again</a>';
                   message = 'This item could not be retreived at this time. '+js;
                }
                else
                    message = '<b>This item is currently unavailable.</b>';
            }
            else if (statusOfAvail == GP.Item.StatusOfAvailability.Unavailable)
                message = '<b>This item is currently unavailable.</b>';
            if (statusOfAvail != GP.Item.StatusOfAvailability.Error && statusOfAvail != GP.Item.StatusOfAvailability.Available) {
                if (Plant.Substitutions.length > 0)
                    message += ' See suggested substitute(s) below.';
                else
                    message += ' There is no suggested substitute.';
            }
            WG.QuickOrderForm.Notice.Availability({ctrl:jQuery(ControlRef), status:statusOfAvail, msg:message});
            
            if (!Plant.IsSubstitute && statusOfAvail == GP.Item.StatusOfAvailability.SoldOut)
            {
                jQuery(ControlRef).find('div#pnlReinstate').css('display','block').find('span').html(Plant.GreatPlains.Item.NextAvailabilityDate);
                jQuery(ControlRef).find('div.item-control-container').addClass('soldout')
                    .parents('table[id$="tblQuickOrderItem"]').css('border','3px solid #4b3828').end()
                    .find('img#imgThumb, div#pnlDescription, div#pnlItemContainer tr:first td')
                    .each(function() { jQuery(this).fadeTo(0,0.65); });
            }
        }
        else {
            // if the selected bin is sold out, it means that there are other bins available, however, this one is selected to
            // auto-show substitutions; in this case, show a special message
            if (SelectedBin.IsSoldOut && !Plant.IsSubstitute) {
                statusOfAvail = GP.Item.StatusOfAvailability.Unavailable;
                message = '<b>This item is currently unavailable to ship '+RequestedShipDate+'.</b><br />';
                message += 'Please select an alternate shipping date' + (Plant.Substitutions.length > 0 ? ', or view available substitutions below.' : '.' );
                jQuery(ControlRef).find('div.item-control-container').parents('table[id$="tblQuickOrderItem"]').css('border', '3px solid #4b3828').end();
                WG.QuickOrderForm.Notice.Availability({ ctrl: jQuery(ControlRef), status: statusOfAvail, msg: message });
            }
        
        
            //if (parseInt(quantityForSelectedBin) < 0)
            //    strQuantityAvailable = '(TBD)';
           
            //jQuery(ControlRef).find('span#lblQuantityAvailable').html(strQuantityAvailable);

            /*var strDisclaimer = 'If you would like to purchase more than the current inventory quantity (' + (parseInt(quantityForSelectedBin) >= 0 ? quantityForSelectedBin : 'TBD') + ')';
                strDisclaimer += ', please <a href="/contactus">contact us</a> for assistance';
            if (statusOfAvail == GP.Item.StatusOfAvailability.TBD)
                strDisclaimer = 'Please <a href="/contactus">contact us</a> for available inventory.';
            
            if (statusOfAvail <= GP.Item.StatusOfAvailability.TBD)
            {
                jQuery(ControlRef).find('div#divDisclaimer').html(strDisclaimer).css('display','block');                
                if (statusOfAvail == GP.Item.StatusOfAvailability.TBD)
                {
                    var message = 'The availability of this is item is to be determined.';
                    WG.QuickOrderForm.Notice.Availability({ctrl:jQuery(ControlRef), status:statusOfAvail, msg:message});
                }
            }*/
        }

        // populate hidden form/data fields
        jQuery(ControlRef).find('input[type="hidden"]').each(function() {
            if (jQuery(this).attr('id') == 'hdnPlantId') { jQuery(this).val(Plant.Id); }
            else if (jQuery(this).attr('id') == 'hdnSize' && SelectedSize) { jQuery(this).val(SelectedSize.Id); }
            else if (jQuery(this).attr('id') == 'hdnIsSubstituteFor' && Plant.IsSubstitute) { jQuery(this).val(Plant.IsSubstituteFor); }
            else if (jQuery(this).attr('id') == 'hdnStatusOfAvailability' && !Plant.IsSubstitute) { jQuery(this).val(Plant.GreatPlains.Item.StatusOfAvailability); }
            else if (jQuery(this).attr('id') == 'hdnBin') {
                var autoSelectedAvailability = jQuery(ddlAvailability).val();
                if ('SUB' == autoSelectedAvailability) {
                    jQuery(this).val('');
                    jQuery(ddlAvailability).trigger('change');
                }
                else {
                    for (var i = 0; i < Plant.GreatPlains.Item.Bins.length; i++) {
                        if (Plant.GreatPlains.Item.Bins[i].AutoSelect && !Plant.GreatPlains.Item.Bins[i].IsSoldOut) {
                            jQuery(this).val(Plant.GreatPlains.Item.Bins[i].Id); break;
                        }
                    }
                }
            }
        });        
        
        // calculating and displaying a base price upon first load        
        WG.QuickOrderForm.Price.Calculate(ControlIndex, jQuery(ControlRef));

        jQuery(ControlRef).find('div#pnlDescription').html(str);
        
        // do not show item information if a critical error has occured
        if (statusOfAvail != GP.Item.StatusOfAvailability.Error)
            jQuery(ControlRef).find('div#pnlItemContainer').css('display','block');

        /*******************************
         * Adjusting main parameters for substitution presentation if current item is sold out or discontinued
         ******/

        //if ( (statusOfAvail == GP.Item.StatusOfAvailability.SoldOut || statusOfAvail == GP.Item.StatusOfAvailability.Discontinued) 
        if ( Plant.Substitutions && Plant.Substitutions.length > 0)
        {
            for (var i = 0; i < Plant.Substitutions.length; i++)
            {
                var ReturnSubstitutionObj = new Object();
                for (obj in ReturnObj) {
                    if (obj != 'Plant')
                        ReturnSubstitutionObj[obj] = ReturnObj[obj];
                }
                // set plant to main substitutional plant object
                ReturnSubstitutionObj.Plant = Plant.Substitutions[i];
                // set flag to avoid certain declarations in the reused function
                ReturnSubstitutionObj.Plant.IsSubstitute = true;
                ReturnSubstitutionObj.Plant.IsSubstituteFor = Plant.Id+':'+Plant.GreatPlains.Id.TrueId+':'+statusOfAvail;
                // set flag to indicate whether to auto-show substitutions or to rely on user interaction
                // this flag only applies to when an item is sold out; if an item is unavailable but has other available dates, and the
                // unavailable date is auto-selected, another method will determine whether or not to auto-show the substitute(s)
                ReturnSubstitutionObj.Plant.HideSubstituteOnLoad = Plant.GreatPlains.Item.HasAvailability && 'SUB' != jQuery(ddlAvailability).val();
                
                DisplayGreatPlainsItem(ReturnSubstitutionObj, ControlIndex, i);
            }
            if (IsMultiForm)
                jQuery(ControlRef).parents('table').find('img#imgNoToAll').show();
        }
        
        if (IsMultiForm)
            WG.QuickOrderForm.Queue.Remove(ControlIndex,ControlRef);
    }
    else
    {
        if (ReturnObj.Error == "0")
        {
            IsMultiForm = ReturnObj.Params[1]=='true'?true:false;            
             
            if (IsMultiForm)
            {
                var ControlRef = jQuery('div.quick-order-items:eq('+ReturnObj.Params[0]+')');
                var gpId = jQuery(ControlRef).find('input#txtItemNumber:first').val().toUpperCase();
                var msg = gpId+' appears to be an invalid item number.';
            }
            else
            {
                var gpId = jQuery('input[id$="txtItemNumber"]:first').val().toUpperCase();
                var js = "FindPlantByPartialGreatPlainsId('"+gpId+"');";
                var msg = 'Plant '+gpId+' could not be retreived at this time. <a href="javascript:'+js+'">Please try again</a>';
            }
            WG.QuickOrderForm.Notice.Worker({load:false,index:ControlIndex,ctrl:ControlRef,error:msg});
        }
        else if (ReturnObj.Error == "1")
            window.location = WG.BasePath + '/plants';
    }

    jQuery('select[id$=ddlSize]').removeAttr('disabled');
}


/**************
/* Method used to persist session state when on the quick order form
/***/
var KeepAliveAttempts = 0;
function PersistSession()
{
    if (KeepAliveAttempts < 30)
        Commerce.PersistSessionState('{}', _PersistSession_OnComplete, _MSAjax_OnError, _MSAjax_OnTimeOut);   
}

function _PersistSession_OnComplete(arg)
{
	var ReturnObj = eval('('+arg+')');
	if (ReturnObj.Error && ReturnObj.Error > 0)
	    window.location = WG.BasePath + '/plants';
    else
	{
	    KeepAliveAttempts += 1;
        var to = window.setTimeout('PersistSession();',900000);
    }
}

/*************************************************************************************************/
/*************************************************************************************************/
/***
/***  Common Services
/***
/*************************************************************************************************/
/*************************************************************************************************/

var G_HasAborted = false;
function _MSAjax_Abort(i)
{
    if (G_Service[i] && G_Service[i].get_executor().get_started())
    {
        G_HasAborted = true;
        G_Service[i].get_executor().abort();
        G_Service[i] = false;
    }
}

function _MSAjax_OnComplete(arg)
{
    //alert(arg);
    return true;
}
function _MSAjax_OnError(arg)
{
    if (! G_HasAborted)
    {
        alert("error has occured: " + arg.get_message());//_message);
        G_HasAborted = false;
    }
}
function _MSAjax_OnTimeOut(arg)
{
   alert("timeout has occured");
}