/*
 * (C) 2006 Inter Age S.r.l.
 *
 * $Source: $
 * $Id: $
 * 
 * Author: Andrea Beggi
 * Desc: Javascript functions used by the Mystral Framework
 */
 
/**
 * Returns the DOM object by the given name
 * @param name The name of the DOM object
 * @return The DOM object by the given name
 */
function getObjectByName(name) {
  var obj = null;
  if (document.getElementById)   {
  	 obj = document.getElementById(name);
  } else if (document.all)  {
  	 obj = document.all[name];
  } else if (document.layers)  {
  	 obj = document.layers[name];
  }
  return obj;
}

/**
 * Add a value to a DynaSelect
 * @param _selName The name of the select
 */
function DynaSelectAdd(_selName) {
	var selbox = getObjectByName(_selName);
	var obj = getObjectByName(_selName + '_value');
	var valObj = getObjectByName(_selName + 'Values');
	var chosen = obj.value;
	var values = '';
	/*
	 * check if the value is required, if it is so:
	 * don't allow to add an empty string
	 */
  	if ((selbox.required == 'true') && (chosen == '')) {
      		return;
  	}

  	if (selbox.options[0].value == ' ') {
      		selbox.options.length = 0;
	}
	var fnd = false;
 	/*check if the value already exists */
	for (n = 0; n < selbox.length; n++){
		if (selbox.options[n].text == chosen){
			fnd = true;
		}
	}
	if (!fnd) {
     	/*add the new option to the select */
		selbox.options[selbox.options.length] = new Option(chosen, selbox.options.length);
	}
 	/*update the selection in the select */
	for (n = 0; n < selbox.length; n++){
    		values = values + selbox.options[n].text + ';';
		if (selbox.options[n].text == chosen){
			selbox.options[n].selected = true;
		} else {
			selbox.options[n].selected = false;
    		}
	}
  	/* update the values stored in the hidden field */
  	
  	valObj.value = values;
}

/**
 * Remove a value to a DynaSelect
 * @param _selName The name of the select
 */
function DynaSelectDel(_selName) {
	var selbox = getObjectByName(_selName);
	var valObj = getObjectByName(_selName + 'Values');
	var values = '';
	/*
	 * check if the field is required, 
	 * if it is so don't delete the first element 
	 */
 	if ((selbox.required == 'true') && (selbox.options.length == 1)) {
      		return;
 	}

	if (selbox.options[0].value != ' ') {
		nomatch = new Array();
		for (n = 0; n < selbox.length; n++){
			if (!selbox.options[n].selected){
				/* keep only the not selected ones */
				nomatch[nomatch.length] = new Array(selbox.options[n].value, selbox.options[n].text);
			}
		}
		/* reset the select */
		selbox.options.length = 0;
		/* if we have removed the last element */		
		if (nomatch.length == 0) {		
			/* insert an empty option */
			selbox.options[0]= new Option('',' ');
		} else {
			for (n = 0; n < nomatch.length; n++){
				selbox.options[n] = new Option(nomatch[n][1], nomatch[n][0]);
			}
		}
	}
	/* update the selection in the select */
	for (n = 0; n < selbox.length; n++){
		values = values + selbox.options[n].text + ';';
	}
	/* update the values stored in the hidden field */
	valObj.value = values;
}
