//contains global javascript functions, commonly used through the site.

//used by the autologout on pages like information.php
var analog_timeout = false;
var times_check_logged = 0;

/*
flashYellow(element)

element is a css selector
	#id of element (including the #)
	.class of element (including the .)
	textarea or any other element name
	
	you can also go deeper down as in css
	ie: p span -- all spans inside of a p

	- Jeff Rubatino 3/31/10 -
		Extended to go to and from existing background
		color rather than hard-coding to #ffffff.

*/

				
				
function flashYellow(element)
{
	jQuery(element).each(function ()
	{
		m = jQuery(this);

		if (m.css("background-color") == undefined || m.css("background-color") == '' 
				|| m.css("background-color") == null || m.css("background-color") == 'transparent' ||
				m.css("background-color") == 'rgba(0, 0, 0, 0)')
			m.css("background-color","#ffffff");

		try{
			m.animate({ backgroundColor: "#FFFF80" }, 1000)
			.animate({ backgroundColor: m.css("background-color") }, 1000);
		} catch (err)
		{
			fade = true;
		}
	});
}

/*
jSwitch(ele1, ele2)

ele1 is the element that is currently displayed (display: block or inline)
ele2 is the element that is not displayed (display: none)

ele1, ele2 is a css selector
	#id of element (including the #)
	
	probably want to use #ids for ele, since they are the most precise selectors


*/
function jSwitch(ele1, ele2)
{
	jQuery(ele1).hide();
	jQuery(ele2).show();
}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


//use like str.trim();
String.prototype.trim2 = function() { return this.replace(/^\s+|\s+$/g, ''); }

function testSpecials(input)
{	
	return input.match(new RegExp("[^A-Za-z0-9_ \f\s\d\n\,\\\"\£\'\+\=\;\:\$\#\@\;\}\{\!\*\(\)\.\:\?\%-]", "g"), "");
}

 function checkemail(email) 
{
	return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}

//checks dates to see if thy are valid
function checkDate(d, m, y)
{
	// if any of the fields is null, invalid, unless they are all null
	if ((isNaN(d)|| isNaN(m) || isNaN(y))) {
		return false;
	}
	
	day = parseInt(d);
	month = parseInt(m);
	year= parseInt(y);

	if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
	{
		if(day > 31)
			return false;
	}
	
	if(month == 4 || month == 6 || month == 9 || month == 11)
	{
		if(day > 30)
		{
			return false;
		}
	}
	
	if(year == 2004 || year == 2008 || year == 2012 || year == 2016)
	{
		if(month == 2 && day > 29)
			return false;
	}
	else
	{
		if(month == 2 && day > 28)
			return false;
	}
	return true;
}

jQuery(document).ready(function () 
{
	function styleButtons()
	{
		jQuery(".round").parent("a").addClass("roundparent");
	}
	styleButtons();
	
	
	/*function checkBarSearch() {
		if (jQuery(".searchstring").val() == ' Last Name') {
			jQuery(".searchstring").val("");
		} else if (jQuery(".searchstring").val() == '') {
			jQuery(".searchstring").val(" Last Name");
		}
	}
	
	jQuery(".searchstring").click(function (){checkBarSearch()});
	jQuery(".searchstring").blur(function (){checkBarSearch()});
    */
	

	/**
		if a link is classed "faqQuestion" or "divToggle", this will toggle the next sibling div open and closed
		
		example
			<a href="#nogo" class="divToggle">Toggle the div</a>
			<div style="display: none">this div starts out closed, but will toggle open and closed</div>
		
		note that css needs to be applied to get the div in it's starting state if it needs to be closed
	*/
	jQuery(".faqQuestion, .divToggle").click(function() {

		jQuery(this).next("div").slideToggle();

	});	

	/*
	var loc = location.href
	jQuery(document).everyTime(5000, function(i) {
		if((loc.indexOf("/account/") != -1))
			checkLogout();
	});
	
	function checkLogout()
	{
		times_check_logged += 1;
		jQuery.post(
			timer_logout_file,
			"action=checkLogout",
			function(message) {
				if(message == "logout")
				{
					jQuery("form[name='logout']").append('<input type="hidden" name="inactive" value="Y" />');
					jQuery("form[name='logout'] a").click();
				}
			});
		if(times_check_logged >= Constants.SESSION_EXPIRE_LIMIT/5 && analog_timeout)
		{
			jQuery("form[name='logout']").append('<input type="hidden" name="inactive" value="Y" />');
			jQuery("form[name='logout'] a").click();
		}
	}
	*/
	
});

