// This script was provided by VKIStudios.com

/*
Author:			Erik Vold
Version:		1.0
Date Created:	Summer 2008
Last Updated:	22081123

Directions:
1. Include this script inside of the body tag, we recommend that it goes as close to the top of the <body> tag as possible (how close to the top depends on usage)


Notes:

1. If you want to track form submissions, and you are using js form validation, then unfortunatly even if the validation fails, the click track function will still execute
2. mailto: links are not tracked in IE6
*/

if(typeof linkAndFormTracking=="undefined" || !linkAndFormTracking){var linkAndFormTracking={};}







linkAndFormTracking.setupAllBut = function( fn, idsToSkip, classNamesToSkip, skipMailTo, skipJavascriptLinks, skipExternalLocations ){
	// add a window onLoad event to setup the click tracking
	this.addLoadEvent(function(){linkAndFormTracking.setupAllLinksEvent(fn, idsToSkip, classNamesToSkip, skipMailTo, skipJavascriptLinks, skipExternalLocations)});
}

linkAndFormTracking.setupAllButEvent = function( fn, idsToSkip, classNamesToSkip, skipMailTo, skipJavascriptLinks, skipExternalLocations ){
	//alert('setup click & submit events');
	
	// attach event to <a> tags
	this.loopThroughATags( fn, idsToSkip, classNamesToSkip, skipMailTo, skipJavascriptLinks, skipExternalLocations );
	// attach the event to <form> tags
	this.loopThroughFORMTags( fn, idsToSkip, classNamesToSkip, skipExternalLocations );
}

linkAndFormTracking.testStringAgainstLocation = function(str){
	var regExp = new RegExp("^(http|https)://"+location.host,'i');
	
	if(regExp.test(str)){
		return true;
	}
	
	return false;
}

linkAndFormTracking.loopThroughATags = function( fn, idsToSkip, classNamesToSkip, skipMailTo, skipJavascriptLinks, skipExternalLocations ){
	//alert('checking <a> tags');

	if(document.getElementsByTagName){
		// get <a> tags
		var aTags = document.getElementsByTagName('a');
		//alert(aTags.length);
		
		var numLinksEventWasAddedTo = 0;
		
		for (var i = 0; i<aTags.length; i++) {
			// if the link is a mailto: link and skipMailTo is true, then skip it.
			if( skipMailTo && aTags[i].protocol == "mailto:" ){
				//alert(aTags[i].id+' - skipped b/c of mailto:');
				continue;
			}
			
			// if the link is a javascript: link and skipJavascriptLinks is true, then skip it.
			if( skipJavascriptLinks && aTags[i].protocol == "javascript:" ){
				//alert(aTags[i].id+' - skipped b/c of javascript:');
				continue;
			}
			
			// determine if we should skip external links
			if( aTags[i].protocol != "javascript:" &&
					aTags[i].protocol != "mailto:" &&
					skipExternalLocations &&
					aTags[i].href.charAt(0) != '#' &&
					!this.testStringAgainstLocation(aTags[i].href) ){
				//alert(aTags[i].id+' - skipped b/c of href = '+aTags[i].href);
				continue;
			}
			
			// determine if the current tag id is not one we should skip
			if( this.skipID(idsToSkip,aTags[i].id) ){
				//alert(aTags[i].id+' - skipped b/c of id');
				continue;
			}
			
			// determine if the current tag class name is not one we should skip
			if( this.skipClassName(classNamesToSkip,aTags[i].className) ){
				//alert(aTags[i].id+' - skipped b/c of className');
				continue;
			}
			
			//alert(aTags[i].id);
			this.appendListener(aTags[i],"click",fn);
			
			numLinksEventWasAddedTo++;
		}
		//alert(numLinksEventWasAddedTo);
	}
}

linkAndFormTracking.loopThroughFORMTags = function( fn, idsToSkip, classNamesToSkip, skipExternalLocations ){
	if(document.getElementsByTagName){
		// get <form> tags
		var formTags = document.getElementsByTagName('form');

		for (var i = 0; i < formTags.length; i++) {			
			// determine if the current tag id is not one we should skip
			if( this.skipID(idsToSkip,formTags[i].id) ){
				continue;
			}

			// determine if the current tag class name is not one we should skip
			if( this.skipClassName(classNamesToSkip,formTags[i].className) ){
				continue;
			}

			// determine if we should skip forms which post to external pages
			if( skipExternalLocations && this.trimStr(formTags[i].action)!='' && !this.testStringAgainstLocation(formTags[i].action) ){
				continue;
			}

			this.appendListener(formTags[i],"submit",fn);
		}
	}
}

