////////////////////////////// Utilities ///////////////////////////


// Simon Wilson's addLoadEvent - http://simon.incutio.com/archive/2004/05/26/addLoadEvent 
// Attach more onload functions without worrying about erasing previously applied onload function. 
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}
/* To use:
addLoadEvent(nameOfFunctionToRunOnPageLoad);
addLoadEvent(function() {
  // more code to run on page load 
  thisFunctionHasArguements(foo,bar,suckit);
});
*/



// ProtoType's DollarSign($) getElementByID Function 
// This is a shorthand way to call document.getElementById("foo")  (http://prototype.conio.net) 
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}
/* To use:
	var foo = $("foo"); (finds the element with ID of foo)
*/




// Get Elements By Class Function
//http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(SEARCHCLASS,NODE,TAG) {
	var classElements = new Array();
	if ( NODE == null )
		NODE = document;
	if ( TAG == null )
		TAG = '*';
	var els = NODE.getElementsByTagName(TAG);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+SEARCHCLASS+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
/* To Use:
var foo = getElementsByClass("special") // gets all tags w/ class of special.
var foo = getElementsByClass("special",varblock,"input") 
// gets all inputs w/ class "special" that are children of collection varblock .
*/




// GP's Modify Class Function
// Specify a target, a class, and whether you want to add that class or remove it.
// Prevents attatching multiple instances of a class to an element.
function changeClass(prmTarget,prmClass,prmAddRemove) {
	if (prmAddRemove==null) prmAddRemove = "add";
	var oldclass = prmTarget.className;
	if (prmAddRemove=="add") {
		if (oldclass=="")
			prmTarget.className = prmClass;
		else if (oldclass.indexOf(prmClass)==-1)
			prmTarget.className += " " + prmClass;
	} 
	else if (prmAddRemove == "remove" && oldclass.indexOf(prmClass)!=-1)
		prmTarget.className = oldclass.replace(prmClass,"");
}





/* GP's toggleClass Function */
function toggleClass(ITEMtoTOGGLE,CLASS1,CLASS2) {
	if (CLASS2 == null) CLASS2 = "";
	if (ITEMtoTOGGLE.className != CLASS1) {
		ITEMtoTOGGLE.className = CLASS1;
	} else {
		ITEMtoTOGGLE.className = CLASS2;
	}
}
/* To use:
	element.onclick = function() {
		toggleClass(this,"oneClass","anotherClass");
	}
	This toggles the elements class from "oneClass" to "anotherClass" every time the 
	element is clicked. If it didnt' have any class to begin with, the element will
	get Class1 at the first click.
*/







// Add Alternating Class Function
// This function will apply a custom class to every other instances of a tag inside a specified parent element.
function stripeAnything(CONTAINER,ITEM,ALTCLASS) {
	var items = CONTAINER.getElementsByTagName(ITEM);
	var counter = 0;
	for (var i=0; i<items.length; i++) {
		counter = counter + 1;
		if (counter % 2 != 0) { 
			changeClass(items[i],ALTCLASS,"add");
		}	
	}
}
// To use, see sample below which applies a class of 'altItem' to alternating P tags inside div#stripeThese.
//   addLoadEvent( function() {
//  	  stripeAnything($("stripeThese"),"p","alt2");
//   });


function stripeTable() {
	var tables = document.getElementsByTagName("table");
	for (var i=0; i<tables.length; i++) {
		if (hasClass(tables[i],"striped"))	{
			stripeAnything(tables[i],"tr","altrow");
		}
	}
}
addLoadEvent(stripeTable);


// Insert Element After function - via Jeremy Keith
function insertAfter(NEWELEMENT,TARGETELEMENT) { 
	var parent = TARGETELEMENT.parentNode;
	if (parent.lastChild == TARGETELEMENT) {
		parent.appendChild(NEWELEMENT);
	} else {
		parent.insertBefore(NEWELEMENT,TARGETELEMENT.nextSibling);
	}
}

// 3 Cookie function from Dustin Diaz that are very useful
// http://www.dustindiaz.com/top-ten-javascript

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}

/* Basic create XMLHttpRequest */
function createXMLHttpRequest() {
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	} else {
		alert("No xmlHttp created.");
	}
}

// Rounds up all a forms elements and combines their names and values into a pseudo querystring.
// Useful for sending form info to AJAX POST calls.
function gatherElms(argForm,argSeparator) {
	if (argSeparator==null) 
		argSeparator = "&";
	var els = argForm.elements;
	var pairs = "";
	for (var i=0; i<els.length; i++) {
		if (els[i].value.length>0) {
			// conditional to only add checkboxes that are checked to the string.
			if (els[i].getAttribute("type")=="text" || els[i].checked==true || els[i].nodeName=="TEXTAREA" || els[i].nodeName=="SELECT")
				pairs += els[i].name + "=" + els[i].value + argSeparator;
		}
	}
	pairs = pairs.substring(0,pairs.length-1); // remove the last argSeparator.
	return pairs;
	// returns something like 'name=Bill&email=bill@gmail&text=I like you&submit=true'
}
// gatherElms($('theForm1')); builds the string using '&' to separate the name-value pairs.
// gatherElms($('theForm2'),"-"); builds the string using '-' to separate the name-value pairs.

// Validation Functions

function valCurrency(prmElem) {
	// 0-1 '-' then 0-1 '$' then 0+ (numeric) then 0-1 '.' then 0+ (numeric) ; match whole pattern
	var pattern = /^\-?\$?[0-9]*\.?[0-9]*$/;
	return (prmElem.match(pattern)) ? true : false;
	// note: with this regex, a value of '$.' will return true.
}

