var selectedPickupLocation = -1;
var selectedDropOffDay = -1;
var selectedDropOffMonth = -1;
var selectedDropOffYear = -1;
var selectedDropOffHour = -1;
var selectedDropOffLocation = -1;
var selectedDobDay = -1;
var selectedDobMonth = -1;
var selectedDobYear = -1;
var selectedCountryOfOrigin = -1;
var selectedCountriesIntoArray = new Array();
var millisecondsInAYear = 1000*60*60*24*365.242199;

function setup()
{
  disable("pickupLocation");
  disable("dropOffDay");
  disable("dropOffMonth");
  disable("dropOffYear");
  disable("dropOffHour");
  disable("dropOffLocation");
  disable("dobDay");
  disable("dobMonth");
  disable("dobYear");
  disable("countryOfOrigin");
  disable("countriesInto");
  disable("resetCountries");
  //document.getElementById("countriesInto").selectedIndex = -1;
  document.getElementById("resetButtonDiv").style.display = "inline";
  disable("resetCountries");

  vrcs = rcs;
  return checkValues();
}

function checkValues()
{
  return valueChangedEvent();
}

function resetCountriesClicked()
{
  document.getElementById("countriesInto").selectedIndex = -1;
  valueChangedEvent("countriesInto");
}

function valueChangedEvent(elementName)
{
  
  
  // pickup date section
  
  
  if ((document.getElementById("pickupDay").selectedIndex == 0) ||
	    (document.getElementById("pickupMonth").selectedIndex == 0) ||
	    (document.getElementById("pickupYear").selectedIndex == 0) ||
	    (document.getElementById("pickupHour").selectedIndex == 0))
  {
	  disable("pickupLocation");
	  disable("dropOffDay");
	  disable("dropOffMonth");
	  disable("dropOffYear");
	  disable("dropOffHour");
	  disable("dropOffLocation");
	  disable("dobDay");
	  disable("dobMonth");
	  disable("dobYear");
	  disable("countryOfOrigin");
	  disable("countriesInto");
	  disable("resetCountries");
	
	  return (true);
  }
  else
  {
    storePlaceHolders();
	  enable("pickupLocation");
    vrcs = rcs;
    
	  var pickupDayVal = parseInt(document.getElementById("pickupDay").value);
	  var pickupMonthVal = parseInt(document.getElementById("pickupMonth").value);
	  var pickupYearVal = parseInt(document.getElementById("pickupYear").value);
    var pickupDate = new Date(pickupYearVal, pickupMonthVal-1, pickupDayVal, 0, 0, 0, 0);
	  var trcs = new Array();
	  var count = 0;
	  
	  for (var i=0; i<vrcs.length; i++)
	  {
	    for (var j=0; j<rcdts.length; j++)
	    {
	      if (vrcs[i] == rcdts[j][0]) 
	      {
	        var startDate = new Date(rcdts[j][1]);
	        var endDate = new Date(rcdts[j][2]);
	        if ((pickupDate.getTime() >= startDate.getTime()) && (pickupDate.getTime() <= endDate.getTime()))
	        {
	          trcs[count++] = vrcs[i];
	        }
	      }
	    }
	  }
	  
	  vrcs = trcs;
	
	  if ((elementName != "pickupLocation") &&
	      (elementName != "dropOffDay") &&
	      (elementName != "dropOffMonth") &&
	      (elementName != "dropOffYear") &&
	      (elementName != "dropOffHour") &&
	      (elementName != "dropOffLocation") &&
	      (elementName != "dobDay") &&
	      (elementName != "dobMonth") &&
	      (elementName != "dobYear") &&
	      (elementName != "countriesInto") &&
	      (elementName != "countryOfOrigin"))
	  {
	    if (buildLocationValues(plsups, "Please select a pickup location", "pickupLocation"))
	    {
	      return true;
	    }
	  }
	
	  lookForValueAndReSelect("pickupLocation", selectedPickupLocation);
  }
  
  
  // pickup location
  
	
  if (document.getElementById("pickupLocation").selectedIndex == 0)
  {
    disable("dropOffDay");
    disable("dropOffMonth");
    disable("dropOffYear");
  	disable("dropOffHour");
  	disable("dropOffLocation");
  	disable("dobDay");
  	disable("dobMonth");
  	disable("dobYear");
  	disable("countryOfOrigin");
  	disable("countriesInto");
  	disable("resetCountries");
  	return (true);
  }
  else
  {
  	enable("dropOffDay");
    enable("dropOffMonth");
    enable("dropOffYear");
  	enable("dropOffHour");
  	
  	locationSelectedNarrowValidRCS(plsups);
  	
  	lookForValueAndReSelect("dropOffDay", selectedDropOffDay);
    lookForValueAndReSelect("dropOffMonth", selectedDropOffMonth);
    lookForValueAndReSelect("dropOffYear", selectedDropOffYear);
    lookForValueAndReSelect("dropOffHour", selectedDropOffHour);
  }
  
  
  // drop-off date section
  
  
  if ((document.getElementById("dropOffDay").selectedIndex == 0) ||
	    (document.getElementById("dropOffMonth").selectedIndex == 0) ||
	    (document.getElementById("dropOffYear").selectedIndex == 0) ||
	    (document.getElementById("dropOffHour").selectedIndex == 0))
  {
  	disable("dropOffLocation");
  	disable("dobDay");
  	disable("dobMonth");
  	disable("dobYear");
  	disable("countryOfOrigin");
  	disable("countriesInto");
  	disable("resetCountries");
  	return (true);
  }
  else
  {
    enable("dropOffLocation");
    
    if ((elementName != "dropOffLocation") &&
	      (elementName != "dobDay") &&
	      (elementName != "dobMonth") &&
	      (elementName != "dobYear") &&
	      (elementName != "countriesInto") &&
	      (elementName != "countryOfOrigin"))
	  {
      buildLocationValues(dlsups, "Please select a drop-off location", "dropOffLocation");
    }
    
    lookForValueAndReSelect("dropOffLocation", selectedDropOffLocation);
  }
  
  
  // drop-off location
  
  
  if (document.getElementById("dropOffLocation").selectedIndex == 0)
  {
	  disable("dobDay");
	  disable("dobMonth");
	  disable("dobYear");
	  disable("countryOfOrigin");
	  disable("countriesInto");
	  disable("resetCountries");
	  return (true);
  }
  else
  {
	  enable("dobDay");
	  enable("dobMonth");
	  enable("dobYear");
	
	  locationSelectedNarrowValidRCS(dlsups);
	
	  if ((elementName != "dobDay") &&
	      (elementName != "dobMonth") &&
	      (elementName != "dobYear") &&
	      (elementName != "countriesInto") &&
	      (elementName != "countryOfOrigin"))
	  {
	    buildDOBYearValues();
    }
	
	  lookForValueAndReSelect("dobDay", selectedDobDay);
    lookForValueAndReSelect("dobMonth", selectedDobMonth);
    lookForValueAndReSelect("dobYear", selectedDobYear);
  }
  
  
  // Date of Birth
  
  
  if ((document.getElementById("dobDay").selectedIndex == 0) ||
	    (document.getElementById("dobMonth").selectedIndex == 0) ||
	    (document.getElementById("dobYear").selectedIndex == 0))
  {
	  disable("countryOfOrigin");
	  disable("countriesInto");
	  disable("resetCountries");
	  return (true);
  }
  else
  {
	  enable("countryOfOrigin");
	
	  if (dobYearSelectedNarrowValidRCS() == false)
	  {
	    lookForValueAndReSelect("countryOfOrigin", selectedCountryOfOrigin);
	    lookForMultipleValuesAndReSelect();
	    return (false);
	  }
	
	  if ((elementName != "countriesInto") &&
	      (elementName != "countryOfOrigin"))
	  {
			//currently not required
    }
	
	  lookForValueAndReSelect("countryOfOrigin", selectedCountryOfOrigin);
  }
  
  
  // Country of Origin
  
  
  if (document.getElementById("countryOfOrigin").selectedIndex == 0)
  {
	  disable("countriesInto");
	  disable("resetCountries");
	  return (true);
  }
  else
  {
	  enable("countriesInto");
	  enable("resetCountries");
	
	  buildCountriesValues();
	
  }
  lookForMultipleValuesAndReSelect();
}

