/////////////////////////////////////
//  Add a flute to the shopping cart
//
function addToCart(serial, tuningPitch, material)
{
  var ajax          = new Ajax();
  var data          = "serial=" + serial + "&tuning=" + tuningPitch + "&material=" + material;
  var handlerScript = "ajaxAddToCart.php";

  //  What to do with the returned data
  var action = function(data)
  {
    document.getElementById('cartCount').innerHTML = data;
    return;
  };

  ajax.doPost(handlerScript, data, action);
  return;
}

////////////////////////////////////////////////////////////////////////
//  Remove an item from the shopping cart and recalculate the total due
//
function removeItem(serial)
{
  var ajax          = new Ajax();
  var data          = "serial=" + serial;
  var handlerScript = "ajaxRemoveFromCart.php";
  
  //  What to do with the returned data
  var action = function(data)
  {
    items = data.split("+");
    //alert(items[0] + ' - ' + items[1] + ' - ' + items[2] + "_");
    
    if(items[0] < 1)
      window.location = "flutes.php";

    total = items[2].substring(0, items[2].length - 1);
    total = "$" + total + ".00";
    document.getElementById('fluteCount').innerHTML = items[0] + " flute(s):";
    document.getElementById(items[1]).style.display = "none";
    document.getElementById('cartTotal').innerHTML  = total;
    return;
  };

  ajax.doPost(handlerScript, data, action);
  return;
}

///////////////////////////////////////////////////////////////////////////////////////
//  Make the shipping information the same as the billing information for the customer
//
function sameAsBill(checked)
{
  var billFields = new Array('fldBillingAddr1', 'fldBillingAddr2', 'fldBillingCity', 'fldBillingState', 'fldBillingCountry', 'fldBillingZip');
  var shipFields = new Array('fldShippingAddr1', 'fldShippingAddr2', 'fldShippingCity', 'fldShippingState', 'fldShippingCountry', 'fldShippingZip');

  for(var i = 0; i < billFields.length; i++)
  {
    if(checked == true)
      inputValue = document.getElementById(billFields[i]).value;
    else
      inputValue = '';
      
    document.getElementById(shipFields[i]).value = inputValue;
  }
  return;
}

//////////////////////////////////////
//  "Create login" section for 
//    purchasing customer
//
function togglePassword(id)
{
  value = document.getElementById(id).checked;
  
  if(value == true)
  {
    document.getElementById("fldPassword").disabled = false;
    document.getElementById("password2").disabled = false;
  }
  else
  {
    document.getElementById("fldPassword").disabled = true;
    document.getElementById("password2").disabled = true;          
  }
  return;
}

//////////////////////////////////////
//  Check required fields on ckeckout
//  form
//
function checkCheckOut()
{
  var submit = true;
  var fields = new Array('fldFirstName', 'fldLastName', 'fldBillingAddr1', 'fldBillingCity', 'fldBillingState', 'fldBillingCountry', 'fldBillingZip', 'fldShippingAddr1', 'fldShippingCity', 'fldShippingState', 'fldShippingCountry', 'fldShippingZip', 'fldPhone1', 'fldEmail', 'fldCCNum', 'expMonth', 'expYear', 'ccvNumber');
  
  for(var i = 0; i < fields.length; i++)
  {
    if(!document.getElementById(fields[i]).value)
    {
      alert('You have one or more fields missing required information.');
      submit = false;
      break;
    }
  }

  if(submit == true)
    document.forms['checkOut'].submit();
  else
    return;
}

//////////////////////////////////////
//  Retrieve the customer information
//
function loginCustomer(formName)
{
  var ajax          = new Ajax();
  var data          = "email=" + document.getElementById("emailLogin").value;
  data             += "&password=" + document.getElementById("pwLogin").value;
  var handlerScript = "ajaxLogin.php";

  //  What to do with the returned customer data
  var action = function(customer)
  {
    if(customer)
    {
      data = customer.split("|");
      //  Populate the form
      for(var i = 0; i < data.length; i++)
      {
        info = data[i].split(":");
        document.getElementById(info[0]).value = info[1];
      }
    }
    else
    {
      //  login failed
    }
    return;
  };

  ajax.doPost(handlerScript, data, action);
  return;
}

