// JavaScript Document
function Switch(butt, obj, fckdiv,argument_id)
{
	// Hide button
	document.getElementById(butt).style.display='none';
	
	o = document.getElementById(obj);
	if (o.style.display!="block")
	{
		o.style.display='block';
		var div = document.getElementById(fckdiv);
		var fck = new FCKeditor("corps_argument" + argument_id);
		fck.ToolbarSet = 'debarena' ;
		div.innerHTML = fck.CreateHtml();
	}
	else
	{
		o.style.display='none';
	}
}

function SwitchOB(butt, obj,arghh)
{
	// Hide button
	document.getElementById(butt).style.display='none';
	if (arghh!=-1) document.getElementById('notconvbut'+ arghh).style.display='none';
	
	curarg = arghh;
	o = document.getElementById(obj);
	
	document.getElementById("explarea").innerHTML = "Pour tenir compte de cet argument, changez votre avis sur le débat (même un tout petit peu !). Le fait que vous ayez changé d'avis donnera des points d'influence à l'auteur de cet argument.";
	if (o.style.display!="block")
	{
		// If the opbar isn't there yet, move it
		var oph = where_the_bar_is.removeChild(opbar_handle);
		o.appendChild(opbar_handle);
		where_the_bar_is = o;
		o.style.display='block';
	}
	else
	{
		o.style.display='none';
	}
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function submitArg(argID, debID, uID)
//(argTitle,argContent,argDID, argFID ,argUID, oID)
 { 
 //objectID= oID;
// running = true;
// original_value=document.getElementById(objectID).style.backgroundColor;
// signal();

	// Gather data from the form
	if (uID==-1)
	{
		name = document.getElementById("new_quick_user_name" + argID).value;
		pw = eval("document.topforme" + argID + "[0].value");
	}
	else
	{
		name ="";
		pw = "";
	}
	//pw = document.topforme("new_quick_user_name_pw" + argID).value;
	if (argID==-1) 
	{
		side = getCheckedValue(document.forms['olympic'].elements['side']);
	}
	else 
	{
		side = 0;
	}
	
	if (argID!=0 && argID!=-1)
	{
		document.getElementById('validate' + argID).style.display = "none";
		argtype= getCheckedValue(document.forms['olympic' + argID].elements['argtype' + argID]);
		document.getElementById('na2' + argID).innerHTML = "Envoi en cours...";
 		document.getElementById('na2' + argID).className = "busy";
	}
	else
	{
		argtype = 0;
	}
	title = document.getElementById("titre_argument" + argID).value;
	//content = document.getElementById("corps_argument" + argID).value;
	FCKeditorAPI.GetInstance('corps_argument' + argID).UpdateLinkedField();
	var content = encodeURIComponent(FCKeditorAPI.GetInstance('corps_argument' + argID).GetHTML());
	
	
	
 
 var _content="title=" + title + "&content=" + content + "&did=" + debID + "&uid=" + uID + "&faid="+ argID + "&newUser=" + name + "&newPw=" + pw + "&side=" + side+ "&argtype=" + argtype
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("HTTP Request non support par le naviguateur")
  return
  } 
 var _url="http://www.debarena.com/fr/php/addArgumentAjax.php"
 xmlHttp.onreadystatechange=function(){argumentPosted(argID);};
 xmlHttp.open("POST",_url,true)
 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlHttp.send(_content)
 }
 
function argumentPosted(argid) 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
//running=false;
//
Switch("réponse_" + argid);
eval(xmlHttp.responseText);
document.getElementById('na' + argid).innerHTML = xmlHttp.responseText;
document.getElementById('na' + argid).className = "done";
 }
} 
 
 
 
 
 
 function GetXmlHttpObject()
 { 
 var objXMLHttp=null
 if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest()
  }
 else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
 return objXMLHttp
 }
 
// ****************************** VOTING ******************************

