/* select/deselect all */

function select_all(d) {
 //d = document.act_form;
 for (i = 0; i < d.elements.length; i++) {
   if (d.elements[i].type == "checkbox" && d.elements[i].name != "selectall") {
     d.elements[i].checked = true;
   }
 }
}

function deselect_all(d) {
 //d = document.act_form;
 for (i = 0; i < d.elements.length; i++) {
   if (d.elements[i].type == "checkbox" && d.elements[i].name != "selectall") {
     d.elements[i].checked = false;
   }
 }
}

function selectdeselect_all(d, thischecked) {
	if(thischecked)
		select_all(d);
	else
		deselect_all(d);
}


/* - tabs - */

var arr_tab = new Array;

function switch_tab(tab){
	var a=0;

	for(a=0; a<arr_tab.length; a++){
		hidediv('tab_body_'+arr_tab[a]);
		if(document.getElementById('tab_'+arr_tab[a])!=null)
			document.getElementById('tab_'+arr_tab[a]).className = "tab";
	}
	showdiv('tab_body_'+tab);
	if(document.getElementById('tab_'+tab)!=null)
		document.getElementById('tab_'+tab).className = "tab_on";
}

function change_pic(id, pic){
	document[id].src=pic;
}

function open_win(url,name,width,height,scrollbars,resizable){
	newWin = window.open(url, name, "width="+width+",height="+height+",scrollbars="+scrollbars+",resizable="+resizable);
	newWin.focus();
}


function rdir(url){
	window.location.href=url;
}
function change_bgcolor(id, color){
	id.style.backgroundColor = color;
}
//============== pop alert (bottom right corner) =========

var alert_timer = "";

function check_alert(){
	
	var url = '/alert/check.php';

	//send and get response
	var request = new XHRequest().getObject();		
	request.onreadystatechange = function() {
		if (request.readyState == 4) {      
			if (request.status == 200) { 
				var txt = request.responseText;
				if(txt!="" && txt.indexOf("error")<0)
					pop_alert(txt);
			}
		}
	}
	url += "?r="+Math.random();	
	request.open("GET",url, true);
	request.send(null);

	alert_timer = setTimeout("check_alert()", 8000);
}

function pop_alert(str){

	getWindowXY();
	getScrollXY();

	count = 0;
	pos = str.indexOf("<div id=alert_");
	while ( pos != -1 ) {
	   count++;
	   pos = str.indexOf("<div id=alert_",pos+1);
	}


	//var total_height = document.getElementById("alert_popup_div").style.height;

	//if(total_height=="" || total_height==0){
		total_height = count * 130;
	//}

	obj = document.getElementById("alert_div");
	obj.style.display = "block";

	//set content
	obj.innerHTML = str;

	//x
	var new_x = myWidth - 200;
	if(!is_ie())
		new_x = new_x - 24;
	obj.style.left = new_x;
	
	//y
	var new_y = myHeight - total_height;
	if(!is_ie())
		new_y = new_y - 22;
	var max_y = new_y;

	//moving y
	popping_alert(max_y, max_y + total_height);

}

function popping_alert(max_y, new_y){
	var timer;

	new_y -=2;
	obj.style.top = new_y;

	if(new_y<=max_y) {
		clearTimeout(timer);
		//stop checking
		clearTimeout(alert_timer);
	} else {
		timer = setTimeout("popping_alert("+max_y+", "+new_y+")", 1);
	}
}

function close_alert(alert_id){

	//delete alert
	send_xml("/alert/check.php?task=Del&alert_id="+alert_id, 0);

	hidediv("alert_"+alert_id);

	//start checking again
	//check_alert();
}


//============== pop layer (move content) ==============

var popwin_timer="";

function show_popwin_move_content(id_source, id_target) {

	//show move content table
	var this_source = document.getElementById(id_source);
	var this_target = document.getElementById(id_target);

	this_target.style.left = getX(this_source);
	this_target.style.top = getY(this_source) + 18;
	this_target.style.visibility = 'visible';

} 
function remain_popwin(id){
	clearTimeout(popwin_timer);
}
function hiding_popwin(id){
	popwin_timer = setTimeout("hide_popwin("+id+")", 1000);
}
function hide_popwin(id){
	if (id.style.visibility == 'visible'){
		id.style.visibility = 'hidden';
	}
}

//===================== popup window =====================

function show_popup(url){

	//background
	oBody = document.getElementById('frame_body');
	oBody.className = "body_bg_fade";

	hide_combos();

	position_popup();

	popup_frame.location=url;

}
function close_popup(id){
	hidediv(id);

	oBody = document.getElementById('frame_body');
	oBody.className = "body_bg";

	show_combos();

}
function reposition_popup(){
	if(document.getElementById('popup').style.display=="block"){
		position_popup();
	}
}
function position_popup(){
		
	obj = document.getElementById('popup');

	getScrollXY();
	getWindowXY();

	var new_left = myWidth/2 - 410;
	if(new_left<0)
			new_left = 0;

	obj.style.left = new_left;
	obj.style.top = screen.height/2 + scrOfY - 380;

	obj.style.display = "block";
}

