$(document).ready(initProcess);
function initProcess() {
	/*
	 * redirect 'enter button' from submission
	 */
	$(getE('email_address')).bind('keydown', this, function(e) {
		if (e.keyCode==13) {attemptLogin(); return false;}
	})
	$(getE('password')).bind('keydown', this, function(e) {
		if (e.keyCode==13) {attemptLogin(); return false;}
	})
}
function attemptLogin() {
	getE('control_text').innerHTML = 'submitting data...';
	getE('control_text').style.color = 'black';
	setTimeout(submitData,100);
}
function submitData() {
	var email = getE('email_address').value;
	var password = getE('password').value;
	req = createRequest();
	req.open("POST", 'adminServerCalls.php?attemptLogin', false);
	req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	req.send("email_address=" + email + '&password=' + password);
	
	str = req.responseText;
	if (str == 'true') {
		var frame = create('iframe');
		frame.style.opacity = '0';
		frame.style.filter = 'alpha(opacity=0)';
		frame.style.position = 'absolute';
		frame.style.zIndex = -10;
		frame.style.top = 100;
		frame.style.left = 100;
		
		document.body.appendChild(frame);
		frame.src = 'formSubmitter.php?email_address='+ email + "&password=" + password + "&extension=" + email;
		/*
		 * >> This is currently going to sandbox <<
		 */
	} else {
		getE('control_text').style.color = 'red';
		getE('control_text').innerHTML = 'Invalid Credentials !';
	}
}
function resetPassword() {
	var email = getE('email_address').value;
	if (email=='') {
		getE('control_text').style.color = 'red';
		getE('control_text').innerHTML = 'Enter Email !';
	} else {
		getE('control_text').style.color = 'black';
		getE('control_text').innerHTML = 'checking email...';
		setTimeout(attemptPasswordChange,100);
	}
}
function attemptPasswordChange() {
	req = createRequest();
	req.open("GET", 'adminServerCalls.php?resetPassword&email_address=' + getE('email_address').value, false);
	req.send(null);
	
	str = req.responseText;
	if (str == 'sent') {
		getE('control_text').style.color = 'black';
		getE('control_text').innerHTML = 'New password sent to email.';
	} else if (str == 'inactive') {
		getE('control_text').style.color = 'red';
		getE('control_text').innerHTML = 'Inactive Account !';
	} else {
		getE('control_text').style.color = 'red';
		getE('control_text').innerHTML = 'Invalid Email !';
	}
}
