function get(id) {
	if(typeof(id)=='object')
    	return id;
	return document.getElementById(id);
}

function getval(elem) {
    elem = get(elem);
	//alert(elem);
    switch(elem.type) {
    case "select-one":
        return elem.options.length<=0?null:elem.options[elem.selectedIndex].value;
    case "checkbox":
        return elem.checked?elem.value:false;
    default:
        return elem.value;
    }
}

function setval(elem, value) {
  //alert('set '+elem.name+' to '+value);
  if(value==null) {
    value='';
  }
  if(elem==null)
	return;
	//alert("setval with null element: "+value);

  if(typeof elem != "object")
    return;

  if(typeof elem.tagName=="undefined" && elem.length) {
      var chk = 0;
      for(var k=0; k<elem.length; k++)
	  if(elem[k].value==value)
	      chk = k;
      elem[chk].checked = true;
      return;
  }


  switch(elem.type) {
    case "checkbox":
    case "radio":
      elem.checked = value && value!="0"?true:false;
      break;
    case "select-one":
        if(!elem.options || !elem.options.length)
            break;
        var found = false;
        for(var i=0; i<elem.options.length; i++) {
            //if(elem.name=='excess') alert(value+' == '+elem.options[i].value);
            if(elem.options[i].value == value) {
                elem.options[i].selected = true;
                found = true;
                break;
            }
        }
        if(!found)
            elem.options[0].selected = true;
        break;
  default:
      elem.value = value;
  }
}

function redirect(url) {
	window.location = url;
}

function build_url(url, params) {
  if(params) {
    var sep = '?';
    for(var name in params) {
        url += sep+escape(name)+'='+escape(params[name]);
        sep = '&';
    }
  }
  return url;
}

function show(elem, really) {
	if(typeof really=="undefined") really = true;
	get(elem).style.display = really? '': 'none';
}

function hide(elem, really) {
	if(typeof really=="undefined") really = true;
	show(elem, !really);
}

function afis2(obj) {
  var buf;
  if(typeof obj == 'object') {
    buf = '';
    for(var i in obj) {
      var value;
      try { value = typeof obj[i] == 'function'? '***function***': obj[i];
      } catch(e) { value = '***N/A***'; }
      buf += i+' -> '+value+', ';
    }
  }
  else {
    buf = obj;
  }
  alert(buf);
}


function insertAtCursor(myField, myValue) {
	//IE support
	if (document.selection) {
		myField.focus();

		//in effect we are creating a text range with zero
		//length at the cursor location and replacing it
		//with myValue
		sel = document.selection.createRange();
		sel.text = myValue;
	}

	//Mozilla/Firefox/Netscape 7+ support
	else if (myField.selectionStart || myField.selectionStart == '0') {

		//Here we get the start and end points of the
		//selection. Then we create substrings up to the
		//start of the selection and from the end point
		//of the selection to the end of the field value.
		//Then we concatenate the first substring, myValue,
		//and the second substring to get the new value.
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
	} else {
		myField.value += myValue;
	}
	myField.focus();
} 

function insertAtCaret(areaId,text) {
	var txtarea = get(areaId); //document.getElementById(areaId);
	var scrollPos = txtarea.scrollTop;
	var strPos = 0;
	var br = ((txtarea.selectionStart || txtarea.selectionStart == '0') ? 
		"ff" : (document.selection ? "ie" : false ) );
	if (br == "ie") { 
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		strPos = range.text.length;
	}
	else if (br == "ff") strPos = txtarea.selectionStart;
	
	var front = (txtarea.value).substring(0,strPos);  
	var back = (txtarea.value).substring(strPos,txtarea.value.length); 
	txtarea.value=front+text+back;
	strPos = strPos + text.length;
	if (br == "ie") { 
		txtarea.focus();
		var range = document.selection.createRange();
		range.moveStart ('character', -txtarea.value.length);
		range.moveStart ('character', strPos);
		range.moveEnd ('character', 0);
		range.select();
	}
	else if (br == "ff") {
		txtarea.selectionStart = strPos;
		txtarea.selectionEnd = strPos;
		txtarea.focus();
	}
	txtarea.scrollTop = scrollPos;
}


function afis(obj) {
	alert(dump(obj));
	
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