function valueChangedCountries()
{
  getMultipleSelectedValues("countriesInto");

  if (document.getElementById("countriesInto").selectedIndex == 0)
  {
    if (selectedCountriesIntoArray.length == 1)
    {
      if ((selectedCountriesIntoArray[0] == 0) || (selectedCountriesIntoArray[0] == -1))
      {
        if (resetCountriesClicked())
        {
          document.getElementById("countriesInto").selectedIndex = 0;
          return (true);
        }
      }
    }
    var tempArray = new Array();
    var count = 0;
    var found = false;
    for (var i=0; i<selectedCountriesIntoArray.length; i++)
    {
      if ((selectedCountriesIntoArray[i] != 0) && (selectedCountriesIntoArray[i] != -1))
      {
        tempArray[count++] = selectedCountriesIntoArray[i];
      }
    }
    selectedCountriesIntoArray = tempArray;
  }
  
  var trcs = new Array();
  var count = 0;
  
  if (vrcs.length != 0)
  {
    countriesSelectedNarrowValidRCS();
    
    buildCountriesValues();
    
    lookForMultipleValuesAndReSelect();
  }
  else
  {
  // This should never happen or be called as the list will be rebuilt
    return (true);
  }
}

function valueChangedCountriesList()
{
  if (document.getElementById("countriesInto").selectedIndex == 0)
  {
    document.getElementById("countriesInto").selectedIndex = 0;
  }
  
  if (document.getElementById("countriesInto").selectedIndex == -1)
  {
    document.getElementById("countriesInto").selectedIndex = 0;
  }
  
  /*getMultipleSelectedValues("countriesInto");

  if (document.getElementById("countriesInto").selectedIndex == 0)
  {
    if (selectedCountriesIntoArray.length == 1)
    {
      if ((selectedCountriesIntoArray[0] == 0) || (selectedCountriesIntoArray[0] == -1))
      {
        if (resetCountriesClicked())
        {
          document.getElementById("countriesInto").selectedIndex = 0;
          return (true);
        }
      }
    }
    var tempArray = new Array();
    var count = 0;
    var found = false;
    for (var i=0; i<selectedCountriesIntoArray.length; i++)
    {
      if ((selectedCountriesIntoArray[i] != 0) && (selectedCountriesIntoArray[i] != -1))
      {
        tempArray[count++] = selectedCountriesIntoArray[i];
      }
    }
    selectedCountriesIntoArray = tempArray;
  }
  
  var trcs = new Array();
  var count = 0;
  
  if (vrcs.length != 0)
  {
    countriesSelectedNarrowValidRCS();
    
    buildCountriesValues();
    
    lookForMultipleValuesAndReSelect();
  }
  else
  {
  // This should never happen or be called as the list will be rebuilt
    return (true);
  }*/
}

