window.addEvent('domready', function(){
    

	var vFirstRequired = false;
	var testBool = true;
	var emailTest = '';
	var passwordTest = '';
	
	$$('form').addEvent('submit', function(e) {
		new Event(e).stop();
		testBool = true;
		//
		$$('textarea').each( function(obj) {
			isRequired(obj);
		});
		$$('select').each( function(obj) {
			isRequired(obj);
		});
		$$('input').each( function(obj) {
			isRequired(obj);
			if( obj.hasClass('email') ){
				if( emailTest == '' ){
					emailTest = obj.value;
				}else if( emailTest != obj.value ){
					firstFocus(obj);
				}
			}
			if( obj.hasClass('password') ){
				if( passwordTest == '' ){
					passwordTest = obj.value;
				}else if( passwordTest != obj.value ){
					firstFocus(obj);
				}
			}
			/*if( obj.hasClass('creditcard') ){
				vRegEx = /^\d{8,}$/;
				if( !vRegEx.test(obj.value) ){
					firstFocus(obj);
				}
			}*/
		});
		// If all checks out send as query string
		if( testBool ){
			this.send({
				onComplete: function(response) {
					formComplete();
					$('form-hold').setStyles({ 'display': 'none' });
					$('form-complete').setStyles({ 'display': 'block' });
				}
			});
		}
	});

	var vRegEx = '';
	function getRegEx(obj){
		if( obj.hasClass( 'required' )){
			if( obj.hasClass( 'text' )){
				vRegEx = new RegExp("[^ a-zA-Z]", "g");
			}else if( obj.hasClass( 'numbers' )){
				vRegEx = new RegExp("[^0-9 ]", "g");
			}else if( obj.hasClass( 'textnumbers' )){
				vRegEx = new RegExp("[^a-zA-Z0-9 ]", "g");
			}else if( obj.hasClass( 'url' )){
                                vRegEx = new RegExp("[^a-z0-9]", "g");
			}else if( obj.hasClass( 'screenname' )){
				vRegEx = new RegExp("[^a-zA-Z0-9 _.]", "g");
			}else if( obj.hasClass( 'address' )){
				vRegEx = new RegExp("[^a-zA-Z0-9 #.@(),-/]", "g");		
			}else if( obj.hasClass( 'email' )){
				vRegEx = new RegExp("[^a-zA-Z0-9 @.-_]", "g");
			}else{
				vRegEx = new RegExp("[^a-zA-Z0-9 ]", "g");
			}
		}
	};		

	function inputAnim(obj){
		var flash = new Fx.Style(obj, 'backgroundColor', {duration:500}).start('#FCCA31','#fff');
	};	
	function isRequired(obj){
		if( obj.hasClass('required') ){
			if( obj.value && obj.value == null || obj.value == '' ){
				firstFocus(obj);
			}
		}
	};
	function firstFocus(obj){
		if( !vFirstRequired ){
			obj.focus();
			vFirstRequired = true;
		}
		inputAnim(obj);
		testBool = false;
		//alert("firstFocus");
	};
	function inputVerify(obj){
		getRegEx(obj);
		if( vRegEx.test(obj.value) && !obj.hasClass('submit') ){
			obj.value = obj.value.replace(vRegEx, '');
			inputAnim(obj);
		}
	};
	function inputBlur(obj){
		obj.setStyles({
			border: '1px solid #A3C886',
			color: '#7aa856'
		});
	};
	function inputFocus(obj){
		obj.setStyles({
			border: '1px solid #fff',
			borderBottom: '1px solid #A3C886',
			color: '#328307'
		});
	};

	$$('input').addEvent('keyup', function(){
		inputVerify(this);
	});
	$$('input').addEvent('blur', function(){
		inputBlur(this);
	});
	$$('input').addEvent('focus', function(){
		inputFocus(this);
	});

	$$('textarea').addEvent('keyup', function(){
		inputVerify(this);
	});
	$$('textarea').addEvent('blur', function(){
		inputBlur(this);
	});
	$$('textarea').addEvent('focus', function(){
		inputFocus(this);
	});

	$$('select').addEvent('keyup', function(){
		inputVerify(this);
	});
	$$('select').addEvent('blur', function(){
		inputBlur(this);
	});
	$$('select').addEvent('focus', function(){
		inputFocus(this);
	});

});
