// JavaScript Document
function ajax(){
	
	this.request = new Array();

	this.iBusy = new Boolean(false);

	this.requestMethod = 'get';

	this.waitTime = 2500;

	this.currentRetries = 1;

	this.maxRetries = 2;

	this.displayProgress = true;

	this.handleResponse = function(){};

	if(navigator.appName == 'Microsoft Internet Explorer'){

		this.requestObject = new ActiveXObject('Microsoft.XMLHTTP');

	} else {

		this.requestObject = new XMLHttpRequest();

	}

	this.sendInput = function(inputList , query , inputType){

		if(typeof inputType == 'undefined'){

			inputType = 'value';

		}

		if(typeof query == 'undefined'){

			query = 'index.php';

		}

		query += '?';
		
		if(inputList.length < 1){

			return false;

		} else {
			
			if(inputList.indexOf(',') == -1){

				var inputArray = new Array();

				inputArray[0] = inputList;

			} else {

				var inputArray = inputList.split(',');

			}
			

			var separator = String('');
			
			for(i in inputArray){

				switch(inputType){

					case "value":
						
						query += separator + inputArray[i] + '=' + document.getElementById(inputArray[i]).value;

						break;
					
					case "innerHTML":

						query += separator + inputArray[i] + '=' + document.getElementById(inputArray[i]).innerHTML;

						break;

				}

				separator = '&';

			}

			this.request.push(query);

			return this.sendRequest();

		}

	}

	this.sendRequest = function(){

		if(this.request.length < 1){

			return false;

		} else {

			if(this.iBusy == true){
	
				this.requestObject.onreadystatechange = function(){};

				this.requestObject.abort();

				this.iBusy = false;
	
			}

			for(i in this.request){
	
				if(this.request[i].length > 512){
	
					this.requestMethod = 'post';
	
				} else {
	
					this.requestMethod = 'get';
	
				}
				
				this.requestObject.open(this.requestMethod, this.request[i] , true);
	
				this.requestObject.onreadystatechange = this.handleResponse;
				
				this.requestObject.send(null);
	
			}
			
			if(this.displayProgress == true){

				wait();

			}

			this.dateObject = new Date();

			this.requestTime = this.dateObject.getTime();
			
			this.iBusy = true;
	
			this.request = new Array();

			return true;

		}
		
	}

}

function handleResponse(){

	if(ajax.requestObject.readyState == 4){
		
		ajax.iBusy = false;

		ajax.response = ajax.requestObject.responseText;
		
		if(ajax.response.length > 0){

			if(ajax.response.indexOf('|') != -1){

				var response = new Array();

				response = ajax.response.split('|');

				if(document.getElementById(response[0]) != null){
				
					document.getElementById(response[0]).innerHTML = response[1];

				}

				if(document.getElementById('errorSpan') != null && response[2] == 0){
					
					show('errorSpan');
					
					document.getElementById('errorSpan').innerHTML = response[3];
					
				}
				
				if(document.getElementById('msgSpan') != null && response[2] == 1){
					
					show('msgSpan');
							
					document.getElementById('msgSpan').innerHTML = response[3];				
		
				}

				if(ajax.displayProgress == true){

					hide('waitBox');

				}

			}

		}

	}

}

function sendRequest(query){

	ajax.handleResponse = handleResponse;

	ajax.displayProgress = true;

	ajax.request.push(query);

	ajax.sendRequest();

}

function sendLogin(){
	
	processor = '../user/index.php?action=sendLogin';
		
	var screenname = document.getElementById('screenname').value;

	var password1 = document.getElementById('password1').value;
	
	var rememberMe = document.getElementById('rememberMe').checked;
	
	query = processor + '&screenname=' + screenname + '&password1=' + password1+'&rememberMe='+rememberMe;

	ajax.handleResponse = handleLoginResponse;

	ajax.displayProgress = true;

	ajax.request.push(query);

	ajax.sendRequest();

}

function handleLoginResponse(){
	
	if(ajax.requestObject.readyState == 4){
		
		ajax.iBusy = false;

		ajax.response = ajax.requestObject.responseText;
		
		if(ajax.response.length > 0){

			if(ajax.response.indexOf('|') != -1){

				var response = new Array();

				response = ajax.response.split('|');

				if(document.getElementById(response[0]) != null){
				
					document.getElementById(response[0]).innerHTML = response[1];

				}

				if(document.getElementById('errorSpan') != null && response[2] == 0){
					
					show('errorSpan');
					
					document.getElementById('errorSpan').innerHTML = response[3];
					
				}
				
				if(document.getElementById('msgSpan') != null && response[2] == 1){
					
					show('msgSpan');
							
					document.getElementById('msgSpan').innerHTML = response[3];				
		
				}

				if(ajax.displayProgress == true){

					hide('waitBox');

				}

				if(response[2] == 1){

					startPollingChats();

				}

			}

		}

	}

}

