// Requester
// mode: 1 - open request state script, 2- get sender/pending data for div, 3 - silent mode, 4 - alert message from response text other then "Done"
// 
var chat_lasthttp;
var chat_x;
var chat_y;
var chat_element;
var chat_being_dragged = false;
var chatwin=null;
var chatwinfail=0;
var connid=null;
var popupblocked=0;
function chat_mouse_move(event)
{
 if(event == null) 
        event = window.event; 

 if (event.pageX || event.pageY) 
 {
  chat_x = event.pageX;
  chat_y = event.pageY;
 }
 else if (event.clientX || event.clientY)
 {
  chat_x = event.clientX + document.body.scrollLeft
   + document.documentElement.scrollLeft;
  chat_y = event.clientY + document.body.scrollTop
   + document.documentElement.scrollTop;
 }

 if (chat_x>=5)
  chat_x=chat_x-5;
 if (chat_y>=5)
  chat_y=chat_y-5;

 if(chat_being_dragged == true) 
 {
  document.getElementById(chat_element).style.left = chat_x +'px';
  document.getElementById(chat_element).style.top = chat_y +'px';
 }
}
function chat_mouse_down(event) 
{
 if(event == null) 
        event = window.event; 

 if (event.pageX || event.pageY) 
 {
  chat_x = event.pageX;
  chat_y = event.pageY;
 }
 else if (event.clientX || event.clientY)
 {
  chat_x = event.clientX + document.body.scrollLeft
   + document.documentElement.scrollLeft;
  chat_y = event.clientY + document.body.scrollTop
   + document.documentElement.scrollTop;
 }

  var chat_div_y=parseInt(document.getElementById(chat_element).style.top);
  chat_div_y=chat_div_y+25;

  if(chat_div_y>chat_y)
  {
   document.body.focus();
   document.onselectstart = function () { return false; };
   document.onmousemove=chat_mouse_move;
   chat_being_dragged = true;
   document.getElementById(chat_element).style.cursor = 'move';
   if (chat_x>=5)
    chat_x=chat_x-5;
   if (chat_y>=5)
    chat_y=chat_y-5;
   document.getElementById(chat_element).style.top = chat_y +'px';
   document.getElementById(chat_element).style.left = chat_x +'px';
   return(false);
  }
}
function chat_mouse_up(event) 
{
 if (chat_being_dragged == true)
 {
  chat_being_dragged = false;
  document.onmousemove=null;
  document.onselectstart = null;
  document.getElementById(chat_element).style.top = chat_y +'px';
  document.getElementById(chat_element).style.left = chat_x +'px';
  document.getElementById(chat_element).style.cursor = 'auto';
 }
}
function makeRequest(url,mode) 
{
	url+= (url.match(/\?/) == null ? "?" : "&") + (new Date()).getTime();
	var httpRequest;

	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;
	}
	if (mode==1) httpRequest.onreadystatechange = function() { parseContents(httpRequest); };
	if (mode==2) httpRequest.onreadystatechange = function() { showContents(httpRequest); };
	if (mode==3) httpRequest.onreadystatechange = function() { return true; };
	if (mode==4)
	{
		httpRequest.onreadystatechange = function() 
		{  
			if (httpRequest.readyState == 4) 
			{
				if (httpRequest.status == 200) 
				{
					var xmldoc = httpRequest.responseXML;
					var state_node = xmldoc.getElementsByTagName('state').item(0);
					if(state_node.firstChild.data!='Done')
					{
						alert(state_node.firstChild.data);
					}
				} 
			}
		}
	}
	httpRequest.open('GET', url, true);
	httpRequest.send(null);
}


