/**
 * @import com.emaildatasource.eds.punt.client.taglib.tagjsp.login_jsp.BUTTON login.button
 * @import com.emaildatasource.eds.punt.client.taglib.tagjsp.login_jsp.PASSWORD login.password
 * @import com.emaildatasource.eds.punt.client.taglib.tagjsp.login_jsp.USER login.user
 * @import com.emaildatasource.eds.punt.client.taglib.tagjsp.login_jsp.COMPLIANT login.compliant
 * @import com.emaildatasource.eds.punt.client.taglib.tagjsp.login_jsp.FORM login.form
 * @import com.emaildatasource.eds.punt.client.taglib.tagjsp.login_jsp.ERROR login.error
 */
eds.punt.client.widget.Login = function(domIds) {

	var keys = eds.punt.client.KeyChain.login;

	var button = null;

	var that = this;

	if ( arguments.length != 1 ) {
		throw "login wrong number of elements";
	}

	eds.webapp.widget.Widget.call(this, domIds);

	this.init = function() {
		setBrowserCompliance();
		focusForm();
		if ( !cookiesEnabled() ) {
			that.disableEl(keys.user);
			that.disableEl(keys.password);
			that.setElHtml(keys.error,
				'<font color=red>EmailAnalyst requires cookies. Please enable cookie and refresh your browser.</font>');
		} else {
			that.enableEl(keys.user);
			that.enableEl(keys.password);
		}
	}

	this.destroy = function() { }

	this.addButton = function(b) {
		button = b;
		this.toggleButton();
	}

	this.acceptButtonClickedEvent = function() {
		var form = this.getEl(keys.form);
		form.submit();
	}

	function focusForm() {
		var passwordField;
		var userField;
		
		userField = that.getEl(keys.user);
		passwordField = that.getEl(keys.password);
		
		if( !userField.value ) {
			userField.focus();
			
		} else {
			passwordField.focus();	

		}
	}

	function setBrowserCompliance() {
		that.setElValue(keys.compliant, true);
	}

	function cookiesEnabled() {
		var NAME = 'punttest';
		var VAL = 47;
		var enabled = false;
		createCookie(NAME,47,1);
		if ( VAL == readCookie(NAME) ) {
			enabled = true;
		}
		eraseCookie(NAME);
		return enabled;
	}

	function createCookie(name,value,days) {
		var expires = "";

		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') {
				c = c.substring(1,c.length);
			}
			if (c.indexOf(nameEQ) == 0) {
				return c.substring(nameEQ.length,c.length);
			}
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name,"",-1);
	}

	this.toggleButtonOrSubmit = function(e) {

		e = e || window.event;

		if (e && e.keyCode == 13) {
			button.click();

		} else {
			this.toggleButton();

		}

		return true;
	}

	this.toggleButton = function() {

		var user = null;
		var password = null;

		if ( !button ) {
			return;
		}
		
		user = this.getElValue(keys.user);
		password = this.getElValue(keys.password);
		
		if( user && password ) {

			button.enable();

		} else {

			button.disable();
		}
	}

	this.toString = function() {
		return '[ login object ]';
	}
}

eds.punt.client.widget.Login.prototype = new eds.webapp.widget.Widget(null);
