function checkSubmission(){
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering checkSubmission()<BR>");
  // }
  // alert ("Returning focus to " + lastField.name);
  lastField.focus();
  submissionOK = (canSubmit || (navigator.userAgent.indexOf("Safari") != -1)) && blnLengthOK;
  return submissionOK;
}

function enableSubmission(formElement){
  // alert ("Submission enabled on " + formElement.name);
  lastField = formElement;
  canSubmit = true;
}

function disableSubmission(formElement){
  // alert ("Submission disabled on " + formElement.name);
  lastField = formElement;
  canSubmit = false;
}

function hide(strObjName) {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering hide(" + strObjName +")<BR>");
  // }
  document.getElementById(strObjName).style.visibility = 'hidden';
  document.getElementById(strObjName).style.display    = 'none';
}

function show(strObjName) {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering show(" + strObjName +")<BR>");
  // }
  document.getElementById(strObjName).style.visibility = 'visible';
  document.getElementById(strObjName).style.display    = 'block';
}

function blank(strObjName) {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering blank(" + strObjName +")<BR>");
  // }
  if (document.getElementById(strObjName).type == 'radio') {
    document.getElementById(strObjName).checked = false;
  } else {
    document.getElementById(strObjName).value = '';
  }
}

function getTotalLength() {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering getTotalLength()<BR>");
  // }
  // alert (document.getElementById("Total").type);
  
  switch(document.getElementById("Total").type) {
    case "text": // run on down to select-one
    case "select-one":
      // alert ("Got value of " + document.getElementById("Total").value);
      return Number(document.getElementById("Total").value);
      break;    
    default:
      // alert ("Got innerHTML of " + document.getElementById("Total").innerHTML);
      return Number(document.getElementById("Total").innerHTML);
  } 
}

function reflectChanges(){
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
   // alert ("Entering reflectChanges()<BR>");
  // }
  if (blnCorded){
    toggleRecommendBoth();
  }
  updatePrices();
}

function toggleOtherSource(){
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering toggleOtherSource()<BR>");
  // }
  if (document.getElementById("Source of measurements other").checked == true){
    show("specifyother");
  } else {
    blank("Other Source Of Measurements");
    hide("specifyother");
  }
  toggleTopFix();
}

function addThisField(fieldObject) {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering addThisField(" + fieldObject.name + ")<BR>");
  // }
  fieldContents = Number (fieldObject.value);
  if (String (fieldContents) == "NaN"){
    fieldContents = 0;
    fieldObject.value = "";
  }
  if (fieldContents < 0)
  {
    fieldContents = fieldContents * -1;        
    fieldObject.value = fieldContents;
  }
  return fieldContents;
}

function calculateBendLength() {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering calculateBendLength()<BR>");
  // }
  totalLength = 0;
  letterNumber = 0;
  runningTotal = 0;
  keepGoing = 1;
  while (keepGoing == 1) {
    fieldName = String.fromCharCode(65+letterNumber) + " to " + String.fromCharCode(65+letterNumber+1);
    fieldObject = document.getElementById(fieldName);
    if (fieldObject) {
      runningTotal = runningTotal + addThisField(fieldObject);
      letterNumber = letterNumber + 1;
    } else {
      keepGoing = 0;
    }
  }
  
  // Add on reverse ends, if there are any...
  fieldObject = document.getElementById("1 to A");
  if (fieldObject) {
    runningTotal = runningTotal + addThisField(fieldObject);
  }
  fieldObject = document.getElementById("2 to " + String.fromCharCode(65+letterNumber));
  if (fieldObject) {
    runningTotal = runningTotal + addThisField(fieldObject);
  }
  
  document.getElementById("Total").innerHTML = runningTotal;
  reflectChanges();
}