function buildDOBYearValues()
{
  var locations = new Array();
  var count = 0;
  var minAge = 100;
  
  if (vrcs.length != 0)
  {
		for (var p=0; p<vrcs.length; p++) // step through valid RateTables
		{
		  for (var k=0; k<rcsups.length; k++) // step through rateTable TO suppliers Array
		  {
		    if (vrcs[p] == rcsups[k][0]) // find the supplier for the valid rateTable
			  {
			    for (var i=0; i<supsages.length; i++) // step through suppliers minimum ages
			    {
			      if (supsages[i][0] == rcsups[k][1]) // we have found the minimum age allowed by the supplier
			      {
			        if (supsages[i][1] <= minAge)
			        {
			          minAge = supsages[i][1];
			        }
			        break;
			      }
			    }
			    break;
			  }
		  }
		}
	
	  //var today = new Date();
	
	  var dobYears = new Array();
    var dobYearValues = new Array();
    
    dobYears[0] = "Year";
    dobYearValues[0] = "-1";
    
    //(get milliseconds since 1/1/1970 / calc milliseconds in a year) find the years since 1970 then add 1970 to get current year
    //var pickupDateYear = parseInt((pickupDate.getTime() / millisecondsInAYear) + 1970);
    var pickupDateYear = parseInt(document.getElementById("pickupYear").value);
    pickupDateYear = pickupDateYear - minAge;

    for (var i=1; i<((maxAge - minAge) + 2); i++)
    {
        dobYears[i] = pickupDateYear;
        dobYearValues[i] = pickupDateYear;
        pickupDateYear--;
    }
    
	  clearListBoxItems("dobYear");
	  for (var i=0; i<dobYears.length; i++)
	  {
	    addListBoxItem("dobYear", dobYears[i], dobYearValues[i]);
	  }
  }
  else
  {
    unSelect("pickupLocation");
    unSelect("dropOffDay");
    unSelect("dropOffMonth");
    unSelect("dropOffYear");
    unSelect("dropOffHour");
    unSelect("dropOffLocation");
    unSelect("dobDay");
    unSelect("dobMonth");
    unSelect("dobYear");
    unSelect("countryOfOrigin");
    unSelectMulti("countriesInto");
    disable("pickupLocation");
    disable("dropOffDay");
    disable("dropOffMonth");
    disable("dropOffYear");
    disable("dropOffHour");
    disable("dropOffLocation");
    disable("dobDay");
    disable("dobMonth");
    disable("dobYear");
    disable("countryOfOrigin");
    disable("countriesInto");
    disable("resetCountries");
    alert("Unfortunately we do not have rates for your pickup date. Please change your date and try again.");
    return (true);
  }
}

