//Browser Detection
var UserAgent = Array();
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){
	UserAgent[0] = 'Firefox';
	var ffversion=new Number(RegExp.$1);
 	if (ffversion>=3)
  		UserAgent[1] = 3;
 	else if (ffversion>=2)
  		UserAgent[1] = 2;
 	else if (ffversion>=1)
  		UserAgent[1] = 1;
}
else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ 
  	UserAgent[0] = 'IE';
 	var ieversion=new Number(RegExp.$1)
 	if (ieversion>=8)
 		UserAgent[1] = 8;
 	else if (ieversion>=7)
  		UserAgent[1] = 7;
 	else if (ieversion>=6)
  		UserAgent[1] = 6;
 	else if (ieversion>=5)
  		UserAgent[1] = 5;
}

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }




function ajaxReq(url,afterfunct) {
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
			httpRequest.overrideMimeType('text/xml');
			// See note below about this line
		}
	} 
	else if (window.ActiveXObject) { // IE
		try {
			httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			try {
				httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
	}
	if (!httpRequest) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	//alert(url);
	httpRequest.onreadystatechange = function() { afterfunct(httpRequest); };
	httpRequest.open('GET', url, true);
	httpRequest.send('');
}
function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
		
	} 
	else { 
		window.onload = function() { 
			if (oldonload) { 
				oldonload(); 	
			} 
			func(); 
		} 
	} 
} 

function append_sid(a) {
	var out = a + '&sid='+ userSid;
	return out;
}
function ajaxReturn(httpRequest) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			var protoJSON = httpRequest.responseText;
			alert(protoJSON);
		}
	}
}
function requestNewForIndex() {
	var reqURL = 'ajaxify.php?act=indexnew';
	reqURL = append_sid(reqURL);
	//alert(reqURL);
	
}
function outputEvents(httpRequest) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			var protoJSON = httpRequest.responseText;
			var incJSON = eval( "(" + protoJSON + ")" );
			var outputUsers = '';
			//alert(incJSON.medals[0].user);
			for (i = 0; i < incJSON.medals.length; i++) {
				outputUsers = outputUsers + '<a class="username-coloured" style="color:#' + incJSON.medals[i].color + '; text-decoration:none;" href="' + append_sid('memberlist.php?mode=viewprofile&u=' + incJSON.medals[i].user) + '">' + incJSON.medals[i].userid + '</a> ';
				
				
			}
			document.getElementById('eventOutput').innerHTML = outputUsers;
		}
	}
}


function requestEvents(a) {
	var reqURL = 'ajaxify.php?act=reqevents&event=' + a;
	ajaxReq(reqURL, outputEvents);
	
}

function fr_confirm(a, b) {
	var user = a;
	var Cconfirm = b;
	if (Cconfirm == 'yes') {
		if (confirm('Желаете ли да добавите потребителя към приятели?')){
			var reqURL = 'ajaxify.php?act=cofriend&confirm=1&id=' + user;
			//alert(reqURL);
			ajaxReq(reqURL, false);
			document.getElementById(user).style.visibility = 'hidden';
		}
	}
	else {
		if (confirm('Желаете ли да премахнете поканата от потребителя?')){
			var reqURL = 'ajaxify.php?act=cofriend&confirm=0&id=' + user;
			//alert(reqURL);
			ajaxReq(reqURL, false);
			document.getElementById(user).style.visibility = 'hidden';
		}
	}
}

function defriend(a) { 
	//var name = b;
	var reqURL = 'ajaxify.php?act=defriend&id=' + a;
	if (confirm('Премахни потребителя от списъка с приятелите си?')) {
		//alert(reqURL);
		ajaxReq(reqURL, false);
		document.getElementById(a).style.visibility = 'hidden';
	}
}

function bffadd(a) {
	var reqURL = 'ajaxify.php?act=bffadd&id=' + a;
	//alert(reqURL);
	ajaxReq(reqURL, false);
	document.getElementById('bff_' + a).innerHTML = '<img src="images/favorite_remove.png" width="16" height="16" onclick="bffdel(' + a + ')">';
}

function bffdel(a) {
	var reqURL = 'ajaxify.php?act=bffdel&id=' + a;
	//alert(reqURL);
	ajaxReq(reqURL, false);
	document.getElementById('bff_' + a).innerHTML = '<img src="images/favorite_add.png" width="16" height="16" onclick="bffadd(' + a + ')">';
}

function test(a) {
	document.write(a);
}

function getCurrentPage(a){
	var str = a.toString();
// 	alert(str);
	var firstSplit = str.split('/');
	var secondSplit = firstSplit[3].split('?')
	var noDot = secondSplit[0].split('.');
	return noDot[0];
	
}

function aknMessage(a) {
	var reqURL = 'ajaxify.php?act=aknmessage&id=' + a;
	ajaxReq(reqURL, false);
	document.getElementById(a).innerHTML = '';
}
//setTimeout('alert(window.uid)', 10000);
