var tempTitles = ["Beep!", "Beep! Beep!", "Mini Musical Monsters", "Musical Monsters", "More Musical Monsters", "Noisy Things", "Pretty Things", "Number Run", "Riff", "Out and About 1 Plus", "Out and About 2 Plus", "Out and About 3", "Out and About 4", "O&A - Assistive Technology", "Dog and Cat Early Literacy", "D&C Letters & Sounds Ph 1", "D&C Letters & Sounds Ph 2", "D&C Letters & Sounds Ph 3"];
var siteTotal = 11;
var productTotal = 0;
var vat = 0;
var mainTotal = 0;
var DELIVERY = 2.50;
var DELIVERYSTRING = "£2.50";
var costTotal = [0, 0];
var preorder = 0;
var firmOrderFound = false;
var tempCost = [];
var emailLines1 = [];
var emailLines2 = [];
var emailLines3 = [];

// Q&D prices
tempCost[0] = [[1, 35], [5, 75],  [10, 125], [11, 175]];
tempCost[1] = [[1, 39], [5, 87], [10, 159], [11, 288]];
tempCost[2] = [[1, 75], [5, 155], [10, 255], [11, 285]];

// Out and About prices
tempCost[3] = [[1, 75], [3, 120], [5, 150], [10, 230], [20, 320], [11, 450]];
tempCost[4] = [[1, 85], [3, 136], [5, 170], [10, 261], [20, 363], [11, 510]];

// Dog and Cat, Pretty Things and Number Run
tempCost[5] = [[1, 45], [5, 93], [10, 153], [11, 200]];

preorderItems = [];

