function PopulateList (item, vQuantity) {
 vSCGroup  = $('.scinfo-group');
 vSCItem   = vSCGroup.find('.' + item);
 vQuantity = vQuantity * 1;
 itemforspan = item.replace(/ddoott/g, ".");

 if (vSCItem.html() != null) {
  vCountSpan = vSCItem.children('span');

  if (vQuantity < 1) {
   //vThisDiv  = vThisForm.parent().remove();
   //vThisDiv.remove();
   $('div.scinfo-group div.' + item).remove();
   RemoveItem(item);
  } else {
   vCountSpan.html(vQuantity + " X");
  }
 } else {
  vNewItem = "<div class=\"scinfo-line " + item + "\"><img class=\"scinfo-del\" onclick=\"modquant('" + item + "')\" src=\"/images/edit.png\" /><input id=\"ed" + item + "\" class=\"val\" value=\"" + vQuantity + "\" /><span id=\"qty" + item + "\" class=\"scinfo-count\">" + vQuantity + " X</span>" + itemforspan + "</div>";

  /* vNewItem = "<div class=\"scinfo-line " + item + "\"><img class=\"scinfo-del\" onclick=\"ShowQuantity(this)\" src=\"/images/edit.png\" /><span class=\"scinfo-count\">" + vQuantity + " X</span>" + item + "<form class=\"scquantity\" onsubmit=\"return false;\">Quantity<br><input class=\"val\" /><br /><br /><input type=\"image\" title=\"Add Quantity\" src=\"images/add.png\" onclick=\"AddItem('" + item + "',this, 'add')\" class=\"edbutton\" border=\"none\" />&nbsp;&nbsp;<input type=\"image\" onclick=\"AddItem('" + item + "',this,'sub')\" title=\"Subtract Quantity\" src=\"images/subtract.png\" class=\"edbutton\" border=\"none\" />&nbsp;&nbsp;<input type=\"image\" onclick=\"SCDelete(this,'" + item + "')\" title=\"Delete Item\" src=\"images/remove.png\" class=\"edbutton\" border=\"none\" /></form></div>"; */

  vSCGroup.append(vNewItem);

  $('#ed' + item).keypress(function(event) {
   if (event.keyCode == '13') {
     modquant(item);
   }
  });
 }
}

function AddEasy (vSetup) {
 aItems = [];
 vOut = "";

  if (vSetup) {
   if ($.cookie('sceqvals')) {
    aItems = $.cookie('sceqvals').split(":");

    for (i in aItems) {
     aWork = aItems[i].split("|");
 
     if (aWork.length > 1) {
      vOut = vOut + ":" + aWork[1] + " X " + aWork[0] + "\n";

      PopulateList(aWork[0], aWork[1]);
     }
    }

    vOut = vOut.replace(/:/g,"");
    vOut = vOut.replace(/ddoott/g,".");

    if ($('#Quote').val() == "Specify: P/N & Qty.") {
     $('#Quote').val("");
    }
    
    //--- true additive functionality
    //vOut = $('#Quote').val() + vOut
 
    $('#Quote').val(vOut);

   $('table.footcollapse tbody').slideDown('fast')
   $('table.footcollapse tfoot tr td a img').attr('src', 'http://www.sealconusa.com/images/easyquote/collapse.gif')
  }

 }
}

function ShowQuantity (obj) {

  vThisForm = $(obj).parent().children('form');

  if (vThisForm.is(':visible')) {
   vThisForm.slideUp("fast");
  } else {
   vThisForm.slideDown("fast");
   vThisForm.children('input').focus();
  }
}

function RemoveItem (item) {
 aItems = [];
 vOut = "";

 aItems = $.cookie('sceqvals').split(":");

 for (i in aItems) {
  aWork = aItems[i].split("|");
 
  if (aWork.length > 1) {
   if (aWork[0] != item) {
    vOut = vOut + ":" + aWork[0] + "|" + aWork[1];
   }
  }
 }

 if (vOut[0] == ":") {
  vOut = vOut.substring(1);
 }

 $.cookie('sceqvals', vOut);
}

function AddItem (item,obj,op) {
 vThisForm  = $(obj).parent();
 vThisInput = vThisForm.children('input');
 aItems = [];
 vOut = "";
 item = item.toString().replace(/\./g, "ddoott");

 if (vThisInput.val() == "" || isNaN(vThisInput.val())) {
  alert("The quantity cannot be empty and must be a number");
  vThisInput.focus();
  return false;
 } else {
  vQuantity = vThisInput.val() * 1;

  if (op == 'sub') {
   vQuantity = vQuantity * -1;
  }
 }

 if ($.cookie('sceqvals')) {
  aItems = $.cookie('sceqvals').split(":");

  for (i in aItems) {
   aWork = aItems[i].split("|");

   if (aWork.length > 1) {
    if (aWork[0] == item) {
     //--- is here, does match, with cookie ---
     if (op == 'replace') {
      vNewQuantity = vQuantity;
     } else {
      vCurrentQuantity = aWork[1] * 1;
      vNewQuantity     = vCurrentQuantity + vQuantity;
     }


     vOut = vOut + ":" + aWork[0] + "|" + vNewQuantity;


     PopulateList(aWork[0], vNewQuantity);
    } else {
    //--- is here, with cookie ---
    vOut = vOut + ":" + aWork[0] + "|" + aWork[1];
    PopulateList(aWork[0], aWork[1]);
    }
   }
  } //---
 } else {
  //--- not here, no cookie ---
  vOut = vOut + ":" + item + "|" + vQuantity;
  PopulateList(item, vQuantity);
 }

 if (vOut.indexOf(item) == -1) {
  //--- not here, post cookie ---
  vOut = vOut + ":" + item + "|" + vQuantity;
  PopulateList(item, vQuantity);
 }

 if (vOut[0] == ":") {
  vOut = vOut.substring(1);
 }

 $.cookie('sceqvals', vOut);

 $('#scinfo').slideDown("fast");
 ShowQuantity(vThisForm);
 AddEasy();
}

function SCDelete (obj, item) {
  doit = confirm("Should the system delete " + item + " from your list of items to quote?");

  if (!doit) {
   return;
  }

 vParentDiv = $(obj).parent().parent();
 vParentDiv.remove();
 RemoveItem(item);
 AddEasy();
}

function modquant(part) {
  if ($('#qty' + part).is(":visible")) {
   $('#qty' + part).fadeOut('fast', function () {
    $('#ed' + part).fadeIn('fast', function () {
     $('#ed' + part).focus();
     $('div.' + part + ' img.scinfo-del').attr('src', '/images/apply.png');
    });
   });

  } else {
   PopulateList(part, $('#ed' + part).val());

   if ($('#ed' + part).val() > 0) {
    AddItem(part, $('#ed' + part), 'replace');
   }

   //--- use for auto quote update ---
   //AddEasy(1);

   $('#ed' + part).fadeOut('fast', function () {
    $('#qty' + part).fadeIn('fast');
    $('div.' + part + ' img.scinfo-del').attr('src', '/images/edit.png');
   });
  }
}