function parseContents(httpRequest) 
{
	var url;

	if (httpRequest.readyState == 4) 
	{
		if (httpRequest.status == 200) 
		{
			var xmldoc = httpRequest.responseXML;
			var state_node = xmldoc.getElementsByTagName('state').item(0);
			if (document.getElementById('stateDiv')!=null)
			{
				// ---------------------debug part need to comment on live site---------------
				document.getElementById('stateDiv').innerHTML='State: '+state_node.firstChild.data+' <br>Request id='+state_node.getAttribute('request_id');
				//-----------end of debug part----------------------
			}
			if (state_node.firstChild.data=='No requests' && document.getElementById('requestDiv'))
			{
				document.getElementById('requestDiv').style.display='none';
			}
			if (state_node.firstChild.data=='Declined' && document.getElementById('requestDiv'))
			{
				if (document.getElementById('requestDiv').style.display!='none')
				{
					alert('IM request declined');
					document.getElementById('requestDiv').style.display='none';
				}
			}
			if (state_node.firstChild.data=='showSender')
			{
				url='/chat/showSender.php';
				makeRequest(url,2);
			}
			if (state_node.firstChild.data=='showPending')
			{
				url='/chat/showPending.php';
				makeRequest(url,2);
			}
			if (state_node.firstChild.data=='showDeclined')
			{
				url='/chat/showDeclined.php';
				makeRequest(url,2);
			}
			if (state_node.firstChild.data=='timeout')
			{
				url='/chat/showTimeout.php';
				makeRequest(url,2);
			}
			if (state_node.firstChild.data=='showBusy')
			{
				url='/chat/showBusy.php';
				makeRequest(url,2);
			}
			if (state_node.firstChild.data=='openChat')
			{
				url='/chat/flashchat.php?room='+state_node.getAttribute('request_id');
				document.newWindow = window.open(url,"chatWindow","status,menubar,height=500,width=550,resizable");
				if (document.newWindow!=null)
				{
					popupblocked=0;
					chatwin=document.newWindow;
					document.newWindow.focus( );
					makeRequest('/chat/closeRequest.php',3);
				}
				else
				{
					popupblocked=1;
					makeRequest('/chat/showPopupBlocked.php',2);
				}
			}
			if (state_node.firstChild.data=='checkChatWin')
			{
				if ( document.getElementById('requestDiv') )
				{
					document.getElementById('requestDiv').style.display='none';
				}
				if (chatwin==null)
				{
					chatwinfail=chatwinfail+1;
					if (chatwinfail==3)
					{
						if (connid!=null)
						{
							delChat(connid);
						}
					}
				}
				else
				{
					chatwinfail=0;
				}
			}
		} 
		else 
		{
			//alert('There was a problem with the request.');
		}
	}
}

function showContents(httpRequest) 
{	
	if (httpRequest.readyState == 4) 
	{
		if (httpRequest.status == 200) 
		{
			if (document.getElementById('requestDiv'))
			{
				// Avoid duplicate display flicker
				if (document.getElementById('requestDiv').style.display!='none')
				{
					if (chat_lasthttp!=httpRequest.responseText)
					{
						document.getElementById('requestDiv').innerHTML=httpRequest.responseText;
						chat_lasthttp=httpRequest.responseText;
					}
				}
				else
				{
					document.getElementById('requestDiv').innerHTML=httpRequest.responseText;
					chat_lasthttp=httpRequest.responseText;
				}
			}
			else 
			{
				var newdiv = document.createElement('div');
				var divIdName = 'requestDiv';
				newdiv.setAttribute('id',divIdName);
				newdiv.innerHTML = httpRequest.responseText;
				chat_lasthttp=httpRequest.responseText;
				
				newdiv.style.display='inline';
				
				newdiv.style.top='40px';
				newdiv.style.backgroundColor='#cccccc';
				newdiv.style.width='160px';
				newdiv.style.zindex='100';
				
				// Altered By Ryan J. Peterson
					var viewportwidth;
					var viewportheight;
					
					// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
					
					if (typeof window.innerWidth != 'undefined')
					{
					     viewportwidth = window.innerWidth,
					     viewportheight = window.innerHeight
					}
					
				       // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
				       
					else if (typeof document.documentElement != 'undefined'
					    && typeof document.documentElement.clientWidth !=
					    'undefined' && document.documentElement.clientWidth != 0)
					{
					      viewportwidth = document.documentElement.clientWidth,
					      viewportheight = document.documentElement.clientHeight
					}
					
					// older versions of IE
					
					else
					{
					      viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
					      viewportheight = document.getElementsByTagName('body')[0].clientHeight
					}
					newdiv.style.position='fixed';
					
					var newPosition = viewportwidth/2+400;
					if( (newPosition + 160) > viewportwidth){
						newPosition = newPosition - ((newPosition + 160) - viewportwidth) - 20;
					}
					newdiv.style.left=newPosition+'px';
					
				// End
				
				document.body.appendChild(newdiv);
				chat_element=divIdName;
				document.getElementById(divIdName).onmousedown=chat_mouse_down;
				document.getElementById(divIdName).onmouseup=chat_mouse_up;
			}
			document.getElementById('requestDiv').style.display='inline';
		} 
		else 
		{
                	//alert('There was a problem with the request.');
		}
	}
}

function acceptRequest(id)
{
	makeRequest('/chat/acceptRequest.php?id='+id,3);
	document.getElementById('requestDiv').style.display='none';
}

function declineRequest(id)
{
	makeRequest('/chat/declineRequest.php?id='+id,3);
	document.getElementById('requestDiv').style.display='none';
	
}

function setDeclined(id)
{
	makeRequest('/chat/setDeclined.php?id='+id,3);
	document.getElementById('requestDiv').style.display='none';
}

function setMissed(id)
{
	makeRequest('/chat/setMissed.php?id='+id,3);
	document.getElementById('requestDiv').style.display='none';
}

function setRequest(id)
{
	makeRequest('/chat/setRequest.php?id='+id,4);
	makeRequest('/chat/getRequestState.php',1);
}

function delChat(id) 
{
	makeRequest('/chat/showCheckWin.php?id='+id,3);
}
