var XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0']

var HttpRequest   = {};
var requestStatus = "";

HttpRequest.getXmlhttpObject = function() {
    var http = null;
    var last_e = null;
    try{ http = new XMLHttpRequest(); }catch(e){}
    if(!http){
      for(var i=0; i<3; ++i){
        var progid = XMLHTTP_PROGIDS[i];
	try{
	  http = new ActiveXObject(progid);
	}catch(e){
	  last_e = e;
	}
        if(http){
	  XMLHTTP_PROGIDS = [progid];  // so faster next time
	  break;
	}
      }
    }
    if(!http){
        if(typeof(console) == 'object')
 	console.debug("XMLHTTP not available", last_e);
    }
    return http;
}

HttpRequest.getText = function(uri, async_cb, fail_ok){
	var http = this.getXmlhttpObject();
	if(async_cb){
		http.onreadystatechange = function(){
			if(4==http.readyState){
				if((!http["status"])||((200 <= http.status)&&(300 > http.status))){
					async_cb(http.responseText);
				}
			}
		}
	}
	http.open('GET', uri, async_cb ? true : false);
	try{
		http.send(null);
		if(async_cb){
			return null;
		}
		if((http["status"])&&((200 > http.status)||(300 <= http.status))){
			throw Error("Unable to load "+uri+" status:"+ http.status);
		}
	}catch(e){
		if((fail_ok)&&(!async_cb)){
			return null;
		}else{
			throw e;
		}
	}
	return http.responseText;
}

setInterval(function() { HttpRequest.getText('/ajax.php?Action=UpdateActivity'); }, 30000);


function switchFriends(Action) {
	var Action = Action;
	if(Action == null) { Action = '1'; }

        MyCookie.Write('SwithMyFriend', Action, 365);
        document.getElementById('MyFriendsBlock').innerHTML = HttpRequest.getText('/ajax.friends.php' + '?Action=' + Action);
        var friendTitle = document.getElementById('MyFriendsTitle');

        if(MyCookie.Read('SwithMyFriend') == '1') {
          document.SwithMyFriendForm.SwithMyFriend[0].checked = true;
          friendTitle.innerHTML = 'Moji přátelé online';
        }

        else if(MyCookie.Read('SwithMyFriend') == '2') {
          document.SwithMyFriendForm.SwithMyFriend[1].checked = true;
          friendTitle.innerHTML = 'Moji přátelé offline';
        }

        else if(MyCookie.Read('SwithMyFriend') == '3') {
          document.SwithMyFriendForm.SwithMyFriend[2].checked = true;
          friendTitle.innerHTML = 'Moji přátelé na chatu';
        }

        else if(MyCookie.Read('SwithMyFriend') == '4') {
          document.SwithMyFriendForm.SwithMyFriend[3].checked = true;
          friendTitle.innerHTML = 'Moji přátelé';
        }

        else {
          document.SwithMyFriendForm.SwithMyFriend[0].checked = true;
          friendTitle.innerHTML = 'Moji přátelé';
        }
}

function swithUserFriends(Action, IdUser) {
	var Action = Action;
	if(Action == null) { Action = '1'; }

        MyCookie.Write('swithUserFriend', Action, 365);
        document.getElementById('UserFriendsBlock').innerHTML = HttpRequest.getText('/ajax.user.friends.php' + '?Action=' + Action + "&IdUser=" + IdUser);
        var friendTitle = document.getElementById('UserFriendsTitle');

        if(MyCookie.Read('swithUserFriend') == '1') {
          document.SwithUserFriendForm.SwithUserFriend[0].checked = true;
          friendTitle.innerHTML = 'Jeho přátelé';
        }

        else if(MyCookie.Read('swithUserFriend') == '2') {
          document.SwithUserFriendForm.SwithUserFriend[1].checked = true;
          friendTitle.innerHTML = 'Má ho v přátelích';
        }
}

function listOppositeFriends(IdUser) {
        document.getElementById('UserOppositeFriendsBlock').innerHTML = HttpRequest.getText('/ajax.user.friends.php' + '?Action=2' + "&IdUser=" + IdUser);
}