function buildLocationValues(locsToSupsArrayName, firstRowString, comboBoxName)
{
  var locations = new Array();
  var count = 0;
  var found = false;
  
  if (vrcs.length != 0)
  {
		for (var p=0; p<vrcs.length; p++) // step through valid RateTables
		{
		  for (var k=0; k<rcsups.length; k++) // step through rateTable TO suppliers Array
		  {
		    if (vrcs[p] == rcsups[k][0]) // find the supplier for the valid rateTable
			  {
			    for (var q=0; q<locsToSupsArrayName.length; q++) // step through pickupLocations TO suppliers Array entries
			    {
				// skipped second level loop as array has fixed length (1 location to 1..* suppliers) 
				// and we know only suppliers could be more than 1
				    for (var n=0; n<locsToSupsArrayName[q][1].length; n++) // step through suppliers for each location
				    {
				      if (rcsups[k][1] == locsToSupsArrayName[q][1][n]) // find the supplier that matches the valid rateTable supplier
			  	    {
				        for (var i=0; i<locations.length; i++) // check if this location has already been added to locations SET
					      {
					        if (locations[i][0] == locsToSupsArrayName[q][0])
					        {
					          found = true;
					          break;
					        }
					      }
					      if (!found) // if not found then add it
					      {
					        for (var j=0; j<locs.length; j++) // step through the locations array
					        {
					          if (locs[j][0] == locsToSupsArrayName[q][0]) // if the locationID is the same we now have its name
					          {
					            locations[count++] = new Array(locs[j][0], locs[j][1]);
					  	        found = false;
					  	        break;
					  	      }
					        }
					      }
					      found = false;
			  	    }
			  	  }
			    }
			    break;
			  }
	    }
	  }
	
	  locations.sort(sortByName);
	
	  var tempArray = new Array();
	  
	  tempArray[0] = new Array(-1, firstRowString);
	  for (var i=0; i<locations.length; i++)
	  {
	    tempArray[i+1] = new Array(locations[i][0], locations[i][1]);
	  }
	  
	  //TODO replace below function call with just a length=0
	  clearListBoxItems(comboBoxName);
	  for (var i=0; i<tempArray.length; i++)
	  {
	    addListBoxItem(comboBoxName, tempArray[i][1], tempArray[i][0]);
	  }
  }
  else
  {
    if (comboBoxName == "pickupLocation")
    {
      unSelect("pickupLocation");
      unSelect("dropOffDay");
      unSelect("dropOffMonth");
      unSelect("dropOffYear");
      unSelect("dropOffHour");
      unSelect("dropOffLocation");
      unSelect("dobDay");
      unSelect("dobMonth");
      unSelect("dobYear");
      unSelect("countryOfOrigin");
      unSelectMulti("countriesInto");
      disable("pickupLocation");
	    disable("dropOffDay");
	    disable("dropOffMonth");
	    disable("dropOffYear");
	    disable("dropOffHour");
	    disable("dropOffLocation");
	    disable("dobDay");
	    disable("dobMonth");
	    disable("dobYear");
	    disable("countryOfOrigin");
	    disable("countriesInto");
	    disable("resetCountries");
	    alert("Unfortunately we do not have rates for your pickup date. Please change your date and try again.");
    }
    else
    {
      disable("dropOffLocation");
	    disable("dobDay");
	    disable("dobMonth");
	    disable("dobYear");
	    disable("countryOfOrigin");
	    disable("countriesInto");
	    disable("resetCountries");
	    alert("Unfortunately we do not have rates for your selection. Please change your selection and try again.");
    }
    return (true);
  }
}

