
/***************************************************************************************************
 * ReplaceInStr
 *
 * A search and replace function for Javascript strings.
 *
 * str:			The string to search through.
 * search_char:	The character or string to search for in the search string.
 * new_text:	The character or string to replace search_char with in str.
 *
 * Returns:		A new string with all of the substitutions made.
 *
 */
 
function ReplaceInStr(str, search_char, new_text)
{
	var new_str;
	var temp;
	var char_index;

	new_str = str;
	char_index = new_str.indexOf(search_char);
	
	while (char_index != -1)
	{
		temp = new_str.substring(char_index + 1, new_str.length);
		new_str = new_str.substring(0, char_index);
		new_str = new_str + new_text;
		new_str = new_str + temp;
		
		char_index = new_str.indexOf(search_char);
	}
	
	return new_str;
}


/***************************************************************************************************
 * Detect
 *
 * Event handler for when a 'href' is clicked on. This will be registered for every link on the 
 * page. Once a click has been caught, the code tests to see if it is an external link and if so
 * forwards the link onto the stats recording page to be noted and redirected.
 *
 * e: The href link that has been clicked on
 *
 */

function Detect(e)
{
	// DEBUG: alert("Starting Link Detection");

	var localSites = new Array(
							"www.accomodationguide.com.au", 
							"www.accommodationguide.com.au",
							"aaat.huonassociates.com.au",
							"tol.aaatourism.com.au",
							"http://210.50.194.37",
							"http://210.50.194.38",
							"http://210.50.194.39",
							"http://localhost",
							"javascript:",
							"Javascript:",
							"mailto:");
	if (e) 
		tg = e.target;
	else 
		tg = window.event.srcElement;
	
	while (tg.nodeName != 'A' )
		tg = tg.parentNode;
	
	var urlLink = tg.href;
	var counter = 0;
	var internal_link = false;
	
	for (counter = 0; counter < localSites.length; counter++)
	{
		if (urlLink.indexOf(localSites[counter]) != -1)
			internal_link = true;
	}
	
	
	//if (internal_link != true)
	//{
	//	{
			// The local host does not need these substitutions.
		
	//		urlLink = ReplaceInStr(urlLink, "/", "%2F");
	//		urlLink = ReplaceInStr(urlLink, "?", "%3F");
	//	}
	//	
	//	urlLink = "/sites/global/includes/openwebsitelink.asp?url=" + urlLink;
	//	
	//	window.location=urlLink;
		// DEBUG: alert(urlLink);
	//}
	
	// Check to see if it is an email.
	
	if (urlLink.indexOf("mailto:") != -1)
	{
		var email = urlLink.toLowerCase();
		var emailURL;
		var CC;
		var subject;
		
		emailURL = ExtractToken(email, "mailto:");
		
		subject = ExtractToken(email, "?subject=");
		
		if (subject != "")
		{
			emailURL = emailURL + "&esub=" + subject;
		}
		else
		{
			subject = ExtractToken(email, "&subject=");
			
			if (subject != "")
				emailURL = emailURL + "&esub=" + subject;
		}
		
		CC = ExtractToken(email, "&cc=");
		
		if (CC != "")
		{
			emailURL = emailURL + "&ecc=" + CC;
		}
		else
		{
			CC = ExtractToken(email, "?cc=");
			
			if (CC != "")
				emailURL = emailURL + "&ecc=" + CC;
		}

		urlLink = "/sites/global/includes/sendemail.asp?eurl=" + emailURL;

		// DEBUG: alert("orig: " + email + "\n" 
				//+ "email: '" + emailURL + "'\n" 
				//+ "subject: '" + subject + "'\n"
				//+ "CC: '" + CC + "'" + "\n"
				//+ "Final: '" + urlLink + "'");
			
		window.location=urlLink;
	}
}

function ExtractToken(str, token)
{
	var value = "";
	var start = -1;
	var end = -1;
		
	start = str.indexOf(token);
	
	if (start != -1)
	{
		start = start + token.length;
		end = FindFirstToken(str, start);	
	
		if (end == -1)
		{
			end = str.length;
		}
		
		value = str.substring(start, end);
	}
		
	return value;
}

function FindFirstToken(str, startPos)
{
	var amp = str.indexOf('&', startPos);
	var question_mark = str.indexOf('?', startPos);
	
	if (amp == -1 && question_mark == -1)
		return -1;
		
	if (amp == -1)
		return question_mark;
	else if (question_mark == -1)
		return amp;
	else if (question_mark < amp)
		return question_mark;
	else if (amp < question_mark)
		return amp;
	else
		return -1;
}

/***************************************************************************************************
 * CreateLinkWatches
 *
 * Registers the Detect function as the event handler for all of the 'hrefs' on the page.
 *
 */

function CreateLinkWatches()
{
	var a = document.getElementsByTagName('A');

	// DEBUG: alert(a.length + " links found");

	for (var i = 0; i < a.length; i++)
		a[i].onclick = Detect;
}

/***************************************************************************************************
 * Init
 *
 * Runs all of the setup code once the page has finished loading.
 *
 */

Init=function()
{
	CreateLinkWatches();
}

// Execute code once page is loaded.

window.onload=Init;