function calculateCurveLength() {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering calculateCurveLength()<BR>");
  // }
  pObject = document.getElementById("A to C");
  pValue  = Number(pObject.value) / 2;
  qObject = document.getElementById("B to D");
  qValue  = Number(qObject.value);
  calculate = 1;
  if (String(pValue) == "NaN") {
    pObject.value = "";
    calculate = 0;
  }
  if (String(qValue) == "NaN") { 
    qObject.value = "";
    calculate = 0;
  }
  if (calculate == 1){
    if (pValue < 0){
       pValue = pValue * -1;
       pObject.value = pValue * 2;
    }
    if (qValue < 0){
       qValue = qValue * -1;
       qObject.value = qValue;
    }
    if (qValue == 0){
      runningTotal = pValue * 2;
    } else {
      radius = (pValue * pValue + qValue * qValue) / (2 * qValue);
      circumference = 2 * Math.PI * radius;
      angle = 2 * Math.asin (pValue / radius);   
      if (radius < qValue){
      angle = (2 * Math.PI) - angle;
      }
      proportion = angle / (2 * Math.PI);
      runningTotal = proportion * circumference;
      runningTotal = Math.round ((runningTotal + 0.5) * 10) / 10;
    }
    // Add on reverse ends, if there are any...
    fieldObject = document.getElementById("1 to A");
    if (fieldObject) {
      runningTotal = runningTotal + addThisField(fieldObject);
    }
    fieldObject = document.getElementById("2 to C");
    if (fieldObject) {
      runningTotal = runningTotal + addThisField(fieldObject);
    }
    document.getElementById("Total").innerHTML = runningTotal;
  } else {
    document.getElementById("Total").innerHTML = 0;
  }
  reflectChanges();
}

function toggleTopFix(){
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering toggleTopFix()<BR>");
  // }
  if ((document.getElementById("Fixing type top").checked == false) || (numBends != "" && numBends != "0" && document.getElementById("Source of measurements track").checked == true)){
    blank ('topfixdistance');
    if (numBends != "0") hide('topfix');
  } else {
    if (numBends != "0") show('topfix');
  }
}

function currencyDisplay(numAmount) {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering currencyDisplay(" + numAmount + ")<BR>");
  // }

  if (numAmount == "") {
   numAmount = 0;
  } else {
    numAmount = numAmount * numExchangeRate;
  }
  if (!blnDecimals) {
    numAmount = Math.round(numAmount);
  } else {
    numAmount = Math.round(numAmount * 100) / 100;
  }
  strAmount = strCurrencySymbol + "&nbsp;" + numAmount;
  decimalPosition = strAmount.indexOf(".");
  switch (decimalPosition) {
    case -1:
      strAmount = strAmount + ".00";
      break;
    default:
      if (strAmount.length - decimalPosition - 1 != "2") {
        strAmount = strAmount + "0";
      }
  }
  return strAmount;
}

