
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function ShowDiv(content_id, menu_id){
	_ShowDiv(content_id, menu_id);
	if (typeof(history.pushState) == 'function'){
		var stateObj = { c_id: content_id, menu: menu_id };
		var text = menu_id.getAttribute('u_link');
		if (text) {
			history.pushState(stateObj, text, $base_url+text);
		}
	}
}

function _ShowDiv(content_id, menu_id){
	var content_div = document.getElementById(content_id);
	var divs = content_div.parentNode.childNodes;
	for(var i in divs){
		if(divs[i].className == 'user_profile_content_tab'){
			divs[i].style.display = 'none';
		}
	}
	content_div.style.display = '';
	var menus = menu_id.parentNode.childNodes;
	for(var i in menus){
		if(menus[i].className == 'user_profile_menu_item_selected'){
			menus[i].className = 'user_profile_menu_item';
		}
	}
	menu_id.className = 'user_profile_menu_item_selected';
}

window.onpopstate = function(event) {
	  if (event.state){
			var stt = event.state;
			if (stt.c_id && stt.menu){
				_ShowDiv(stt.c_id, stt.menu);
				alert(stt.menu.getAttribute('u_link'));
			}
		}
};

function AddToFrends(user_id, $obj) {
	var url = $base_url+'user/manage/';
	var val = 'user_id='+user_id;
	val += '&action=addtofrend';
	var f = function(respTxt, respXML){
		$res = ParseResult(respXML);
		if ($res.result){
			$obj.style.display = 'none';
			$obj.onclick = function(){return false;}
		} else {
			alert('FAIL');
		}
	}
	doXMLHttpRequest_DHS(url, val, f);
}

function RemoveFrend(user_id) {
	var url = $base_url+'user/manage/';
	var val = 'user_id='+user_id;
	val += '&action=removefrend';
	var f = function(respTxt, respXML){
		location.reload(true);
	}
	doXMLHttpRequest_DHS(url, val, f);
}

function voteUser(user_id, vote){
	var url = $base_url+'user/manage/';
	var val = 'user_id='+user_id;
	val += '&action=voteuser';
	val += '&vote='+vote;
	var f = function(respTxt, respXML){
		$res = ParseResult(respXML);
		if ($res.result == 1){
			var num = document.getElementById('rating_'+vote);
			num.innerHTML = parseInt(num.innerHTML) + 1;
		} else {
			if ($res.data[0]['cnt'] > 0){
				alert('Nav pagājušas 24 stundas kopš pagājušā balsojuma');
			} else {
				alert('Neizdevās nobalsot');
			}
		}
	}
	doXMLHttpRequest_DHS(url, val, f);
}

function SetMailRead(mail_id, type){
	var url = $base_url+'user/manage';
	var val = 'mail_id='+mail_id;
	val += '&action=setmailread';
	val += '&type='+type;
	var f = function(respTxt, respXML){
		return true;
	}
	doXMLHttpRequest_DHS(url, val, f);
}

function OpenMailToForm(username, u_id){
	var direct = false;
	var mail_div = document.getElementById('user_profile_mail_menu');
	if (mail_div){
		var new_mail_div = document.getElementById('user_inbox_new_menu');
		if (new_mail_div){
			direct = true;
			ShowDiv('user_profile_mail', mail_div);
			ShowDiv('user_inbox_new', new_mail_div);
			document.getElementById('mail_to').value = username;
			document.getElementById('mail_to').readOnly = true;
			//document.getElementById('mail_to').disabled = true;
		}
	}
	if (!direct){
		document.getElementById('OpenMailToForm_'+u_id).submit();
	}
	return true;
}

function print_r($array){
	var $str = '';
	for (var $i in $array){
		if (typeof($array[$i]) == 'array' || typeof($array[$i]) == 'object'){
			$str += $i+"=>{\n";
			$str += print_r($array[$i])+"}\n";
		} else {
			$str += $i+'=>'+$array[$i]+"\n";
		}
	}
	return $str;
}

function count($array){
	var $count = 0;
	for (var $i in $array){
		$count++;
	}
	return $count;
}

function ParseResult($XMLData){
	var $XMLData = $XMLData.getElementsByTagName('response')[0];
	var $result = $XMLData.getElementsByTagName('result')[0];
	$result = $result.firstChild.nodeValue;
	var $result_data = new Array();
	var $items = $XMLData.getElementsByTagName('items')[0];
	$items = $items.getElementsByTagName('item');
	for(var $i in $items){
		var $item = new Array();
		for(var $j in $items[$i].childNodes){
			if ($items[$i].childNodes[$j].nodeType == 1) {
				$item[$items[$i].childNodes[$j].tagName] = $items[$i].childNodes[$j].firstChild.nodeValue;
			}
		}
		if(count($item) > 0){
			var $k = count($result_data);
			$result_data[$k] = new Array();
			for($l in $item){
				$result_data[$k][$l] = $item[$l];
			}
		}
	}
	return {'result':$result,
					'data'  :$result_data};
}

function editClass(id, c_name, action){
	if (action == 1){
		if (!hasClass(id, c_name, 3)) document.getElementById(id).className += " "+c_name;
	} else if (action == 2){
		var reg = new RegExp('(\\s|^)'+c_name+'(\\s|$)');
		document.getElementById(id).className = document.getElementById(id).className.replace(reg,'');
	} else if (action == 3){
		var reg = new RegExp('(\\s|^)'+c_name+'(\\s|$)');
		return document.getElementById(id).className.match(reg);
	}
}
