﻿
//===========================================================================================
//                       Copyright © 2007 The LeanMan, LLC
//===========================================================================================
// File: ShoppingCart.js 
//
// Description: Client Side scripts for editing grids.  Includes checkbox selection and
//              row highlighting.
//
//-------------------------------------------------------------------------------------------
// History                                                   By               Date
//-------------------------------------------------------------------------------------------
// Created - Based on EditGridFunctions                      Daniel Knauf     08/29/2007
//===========================================================================================



  // Procedure: highlightAllCheckedRows
  function highlightAllCheckedRows (strForm, strCheckboxID, strColor) {

    var aryCheckboxes = eval('document.forms.' + strForm + '.' + strCheckboxID);
    if (aryCheckboxes.length == null){
      if(aryCheckboxes.checked){
        highlightCheckedRow(aryCheckboxes,strColor);
      }
    } else {
      var lngCtr;
      for (lngCtr = 0; lngCtr < aryCheckboxes.length; lngCtr++){
        if(aryCheckboxes[lngCtr].checked){
          highlightCheckedRow(aryCheckboxes[lngCtr],strColor);
        }
      }
    }
  }

  // Procedure: checkAllCheckboxesOnFormAndHighlight
  function checkAllCheckboxesOnFormAndHighlight (strForm, strCheckAllName, strColor) {

    var objCheckAll;
    var intCtr;
    
    // Find the checkAll checkbox
    for(intCtr=0; intCtr<(document.forms[strForm].elements.length); intCtr++)
    {
      if (document.forms[strForm].elements[intCtr].id == strCheckAllName)
      {      
        objCheckAll = document.forms[strForm].elements[intCtr];
        break;
      }
    }
    
    // Look for all other checkboxes on this form, and then highlight them
    for(intCtr=0; intCtr<(document.forms[strForm].elements.length); intCtr++)
    {
        if( document.forms[strForm].elements[intCtr].type=="checkbox" && document.forms[strForm].elements[intCtr].id != strCheckAllName )
        {
            // Check and then highlight
            document.forms[strForm].elements[intCtr].checked = objCheckAll.checked
            if (objCheckAll.checked == 0)
            {
                unhighlightRow( document.forms[strForm].elements[intCtr] );
            }
            else
            {
                highlightCheckedRow( document.forms[strForm].elements[intCtr], strColor);
            }

        }
    }
    
  }


  // Procedure: atLeastOneCheckBoxIsChecked
  function atLeastOneCheckBoxIsChecked (strForm) {
  
    var ctrChecked;
    ctrChecked = 0;
    
    // Look for all other checkboxes on this form, and then highlight them
    for(intCtr=0; intCtr<(eval(strForm).elements.length); intCtr++)
    {
        if( eval(strForm).elements[intCtr].type=="checkbox")
        {
            // Check and then highlight
            if (eval(strForm).elements[intCtr].checked == true)
            {
              ctrChecked++;
            }

        }
    }
    
    if (ctrChecked == 0)
    {
        alert('You must select at least one item.');
        return false;
    }
    else
    {
        return true;
    }
        
  }
      

  // Procedure: highlightCheckedRow	
  function highlightCheckedRow (object, color) {

    var tr;
    if (object.parentNode) {
      tr = object.parentNode;
      while (tr.nodeName.toLowerCase() != 'tr')
        tr = tr.parentNode;
    }
    else if (object.parentElement) {
      tr = object.parentElement;
      while (tr.tagName.toLowerCase() != 'tr')
        tr = tr.parentElement;
    }
    if (tr) {
      if (object.checked) {
        tr.oldBackgroundColor = tr.style.backgroundColor;
        tr.style.backgroundColor = color;
      }
      else {
        tr.style.backgroundColor = tr.oldBackgroundColor;
      }
    }
  }


  // Procedure: highlightRow	
  function highlightRow (object, color) {

    // highlight checked row
    var tr;
    if (object.parentNode) {
      tr = object.parentNode;
      while (tr.nodeName.toLowerCase() != 'tr')
        tr = tr.parentNode;
    }
    else if (object.parentElement) {
      tr = object.parentElement;
      while (tr.tagName.toLowerCase() != 'tr')
        tr = tr.parentElement;
    }
    if (tr) {
      tr.oldBackgroundColor = tr.style.backgroundColor;
      tr.style.backgroundColor = color;
    }
  }


  // Procedure: unhighlightRow
  function unhighlightRow (object) {
    var tr;
    if (object.parentNode) {
      tr = object.parentNode;
      while (tr.nodeName.toLowerCase() != 'tr')
        tr = tr.parentNode;
    }
    else if (object.parentElement) {
      tr = object.parentElement;
      while (tr.tagName.toLowerCase() != 'tr')
        tr = tr.parentElement;
    }
    if (tr) {
      tr.style.backgroundColor = '#FFFFFF';
    }
  }