var PollingInterval = 10;
var PollingActive = true;
UserAccount = UserAccount.substring(0,UserAccount.indexOf('@'));

// Run our intial polling, after this, future requests are timed based on the interval
setTimeout("ChatPoller(UserAccount);",1000);

function ChatPoller(UserAccount){
	if(!document.getElementById){ PollingActive = false; return; } // Don't poll if they don't support DHTML
	if(UserAccount == ""){ PollingActive = false; return; } // Don't poll if we don't have a user
	if(PollingActive){
		HttpGetRequest('/private_chat/poller.aspx?name=' + escape(UserAccount) + '&rand=' + Math.random(), ChatPollerResponseHandler);
	}
}

function ChatPollerResponseHandler(){
	// here's where we'll place the alert window and notify the user if necessary!
	if (http_request.readyState == 4) {
	try {
		if (http_request.status == 200) {
			if(http_request.responseText != ""){
				// Let the user know about the request
				var el = document.getElementById('ChatAlerter');
				el.className = "ShowAlerter";
				el.style.top = getVerticalScroll() + 50;
				var el2 = document.getElementById('ChatAlerterUsername');
				el2.innerHTML = http_request.responseText;
			} else {
				// Hide the window - the request has expired
				var el = document.getElementById('ChatAlerter');
				el.className = "HideAlerter";
			}
		} // end if status == 200
	} catch(e) { el.innerHTML = "We encountered an error processing your results. Please try again later."; } // end try
	// Get ready to poll again
	setTimeout("ChatPoller(UserAccount);", PollingInterval * 1000);
	} // end readyState == 4
}

function getVerticalScroll(){
	return document.body.scrollTop;
}


function ChatRequest(User){
	HttpGetRequest('/private_chat/request.aspx?sender=' + escape(UserAccount) + '&recipient=' + escape(User), ChatRequestResponseHandler);
	return;
}

function ChatRequestResponseHandler(){
	if (http_request.readyState == 4) {
		try {
			if (http_request.status == 200) {
				PollingActive = false;
				var WindowOptions = "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0";
				var NewWin = window.open('private_chat/index.aspx?name=' + escape(UserAccount),'PrivateChat','width=541,height=402,'+ WindowOptions);
				NewWin.focus();
				return;
			}
		} catch(e) { }
	}
}


function ChatRequestReject(){
	// Close the popup and let the other user no that they were rejected
	var el = document.getElementById("ChatAlerter");
	el.className="HideAlerter";
	HttpGetRequest('/private_chat/poller.aspx?name=' + escape(UserAccount) + '&status=Y', ChatRequestRejectResponseHandler);
}

function ChatRequestRejectResponseHandler(){
	if (http_request.readyState == 4) {
		try {
			if (http_request.status == 200) {
				// Rejected!
				HttpGetRequest('/private_chat/message.aspx?sender=' + escape(UserAccount) + '&recipient=' + escape(el2.innerHTML) + "&message=User+declined+your+chat+request", null);
			}
		} catch(e) { }
	}
}

function ChatRequestAccept(){
	// Close the popup and let the other user know we're coming
	var el2 = document.getElementById('ChatAlerterUsername');
	el2.innerHTML = http_request.responseText;
	HttpGetRequest('/private_chat/request.aspx?sender=' + escape(UserAccount) + '&recipient=' + escape(el2.innerHTML), ChatRequestResponseHandler);
	var el = document.getElementById("ChatAlerter");
	el.className="HideAlerter";
	OpenPrivateChat();
}

function OpenPrivateChat(){
	PollingActive = false;
	var WindowOptions = "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0";
	var NewWin = window.open('private_chat/index.aspx?name=' + escape(UserAccount),'PrivateChat','width=541,height=402,'+ WindowOptions);
	NewWin.focus();
	return;
}