function buildCountriesValues()
{
  var countries = new Array();
  var count = 0;
  var found = false;
  
  if (vrcs.length != 0)
  {
		for (var p=0; p<vrcs.length; p++) // step through valid RateTables
		{
		  for (var k=0; k<rcsups.length; k++) // step through rateTable TO suppliers Array
		  {
		    if (vrcs[p] == rcsups[k][0]) // find the supplier for the valid rateTable
				{
				  for (var q=0; q<supsc.length; q++) // step through suppliers TO countries taking cars into Array
				  {
					// skipped second level loop as array has fixed length (1 location to 1..* suppliers) 
					// and we know only suppliers could be more than 1
						if (supsc[q][0] == rcsups[k][1]) // found the suppliers countries allowed into for this supplier
						{
						  for (var n=0; n<supsc[q][1].length; n++) // step through each country in sups to countries array
						  {
						    for (var m=0; m<countries.length; m++) // step through the countries set
						    {
						      if (countries[m][0] == supsc[q][1][n])
						      {
						        found = true;
							      break;
						      }
						    }
						    if (!found) // if not found then add it
							  {
							    for (var j=0; j<cos.length; j++) // step through the countries allowed into array
							    {
							      if (cos[j][0] == supsc[q][1][n]) // if the locationID is the same we now have its name
							      {
							        countries [count++] = new Array(cos[j][0], cos[j][1]);
							  	    found = false;
							  	    break;
							  	  }
							    }
							  }
							  found = false;
						  }
						  break;
						}
			    }
			  break;
			  }
		  }
		}
	  
		countries.sort(sortByName);
		  
		var tempArray = new Array();
		  
		tempArray[0] = new Array(-1, "Not Leaving the Country");
		for (var i=0; i<countries.length; i++)
		{
		  tempArray[i+1] = new Array(countries[i][0], countries[i][1]);
		}
		  
		//TODO replace below function call with just a length=0
		clearListBoxItems("countriesInto");
		for (var i=0; i<tempArray.length; i++)
		{
		  addListBoxItem("countriesInto", tempArray[i][1], tempArray[i][0]);
		}
  }
}

function countriesSelectedNarrowValidRCS()
{
  var trcs = new Array();
  var count = 0;
  var foundAll = false;
  var found = false;
  	
  for (var i=0; i<vrcs.length; i++) // step through all valid rateTables
  {
    for (var j=0; j<rcsups.length; j++) // step through all ratetalbe to suppliers
    {
      if (vrcs[i] == rcsups[j][0]) // if ratetable the same ... we found our supplier [j][1]
      {
        for (var m=0; m<supsc.length; m++) // step through suppliers to countries allowed to take vehicle into array
        {
          if (supsc[m][0] == rcsups[j][1]) // if we find the supplier we have the countries list countries
          {
            for (var p=0; p<selectedCountriesIntoArray.length; p++) // step through all clients selected countries so far
            {
              for (var n=0; n<supsc[m][1].length; n++) // step through all the suppliers countries
              {
                if (supsc[m][1][n] == selectedCountriesIntoArray[p])
                {
                  //trcs[count++] = vrcs[i];
                  //foundAll = true;
                  found = true;
                  break;
                }
              }
              if (found)
              {
                foundAll = true;
                found = false;
              }
              else
              {
                foundAll = false;
                break; // break because this supplier has failed in catering for all selected countries
              }
            }  
            if (foundAll)
            {
              trcs[count++] = vrcs[i];
            }
            else
            {
              
            }
          }
        }
      }
    }
  }
  vrcs = trcs;
}

function locationSelectedNarrowValidRCS(locsToSupsArrayName)
{
  var trcs = new Array();
  var count = 0;
  	
  for (var i=0; i<vrcs.length; i++) // step through all valid rateTables
  {
    for (var j=0; j<rcsups.length; j++) // step through all ratetalbe to suppliers
    {
      if (vrcs[i] == rcsups[j][0]) // if ratetable the same ... we found our supplier [j][1]
      {
        for (var m=0; m<locsToSupsArrayName.length; m++) // step through all the pickuplocations to suppliers
        {
          if (locsToSupsArrayName[m][0] == selectedPickupLocation) // if we find the location the client selected
          {
            for (var n=0; n<locsToSupsArrayName[m][1].length; n++) //then step through all the pickuplocations suppliers to see if the valid ratetable still applies
            {
              if (locsToSupsArrayName[m][1][n] == rcsups[j][1])
              {
                trcs[count++] = vrcs[i];
              }
            }
          }
        }
      }
    }
  }
  vrcs = trcs;
}

