$(document).ready(function(){
	// add counters
	$('.counter')
		.find('.remove')
		.html('Character Count:')
		.after('<div class="used">0</div>');
	
	// counter events
	//$('.mainrow').find('input').live('keyup', counter).live('focus', focusInput).live('blur', blurInput).each(counter);
	//$('.mainrow').find('textarea').live('keyup', counter).live('focus', focusInput).live('blur', blurInput).each(counter);
	$('.mainrow').find('input').live('blur', blurInput).each(counter);
	$('.mainrow').find('textarea').live('blur', blurInput).each(counter);
	
	// Required field checks - don't really need events for optional fields
	// delegate
	$('#prform').delegate('input', 'blur', validateInput);
	$('#prform').delegate('input', 'keyup', counter);
	$('#prform').delegate('input', 'focus', focusInput);
	$('#prform').delegate('textarea', 'blur', validateInput);
	$('#prform').delegate('textarea', 'keyup', counter);
	$('#prform').delegate('textarea', 'focus', focusInput);
	
	$('#category').live('change', validateCategory);
	
	/*
	// live
	$('#headline').live('blur', validateInput);
	$('#summary').live('blur', validateInput);
	$('#body').live('blur', validateInput);
	$('#tags').live('blur', validateInput);
	$('#category').live('change', validateCategory);
	$('#author').live('blur', validateInput);
	$('#email').live('blur', validateEmail);
	$('#code').live('blur', validateCode);
	*/
	
	// override form submission
	$('#prform').submit(checkSubmission);
	
	// correct the color of search field on focus
	$('#searchfor').bind('focus', function(){
		if ($(this).val() == "Search Press Releases") { $(this).val(""); $(this).addClass('highlight'); }
	});
});

function focusInput() {
	$(this).parent().addClass('focus');
}
function blurInput() {
	$(this).parent().removeClass('focus');
}

function checkSubmission() {
	submitForm = true;
	// CHECK REQUIRED FIELDS
	required = $(this).find('.required');
	
	required.find('input').filter(validateInput);		// forces recheck of required fields
	required.find('textarea').filter(validateInput);	// forces recheck of required fields
	required.find('select').filter(validateCategory);	// forces recheck of required fields
	
	if (required.not('.ok').length > 0) { // some required fields aren't up to scratch
		submitForm = false;
	}
	
	// CHECK OPTIONAL FIELDS
	optional = $(this).find('.optional');
	optionalOK = optional.find('input').filter(validateInput).length;	// number of <input> that are ok
	selects = optional.find('select').length;							// number of <select> - won't need checking
	
	if (optionalOK + selects == optional.length) {
		// all optionals are ok
	} else {
		// optionals were invalid
		submitForm = false;
	}
	
	if (!submitForm) {
		//$('#submitform').parent().find('.info').text('Please correct the errors in the form before submitting');
		//return false
        return true;
	} else {
		$('#submitform').parent().find('.info').text('before');
		return true;
	}
}

function counter() {
	inputdiv = $(this).parent();
	count = $(this).val().length;
	max = inputdiv.find('.max').text();
	if (count > max) {
		inputdiv.removeClass('ok').addClass('over').find('.used').text(count);
	} else {
		inputdiv.removeClass('over').find('.used').text(count);
	}
}

function checkLength(element,min,max) {
	var value = $(element).val();
	
	if (value.length == 0) {
		if ($(element).parent().hasClass('required')) {
			$(element).parent().removeClass('ok').find('.info').html("Required");
			return false;
		} else {
			return true; // not required so empty is ok
		}
	}
	if (value.length < min) {
		$(element).parent().removeClass('ok').find('.info').html("Minimum " + min + " characters");
		return false;
	} else if (value.length > max) {
		$(element).parent().removeClass('ok').find('.info').html("Maximum " + max + " characters");
		return false;
	} else {
		return true;
	}
}

function validateInput(element) {
	inputID = $(this).attr('id');
	if (inputID == "email") {
		return validateEmail(element);
	} else if (inputID == "code") {
		return validateCode(element);
	} else if (inputID == "rcode") {
		return true;
	} else if (inputID == "submitform") {
		return false;
	} 
	
	var min = $(this).parent().find('.min').text();
	var max = $(this).parent().find('.max').text();
	
	if (!checkLength($(this),min,max)) { // length wasn't within range
		return false;
	} else {
		$(this).parent().addClass('ok').find('.info').html('');
		return true;
	}
}

function validateCategory() {
	var category = $('#category');
	
	if (category.val() == 0) { // length wasn't within range
		category.parent().removeClass('ok').find('.info').html("Required");
		return false;
	} else {
		category.parent().addClass('ok').find('.info').html('');
		return true;
	}
}

function validateEmail() {
	email = $('#email');
	var min = email.parent().find('.min').text();
	var max = email.parent().find('.max').text();
	
	if (!checkLength(email,min,max)) { // length wasn't within range
		return false;
	} else if (email.val().indexOf(".") <= 1 || email.val().indexOf("@") <= 2) {
		email.parent().removeClass('ok').find('.info').html("Email address is invalid");
		return false;
	} else {
		email.parent().addClass('ok').find('.info').html('');
		return true;
	}
}

function validateCode() {
	var code = $('#code');
	
	if (code.val().length < 5) { // length wasn't within range
		code.parent().removeClass('ok').find('.info').html("Required");
		return false;
	} else {
		code.parent().addClass('ok').find('.info').html('');
		return true;
	}
}







