var spanCentresCompositeValuesArray;
var spanCentresThroughValuesArray;
var spanCentresOverValuesArray;
var spanCentres2TowerValuesArray;
var spanCentres1TowerValuesArray;
var buildMethodValuesArray;
var customClearHeight;

window.onload = initPage;

function initPage() {
  if(document.getElementById('flash_object')) {
    InsertFlashObject("flash_object", "flash/photo-sequence.swf", 300, 225);
  }
  if(document.getElementById('fstandardtype') && document.getElementById('fstandardspancentres')) {
    if(typeof(spanCentresCompositeValuesArray) != 'undefined'
       && typeof(spanCentresThroughValuesArray) != 'undefined'
       && typeof(spanCentresOverValuesArray) != 'undefined'
       && typeof(spanCentres2TowerValuesArray) != 'undefined'
       && typeof(spanCentres1TowerValuesArray) != 'undefined') {
       customClearHeight = '';
       document.getElementById('fstandardtype').onchange = modifyStandardFields;
       document.getElementById('fcustomtype').onchange = modifyCustomFields;
    } 
  }
  if(document.getElementById('bridge_span')) {
    document.getElementById('bridge_span').onchange = setSteelDeckedBridgesFieldsForSpan;        
  }
 
  externalLinks();
}

function externalLinks() { 
  if(!document.getElementsByTagName) return; 
  var anchors = document.getElementsByTagName("a"); 
  for(var i=0; i<anchors.length; i++) { 
    var anchor = anchors[i]; 
    if(anchor.getAttribute("href") && anchor.getAttribute("rel") == "external" || anchor.getAttribute("rel") == "external nofollow" ) 
      anchor.target = "_blank"; 
  } 
}

function InsertFlashObject(objID, flashLocation, width, height) {
  var flashObject;
  flashObject = "<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0' width='"+width+"' height='"+height+"'>";
  flashObject = flashObject + "<param name='wmode' value='transparent' />";
  flashObject = flashObject + "<param name='movie' value='"+flashLocation+"' />";
  flashObject = flashObject + "<param name='quality' value='high' />";
  flashObject = flashObject + "<embed src='"+flashLocation+"' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+width+"' height='"+height+"' wmode='transparent'";
  flashObject = flashObject + "></embed></object>";
  document.getElementById(objID).innerHTML = flashObject;
}

function set_form_arrays(arrayString) {
  var fA = arrayString.split(';');
  for(lA=0;lA<fA.length;lA++) {
    var bridgeArray = fA[lA].split('=');
    switch(bridgeArray[0]) {
      case '0':
        spanCentresCompositeValuesArray = bridgeArray[1].split('|');
      break;
      case '1':
        spanCentresThroughValuesArray = bridgeArray[1].split('|');
      break;
      case '2':
        spanCentresOverValuesArray = bridgeArray[1].split('|');
      break;
      case '3':
        spanCentres2TowerValuesArray = bridgeArray[1].split('|');
      break;
      case '4':
        spanCentres1TowerValuesArray = bridgeArray[1].split('|');
      break;
      case '5':
        buildMethodValuesArray = bridgeArray[1].split('|');
      break;
      default:
        alert('The form did not load properly, please refresh the page');
      break;
    }
  }
}

