// Opens a window in order to view the build pictures
function openBuildPic(imageName) {
  openWindow('/images/build_img/' + imageName)
}


// opens a popup window based on either default or supplied arguments
var myWin, focusWin, genericWin;
function openWindow(url, winWidth, winHeight, windowName) {
var winWidth = winWidth ? winWidth : 205;
var winHeight = winHeight ? winHeight : 230;
var parms = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, height=' + winHeight + ',width=' + winWidth + ', left=0, top=0';
    if (windowName) {
    	genericWin = window.open(url, 'myWin', parms);
    } else {
    	focusWin = window.open(url, 'myWin', parms);
    }
    if (focusWin) {
        focusWin.focus();
    }
}

var stepInc = 2;
function advanceStep(inc) {
  var frm = document.sb_form;
  if (inc == 6) {
     checkChairForm(frm);
     return false;
  }
  if (inc == 5) {
    frm.continueButton.value = 'Complete Order';
  } else {
    frm.continueButton.value = 'Next >>>';
  }
  var steps = [1,2,3,4,5];
  for (var i in steps) {
    if (steps[i] == inc) {
      document.getElementById('step' + steps[i]).style.display = 'block';
      document.getElementById('tabStep' + steps[i]).style.backgroundImage = 'url(/images/tab/tab_bgrnd_on' + inc + '.gif)';
      stepInc = inc+1; // Set the step in order that the next button supplies the correct argument
      saveTabId(inc); // Save in case the page is refreshed
    } else {
      document.getElementById('step' + steps[i]).style.display = 'none';
      document.getElementById('tabStep' + steps[i]).style.backgroundImage = 'url(/images/tab/tab_bgrnd.gif)';
    }
  }
//document.getElementById('helpContainer').innerHTML = ''; // Reset help - moved to build container div for W3C validation
 }

function chooseAttSelfProp(val) {
  var attSelfPropDivs = ['attendantDiv','selfPropellingDiv'];
  setAttendant(val); // Save the attendant value
  for (var i in attSelfPropDivs) {
    if (attSelfPropDivs[i] == val) {
      document.getElementById(attSelfPropDivs[i]).style.display = 'block';
    } else {
      document.getElementById(attSelfPropDivs[i]).style.display = 'none';
    }
  }
  if (val == 'attendantDiv') {
    openBuildPic('bp002at.jpg');
    document.getElementById('narrow_access_104').checked = false;
    document.getElementById('narrow_access_104').disabled = true;
    getPrice(0, 'narrow_access');
  } else {
    openBuildPic('bp002spcr.jpg');
    document.getElementById('narrow_access_104').disabled = false;
  }
}

function resetForm(frm) {
  for (var i = 0; i < frm.elements.length; i++) {
    if ((frm.elements[i].type == 'radio' || frm.elements[i].type == 'checkbox') && frm.elements[i].checked == true) {
      frm.elements[i].checked = false;
      getPrice(0, frm.elements[i].name);
    } else if (frm.elements[i].type == 'text') {
      frm.elements[i].value = '';
    }
  }
}

