var jqf = jQuery.noConflict();
jqf(document).ready(function(){

	// submit the form//
	jqf('#communityForm').submit(function() {
		var errorString = false;

		// validation //
		if (typeof(jqf('#c_fname').attr('value')) == 'undefined' || jqf('#c_fname').attr('value') == '') {
			errorString = true;
			jqf('label[for=c_fname]').addClass('error');
			jqf('#c_fname').addClass('error');
			if (errorString == false) {
				jqf('#c_fname').focus();
			}
		}
		else {
			jqf('#c_fname').removeClass('error');
			jqf('label[for=c_fname]').removeClass('error');
		}

		email=/^.+@.+\..{2,6}$/
		if (typeof(jqf('#c_email').attr('value')) == 'undefined' || jqf('#c_email').attr('value') == '' || email.test(jqf('#c_email').attr('value')) == false) {
			if (errorString == false) {
				jqf('#c_email').focus();
			}
			errorString = true;
			jqf('#c_email').addClass('error');
			jqf('label[for=c_email]').addClass('error');
		}
		else {
			jqf('label[for=c_email]').removeClass('error');
			jqf('#c_email').removeClass('error');
		}

		if (typeof(jqf('#c_comment').attr('value')) == 'undefined' || jqf('#c_comment').attr('value') == '') {
			if (errorString == false) {
				jqf('#c_comment').focus();
			}
			errorString = true;
			jqf('label[for=c_comment]').addClass('error');
			jqf('#c_comment').addClass('error');
		}
		else {
			jqf('#c_comment').removeClass('error');
			jqf('label[for=c_comment]').removeClass('error');
		}

		if (errorString == true) {
			if (jqf('#communityFormBox h4.sent').length > 0) {
				jqf('#communityFormBox h4.sent').hide();
			}
			if (jqf('#communityFormBox h4.error').length == 0) {
				jqf('#communityFormBox').prepend('<h4 class="error">Please, fill out highlighted fields and try again.</h4>');
			}
			var destination = jqf('label.error').offset().top;
			jqf('html').animate({scrollTop: destination});
			return false;
		}

		// get radio inputs //
		var c_corporate = '';
		jqf('input[name=c_corporate]').each(function() {
			if (jqf(this).attr('checked')) {
				c_corporate = jqf(this).attr('value');
			}
		});
		var c_personal = '';
		jqf('input[name=c_personal]').each(function() {
			if (jqf(this).attr('checked')) {
				c_personal = jqf(this).attr('value');
			}
		});
		var c_funding = '';
		jqf('input[name=c_funding]').each(function() {
			if (jqf(this).attr('checked')) {
				c_funding = jqf(this).attr('value');
			}
		});

		//get data //
		queryString = 'c_fname=' + jqf('#c_fname').attr('value') + '&'
			+ 'c_jobtitle=' + jqf('#c_jobtitle').attr('value') + '&'
			+ 'c_division=' + jqf('#c_division').attr('value') + '&'
			+ 'c_surname=' + jqf('#c_surname').attr('value') + '&'
			+ 'c_location=' + jqf('#c_location').attr('value') + '&'
			+ 'c_hours=' + jqf('#c_hours').attr('value') + '&'
			+ 'c_day=' + jqf('#c_day').attr('value') + '&'
			+ 'c_month=' + jqf('#c_month').attr('value') + '&'
			+ 'c_year=' + jqf('#c_year').attr('value') + '&'
			+ 'c_email=' + jqf('#c_email').attr('value') + '&'
			+ 'c_donation=' + jqf('#c_donation').attr('value') + '&'
			+ 'c_corporate=' + c_corporate + '&'
			+ 'c_funding=' + c_funding + '&'
			+ 'c_personal=' + c_personal + '&'
			+ 'c_comment=' + jqf('#c_comment').attr('value');

		// send form //
		jqf.ajax({
			type: 'POST',
			url: '/ajax/communityForm.ajax.php?js=1',
			data: queryString,
			success: function(msg) {
				if (msg == 'success') {
					if (jqf('#communityFormBox h4.error').length != 0) {
						jqf('#communityFormBox h4.error').hide();
					}

					if (jqf('#communityFormBox h4.sent').length == 0) {
						jqf('#communityFormBox').prepend('<h4 class="sent">Message sent successfully.</h4>');
					}
					jqf('#c_fname').attr('value', '');
					jqf('#c_jobtitle').attr('value', '');
					jqf('#c_division').attr('value', '');
					jqf('#c_surname').attr('value', '');
					jqf('#c_location').attr('value', '');
					jqf('#c_hours').attr('value', '');
					jqf('#c_day').attr('value', '');
					jqf('#c_month').attr('value', '');
					jqf('#c_comment').attr('value', '');
					jqf('#c_year').attr('value', '');
					jqf('#c_email').attr('value', '');
					jqf('#c_donation').attr('value', '');
					jqf('#c_comment').attr('value', '');
					jqf('input[name=c_corporate]').attr('checked', false);
					jqf('input[name=c_funding]').attr('checked', false);
					jqf('input[name=c_personal]').attr('checked', false);
				}
				else {
					if (jqf('#communityFormBox h4.error').length == 0) {
						jqf('#communityFormBox').prepend('<h4 class="error">Please, fill out highlighted fields and try again.</h4>');
					}
					jqf('#c_email').addClass('error');
					jqf('label[for=c_email]').addClass('error');
				}
				var destination = jqf('#communityFormBox').offset().top;
				jqf('html').animate({scrollTop: destination});
			}
		});
		return false;
	});
});