linkAndFormTracking.skipID = function(idsToSkip,eleID){	
	// determine if the given tag id is not one we should skip
	if(this.typeOf(idsToSkip)=="array" && idsToSkip.length > 0){
		for(var j=0;j<idsToSkip.length;j++){
			if(idsToSkip[j]==eleID){
				return true;
			}
		}
	}

	return false;
}

linkAndFormTracking.skipClassName = function(classNamesToSkip,eleClassName){
	// determine if the given tag class name is not one we should skip
	if(this.typeOf(classNamesToSkip)=="array" && classNamesToSkip.length > 0){
		for(var j=0;j<classNamesToSkip.length;j++){
			if(classNamesToSkip[j]==eleClassName){
				return true;
			}
		}
	}
	
	return false;
}








linkAndFormTracking.setupLookFor =  function( fn, urlsToInclude ){
	this.addLoadEvent(function(){ linkAndFormTracking.setupLookForEvent( fn, urlsToInclude ) });
}

linkAndFormTracking.setupLookForEvent = function( fn, urlsToInclude ){
	linkAndFormTracking.loopThroughATagsAndInclude( fn, urlsToInclude );
	
	return;
}


linkAndFormTracking.loopThroughATagsAndInclude = function( fn, hrefsToInclude ){
	if (document.getElementsByTagName) {
		// get <a> tags
		var aTags = document.getElementsByTagName('a');
		
		var numLinksEventWasAddedTo = 0;
		
		var aTagLoopContinue = false;
		// looping through <a> tags
		for (var i = 0; i < aTags.length; i++) {
			aTagLoopContinue = false;
			
			// if id matches an include id, then attach event
			if(aTagLoopContinue){
				continue;
			}
			
			// if className matches an include class, then attach event
			if(aTagLoopContinue){
				continue;
			}
			
			// if href matches an include href, then attach event
			for(var j=0; j<hrefsToInclude.length;j++){
				if( hrefsToInclude[j] == aTags[i].href ){
					// continue outer loop
					aTagLoopContinue = true;
					
					//alert(aTags[i].id);
					this.appendListener(aTags[i],"click",fn);
					numLinksEventWasAddedTo++;
					
					// stop this loop
					break;
				}
			}
			if(aTagLoopContinue){
				continue;
			}
		}
		
		//alert(numLinksEventWasAddedTo);
	}
	
	return;
}












/*** HELPER METHODS ***/
linkAndFormTracking.appendListener=function(obj,evnt,func){	
	if (obj.addEventListener){
		obj.addEventListener(evnt,func,false);
	}
	else if(obj.attachEvent){
		obj.attachEvent("on" + evnt,func);
	}
}

linkAndFormTracking.typeOf = function(value){
	var s = typeof value;
	if (s === 'object') {
		if (value) {
			if (typeof value.length === 'number' &&
					!(value.propertyIsEnumerable('length')) &&
					typeof value.splice === 'function'){
				s = 'array';
			}
		}
		else{
			s = 'null';
		}
	}
	return s;
}

linkAndFormTracking.addLoadEvent = function(func){
	if (typeof window.onload != 'function'){
		window.onload = func;
	}
	else{
		var oldonload = window.onload;
		
		window.onload = function(){
		if (oldonload){oldonload();}
			func();
		}
	}
}

linkAndFormTracking.trimStr = function(str){
	var whitespace='\s\n\r\t\f\x0b\xa0\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u200b\u2028\u2029\u3000';
	for(var i=0;i<str.length;i++){
		if(whitespace.indexOf(str.charAt(i)) === -1){
			str = str.substring(i);
			break;
		}
	}
	for(i=str.length-1;i>=0;i--){
		if(whitespace.indexOf(str.charAt(i)) === -1){
			str = str.substring(0, i + 1);
			break;
		}
	}
	return whitespace.indexOf(str.charAt(0)) === -1 ? str : '';
}