/*
 * Copyright (c) 2002-2007 Julien WILK (http://LDDb.com/)
 * Not to be modified/reused without authorization.
 */

var CheckedCount = 0;

function check_export(thisForm)
{
	if(checked_submit == 'export' || checked_submit == 'delete')
	{
		if(!CheckedCount)
		{
			alert('Please select one or more titles first.');
		  return false;
		}
		else
			return true;
	}
	else // list/all
		return true;
}

function UpdateAll(thisForm,boxname,check_all)
{
  var elts = document.forms[thisForm].elements;
  var AllChecked = true;
  CheckedCount = 0

  if (elts)
  {
    for (i=0; i<elts.length; i++)
    {
      if((elts[i].type == 'checkbox') && (elts[i].name.substr(0,boxname.length) == boxname))
     	{
   			AllChecked = AllChecked && elts[i].checked;
     		if(elts[i].checked)
     			CheckedCount++;
      }
    }
  }
  if(AllChecked)
    $(check_all).checked = true;
  else
    $(check_all).checked = false;
}

function CheckAll(thisForm,boxname,cond)
{
  var elts = document.forms[thisForm].elements;
  if (elts)
  {
    for (i=0; i<elts.length; i++)
    {
      // alert("elt = "+elts[i].value);
      if ((elts[i].type == 'checkbox') && (elts[i].name.substr(0,boxname.length) == boxname))
      {
        elts[i].checked = cond;
      }
    }			
  }
  if(cond)
    CheckedCount++; // just to make sure it's >0
  else
  	CheckedCount = 0;
}	

function MergeAll(thisForm,cond)
{
  var elts = document.forms[thisForm].elements;
  if (elts)
  {
    for (i=0; i<elts.length; i++)
    {
      if (elts[i].type == 'radio')
      {
        if(elts[i].value == cond)
        {
          // alert("elt = "+elts[i].value);
          elts[i].checked = true;
        }
      }
    }			
  }
}	

function display_collwish(this_form,toggle,target,checkname,button)
{
  if($(target))
  {
    switch(toggle)
    {
      case 'show':
        ($(target+'_show')).style.display = 'none';
        ($(target+'_hide')).style.display = '';
        ($(target)).style.display = '';
        if($(target+'_sep'))
          ($(target+'_sep')).style.display = '';
        ($(button)).style.display = '';

        var elts = document.forms[this_form].elements;
        if (elts)
        {
          for (i=0; i<elts.length; i++)
          {
            //alert(elts[i].name);
            if (elts[i].name.substr(0,checkname.length+1) == checkname+'_')
            {
              //alert('xxx_'+elts[i].name.substr(5));
              ($(target+'_'+elts[i].name.substr(checkname.length+1))).style.display = '';
            }
          }			
        }
      break;
      
      case 'hide':
  
        ($(target+'_show')).style.display = '';
        ($(target+'_hide')).style.display = 'none';
        ($(target)).style.display = 'none';
        if($(target+'_sep'))
          ($(target+'_sep')).style.display = 'none';
        ($(button)).style.display = 'none';
      
        var elts = document.forms[this_form].elements;
        if (elts)
        {
          for (i=0; i<elts.length; i++)
          {
            //alert(elts[i].name);
            if (elts[i].name.substr(0,checkname.length+1) == checkname+'_')
            {
            	//alert('collwish_'+elts[i].name.substr(5));
              ($(target+elts[i].name.substr(checkname.length))).style.display = 'none';
            }
          }			
        }
      break;
    }
  }
}

function display_notes(this_form,toggle,target,id,button)
{
  switch(toggle)
  {
    case 'show':
// visuals feedback
      $(button+'_updated_'+id).style.display = 'none';
      $(button+'_updating_'+id).style.display = 'none';
      $(button+'_'+id).style.display = '';

// visuals controls
      $(target+'_'+id+'_show').style.display = 'none';
      $(target+'_'+id+'_hide').style.display = '';
      $(target+'_input_'+id).style.display = '';
      $(target+'_'+id).style.display = 'none';

// focus
      $('NOTES_NEW_'+id).focus();
    break;
    
    case 'hide':
      ($(button+'_'+id)).style.display = 'none';
      ($(target+'_'+id+'_show')).style.display = '';
      ($(target+'_'+id+'_hide')).style.display = 'none';
      ($(target+'_input_'+id)).style.display = 'none';
// Hide -> cancel, we need the previous value
      ($('NOTES_NEW_'+id)).value = ($('NOTES_OLD_'+id)).value;
      ($(target+'_'+id)).style.display = '';
    break;
  }
}


