//----------------------------------------------------------------------------
// getElementById
//----------------------------------------------------------------------------
function gebi(id){
	var el=document.getElementById(id);
	if(el==null){
  	alert('gebi: cannot find element by id "'+id+'"');
 	}
 	else{
		return el;
	}
}
//----------------------------------------------------------------------------
// HTML encode/decode
//----------------------------------------------------------------------------
String.prototype.encodeHTML=function(){
	return this.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}
String.prototype.decodeHTML=function(){
	return this.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>');
}
//------------------------------------------------------------------------------
// sets list of values for given combo
//------------------------------------------------------------------------------
function fill_combo(combo_idobj,list,value)
{
  fill_combo(combo_idobj,list,value,false);
}
function fill_combo(combo_idobj,list,value,required)
{
  //alert('fill_combo:'+combo_idobj);
  var combo;
  var opt;
  var selIndex=0;
  var selOpt=null;
	if(typeof combo_idobj=='object'){
  	combo=combo_idobj;
	}
	else{
  	combo=document.getElementById(combo_idobj);
	}
	//alert('fill_combo:'+combo.id+'->'+value+' options:'+list.length);
	//alert(list);
	if(combo){
  	while(combo.length>0)
  	{
			combo.remove(0);
		}
		if(!required){
			//alert('adding empty option');
			opt=new Option('','');
			opt.selected=true;
			combo.options.add(opt);
		}
		//combo.selectedIndex=required?0:1;
		for(var i=0;i<list.length;i++)
		{
			//alert('adding option #'+i);
  		var le=list[i];
			//alert('adding option: '+le+' type[id]:'+typeof le["id"]+' type[0]:'+typeof le[0]);
		  opt=new Option();
			opt.value=(le[0]==undefined)?le["id"]:le[0];
			opt.text =(le[1]==undefined)?le["name"]:le[1];
			combo.options.add(opt);
			if(opt.value==value){
				selIndex=i+(required?0:1);
				//alert(combo.selectedIndex);
				//combo.selectedIndex=selIndex;
				opt.selected=true;
				//opt.defaultSelected=true;
				//selOpt=opt;
				//setTimeout(function(){combo.selectedIndex=selIndex},500);
			}
		}
		if(selIndex>0) {
			//opera bug - does NOT seem to be fixed in 9.50
			//combo.selectedIndex=selIndex;
			//setTimeout(function(){combo.selectedIndex=selIndex},500);
		}
		if(selOpt){
    	//selOpt.selected=true;
		}
		//combo.style.display=(list.length==0)?'none':'';
	}
	else{
  	alert('Cannot find combo:'+combo_idobj);
	}
}
//----------------------------------------------------------------------------
// sets value for day, month and year elements of date_input set
//----------------------------------------------------------------------------
function date_input_set(id_prefix,id_suffix,value)
{
	dh=document.getElementById('date'+id_suffix);
	d=document.getElementById(id_prefix+'Day' +id_suffix);
	m=document.getElementById(id_prefix+'Month'+id_suffix);
	y=document.getElementById(id_prefix+'Year'  +id_suffix);
	dh.value=value;
	var a=value.split('-');
	y.value=a[0];
	m.value=a[1];
	d.value=a[2];
}
//----------------------------------------------------------------------------
// submit form asynchronously
//----------------------------------------------------------------------------
function asyncSubmitJson(form,fun){
  var pars='';
	var message;  
	for(var i=0;i<form.elements.length;i++){
	  var el=form.elements[i];
	  if(el.name){
	  	switch(el.type){
			case 'hidden':
			case 'text':
			case 'password':
			case 'textarea':
			case 'select-one':
			case 'submit':
				break;
			case 'radio':
			case 'checkbox':
				if(!el.checked) continue;
				break;
			default:
				alert('async_submit: unknown element type "'+el.type+'"');
				continue;
			}
			pars+=(pars?'&':'')+encodeURIComponent(el.name)+'='+encodeURIComponent(el.value);
		}
	}
	var url=(form.action=="")?window.location.pathname:form.action;
	switch(form.method.toLowerCase()){
  	case "":
  	case "get":
  		url+="?"+pars;
  		message="";
 		break;
 		case "post":
 			message=pars;
		break;
		default:
			alert("async_submit: invalid form method '"+form.method+"'");
		return false;
 	}
	//alert(url+"\n"+fun+"\n"+form.method+"\n"+new Array(new Array("Content-Type","application/x-www-form-urlencoded"))+"\n"+message);
	asyncRequestJson(url,fun,form.method,new Array(new Array("Content-Type","application/x-www-form-urlencoded")),message);
	return false;
}

