// JavaScript Document
var req=new Array();
var The_ID=new Array();

function getobjbyid(id){ // Short Name for "document.getElementById"
    var obj = null;
    if(document.getElementById)obj = document.getElementById(id);
    else if(document.all)obj = document.all[id];
    return obj;
}

function sR(url,params,HttpMethod,anyID){
	if (!HttpMethod)HttpMethod="POST";
	req.push(initXMLHTTPRequest());
	The_ID.push(anyID);
	i=parseInt(req.length)-1;
		if (req[i]){
			req[i].open(HttpMethod,url,true);
			req[i].onreadystatechange=onReadyState;
			req[i].setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			req[i].send(params);
		}
}

function initXMLHTTPRequest(){ // Initialize the XML HTTP  Object
	var xRequest=null;
	if (window.XMLHttpRequest)xRequest=new XMLHttpRequest();
	else if (window.ActiveXObject)xRequest=new ActiveXObject("Microsoft.XMLHTTP");
	else {	}
	return xRequest;
}


function onReadyState(){
	for (i=0;i<req.length;i++)
	{
		if (req[i]) {
			var ready=req[i].readyState;
			var data=null;	
			if (ready==4) {	
				pass_ID=The_ID[i];
				
				data=req[i].responseText;
				
				The_ID.splice(i,1);
				
				req.splice(i,1);		

				if(data.indexOf("[MSGBOX]")>-1)
				{
					alert(data.replace("[MSGBOX]",""));
				}
				else if(data.indexOf("[REFRESH]")>-1)
				{
					location.reload();
				}
				else if(data.indexOf("[PASSWORD_CHANGED]")>-1)
				{
					alert("Your password has been successfully changed.");
					location.reload();
				}
				else if(data.indexOf("[MAIN_PAGE]")>-1)
				{
					var loc = window.location;
					var arr=(loc.toString()).split("?");
					alert("Your password has been successfully changed.");
					window.location = arr[0];
				}
				else if(data.indexOf("[MAIL_ERROR]")>-1)
				{
					var loc = window.location;
					var arr=(loc.toString()).split("?");
					window.location = (arr[0]+'?op=forget_password&msg_type=error');
				}
				else if(data.indexOf("[MAIL_SENT]")>-1)
				{
					var loc = window.location;
					var arr=(loc.toString()).split("?");
					window.location = (arr[0]+'?op=forget_password&msg_type=mail_sent');
				}
				else if(data.indexOf("[INVALID_USER]")>-1)
				{
					document.getElementById("password").value = "";
					document.getElementById("password").focus();
					document.getElementById("error_message").innerHTML = "Invalid user name or password";
					document.getElementById("error_message").style.display = "block";
				}
				else if(data.indexOf("[INACTIVE_USER]")>-1)
				{
					document.getElementById("password").value = "";
					document.getElementById("password").focus();
					document.getElementById("error_message").innerHTML = "Error while logging in, Inactive user.";
					document.getElementById("error_message").style.display = "block";
				}
				else if(data.indexOf("[INVALID_NAME]")>-1)
				{
					document.getElementById("username").value = "";
					document.getElementById("username").focus();
					document.getElementById("error_message").innerHTML = "Invalid user name";
					document.getElementById("error_message").style.display = "block";
				}
				else if(data.indexOf("[INVALID_PASSWORD]")>-1)
				{
					document.getElementById("password").value = "";
					document.getElementById("conf_password").value = "";
					
					document.getElementById("password").focus();
					
					document.getElementById("error_message").innerHTML = "Invalid password";
					document.getElementById("error_message").style.display = "block";
				}
				else if(data.indexOf("[INCORRECT_OLDPASSWORD]")>-1)
				{
					document.getElementById("old_password").value = "";
					
					document.getElementById("old_password").focus();
					
					document.getElementById("error_message").innerHTML = "Invalid password";
					document.getElementById("error_message").style.display = "block";
				}
				
				else if(data.indexOf("[VALIDATE]")>-1) { }// Do Something
				
				else if(data.indexOf("[DISPLAY_ERROR]")>-1){ } // Do Something Else
				
				else if(data.indexOf("[NULL]")>-1){ }// Do Nothing In The Html
				
				else toConsole(data,pass_ID);
			}		
			else {	/* Something Here */	}
		}	
	}
}

function toConsole(data,pass_ID)
{
	if (data!=null && getobjbyid(pass_ID)){
		if(getobjbyid(pass_ID))getobjbyid(pass_ID).innerHTML=data;
	}
}
