var _use_alert_box = false;
var _display_all_error_messages = true;

$(function() 
{
	$(".cms_signup_button").click(function(e)
	{
		$form = $(this).closest('form');

		$form.find('input[name="method"]').val('ajax');
		e.stopImmediatePropagation();

//#################################################################################
//#################################################################################
//#################################################################################
		//console.log( $form.find('input[name="rules_js"]').val() );
//#################################################################################
//#################################################################################
//#################################################################################
		
		if ( $form.find('input[name="rules_js"]').val() != '' && eval( decodeURIComponent( $form.find('input[name="rules_js"]').val() ) ) === false ) return false;
		var data = $form.serialize();	
		var host_path=$('input[name="host_path"]').val();
		$.getJSON(host_path+'Classes/ajaxManager.cms.php?jsoncallback=?',data,function(data) {
			var data = eval(data);
			$form.find('.cms_error_message').remove();
			$form.find('.cms_error_label').remove();
			$form.find('.cms_error_break').remove();
			if ( data.status == 'ERR' )
			{
				for (i in data.error_strings)
				{
					if ($('input[name="cms_fields[' + i + '][]"]').length > 0)
						lmnt = $('input[name="cms_fields[' + i + '][]"]');
					else
						lmnt = $('input[name="cms_fields[' + i + ']"]');

					_messages = data.error_strings[i].split('|');
					if (_messages.length > 0)
					{
						for (z = 0; z < _messages.length; z++)
						{
							if (_use_alert_box === true)
							{
								alert( _messages[z] );
							}
							else
							{
								_html = $('<label class="label-display cms_error_label">&nbsp;</label><div class="cms_error_message">&raquo; ' + _messages[z] + '</div><div class="cms_error_break" style="clear:both;"></div>');
								if (lmnt.length > 1)
									$(lmnt[0]).closest('.inputcol').after( _html );
								else
									$(lmnt[lmnt.length - 1]).after( _html );
							}
						}
					}

					$(lmnt[0]).focus();

					if (_display_all_error_messages === false) return false;
				}
			}
			else
			{
				if (typeof window['cms_form_submit_success'] == 'function') 
					window['cms_form_submit_success'].call( this, data );
				else
				{
					$form[0].reset();
					alert('Thank you, we now have your information.');
				}
			}
		});

		return false;
	});
});

jQuery.expr[':'].regex = function(elem, index, match) 
{
	var matchParams = match[3].split(','),
		validLabels = /^(data|css):/,
		attr = { method: matchParams[0].match(validLabels) ? matchParams[0].split(':')[0] : 'attr', property: matchParams.shift().replace(validLabels,'') },
		regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), 'ig');
	return regex.test(jQuery(elem)[attr.method](attr.property));
}

function DATE_VALID( lmnt_selector, data, fail )
{
	dd = $( lmnt_selector.replace('.\\]', 'd\\]') ).val();
	mm = $( lmnt_selector.replace('.\\]', 'm\\]') ).val();
	yy = $( lmnt_selector.replace('.\\]', 'y\\]') ).val();

	d = new Date(yy + '/' + mm + '/' + dd);
	if ( d.getFullYear() != yy || d.getMonth() + 1 != mm || d.getDate() != dd ) { alert( fail ); $(lmnt_selector).focus(); return false; }

	return true;
}

function DATE_BETWEEN( lmnt_selector, data, fail )
{
	return true;
}

function EMAIL_VALID( lmnt_selector, data, fail )
{
	if ( !(/^[A-Za-z0-9._%+-]+\@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/m).test( trim( $(lmnt_selector).val() ) ) ) { alert( fail ); $(lmnt_selector).focus(); return false; }

	return true;
}

function IS_DIGIT( lmnt_selector, data, fail )
{
	if ( !/^[0-9.\-,()\s]+$/m.test( trim( $(lmnt_selector).val() ) ) ) { alert( fail ); $(lmnt_selector).focus(); return false; }
	
	return true;
}

function NOT_NULL( lmnt_selector, data, fail )
{ 
	if ($(lmnt_selector).length == 1)
	{
		if ( trim( $(lmnt_selector).val() ) == '' ) { alert( fail ); $(lmnt_selector).focus(); return false; }
	}
	else
	{
		if ( $(lmnt_selector)[0].nodeName == 'SELECT' ) /* Date Selects */
		{
			if  ( trim( $( lmnt_selector.replace('.\\]', 'd\\]') ).val() ) == '' || 
				  trim( $( lmnt_selector.replace('.\\]', 'm\\]') ).val() ) == '' || 
				  trim( $( lmnt_selector.replace('.\\]', 'y\\]') ).val() ) == '' )
				{ alert( fail ); $(lmnt_selector).focus(); return false; }
		}
		else if ( $(lmnt_selector + ':checked').length == 0 ) { alert( fail ); $(lmnt_selector).focus(); return false; }
	}

	return true;
}

function NUM_CHARS( lmnt_selector, data, fail )
{
	_len = parseInt( data );
	if ( !_len || _len <= 0 ) _len = 0;

	if ( trim( $(lmnt_selector).val() ).length > _len ) { alert( fail ); $(lmnt_selector).focus(); return false; }
	
	return true;
}

function VALUE_GT( lmnt_selector, data, fail )
{
	_val = parseInt( data );
	if ( !_val ) _val = 0;

	if ( parseInt( trim( $(lmnt_selector).val() ) ) < _val ) { alert( fail ); $(lmnt_selector).focus(); return false; }

	return true;
}

function VALUE_LT( lmnt_selector, data, fail )
{
	_val = parseInt( data );
	if ( !_val ) _val = 0;

	if ( parseInt( trim( $(lmnt_selector).val() ) ) > _val ) { alert( fail ); $(lmnt_selector).focus(); return false; }

	return true;
}

/*

	Note: SAME_AS is not case sensitive.

*/
function SAME_AS( lmnt_selector, data, fail )
{
	if ( trim( $( lmnt_selector ).val() ).toLowerCase() != trim( $( '[name="cms_fields[' + parseInt( data ) + ']"]' ).val() ).toLowerCase() )
		{ alert( fail ); $(lmnt_selector).focus(); return false; }

	return true;
}

function trim( str ) { return str.replace(/^\s+|\s+$/gm, ''); }
