var agent=(window.navigator.appName=='Netscape')?'Firefox':'IE';

//Ajax functions
function InitXMLHttpRequest() {
	var req=null;
	// Make a new XMLHttp object
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return req;
}
function AjaxQuery(query, dest, func, params){
	var req=InitXMLHttpRequest();
	
	if(dest!=null) var field=document.getElementById(dest);
	req.onreadystatechange = function() {
		if(dest=='') return 0;
		if (req.readyState == 4) {
			if(dest!=null) RunJS(dest, req.responseText);
			if(func!=null) {
				exec_str="func(";
				if(params!=null){
					var len=params.length;
					for(var i=0;i<len;i++){
						exec_str+="params["+i+"]";
						if(i!=(len-1)) exec_str+=', ';
					}
				}
				exec_str+=");";
				eval(exec_str);
			}
		}
	}
	req.open("GET", query+'&rnd='+Math.random(), true);
	req.send(null);
	if(dest!=null) field.innerHTML = "<div align='center'>Loading data...</div>";
}

function RunJS(div_name, response){
	var d2 = document.getElementById(div_name);

	//Check user browser
	var agent=(window.navigator.appName=='Netscape')?'Firefox':'IE';

	if(agent=='IE'){
		d2.innerHTML=response;
		var arr_scripts = d2.getElementsByTagName("script");
		for(i in arr_scripts){
			var src = arr_scripts[i].src;
			if(null!=src){
				var has_question = (src.indexOf('?', 1)>0 ? true : false);
				if(0==src.length)
					arr_scripts[i].setAttribute('src', '');
				else
					arr_scripts[i].setAttribute('src', arr_scripts[i].src + (has_question==true ? '&' : '?') + '1=1');
			}
		}
	}
	else{
		//Mozilla
		var d1=d2.parentNode;
		var d_new=document.createElement('div');
		
		//Set same attributes as original
		var attributes=d2.attributes;
		var len=attributes.length;

		d_new.innerHTML=response;
		for(var i=0; i<len; i++){
			d_new.setAttribute(attributes[i].name, attributes[i].value);
		}
		
		d1.replaceChild(d_new, d2);
	}
}

//Common functions
function LoadCatForm(id_cat, id_curr, dest_div){
	AjaxQuery('ajax_common.php?sel=edit_cat&id_cat='+id_cat+'&id_curr='+id_curr, dest_div);
}

function LoadObjectForm(id_object, id_root, dest_div){
	AjaxQuery('ajax_common.php?sel=edit_object&id_object='+id_object+'&id_root='+id_root, dest_div);
}

function LoadCategoryForm(id_object, id_cat, dest_div){
	AjaxQuery('ajax_common.php?sel=get_ado&id='+id_object+'&id_cat='+id_cat, dest_div);
}

function ClearWindow(dest_div){
	var div=document.getElementById(dest_div);
	div.innerHTML='';
	window.scroll(0,0);
}
function getRandColor(){
	min=0;
	max=16777215;
	random_int = getRandom(min,max);
	rand_color_hex = d2h(random_int);
	return rand_color_hex;
}

function getRandom(min,max){
	rand = Math.floor(min+(max-min)*Math.random());
	return rand;
}

function d2h(d) {
	return d.toString(16);
}
function h2d(h) {
	return parseInt(h,16);
}
function showNoticeOnEl(msg,jq_obj_el){
	if (!msg) return
	var offset = jq_obj_el.offset();
	var x = offset.left;
	var y = offset.top;
	var rand = Math.random()*10;
	var rand_str = rand.toString();
	rand = rand_str.replace(".","");
	$("<div id='temp"+rand+"'>"+msg+"</div>").appendTo("body").addClass("notice_div1_on_el").fadeIn("slow").fadeTo(2000,1).fadeOut("slow",function(){
		$(this).remove();
	});
	$("#temp"+rand).click(function(){
		$(this).remove();
	});
	$("#temp"+rand).css("top",(y+15)+"px");
	$("#temp"+rand).css("left",(x+5)+"px");
}
function getJQData(data){alert(data);
	if (!data){
		showNotice('system error!!!');
		return false;
	}
	var resp;
	resp = eval('('+data+')');
	if (!resp){
		//showNotice('system error!!!');
		//return false;
	}
	return resp;
}
function ajaxLoadingImage(status, id_container){
	if (status)
		$("#"+id_container).html('<img id="img_loading" src="'+TEMPLATE_PATH+'/images/ajax-loader.gif" alt="" border="0" />');
	else
		$("#"+id_container).html('');
}