function modifyStandardFields() {
  document.getElementById('fstandardspancentres').options.length = 0;
  document.getElementById('fstandardbuildmethod').options.length = 0;
  document.getElementById('fstandardnospans').options.length = 0;
  switch(this.value) {
    case '0':
      for(iA=0;iA<spanCentresCompositeValuesArray.length;iA++) {
        document.getElementById('fstandardspancentres').options[iA] = new Option(spanCentresCompositeValuesArray[iA], spanCentresCompositeValuesArray[iA], false, false);
      }
      document.getElementById('fstandardbuildmethod').options[0] = new Option(buildMethodValuesArray[0], 0, false, false);
      document.getElementById('fstandardbuildmethod').options[1] = new Option(buildMethodValuesArray[1], 1, false, false);
      for(xx=0;xx<10;xx++) {
        document.getElementById('fstandardnospans').options[xx] = new Option(xx + 1, xx, false, false);
      }
      document.getElementById('fstandardbuildmethod').disabled = false;
      document.getElementById('fstandardnospans').disabled = false;
      document.getElementById('fstandardclearheight').value = 'unrestricted';
    break;
    case '1':
      for(iA=0;iA<spanCentresThroughValuesArray.length;iA++) {
        document.getElementById('fstandardspancentres').options[iA] = new Option(spanCentresThroughValuesArray[iA], spanCentresThroughValuesArray[iA], false, false);
      }
      document.getElementById('fstandardbuildmethod').options[0] = new Option(buildMethodValuesArray[0], 0, false, false);
      document.getElementById('fstandardbuildmethod').options[1] = new Option(buildMethodValuesArray[1], 1, false, false);
      for(xx=0;xx<10;xx++) {
        document.getElementById('fstandardnospans').options[xx] = new Option(xx + 1, xx, false, false);
      }
      document.getElementById('fstandardbuildmethod').disabled = false;
      document.getElementById('fstandardnospans').disabled = false;
      document.getElementById('fstandardclearheight').value = '6.00';
    break;
    case '2':
      for(iA=0;iA<spanCentresOverValuesArray.length;iA++) {
        document.getElementById('fstandardspancentres').options[iA] = new Option(spanCentresOverValuesArray[iA], spanCentresOverValuesArray[iA], false, false);
      }
      document.getElementById('fstandardbuildmethod').options[0] = new Option(buildMethodValuesArray[0], 0, false, false);
      document.getElementById('fstandardbuildmethod').options[1] = new Option(buildMethodValuesArray[1], 1, false, false);
      for(xx=0;xx<10;xx++) {
        document.getElementById('fstandardnospans').options[xx] = new Option(xx + 1, xx, false, false);
      }
      document.getElementById('fstandardbuildmethod').disabled = false;
      document.getElementById('fstandardnospans').disabled = false;
      document.getElementById('fstandardclearheight').value = 'unrestricted';
    break;
    case '3':
      for(iA=0;iA<spanCentres2TowerValuesArray.length;iA++) {
        document.getElementById('fstandardspancentres').options[iA] = new Option(spanCentres2TowerValuesArray[iA], spanCentres2TowerValuesArray[iA], false, false);
      }
      document.getElementById('fstandardbuildmethod').options[0] = new Option(buildMethodValuesArray[2], 2, false, false);
      document.getElementById('fstandardbuildmethod').disabled = true;
      document.getElementById('fstandardnospans').options[0] = new Option('N/A', '999', false, false);
      document.getElementById('fstandardnospans').disabled = true;
      document.getElementById('fstandardclearheight').value = '6.00';
    break;
    case '4':
      for(iA=0;iA<spanCentres1TowerValuesArray.length;iA++) {
        document.getElementById('fstandardspancentres').options[iA] = new Option(spanCentres1TowerValuesArray[iA], spanCentres1TowerValuesArray[iA], false, false);
      }
      document.getElementById('fstandardbuildmethod').options[0] = new Option(buildMethodValuesArray[3], 3, false, false);
      document.getElementById('fstandardbuildmethod').disabled = true;
      document.getElementById('fstandardnospans').options[0] = new Option('N/A', '999', false, false);
      document.getElementById('fstandardnospans').disabled = true;
      document.getElementById('fstandardclearheight').value = '6.00';
    break;
    default:
      alert('The value selected is invalid, please refresh the page');
    break;
  }
}

function modifyCustomFields() {
  switch(this.value) {
    case '0':
      customClearHeight = document.getElementById('fcustomclearheight').value;
      document.getElementById('fcustomclearheight').disabled = true;
      document.getElementById('fcustomclearheight').value = 'unrestricted';
    break;
    case '1':
      document.getElementById('fcustomclearheight').disabled = false;
      document.getElementById('fcustomclearheight').value = customClearHeight;
    break;
    case '2':
      customClearHeight = document.getElementById('fcustomclearheight').value;
      document.getElementById('fcustomclearheight').disabled = true;
      document.getElementById('fcustomclearheight').value = 'unrestricted';
    break;
    case '3':
      document.getElementById('fcustomclearheight').disabled = false;
      document.getElementById('fcustomclearheight').value = customClearHeight;
    break;
    case '4':
      document.getElementById('fcustomclearheight').disabled = false;
      document.getElementById('fcustomclearheight').value = customClearHeight;
    break;
    default:
      alert('The value selected is invalid, please refresh the page');
    break; 
  }
}

function set_disabled_fields(arrayString) {
  var fA = arrayString.split(';');
  for(lA=0;lA<fA.length;lA++) {
    var fieldArray = fA[lA].split('=');
    if(document.getElementById(fieldArray[1])) {
      document.getElementById(fieldArray[1]).disabled = true;
    }
  }
}

function setSteelDeckedBridgesFieldsForSpan() {
  
  switch(this.value) {
    case '12.9m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 1', 'Type 1', false, false);
    break;
    case '15.24m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 1', 'Type 1', false, false);
    break;
    case '18.29m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 2', 'Type 2', false, false);
    break;
    case '21.34m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 2', 'Type 2', false, false);
    break;
    case '24.38m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 3', 'Type 3', false, false);
    break;
    case '27.43m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 3', 'Type 3', false, false);
    break;
    case '30.48m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 3', 'Type 3', false, false);
    break;
    case '40.4m/50.4m/60.4m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 4', 'Type 4', false, false);
      
      var bridgeType=document.getElementById("bridge_type")
      bridgeType.remove(0);
      document.getElementById('bridge_type').options[0] = new Option('CTTT', 'CTTT', false, false);
    break;
    case '70.5m/80.5m/90.5m':
      var bridgeLaunchKit=document.getElementById("bridge_launch_kit")
      bridgeLaunchKit.remove(0);
      document.getElementById('bridge_launch_kit').options[0] = new Option('Type 4', 'Type 4', false, false);
      
      var bridgeType=document.getElementById("bridge_type")
      bridgeType.remove(0);
      document.getElementById('bridge_type').options[0] = new Option('CTTT', 'CTTT', false, false);
    break;
  
    default:
      alert('The value selected is invalid, please refresh the page');
    break;
  }
}
