var xh=false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try{
	xh = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
	try{
		xh = new ActiveXObject("Microsoft.XMLHTTP");
	}catch(err){
    	xh=false;
	}
}
@end @*/

if(!xh && typeof XMLHttpRequest != 'undefined'){
	xh=new XMLHttpRequest();
}
var queue_being_processed = false;
var xhreq_queue = [];
function send(handlerpath, argobj, on_request_complete){
	send_underlying(handlerpath, "obj=" + Object.toJSON(argobj).gsub('&', ''), on_request_complete);
}
function send_underlying(handlerpath, argstring, on_request_complete){
	if(handlerpath.indexOf('.z')==-1){
		handlerpath = handlerpath + '.z';
	}
	//handlerpath = 'handlers/' + handlerpath;
	add_to_queue(handlerpath, argstring, on_request_complete);
}
function add_to_queue(handlerpath, argstring, on_request_complete){
    xhreq_queue = $A(xhreq_queue);
    xhreq_queue[xhreq_queue.length] = {'handlerpath':handlerpath, 'argstring':argstring, 'on_request_complete':on_request_complete};
    
    if(!queue_being_processed){
        queue_being_processed = true;
        process_xh_queue();
    }
}
function process_xh_queue(){
    if(xhreq_queue.length > 0){
        var reqitem = xhreq_queue[0];
        sendtoserver(reqitem.handlerpath, reqitem.argstring, reqitem.on_request_complete, function(){
            xhreq_queue = xhreq_queue.without(reqitem);
            process_xh_queue();
        });
    }else{
        queue_being_processed = false;
    }
}
function sendtoserver(handlerpath, argstring, on_request_complete, queue_callback){
    try{
		xh.open("POST", handlerpath, true);
		xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xh.onreadystatechange = function(){
			if(xh.readyState==4){
				on_request_complete(xh.responseText);
				queue_callback();
			}
		};
		xh.send(argstring);
	}catch(e){
		alert('core::send->' + e.message + "," + e.name + "," + e.stack + "," + e.lineNumber + "," + e.fileName);
	}
}
function clean(str){
	return str.gsub("&", "%26");
}
function isenter(__handler){
    e = window.event ? event : e;
    lastKeyPressed = e.keyCode ? e.keyCode : e.charCode;
	if(lastKeyPressed==13 || lastKeyPressed==3) __handler();
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
var timeoutfornotice;
function notice(msg){
	if(msg===null || msg==='') return;
	clearTimeout(timeoutfornotice);
	$('dialogue').update(msg);
	$('dialogue').show();
	timeoutfornotice = setTimeout(function(){
		$('dialogue').hide();
		$('dialogue').update('');
	}, 3000);
}
function block_blank_selection(obj){
  if($F(obj)=="") obj.selectedIndex = 0;
}
function nil(obj){
  if(obj==null || obj==undefined || obj.strip().length==0 || obj.strip().toString()=="0"){
    return true;
  }else{
    return false;
  }
}
function update_item(key, callback){
  update_one(key, callback);
}
function update_page(callback){
  update_all(callback);
}
function update_all(callback){
  send(page_state._controller_name, page_state, hr);
  var response_object;
  function hr(resp){
    try{
      response_object = eval('('+resp+')');
      set_page_contents();
      if(callback != null){
        try{
          callback(response_object);
        }catch(ex2){
          alert("1custom callback failed\n\n"+ex2.message);
        }
      }
      return response_object;
    }catch(ex){
      alert(resp+'\n\n'+ex.message);
    }
    function set_page_contents(){
      Object.keys(response_object).each(loop_wrapper);
      function loop_wrapper(item){
        if(!item.startsWith('_')){
          var key = item;
          var value = response_object[key];
          if($(key).innerHTML.toLowerCase() != value.toLowerCase()){
            $(key).update(value);
            if(page_state._highlight_on_update) new Effect.Highlight(key, {duration:0.25});
          }
        }
      }
    }
  }
}
function update_one(key, callback){
  page_state['_only_get_this'] = key;
  var response_object = update_all();
  if(callback != null){
    try{
      callback(response_object);
    }catch(ex2){
      alert("2custom callback failed\n\n"+ex2.message);
    }
  }
  page_state['_only_get_this'] = undefined;
}
var last_cmd = {};
var last_formdata = {};
function cc(){
	var __obj = {c:arguments[0]};
	for(i=1;i<arguments.length-1;i=i+2){
		__obj[arguments[i]] = arguments[i+1];
	}
	cmd(__obj, arguments[arguments.length-1]);
}
function cmd(o,cbk){
  page_state.cmd = o;
  page_state.formdata = collect_all_form_data();
  send(page_state._controller_name, page_state, function(r){
    var obj;
    try{
      obj = eval('('+r+')');
      cbk(obj);
    }catch(ex){
      alert('callback not executed:' + '\n\n' + r + '\n----------------\n'+ex.message);
    }
  });
  last_cmd = o;
  last_formdata = page_state.formdata;
  page_state.cmd = undefined;
  page_state.formdata = undefined;
}
function collect_all_form_data(){
  var fd = {};
  $$('select').each(function(item){
    try{
      if(item.id != null && item.id != '') fd[item.id] = $F(item);
    }catch(ex){
    }
  });
  $$('input').each(function(item){
    try{
      if(item.id != null && item.id != '') fd[item.id] = $F(item);
    }catch(ex){
    }
  });
  $$('textarea').each(function(item){
    try{
      if(item.id != null && item.id != '') fd[item.id] = $F(item);
    }catch(ex){
    }
  });
  return fd;
}
