
function BxLoginForm()
{
	this.rUserName = null;
	this.rPassword = null;
	this.rSubmit = null;
	this.rCancel = null;
	this.rEMail = null;
	this.rResend = null;
	this.rRetrieve = null;
	this.rMainForm = null;
	this.logedin = 'undefined';
	this.createLoginForm();
		
	var d = document.getElementById('background');
	d.style.width = document.body.clientWidth + "px";
	d.style.height = (window.innerHeight ? (window.innerHeight + 30) : screen.height) + "px";				
	d.style.top = getScroll() - 30 + "px";
	
}

BxLoginForm.prototype.createElem = function(sTagName, sName, rParent, sClass, sType)
{
	var rElem = document.getElementById(sName);
   	if(!rElem){
		try
   		{
    		rElem = document.createElement('<' + sTagName + ' name="' + sName + '" '+ (sType?('type="'+sType+'"'):'') +'>');
    	}
    	catch(variable)
    	{
			rElem = document.createElement(sTagName);
			rElem.name = sName;
			rElem.type = sType;
   		}
   		rElem.id = sName;
   		if(sClass)rElem.className = sClass;
   		rElem.style.cssText = "z-index:99;";
   		
   		rParent.appendChild(rElem);
   	}
   	return rElem;
}

BxLoginForm.prototype.createInputElement = function(sName, rParent, sType, sClass)
{
	var rElem = this.createElem('input', sName, rParent, sClass, sType);
	return rElem;
}




/**
 * Inserts Error message into <div> with id=sType
 *
 * @param	string	$sType		Type of the error message
 * @param	string	$sMesage	String with error message
 */
BxLoginForm.prototype.insertMessage = function (sType, sMessage, sClassName)
{
	var rErrMessageElem = document.getElementById(sType);
	
	this.removeMessage(sType);
	
	var rErrElem = document.createElement('div');
	rErrElem.id = sType + 'child';
	if(sClassName) rErrElem.className = sClassName;
	rErrElem.innerHTML = sMessage;
	rErrMessageElem.appendChild(rErrElem);
}

/**
 * Removes Error message from <div> with id=sType
 *
 * @param	string	$sType		Type of the error message
 */
BxLoginForm.prototype.removeMessage = function(sType)
{
	var rErrMessageElem = document.getElementById(sType);
	var rErrElem = document.getElementById(sType + 'child');
	if(rErrElem)rErrMessageElem.removeChild(rErrElem);
}

/**
 * Removes Error message from <div> with id=sType and returns false
 *
 * @param	string	$sType		Type of the error message
 */
BxLoginForm.prototype.acceptValue = function(sType)
{
   	this.removeMessage(sType);
  	return false;    
}

/**
 * Inserts Error message into <div> with id=sType returns true and makes Global Incorrect Data Flag true
 *
 * @param	string	$sType		Type of the error message
 * @param	string	$sMesage	String with error message
 */
BxLoginForm.prototype.rejectValue = function(sType, sMessage)
{
	this.insertMessage(sType, sMessage, 'ErrMsg');
   	return true;		
}

BxLoginForm.prototype.checkUserName = function()
{
	var sUserName = this.rUserName.value;
	var re = /^[a-zA-Z0-9]{3,15}$/;
	
	if(!sUserName.match(re)) return this.rejectValue('login-ErrMsgUserName', "User names must have 3-15 characters, and only alphanumeric characters. Please enter it again.");
	else return this.acceptValue('login-ErrMsgUserName');
	
}

BxLoginForm.prototype.checkPassword = function()
{
	var sPass = this.rPassword.value;
	
	if(sPass.length >= 4) return this.acceptValue('login-ErrMsgPassword');	
	else
	return this.rejectValue('login-ErrMsgPassword', "Your password must be at least six characters long. Please enter it again.");	
}

BxLoginForm.prototype.checkEMail = function()
{
	var sEMail = this.rEMail.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;

    if (!sEMail.match(re)) return this.rejectValue('login-ErrMsgEMail', "Your email address wasn't entered correctly. Please enter a valid email address.");
    else return this.acceptValue('login-ErrMsgEMail');
}