function forgotPassword(){
	
	processor = '../user/index.php?action=forgotPassword';

	sendRequest(processor);
	
}

function showLogin(){
	
	processor = '../user/index.php?action=showLogin';
	
	sendRequest(processor);
	
}

function showChangePassword(){
	
	processor = '../user/index.php?action=showChangepassword';
	
	sendRequest(processor);
	
}

function changePassword(){
	
	processor = '../user/index.php?action=changePassword';
	
	processor = processor + '&password0=' + document.getElementById('password0').value + '&password1=' + document.getElementById('password1').value + '&password2=' + document.getElementById('password2').value;
	
	sendRequest(processor);
	
}
function resetPassword(){
	
	processor = '../user/index.php?action=resetPassword&email=' + document.getElementById('email').value;
	
	sendRequest(processor);
	
}
function signUp(){
	
	processor = '../user/index.php?action=signUp';
	
	sendRequest(processor);
	
}
function signUpPost(){
	
	processor = '../user/index.php?action=signUpPost&screenname='+document.getElementById('screenname').value+'&email='+document.getElementById('email').value;
	
	sendRequest(processor);
	
}
function signOut(){
	
	processor = '../user/index.php?action=signOut';
	
	sendRequest(processor);
	
}

var newWin = null;

function popUp(strURL, strType, strHeight,strWidth) {

	if (newWin != null && !newWin.closed)

		newWin.close();

		var strOptions="";

	if (strType=="elastic")

		strOptions="scrollbars,"+"resizable,height="+strHeight+",width="+strWidth;
	
		newWin = window.open(strURL, 'newWin',
	
		strOptions);
	
		newWin.focus();

}

var newWin2 = null;

function popUp2(strURL, strType, strHeight,strWidth) {

	if (newWin2 != null && !newWin2.closed)

		newWin2.close();

		var strOptions="";

	if (strType=="elastic")

		strOptions="scrollbars,"+"resizable,height="+strHeight+",width="+strWidth;
	
		newWin = window.open(strURL, 'newWin2',
	
		strOptions);
	
		newWin2.focus();

}

function wait(){

	if(document.getElementById('errorSpan') != null){
		
		hide('errorSpan');
		
	}

	if(document.getElementById('msgSpan') != null){
		
		hide('msgSpan');
		
	}

	show('waitBox');

}

function show(objectID){
	
	changeOpac(0 , objectID);
	
	document.getElementById(objectID).style.display = 'block';
	
	opacity(objectID , 0 , 100 , 600);	
	
}
function hide(objectID){
		
	document.getElementById(objectID).style.display = 'none';
	
	changeOpac(100 , objectID);
	
	opacity(objectID , 100 , 0 , 600);
	
	
}

function opacity(id, opacStart, opacEnd, millisec) {

	var speed = Math.round(millisec / 100);

	var timer = 0;

	if(opacStart > opacEnd) {

		for(i = opacStart; i >= opacEnd; i--) {

			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));

			timer++;

		}

	} else if(opacStart < opacEnd) {

		for(i = opacStart; i <= opacEnd; i++){

			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));

			timer++;

		}

	}

}

function changeOpac(opacity, id) {

	var object = document.getElementById(id).style; 

	object.opacity = (opacity / 100);

	object.MozOpacity = (opacity / 100);

	object.KhtmlOpacity = (opacity / 100);

	object.filter = "alpha(opacity=" + opacity + ")";

}

function shiftOpacity(id, millisec) {

	if(document.getElementById(id).style.opacity == 0) {

		opacity(id, 0, 100, millisec);

	} else {
		
		opacity(id, 100, 0, millisec);

	}

}

function blendimage(divid, imageid, imagefile, millisec) {

	var speed = Math.round(millisec / 100);

	var timer = 0;
	
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	changeOpac(0, imageid);
	
	document.getElementById(imageid).src = imagefile;

	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {

	var currentOpac = 100;
	
	if(document.getElementById(id).style.opacity < 100) {

		currentOpac = document.getElementById(id).style.opacity * 100;

	}

	opacity(id, currentOpac, opacEnd, millisec)
}

