// Please do not consider this a valid example of my ability to write Javascript.
// If you want a code sample from me, please email me: http://www.kristoferbaxter.com/contact

var showErrors = false;

Element.extend({
    validName : function() { return this.findValue().isName(); },
    validPhone : function() { return this.findValue().isPhoneNumber(); },
    validEmail : function() { return this.findValue().isEmail(); },
    validStreet : function() { return this.findValue().isStreet(); },
    validZip : function() { return this.findValue().isZip(); },
    isNotEmpty : function() { return this.findValue().isNotEmpty(); },
    isEmpty : function() { return this.findValue().isEmpty(); },
    validate : function(re) { return this.findValue().validate(re); },
    findValue : function() { 
    	if (this.getValue()) return this.getValue();
        else if (this.getText().isNotEmpty()) return this.getText();
        return "";
    }
});
String.extend({
    isName : function() {
        var re = /^[A-Za-z](['. -])?([A-Za-z](['. -'])?)+$/;
        if (this.match(re)) return true;
        return false;
    },
    isPhoneNumber : function() {
        var re = /^\(?\d{3}(?:\)|[. -])[. -]?\d{3}[-. ]\d{4}$/  
        if (this.match(re)) return true;
        return false;
    },
    isEmail : function() {
        var re =/^[A-Za-z0-9]+[\w.-]*?[A-Za-z0-9]+@[A-Za-z0-9]+[\w.-]*?\.[A-Za-z0-9]{2,5}$/;
        if (this.match(re)) return true;
        return false;
    },

    isStreet : function() {
        var re = /^\d{2,}\s\b[A-Z][\w .]*[^\W]\.?$/i;
        if (this.match(re)) return true;
        return false;
    },

    isZip : function() {
        var re = /^\d{5}(?:[-._ ]\d{4})?$/;
        if (this.match(re)) return true;
        return false;
    },
    isNotEmpty : function() {
        var re = /\w+/
        if (this.match(re)) return true;
        return false;
    },
    isEmpty : function() {
        var re = /\w+/
        if (this.match(re)) return false;
        return true;
    },
    validate : function(re) {
        if (this.match(re)) return true;
        return false;
    }
});

window.addEvent("domready", function() {
	if ($$("#main p a")[0]) {
		var emailaddress = new Ajax("/components/emaildecode.php", { method: "get", data: "key=" + $$("#main p a")[0].getProperty("href").replace("mailto:",""), onComplete: function() {
			$$("#main p a")[0].setProperty("href", "mailto:" + this.response.text);
		}}).request();
	}
	$$('form.email .validate, form.email label').each(function(el) {
		if (el.getParent().getElement('div')) { showErrors = true; }
		el.addEvent('click', function(ev) {
			if (el.getParent().getElement('div')) { 
				if (el.getParent().getElement('div').getStyle('visibility') != 'hidden') var fx = new Fx.Style(el.getParent().getElement('div'), 'opacity', {duration:500}).start(1,0);
			}
		});
		el.addEvent('focus', function(ev) {
			if (el.getParent().getElement('div')) { 
				if (el.getParent().getElement('div').getStyle('visibility') != 'hidden') var fx = new Fx.Style(el.getParent().getElement('div'), 'opacity', {duration:500}).start(1,0);
			}
		});
		if (el.getTag() != 'label') {
			el.addEvent('blur', function(ev2) {
				status = validate(el, showErrors);
			});
		}
	});
	if ($('submitter')) { 
		$('submitter').addEvent('click', function(e) {
			var status = true;
			showErrors = true;
			status = validateAll($('submitter').getParent().getParent().getElements('.validate'));
			if (status) { $('submitter').getParent().getParent().submit(); }
			else { new Event(e).stop(); }
		});
	}
	if ($('yourname')) { $('yourname').focus(); }
});

validate = function(el, showErrors) {
	var message = "";
	var status = "";
	if (el.hasClass('email')) { message = "Enter a valid email address please!"; status = el.validEmail(); }
	else if (el.hasClass('personname')) { message = "Enter your name please!"; status = el.validName(); }
	else if (el.getTag() == "textarea") { message = "Give me something to work with here!"; status = el.isNotEmpty(); }
	if (!el.getParent().getElement('div')) { 
		if (showErrors) {
			var newDiv = new Element('div', { 'class': 'errormessage' } ).setText(message).injectInside(el.getParent()); 
		}
		else {
			var newDiv = new Element('div', { 'class': 'errormessage' } ).setText(message).setOpacity(0).injectInside(el.getParent()); 
		}
	} 
	if (!status) { 
		if (showErrors) { var fx = new Fx.Style(el.getParent().getElement('div'), 'opacity', {duration:500}).start(0,1); }
		return false;
	}
	else {
		return true;
	}
}

validateAll = function(array) {
	var errorarray = [];
	var status = "";
	array.each(function(el,index) {
		status = validate(el,false);
		if (!status) errorarray.push(el.getParent().getElement('div')); //array[index] = el.getParent().getElement('div');
	});
	var multipleErrors = new Fx.Elements(errorarray);
	switch(errorarray.length) {
		case 0:
			break;
		case 1:
			multipleErrors.start({ '0': { 'opacity': [0,1] } });
			break;
		case 2:
			multipleErrors.start({ '0': { 'opacity': [0,1] }, '1': { 'opacity': [0,1] } });
			break;
		case 3:
			multipleErrors.start({ '0': { 'opacity': [0,1] }, '1': { 'opacity': [0,1] }, '2': { 'opacity': [0,1] } });
			break;
	}
	if (errorarray.length > 0) { return false; }
	else return true;
}