﻿/// <reference path="jquery-1.3.2-vsdoc.js" />

// This is the initial jQuery starting point.  The 'main' function is run once the DOM of every page is established.

  $(document).ready(function() {
    main();
  });

  function main() {
    // Nothing to do at this time
  }
  function AddProductID(productID, checkBox) {
//if product is selected
      if ($(checkBox).is(':checked')) {
        //get selected product ids
            var hdProdIds = $('input[class="selectedProductIds"]').val();
            
            //if no product is selected
          if (hdProdIds.length > 0) {
              // $('input[name="selectedProductIds"]').val(hdProdIds+ productID);
              hdProdIds = hdProdIds + ',' + productID;

              $('input[class="selectedProductIds"]').val(hdProdIds);
      }
          else {
              $('input[class="selectedProductIds"]').val(productID);
            }
           
      }
      else {
          var hdProdIds = $('input[class="selectedProductIds"]').val();
           if (hdProdIds == productID) {
               $('input[class="selectedProductIds"]').val('');
             
          }
          else {
              //check if the id is last id or not
              var indexOfProdId  = hdProdIds.indexOf(productID);
              //check if the product is last ib the string
              var lentot = productID.length + indexOfProdId;
              if (lentot == hdProdIds.length) {
                  hdProdIds = hdProdIds.substring(0,(indexOfProdId - 1));
                  $('input[class="selectedProductIds"]').val(hdProdIds);
              }
              else {
                 
                  hdProdIds = hdProdIds.substring(0, indexOfProdId) + hdProdIds.substring((indexOfProdId+1+productID.length),hdProdIds.length);

                  $('input[class="selectedProductIds"]').val(hdProdIds);
              }            
             
          }
         
        }
      
     
  }
  

  // This function must be called with all modal dialog boxes which have fields that
  // require validation.  It performs a call to the ASP.Net function, "Page_ClientValidate()",
  // and then continues on calling "CloseModalDialog()" if the validation is okay.
  // Note: Surprisingly, companion functions like "ValidatorValidate()" do not work, 
  // perhaps due to the way ThickBox rearranges DOM elements.
  function CheckValidationBeforeClose(element) {
    if (Page_ClientValidate())
      CloseModalDialog(element);
  }


  // Used to close a ThickBox modal dialog and then force a postback,
  // which a server-side control is seemingly unable to do on its own
  // when fired from within the confines of a ThickBox object.
  function CloseModalDialog(element) {

       
tb_remove();
  setTimeout('__doPostBack(\'' + element.name + '\',\'\')', 500);  // 500ms seems to give ThickBox enough time to remove itself
  }


  // Used to close a ThickBox modal dialog without causing a postback.
  function CancelModalDialog() {
    tb_remove();
  }



  // Prepares all textboxes such that the background changes to a distinct
  // color upon focus and then returns to white after focus is lost.
  function PrepareDefaultEventHandlers() {
    $(":text").focus(textboxHighlight).blur(textboxRemoveHighlight);
    $(":password").focus(textboxHighlight).blur(textboxRemoveHighlight);
    $("textarea").focus(textboxHighlight).blur(textboxRemoveHighlight);
  }

  function textboxHighlight() {
    $("#" + this.id).css({ 'background-color': '#ffff40' });
  }

  function textboxRemoveHighlight() {
    $("#" + this.id).css({ 'background-color': 'white' });
  }
