// JavaScript Document

function reqfield (parm, parm1) {
	if (parm.length >1){
		document.getElementById(parm1).className = 'reqFilled';
	} else {
		document.getElementById(parm1).className = 'req';
	}
}

function testEmail (parm) {
	var reEmail = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;
	return reEmail.test(parm);
}

function checkForm(){
	fields = new Array('firstname', 'lastname', 'city', 'state', 'email', 'email2', 'username', 'pass')
	var missingitems = '';
	for (i = 0; i<fields.length;i++){
		var thefield = document.getElementById(fields[i])
		fieldname = fields[i].replace('name', ' name');
		if (thefield.value.length < 1){
			thefield.className = 'req'
			missingitems += '  -- ' + fieldname +' required\n'
		} else {
			if (fieldname == 'email'){
				if (!testEmail(thefield.value)){
					missingitems += '  -- valid email address required\n'
					thefield.className = 'req'
				} else if (document.getElementById('email').value.toLowerCase() != document.getElementById('email2').value.toLowerCase()){
					missingitems += '  -- the email addresses do not match\n'
					document.getElementById('email2').className = 'req'						
				} 
			}
		}
	}
	
	if(!$("human").checked && !$("robot").checked) {
		missingitems += '  -- please describe yourself as human or dirty spamming robot\n';
	}
	
	if (missingitems == ''){
		return true;
	} else {
		alert('ERROR!\nYour submission contained the following errors:\n\n' + missingitems + '\nPlease correct and resubmit');
		return false;
	}
}

Event.observe(window,"load",function(){
	
		var p = new Element("p");
		var label = new Element("label",{"className":"fullwidth"}).update("How would you describe yourself:");
		var input1 = new Element("input",{"type":"radio","name":"whoiam","value":"human","id":"human"});
		var label1 = new Element("label",{"className":"radiolabel","for":"human"}).update("I'm a human being");
		var input2 = new Element("input",{"type":"radio","name":"whoiam","value":"robot","id":"robot"});
		var label2 = new Element("label",{"className":"radiolabel","for":"robot"}).update("I'm a dirty, low-down, spamming web-bot").observe("click",alertRobot);
		var br = new Element("br");
		
		p.insert(label);
		p.insert(input1);
		p.insert(label1);
		p.insert(br);
		p.insert(input2);
		p.insert(label2);

		$("passp").insert({"after":p});

});

function alertRobot(){
	alert("Nice try, robot.  Are you sure you want to identify as a robot?  Your registration ends here, if so.");
}
