﻿// JavaScript Library


// 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';
}



// 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 .
*/



// 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) {
	argSeparator = argSeparator || "&";
	var els = argForm.elements;
	var pairs = "";
	for (var i=0; i<els.length; i++) {
		if (els[i].value.length>0) {
			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.




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;
}



// Jeff's QueryString Object - var x = QueryString.get("id");
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();


function cl(argString) {
	try {
		console.log(argString);	
	}
	catch(err) { }
}



function hasParent(el, parentToFind) {
	// returns boolean on whether or not el is an ancestor of parentToFind
	var bool = false;
	do {
		el = el.parentNode || false;
		if (!el) break;
		bool = (el == parentToFind) ? true : bool;
	}
	while (el && !bool)
	return bool; 
}




function checkajax() {
	var test = true;
	if (test) 
		alert("Hi. I'm in debug mode. Here's the ajax response: " + arguments[0]);
}


/***************** Here down uses DOMAssistant stuff ***********************/

function ale(prmFunc) {
	DOMAssistant.DOMReady(prmFunc);
}


function DAstriper(argDACollection, argAltClass) {
	// function defaults to the table.striped row striper if no arguments are passed.
	argDACollection = argDACollection || ".striped tr";
	argAltClass = argAltClass || "altRow";
	var counter = 0;
	$(argDACollection).each(function(){
		counter++;
		if (counter % 2) $(this).addClass(argAltClass);
	});
}
DOMAssistant.DOMReady(DAstriper);




function toggleClass(el, oldclass, newclass) {
	$(el).removeClass(oldclass);
	$(el).addClass(newclass);
}


function prTabColoring(strType) {
	$("#maintab li").each(function(){
		if ($(this).hasClass(strType))
			$(this).addClass("selected");
	});
}



function linkpopper() {
    $("a").each(function(){
        if (this.href.match("http://")) {
            this.setAttribute("_taget","blank");
        }
    });
}
ale(linkpopper);

