function validname(validator, id, fieldname, notrequired){
	if(!notrequired){
		validator.register(id,{type: "required", re: /[^.*]/, msg: "Please enter your "+fieldname+"."});
	}
	validator.register(id,{type: "alpha", re: /^[0-9a-z /'._-]*$/i, msg: "The "+fieldname+" field can only contain letters and numbers."});
}

function validUsername(validator, id, message){
	if(!message){
		message = "Please enter a valid Username.";
	}
	
    validator.register(id,{type: "required", re: /^[a-z0-9A-Z]{4,17}$/i, msg: "Please choose a username at least 4 characters long.<br/>Your username can only contain letters and numbers."});
	//validator.register(id,{type: "required", re: /[^.*]/, msg: "Please enter your Username."});
	//validator.register(id,{type: "username", re: /^[a-z0-9@._-]{1,}$/i, msg: "Please enter a valid Username."});
}

function validemail(validator, id, message){
	if(!message){
		message = "Please enter a valid Email Address.";
	}
	validator.register(id,{type: "email", re: /^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i, msg: message});
}

function validpassword(validator, id, confirmid){
	validator.register(id,{type: "password", re: /^([^ ]{6,100})?$/i, msg: "Your password must be at least 6 characters long, and without space."});
	validator.register(id,{type: "nomatchfield", idFields: ["firstName","lastName","emailAddress"], msg: "Your password cannot be the same as your name or email address."});
	validator.register(id,{type: "nomatchword", words: ["password"], msg: "Your password cannot be 'password'."});
	if(confirmid){
		validator.register(confirmid, {type: "confirm", idField: id, msg: "Please enter your password again to confirm it."});
	}
}

function validPhone(validator, id, fieldname){
	validator.register(id,{type: "phone", re: /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,20})+$/, msg: "Please enter an valid "+fieldname+"."});
}

function validCellphone(validator, id, fieldname){
	validator.register(id,{type: "cellphone", re: /^[0-9]*$/, msg: "Please enter an valid "+fieldname+"."});
}

function validrequired(validator, id, fieldname){
	validator.register(id,{type: "required", re: /[^.*]/, msg: "Please enter your "+fieldname+"."});
}

function validaddress(validator, id, fieldname, notrequired){
	if(!notrequired){
		validator.register(id,{type: "required", re: /[^.*]/, msg: "Please enter your "+fieldname+"."});
	}
	validator.register(id,{type: "address", re: /^[a-z0-9 ,#@\&()._-]*$/i, msg: "Please enter a valid "+fieldname+"."});
}

function validCity(validator, id, fieldname, notrequired){
	if(!notrequired){
		validator.register(id,{type: "required", re: /[^.*]/, msg: "Please enter your "+fieldname+"."});
	}
}

function validcardname(validator, id){
	validator.register(id,{type: "cardname", re: /^[a-z '._-]+$/i,  msg: "Please enter the name of the card holder, exactly as it appears on the card."});
}

function validcardnumber(validator, id){
	validator.register(id,{type: "onlynumber", re: /^\d{15,16}$/, msg: "Please enter the 15 or 16 digit card number."});
}

function validcardnumberhead(validator, id){
	validator.register(id,{type: "onlynumber", re: /^\d{12}$/, msg: "Please enter the first 12 digit card number."});
}

function validCVN(validator, id){
	validator.register(id,{type: "CVN", re: /^\d{3,4}$/, msg: "Please enter the 3 or 4 CVN number from the back of your card."});
}

function validMoney(validator, id){
	validator.register(id,{type: "Money", re: /^[\$]{0,1}\d{1,7}$/, msg: "We do not accept donations of greater than $9999999 on the website"});
	validator.register(id,{type: "Lowest", lowest: 5, msg: "The minimum donation is $5 per month."});
}

function validcomment(validator, id){
	validator.register(id,{type: "comment", re: /[^.*]/, msg: "Please enter your comment."});
}

function validExtraChannel(validator, id){
	validator.register(id,{type: "required", re: /[^.*]/, msg:"Please tell us how you hear about us."});
}

function init(validator){
	if(!validator){
		return;
	}
	validUsername(validator,"username", "Username");
	validname(validator,"firstName", "First Name");
	validname(validator,"lastName", "Last Name");
	validemail(validator, "emailAddress");
	validemail(validator, "currentemailAddress", "Please enter the Email Address that you used to register.");
	validrequired(validator,"currentpassword", "password");
	validpassword(validator,"inputpassword", "confirmPassword");
	//validnumber(validator,"postcode", "Postcode");
	validPhone(validator,"phone", "Phone Number");
	validCellphone(validator,"cellphone", "Number");
	//validname(validator,"country", "Country");
	//validname(validator,"state", "State");
	//validname(validator,"suburb", "Suburb", true);
	validCity(validator,"city", "City Name");
	validaddress(validator,"streetAddress1", "Street Address");
	validaddress(validator,"streetAddress2", "Addtional Street Address", true);
	validname(validator,"ownerName", "Daffodil's Owner name");
	validcardname(validator,"nameoncard", "Name on Card");
	validrequired(validator,"story", " a description or story. This will be shown in the Daffodil field for other visitors to read");
	validrequired(validator,"monthlydonate", "Monthly Donation Amount");
	validrequired(validator,"cardtype", "Card Type");
	validrequired(validator,"expirymonth", "Expiry month");
	validrequired(validator,"expiryyear", "Expiry year");
	validrequired(validator,"primarycolour", "Primary colour");
	validrequired(validator,"secondarycolour", "Secondary colour");
	validcardnumberhead(validator, "cardnumberhead");
	validcardnumber(validator, "cardnumber");
	validCVN(validator, "cvn");
	validMoney(validator, "monthlydonatemanual");
	validcomment(validator, "comment");
	validExtraChannel(validator, "extraChannel");

	new cascading("country",["Australia"] ,["statecontainer","suburbcontainer","citycontainer"]);
	new cascading("monthlydonate",["Other..."] ,["monthlydonatemanualcontainer"]);
}

//Only show extra element when value match
var cascading = new Class({
	initialize: function(ele, values, extraelements){
		if(!$(ele)){
			return false;
		}
		this.ele = $(ele);
		this.values = values;
		this.extraelements = extraelements;
		this.ele.addEvent("change", function() {
			this._validate();
		}.bind(this));
		this._validate();
		return true;
	},

	_validate: function(){
		var newvalue = this.ele.getValue();
		var match = false;
		this.values.each(function(value){
			if(value.toLowerCase() == newvalue.toLowerCase()){
				match = true;
			}
		});

		if(match){
			this.extraelements.each(function(extra){
				if ($(extra)){
					$(extra).setStyles({"display": ""});
				}
			});
		}else{
			this.extraelements.each(function(extra){
				if ($(extra)){
					$(extra).setStyles({"display": "none"});
				}
			});
		}
	}
});

function addEventsForExtraChannel(validator){
	var extraChannelSpan = $('extraChannelSpan');
	var extraChannel = $('extraChannel');
	if (extraChannel){
		extraChannel.addEvent('change',function(e){
			if (extraChannel.options[extraChannel.selectedIndex].value == 'Other'){
				extraChannelSpan.setStyle('display','inline');
				if ($('extraChannelrequired_msg')){
					validator._msgRemove($('extraChannel'),{type: "required", re: /[^.*]/, msg:"Please tell us how you hear about us."});
				}
			}else {
				extraChannelSpan.setStyle('display','none');
			}
		});
	}
}

window.addEvent("domready", function() {  
	var purchaseformValidator = new fValidator("form1",{},"theMoneyStock");
	addEventsForExtraChannel(purchaseformValidator);
	init(purchaseformValidator);
}); 

function moveerror(){
	if(document.getElementById('error'))
	{
		document.getElementById('error').innerHTML='';
	}
}
