// ------ SELECT ----------

function isThereValueInCombo(combo, value) {
  return (IndexOfValueInCombo(combo, value) > -1);
}

function IndexOfValueInCombo(combo, value) {
  if (combo) {
    for (var i = 0; i<combo.options.length; i++) {
      if (combo.options[i].value == value) {
         return i
      }
    }
  }
  return -1
}

function AddOption(combo, option, beforeIndex)
{
  if (combo && option) {
      try {
        // standards compliant; doesn't work in IE
        if (beforeIndex && combo.options[beforeIndex]) {
          combo.add(option, combo.options[beforeIndex]); 
        } else {     
          combo.add(option, null); 
        }
      }
      catch(ex) {    
        // IE only
        if (beforeIndex) {
          combo.add(option, beforeIndex);
        } else {
          combo.add(option); 
        }
      }   
  }
}

function AddOptionInCombo(combo, value, text) {
    var newOption = document.createElement('option');   
    newOption.text = text;         
    newOption.value = value;  
    AddOption(combo, newOption);
}

function ClearCombo(combo) {
  if (combo) {
    combo.disabled = true;
    combo.options.length = 0;
    var newOption = document.createElement('option');   
    var def = "Няма елементи";
    newOption.text = '-- ' + (window.RS_NO_OPTIONS || def) + ' --';         
    newOption.value = -1;  
    AddOption(combo, newOption);
  } 
}

function EnableCombo(combo) {
  if (combo) {
    combo.options.length = 0;
    var newOption = document.createElement('option');   
    newOption.text = '-- '+(window.RS_CHOOSE || 'Избор')+' --';         
    newOption.value = -1;  
    AddOption(combo, newOption);   
    combo.disabled = false;
  }  
}

function StartLoadingCombo(combo, dontClear){
  if (combo) {
    if (!dontClear) combo.options.length = 0;
    var newOption = document.createElement('option');   
    newOption.text = '-- '+(window.RS_LOADING || 'Зареждане') + ' --';         
    newOption.value = -1;  
    AddOption(combo, newOption, 0);   
    combo.disabled = true;
  }
}

function EndLoadingCombo(combo, notCleared) {
  if (combo) {
   if (notCleared) {
     if (combo.options[0].value == -1 && combo.options[0].text == '-- '+(window.RS_LOADING || 'Зареждане') + ' --') {
       combo.remove(0);
     } 
     if (combo.options[0].text == '-- '+(window.RS_CHOOSE || 'Избор') + ' --') {
       combo.options[0].text = '-- '+(window.RS_NO_OPTIONS || 'Няма елементи')+' --';
     } else {
       combo.disabled = false;
     }
   } else {
     if (combo.options.length == 1) {
       combo.options[0].text = '-- '+(window.RS_NO_OPTIONS || 'Няма елементи')+' --';
     } else { 
       combo.disabled = false;
       combo.options[0].text = '-- '+(window.RS_CHOOSE || 'Избор')+' --';
     }
   }
  }  
}

function SetFocusToFirst( aForm )
{
  if (aForm) {
	if( aForm.elements[0]!=null) {
		var i;
		var max = aForm.length;
		for(var i = 0; i < max; i++ ) {
			if( aForm.elements[ i ].type != "hidden" &&
				!aForm.elements[ i ].disabled &&
				!aForm.elements[ i ].readOnly && aForm.elements[ i ].style.display != 'none') {
				aForm.elements[ i ].focus();
				break;
			}
		}
	}
	}
	
}

function LoadCombo(combo, data) {
  for(var i = 0; i< data.length; i++) {
    var newOption = document.createElement('option');   
    newOption.text = data[i].text;         
    newOption.value = data[i].value;  
    AddOption(combo, newOption);
  }
}

