$(document).ready(function() {
	initForms();
});

function initForms(){
	$('label.req').prepend('<span class="required">* </span>');
	$('textarea').blur(function(){
		var max = $(this).attr('maxlength');
		if (typeof(max) !== 'undefined' && this.value.length > max){
			this.value = this.value.substr(0, max);
			alert('Your input has been limited to '+max+' characters.');
		}
	});
	$('#reload').click(function(){
		$('#captcha').attr('src', '/securimage/securimage_show.php?' + Math.random()); 
		return false;
	});
}	

function validate(id, req, regex){
	var field = $('#'+id);
	var label = $('label[for='+id+']');
	var textbox = field.is(':text') || field.is('textarea') || field.is(':password') || field.is(':file'); 
	var checkbox = (field.is(':checkbox') || field.is(':radio'));
	var selectbox = field.is('select');
	var msg = '';
	if (req && ((textbox && field.val().length === 0) || (checkbox && !field.is(':checked')) || (selectbox && field.val().length === 0))){
		field.add(label).addClass('in-error');
		msg += label.find('span.msg-req').html() + '<br />';
	}
	else if (textbox && field.val().length > 0 && regex && !regex.test(field.val())){
		field.add(label).addClass('in-error');
		msg += label.find('span.msg').html() + '<br />';
	}
	else {
		field.add(label).removeClass('in-error');
	}
	return msg;
}

function validateRadios(name) {
	var checked = false, msg = '';
	$('input[name='+name+']:checked').each(function(){
		checked = true;
	});
	if (!checked) {
		msg = $('label[for='+name+']').eq(0).find('span.msg').html() + '<br />';
	}
	return checked ? '' : msg;
}

function checkForm(msg){
	if (msg !== '') {
		$('#error-msg').html(msg).show();
		$('#reload').trigger('click');
		$('#captcha_code').val('');
		return false;
	} else {
		$('#valid').val($('#token').val());
		$('#error-msg').html('').hide();
		return true;
	}
}

function checkFormPopup(msg){
	if (msg !== '') {
		msg = '<strong>We\'re sorry, there was a problem with your form.</strong><div class="error">'+msg+'</div>';
		$('#pop-error').html(msg);
		$('#trigger-error').trigger('click');
		return false;
	} else {
		$('#valid').val($('#token').val());
		$('.pop-close').trigger('click');
		return true;
	}
}

function trimInput(){
	$(':input[type=text], textarea').each(function(){
		var val = $(this).val().replace(/^\s+|\s+$/, ''); // trim leading/trailing whitespace
		$(this).val(val);
	});
}

function cleanInput(){
	$(':input[type=text], textarea').each(function(){
		var val = $(this).val()
//			.replace(/"/g, '&#34;') // replace " with entity
//			.replace(/'/g, '&#39;') // replace ' with entitiy
//			.replace(new RegExp('/', 'g'), '&#47;') // replace / with entity
//			.replace(/_/g, '&#95;') // replace _ with entity
//			.replace(/%/g, '&#37;'); // replace % with entity
		$(this).val(val);
	});
}

var rx = {
	name: /^[\-A-Z',\.\s]*$/i, // matches name: letters, dash, period, comma, apostrophe [\w -.,']
	addr: /^[\-\w0-9:\,#\.\/\s]*$/, // matches address: letters, numbers, dash, period, comma, slash, colon, hash [\w\d -.,/:#]
	zip: /^\d{5}([\-\s]?\d{4})?$/, // matches zip code: ##### or #####-#### or ##### ####
	phone: /^\(?\d{3}\)?[\.\s\-]?\d{3}[\.\s\-]?\d{4}$/, // matches phone number: ###-###-#### or (###) ###-#### or ### ### #### or ###.###.#####
	email: /^[A-Z0-9._+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i, // matches email: xx@xx.xxx
	date: /^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20)\d\d$/, // matches date: dd/mm/yyyy
	trim: /^\s+|\s+$/, // matches leading or trailing whitespace (space, tab, line break)
	tag: /<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)<\/\1>/i, // matches html tag, return content inside tag
	date: /^(0[1-9]|1[012])[-\s\/\.](0[1-9]|[12][0-9]|3[01])[-\s\/\.](19|20)\d\d$/, // matches date: MM-DD-YYYY or MM.DD.YYYY or MM/DD/YYYY or MM DD YYYY
	time: /^(1[0-2]|0?[1-9])(:[0-5][0-9])*$/,
	money: /^\$?([0-9]+(,[0-9]{3})*(\.[0-9]{2})?)?$/
};


