	
	// This Javascript file controls the AJAX requests and windows for the Chat application
	
	window.onscroll = repositionChatRequestWindow; 
	
	function load(){
		
	}
	
	var req;
	var incoming;
	var outgoing;
	var reply;
	
	var OpenForChat;
	var Chatting;
	
	OpenForChat = 1; // Set the flag so we can initially contact them
	Chatting = 0;
	
	//Browser Support Code
	function ajaxFunction(){

	try{
		// Opera 8.0+, Firefox, Safari
		req = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	req.onreadystatechange = function(){
		if(req.readyState == 4){
			if(req.responseText != "False" & OpenForChat == 1 & Chatting==0)
			{	
				var error;
				error = req.responseText.match("Warning:");
				if(error == null){
					
					var objh;
					var objw;
					objh = parseFloat(document.getElementById("RequestWindow").style.height)/2;
					objw = parseFloat(document.getElementById("RequestWindow").style.width)/2;
					
					document.getElementById("RequestWindow").style.visibility = "visible";
					document.getElementById("RequestWindow").style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
 					document.getElementById("RequestWindow").style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';

					//document.getElementById("RequestWindow").style.top = (screen.height - 300) / 2;
					//document.getElementById("RequestWindow").style.left = (screen.width - 275) / 2;
					document.getElementById("OperatorName").innerHTML = req.responseText;
					clearInterval(CheckForChatRequestsID);
				}
			} 
		}
	}
	
	var IP;
	var url;
	var params;
	
	IP = document.getElementById("IP").innerHTML;
	params = "IP="+IP;
	url = "ChatRequest.php";
	
	req.open("POST", url, true);
	
	//Send the proper header information along with the request
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");

	req.send(params); 

}

	function repositionChatRequestWindow()
	{
		
			var objh;
			var objw;
			objh = parseFloat(document.getElementById("RequestWindow").style.height)/2;
			objw = parseFloat(document.getElementById("RequestWindow").style.width)/2;
							
			document.getElementById("RequestWindow").style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
 			document.getElementById("RequestWindow").style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';
			
			var objh;
			var objw;
			objh = parseFloat(document.getElementById("ChatWindow").style.height)/2;
			objw = parseFloat(document.getElementById("ChatWindow").style.width)/2;
							
			document.getElementById("ChatWindow").style.top = Math.floor(Math.round((document.documentElement.offsetHeight/2)+document.documentElement.scrollTop)-objh)+'px';
 			document.getElementById("ChatWindow").style.left = Math.floor(Math.round((document.documentElement.offsetWidth/2)+document.documentElement.scrollLeft)-objw)+'px';

	}

	function AcceptedChat()
	{
		CloseChatRequestWindow();
		OpenForChat = 0;
		Chatting = 1;
		
		document.getElementById("ChatWindow").style.visibility = "visible";
		document.getElementById("ChatWindow").style.top = 100;
		document.getElementById("ChatWindow").style.left = (screen.width - 500) / 2;
		
		ReplyToRequest()
		
		setInterval("CheckIncomingMessages()",10000);
		
	}
	
	
	function DeclinedChat()
	{
		CloseChatRequestWindow();	
		OpenForChat = 0; // Set a flag. They have declined a chat, so lets not bug them with the request window
		Chatting = 0;
		
		ReplyToRequest()
		
	}


	function CloseChatRequestWindow()
	{
		document.getElementById("RequestWindow").style.visibility = "hidden";
	}
	 
	
	function EndChat()
	{
		SendEndChatMessage();
		document.getElementById("ChatWindow").style.visibility = "hidden";	
		Chatting = 0;
		OpenForChat = 1;
	}

	function SendMessage()
	{
		SendOutgoingMessage();
	}
	
	// ***********************************************************************************************************
	// ***********             INDICATE CHAT Accept/Decline           ******************************************************
	// ***********************************************************************************************************

	//Browser Support Code
	function ReplyToRequest(){
	try{
		// Opera 8.0+, Firefox, Safari
		reply = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			reply = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				reply = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	// Create a function that will receive data sent from the server
	reply.onreadystatechange = function(){
		if(reply.readyState == 4 ){	
			// No need to do anything here, we have replied to the chat request		
		}
	}
	
	var IP;
	var url;
	var params;
	var Accepted;
	
	if(Chatting == 0){
		Accepted = "False"
	} else {
		Accepted = "True"
	}
	
	IP = document.getElementById("IP").innerHTML;
	params = "IP="+IP+"&Accepted="+Accepted;
	url = "ChatRequestReply.php";
	
	reply.open("POST", url, true);
	
	//Send the proper header information along with the request
	reply.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	reply.setRequestHeader("Content-length", params.length);
	reply.setRequestHeader("Connection", "close");

	reply.send(params); 

}

	// ***********************************************************************************************************
	// ***********             INCOMING MESSAGES            ******************************************************
	// ***********************************************************************************************************

	//Browser Support Code
	function CheckIncomingMessages(){
	try{
		// Opera 8.0+, Firefox, Safari
		incoming = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			incoming = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				incoming = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	// Create a function that will receive data sent from the server
	incoming.onreadystatechange = function(){
		if(incoming.readyState == 4 & Chatting == 1 & incoming.responseText != ''){	
				document.getElementById("txt_Incoming").innerText += "\nSailing-Whitsundays: "+incoming.responseText;
				document.getElementById("txt_Incoming").scrollTop = document.getElementById("txt_Incoming").scrollHeight - document.getElementById("txt_Incoming").clientHeight;
		}
	}
	
	var IP;
	var url;
	var params;
	
	IP = document.getElementById("IP").innerHTML;
	params = "IP="+IP;
	url = "ChatMessages.php";
	
	incoming.open("POST", url, true);
	
	//Send the proper header information along with the request
	incoming.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	incoming.setRequestHeader("Content-length", params.length);
	incoming.setRequestHeader("Connection", "close");

	incoming.send(params); 

}

	// ***********************************************************************************************************
	// ***********             OUTGOING MESSAGES            ******************************************************
	// ***********************************************************************************************************

	//Browser Support Code
	function SendOutgoingMessage(){
	try{
		// Opera 8.0+, Firefox, Safari
		outgoing = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			outgoing = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				outgoing = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	// Create a function that will receive data sent from the server
	outgoing.onreadystatechange = function(){
		if(outgoing.readyState == 4 & Chatting == 1){	
				document.getElementById("txt_Incoming").innerText += "\n\nYou: "+document.getElementById("txt_Outgoing").innerText;	
				document.getElementById("txt_Outgoing").innerText = "";	
				document.getElementById("txt_Incoming").scrollTop = document.getElementById("txt_Incoming").scrollHeight - document.getElementById("txt_Incoming").clientHeight;
				
		}
	}
	
	var IP;
	var url;
	var params;
	var message;
	
	message = document.getElementById("txt_Outgoing").innerText;	
	message.replace("'","");
	
	IP = document.getElementById("IP").innerHTML;
	
	params = "IP="+IP+"&Message="+message;
	url = "ChatMessagesIncoming.php";
	
	outgoing.open("POST", url, true);
	
	//Send the proper header information along with the request
	outgoing.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	outgoing.setRequestHeader("Content-length", params.length);
	outgoing.setRequestHeader("Connection", "close");

	outgoing.send(params); 

}

// ***********************************************************************************************************
	// ***********             Closing Chat Session at visitors request           ******************************************************
	// ***********************************************************************************************************

	//Browser Support Code
	function SendEndChatMessage(){
	try{
		// Opera 8.0+, Firefox, Safari
		outgoing = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			outgoing = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				outgoing = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	// Create a function that will receive data sent from the server
	outgoing.onreadystatechange = function(){
		if(outgoing.readyState == 4 & Chatting == 1){	
				document.getElementById("txt_Incoming").innerText += "\n\nYou: "+document.getElementById("txt_Outgoing").innerText;	
				document.getElementById("txt_Outgoing").innerText = "";	
				
		}
	}
	
	var IP;
	var url;
	var params;
	var message;
	
	message = "User has closed the chat session..";	
	message.replace("'","");
	
	IP = document.getElementById("IP").innerHTML;
	
	params = "IP="+IP+"&Message="+message;
	url = "ChatMessagesIncoming.php";
	
	outgoing.open("POST", url, true);
	
	//Send the proper header information along with the request
	outgoing.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	outgoing.setRequestHeader("Content-length", params.length);
	outgoing.setRequestHeader("Connection", "close");

	outgoing.send(params); 

}

