function Hash() {
  this.length = 0;
  this.items = new Array();
  for (var i = 0; i < arguments.length; i += 2) {
    if (typeof(arguments[i + 1]) != 'undefined') {
      this.items[arguments[i]] = arguments[i + 1];
      this.length++;
    }
  }
  this.getItem = function(in_key) {
    return this.items[in_key];
  }
}
// set selectable options
function setOptions(hash, select) {
  var x = 0;
  select.options.length = 0;
  for (var a in hash.items)
    select.options[x++] = new Option(hash.items[a],a);
}

// set select box options based on the selected value of another
function setSelect(select_in, hash_in, hash_out, select_out) {
  var id = select_in.options[select_in.selectedIndex].value;
  var arr = hash_in.getItem(id);
//alert('ARR ' + id + '=' + arr);
  select_out.options.length = 0;
  var x = 0;
  for (var a in arr)
    select_out.options[x++] = new Option(hash_out.getItem(arr[a]),arr[a]);
}

function addWarranty(brand,product,serial,date,btn) {
  warranties.push(new Array(
    brand.options[brand.selectedIndex].value,
    product.options[product.selectedIndex].value,
    serial.value,
    date.value
  ));
  serial.value = '';
  btn.disabled = true;
  displayWarranties();
}
function delWarranty(idx) {
  warranties.splice(idx,1);
  displayWarranties();
}

// display warranties array as a table and also encode as a single string
function displayWarranties() {
  if (warranties.length > 0) {
    var html = '<TABLE BORDER="1"><TR CLASS="form_head"><TD>Brand</TD><TD>Product</TD><TD>Serial</TD><TD>Purchased</TD><TD>DELETE</TD></TR>';
    for (a = 0; a < warranties.length; a++) {
      html += '<TR CLASS="form_data">';
      html += '<TD WIDTH=100>' + brands.getItem(warranties[a][0]) + '</TD>';
      html += '<TD WIDTH=300>' + products.getItem(warranties[a][1]) + '</TD>';
      html += '<TD WIDTH=100>' + warranties[a][2] + '</TD>';
      html += '<TD WIDTH=100>' + warranties[a][3] + '</TD>';
      html += '<TD><INPUT TYPE="BUTTON" VALUE="Remove" ONCLICK="javascript:delWarranty(' + a + ');"/></TD>';
      html += '</TR>';
    }
    html += '</TABLE><BR/>';
    html += '<INPUT TYPE="SUBMIT" NAME="sub1" VALUE="SUBMIT" DISABLED="DISABLED"/>';

    document.form1.warranties.value=warranties.toString();
  }
  else
    html = 'Please Add Warranties using the button above';
  
  document.getElementById('div1').innerHTML=html;
  checkForm();
}
function setTodaysDate(datefield) {
  var x = new Date();
  var m = x.getMonth() + 1;
  if (m < 10)
    m = '0' + m;
  var d = x.getDate();
  if (d < 10)
    d = '0' + d;
  datefield.value = x.getFullYear() + '-' + m + '-' + d;
}
/*
function checkForm() {
  var a = document.form1;
  var e = true;
  if (a.email.value && a.email.value.indexOf('@') < 1)
    e = false;
  if (a.sub1)
    a.sub1.disabled = !(a.firstname.value && a.lastname.value && a.address.value && a.city.value && a.postcode.value && a.phone.value && e && a.store.value && a.salesperson.value);
}
*/
function checkForm() {
  var a = document.form1;
  if (a.sub1)
    a.sub1.disabled = !(a.firstname.value && a.lastname.value && a.address.value && a.city.value && a.postcode.value && a.phone.value && a.store.value && a.salesperson.value);
}
function checkWarrForm() {
  var a = document.form1;
  a.sub2.disabled = !(a.serial.value && a.date.value);
}