function castVote(val,argID, notif_area)
{
	// Hiding button and vote zone
	document.getElementById("vote_" + argID).style.display="none";
	document.getElementById("btvote_" + argID).style.display="none";
	if (argID!=-1) document.getElementById("notconvbut" + argID).style.display="none";
	
	if (notif_area!="none")
	{
		document.getElementById(notif_area).innerHTML = "Vote en cours...";
 		document.getElementById(notif_area).className = "busy";
	}
	var _content="did=" + G_info.did + "&aid=" + argID + "&uid=" + G_info.uid + "&val=" + val;
	 xmlHttp=GetXmlHttpObject();
	 if (xmlHttp==null)
	  {
	  alert ("HTTP Request non support par le naviguateur");
	  return;
	  } 
	 var _url="http://www.debarena.com/fr/php/addVote.php5";
	 xmlHttp.onreadystatechange= function()
	 								{
										if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
										{
											document.getElementById(notif_area).innerHTML = xmlHttp.responseText;
 											document.getElementById(notif_area).className = "done";
										}
									};
	 xmlHttp.open("POST",_url,true);
	 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 xmlHttp.send(_content);
}

// ******************************* Qualitative vote *****************************
function q_vote(argID,srcID, target_id, qualityID)
{
	// Hiding button and vote zone
	document.getElementById("qvote_area" + argID).style.display="none";
	
	notif_area = "na" + argID;
	document.getElementById(notif_area).innerHTML = "Vote en cours...";
 	document.getElementById(notif_area).className = "busy";

	var _content="argid=" + argID + "&target=" + target_id + "&source=" + srcID + "&qualityID=" + qualityID;
	 xmlHttp=GetXmlHttpObject();
	 if (xmlHttp==null)
	  {
	  alert ("HTTP Request non support par le naviguateur");
	  return;
	  } 
	 var _url="http://www.debarena.com/fr/php/addQualitativeVote.php5";
	 xmlHttp.onreadystatechange= function()
	 								{
										if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
										{
											document.getElementById(notif_area).innerHTML = xmlHttp.responseText;
 											document.getElementById(notif_area).className = "done";
										}
									};
	 xmlHttp.open("POST",_url,true);
	 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 xmlHttp.send(_content);
}

// ****************************** Sending invitation ******************************
function sendInvite(uid_, uname, did_, tostr, dtitle, dcat, dkw)
{
 document.getElementById(('notification_area')).innerHTML = "Envoi en cours...";
 document.getElementById(('notification_area')).className = "busy";
 
 var _content="fromid=" + uid_ + "&to=" + tostr + "&fromname=" + uname + "&did=" + did_ + "&dtitle=" + dtitle + "&category=" + dcat + "&kw=" + dkw
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null)
  {
  alert ("HTTP Request non support par le naviguateur")
  return
  } 
 var _url="http://www.debarena.com/fr/php/sendIvite.php"
 xmlHttp.onreadystatechange=invited
 xmlHttp.open("POST",_url,true)
 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 xmlHttp.send(_content)
}

function invited() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById(('notification_area')).innerHTML = xmlHttp.responseText;
 document.getElementById(('notification_area')).className = "done";
 document.getElementById(('invitefield')).value = "";
 }
} 


// ********************************** Adding user to group *********************************
function add2group(nuid, gid, usrnm, grpnm)
{
 if (nuid.indexOf("@")!=-1)
 {
	 sendInvite(-1, usrnm, gid, nuid, "na", "na", grpnm)
 }
 else
 {
	 document.getElementById(('notification_area')).innerHTML = "Inscription...";
	 document.getElementById(('notification_area')).className = "busy";
	 
	 var _content="nu=" + nuid + "&gid=" + gid
	 xmlHttp=GetXmlHttpObject()
	 if (xmlHttp==null)
	  {
	  alert ("HTTP Request non support par le naviguateur")
	  return
	  } 
	 var _url="http://www.debarena.com/fr/php/addUser2group.php"
	 xmlHttp.onreadystatechange=userAdded
	 xmlHttp.open("POST",_url,true)
	 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 xmlHttp.send(_content)
 }
}

function userAdded() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById(('notification_area')).innerHTML = xmlHttp.responseText;
 document.getElementById(('notification_area')).className = "done";
 document.getElementById(('invitefield')).value = "";
 }
} 