var $ = _$ = jQuery;

function apaslt09_contact_form() {
	var _$form = _$('#body div.contactForm');
    var formAction = _$form.find('input.form_action').val();
	
	// onSubmit Modifications + Cleanup
    _$('body > form')
		.attr('action', formAction)
		.removeAttr('autocomplete')
		.removeAttr('id')
		.removeAttr('style')
		.removeAttr('enctype')
		.removeAttr('name');
	_$('input#__EVENTTARGET, input#__EVENTARGUMENT, input#__VIEWSTATE', 'body > form > div:first-child').remove();
	_$('input#__dnnVariable').remove();
	_$('input.form_action', _$form).remove();
	
	// Validation
	_$('body > form').submit(function() {
		var halt = "";
		_$('div.contactForm input, div.contactForm textarea').each(function() {
			if( _$(this).val() == _$(this).parents('.form-item').find('label').text() ) {
				halt = "true";
			}
		});
		if(halt == "") {
			return true;
		}
		else {
			window.alert("A required field was left blank. Please complete all fiends before submitting.");
			return false;
		}
	});
	
	// Inline Labels
	apaslt09_inline_labels(_$form);
	_$form.addClass('js-enabled').show();

    // jNice
    _$form.jNice();
    _$form.find('textarea, select[multiple]').addClass('js');
}

_$(document).ready(function() {
    if( _$('#dnn_ControlPanel table').length ) {
		_$('#dnn_ControlPanel').hide();
		if( _$('#body.editMode, #body.designMode').length ) {
			_$('#body div.contactForm').before('<p><strong>Both the Form and Control Panel are disabled on this page in Design and Edit mode.</strong><br/><br/><a href="/Home/ContactUs/tabid/60/ctl/Tab/action/edit/returntabid/41/Default.aspx">Edit page settings</a> or <a href="/">return to the home page</a> and disable Design/Edit mode to view the form.</p>');
		}
		else {
			_$('#body div.contactForm').before('<p><strong>The Control Panel is disabled on this page.</strong><br/><br/><a href="/Home/ContactUs/tabid/60/ctl/Tab/action/edit/returntabid/41/Default.aspx">Edit page settings</a> or <a href="/">return to the home page</a>.<br/><br/>&nbsp;<br/></p>');
			apaslt09_contact_form();
		}
	}
	else {
		apaslt09_contact_form();
	}
});
