
var poFormR = { fullname : 'Your name',
                   email : 'Email address',
                   phone : 'Phone number' };

var itemmax = 15;
var qtymax = 10;

function formSubmit(formRef) {
  var valid = validate(formRef);
  if (valid) {
    formRef.submitBtn.value = "Please wait ...";
    formRef.submitBtn.disabled = true;
  }
  return(valid);
}

function writeform() {
  for (i = 0; i < itemmax; i++) {
    document.writeln('<div id="item'+i+'" class="item">');
    document.writeln('<select name="pitem'+i+'" class="product" onChange="populatesize('+i+');"><option value="error"></option><option value="">&nbsp;</option><option value="">&nbsp;</option><option value="">&nbsp;</option></select>');
    document.writeln('<select name="psize'+i+'" class="size" onChange="setprodimg((formRef.elements[\'pitem'+i+'\'].selectedIndex - 1),(this.selectedIndex-1));"><option value="error"></option><option value="">&nbsp;</option><option value="">&nbsp;</option><option value="">&nbsp;</option></select>');
    document.writeln('<select name="pqty'+i+'" class="quantity"><option value="error"></option><option value="">&nbsp;</option><option value="">&nbsp;</option><option value="">&nbsp;</option></select>');
    document.writeln('<a href="javascript:delitem('+i+');" title="remove this item"><img align="absmiddle" src="images/btn-del_item.gif" alt="remove this item"></a>');
    document.writeln('<a id="addBtn'+i+'" href="javascript:additem('+(i+1)+');" title="add another item?"><img align="absmiddle" src="images/btn-add_item.gif" alt="add another item?"></a><br />');
    document.writeln('</div>');
  }
}

function initform() {
  formRef = document.forms['poForm'];
//  formRef.fullname.focus();

  for (i = 0; i < itemmax; i++) {
    resetopt(formRef.elements['pitem'+i]);
    resetopt(formRef.elements['pqty'+i]);

    formRef.elements['pitem'+i].options[0] = new Option('select item','');
    for (j = 0; j < items.length; j++) {
      formRef.elements['pitem'+i].options[j+1] = new Option(items[j].name,items[j].name);
    }

    formRef.elements['psize'+i].options[0] = new Option('select size','');
    formRef.elements['psize'+i].disabled = true;

    for (j = 1; j < qtymax; j++) {
      formRef.elements['pqty'+i].options[j-1] = new Option(j,j);
    }
    formRef.elements['pqty'+i].options[qtymax-1] = new Option(qtymax+'+',qtymax+'+');
  }
  additem(0);
}

function additem(rownum) {
  if ((rownum > 0) && ((formRef.elements['pitem'+(rownum-1)].selectedIndex == 0) || (formRef.elements['psize'+(rownum-1)].selectedIndex == 0))) {
    alert('Please finish selecting a product & size before adding a new item!');
  }
  else {
    document.getElementById('item'+rownum).style.display = 'block';
    document.getElementById('addBtn'+rownum).style.display = 'inline';
    if (rownum > 0) { document.getElementById('addBtn'+(rownum-1)).style.display = 'none'; }
    document.images['prodimg'].src = '/images/space.gif';
  }
}

function delitem(rownum) {
  formRef.elements['pitem'+rownum].selectedIndex = 0;
  formRef.elements['psize'+rownum].selectedIndex = -1;
  formRef.elements['psize'+rownum].disabled = true;
  formRef.elements['pqty'+rownum].selectedIndex = 0;

  var totalvis = 0;
  var lastvis = 0;
  for (i = 0; i < itemmax; i++) {
    if (document.getElementById('item'+i).style.display == 'block') {
      totalvis++;
      if (i != rownum) { lastvis = i; }
    }
  }
  if (totalvis > 1) { document.getElementById('item'+rownum).style.display = 'none'; }
  if (rownum > lastvis) {
    document.getElementById('addBtn'+lastvis).style.display = 'inline';
  }
  document.images['prodimg'].src = '/images/space.gif';
}

function populatesize(rownum) {
  var pitemRef = formRef.elements['pitem'+rownum];
  var psizeRef = formRef.elements['psize'+rownum];
  resetopt(psizeRef);
  var pname = pitemRef.options[pitemRef.selectedIndex].value;

  for (var i = 0; i < items[pitemRef.selectedIndex-1].size.length; i++) {
    psizeRef.options[i+1] = new Option(items[pitemRef.selectedIndex-1].size[i]+': '+items[pitemRef.selectedIndex-1].dimension[i],items[pitemRef.selectedIndex-1].size[i]);
  }
  if (pitemRef.selectedIndex == items.length) {
    psizeRef.selectedIndex = 1;
    alert('Please specify details about this custom mat in the comments box.');
  }
  else {
    var itemindex = (formRef.elements['pitem'+rownum].selectedIndex - 1);
    setprodimg(itemindex,0);
    psizeRef.disabled = false;
  }
}

function setprodimg(itemindex,sizeindex) {
  if (itemindex < items.length) {
    document.images['prodimg'].src = getprodimg(itemindex,sizeindex);
  }
}

function getprodimg(itemindex,sizeindex) {
  imgName = items[itemindex].name.replace(/door|table|\W/ig,'').toLowerCase() + '-';
  imgName += items[itemindex].size[sizeindex].replace(/\W/g,'').toLowerCase();
  return('/images/samples/'+imgName+'.gif');
}

function resetopt(objRef) {
  var numopt = objRef.options.length;
  for(j = numopt; j > 0; j--) {
    objRef.options[j] = null;
  }
}