function checkChairForm(frm) {
  var Msg = '';
  var seatWidthChecked = false;
  for (var i = 0; i < frm.seat_width.length; i++) {
    if (frm.seat_width[i].checked == true) {
      seatWidthChecked = true;
    }
  }
  var seatDepthChecked = false;
  for (var i = 0; i < frm.seat_depth.length; i++) {
    if (frm.seat_depth[i].checked == true) {
      seatDepthChecked = true;
    }
  }
  var occupantWeightChecked = false;
  for (var i = 0; i < frm.occupant_weight.length; i++) {
    if (frm.occupant_weight[i].checked == true) {
      occupantWeightChecked = true;
    }
  }
  var attSelfPropSelectChecked = false;
  for (var i = 0; i < frm.attSelfPropSelect.length; i++) {
    if (frm.attSelfPropSelect[i].checked == true) {
      attSelfPropSelectChecked = true;
    }
  }
  var rearWheelSize = false;
  if (frm.attSelfPropSelect[0].checked == true) { // Attendant option selected
    for (var i = 0; i < frm.attendant.length; i++) {
      if (frm.attendant[i].checked == true) {
        rearWheelSize = true;
      }
    }
  } else if (frm.attSelfPropSelect[1].checked == true) { // Self propelled option selected
    for (var i = 0; i < frm.rear_wheel_size.length; i++) {
      if (frm.rear_wheel_size[i].checked == true) {
        rearWheelSize = true;
      }
    }
  }
  var frontCastorChecked = false;
  for (var i = 0; i < frm.front_castor.length; i++) {
    if (frm.front_castor[i].checked == true) {
      frontCastorChecked = true;
    }
  }
  var frontCastorPositionChecked = false;
  for (var i = 0; i < frm.front_castor_position.length; i++) {
    if (frm.front_castor_position[i].checked == true) {
      frontCastorPositionChecked = true;
    }
  }
  var armrestHeightChecked = false;
  for (var i = 0; i < frm.armrest_height.length; i++) {
    if (frm.armrest_height[i].checked == true) {
      armrestHeightChecked = true;
    }
  }
  var armrestChecked = false;
  if (frm.side_panel_only.checked == true) { // We don't need to check the armrest
    armrestChecked = true;
  } else {
    for (var i = 0; i < frm.armrest.length; i++) {
      if (frm.armrest[i].checked == true) {
        armrestChecked = true;
      }
    }
  }
  var backPostHeightChecked = false;
  for (var i = 0; i < frm.back_post_height.length; i++) {
    if (frm.back_post_height[i].checked == true) {
      backPostHeightChecked = true;
    }
  }
  var backRestTypeChecked = false;
  for (var i = 0; i < frm.backrest_type.length; i++) {
    if (frm.backrest_type[i].checked == true) {
      backRestTypeChecked = true;
    }
  }

  if (seatWidthChecked == false) {
    Msg += 'Please select a seat width\n';
  }
  if (seatDepthChecked == false) {
    Msg += 'Please select a seat depth\n';
  }
  if (occupantWeightChecked == false) {
    Msg += 'Please select an occupant weight\n';
  }
  if (attSelfPropSelectChecked == false) {
    Msg += 'Please select a method of propulsion\n';
  }
  if (rearWheelSize == false) {
    Msg += 'Please select a rear wheel size\n';
  }
  if (frontCastorChecked == false) {
    Msg += 'Please select a front castor size\n';
  }
  if (frontCastorPositionChecked == false) {
    Msg += 'Please select a front castor position\n';
  }
  if (armrestHeightChecked == false) {
    Msg += 'Please select an armrest height\n';
  }
  if (armrestChecked == false) {
    Msg += 'Please select an armrest\n';
  }
  if (backPostHeightChecked == false) {
    Msg += 'Please select a back post height\n';
  }
  if (backRestTypeChecked == false) {
    Msg += 'Please select a backrest type\n';
  }

  if (Msg) {
    alert(Msg);
    return false;
  } else {
    frm.submit();
    return true;
  }
}

function chooseDepthSelections(frm, val) {
  val = parseInt(val)
  switch (val) {
    case 6:
      showRadioButtons(['seat_depths_short']);
    break;
    case 7:
      showRadioButtons(['seat_depths_short']);
    break;
    case 8:
      showRadioButtons(['seat_depths_short']);
    break;
    case 9:
      showRadioButtons(['seat_depths_short','seat_depths_long']);
    break;
    case 10:
      showRadioButtons(['seat_depths_short','seat_depths_long']);
    break;
    case 11:
      showRadioButtons(['seat_depths_long']);
    break;
    case 12:
      showRadioButtons(['seat_depths_long','seat_depths_extra_long']);
    break;
    case 13:
      showRadioButtons(['seat_depths_long','seat_depths_extra_long']);
    break;
    case 14:
      showRadioButtons(['seat_depths_extra_long']);
    break;
  }
  disableOccupantWeightsBasedOnWidth(frm, val);
}