//==================== ajax ===========================

function refresh_div(id, url, no_cache){
	//send and refresh div

	var request = new XHRequest().getObject();
		
	request.onreadystatechange = function() {
		if (request.readyState == 4) {      
			if (request.status == 200) { 
				var txt = request.responseText;
				if(txt!="")
					document.getElementById(id).innerHTML = txt;
			}
		}
	}
	if(no_cache)
		url += "&r="+Math.random();
	
	request.open("GET",url, true);
	request.send(null);
}

function refresh_div_frame(frame_div, content_div){
	var c = document.getElementById(content_div);
	var f = document.getElementById(frame_div).contentWindow.document;
	
	if(f.location.href!="about:blank"){
		new_content = f.body.innerHTML;
		if(new_content!=""){
			c.innerHTML = new_content;
		}
	}
}

function update_xml(url, msg){
	//send and confirm
	var request = new XHRequest().getObject();		
	request.onreadystatechange = function() {
		if (request.readyState == 4) {      
			if (request.status == 200) { 
				//ok
				if(msg!="")
					alert(msg);
			}
		}
	}
	url += "&r="+Math.random();	
	request.open("GET",url, true);
	request.send(null);
}

function send_xml(url, to_confirm){
	//send and confirm
	var request = new XHRequest().getObject();		
	request.onreadystatechange = function() {
		if (request.readyState == 4) {      
			if (request.status == 200) { 
				//ok
				if(to_confirm)
					alert("Saved");
			}
		}
	}
	url += "&r="+Math.random();	
	request.open("GET",url, true);
	request.send(null);
}
function get_xml(url){
	//send and get response
	var request = new XHRequest().getObject();		
	request.onreadystatechange = function() {
		if (request.readyState == 4) {      
			if (request.status == 200) { 
				var txt = request.responseText;
				if(txt!="")
					return txt;
			}
		}
	}
	url += "&r="+Math.random();	
	request.open("GET",url, true);
	request.send(null);
}

function send_ajax(obj, url) {
  var getstr = "?";

  for (i=0; i<obj.elements.length; i++) {
	//alert(obj.elements[i].type+"="+obj.elements[i].name+"="+obj.elements[i].value);

	if (obj.elements[i].type == "checkbox") {
	   if (obj.elements[i].checked) {
		  getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
	   } else {
		  getstr += obj.elements[i].name + "=&";
	   }
	} else if (obj.elements[i].type == "radio") {
	   if (obj.elements[i].checked) {
		  getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
	   }
	} else { //text, hidden, textarea
		getstr += obj.elements[i].name + "=" + obj.elements[i].value + "&";
	}
  
	 if (obj.elements[i].tagName == "select") {
		var sel = obj.elements[i];
		getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
	 }
	 
  }
  //alert(url + getstr);
  send_xml(url + getstr, 1);
  //makeRequest('get.php', getstr);
}


//====================== hide and show =====================

var swapimage_image;
function swapimage(id, img1, img2){
	if(id!=""){
		if(swapimage_image==img1){
			document[id].src=img2;
			swapimage_image=img2;
		} else {
			document[id].src=img1;
			swapimage_image=img1;
		}
	}
}

function swapclass(id, class1, class2){
	var item  = document.getElementById(id);

	if(item.className == class1){
		item.className=class2;
	} else {
		item.className=class1;	
	}
}

function hide_combos() {
	var arCombo = document.getElementsByTagName("select");
	for(var i=0;i<arCombo.length;i++) {
		arCombo[i].style.visibility="hidden";
	}
}
function show_combos() {
	var arCombo = document.getElementsByTagName("select");
	for(var i=0;i<arCombo.length;i++) {
		arCombo[i].style.visibility="inherit";
	}
}
function hideandshow(id){
	if (document.getElementById(id).style.display == "block")
		document.getElementById(id).style.display = "none";
	else
		document.getElementById(id).style.display = "block";
}
function showdiv(id){
	if(document.getElementById(id)!=null)
		document.getElementById(id).style.display = "block";
}
function hidediv(id){
	if(document.getElementById(id)!=null)
		document.getElementById(id).style.display = "none";
}
function ifshowdiv(istrue, id){
	if(istrue)
		showdiv(id);
	else
		hidediv(id);
}
function hideshow(thisvalue, id){
	if(thisvalue==1)
		showdiv(id);
	else
		hidediv(id);
}

function switchtab(id1, id2){
	showdiv(id1);
	hidediv(id2);
}