
// County table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//
var county = '\
1:101:Gjøvik|\
1:102:Vestre Toten|\
1:103:Østre Toten|\
2:201:Farsund|\
2:202:Feda|\
2:203:Flekkefjord|\
2:204:Kvinesdal|\
2:205:Lyngdal|\
2:206:Vanse|\
3:301:Finnøy|\
3:302:Hjelmeland|\
3:304:Hommersåk|\
3:303:Lund|\
3:305:Sokndal|\
3:306:Strand|\
4:151:Hamar|\
4:153:Nes og omegn|\
4:150:Ringsaker|\
4:152:Stange|\
5:51:Askim|\
5:52:Eidsberg|\
5:53:Fredrikstad|\
5:54:Hobøl|\
5:55:Hvaler|\
5:56:Moss|\
5:57:Råde|\
5:58:Rakkestad|\
5:59:Rygge|\
5:60:Sarpsborg|\
5:61:Skiptvet|\
5:62:Spydeberg|\
5:63:Trøgstad|\
5:64:Våler|\
6:75:Aurskog-Høland|\
6:76:Eidsvoll|\
6:77:Fet|\
6:78:Gjerdrum|\
6:79:Hurdal|\
6:80:Nannestad|\
6:81:Nes|\
6:82:Nesodden|\
6:83:Nittedal|\
6:84:Rælingen|\
6:85:Vestby\
';

// Province data table
//
// To edit the list, just delete a line or add a line. Order is important.
// The order displayed here is the order it appears on the drop down.
//

var province = '\
6:Akershus|\
4:Hedmark|\
1:Oppland|\
3:Rogaland|\
2:Vest-Agder|\
5:Østfold\
';

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the province selected with the counties from the province list
function populateProvince(defaultProvince) {
  if ( postProvince != '' ) {
    defaultProvince = postProvince;
  }
  var provinceLineArray = province.split('|');  // Split into lines
  var selObj = document.getElementById('provinceSelect');
  selObj.options[0] = new Option('- Velg -','');
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < provinceLineArray.length; loop++) {
    lineArray = provinceLineArray[loop].split(':');
    provinceCode  = TrimString(lineArray[0]);
    provinceName  = TrimString(lineArray[1]);
    if ( provinceCode != '' ) {
      selObj.options[loop + 1] = new Option(provinceName, provinceCode);
    }
    if ( defaultProvince == provinceCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
}

function populateCounty() {
  var selObj = document.getElementById('countySelect');
  var foundCounty = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option('- Velg -','');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with countys from the selected province
  var countyLineArray = county.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < countyLineArray.length; loop++) {
    lineArray = countyLineArray[loop].split(":");
    provinceCode  = TrimString(lineArray[0]);
    countyCode    = TrimString(lineArray[1]);
    countyName    = TrimString(lineArray[2]);
  if (document.getElementById('provinceSelect').value == provinceCode && provinceCode != '' ) {
    // If it's a input element, change it to a select
      if ( selObj.type == 'text' ) {
        parentObj = document.getElementById('countySelect').parentNode;
        parentObj.removeChild(selObj);
        var inputSel = document.createElement("SELECT");
        inputSel.setAttribute("name","county");
        inputSel.setAttribute("id","countySelect");
        parentObj.appendChild(inputSel) ;
        selObj = document.getElementById('countySelect');
        selObj.options[0] = new Option('Select County','');
        selObj.selectedIndex = 0;
      }
      if ( countyCode != '' ) {
        selObj.options[optionCntr] = new Option(countyName, countyCode);
      }
      // See if it's selected from a previous post
      if ( countyCode == postCounty && provinceCode == postProvince ) {
        selObj.selectedIndex = optionCntr;
      }
      foundCounty = true;
      optionCntr++
    }
  }
}

function initProvince(province) {
  populateProvince(province);
  populateCounty();
}