function displayCustomSeatDepth(frm, seatDepthVal) {
  disableOccupantWeights(frm, seatDepthVal);
  if (seatDepthVal == 10000) {
    document.getElementById('specify_select').style.display = 'block';
    getPrice(10000, 'seat_depth');
    saveCustomSeathDepthChecked(1);
  } else {
    document.getElementById('specify_select').style.display = 'none';
    saveCustomSeathDepthChecked(0);
  }
}

function disableOccupantWeightsBasedOnWidth(frm, seatWidthVal) {
  for (var i = 0; i < frm.occupant_weight.length; i++) {
    if (seatWidthVal <= 9) {
      if (frm.occupant_weight[i].value == 108) {
        frm.occupant_weight[i].checked = false;
        frm.occupant_weight[i].disabled = true;
        getPrice(105, frm.occupant_weight[i].name);
        validateOccupantWeight(105, frm);
      }
    } else {
      frm.occupant_weight[i].disabled = false;
    }
  }
}

function disableOccupantWeights(frm, seatDepthVal) {
  for (var i = 0; i < frm.occupant_weight.length; i++) {
    if (seatDepthVal == 17 || seatDepthVal == 18) {
      if (frm.occupant_weight[i].value == 105 || frm.occupant_weight[i].value == 108) {
        frm.occupant_weight[i].checked = false;
        frm.occupant_weight[i].disabled = true;
      } else {
        frm.occupant_weight[i].disabled = false;
      }
    } else if (seatDepthVal == 20 || seatDepthVal == 21) {
      if (frm.occupant_weight[i].value == 108) {
        frm.occupant_weight[i].disabled = false;
      } else {
        frm.occupant_weight[i].checked = false;
        frm.occupant_weight[i].disabled = true;
      }
    } else if (seatDepthVal == 10000) {
      frm.occupant_weight[i].disabled = false;
    } else {
      frm.occupant_weight[i].disabled = false;
    }
  }
}

function validateOccupantWeight(val, frm) {
  for (var i = 0; i < frm.backrest_fabric.length; i++) {
    if (val >= 107) {
      if (frm.backrest_fabric[i].value == 83) {
        frm.backrest_fabric[i].checked = true;
        getPrice(frm.backrest_fabric[i].value, frm.backrest_fabric[i].name);
      } else {
        frm.backrest_fabric[i].disabled = true;
      }
    } else {
      frm.backrest_fabric[i].checked = false;
      frm.backrest_fabric[i].disabled = false;
      if (frm.backrest_fabric[i].value == 83) {
        getPrice(0, frm.backrest_fabric[i].name);
      }
    }
  }
  for (var i = 0; i < frm.front_castor.length; i++) {
    if (val >= 107) {
      if (frm.front_castor[i].value == 44) {
        frm.front_castor[i].checked = true;
        getPrice(frm.front_castor[i].value, frm.front_castor[i].name);
      } else {
        frm.front_castor[i].disabled = true;
      }
    } else {
      frm.front_castor[i].disabled = false;
      frm.front_castor[i].checked = false;
      if (frm.front_castor[i].value == 44) {
        getPrice(0, frm.front_castor[i].name);
      }
    }
  }
  for (var i = 0; i < frm.armrest_position.length; i++) {
    if (val >= 107) {
      if (frm.armrest_position[i].value == 59) {
        frm.armrest_position[i].checked = true;
        getPrice(frm.armrest_position[i].value, frm.armrest_position[i].name);
      } else {
        frm.armrest_position[i].disabled = true;
      }
    } else {
      frm.armrest_position[i].checked = false;
      frm.armrest_position[i].disabled = false;
      if (frm.armrest_position[i].value == 59) {
        getPrice(0, frm.armrest_position[i].name);
      }
    }
  }
  for (var i = 0; i < frm.backrest_type.length; i++) {
    if (val >= 108) {
      if (frm.backrest_type[i].value == 78) {
        frm.backrest_type[i].checked = true;
        getPrice(frm.backrest_type[i].value, frm.backrest_type[i].name);
      } else {
        frm.backrest_type[i].disabled = true;
      }
    } else {
      frm.backrest_type[i].disabled = false;
      frm.backrest_type[i].checked = false;
      if (frm.backrest_type[i].value == 78) {
        getPrice(0, frm.backrest_type[i].name);
      }
    }
  }
  for (var i = 0; i < frm.footrest.length; i++) {
    if (val >= 107) {
      if (frm.footrest[i].value == 119) {
        frm.footrest[i].checked = true;
        getPrice(frm.footrest[i].value, frm.footrest[i].name);
      } else {
        frm.footrest[i].disabled = true;
      }
    } else {
      frm.footrest[i].checked = false;
      frm.footrest[i].disabled = false;
      if (frm.footrest[i].value == 119) {
        getPrice(0, frm.footrest[i].name);
      }
    }
  }
}

