// alert("Here in js/ajax.js!");

function CleanParam(param)
{
  return param.replace(" ", "--");
}

function removeAllOpt(targetComboBoxId)
{
	var selectControl = document.getElementById(targetComboBoxId);

	for(var i = selectControl.options.length-1; i >= 0; i--)
		selectControl.remove(i);
}

function addCbOption(selectbox, value, text)
{
	var optn = document.createElement("OPTION");
	optn.value = value;
	optn.text = text;		
	selectbox.options.add(optn); 
}

// OK
function ajaxPopulateSELECT(e, scriptToCall, targetSELECTid, defaultStr, otherParams, SELECTidsToEmpty)
{  
  var URL = (typeof(otherParams) != "undefined") ? scriptToCall + "/" + e.value + "/" + otherParams : scriptToCall + "/" + e.value;
  URL = CleanParam(URL);
  $.post(URL, function(data){ 
    $("#"+targetSELECTid).empty();
    var elem = document.getElementById(targetSELECTid);
    if (data)
    {
      if (typeof(defaultStr) != "undefined")
        addCbOption(elem, 0, defaultStr);
      $.each(data, function(key, val){			
				addCbOption(elem, key, val);
			});	
    }	
   if (typeof(SELECTidsToEmpty) != "undefined")
    {
      var idarr = SELECTidsToEmpty.split(",");
      for(var i in idarr)
        $("#"+idarr[i]).empty(); 
    }			
  }, "json");
}

// OK
function ajaxPopulateCHECKBOX(scriptToCall, sourceContainerID, targetContainerID, targetName, onClickFunction, emptyContainers)
{ 
  var arr = new Array();      
    $.each($("#" + sourceContainerID + " input[type=checkbox]:checked"), function() {
    arr.push($(this).val());
  });
  var strcid = arr.toString();
  if (strcid == "")
    $("#"+targetContainerID).empty();
  else
    $.post(scriptToCall, {strcid: strcid}, function(data){ 
      $("#"+targetContainerID).empty();
      $.each(data, function(key, val){
        $("#"+targetContainerID).append( 
          $(document.createElement("input"))
            .attr({
                type   : 'checkbox',
                checked: false,
                id     : targetContainerID + key,
                name   : targetName,
                value  :  key})
            .click(onClickFunction) 
            )
            .append(
              $(document.createElement('label'))
               .attr({'for':  targetContainerID + key})
               .text(val))} );
      if (typeof(emptyContainers) != "undefined")
      {
        var items = emptyContainers.split();  
        for(var i in items)
          $("#"+items[i]).empty();
      }
    }, "json");     
}


function ajaxPopulateCOUNT(e, scriptToCall, targetSELECTid, defaultStr, otherParams, SELECTidsToEmpty)
{  
  var URL = (typeof(otherParams) != "undefined") ? scriptToCall + "/" + e.value + "/" + otherParams : scriptToCall + "/" + e.value;
  URL = CleanParam(URL);
  $.post(URL, function(data){ 
    $("#"+targetSELECTid).empty();  
    if (data)
    {
    $("#result_count").html(data);
    }	
    if (typeof(SELECTidsToEmpty) != "undefined")
      for(var i in SELECTidsToEmpty)
        $("#"+SELECTidsToEmpty[i]).empty();					
  }, "json");
}