BxLoginForm.prototype.login = function()
{
	checkUserNameResult = this.checkUserName();
	checkPasswordResult = this.checkPassword();
	if((!checkUserNameResult)&&(!checkPasswordResult))
	{
		var aData = new Array(new DataToServer('sUserName', this.rUserName.value),
							  new DataToServer('sPassword', this.rPassword.value),
  							  new DataToServer('bLoginAction', '1'));
	
		var rThisObject = this;			
		
		function myFunction(requestObject)
		{
			var sDataFromServer = requestObject.responseXML;
			var rResultNode = sDataFromServer.getElementsByTagName('result')[0];
			var sResult = rResultNode.firstChild.nodeValue;
		
			if(sResult=='Granted')
			{
				rThisObject.logedin = true;
				
				rThisObject.removeMainForm();
				if(rThisObject.onLogedIn)eval(rThisObject.onLogedIn);
			}
			else
			{
				if(sResult=='Denied')
				{
					rThisObject.logedin = false;
					
					rThisObject.insertMessage('login-ErrMsgGlobal', 'Please try entering your information again. Forgot your password? You can retrieve it using the form down bellow. Not joined? Join using this link <a href="'+aBxConfig['urlRoot']+'join"/>JOIN</a>', 'ErrMsg');
					
					rThisObject.removeRetriveForm();
					rThisObject.removeVerificationForm();
					rThisObject.createRetriveForm();
				}
				else
				{
					var rEMailNode = sDataFromServer.getElementsByTagName('email')[0];
					var sEMail = rEMailNode.firstChild.nodeValue;
					
					rThisObject.logedin = false;
					
					rThisObject.removeMessage('login-ErrMsgGlobal');
					rThisObject.removeRetriveForm();
					rThisObject.removeVerificationForm();
					rThisObject.createVerificationForm(sEMail);
				}
			}
		}
		
		var rAjax = new BxRequest('POST',aBxConfig['urlRoot'] + 'logincheck',myFunction,aData);
	}
}

BxLoginForm.prototype.cancel = function()
{
	this.logedin = false;
	this.removeMainForm();
}

BxLoginForm.prototype.resend = function()
{
	var aData = new DataToServer('sEMail',this.rEMail.value);

	var rThisObject = this;			
		
	function myFunction(requestObject)
	{
		var sDataFromServer = requestObject.responseText;
		
		if(sDataFromServer)
		{
			var text = '<div class="OkMsg" id="server">Great, mail was send.</div>';
			rThisObject.rResend.disabled = true;
		}
		else var text = '<div class="ErrMsg" id="server">All bad. Could\'t send message</div>';

		rThisObject.insertMessage('login-ErrMsgEMail', text);
	}
	var rAjax = new BxRequest('POST',aBxConfig['urlRoot']+'Join/resendVerification',myFunction,aData);
}


BxLoginForm.prototype.retrieve = function()
{
	checkEMailResult = this.checkEMail();
	if(!checkEMailResult)
	{
		var aData = new DataToServer('sEMail',this.rEMail.value);
	
		var rThisObject = this;			
		
		function myFunction(requestObject)
		{
			var sDataFromServer = requestObject.responseText;
			if(sDataFromServer)
			{
				rThisObject.removeRetriveForm();
				rThisObject.insertMessage('login-ErrMsgGlobal', 'Great, password was send.', 'OkMsg');
			}
			else var text = '<div class="ErrMsg" id="server">All bad. Could\'t send message</div>';

			rThisObject.insertMessage('login-ErrMsgEMail', text);			
		}
		var rAjax = new BxRequest('POST',aBxConfig['urlRoot']+'retrievePassword',myFunction,aData);
	}
}


BxLoginForm.prototype.createMainForm = function ()
{
	var rDocumentBody = document.getElementsByTagName('body')[0];
	var e = rDocumentBody;

	var rBackGround = this.createElem('div','background',rDocumentBody,'back-ground');
	
	//rBackGround.cssText = "z-index:50000;position:absolute;" + (getScroll() - 30) + "px; left:0px;width:" + e.clientWidth + "px;height:" + (window.innerHeight ? (window.innerHeight + 30) : screen.height) + "px;filter:alpha(opacity=50);-moz-opacity:.50;opacity:.50;background-color:#ccc;text-align:center";
	
	return this.createElem('div','main-form',rDocumentBody,'login-form');	
}

BxLoginForm.prototype.removeMainForm = function ()
{
	var rDocumentBody = document.getElementsByTagName('body')[0];
	var rBackGround = document.getElementById('background');
	
	if(this.onClose) eval(this.onClose);
	
	if(this.rMainForm){
		rDocumentBody.removeChild(rBackGround);
		rDocumentBody.removeChild(this.rMainForm);
		this.rUserName = null;
		this.rPassword = null;
		this.rSubmit = null;
		this.rCancel = null;
		this.rEMail = null;
		this.rResend = null;
		this.rRetrieve = null;
		this.rMainForm = null;
	}
}

