// JavaScript Document
//Add contact js

function Trimt(str){ return str.replace(/^\s*|\s*$/g,""); }

	var contactMakeId = 0;
	var contactRespDiv = null;
	function invokeFR(evt, contact, respDiv){
		
		contactMakeId = contact;
		contactRespDiv = respDiv;
		
		if(!document.getElementById('friendRequestDiv')){
			
			/*var frDiv = "<div class='msgdiv' id='friendRequestDiv' style='position:absolute; display:none; z-index:1099;'>
      <div class='links top stacked clear'><a href='javascript: void(0);' onclick='javascript: closeFR();'><img src='../images/cancel_inv.jpg' alt='Close' width='10' height='10' vspace='5' border='0' /></a></div>
      <h2>Contact Request</h2>
      <div style='padding: 15px;'> Message<br />
        <textarea name='FRmsg' id='FRmsg' cols='55' rows='5' style='margin-bottom: 3px;'></textarea>
        <br />
        <input name='button' type='button' class='green_bn' id='button' value='Submit' onclick='javascript: sendFR();' />
        <input name='button2' type='reset' class='cancel_bn' id='button2' value='Cancel' onclick='javascript: closeFR();' />
     </div>
</div>";*/

		var frDiv = "<div class='msgdiv' id='friendRequestDiv' style='position:absolute; display:none; z-index:1099;'><div class='links top stacked clear'><a href='javascript: void(0);' onclick='javascript: closeFR();'><img src='"+siteUrl+"/images/cancel_inv.png' alt='Close' width='10' height='10' vspace='5' border='0' /></a></div><h2>Contact Request</h2><div style='padding: 15px;'> Message<br /><textarea name='FRmsg' id='FRmsg' cols='55' rows='5' style='margin-bottom: 3px;'></textarea><br /><input name='button' type='button' class='green_bn' id='button' value='Submit' onclick='javascript: sendFR();' /> <input name='button2' type='reset' class='cancel_bn' id='button2' value='Cancel' onclick='javascript: closeFR();' /></div></div>";
			
			var tDiv = document.createElement("div");
			tDiv.innerHTML = frDiv;
			document.body.appendChild(tDiv);
			
		}
		
		var FRDiv = document.getElementById('friendRequestDiv');
		
		var coor = captureCoordinates(evt);
		
		var pgDim = pageDimensions();
		//div width = 408
		var left =0, top =0;
		
		left = ((coor.x + 408) > pgDim.width)?(coor.x - 420):coor.x;
		top = ((coor.y + 215) > pgDim.height)?(coor.y - 220):coor.y;
		//Fix dialog's position
		
		FRDiv.style.top = top+"px";
		FRDiv.style.left = (left)+"px";
		
		FRDiv.style.display = "block";
				
	}
	function closeFR(){
		document.getElementById('friendRequestDiv').style.display = "none";
		document.getElementById('FRmsg').value = "";
	}
	function sendFR(){
		
		if(contactMakeId == 0 || contactRespDiv == null)	return false;
		
		var postVariables = "axn=FR&contactId="+contactMakeId+"&msg="+encodeURIComponent(document.getElementById('FRmsg').value);
			
		var xmlObject = createXMLHttpRequest();
		
		var filePath = "contacts/rmt_addFR.php";
		if(typeof(isRoot) == 'undefined' || isRoot == false){
			filePath = "../"+filePath;	
		}
					
		xmlObject.open("POST", filePath, true);
		xmlObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlObject.onreadystatechange = function(){
											
											if(xmlObject.readyState == 4){
												//alert(xmlObject.responseText);
												document.getElementById(contactRespDiv).innerHTML = xmlObject.responseText;
											}else{
												document.getElementById(contactRespDiv).innerHTML = "Sending Your Request...";
											}
											
									   };
		xmlObject.send(postVariables);
		closeFR();
	}
	
	function removeContact(respDiv, contactId, contactName){
		
		var ok = confirm("Are you sure you want to remove "+contactName+" from your contact list?");
		if(!ok)	return false;
		
		var postVariables = "axn=RemoveContact&contactId="+contactId;
			
		var xmlObject = createXMLHttpRequest();
		var filePath = "contacts/rmt_removeContact.php";
		
		if(typeof(isRoot) == 'undefined' || isRoot == false){
			filePath = "../"+filePath;	
		}
		//alert(filePath);
		xmlObject.open("POST", filePath, true);
		xmlObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlObject.onreadystatechange = function(){
											
											if(xmlObject.readyState == 4){
												//alert(xmlObject.responseText);
												document.getElementById(respDiv).innerHTML = xmlObject.responseText;
											}else{
												document.getElementById(respDiv).innerHTML = "Removing...";
											}
											
									   };
		xmlObject.send(postVariables);
		
	}
	
	function PingFrnd(evt, contact, contactname,respDiv)
	{
		/*alert(contact);
		alert(contactname);*/
		if(document.getElementById("pngusrid").value!="")
		{
			cid=document.getElementById("pngusrid").value;
			document.getElementById('pingDiv'+cid).style.display='none';
			document.getElementById('pingcmt'+cid).value="";
		}
		/*if(document.getElementById('pingDiv'))
		{
			//document.body.removeChild(document.getElementById('pingDiv'));
			if(document.getElementById('pingDiv').style.display=='') 
			{	
			 document.getElementById('pingDiv').style.display='none';
			 document.getElementById('pingDiv').innerHTML="";
			}
		}*/
		/*
		if(!document.getElementById('pingDiv'))
		{
		*/
		/*var frDiv = "<div class='msgdiv' id='pingDiv' style='position:absolute; display:none; z-index:1099;'><div class='links top stacked clear'><a href='javascript: void(0);' onclick='javascript: closePing();'><img src="+siteUrl+"/images/cancel_inv.jpg alt='Close' width='10' height='10' vspace='5' border='0' /></a></div><h2>Ping "+contactname+"</h2><div class='pinguser' style='padding: 5px 0 0 15px; width:60%;'> Type a Quick Ping</div><div class='pingaction' style='padding: 5px 0 0 0;'> <input type='checkbox' name='prvtchk' id='prvtchk' /> Private</div><br /><div style='padding: 0 0 10px 15px;'><textarea name='pingcmt' id='pingcmt' cols='55' rows='5' style='margin-bottom: 3px; width: 365px;'></textarea><br /><input name='button' type='button' class='green_bn' id='button' value='Submit' onclick='sendPing("+contact+", \""+contactname+"\")' /> <input name='button2' type='reset' class='cancel_bn' id='button2' value='Cancel' onclick='javascript: closePing("+contact+");' /></div></div>";*/
			//alert(frDiv);
			/*var tDiv = document.createElement("div");
			tDiv.innerHTML = frDiv;
			document.body.appendChild(tDiv);*/
			
		/*
		}
		*/
		
		var pngDiv = document.getElementById('pingDiv'+contact);
		document.getElementById('pingDiv'+contact).style.display='';
		document.getElementById("pngusrid").value=contact;
		var coor = captureCoordinates(evt);
		
		var pgDim = pageDimensions();
		//div width = 408
		var left =0, top =0;
		
		left = ((coor.x + 408) > pgDim.width)?(coor.x - 420):coor.x;
		top = ((coor.y + 215) > pgDim.height)?(coor.y - 220):coor.y;
		//Fix dialog's position
		
		pngDiv.style.top = top+"px";
		pngDiv.style.left = (left)+"px";
		
		pngDiv.style.display = "block";
				
	}
	function closePing(rid){
		/*var d = document.getElementById('pingDiv');
		var olddiv = document.getElementById(rid);
		document.body.removeNode(d);
		document.body.removeChild(document.getElementById('pingDiv'));*/
		document.getElementById('pingcmt'+rid).value="";
		document.getElementById('pingDiv'+rid).style.display = "none";
		//document.getElementById('pingcmt').value = "";
	}
	function showPinglnk(rid,rname)
	{
		respdivid="PingContactDiv"+rid;
		pnglnk="<a href='javascript: void(0);' onclick='javascript: PingFrnd(event, \""+rid+"\",\""+rname+"\",\""+respdivid+"\")'>Ping Me</a>";
		document.getElementById(respdivid).innerHTML =pnglnk;
		//closePing(rid);
		//document.getElementById('pingDiv').style.display = "none";
	}
	function sendPing(rid,rname)
	{
		//alert(rid);
		if(user_sessid=="")
		{
			alert("Please Login to Send Ping");
			return false;
		}	
		try { 
			xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
		 } 
		 catch (e) 
		 { 
	 		// browser doesn't support ajax. handle however you want
		 } 
		 pingcnt=Trimt(document.getElementById('pingcmt'+rid).value);
		 if(pingcnt=="")
		{
				alert("Enter Ping Content");
				document.getElementById('pingcmt'+rid).focus();
				return false;
		}
		xmlhttp.onreadystatechange =function()
		{
			respdivid="PingContactDiv"+rid;
			 if ((xmlhttp.readyState == 4) ) 
			 { 
				 //alert(xmlhttp.responseText);
				 //allpingsid
				 
				 /*
				 <a href="javascript: void(0);" onclick="javascript: PingFrnd(event, <?=$user_info->user_id?>,'<?=$user_info->fullname?>','PingContactDiv<?=$user_info->user_id?>');">Ping Me</a>
				 */
				document.getElementById(respdivid).innerHTML ='Ping Sent Successfully!';
				setTimeout("showPinglnk('"+rid+"','"+rname+"')",1000);
				document.getElementById('pingcmt'+rid).value="";
			}
			else
			{
				document.getElementById('pingDiv'+rid).style.display = "none";
				document.getElementById(respdivid).innerHTML ='<img src="../images/loading.gif">';
				i=0;
			//document.getElementById("IndustLoadAlert").style.display = ""; 
			}
		} 
	
	//The following will send the request to the PHP file using the POST method:
	 		xmlhttp.open('post',  '../functions/pingoperations.php');
		 	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			//prvtchk
			prvtstat=0;
			if(document.getElementById("prvtchk"+rid).type=="checkbox" && document.getElementById("prvtchk"+rid).checked)
			{
					prvtstat=1;
			}
			//alert(prvtstat);return false;
			
			pingcnt=encodeURIComponent(pingcnt);
			xmlhttp.send('mode=add&rid='+rid+'&pingcnt='+pingcnt+'&prvtstat='+prvtstat);  
			//closePing();
	}
	