function validateTyreSize(val, frm) {
  for (var i = 0; i < frm.accessories.length; i++) {
    if (frm.accessories[i].value == 93 || frm.accessories[i].value == 94) {
      if (val == 27) {
        frm.accessories[i].disabled = false;
      } else {
        frm.accessories[i].disabled = true;
        frm.accessories[i].checked = false;
      }
    } else {
      frm.accessories[i].disabled = false;
    }
  }
  if (val == 27) {
    frm.handrim[1].disabled = false;
  } else {
    frm.handrim[1].disabled = true;
    frm.handrim[1].checked = false;
  }

  if (val == 24 || val == 25 || val == 27) {
    document.getElementById('specify_spoke_guard').style.display = 'block';
  } else {
    document.getElementById('specify_spoke_guard').style.display = 'none';
  }
}

function validateGreenTyre(val, frm) {
  if (val == 34) {
    document.getElementById('select_tyre_colour').style.display = 'block';
  } else {
    document.getElementById('select_tyre_colour').style.display = 'none';
  }
}

function showRadioButtons(ids) {
  hideIds = ['seat_depths_short','seat_depths_long','seat_depths_extra_long'];
  for (var i in hideIds) {
    document.getElementById(hideIds[i]).style.display = 'none';
  }
  if (ids != '') {
    for (var i in ids) {
      document.getElementById(ids[i]).style.display = 'block';
    }
  }
}

function saveCustomSeatDepth(fld) {
  if (fld.value < 13 || fld.value > 23) {
    if (fld.value < 13) {
      alert ('Please enter a seat depth of at least 13 inches');
    }
    if (fld.value > 23) {
      alert ('Please enter a seat depth less than 24 inches');
    }
    fld.value = '';
    return false;
  }
  saveCustomSeatDepthVal(fld.value);
}

function displayCustomSeatHeight(val, frm, container) {
  if (val == 117) {
    document.getElementById(container).style.display = 'block';
  } else {
    document.getElementById(container).style.display = 'none';
  }
}

function saveCustomSeatHeight(fld) {
  saveCustomSeatHeightVal(fld.value)
}

function displayCustomBackPostHeight(val, frm) {
  if (val == 73) {
    document.getElementById('select_backpost').style.display = 'block';
  } else {
    document.getElementById('select_backpost').style.display = 'none';
  }
}

function displayArmrest(chckd) {
  if (chckd) {
    document.getElementById('select_armrest_position').style.display = 'none';
  } else {
    document.getElementById('select_armrest_position').style.display = 'block';
  }
  chckd = chckd == true ? 1 : 0;
  savedisplayArmrest(chckd)
}

function saveCustomBackPostHeight(fld) {
  saveCustomBackPostHeightVal(fld.value)
}

// Added by FJA for hidden reveal seat options //
function clearButtons() {
  document.forms[1].seat_depth_option[0].checked=false;
  document.forms[1].seat_depth_option[1].checked=false;
  document.forms[1].seat_depth_option[2].checked=false;
  document.forms[1].seat_depths_short.selectedIndex=0;
  document.forms[1].seat_depths_long.selectedIndex=0;
  document.forms[1].seat_depths_extra_long.selectedIndex=0;
}

// END Added by FJA //