BxLoginForm.prototype.createLoginForm = function ()
{	
	this.rMainForm = this.createMainForm();
	
	var rFieldSet = this.createElem('fieldset','fieldset1',this.rMainForm);
	var rLegend = this.createElem('legend','legend1',rFieldSet);
		rLegend.innerHTML = 'Please login';
		
	var rUserDiv = this.createElem('div','userdiv',rFieldSet,'login-div');
	var rUserLabel = this.createElem('label','userlabel',rUserDiv);
		rUserLabel.innerHTML = 'Username';	
	this.rUserName = this.createInputElement('login-username',rUserDiv,'text','form-text');
	var rUserSpan = this.createElem('span','userspan',rUserDiv,'form-note');
		rUserSpan.innerHTML = '';
	var rUserErrorDiv = this.createElem('div','login-ErrMsgUserName',rUserDiv);
		
	var rPasswordDiv = this.createElem('div','passworddiv',rFieldSet,'login-div');
	var rPasswordLabel = this.createElem('label','passwordlabel',rPasswordDiv);
		rPasswordLabel.innerHTML = 'Password';	
	this.rPassword = this.createInputElement('login-password',rPasswordDiv,'password','form-text');
	var rPasswordSpan = this.createElem('span','passwordspan',rPasswordDiv,'form-note');
		rPasswordSpan.innerHTML = '';
	var rPasswordErrorDiv = this.createElem('div','login-ErrMsgPassword',rPasswordDiv);
	
	var rSubmitDiv = this.createElem('div','submitdiv',rFieldSet,'submit-div');
	this.rSubmit = this.createInputElement('login-submit',rSubmitDiv,'button','submit-button');
	this.rSubmit.value = 'Login'
	this.rSubmit.onclick = function(){loginform.login();}

	this.rCancel = this.createInputElement('login-cancel',rSubmitDiv,'button','submit-button');
	this.rCancel.value = 'Cancel'
	this.rCancel.onclick = function(){loginform.cancel();}

	
	var rGlobalErrorDiv = this.createElem('div','login-ErrMsgGlobal',this.rMainForm,'global-message');
}

BxLoginForm.prototype.removeVerificationForm = function()
{	
	var rFieldSet = document.getElementById('fieldset2');
	
	if(rFieldSet){
		this.rMainForm.removeChild(rFieldSet);
		this.rEMail = null;
		this.rResend = null;
	}
}

BxLoginForm.prototype.createVerificationForm = function(sEMail)
{		
	this.rMainForm = this.createMainForm();
	
	var rDiv = this.createElem('div','',this.rMainForm, 'err2');
	rDiv.innerHTML = 'Login Failed! You did not activate your account or your password is not correct!';
	
	var rFieldSet = this.createElem('fieldset','fieldset2',this.rMainForm);
	var rLegend = this.createElem('legend','legend2',rFieldSet);
		rLegend.innerHTML = 'Never Got the Email?';
		
	var rEMailDiv = this.createElem('div','emaildiv',rFieldSet,'resend-div');
	var rEMailLabel = this.createElem('label','emaillabel',rEMailDiv);
		rEMailLabel.innerHTML = 'Email address: ';	
	this.rEMail = this.createInputElement('login-email',rEMailDiv,'text','form-text');
	this.rEMail.value = sEMail;
	//this.rEMail.disabled = true;
	
	this.rResend = this.createInputElement('login-resend',rEMailDiv,'button','resend-button');
	this.rResend.value = 'Resend Activation E-mail'
	this.rResend.onclick = function(){loginform.resend();}
	
	var EMailSpan = this.createElem('span','emailspan',rEMailDiv,'form-note');
		EMailSpan.innerHTML = '';
	var rEMailErrorDiv = this.createElem('div','login-ErrMsgEMail',rEMailDiv);	
}

BxLoginForm.prototype.removeRetriveForm = function()
{	
	var rFieldSet = document.getElementById('fieldset3');
	
	if(rFieldSet){
		this.rMainForm.removeChild(rFieldSet);
		this.rEMail = null;
		this.rRetrieve = null;
	}
}

BxLoginForm.prototype.createRetriveForm = function()
{		
	this.rMainForm = this.createMainForm();
	
	var rFieldSet = this.createElem('fieldset','fieldset3',this.rMainForm);
	var rLegend = this.createElem('legend','legend3',rFieldSet);
		rLegend.innerHTML = 'Lost Your Username or Password?';
		
	var rEMailDiv = this.createElem('div','emaildiv',rFieldSet,'resend-div');
	var rEMailLabel = this.createElem('label','emaillabel',rEMailDiv);
		rEMailLabel.innerHTML = 'Enter your e-mail address';	
	this.rEMail = this.createInputElement('login-email',rEMailDiv,'text','form-text');
	
	this.rRetrieve = this.createInputElement('login-resend',rEMailDiv,'button','resend-button');
	this.rRetrieve.value = 'Retrieve password'
	this.rRetrieve.onclick = function(){loginform.retrieve();}
	
	var EMailSpan = this.createElem('span','emailspan',rEMailDiv,'form-note');
		EMailSpan.innerHTML = 'This address and your login will be used to retrieve password.';
	var rEMailErrorDiv = this.createElem('div','login-ErrMsgEMail',rEMailDiv);	
}
