function ajaxFormHandler( target, error_target, ajax_target ){
	
	var method = $("#" + target).attr('method');
	var destination = $("#" + target).attr('action');
	var formInputs = $( '#' + target + ' :input');
	var formData = '';
	var formDataR = new Array();
	var i = 0;
	var error = 1;
	
	formInputs.each(function(){ 
	
		if($(this).attr('type') == 'checkbox'){
			if($('#' + $(this).attr('id') + ':checked').val() == 1){
				formDataR[i++] = $(this).attr('name') + '=' + escape($(this).attr('value'));
			}
		} 
		else {
			formDataR[i++] = $(this).attr('name') + '=' + escape($(this).attr('value'));
		}
	
		if($(this).attr('data')){
			if( input_validate ( $(this).attr('id') ) == 0){ error = 0; }
		}
	});
	
	if( error == 1 ){
		
		formData = formDataR.join('&');
		
		$.ajax({
			type: method,
			url: destination,
			data: formData,
			  beforeSend: function(){
				$("#reply_submit").hide();
				$("#reply_button").show();
			  },
			success: function(msg){
				$("#reply_button").hide();
				$("#reply_submit").show();
				$("#" + ajax_target).html(msg);
				reset_form(formInputs);
			}
		});
	}
	else if ( error == 0 )
	{
		$("#" + error_target).html("Me thinks you missed something. You best double check your work.");
	}
}

function reset_form( inputs ){

	var remember;
	
	remember = ($("#reply_save").attr('checked'))?true:false;
	
	if(remember){
		inputs.each(function(){ 
			if($(this).attr('data')){
				$(this).removeClass("reply_error");
				$(this).removeClass("reply_pass");
			}
		});
		$('#reply_comment').attr('value', '');
	} else {
		inputs.each(function(){ 
			if($(this).attr('data')){
				if($(this).attr('type') == 'checkbox'){
					$(this).attr('checked', false);
				} else {
					$(this).attr('value',''); 
				}
				$(this).removeClass("reply_error");
				$(this).removeClass("reply_pass");
			}
		});
	}
}

function input_validate ( input_id ){

	var value = $("#" + input_id).attr('value');
	var requirements = $("#" + input_id).attr('data');
	var verdict = 2;
	
	// check for required
	if( requirements.indexOf('required') > -1 ){ verdict = validate_required ( input_id, value ); }
	
	// check for email
	if( requirements.indexOf('email') > -1 ){ verdict = validate_email ( input_id, value ); }
	
	// check for url
	if( requirements.indexOf('url') > -1 ){ verdict = validate_url ( input_id, value ); }
	
	if( verdict == 0 ){
		$("#" + input_id).removeClass("reply_pass");
		$("#" + input_id).addClass("reply_error");
	}
	else if( verdict == 1 ) {
		$("#" + input_id).removeClass("reply_error");
		$("#" + input_id).addClass("reply_pass");
	}
	else {
		$("#" + input_id).removeClass("reply_error");
		$("#" + input_id).removeClass("reply_pass");
	}
	
	return verdict;
}

function validate_required ( input_id, value ){
	if( value == '' ){ return 0; }
	else if( value != '' ){ return 1; }
}

function validate_email ( input_id, value ){

	var emailRegex= new RegExp(/^([a-zA-Z0-9_]{3,})(((\.|\-|\_)[a-zA-Z0-9]{2,})+)?@([a-z]{2,})(\-[a-z0-9]{2,})?(\.[a-z]{2,})+$/);

	if( !emailRegex.test( value ) ){ return 0; }
	else if( emailRegex.test( value ) ){ return 1; }
}

function validate_url ( input_id, value ){

	if( value == '' || value == null ){ return 2; }
	
	var urlRegex= new RegExp(/http:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/);

	if( !urlRegex.test( value ) ){ return 0; }
	else if( urlRegex.test( value ) ){ return 1; }
}

function formHandler( form_id ){

	var method = $("#" + form_id).attr('method');
	
	if( method == "get" ){
	
		var destination = $("#" + form_id).attr('action');
		var $inputs = $( '#' + form_id + ' :input');
		
		$inputs.each(function(){ 
			if( $(this).attr('type') != "image" ){
				destination += "/" + $(this).attr('name') + ":" + $(this).attr('value');
			}
		});
		
		window.location = destination;
	}
}



