

function RegisterForm() {

	this.formid = "register_form";
	this.errordivid = "register_error";	
	this.helpdiv = null;
	this.form = null;
	this.ruleswin = null;
	this.fields = new Array('firstname', 'lastname', 'email', 'email2', 'password', 'password2', 'domain', 'name', 'month');
	//this.fields = new Array('firstname', 'lastname', 'email', 'email2', 'password', 'password2', 'domain', 'name');
	
	/*our constructor */
	{	
		this.form = document.getElementById(this.formid);
		//alert('got form ' + this.form);
	}
	
	this.notUnique = function(id) {
		document.getElementById(id).innerHTML = '*';
		document.getElementById(id).style.color = '#f99400';
	}

	this.notValid = function(id) {
		document.getElementById(id).style.color = '*';
		document.getElementById(id).innerHTML = '#f99400';
	}
			
	this.processForm = function() {
					
		var valid = true;
		var errordiv = document.getElementById(this.errordivid);		
		//errordiv.style.display = "none";	
		
		document.getElementById('accept_prompt').style.color = '#f99400';
		document.getElementById('accept_prompt').innderHTML = '*';
		
		for(var i = 0; i < this.fields.length; i++) {
			//alert(this.fields[i]);
			var input = document.getElementById(this.fields[i]);
			var promptid =  this.fields[i] + '_prompt';				
			var prompt = document.getElementById(promptid)			
			prompt.innerHTML = '';
			if(! input.value || input.value == '') {;
				prompt.innerHTML = '*';
				prompt.style.color = '#f99400';
				valid = false;
			}
		}
		if(valid == false) {			
		
			errordiv.style.display = "block";
			errordiv.innerHTML = "Please complete the highlighted fields";
			
		} else {
			if(document.getElementById('email').value != document.getElementById('email2').value) {
				document.getElementById('email_prompt').style.color = '#f99400';
				document.getElementById('email_prompt').innerHTML = '*';
				document.getElementById('email2_prompt').style.color = '#f99400';				
				document.getElementById('email2_prompt').innerHTML = '*';
				valid = false;
				errordiv.innerHTML = "Please make sure that your email addresses match";
				errordiv.style.display = "block";				
			} else if(document.getElementById('password').value != document.getElementById('password2').value) {
				document.getElementById('password_prompt').style.color = '#f99400';
				document.getElementById('password_prompt').innerHTML = '*';				
				document.getElementById('password2_prompt').style.color = '#f99400';			
				document.getElementById('password2_prompt').innerHTML = '*';
				valid = false;
				errordiv.innerHTML = "Please make sure that your passwords match";
				errordiv.style.display = "block";				
			} else if (! this.validateEmail(document.getElementById('email').value) ) {
				document.getElementById('email_prompt').style.color = '#f99400';
				document.getElementById('email2_prompt').style.color = '#f99400';				
				document.getElementById('email_prompt').innerHTML = '*';
				document.getElementById('email2_prompt').innerHTML = '*';								
				valid = false;
				errordiv.innerHTML = "Please enter a valid email address";
				errordiv.style.display = "block";	
			} else if (! this.validateDomain(document.getElementById('domain').value) ) {
				document.getElementById('domain_prompt').style.color = '#f99400';
				document.getElementById('domain_prompt').innerHTML = '*';
				valid = false;
				errordiv.innerHTML = "A sub domain can only contain alphanumeric characters and dashes (a-z, 0-1, '-')";
				errordiv.style.display = "block";								
			} else if ( document.getElementById('password').value.length < 5 ) {
				document.getElementById('password_prompt').style.color = '#f99400';
				document.getElementById('password_prompt').innerHTML = '*';
				document.getElementById('password2_prompt').style.color = '#f99400';
				document.getElementById('password2_prompt').innerHTML = '*';			
				valid = false;
				errordiv.innerHTML = "Passwords must be at least 5 characters";
				errordiv.style.display = "block";							
				document.getElementById('password').value = '';
				document.getElementById('password2').value = '';												
			} else if (document.getElementById('accept').checked == false) {
				errordiv.innerHTML = "You must accept the contest rules to enter.";
				document.getElementById('accept_prompt').style.color = '#f99400';
				document.getElementById('accept_prompt').innerHTML = '*';
				valid = false;
				errordiv.style.display = "block";	
			}
		}
		if(valid == true) {
			this.form.submit();
		} else {
			if(this.helpdiv) {
				this.closeHelp();
			}
		}
	}
		
	this.help = function(id) {
		if(this.helpdiv) {
			this.closeHelp();
		}
		var errordiv = document.getElementById(this.errordivid);		
		errordiv.style.display = "none";		
		this.helpdiv = document.getElementById(id);
		this.helpdiv.style.display = 'block';
		//window.scroll(0,0);
	}
	
	this.closeHelp = function() {
		this.helpdiv.style.display = 'none';
		this.helpdiv = null;		
	}
	
	this.validateEmail = function(email) {
		isvalid = false;
		var emailregex  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		//if (filter.test(email)) {
		if(email.match(emailregex)) {
			isvalid = true;
		}
		return isvalid;
	}

	this.validateDomain = function(domain) {
		isvalid = false;
		var domainregex  = /^([a-zA-Z0-9\-])+$/;
		//if (filter.test(email)) {
		if(domain.match(domainregex)) {
			isvalid = true;
		}
		return isvalid;
	}
	
	
	this.rules = function() {
		if(this.ruleswin && ! this.ruleswin.closed) {
			this.ruleswin.close();
		}
		this.ruleswin=window.open('/rules.html','rules','height=640,width=480,scrollbars=yes');
	}
	
	this.closeRules = function() {
		if(this.ruleswin && ! this.ruleswin.closed) {
			this.ruleswin.close();
		}	
	} 
}