var maxLen = 130;

function selectRegions(target,sourceArray,title, cleraTarget)
{
  selectType(target, sourceArray, title, clearTarget);
}

function selectType(target, sourceArray, title, clearTarget)
{
  if (sourceArray && sourceArray.length > 0)
  {
    target.options.length = sourceArray.length + 1;
    target.options[0].text = '-- ' + title;
    target.options[0].value = -1;

    for (i = 0; i < sourceArray.length; i++)
    {
      target.options[i + 1].value = sourceArray[i].substr(0, sourceArray[i].indexOf('|'));
      target.options[i + 1].text = sourceArray[i].substr(sourceArray[i].indexOf('|') + 1, sourceArray[i].length);
    }
    target.options.selectedIndex = 0;
    target.disabled = false;
  }
  else
  {
    target.selectedIndex = 0;
    target.disabled = true;
  }

  if (clearTarget)
  {
    clearTarget.selectedIndex = 0;
    clearTarget.disabled = true;
  }
}

function checkLen()
{
  var totalLen = generateAdvertText().length;

  if (totalLen > maxLen)
  {
    charsLeft = 0;
  }
  else
  {
    charsLeft = maxLen - totalLen;
  }

  document.getElementById("cnt").innerHTML = charsLeft;
}

function updateEquipment(target)
{
  if (target.checked)
  {
    addEquipment(target.value);
  }
  else
  {
    removeEquipment(target.value);
  }
}

function addEquipment(equipmentID)
{
  checkedEquipment[checkedEquipment.length] = equipmentID;
}

function removeEquipment(equipmentID)
{
  var newArray = new Array();
  for (i in checkedEquipment)
  {
    if (checkedEquipment[i] != equipmentID)
    {
      newArray[newArray.length] = checkedEquipment[i];
    }
  }
  checkedEquipment = newArray;
}

function generateAdvertText()
{
  var temp = "";

 if (document.mainForm.description.value != '')
  {
    temp += document.mainForm.description.value;
    temp += ". ";
  }
  var eqTemp= new Array();
  for (i in checkedEquipment){
    eqTemp[i]=equipment[checkedEquipment[i]];
  }
  var eqSort = eqTemp.sort();
  for (i in eqSort)
  {
    if (i > 0)
    {
      temp += ", ";
    }

    temp += eqSort[i];
  }

  temp += (eqSort.length > 0) ? "." : "";

  return temp;
}

function getObj(name)
{
  if (document.getElementById)
  {
    return document.getElementById(name);
  }
  else if (document.all)
  {
    return document.all[name];
  }
  return null;
}