function updatePrices() {
  // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)")){
    // alert ("Entering updatePrices()");
  // }
  var arrPageLengths     = new Array ();
  var arrPageMaxLengths  = new Array ();
  var arrPagePrices      = new Array ();
  var arrPageRRPs        = new Array ();
  
  var numTotalLength = getTotalLength();
  
  if (strElectric != "y" && strCorded != "y" && blnCanEnforcePulleyOrBridge) {
    if (400 <= numTotalLength){
      show("connectionbridge");
    } else {
      blank("Connection bridge no");
      blank("Connection bridge yes");
      hide("connectionbridge");
    }
  }
  
  if (0 < numOverlapArms) {
    show("includedoverlaparms");
    show("includedoptions");
  } else {
    hide("includedoverlaparms");
  }

  blnLengthOK = false;
  // for each element of our arrays...
  for (i=0; i < arrDBCodes.length; i++) {
    // if this length is greater than the actual length
    // but shorter than whatever length we're currently remembering 
    // then remember this one instead (one for each version on the page)
    if ((numTotalLength <= arrDBLengths[i]) && (!arrPageLengths[arrDBCodes[i]] || (arrDBLengths[i] < arrPageLengths[arrDBCodes[i]]))) {
      arrPageLengths[arrDBCodes[i]] = arrDBLengths[i];
      arrPagePrices[arrDBCodes[i]]  = arrDBPrices[i];
      arrPageRRPs[arrDBCodes[i]]    = arrDBRRPs[i];
    }
   // if this is the longest length so far
   // remember so we can display a message if their length is too long
    if ((0 < arrDBLengths[i]) && (!arrPageMaxLengths[arrDBCodes[i]] || (arrPageMaxLengths[arrDBCodes[i]] < arrDBLengths[i]))){
      arrPageMaxLengths[arrDBCodes[i]] = arrDBLengths[i];
    }
  }
  
  var numTempRowNum = 0;
  // For each version on the page
  for (var j in arrPageMaxLengths) {
    numTempRowNum = numTempRowNum + 1;
    // Work out extra costs (different for each version)
    if (numOverlapArms > 0) {
      numExtrasCost = arrOverlapArmCosts[j] * numOverlapArms;
      numExtrasRRP  = arrOverlapArmRRPs[j]  * numOverlapArms;
    } else {
      numExtrasCost    = 0;
      numExtrasRRP     = 0;
    }
    if (strElectric != "y" && strCorded != "y") {
      if (document.getElementById('Connection bridge yes').checked == true) {
        show("includedbridge");
        show("includedoptions");
        blnBridge = true;
        numExtrasCost = numExtrasCost + arrPulleyCosts[j];
        numExtrasRRP  = numExtrasRRP  + arrPulleyRRPs[j];
      } else {
        hide("includedbridge");
        blnBridge = false;
      }
    } else {
      // alert ((1 < numCurtains) + " AND (" +  blnCorded + " AND (" + (2 < numCurtains) + " OR " + (document.getElementById('Equal width no').checked == true) + " OR " + (0 < numReverseEnds) + " OR " + (document.getElementById('Cord side both').checked == true) + " OR " + (document.getElementById('Heavy curtains yes').checked == true) + "))")
      if ((1 < numCurtains) && (blnCorded && (2 < numCurtains || document.getElementById('Equal width no').checked == true || 0 < numReverseEnds || document.getElementById('Cord side both').checked == true || document.getElementById('Heavy curtains yes').checked == true))){
        show("includedpulley");
        show("includedoptions");
        blnPulley = true;
        numExtrasCost = numExtrasCost + arrPulleyCosts[j];
        numExtrasRRP  = numExtrasRRP  + arrPulleyRRPs[j];
      } else {
        hide("includedpulley");          
        blnPulley = false;
      }
    }
    if (numOverlapArms == 0 && !blnPulley && !blnBridge) {
      hide("includedoptions");
    }
    if (arrPageMaxLengths[j] < numTotalLength){
      // it's too long so say so
      document.getElementById(j + " RRP").innerHTML   = "";
      document.getElementById(j + " inc").innerHTML   = "";
      document.getElementById(j + " inc").innerHTML   = strMaxLength;
      document.getElementById(j + " submit").disabled = true;
    } else {
      if (0 < numTotalLength) {
        // we have an OK length
        // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)") && (numTempRowNum==1)){
          // alert ("Current value is: " + document.getElementById(j + " RRP").innerHTML + "\nNew value will be: " + "&nbsp;" + currencyDisplay(arrPageRRPs[j] + numExtrasRRP));
        // }
        // if ((navigator.appName != "Microsoft Internet Explorer") || (navigator.appVersion != "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)") || (numTempRowNum==1)){
          document.getElementById(j + " RRP").innerHTML   = "";
          document.getElementById(j + " RRP").innerHTML   = "&nbsp;" + currencyDisplay(arrPageRRPs[j] + numExtrasRRP);
        // }
        
        // if ((navigator.appName == "Microsoft Internet Explorer") && (navigator.appVersion == "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)") && (numTempRowNum==1)){
          // alert ("Current value is: " + document.getElementById(j + " inc").innerHTML + "\nNew value will be: " + "&nbsp;" + currencyDisplay(arrPagePrices[j] + numExtrasCost));
        // }
        // if ((navigator.appName != "Microsoft Internet Explorer") || (navigator.appVersion != "4.0 (compatible; MSIE 5.0; Macintosh; I; PPC)") || (numTempRowNum==1)){
          document.getElementById(j + " inc").innerHTML   = "";
          document.getElementById(j + " inc").innerHTML   = "&nbsp;" + currencyDisplay(arrPagePrices[j] + numExtrasCost);
        // }
        document.getElementById(j + " submit").disabled = false;
        blnLengthOK = true;
      } else {
        // we have no length
        document.getElementById(j + " RRP").innerHTML   = "";
        document.getElementById(j + " inc").innerHTML   = "";
        document.getElementById(j + " submit").disabled = true;
      }
    } 
  }
}

var blnLengthOK = false;
var canSubmit   = false;

var blnPulley = false;
var blnBridge = false;

var numCurtains    = 0;
var numOverlapArms = 0;

var arrOverlapArmCosts = new Array ();
var arrOverlapArmRRPs  = new Array ();
