//----------------------------------------------------------------------------
// getElementById
//----------------------------------------------------------------------------
function gebi(id,silent){
	var el=document.getElementById(id);
	if(el==null){
	  if(silent!=true){
			alert('gebi: cannot find element by id "'+id+'"');
		}
 	}
 	else{
		return el;
	}
}
//----------------------------------------------------------------------------
// Force location.reload from server
//----------------------------------------------------------------------------
window.location.refresh=function(){
	window.location.reload(true);
}
/*
try{
	Location.reload=function(){
		this.reload(true);
	}
}
catch(e){
	//Location object does not exist in Chrome
}
*/
//----------------------------------------------------------------------------
// exchange values of elements
//----------------------------------------------------------------------------
function exchange_vals(el1,el2){
  if(el1.tagName!==el2.tagName){
    alert('exchange_vals: cannot exchange elements of different types');
    return;
	}
	switch(el1.tagName){
	case 'INPUT':
		var tempVal=el1.value;
		el1.value=el2.value;
		el2.value=tempVal;
	break;
	case 'BUTTON':
	case 'SPAN':
		var tempVal=el1.innerHTML;
		el1.innerHTML=el2.innerHTML;
		el2.innerHTML=tempVal;
	break;
  default:
    alert('exchange_vals: unknown element type: '+el1.tagName);
	}
}
//----------------------------------------------------------------------------
// 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,'>');
}
//------------------------------------------------------------------------------
//if id is passed, returns object. if object is passed, returns it
//------------------------------------------------------------------------------
function idobj2obj(idobj){
	if(typeof idobj=='object'){
  	return idobj;
	}
	else{
  	return gebi(idobj);
	}
}
//------------------------------------------------------------------------------
// delete all options from given combo
//------------------------------------------------------------------------------
function clear_combo(combo_idobj){
  var combo=idobj2obj(combo_idobj);
	while(combo.length>0){
		combo.remove(0);
	}
}
//------------------------------------------------------------------------------
// 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=idobj2obj(combo_idobj);
  var opt;
  var selIndex=0;
  var selOpt=null;
	//alert('fill_combo:'+combo.id+'->'+value+' options:'+list.length);
	//alert(list);
	if(combo){
	  clear_combo(combo);
		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+'\ntypeof le:'+typeof le+'\ntypeof le[id]:'+typeof le["id"]+'\ntypeof le[0]:'+typeof le[0]);
		  opt=new Option();
			if(typeof le=='string'){
				opt.value=le;
				opt.text =le;
			}
			else{
				opt.value=le[0]==undefined?(le["id"]  ==undefined?le:le["id"  ]):le[0];
				opt.text =le[1]==undefined?(le["name"]==undefined?le: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 current option in combo
//------------------------------------------------------------------------------
function set_combo(combo,value)
{
	if(typeof combo!='object'){
  	combo=gebi(combo);
	}
	combo.selectedIndex=-1;
	for(var i=0;i<combo.options.length;i++)
	{
	  var opt=combo.options[i];
		if(opt.value==value){
			opt.selected=true;
		}
	}
}
//----------------------------------------------------------------------------
// 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;
}