function display_inventory(toggle,id)
{
  switch(toggle)
  {
    case 'show':
      ($('inv_'+id+'_modify')).style.display = '';
      ($('inv_'+id+'_show')).style.display = 'none';
      ($('inv_'+id+'_hide')).style.display = '';
// Description
      ($('inv_desc_show_'+id)).style.display = 'none';
      ($('inv_desc_edit_'+id)).style.display = '';
// Price
      ($('inv_price_show_'+id)).style.display = 'none';
      ($('inv_price_edit_'+id)).style.display = '';
// Quantity
      ($('inv_qty_show_'+id)).style.display = 'none';
      ($('inv_qty_edit_'+id)).style.display = '';
// Condition
      ($('inv_cond_show_'+id)).style.display = 'none';
      ($('inv_cond_edit_'+id)).style.display = '';
    break;
    
    case 'hide':
      $('inv_'+id+'_modify').style.display = 'none';
      $('inv_'+id+'_show').style.display = '';
      $('inv_'+id+'_hide').style.display = 'none';
    	$('inv_'+id+'_delete').style.display = '';
    	$('inv_'+id+'_update').style.display = 'none';
// Description
      ($('inv_desc_edit_'+id)).style.display = 'none';
      ($('DESC_NEW_'+id)).value = ($('DESC_OLD_'+id)).value; // Hide -> restore
      ($('inv_desc_show_'+id)).style.display = '';
// Description
      ($('inv_price_edit_'+id)).style.display = 'none';
      ($('PRICE_NEW_'+id)).value = ($('PRICE_OLD_'+id)).value; // Hide -> restore
      ($('inv_price_show_'+id)).style.display = '';
// Quantity
      ($('inv_qty_edit_'+id)).style.display = 'none';
      ($('QTY_NEW_'+id)).value = ($('QTY_OLD_'+id)).value; // Hide -> restore
      ($('inv_qty_show_'+id)).style.display = '';
// Condition
      ($('inv_cond_edit_'+id)).style.display = 'none';
      ($('COND_NEW_'+id)).value = ($('COND_OLD_'+id)).value; // Hide -> restore
      ($('inv_cond_show_'+id)).style.display = '';
    break;
  }
}

function update_inventory(id)
{
  var old_desc,  new_desc;
  var old_price, new_price;
  var old_qty,   new_qty;
  var old_cond,  new_cond;

  var error = false;

  old_desc = $('DESC_OLD_'+id).value;
  new_desc = $('DESC_NEW_'+id).value;

  old_price = $('PRICE_OLD_'+id).value;
  new_price = $('PRICE_NEW_'+id).value;

  old_qty = $('QTY_OLD_'+id).value;
  new_qty = $('QTY_NEW_'+id).value;

  old_cond = $('COND_OLD_'+id).value;
  new_cond = $('COND_NEW_'+id).value;


  if((new_desc!=old_desc)   ||
     (new_price!=old_price) ||
     (new_qty!=old_qty)     ||
     (new_cond!=old_cond))
  {
    if(!IsNumeric(new_price) || (new_price == 0))
    {
      new Effect.Highlight('PRICE_NEW_'+id,{startcolor: '#FF0000'});
    	$('PRICE_NEW_'+id).value = $('PRICE_OLD_'+id).value;
    	error = true;
    }
    if(!IsInteger(new_qty))
    {
      new Effect.Highlight('QTY_NEW_'+id,{startcolor: '#FF0000'});
    	$('QTY_NEW_'+id).value = $('QTY_OLD_'+id).value;
    	error = true;
    }
    if(!IsInteger(new_cond))
    {
      new Effect.Highlight('COND_NEW_'+id,{startcolor: '#FF0000'});
    	$('COND_NEW_'+id).value = $('COND_OLD_'+id).value;
    	error = true;
    }
  }

	if(!error)
	{
    //$("updating").style.display = '';

    $("inv_"+id+"_modify").style.display = 'none';
    $("inv_"+id+"_hide").style.display = 'none';
    $("inv_"+id+"_delete").style.display = 'none';
    $("inv_"+id+"_update").style.display = '';
    sajax_update_inventory(id,new_desc,new_price,new_qty,new_cond,update_inventory_cb);
  }
}

function update_inventory_cb(contents)
{
  if(contents == false)
  {
    alert('Problem/bug!');
    return 0;
  }

  var param = contents.split('|');
  var id    = param[0];
  var desc  = param[1];
  var price = param[2];
  var qty   = param[3];
  var cond  = param[4];

  $('DESC_OLD_'+id).value = desc;
  $('inv_desc_show_'+id).innerHTML = desc;

  $('PRICE_OLD_'+id).value = price;
  $('inv_price_show_'+id).innerHTML = '<b>$'+price+'</b>';

  $('QTY_OLD_'+id).value = qty;
  $('inv_qty_show_'+id).innerHTML = '<b>'+qty+'</b>';

  $('COND_OLD_'+id).value = cond;
  $('inv_cond_show_'+id).innerHTML = '<b>'+cond+'</b>';

  $('inv_cond_image_'+id).src = $('inv_cond_image_'+id).src.replace(/\d+/,cond);
  display_inventory('hide',id);

  $('PRICE_NEW_'+id).value = price;
  $('QTY_NEW_'+id).value = qty;
  $('COND_NEW_'+id).value = cond;
}

/************************************/

function IsNumeric(strString)
//  check for valid numeric strings	
{
  var strValidChars = "0.123456789";
  var strChar;
  
  if (strString.length == 0) return 0;
  
  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length; i++)
    {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
      return false;
    }
  }
  if(strString*1 == 0)
    return false;
  else
    return true;
}

function IsInteger(strString)
//  check for valid numeric strings	
{
  var strValidChars = "0123456789";
  var strChar;
  
  if (strString.length == 0) return 0;
  
  //  test strString consists of valid characters listed above
  for (i = 0; i < strString.length; i++)
    {
    strChar = strString.charAt(i);
    if (strValidChars.indexOf(strChar) == -1)
    {
      return false;
    }
  }
  if(strString*1 == 0)
    return false;
  else
    return true;
}
