var selectedOptions = [];
function countSelected(select,maxNumber){
   for(var i=0; i<select.options.length; i++){
     if(select.options[i].selected && !new RegExp(i,'g').test(selectedOptions.toString())){
        selectedOptions.push(i);
     }

     if(!select.options[i].selected && new RegExp(i,'g').test(selectedOptions.toString())){
      selectedOptions = selectedOptions.sort(function(a,b){return a-b});  
       for(var j=0; j<selectedOptions.length; j++){
         if(selectedOptions[j] == i){
            selectedOptions.splice(j,1);
         }
       }
     }

     if(selectedOptions.length > maxNumber){
      //var throwAlert = true;
        select.options[i].selected = false;
        selectedOptions.pop();
     }  
   }            

     //if(throwAlert == true){
     //   alert('You may only choose '+maxNumber+' options!!');
     //   document.body.focus();
     //}
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function movePopup()
{
	chatPopup = document.getElementById('chatpopup');
	
	if (chatPopup!=null)
	{
	
		var vHeight = 0;
		/*if (document.all) {
			if (document.documentElement) {
				vHeight = document.documentElement.clientHeight;
			} else {
				vHeight = document.body.clientHeight
			}
		} else {
		  vHeight = window.innerHeight;
		}*/

		chatPopup.style.top = f_scrollTop()+ f_clientHeight() - 268;
	}
}


//images
var xmlhttp;

function showPicture(str)
{
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
document.getElementById("imageDisplay").innerHTML="<table width='100%' height='100%'><tr><td background='images/design/fondPic.png' align='center'><img src='images/design/loading.gif'></td></tr></table>";
document.getElementById("imageDisplay").style.zIndex=10;
document.getElementById("imageDisplay").style.height="100%";
var url="displayPic.php";
url=url+"?fichier="+str;
url=url+"&sid="+Math.random();

xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  xmlDoc=xmlhttp.responseText;
  document.getElementById("imageDisplay").innerHTML=xmlDoc;
  /*xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
  document.getElementById("lastname").innerHTML=
  xmlDoc.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
  document.getElementById("job").innerHTML=
  xmlDoc.getElementsByTagName("job")[0].childNodes[0].nodeValue;
  document.getElementById("age_text").innerHTML="Age: ";
  document.getElementById("age").innerHTML=
  xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue;
  document.getElementById("hometown_text").innerHTML="<br/>From: ";
  document.getElementById("hometown").innerHTML=
  xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue;
  */}
}

function GetXmlHttpObject()
{
	
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function reduceImage()
{
	document.getElementById("imageDisplay").style.zIndex=-1;
	document.getElementById("imageDisplay").innerHTML="";
	document.getElementById("imageDisplay").style.height="0px";
}

//friends
var xmlnewfriendhttp;
function addFriend(id1,id2)
{
	if (id1!=id2)
	{
		if (id1<id2) {
			nomFichier=id1+"-"+id2;
		} else {
			nomFichier=id2+"-"+id1;
		}

		xmlnewfriendhttp=GetXmlHttpObject();
		if (xmlnewfriendhttp==null)
		{
			alert ("Browser does not support HTTP Request");
			return;
		}
		
		var url="ajoutami.php";
		url=url+"?idCpt="+id2+"&idCpt2="+id1;
		url=url+"&sid="+Math.random();
		
		xmlnewfriendhttp.onreadystatechange=stateGlobalFriendChanged;
		xmlnewfriendhttp.open("GET",url,true);
		xmlnewfriendhttp.send(null);
	}
}

function stateGlobalFriendChanged()
{
	if (xmlnewfriendhttp.readyState==4)
	{
		var xmlDoc=xmlnewfriendhttp.responseText;      
		document.getElementById('ajoutFriend').innerHTML = xmlDoc+'<img src="images/design/coeur.png">';
		//alert(xmlDoc);
	}
}