function valEmail(prmElem) {
	// 1+ (alphanum OR '-' OR '.') then 1 '@' then any (alphanum OR '-') then 1 '.' 
	// then 0+ (alphanum OR '-' OR '.') then 2+ (alphanum); match the whole pattern.
	var pattern = /^[a-zA-Z0-9\-\.]+@{1}[a-zA-Z0-9\-]+\.{1}[a-zA-Z0-9\-\.]*[a-zA-Z0-9]{2,}$/;
	return (prmElem.match(pattern)) ? true : false;
}

function valPhone(prmElem) {
	// 0-1 whitespace then 0-1 '(' then 3 (num) then  0-1 ')'||'-' then 0-1 whitespace 
	// then 3 (num) then 0-1 '-'||whitespace then 4 (num); match whole pattern
	var pattern = /^\s?\(?[0-9]{3}(\)|-)?\s?[0-9]{3}(-|\s)?[0-9]{4}$/;
	return (prmElem.match(pattern)) ? true : false;
}

function valSSNum(prmElem) {
	// 3 (num) then 0-1 '-' then 2 (num) then 0-1 '-' then 4 (num); match whole pattern
	var pattern = /^[0-9]{3}-?[0-9]{2}-?[0-9]{4}$/;
	return (prmElem.match(pattern)) ? true : false;
}

function valZip(prmElem) {
	// 5 (num); match whole pattern
	var pattern = /^[0-9]{5}$/;
	return (prmElem.match(pattern)) ? true : false;
}

function valDate(prmElem,argFormat) {
	if (argFormat==null) argFormat = "mmddyyyy";
	var pattern;
	if(argFormat=="mmddyyyy") {
		pattern = /(^0?[1-9]{1}|^[1-2]{1}[0-2]{1})(-|\/)?(0?[1-9]{1}|[1-2]{1}[0-9]{1}|30|31){1}(-|\/)?(19[0-9]{1}[0-9]{1}|200[0-8]{1})$/;                 
	}
	return (prmElem.match(pattern)) ? true : false;
}

function isFilled(prmEl) {
	if (prmEl.value == prmEl.defaultValue) {
		return false;
	} else if (prmEl.nodeName=="SELECT" && prmEl.selectedIndex == 0) {
		return false;
	} else {
		return true;
	}
}

 

function hasClass(prmEl,prmClass) {
	var cl = prmEl.className;
	var ar = new Array();
	ar = cl.split(' ');
	var pass = false;
	for (var i=0; i<ar.length; i++) {
		if (ar[i]==prmClass) pass = true;
	}
	return (pass) ? true : false;
}


function countNextSiblingEls(prmEl) {
	var sibs = prmEl.parentNode.childNodes;
	var elPoz = 0;
	for (var i=0; i<sibs.length; i++) {
		if (sibs[i]==prmEl) elPoz = i;
	}
	countSibs = 0;
	for (var j=elPoz+1; j<sibs.length; j++) {
		if (sibs[j].nodeType==1) countSibs += 1;
	}
	return countSibs;
}


function killsubdiv() {
	if ($('sbSubnav')) {
		if ($('sbSubnav').childNodes.length <= 1) {
			$('sbSubnav').style.display = "none";
		}
	}
}
addLoadEvent(killsubdiv);



// Button Link Rounded Corners generator -- <span class="bl_1"></span>

function roundButtonLinks() {
	var bls = getElementsByClass("buttonlink",document,"a");
	for (var i=0; i<bls.length; i++) {
			bls[i].innerHTML += "<span class='bl_1'></span><span class='bl_2'></span><span class='bl_3'></span><span class='bl_4'></span>";
	}
}
addLoadEvent(roundButtonLinks);




// Just to test Apply Online thing: discard immediately

function checkit() {
	if ($('sbProfileList')) {
		var lis = $('sbProfileList').getElementsByTagName("li");
		for (var i=0; i<lis.length; i++) {
			if (i < 6) {
				changeClass(lis[i],"checked","add");
			}
		}
	}
}
addLoadEvent(checkit);

// Jeff's QueryString Object
var QueryString =  {

	keyValuePairs : Array, 
	q : "", 
	init : function () {
		
		this.q = window.location.search;
		if (this.q.length > 1) {
			this.q = this.q.substring(1, this.q.length);
		} else {
			this.q = null;
		}
		this.keyValuePairs = new Array();
		if (this.q) {
			for(var i=0; i < this.q.split("&").length; i++) {
				this.keyValuePairs[i] = this.q.split("&")[i];
			}
		}			
	}, 
	get : function(key) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == key)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	},
	toString : function() {return this.q;}, 
	getKeys : function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}, 
	
	getKeyValuePairs : function() {return this.keyValuePairs;}, 
	getLength : function() {return this.keyValuePairs.length;}		
  
}
QueryString.init();

addLoadEvent(function() {
  QueryString.init();
});


function cl(prmStr) {
	try {
		console.log(prmStr);
	}
	catch(err) {
		
	}
}	


function linkdisclaimer() {
	var a = document.links;
	for (var i=0; i<a.length; i++) {
		if (!a[i].href.match("mailto:")) continue;	
		a[i].setAttribute("title","Under Florida law, e-mail addresses are public records. If you do not want your e-mail address released in response to a public records request, do not send electronic mail to this entity. Instead, contact this office by phone or in writing.");
	}
}
addLoadEvent(linkdisclaimer);



function hd() {
	var lis = $("sbSubnav").getElementsByTagName("li");
	for (var i=0; i<lis.length; i++) {
		if (lis[i].innerHTML.indexOf("Apply Online") != -1) {
			lis[i].style.display = "none";	
		}
	}
}
//addLoadEvent(hd);