var tempProductCost = [0, 0, 0, 0, 1, 0, 5, 5, 2, 3, 3, 3, 3, 4, 5, 5, 5, 5];
var formFields = ["first_name", "last_name", "job_title", "school", "address1", "address2", "town", "county", "postcode", "tel", "fax", "email", "store"];
var detailFields = ["First name", "Last name", "Job title", "School", "Address line 1", "Address line 2", "Town", "County", "Postcode", "Telephone", "Fax", "E-mail", "Store"];
var productPages = ["beep", "beepbeep", "minimusicalmonsters1", "musicalmonsters1", "moremusicalmonsters1", "noisythings", "prettythings1", "numberrun1", "riff", "outandabout1", "outandabout2", "outandabout3", "outandabout4", "outandabout5", "dogandcatdisc1", "dogandcat_phase1", null, null];

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function showRows(formType) {
  productTotal = 0;
  costTotal = [0, 0];
  preorder = 0;

  if(arguments.length > 2 && arguments[2] == true) {
    var viewOnly = true;
  } else {
    var viewOnly = false;
  }

  emailLines1 = [];
  emailLines2 = [];
  emailLines3 = [];

  var tempProducts = getProducts();
  if(tempProducts != "") {
    tempProducts = tempProducts.split(",");
    //var tempOffer = 0;
    //for(var i = 0; i < tempProducts.length; i += 3) {
       //if(tempProductCost[tempProducts[i]] == 0 && tempProducts[i + 2] == 0) {
         //tempOffer++;
       //}
    //}
    // Adjust for offer price
    //if(tempOffer >= 2) {
      //tempCost[0][0][1] = 30;
    //} else {
      //tempCost[0][0][1] = 35;
    //}
    // Trial order
    var tempPos = 0;
    for(var i = 0; i < tempProducts.length; i += 3) {
      if(tempProducts[i + 1] == 0) {
        showRow(tempProducts[i], tempProducts[i + 1], tempProducts[i + 2], viewOnly, tempPos);
        productTotal++;
      } else {
         // Show that there has been a firm order, as haven't shown rows yet, set backt to 0 later
         costTotal[1] = 1;
      }
      tempPos++;
    }

  if(costTotal[0] > 0) {
      //if(viewOnly == true) {
        //var tempColSpan = 3;
      //} else {
        //var tempColSpan = 4;
      //}
      //document.writeln('<tr height = "5"><td colspan="' + tempColSpan + '"><font color="#FFFFFF" size="0">&nbsp; </font></td></tr>');

      // If also made firm order with trial order
      if(costTotal[1] > 0) {
        var rowSpanCount = 3;
	emailLines1.push("");
        emailLines2.push("Trial order ");
        emailLines3.push("(" + getPrice(costTotal[0]) + ")");
        document.writeln('<tr><td rowSpan=' + rowSpanCount + '><font color="#FFFFFF" face="Arial, Helvetica, sans-serif" size="3">&nbsp;</font></td><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
        showRowEnd(viewOnly);

        vatTrial = (costTotal[0] * 0.20);
        mainTotalTrial = (costTotal[0] + vatTrial);
        vatTrial = ((Math.round(vatTrial * 100) / 100));
        mainTotalTrial = ((Math.round(mainTotalTrial * 100) / 100));
        emailLines1.push("");
        emailLines2.push("Trial order VAT ");
        emailLines3.push("(" + getPrice(vatTrial) + ")");
        document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
        showRowEnd(viewOnly);
	emailLines1.push("");
        emailLines2.push("Trial order total ");
        emailLines3.push("(" + getPrice(mainTotalTrial) + ")");
        document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
        showRowEnd(viewOnly);

        if(viewOnly == true) {
          var tempColSpan = 3;
        } else {
          var tempColSpan = 4;
        }
        document.writeln('<tr><td colspan="' + tempColSpan + '><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">&nbsp; </font></td></tr>');
        emailLines1.push(" ");
        emailLines2.push(" ");
        emailLines3.push(" ");
      }
    }

    costTotal[1] = 0;
    // Firm order
    var tempPos = 0;
    for(var i = 0; i < tempProducts.length; i += 3) {
      if(tempProducts[i + 1] == 1) {
        showRow(tempProducts[i], tempProducts[i + 1], tempProducts[i + 2], viewOnly, tempPos);
        productTotal++;
      }
      tempPos++;
    }
    if(costTotal[1] > 0) {
      //if(viewOnly == true) {
        //var tempColSpan = 3;
      //} else {
        //var tempColSpan = 4;
      //}
      //document.writeln('<tr height = "5"><td colspan="' + tempColSpan + '"><font color="#FFFFFF" size="1">&nbsp;</font></td></tr>');
    }

    rowSpanCount = 5;
    var rowSpanShown = false;

    // Full final part of order details
    if(costTotal[1] > 0) {
      emailLines1.push("");
      emailLines2.push("Firm order ");
      emailLines3.push(getPrice(costTotal[1]));
      if(rowSpanShown == false) {
        rowSpanShown = true;
        document.writeln('<tr><td rowSpan=' + rowSpanCount + '><font color="#FFFFFF" face="Arial, Helvetica, sans-serif" size="3">&nbsp;</font></td><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
        showRowEnd(viewOnly);
      } else {
        document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
        showRowEnd(viewOnly);
      }
    } else if(costTotal[0] > 0) {
      emailLines1.push("");
      emailLines2.push("Trial order ");
      emailLines3.push("(" + getPrice(costTotal[0]) + ")");
      if(rowSpanShown == false) {
        rowSpanShown = true;
        document.writeln('<tr><td rowSpan=' + rowSpanCount + '><font color="#FFFFFF" face="Arial, Helvetica, sans-serif" size="3">&nbsp;</font></td><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
        showRowEnd(viewOnly);
      } else {
        document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="#FF0000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
        showRowEnd(viewOnly);
      }
    }

    // Show following costs in red with brackets if trial order only
    if(costTotal[1] > 0) {
      var priceColour = "#000000";
      var leftBracket = "";
      var rightBracket = "";
    } else {
      var priceColour = "#FF0000";
      var leftBracket = "(";
      var rightBracket = ")";
    }

    emailLines1.push("");
    emailLines2.push("Delivery ");
    emailLines3.push(leftBracket + DELIVERYSTRING + rightBracket);
    document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="' + priceColour + '" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
    showRowEnd(viewOnly);
    if(costTotal[1] > 0) {
      var subtotal = (costTotal[1] + DELIVERY);
    } else {
      var subtotal = (costTotal[0] + DELIVERY);
    }
    vat = (subtotal * 0.20);
    mainTotal = (subtotal + vat);
    vat = ((Math.round(vat * 100) / 100));
    mainTotal = ((Math.round(mainTotal * 100) / 100) + "");
    emailLines1.push("");
    emailLines2.push("Subtotal ");
    emailLines3.push(leftBracket + getPrice(subtotal) + rightBracket);
    document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="' + priceColour + '" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
    showRowEnd(viewOnly);
    emailLines1.push("");
    emailLines2.push("VAT ");
    emailLines3.push(leftBracket + getPrice(vat) + rightBracket);
    document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3">' + emailLines2[emailLines2.length - 1] + '</font></td><td><div align="right"><font color="' + priceColour + '" face="Arial, Helvetica, sans-serif" size="3">' + emailLines3[emailLines3.length - 1] + '</font></div></td>');
    showRowEnd(viewOnly);
    emailLines1.push("");
    emailLines2.push("Total ");
    emailLines3.push(leftBracket + getPrice(mainTotal) + rightBracket);
    document.writeln('<tr><td align="right"><font color="#000000" face="Arial, Helvetica, sans-serif" size="3"><b>' + emailLines2[emailLines2.length - 1] + '</b></font></td><td><div align="right"><font color="' + priceColour + '" face="Arial, Helvetica, sans-serif" size="3"><b>' + emailLines3[emailLines3.length - 1] + '</b></font></div></td>');
    showRowEnd(viewOnly);
  }
}

function showRowEnd(viewOnly) {
  if(viewOnly == true) {
    document.writeln('</tr>');
  } else {
    document.writeln('<td><font color="#FFFFFF" face="Arial, Helvetica, sans-serif" size="3">&nbsp;</font></td></tr>');
  }
}

function showRow(showTitle, showType, showCost, viewOnly, deletePos) {
  if(viewOnly == false) {
    var tempShowTitle = tempTitles[showTitle];
  } else {
    var tempShowTitle = (tempTitles[showTitle] + "&nbsp; &nbsp; &nbsp; ");
  }
  emailLines1.push(tempTitles[showTitle] + "&nbsp; &nbsp;");
  document.writeln('<tr><td><div align="left"><font face="Arial, Helvetica, sans-serif" size="3">' + tempShowTitle + '</font></div></td>');
  document.writeln('<td><div align="left"><font face="Arial, Helvetica, sans-serif" size="3">');
  if(showType == 0) {
    document.writeln('Trial -');
    emailLines2.push(" Trial - ");
  } else {
    document.writeln('Firm order -');
    emailLines2.push(" Firm order - ");
  }
  var tempOption = tempProductCost[showTitle];
  checkPreorder(parseInt(showTitle) + 1);
  if(tempCost[tempOption][showCost][0] == 1) {
    var tempLicence = "single user";
  } else if(tempCost[tempOption][showCost][0] == siteTotal) {
    var tempLicence = "site licence";
  } else {
    var tempLicence = (tempCost[tempOption][showCost][0] + " user");
  }
  emailLines2[emailLines2.length - 1] += tempLicence;
  document.writeln(tempLicence);
  document.writeln('</font></div></td>');
  document.writeln('<td><div align="right">');
  if(showType == 0) {
    document.writeln('<font color="#FF0000" face="Arial, Helvetica, sans-serif" size="3">');
    costTotal[0] += parseInt(tempCost[tempOption][showCost][1]);
  } else {
    document.writeln('<font color="#000000" face="Arial, Helvetica, sans-serif" size="3">');
    costTotal[1] += parseInt(tempCost[tempOption][showCost][1]);
  }
  if(showType == 0) {
    document.writeln('(' + getPrice(tempCost[tempOption][showCost][1]) + ')');
    emailLines3.push("  (" + getPrice(tempCost[tempOption][showCost][1]) + ")");
  } else {
    document.writeln('' + getPrice(tempCost[tempOption][showCost][1]));
    emailLines3.push("  " + getPrice(tempCost[tempOption][showCost][1]));
  }
  document.writeln('</font></div></td>');
  if(viewOnly == false) {
    document.writeln('<td><div align="center"><font size="2" face="Arial, Helvetica, sans-serif"><a style="text-decoration:none" href="" onClick="removeProduct(' + deletePos + ')"><font color="#FF00CC">Delete</font></a></font></td>');
  }
  document.writeln('</tr>');
}

function showTableButtons(formType) {
  var tempProducts = getProducts();
  if(tempProducts != "") {
    if(costTotal[0] > 0 || preorder == 1) {
      document.writeln('<p></p>');
      if(preorder == 1) {
        document.writeln('<div align="left"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif">' + 'Please note that items marked pre-order will be delivered when available.' + '</font></div>');
        if(costTotal[0] > 0) {
          document.writeln('<div align="left"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif">' + '&nbsp; ' + '</font></div>');
        }
      }
      if(costTotal[0] > 0) {
        document.writeln('<div align="left"><font color="#FF0000" size="2" face="Arial, Helvetica, sans-serif">' + getChargeText() + '</font></div>');
      }
    }
    document.writeln('<p></p>');
    document.writeln('<div align="right"><input type="button" name="ContinueOrderButton" value="Continue" onClick="continueOrder()"></div>');
  }
}

function checkPreorder(preorderPos) {
  for(var i = 0; i < preorderItems.length; i++) {
    if(preorderItems[i] == preorderPos) {
      preorder = 1;
      break;
    }
  }
}

function getChargeText() {
  if(costTotal[0] > 0) {
    return("The amounts shown in brackets show the cost only if you decide you want to keep the software after the 28 day trial is over. If you do not wish to keep the software after your trial please ring us to request a SAE. Please note that all trial orders must have a valid school email and street address.");
    //if(costTotal[1] > 0) {
      //return("You will only be charged for the trial items in your order if you decide to keep them after the 28 day trial is over.");
    //} else {
      //return("The trial will be free. All above costs show what the price will be if you do decide to keep the items after the 28 day trial is over.");
    //}
  } else {
    return("");
  }
}

function getChargeTextQD() {
  if(costTotal[0] > 0) {
    if(costTotal[1] > 0) {
      return("Both trial and firm order");
    } else {
      return("Trial order only");
    }
  } else {
    return("Firm order only");
  }
}

function showCostMessage() {
  if(preorder == 1) {
    document.writeln('<div align="left"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif">' + 'Please note that items marked pre-order will be delivered when available.' + '</font></div>');
    if(costTotal[0] > 0) {
      document.writeln('<div align="left"><font color="#000000" size="2" face="Arial, Helvetica, sans-serif">' + '&nbsp; ' + '</font></div>');
    }
  }
  if(costTotal[0] > 0) {
    document.writeln('<div align="left"><font color="#FF0000" size="2" face="Arial, Helvetica, sans-serif">' + getChargeText() + '</font></div>');
  }
}

function continueOrder() {
  // window.location="http://www.q-and-d.co.uk/orderform.htm";
  window.location="orderform.htm";
}

function backToOrder() {
  updateForm();
  window.location="productform.htm";
}

function backToDetails() {
  window.location="orderform.htm";
}

function backToHome() {
  removeDetails();
  window.location="http://www.q-and-d.co.uk/index.htm";
}

function finishOrder() {
  updateForm();
  if(document.form1.first_name.value == "" || document.form1.last_name.value == "" || document.form1.address1.value == "" || document.form1.town.value == ""|| document.form1.postcode.value == "" || document.form1.tel.value == "" || document.form1.email.value == "") {
    window.location="ordererror.htm";
  } else {
    window.location="orderfinish.htm";
  }
}

function showBackProductButton() {
  var tempURL = String(window.location);
  if(tempURL.indexOf("?") != -1) {
    document.writeln('<p><div align="left"><input type="button" name="BackProductButton" value="Back to Catalogue" onClick="backToLastProduct()"></div></p>');
  }
}

function backToLastProduct() {
  var tempURL = String(window.location);
  var lastProduct = tempURL.slice(tempURL.lastIndexOf("?") + 1);
  lastProduct = parseInt(lastProduct.slice(2));
  if(productPages[lastProduct - 1] != null) {
    window.location=(productPages[lastProduct - 1] + ".htm");
  } else {
    window.location="catalogue.htm";
  }
}

function changeLicence() {
  // Show price options
  if(document.form1.select.selectedIndex == 0) {
    var tempIndex = 1;
  } else {
    var tempIndex = (document.form1.select.selectedIndex - 1);
  }
  var tempOption = tempProductCost[tempIndex];
  for(var i = 0; i < tempCost[tempOption].length; i++) {
    if(tempCost[tempOption][i][0] == 1) {
      var tempLicence = "Single user ";
    } else if(tempCost[tempOption][i][0] == siteTotal) {
      var tempLicence = "Site licence ";
    } else {
      var tempLicence = (tempCost[tempOption][i][0] + " user ");
    }
    tempLicence += getPrice(tempCost[tempOption][i][1]);
    document.form1.select3.options[i] = new Option(tempLicence);
  }
  // Hide unused options
  var tempTotal = document.form1.select3.options.length;
  for(var i = tempCost[tempOption].length; i < tempTotal; i++) {
    document.form1.select3.options[document.form1.select3.options.length - 1] = null;
  }
  document.form1.select3.selectedIndex = 0;
}

function showProductForPage() {
  var tempURL = String(window.location);
  var lastProduct = tempURL.slice(tempURL.lastIndexOf("?") + 1);
  if(lastProduct != "") {
    var foundHomeProduct = false;

    // Home user product
    if(lastProduct == "p=0") {
      foundHomeProduct = true;
    } else {
      var tempLastProductList = ["p=a"];
      for(var i = 1; i <= tempLastProductList.length; i++) {
        if(lastProduct == tempLastProductList[i - 1]) {
          lastProduct = ("p=" + i);
          foundHomeProduct = true;
          break;
        }
      }
    }

    lastProduct = parseInt(lastProduct.slice(2));

    if(foundHomeProduct == true) {
      var tempTitles = ["Beep!", "Beep! Beep!", "Mini Musical Monsters", "Noisy Things", "Number Run"];

      // Q&D prices
      tempCost = [];
      tempCost[0] = [[1, 20]];

      var tempProductCost = [0, 0, 0, 0, 0];

      changeLicence();
    }

    if(lastProduct != null && lastProduct != 0) {
      // Academic users selected from product page
      document.form1.select.selectedIndex = lastProduct;
      changeLicence();
    }
  }
}


function setOrder() {
  document.form1.first_name.size=25;
  document.form1.last_name.size=25;
  document.form1.job_title.size=18;
  document.form1.school.size=24;
  document.form1.address1.size=30;
  document.form1.address2.size=30;
  document.form1.town.size=25;
  document.form1.county.size=15;
  document.form1.postcode.size=10;
  document.form1.tel.size=15;
  document.form1.fax.size=15;
  document.form1.email.size=30;
}

function showHiddenRows() {
  //productTotal = 0
  //costTotal = [0, 0];

  //var tempProducts = getProducts();
  //if(tempProducts != "") {
    //tempProducts = tempProducts.split(",");
    //for(var i = 0; i < tempProducts.length; i += 3) {
       //showHiddenRow(tempProducts[i], tempProducts[i + 1], tempProducts[i + 2]);
       //productTotal++;
    //}
  //}
  //if(costTotal[0] > 0) {
    //document.writeln('<input type=hidden name="trialTotal" value="Trial total: ' + getPrice(costTotal[0]) + '">');
  //} else {
    //document.writeln('<input type=hidden name="trialTotal" value=" ">');
  //}
  //if(costTotal[1] > 0) {
    //document.writeln('<input type=hidden name="orderTotal" value="Firm order total: ' + getPrice(costTotal[1]) + '">');
  //} else {
    //document.writeln('<input type=hidden name="orderTotal" value=" ">');
  //}
  //document.writeln('<input type=hidden name="delivery" value="Delivery: ' + DELIVERYSTRING + '">');
  //document.writeln('<input type=hidden name="subtotal" value="Subtotal ' + getPrice(costTotal[0] + costTotal[1] + DELIVERY) + '">');
  //document.writeln('<input type=hidden name="vat" value="VAT ' + getPrice(vat) + '">');
  //document.writeln('<input type=hidden name="trialMessage" value="' + getChargeText() + '">');
  //document.writeln('<input type=hidden name="trialMessageQD" value="' + getChargeTextQD() + '">');
  //document.writeln('<input type=hidden name="total" value="Total: ' + (costTotal[0] + costTotal[1] + DELIVERY + vat) + '">');

  //document.writeln('<input type=hidden name="products" value="' + productTotal + '">');

  document.writeln('<input type=hidden name="line1_0" value="' + emailLines1.length + '">');
  for(var i = 0; i < emailLines1.length; i++) {
    document.writeln('<input type=hidden name="line1_' + (i + 1) + '" value="' + emailLines1[i] + '">');
  }
  document.writeln('<input type=hidden name="line2_0" value="' + emailLines2.length + '">');
  for(var i = 0; i < emailLines2.length; i++) {
    document.writeln('<input type=hidden name="line2_' + (i + 1) + '" value="' + emailLines2[i] + '">');
  }
  document.writeln('<input type=hidden name="line3_0" value="' + emailLines3.length + '">');
  for(var i = 0; i < emailLines3.length; i++) {
    document.writeln('<input type=hidden name="line3_' + (i + 1) + '" value="' + emailLines3[i] + '">');
  }

  var tempDetails = getDetails();
  tempDetails = tempDetails.split(",");
  for(var i = 0; i < (formFields.length - 1); i++) {
    if(tempDetails[i] != "") {
      document.writeln('<input type="hidden" name="' + formFields[i] + '" value = "' + tempDetails[i] + '">');
    } else {
      document.writeln('<input type="hidden" name="' + formFields[i] + '" value = " ">');
    }
  }

  if(tempDetails[tempDetails.length - 1] == "true") {
    document.writeln('<input type="hidden" name="store" value = "Do NOT keep me informed of Q&D products.">');
  } else {
    document.writeln('<input type="hidden" name="store" value = "Keep me informed of Q&D products.">');
  }

  if(preorder == 1) {
    document.writeln('<input type="hidden" name="preorderMessage" value = "true">');
  } else {
    document.writeln('<input type="hidden" name="preorderMessage" value = "false">');
  }

  if(costTotal[0] > 0) {
    document.writeln('<input type="hidden" name="trialMessage" value = "true">');
  } else {
    document.writeln('<input type="hidden" name="trialMessage" value = "false">');
  }

  //if(firmOrderFound == true && document.form1.radio[0].checked == true) {
    //document.writeln('<input type="hidden" name="userRedirect" value = "true">');
  //} else {
    document.writeln('<input type="hidden" name="userRedirect" value = "false">');
  //}
}

function showHiddenRow(showTitle, showType, showCost) {
  var tempOption = tempProductCost[showTitle];
  checkPreorder(parseInt(showTitle) + 1);
  if(showType == 0) {
    var tempType = "Trial -";
    costTotal[0] += parseInt(tempCost[tempOption][showCost][1]);
  } else {
    var tempType = "Firm order -";
    costTotal[1] += parseInt(tempCost[tempOption][showCost][1]);
  }
  if(tempCost[tempOption][showCost][0] == 1) {
    var tempLicence = "single user";
  } else if(tempCost[tempOption][showCost][0] == siteTotal) {
    var tempLicence = "site licence";
  } else {
    var tempLicence = (tempCost[tempOption][showCost][0] + " user");
  }
  document.writeln('<input type=hidden name="product' + (productTotal + 1) + '" value="' + tempTitles[showTitle] + ' ' + tempType + ' ' + tempLicence + ' ' + getPrice(tempCost[tempOption][showCost][1]) + '">');
}

function getPrice(price) {
  price = String(price);
  var tempIndex = price.indexOf(".");
  if(tempIndex == -1) {
    price += ".00";
  } else {
    var tempPointLength = (price.slice(tempIndex + 1)).length;
    if(tempPointLength == 0) {
      price += "00";
    } else if(tempPointLength == 1) {
      price += "0";
    }
  }
  return("£" + price);
}

function getProducts() {
  var tempProducts = document.cookie.split(";");
  for(var i = 0; i < tempProducts.length; i++) {
    if(tempProducts[i].indexOf("qdproduct") != -1) {
      if(tempProducts[i].indexOf("=") != -1) {
        tempProducts = (tempProducts[i].split("="))[1];
        return(tempProducts);
      }
    }
  }
  return("");
}

function removeDetails() {
  if(document.cookie != undefined && document.cookie != "") {
    document.cookie = "qdproduct=";
    document.cookie = "qddetails=";
  }
}

function addProduct() {
  if(document.form1.select.selectedIndex > 0) {
    var tempProducts = getProducts();
    if(tempProducts != "") {
      tempProducts += ",";
    }
    tempProducts += ((document.form1.select.selectedIndex - 1) + "," + document.form1.select2.selectedIndex + "," + document.form1.select3.selectedIndex);
    document.cookie = ("qdproduct=" + tempProducts);
  }
  window.location.reload();
}

function removeProduct(productPos) {
  var tempProducts = getProducts();
  if(tempProducts != "") {
    tempProducts = tempProducts.split(",");
    tempProducts.splice(productPos * 3, 3);
    tempProducts = tempProducts.join(",");
    document.cookie = ("qdproduct=" + tempProducts);
    window.location.reload();
  }
}

function submitOrder() {
  onClick="updateForm()"

  if(firmOrderFound == true) {
    if(document.form1.radio[0].checked == true) {
      document.form1.userRedirect.value = "true";
    }
  }

  document.form1.submit();
}

function testCookie() {
  if(document.cookie == undefined || document.cookie == "") {
    document.cookie = "qdproduct=";
  }
  if(document.cookie == undefined || document.cookie == "") {
    //window.location="http://www.q-and-d.co.uk/productsorder.htm";
    window.location="productsorder.htm";
  }
}

function getDetails() {
  var tempDetails = document.cookie.split(";");
  for(var i = 0; i < tempDetails.length; i++) {
    if(tempDetails[i].indexOf("qddetails") != -1) {
      if(tempDetails[i].indexOf("=") != -1) {
        tempDetails = (tempDetails[i].split("="))[1];
        tempDetails = unescape(tempDetails);
        return(tempDetails);
      }
    }
  }
  return("");
}

function showForm() {
  var formDetails = getDetails();
  if(formDetails != "") {
    formDetails = formDetails.split(",");
    for(var i = 0; i < formDetails.length; i++) {
      if(formFields[i] == "store") {
        if(formDetails[i] == "true") {
          document.form1[formFields[i]].checked = true;
        } else {
          document.form1[formFields[i]].checked = false;
        }
      } else {
        document.form1[formFields[i]].value = formDetails[i];
      }
    }
  }
}

function updateForm() {
  var formDetails = "";
  for(var i = 0; i < formFields.length; i++) {
    var checkValues = [["¬", ""], ["`", ""], ["^", ""], [",", ""], [";", ""], ["=", ""]];
    if(formFields[i] == "store") {
      tempValue = "" + escape(String(document.form1[formFields[i]].checked));
    } else {
      tempValue = document.form1[formFields[i]].value;
      for(var j = 0; j < checkValues.length; j++) {
        var tempPos = tempValue.indexOf(checkValues[j][0]);
        while(tempPos != -1) {
          tempValue = (tempValue.slice(0, tempPos) + checkValues[j][1] + tempValue.slice(tempPos + 1)); 
          tempPos = tempValue.indexOf(checkValues[j][0]);
        }
      }
      tempValue = escape(tempValue);
    }
    if(i > 0) {
      formDetails += ",";
    }
    formDetails += tempValue;
    document.cookie = ("qddetails=" + formDetails);
  }
}

function showOrderOptions() {
  firmOrderFound = false;
  var tempProducts = getProducts();
  if(tempProducts != "") {
    tempProducts = tempProducts.split(",");

    for(var i = 0; i < tempProducts.length; i += 3) {
      if(tempProducts[i + 1] == 1) {
        firmOrderFound = true;
        break;
      }
    }

    if(firmOrderFound == true) {
      document.writeln('<table width="475" cellpadding="0" cellspacing="0">');
      document.writeln('<tr>');
      document.writeln('<td><label>');
      document.writeln('<input type="radio" name="radio">Pay now by credit card / PayPal</label></td><td>');
      document.writeln('<!-- PayPal Logo --><table border="0" cellpadding="10" cellspacing="0" align="center"><tr><td align="center"></td></tr>');
      document.writeln('<tr><td align="center"><a href="#" onclick="javascript:window.open(\'https://www.paypal.com/us/cgi-bin/webscr?cmd=xpt/cps/popup/OLCWhatIsPayPal-outside\',\'olcwhatispaypal\',\'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=350\');"><img  src="https://www.paypal.com/en_US/i/bnr/horizontal_solution_PP.gif" border="0" alt="Solution Graphics"></a></td></tr></table><!-- PayPal Logo -->');
      document.writeln('</td></tr>');
      document.writeln('<tr>');
      document.writeln('<td colspan="2"><label>');
      document.writeln('<input name="radio" type="radio" checked>Send me an invoice for the amount owed</label></td>');
      document.writeln('</tr>');
      document.writeln('</table>');
    }
  }
}

function showForm2() {
  firmOrderFound = false;
  var tempProducts = getProducts();
  if(tempProducts != "") {

    tempProducts = tempProducts.split(",");

    for(var i = 0; i < tempProducts.length; i += 3) {
      if(tempProducts[i + 1] == 1) {
        firmOrderFound = true;
        break;
      }
    }

    if(firmOrderFound == true) {
      document.writeln('<form name="form2" action="https://www.paypal.com/cgi-bin/webscr" method="post">');
      document.writeln('<input type="hidden" name="cmd" value="_cart">');
      document.writeln('<input type="hidden" name="upload" value="1">');
      document.writeln('<input type="hidden" name="business" value="info@q-and-d.co.uk">');
      document.writeln('<input type="hidden" name="currency_code" value="GBP">');
      document.writeln('<input type="hidden" name="first_name" value="' + document.form1.first_name.value + '">');
      document.writeln('<input type="hidden" name="last_name" value="' + document.form1.last_name.value + '">');
      document.writeln('<input type="hidden" name="address1" value="' + document.form1.address1.value + '">');
      document.writeln('<input type="hidden" name="address2" value="' + document.form1.address2.value + '">');
      document.writeln('<input type="hidden" name="city" value="' + document.form1.town.value + '">');
      document.writeln('<input type="hidden" name="state" value="' + document.form1.county.value + '">');
      document.writeln('<input type="hidden" name="zip" value="' + document.form1.postcode.value + '">');
      document.writeln('<input type="hidden" name="night_phone_b" value="' + document.form1.tel.value + '">');
      document.writeln('<input type="hidden" name="email" value="' + document.form1.email.value + '">');
      document.writeln('<input type="hidden" name="return" value="http://www.q-and-d.co.uk/orderreply2.htm">');
      document.writeln('<input type="hidden" name="cancel_return" value="http://www.q-and-d.co.uk/orderreplycancel.htm">');
      document.writeln('<input type="hidden" name="handling_cart" value="' + DELIVERY + '">');

      var tempTaxTotal = 0;
 
      // Show individual items
      var tempItemPos = 1;
      for(var i = 0; i < tempProducts.length; i += 3) {
        var tempItemName = tempTitles[tempProducts[i]];
        var tempOption = tempProductCost[tempProducts[i]];
        if(tempProducts[i + 1] == 0) {
          tempItemName += " TRIAL";
        }
        if(tempCost[tempOption][tempProducts[i + 2]][0] == 1) {
          tempItemName += " single user";
        } else if(tempCost[tempOption][tempProducts[i + 2]][0] == siteTotal) {
          tempItemName += " site licence";
        } else {
          tempItemName += (" " + tempCost[tempOption][tempProducts[i + 2]][0] + " user");
        }
        document.writeln('<input type="hidden" name="item_name_' + tempItemPos + '" value="' + tempItemName + '">');
	if(tempProducts[i + 1] == 0) {
          // Trial order
          document.writeln('<input type="hidden" name="amount_' + tempItemPos + '" value="' + 0.00 + '">');
	} else {
          // Firm order
          document.writeln('<input type="hidden" name="amount_' + tempItemPos + '" value="' + tempCost[tempOption][tempProducts[i + 2]][1]  + '">');
          tempTaxTotal += parseInt(tempCost[tempOption][tempProducts[i + 2]][1]);
	}
        tempItemPos++;
      }

      tempTaxTotal = (tempTaxTotal + DELIVERY);
      tempTaxTotal = (tempTaxTotal * 0.20);
      var tempTaxTotalVat = ((Math.round(tempTaxTotal * 100) / 100));

      document.writeln('<input type="hidden" name="tax_cart" value="' + tempTaxTotalVat + '">');

      //var tempCustomLine = ("Phone: " + document.form1.tel.value + "   ");
      //tempCustomLine += ("Fax: " + document.form1.fax.value + "   ");
      //tempCustomLine += ("Job title: " + document.form1.job_title.value + "   ");
      //tempCustomLine += ("School: " + document.form1.school.value + "   ");
      //tempCustomLine += ("Q&D info: " + document.form1.store.value + "   ");
      //document.writeln('<input type="hidden" name="custom" value="' + tempCustomLine + '">');

      document.writeln('</form>');
    }
  }
}

function openPrint() {
  window.open('printform.htm');
  //window.open('printform.htm', menubar='0');
}

function openConditions() {
  window.open('conditions.htm');
}

function printForm() {
  window.print();
}

function showDetails() {
  var tempDetails = getDetails();
  tempDetails = tempDetails.split(",");
  document.writeln('<div align="right">');
  
  if(arguments.length > 0 && arguments[0] == true) {
    document.writeln('<p>');
    for(var i = 0; i < (formFields.length - 1); i++) {
      if(tempDetails[i] != "") {
        document.writeln('<font face="Arial, Helvetica, sans-serif" size="3" align="right"><b>' + detailFields[i] + ':</b> ' + tempDetails[i] + '</font><br>');
      }
    }
    document.writeln('<br>');
  } else {
    for(var i = 0; i < (formFields.length - 1); i++) {
      if(tempDetails[i] != "") {
        document.writeln('<p><font face="Arial, Helvetica, sans-serif" size="3" align="right"><b>' + detailFields[i] + ':</b> ' + tempDetails[i] + '</font></p>');
      }
    }
  }

  document.writeln('<font face="Arial, Helvetica, sans-serif" size="2" align="right">');
  if(tempDetails[tempDetails.length - 1] == "true") {
    document.writeln('Do NOT keep me informed of Q&D products.</font></p>');
  } else {
    document.writeln('Keep me informed of Q&D products.</font></p>');
  }
  document.writeln('</div>');
}

function leaveReset(leaveLocation) {
  removeDetails();
  window.location=leaveLocation;
}

function closeCurrentWindow() {
  window.close();
}