function dobYearSelectedNarrowValidRCS()
{
  var trcs = new Array();
  count = 0;
  
  var selectedDay = getSelectedValue("dobDay");
  var selectedMonth = getSelectedValue("dobMonth");
  var selectedYear = getSelectedValue("dobYear");
  
  var dateOfBirth = new Date(selectedYear, selectedMonth-1, selectedDay, 0, 0, 0, 0);
  //var today = new Date();
  var pickupDayVal = parseInt(document.getElementById("pickupDay").value);
	var pickupMonthVal = parseInt(document.getElementById("pickupMonth").value);
	var pickupYearVal = parseInt(document.getElementById("pickupYear").value);
	  
  var pickupDate = new Date(pickupYearVal, pickupMonthVal-1, pickupDayVal, 0, 0, 0, 0);
  
  var dobTemp = (dateOfBirth.getTime() / millisecondsInAYear) + 1970;
  var pickupDateTemp = (pickupDate.getTime() / millisecondsInAYear) + 1970;
  var clientAge = parseInt((pickupDateTemp - dobTemp));
  
  for (var i=0; i<vrcs.length; i++) // step through all valid rateTables
  {
    for (var j=0; j<rcsups.length; j++) // step through all ratetalbe to suppliers
    {
      if (vrcs[i] == rcsups[j][0]) // if ratetable the same ... we found our supplier [j][1]
      {
        for (var m=0; m<supsages.length; m++) // step through all suppliers to min ages
        {
          if (supsages[m][0] == rcsups[j][1]) // if supplier the same ... we found our min Age [m][1]
          {
            if (clientAge >= supsages[m][1]) //if clientAge > than the minimum allowed suppliers age then RateTable is Valid
            {
              trcs[count++] = vrcs[i];
            }
          }
        }
      }
    }
  }
      
  vrcs = trcs;
  
  if (vrcs.length != 0)
  {
    return (true);
  }
  else
  {
    unSelect("countryOfOrigin");
    unSelectMulti("countriesInto");
    disable("countryOfOrigin");
    disable("countriesInto");
    disable("resetCountries");
    alert("Unfortunately we do not have rates for the requested age group.");
    return (false);
  }
}

function storePlaceHolders()
{
  selectedPickupLocation = getSelectedValue("pickupLocation");
  selectedDropOffLocation = getSelectedValue("dropOffLocation");
  selectedDobDay = getSelectedValue("dobDay");
  selectedDobMonth = getSelectedValue("dobMonth");
  selectedDobYear = getSelectedValue("dobYear");
  selectedCountryOfOrigin = getSelectedValue("countryOfOrigin");
  getMultipleSelectedValues("countriesInto");
}

function lookForValueAndReSelect(elementName, storedValue)
{
  if (storedValue != -1)
  {
		var pl = document.getElementById(elementName)
		for (var i=0; i<pl.length; i++)
		{
		  if (pl.options[i].value == storedValue)
		  {
		    pl.selectedIndex = i;
		  }
		}
  }
}

function lookForMultipleValuesAndReSelect()
{
  for (var j=0; j<selectedCountriesIntoArray.length; j++)
  {
		if (selectedCountriesIntoArray[j] != -1)
		{
		  var pl = document.getElementById("countriesInto")
		  for (var i=0; i<pl.length; i++)
		  {
				if (pl.options[i].value == selectedCountriesIntoArray[j])
				{
				  pl.options[i].selected = true;
				}
		  }
		}
  }
}

function getSelectedValue(name)
{ 
  var ele = document.getElementById(name);
  if (ele.length != 0)
  {
  	return ele.options[ele.selectedIndex].value;
  }
}

function getMultipleSelectedValues(name)
{
  selectedCountriesIntoArray = new Array();
  var count = 0;
  
  var ele = document.getElementById(name);
  for (var i=0; i<ele.options.length; i++)
  {
    if (ele.options[i].selected)
    {
      selectedCountriesIntoArray[count++] = ele.options[i].value;
    }
  }
}

function disable(elementName)
{  	  
  document.getElementById(elementName).disabled=true;
}

function enable(elementName)
{  	  
  document.getElementById(elementName).disabled=false;
}

function unSelect(elementName)
{  	  
  document.getElementById(elementName).selectedIndex = 0;
}

function unSelectMulti(elementName)
{  	  
  document.getElementById(elementName).selectedIndex = -1;
}

function addListBoxItem(listBoxName, sName, sValue)
{
  var listBox = document.getElementById(listBoxName);
  listBox.options[listBox.length] = new Option(sName, sValue);
}

function clearListBoxItems(listBoxName)
{
  var listBox = document.getElementById(listBoxName);
  listBox.length = 0;
}

function sortByName(a, b)
{
  if (a[1]<b[1]) return -1;
  if (a[1]>b[1]) return 1;
  return 0;
}