<!-- Update donation wizard link -->


/*** Constraints
- All tags are case sensitive (source/kw/c1 must be lowercase)
- Source and c1 variables must be 20 characters or less and the kw tag must be 50 characters or less.
- Only alphanumerics allowed
***/
function updateAALinks() {
	// Setup vars
	var ref = "nsal";								// Referring org
	var c1 = getURLVar("c1", "donatenow");				// Campaign name
	var kw = getURLVar("kw", "");						// Keywords
	var source = getURLVar("source", "");				// Source of traffic
	
	var referrer = document.referrer;					// Referrer to this page
	var searchEngineArray = [						// Multi dim array containing search engines and their associated GET paramters for seach keywords
		    ['google\.com', 'Google', 'q'],                // Google
		    ['web\.search\.yahoo\.com', 'Yahoo', 'p'],    // Yahoo
		    ['search\.yahoo\.com', 'Yahoo', 'p'],    	// Yahoo
		    ['search\.msn', 'MSN', 'q'],                	// MSN
		    ['live\.com', 'Live', 'q'],               	 // Live
		    ['search\.aol', 'AOL', 'query'],        		// AOL
		    ['ask\.com', 'ASK', 'q'],                     // Ask.com
		    ['altavista\.com', 'Altavista', 'q']		// AltaVista
		];

	// Set referrer & query string
	var matches = new Array();						// Will hold match, if occurs
	var re = new RegExp();							// Holds regex
	// Loop over each supported search engine in array and see if match exists
	for(var i=0; i<searchEngineArray.length; i++) {	
		re = new RegExp(searchEngineArray[i][0], 'i');	// Set regex
		matches = referrer.match(re);					// Exec search
		
		
		// If there was a match then set referrer & keywords
		if( matches != null && matches.length > 0 ) {			
			source = searchEngineArray[i][1].substring(0, 19);					// Set source to first 20 chars of SE name
			kw = getURLVar(searchEngineArray[i][2], "", referrer).substring(0,49);	// Get the keywords used in the search
			break;
		}else{
			source = '';
			kw = '';
		}
	}
	
	// Loop over links and update as necessary
	for(var i=0; i<document.links.length; i++) {
		// Check for and update donate wizard links
		if( document.links[i].href.match(/^http:\/\/www\.donationwizard\.com\/donate\.php/) ) {
			document.links[i].href = "http://www.donationwizard.com/donate.php?ref="+escape(ref)+"&c1="+escape(c1)+"&source="+escape(source)+"&kw="+escape(kw);
		}
		
		// Check for and update links to related pages
		if( document.links[i].href.match(/^http:\/\/www\.nsalamerica\.org\/animalautos\/donation_tips.html/) ) {
			document.links[i].href = "http://www.nsalamerica.org/animalautos/donation_tips.html?ref="+escape(ref)+"&c1="+escape(c1)+"&source="+escape(source)+"&kw="+escape(kw);
		}
		if( document.links[i].href.match(/^http:\/\/www\.nsalamerica\.org\/animalautos\/tax_benefits.html/) ) {
			document.links[i].href = "http://www.nsalamerica.org/animalautos/tax_benefits.html?ref="+escape(ref)+"&c1="+escape(c1)+"&source="+escape(source)+"&kw="+escape(kw);
		}
		if( document.links[i].href.match(/^http:\/\/www\.nsalamerica\.org\/animalautos\/faq.html/) ) {
			document.links[i].href = "http://www.nsalamerica.org/animalautos/faq.html?ref="+escape(ref)+"&c1="+escape(c1)+"&source="+escape(source)+"&kw="+escape(kw);
		}
	}
}





// Return URL variables - support function
function getURLVar(urlVarName, defaultValue, externalURL) {
	var urlHalves = '';
	
	// If an externalURL is passed then use that instead of document.location
	if( typeof(externalURL) != "undefined") {
		urlHalves = String(externalURL).split('?');
	}else{
		urlHalves = String(document.location).split('?');
	}
		
	//divide the URL in half at the '?'
	var urlVarValue = '';
	if(urlHalves[1]){
		//load all the name/value pairs into an array
		var urlVars = urlHalves[1].split('&');
		//loop over the list, and find the specified url variable
		for(i=0; i<=(urlVars.length); i++){
			if(urlVars[i]){
				//load the name/value pair into an array
				var urlVarPair = urlVars[i].split('=');
				if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
					//I found a variable that matches, load it's value into the return variable
					urlVarValue = urlVarPair[1];
				}
			}
		}
	}
	
	if( urlVarValue == "" ) {
		urlVarValue = defaultValue;
	}
	
	return urlVarValue;   
}