/*
 * Ext JS Library 2.0.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
    var win;
	var xmlObj = null;
	
	function hideWindow()
	{
	win.hide();	
	}
	
		
	function GetXmlHttpObject()
{
 var obj = null;
 if (window.XMLHttpRequest)
 {
 obj = new XMLHttpRequest();
 }
 if(window.ActiveXObject)
 {
 obj = new ActiveXObject("Microsoft.XMLHTTP");
 }
 return obj;

}



function registerUser()

{
document.getElementById("registerBtn").disabled = true;
document.getElementById("errorDiv").innerHTML = "";
document.getElementById("infoDiv").style.visibility = "visible";
document.getElementById("infoDiv").innerHTML = "Processing...please wait";
xmlObj = GetXmlHttpObject();

if(xmlObj == null)

{

	alert("your browser doesnot support Http");

	return;

}
var user = document.getElementById("user").value;
var pass = document.getElementById("pass").value;
var cpass = document.getElementById("cpass").value;
var regCode = document.getElementById("betaRegCode").value;
var url = "/server/servlet/NewUserServlet?username="+user+"&pass=" + pass + "&cpass=" + cpass + "&betaRegCode=" + regCode  + "&source=ajax&rand="+Math.random();

xmlObj.onreadystatechange=stateChanged; 

xmlObj.open("GET",url,true);
//xmlObj.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
xmlObj.send(null);
}

function stateChanged()

{

	if(xmlObj.readyState == 4 || xmlObj.readyState == "complete")

	{
var res = xmlObj.responseText;
if(res != "true")
{
	document.getElementById("infoDiv").innerHTML = "";
	document.getElementById("infoDiv").style.visibility = "hidden";
		document.getElementById("errorDiv").style.visibility = "visible";
	document.getElementById("errorDiv").innerHTML = res;
document.getElementById("registerBtn").disabled = false;
}
else
{
	document.getElementById("errorDiv").innerHTML = "";
	document.getElementById("errorDiv").style.visibility = "hidden";
	document.getElementById("regForm").innerHTML = "";
	document.getElementById("regForm").style.visibility = "hidden";
	document.getElementById("infoDiv").style.visibility = "visible";
	document.getElementById("infoDiv").innerHTML = "";
	document.getElementById("infoDiv").innerHTML = "You have been successfully registered. An email has been sent to your address, please activate your account to start using the service. <a href='#' onClick='hideWindow()'>Click here to close</a>";
	

}
	//document.getElementById("topics").innerHTML = xmlObj.responseText;	

	}
}


function verifyEmptyFields()
{
var empty = false;
var user = document.getElementById("user").value;
var pass = document.getElementById("pass").value;
var cpass = document.getElementById("cpass").value;
var errStr = "";
var notEqual = false;

if(user == "")
{
	empty = true;
	errorStr = "Email,";
}
else if(pass == "")
{
	empty = true;
	errorStr = "Password,";
	
}

else if(cpass == "")
{
	empty = true;
	errorStr = "Confirm Password,";
	
}

else if(cpass != pass)
{
	empty = true;
	errorStr = "password and confirm password should be same";
	notEqual = true;
	
}


if(empty == true)
{
	if(notEqual == true)
document.getElementById("errorDiv").innerHTML = errorStr;	
else
document.getElementById("errorDiv").innerHTML = errorStr + " cannot be empty";	
}
else
{
registerUser();	
}


}


Ext.onReady(function(){
    
    var button = Ext.get('show-btn');

    button.on('click', function(){
        // create the window on the first click and reuse on subsequent clicks
        if(!win){
            win = new Ext.Window({
                el:'hello-win',
                layout:'fit',
                width:300,
                height:200,
                closeAction:'hide',
                plain: true,
				modal: true,
				resizable: false
            });
        }
        win.show(this);
    });
});
