$(document).ready(function(){
	// controls character input/counter
	$('#Advert_Body').keyup(function() {
		var maxChars = 500;
		var charLength = $(this).val().length;
		// Displays count
		$('#charCount').html(charLength + ' of ' + maxChars + ' characters used');
		// Alerts when 250 characters is reached
		if($(this).val().length > maxChars)
			$('#charCount').html(charLength + ' of ' + maxChars + ' characters used. You only have up to ' + maxChars + ' characters. If you submit this text in this condition it will be truncated to ' + maxChars + ' characters.');
	});
	$('#registered_companies_Brief_Description').keyup(function() {
		var maxChars = 500;
		var charLength = $(this).val().length;
		// Displays count
		$('#charCountBD').html(charLength + ' of ' + maxChars + ' characters used');
		// Alerts when 250 characters is reached
		if($(this).val().length > maxChars)
			$('#charCountBD').html(charLength + ' of ' + maxChars + ' characters used. You only have up to ' + maxChars + ' characters. If you submit this text in this condition it will be truncated to ' + maxChars + ' characters.');
	});
	
	$( '#tradeOther' ).hide();
	
	$( '#registered_companies_Affiliation' ).bind( 'change', function() {
		switch( $( this ).val() ) {
			case '2':
			case '3':
			case '5':
				$( '#tradeOther' ).slideDown( 'normal' );
				break;
				
			default:
				$( '#tradeOther' ).slideUp( 'normal' );
				break;
		}
	} );
	$( '#registered_companies_Affiliation' ).change();//trigger a change event
	
	$( '#regForm' ).submit( checkForm );
	
	//get rid of Not Applicable options
	$( 'option' ).each( function() {
		if ( $( this ).text() == 'Not Applicable' ) $( this ).text( 'Please Select...' );
	} );
});

function checkForm() {
	if ( $( '#registered_companies_Company_Name' ).val() == '' ) {
		doFieldValidate( 'Company_Name', 'Company name must be completed' );
		return false;
	}

	if ( $( '#registered_companies_Primary_Contact' ).val() == '' ) {
		doFieldValidate( 'Primary_Contact', 'The primary contact\'s name must be entered' );
		return false;
	}
	
	if ( $( '#registered_companies_Contact_Email' ).val() == '' ) {
		doFieldValidate( 'Contact_Email', 'A contact email must be given' );
		return false;
	}
	
	if ( $( '#registered_companies_Password' ).val() == '' ) {
		doFieldValidate( 'Password', 'Please enter a password' );
		return false;
	}
	
	if ( $( '#registered_companies_Password_Confirm' ).val() == '' ) {
		doFieldValidate( 'Password_Confirm', 'Please confirm your password by entering it twice' );
		return false;
	}
	
	if ( $( '#registered_companies_Password_Confirm' ).val() != $( '#registered_companies_Password' ).val() ) {
		doFieldValidate( 'Password_Confirm', false );
		doFieldValidate( 'Password', 'Your passwords do not match' );
		return false;
	}
	
	if ( $( '#registered_companies_Security_Question' ).val() == '' ) {
		doFieldValidate( 'Security_Question', 'Please enter a security question' );
		return false;
	}
	
	if ( $( '#registered_companies_Security_Answer' ).val() == '' ) {
		doFieldValidate( 'Security_Answer', 'Please enter a security answer' );
		return false;
	}
	
	if ( $( '#registered_companies_Web_Address' ).val() == 'www.' ) {
		$( '#registered_companies_Web_Address' ).val( '' );
	}
	
	if ( $( '#securityCode' ).val().toUpperCase() != $( '#jkg3e879t3yug678edtgy' ).val().toUpperCase() ) {
		doFieldValidate( 'securityCode', 'Your security code does not match.' );
		return false;
	}
	
	if ( $( '#securityCode' ).val() == '' ) {
		doFieldValidate( 'securityCode', 'You must complete the security code' );
		return false;
	}
	
	return true;
}

function doFieldValidate( fieldname, message ) {
	if ( message ) alert( message );
	$( '#registered_companies_' + fieldname ).focus();
	$( '#registered_companies_' + fieldname ).css( 'background', '#FCC' );
}