function getWidget(widget,tag,input_id,target_div){
  //alert('I was given the following tag number: '+tag);
  //alert(input_id);
  var inputs='tag_id:'+tag+';';
  if (input_id!=null){
      var input=document.getElementById(input_id);
      //alert('input type:'+input.type+'  input value:'+input.value);
      switch(input.type){
        case "text":
        case "textarea": 
            inputs=inputs+input_id+':'+ajaxencode(escape(input.value))+';';
            break;
        case "select-one":
            inputs=inputs+input_id+':'+ajaxencode(escape(input.value))+';';
            break;
        case "select-multiple":
          var selected = new Array();
          var index = 0;
          inputs=inputs+input_id+':';
          for (var intLoop=0; intLoop < input.length; intLoop++) {
             if (input[intLoop].selected) {
                inputs=inputs+input[intLoop].value+'`';
             }
          }
          inputs=inputs+';';
          break;
        default: input_value='';break;
      }
  }
  if (target_div !=null)
  {
    inputs=inputs+'ajaxlink:true;';
    //alert('Calling get widget with: widget='+widget+'   inputs='+inputs+'   target_div='+target_div);
    x_getWidget(widget,inputs,setWidget,target_div);
  }
  else
  {
    //alert('Calling get widget with: widget='+widget+'   inputs='+inputs+'   tag='+tag);
    x_getWidget(widget,inputs,setWidget,tag);
  }

  
}

function ajaxencode(string){
  var result='';
  for (var i = 0; i <  string.length; i++) {
    if (string.charAt(i) == ":") result += "%58";
    else if (string.charAt(i) == ";") result += "%59";
    else result += string.charAt(i);
  }
  return result;
}

/*
function gettagname(string){
  var result='';
  var tagname=false;
  for (var i = 0; i <  string.length; i++) {
    if (tagname==true)
        result+=string.charAt(i);
    if (tagname!=true && string.charAt(i) == ".") 
        tagname=true;
  }
  return result;
}
*/

function setWidget(stuff,tag_div_id){
  //alert('setWidget called! tag_div_id is: '+tag_div_id);
  var tag=document.getElementById(tag_div_id);
  //alert('tag is: '+tag.innerHTML);
  //tag.innerHTML=stuff;
  
  //The stuff is two html div blocks - one div block is the widget ("widget_content"), the other is the debug info ("widget_debug")
  var stage=document.getElementById('widget_staging_area');
  if (!stage){
    alert('Your template must include a hidden div called "widget_staging_area"');
    return false;
  }
  var widget_debug=document.getElementById('debugcontent');
  stage.innerHTML=stuff;
  tag.innerHTML=document.getElementById('widget_content').innerHTML;
  if (widget_debug)
    widget_debug.innerHTML=document.getElementById('widget_debug').innerHTML;
  stage.innerHTML='';
}

