//-----------------------------------------
// DWR scripts

// Include this script in any jsp where you
// wich to make dwr (ajax) calls. 
// Don't forget to call the function pcsInit
// in the onLoad event to test if the cliente
// browser suports dynamic options.
//-----------------------------------------

var optionTest = true;

function getOptions(queryType, targetId, conditionFieldId_1, conditionFieldId_2){
  var reply = function(data){
  	if (data != null){
			var target = document.getElementById(targetId);
			for(i=0 ; i<data.length ; i+=2){
				target.options[i/2+1] = new Option(data[i],data[i+1]);
			}
			var opt = target.options;
			while(opt.length > (data.length/2)+1){
				opt[opt.length-1] = null;
			}
  	}
  };
  if(optionTest){
	  var criteria1 = document.getElementById(conditionFieldId_1);
	  if(criteria1)
	  	criteria1= criteria1.value;
	  var criteria2 = document.getElementById(conditionFieldId_2);
	  if(criteria2)
	  	criteria2= criteria2.value;
  	DwrManager.getOptions(queryType, criteria1, criteria2, reply);
  }
}

//Test if browser supports dynamic options.
//This function must be runned in the onLoad
//event of the page to determine if the cliente
//browser suports dynamic options.
function pcsInit(formName, testComboName){
	optionTest = true;
	var lgth = document.forms[formName].elements[testComboName].options.length;

	document.forms[formName].elements[testComboName].options[lgth] = new Option("_","_");
	if (document.forms[formName].elements[testComboName].options[lgth])
		document.forms[formName].elements[testComboName].options[lgth] = null;	
	else
		optionTest = false;
}


