   /* ###################################
   Vars
     pfRetailBasketID - Persisted in a cookie chech on load
   ################################### */

   var pfRetailBasketID = null;


  function EmailPriceEnquiry(UserID, InventoryID) {
    AjaxRequest.get(
      {
        'url':'eJIFCont.php',
        'parameters':{'Class':'TUser',
                      'OID':UserID,
                      'Method':'PriceEnquiry',
                      'InventoryID':InventoryID
                      },
        'onSuccess':function(Req) {
          alert('Your enquiry has been sent');
        }
      }
    );
  }

  function ProductsFrameQtyKeyUp(event, QtyInput, Button, DecimalPlaces) {
  // if its and enter try posting to the basket
    if (event.keyCode == 13) {
      AddToBasket(QtyInput);
      return true
    }
    if (event.keyCode == 9 || event.keyCode == 16) { // tab and back tab
      return true
    }
    var Qty = QtyInput.value;
    if  (Qty == '.' && DecimalPlaces > 0) {Qty = '0';} // allow decimal transition
    var LastChar = Qty.substring(Qty.length-1,Qty.length);
    var S = parseFloat(Qty);
    if (Button != null) {Button.disabled = isNaN(S);}
    var DP = 0;
    if (Qty.indexOf('.') >= 0) {
      DP = Qty.length-Qty.indexOf('.')-1;
    }

    if (isNaN(S) || (DP > DecimalPlaces) || Qty >= 1000 || (DecimalPlaces == 0 && LastChar == '.')) { // trim the last char
      if (Qty >= 1000) {
        alert('Max Qty per item is 999 of each unit (ie 100,000 for 1000s), for orders larger than this contact JIF directly');
      }
      S = QtyInput.value;
      QtyInput.value = S.substring(0,S.length-1);
    } else {
      QtyInput.style.backgroundColor = clEditedColour;
//      if (Button != null) {Button.style.backgroundColor = clEditedColour;}
    }
  }

  function SetPrices(InventoryID, Price, NetPrice, Discount) {
    var ID = 'TInventoryItem['+InventoryID+'].Price';
    ejc_SetValueByID(ID, Price);
    ID = 'TInventoryItem['+InventoryID+'].NetPrice';
    ejc_SetValueByID(ID, NetPrice);
  }

  function SetDiscount(InventoryID, Discount, Active) {
    ID = 'TInventoryItem['+InventoryID+'].QuotedDiscount';
    SetValueByID(ID, Discount);
    if (Active) {
      eJIF_SetColourByID(ID, 'background', clOKColour);
    } else {
      eJIF_SetColourByID(ID, 'background', clDefaultBackGround);
    }
  }

  function SetItemDiscount(Input) {
    var Discount = parseFloat(Input.value);
    var Details = new AjaxLib_TInputDetail(Input);
    var InventoryID = Details.OID;
    var AccountNo = document.getElementById('AccountNo').value;

    if (AccountNo != null) {
      AjaxRequest.get(
        {
          'url':'eJIFCont.php',
          'parameters':{'Class':'TDebtor',
                        'OID':AccountNo,
                        'Method':'SetProductDiscount',
                        'InventoryID':InventoryID,
                        'Discount':Discount
                        },
          'onSuccess':function(Req) {
            if (AjaxLib_DisplayError(Req.responseXML)) {
              SetDiscount(Discount); // should be the before value
            } else {
              var Price = AjaxLib_TagValue(Req.responseXML,'Price');
              var NetPrice = AjaxLib_TagValue(Req.responseXML,'NetPrice');
              SetPrices(InventoryID, Price, NetPrice);
            }
            Input.style.backgroundColor = clOKColour;
          },
          'onLoading':function(Req) {
            Input.style.backgroundColor = clProcessingColour;
          }
        }
      )
    }

  }

  function DeleteQuotedDiscount(Button) {
    alert('DeleteQuotedDiscount - Needs fixing');
    var Details = new AjaxLib_TInputDetail(Button);
    var InventoryID = Details.OID;
    var AccountNo = document.getElementById('AccountNo').value;

    if (AccountNo != null) {
      AjaxRequest.get(
        {
          'url':'eJIFCont.php',
          'parameters':{'Class':'TDebtor',
                        'OID':AccountNo,
                        'Method':'DeleteProductDiscount',
                        'InventoryID':InventoryID
                        },
          'onSuccess':function(Req) {
            if (AjaxLib_DisplayError(Req.responseXML)) {
              Button.style.backgroundColor = 'lightgrey';
            } else {
              var Price = AjaxLib_TagValue(Req.responseXML,'Price');
              var NetPrice = AjaxLib_TagValue(Req.responseXML,'NetPrice');
              SetPrices(InventoryID, Price, NetPrice);
              SetDiscount(InventoryID, '0.0', false);
              eJIF_SetVisibilityByID(Button.id);
            }
          },
          'onLoading':function(Req) {
            Button.style.backgroundColor = clProcessingColour;
          }
        }
      )
    }
  }

   /* ###################################
     Retail Functions
     pf_OnLoad() validates pfRetailBasketID, then sets the ItemCount

   ################################### */

  function pf_InitRetailClient() {
    // get the retail basket id from a cookie
    EJIFWebUserID = ejc_GetCookie('EJIFWebUserID');
    var Parameters = {'Class'         : 'TRetailUser',
                      'OID'           : 'static', //
                      'Method'        : 'CheckWebUserID',
                      'WebUserID'     : EJIFWebUserID};
    AjaxLib_CallMethod(null, Parameters, pf_RetailClientChecked);
    // if one doesn't exist request a value from the server
  }

  function pf_RetailClientChecked(Input, XMLDoc) {
    // set the cookie
    // move this to AjaxLib AjabLib_GetMethodResult();
    var Elements = XMLDoc.getElementsByTagName('result');
    if (Elements.length == 1) {
      var Attribs = Elements[0].attributes;
      var S = Attribs.getNamedItem('return').value;
      var Result = window.top.php_unserialize(S);
      // attribs are RetailBasketID, ItemCount
      ejc_SetCookie('EJIFWebUserID', Result.EJIFWebUserID, 120);
    } else {
      window.alert('Failed to init basket');
    }
    // Update the Qty in the Basket A[1] ?
  }

  function pf_AddToBasket(Input) {
    if (Input.style.backgroundColor != clEditedColour) {return;}
    window.top.eJIF_SendIFramesMessage('basket');
    var Quantity = Input.value;
    var Details = new AjaxLib_TInputDetail(Input);
    var Parameters = {'InventoryID':Details.Parameter0, 'Quantity':Quantity};
    AjaxLib_CallMethod(Input, Parameters, pf_AddedToBasket);
  }

  function pf_SetQuotedDiscount(Input) {
    if (Input.style.backgroundColor != clEditedColour) {return;}
//    var Discount = Input.value;
//    var Details = new AjaxLib_TInputDetail(Input);
//    var Parameters = {'Parameter1':Discount};
    AjaxLib_CallMethod(Input); //, Parameters);
  }

  function pf_AddedToBasket(Input, XMLDoc) {
//    eJIF_UpdateFormFromAjax(Input, XMLDoc);
    if (Input.value == '') {Input.value = '0';}
    var Quantity = parseFloat(Input.value);
    if (Quantity == 0) {
      Input.style.backgroundColor = clDefaultBackGround;
    } else {
      Input.style.backgroundColor = clOKColour;
    }
  }


  var _RowID;

  function tpt_UnsuppressItem(Button, RowID) {
    _RowID = RowID;
    if (eJIF_Confirm('Unsuppress this item')) {
      AjaxLib_CallMethod(Button, null, tpt_UnsuppressItemOnSucess)
    }
  }

  function tpt_UnsuppressItemOnSucess(Button, XMLDoc) {
    eJIF_DeleteTableRowByID(_RowID);
    window.top.eJIF_SendIFramesMessage('suppressed');
  }


