goog.require('goog.array');
goog.require('goog.dom');
goog.require('goog.dom.classes');
goog.require('goog.events');
goog.require('goog.net.EventType');
goog.require('goog.net.XhrIo');

validateField = function(el, condition) {
	if (condition) {
		el.style.background = 'white';
		return true; 
	} else {
		el.style.background = 'red';
		return false;
	}		
};

registrationForm = function(handleId, emailId, pass1Id, pass2Id, subId, formId) {
	this.formEl = goog.dom.$(formId);
	this.handleEl = goog.dom.$(handleId);
	this.pass1El = goog.dom.$(pass1Id);
	this.pass2El = goog.dom.$(pass2Id);
	this.emailEl = goog.dom.$(emailId);
	this.submitEl = goog.dom.$(subId);
	this.xmlObj = new goog.net.XhrIo();
	this.addListeners();
};

registrationForm.prototype.addListeners = function() {
	goog.events.listen(this.formEl, goog.events.EventType.SUBMIT,
					   function(evt) {
						   if (this.verifyInput()) {this.registerUser();}
						   evt.preventDefault();},
					   true, this);
	goog.events.listen(this.xmlObj,
					   goog.net.EventType.COMPLETE,
					   function() {this.displayResult();},
					   true, this);
};

registrationForm.prototype.verifyInput = function() {
	var success = true;
	if (this.handleEl.value.length < 6) {
	  this.handleEl.style.background = 'red';
	  success = false;
	} else {
	  this.handleEl.style.background = 'white';	
	}
	var regEx = new RegExp('^.+@[^\.].*\.[a-z]{2,}');
	if (!regEx(this.emailEl.value)) {
	  this.emailEl.style.background = 'red';
	  success = false;
	} else {
	  this.emailEl.style.background = 'white';	
	}
	if (this.pass1El.value.length < 6 || this.pass2El.value.length < 6 ||
		this.pass1El.value != this.pass2El.value) {
	  this.pass1El.style.background = 'red';
	  this.pass2El.style.background = 'red';
	  success = false;
	} else {
	  this.pass1El.style.background = 'white';
	  this.pass2El.style.background = 'white';	
	}
	return success;
};

registrationForm.prototype.registerUser = function() {
	goog.dom.$('alert-message').innerHTML = "";
	var url = 'http://www.fcycle.com/actions/user_registration.php'
	var data = 'handle=' + this.handleEl.value
	  + '&email=' + this.emailEl.value
	  + '&password=' + this.pass1El.value;
	this.xmlObj.send(url, 'POST', data);
};

registrationForm.prototype.displayResult = function() {
	if (this.xmlObj.isSuccess()) {
		eval('var response = (' + this.xmlObj.getResponseText() + ');');
		var container = goog.dom.$dom('div');
		if (response[0].success == 'true') {
		  container.style.color = "green";
  		  setTimeout("location.href = '/';", 2500);
		}
		else 
		  container.style.color = "red";
		container.innerHTML = response[0].message;
		goog.dom.appendChild(goog.dom.$('alert-message'), container);
	}
};

loginForm = function(handleId, passId, subId, formId) {
	this.formEl = goog.dom.$(formId);
	this.handleEl = goog.dom.$(handleId);
	this.passEl = goog.dom.$(passId);
	this.submitEl = goog.dom.$(subId);
	this.xmlObj = new goog.net.XhrIo();
	this.addListeners();
};

loginForm.prototype.addListeners = function () {
	goog.events.listen(this.formEl, goog.events.EventType.SUBMIT,
					   function(evt) {
						   if (this.verifyInput()) {this.logUserIn();}
						   evt.preventDefault();},
					   true, this);
	goog.events.listen(this.xmlObj,
					   goog.net.EventType.COMPLETE,
					   function() {this.displayResult();},
					   true, this);	
};

loginForm.prototype.verifyInput = function() {
	var h_success = validateField(this.handleEl, this.handleEl.value.length >= 6);
	var p_success = validateField(this.passEl, (this.passEl.value.length >= 6));
	return h_success && p_success;
};

loginForm.prototype.logUserIn = function() {
	goog.dom.$('alert-message').innerHTML = "";
	var url = '/actions/user_login.php';
    var data = 'handle=' + this.handleEl.value
	  + '&password=' + this.passEl.value;
	  this.xmlObj.send(url, 'POST', data);
};

loginForm.prototype.displayResult = function () {
	if (this.xmlObj.isSuccess()) {
		eval('var response = (' + this.xmlObj.getResponseText() + ');');
		var container = goog.dom.$dom('div');
		if (response[0].success == 'true') {
		  container.style.color = "green";
		  setTimeout("location.href = '/';", 2500);
		}
		else 
		  container.style.color = "red";
		container.innerHTML = response[0].message;
		goog.dom.appendChild(goog.dom.$('alert-message'), container);
	}
};
