/*  This could be added as a function that always runs...
    It allows you to check the value against the title tag.
$(document).ready(function()
{
    $(".defaulted").focus(function()
    {
        if ($(this).val() == $(this)[0].title)
        {
            $(this).removeClass("defaultTextActive");
            $(this).val("");
        }
    });

    $(".defaulted").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).addClass("defaultTextActive");
            $(this).val($(this)[0].title);
        }
    });

    $(".defaultText").blur();
});

*/

function DataCheck(frm)
{
	$('.error').removeClass('error');
	$('#errormessage').html("");

	var res = true;

	$('.req:visible').each(
		function(item) {
			if($(this).val().trim() == "")
			{
				$(this).addClass('error');
				res = false;
			}
			else if($(this).hasClass('numeric') && isNaN($(this).val().trim()))
			{
				$(this).addClass('error').after('<span class="err" style="padding-left: 10px;">Numbers Only</span>');
				res = false;
			}
		}
	);
	if (!res)
	{
		$('#errormessage').html("Please complete all required fields.").addClass('error');
	}
	return res;
}

/* Pass the ID of the form and this will serialize it */
function PostForm(frm)
{
	var data = $('#'+frm).serialize();
	$.post('/include/ajax/actions.php', {
			frm: data
		}, function(output){
			if (output != "")
			{
				alert(output);
			}
			location.reload();
		});
}

/* Used as a generic catchall delete ajax call */
/* We need three values, table, ID, and return url.*/
function DeleteRecord(type, id, url)
{
	$.post('/include/ajax/actions.php', {
			action: 'delete_record',
			table: type,
			ID: id
		}, function(output){
			if (output != "")
			{
				alert(output);
			}
			location.reload();
		});
}

function SessionCache(use_ID)
{
	$('#'+use_ID).val().trim();
	$.post("/include/sbx/retail/AJAX_actions.php", {
			action: 'session_cache',
			id: use_ID,
			value: $('#'+use_ID).val()
		}, function(output) {
			$('#'+use_ID).addClass(output);
		});
}

function CheckEmail(str) {
	if (str == '') return true;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else
		return false;
}

function SendEmail()
{
	var msg = "";
	if (!DataCheck())
	{
		msg += "Please complete all fields.";
	}
	if (!verifyEmailFormat($('#email').val()))
	{
		msg += "Invalid Email Address.";
		$('#email').addClass('error');
	}

	if (msg == "")
	{
		$.post("/jadeanvil/include/ajax/actions.php", {
				name: $('#name').val(),
				email: $('#email').val(),
				msg: $('#msg').val(),
				action: 'email'
			}, function(output) {
				$('#contactform').html('<legend>&nbsp;CONTACT</legend><div class="jade" style="text-align: center; margin-top: 80px;">Your message has been sent.<br /><br /><br />You\'ll hear back soon.</div>');
			});
	}
	else
	{
		$('#err').addClass('error').html(msg);
	}
}

function ImageReplace(tmp, state)
{
	$(tmp).attr({src: '/files/images/nav/'+$(tmp).attr('replace')+'_'+state+'.png'});
}

function SelectThis(tmp)
{
	$('ul.selector li').removeClass("active");
	$('#'+tmp).addClass("active");
	var use_val = tmp.split("_");
	$('#'+use_val[0]).val(use_val[1]);
}

function SwapOut(id)
{
	$('#'+id).html('<input type="file" name="img" id="img" />');
}

function LoadContent(id, content)
{
	$('#'+id).load('/include/ajax/actions.php', {
			action: 'load_new_image',
			content: content
		});
}
