// JavaScript Document
var registration = Class.create();
registration.prototype = {
	
	initialize: function(options) {
		this.emailvalidated = false;
		this.passwordvalidated = false;
		this.setoptions(options);
	},
	
	setoptions: function(options) {
		this.options = {
			ajaxurl: '/myhamilton/register/ajaxcalls/ajaxcalls.cfm',
			requiredtextcolor: '#BF9260',
			emailimgid: 'validateemail',
			passwordimgid: 'validatepassword',
			checkedimgpath: '/myhamilton/register/images/checked.gif',
			notcheckedimgpath: '/myhamilton/register/images/not-checked.gif'
		}
		Object.extend(this.options, options || {});
	},
	
	getElemRequired: function(elem){
		if (typeof elem != 'undefined')
			this.elem = elem;
		var msgelem = document.getElementsByClassName('message', this.elem.parentNode);
		if (msgelem.length == 1){
			return msgelem[0];
		}
	}, 
	
	updateRequired: function(elem){
		this.elem = elem;
		if (!validate.inputisblank(this.elem))
			this.setRequiredColor('#929FAB');
		else
			this.setRequiredColor(this.options.requiredtextcolor);
		
	},
	
	setRequiredColor: function(color){
		var txtelem = this.getElemRequired();
		if (txtelem){
			txtelem.style.color = color;
		}
	},
	
	setRequiredText: function(text){
		if (typeof text != 'undefined'){
			var txtelem = this.getElemRequired();
			txtelem.innerHTML = '<em>'+text+'</em>';
		}
	},
	
	setEmail: function(elem){
		this.email = $F(elem);
	},
	
	setPassword: function(elem){
		this.elem = elem;
		if ($F(elem).length < 6)
			this.setRequiredText('* Must be at least 6 characters');
		this.password = $F(elem);
	},
	
	validateEmail: function(elem){
		if ($F(elem) == this.email){
			this.validateUniqueEmail();
		}else
			this.setEmailBad();
	},
	
	validateUniqueEmail: function(){
		//make sure emailerrormsg is not visisble
		$('emailerrormsg').style.display = 'none';
		//make ajax call to ensure that email is not already used in LDAP guests.hamilton.edu
		if (!this.currentlyvalidatingemail){
			this.currentlyvalidatingemail = true;
			this.setEmailLoading(true);
			var pars = 'action=validateUniqueEmail&email='+this.email;
			var MyAjax = new Ajax.Request(this.options.ajaxurl, {method: 'post', parameters: pars, onSuccess: function(t){
				if (t.responseText.trim() == 1){
					//email entered is not already in LDAP
					reg.setEmailLoading(false);
					reg.setEmailOK();
				}else{
					//set message
					$('emailerrormsg').style.display = 'inline';
					$('emailerrormsg').innerHTML = '<em>This e-mail address is already in use.</em>  Please contact the <a href="mailto:helpdesk@hamilton.edu">Help Desk</a> if you have forgotten your password or need help logging in.</em>';
					new Effect.Highlight('emailerrormsg',{endcolor: '#f5eede', afterFinish:function(e){$('emailvalidation').value = ''; $('email').focus(); $('email').value = '';}});
					
					reg.setEmailLoading(false);
					self.setTimeout(function(){ new Effect.Fade('emailerrormsg'); }, 10000);
				}
				reg.currentlyvalidatingemail = false;
			}});
		}
	},
	
	setEmailLoading: function(loadimage){
		if (loadimage == true)
			Element.show('emailvalidationind');
		else if (loadimage == false)
			new Effect.Fade('emailvalidationind');
		if ((loadimage == null) || (typeof(loadimage) == "undefined"))
			Element.show('emailvalidationind');
	},
	
	validatePassword: function(elem){
		if ($F(elem) == this.password)
			if ($F(elem).length >= 6){
				this.setPasswordOK();
			}else{
				this.setPasswordBad();
			}
		else
			this.setPasswordBad();
	},
	
	setEmailOK: function(){
		var imgelem = $(this.options.emailimgid);
		imgelem.src = this.options.checkedimgpath;
		this.emailvalidated = true;
	},
	
	setEmailBad: function(){
		var imgelem = $(this.options.emailimgid);
		imgelem.src = this.options.notcheckedimgpath;
		this.emailvalidated = false;
	},
	
	setPasswordOK: function(){
		var imgelem = $(this.options.passwordimgid);
		imgelem.src = this.options.checkedimgpath;
		this.passwordvalidated = true;
	},
	
	setPasswordBad: function(){
		var imgelem = $(this.options.passwordimgid);
		imgelem.src = this.options.notcheckedimgpath;
		this.passwordvalidated = false;
	},
	
	validatestep2: function(){
		if (this.emailvalidated && this.passwordvalidated){
			return true;
		}else{
			if (!this.emailvalidated){
				window.alert('The Email addresses you entered did not match.\n\nPlease re-enter your email address again.');
				this.email = $F('email');
				$('emailvalidation').value = '';
				$('emailvalidation').focus();
				return false;
			}
			if (!this.passwordvalidated){
				if ($F('password').length < 6){
					this.setRequiredText('Password must be at least 6 characters');
					new Effect.Highlight(this.getElemRequired($('password')),{endcolor:'#F5EEDE'});
				}else{
					window.alert('The passwords you entered did not match.\n\nPlease re-enter your password again.');
				}
				this.password = $F('email');
				$('passwordvalidation').value = '';
				$('passwordvalidation').focus();
				return false;
			}
		}
	},
	
	validatereset: function(){
		if (this.passwordvalidated){
			$('resetform').submit();
		}else{
			if (!this.passwordvalidated){
				if ($F('password').length < 6){
					this.setRequiredText('* Must be at least 6 characters');
					new Effect.Highlight(this.getElemRequired($('password')),{endcolor:'#F5EEDE'});
				}else{
					window.alert('The passwords you entered did not match.\n\nPlease re-enter your password again.');
				}
				$('passwordvalidation').value = '';
				$('passwordvalidation').focus();
				return false;
			}
		}
	},
	
	addClassYear: function(ctr){
		var nextcount = 1 + Number(ctr);
		var prevcount = Number(ctr) - 1;
		var count = ctr;
		var newHTML = "<label for=\"classyr"+count+"\">Child's Class Year:</label>" +
							"<select name=\"classyr"+count+"\" id=\"classyr"+count+"\" style=\"width:169px;\">" +
								"<option selected value=\"\">-none-</option>";
		var today = new Date();
		for (var i=today.getFullYear()+4; i>=1920; i--){
			newHTML += "<option value=\""+i+"\">"+i+"</option>";
		}
		newHTML += "</select> <span class=\"message\" id=\"addclassyearanchor"+count+"\">[<em><a href=\"javascript:void(0)\" onclick=\"reg.addClassYear("+nextcount+");\">Add another child's class year</a></em>]</span>";
		//increment classyrcounter element
		$('classyrcounter').value = count;
		$('addclassyearblock'+count).innerHTML = newHTML;
		$('addclassyearanchor'+prevcount).style.display = 'none';
		new Insertion.After('addclassyearblock'+count, '<div id="addclassyearblock'+nextcount+'" style="display:none;"></div>');
		new Effect.Appear('addclassyearblock'+count);
	}
}

validate = {
	inputisblank: function(e){
		var s = $F(e)
		for (var i=0; i<s.length; i++){
			var c = s.charAt(i);
			if ((c != ' ') && (c != '\n') && (c != '') && (c != null)) return false;
		}
		return true;
	}
}

setEmailandPassword = function(){
	if ($F('email') != '')
		reg.setEmail('email');
	if ($F('password') != '')
		reg.setPassword('password');
}

// Removes whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and ending whitespaces
String.prototype.trim = function(){
	var s = this.toString();
	return LTrim(RTrim(s));
}
