/*!
 * Utility Javascript File for GTS
 * "util.js"
 *
 */
String.prototype.encode = function()
{
	return escape(this.replace(/'/g,"[q]").replace(/"/g,"[qq]"));
}
	
String.prototype.decode = function()
{
	return unescape(this).replace(/%q%/g,"'").replace(/%qq%/g,"\"");
}


/*!
 * Retired functions - replaced - emptied to prevent use
 */
function initMenu()
{
	// Doing NOTHING!
}

function doCrumbs()
{
	// Doing NOTHING!
}

function addLoadEvent()
{
	// Doing NOTHING!
}

function gotocountry()
{
	// Doing NOTHING!
}


var expireEvents = "";

/*!
 * Old functions - don't know if still in use
 */
// pop-up windows //
function showPopUp(file, width, height)
{
	var new_win = window.open(file, 'new_win', ('HEIGHT=' + height + ',WIDTH=' + width + ',location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,status=yes'));
}

function agendaPop(file, width, height)
{
	var new_win = window.open(file, 'new_win', ('HEIGHT=' + height + ',WIDTH=' + width + ',location=no,menubar=no,resizable=yes,scrollbars=yes,toolbar=no,status=yes'));
	
	new_win.print();
}

// collapsable menus //
function showsublist(item) {
	var xdisplay = document.getElementById(item).style.display;
	document.getElementById(item).style.display = ((xdisplay == "block") ? "none" : "block");

	var aimage = document.getElementById(item).parentElement.style.listStyleImage;
	document.getElementById(item).parentElement.style.listStyleImage = ((aimage == "url(/transactionservices/home/img/carat_open.gif)") ? "url(/transactionservices/home/img/carat_closed.gif)" : "url(/transactionservices/home/img/carat_open.gif)");
}

// clear text in form field upon focus //
function clearText(field){
	if (field.defaultValue == field.value) field.value = '';
	else if (field.value == '') field.value = field.defaultValue;
}

// toggle display of object //
function showDiv(sec)
{
	if(document.getElementById(sec).style.display == 'none')
	{
		document.getElementById(sec).style.display = 'inline';
	}
	else
	{
		document.getElementById(sec).style.display = 'none';
	}
}

// Left Menu Functions //
function returnDocument()
{
	var file_name = document.location.href;
	var end = (file_name.indexOf("?") == -1) ? file_name.length : file_name.indexOf("?");
	return file_name.substring(file_name.lastIndexOf("/")+1, end);
}


/*!
 * Known to be used functions
 */
function urlParser(uri)
{
	var urlPattern=new RegExp("^[a-z]{4,5}://[A-Za-z0-9.]+/{1}");
	
	this.raw = document.URL;
	this.server = urlPattern.exec(this.raw);
	this.subDirs = this.raw.replace(this.server,"").split("/");
	this.file = this.subDirs[this.subDirs.length - 1];
	
	this.match = function (query)
	{
		if (this.raw.indexOf(query) >= 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
}

function here()
{
	this.uri = new urlParser(document.URL);
	
	if (document.referrer != "")
	{
		this.ref = new urlParser(document.referrer);
		this.hasRef = true;
	}
	else
	{
		this.ref = "";
		this.hasRef = false;
	}
}

/* Client-side access to querystring name=value pairs
*/
function Querystring(qs) { // optionally pass a querystring to parse
	this.params = {};
	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;

	// Turn <plus> back to <space>
	// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
	
	// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}
Querystring.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}
Querystring.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}

function isEven(num) {
	return !(num % 2);
}

//---------Eugene Galper added (copied it from navigation.js to fix region section country dropdown script ------ *************
/* 
function gotocountry()
{
	var URL = document.regiondropdown.site.options[document.regiondropdown.site.selectedIndex].value;
	var myCountry = document.regiondropdown.site.options[document.regiondropdown.site.selectedIndex].innerHTML;
	
	setCountry(myCountry);
	
	window.location.href = URL;
}
*/

/*!
 * EVERYTHING BELOW HERE IS DEPENDENT ON JQUERY
 * --------------------------------------------
 * jQuery is currently being loaded (minified) via "breadcrumb.js".
 * "breadcrumb.js" must load BEFORE this file or the functions will
 * not work.
 * 
 * Everything has been wrapped so that it should fire even if
 * jQuery is put into "no conflict" mode.
 */

/*!
 * Extensions
 */
jQuery(function($) {
	$.synchMenu = function(element)
	{
		menu_main = element + "_main";
		menu_sub = element + "_sub";
		
		$(menu_main).addClass("selected");
		$(menu_sub).removeClass("hide");
	}
	
	$.Cookie = function()
	{
		this.create = function(name,value,days)
		{
			if (days) {
				var date = new Date();
				date.setTime(date.getTime()+(days*24*60*60*1000));
				var expires = "; expires="+date.toGMTString();
			}
			else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		}
		
		this.read = function(name)
		{
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		}
		
		this.erase = function(name) {
			this.create(name,"",-1);
		}
		
		this.test = function()
		{
			var name = "CookieTest";
			
			try
			{
				this.create(name,'true',1);
				
				if (this.read(name) == "true")
				{
					this.erase(name);
					return true;
				}
			}
			catch(e)
			{
				return false;
			}
		}
	}
	
	$.getLocation = function()
	{
		var Cookie = new $.Cookie();
		
		try
		{
			return unescape(Cookie.read("UserLocation"));
		}
		catch(e)
		{
			return null;
		}
	}
	
	$.setLocation= function(here)
	{
		var Cookie = new $.Cookie();
		
		if (Cookie.test())
		{
			Cookie.create("UserLocation",escape(here),1);
			return here;
		}
		else
		{
			return here;
		}
	}
	
	$.getCountry = function()
	{
		var Cookie = new $.Cookie();
		
		try
		{
			return unescape(Cookie.read("UserCountry"));
		}
		catch(e)
		{
			return null;
		}
	}
	
	$.setCountry = function(here)
	{
		var Cookie = new $.Cookie();
		
		if (Cookie.test())
		{
			Cookie.create("UserCountry",escape(here.replace(/ /g,"_")),1);
			return here;
		}
		else
		{
			return here;
		}
	}
	
	$.getClientType = function()
	{
		var Cookie = new $.Cookie();
		
		try
		{
			return unescape(Cookie.read("ClientType"));
		}
		catch(e)
		{
			return null;
		}
	}
	
	$.setClientType = function(here)
	{
		var Cookie = new $.Cookie();
		
		if (Cookie.test())
		{
			Cookie.create("ClientType",escape(here),1);
			return here;
		}
		else
		{
			return here;
		}
	}
	
	$.gotocountry = function()
	{
		var URL = document.regiondropdown.site.options[document.regiondropdown.site.selectedIndex].value;
		var myCountry = document.regiondropdown.site.options[document.regiondropdown.site.selectedIndex].innerHTML;
		
		$.setCountry(myCountry);
		
		window.location.href = URL;
	}

	
	$.fn.extend({
		hasSiblings: function(elems)
		{
			return this.each (function()
			{
				if (elems !== undefined)
				{
					if ($(this).siblings(elems).length < 1)
						return false;
					else
						return true;
				}
				else
				{
					if ($(this).siblings.length < 1)
						return false;
					else
						return true;
				}
			});
		},
		
		exists: function()
		{
			if ($(this).length > 0)
				return true;
			else
				return false;
		},
		
		expire: function(expiration)
		{
			return this.each (function() {
				var datePattern=new RegExp("[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]");
				var numberPattern = new RegExp(/^0[1-9]/);
				var today = new Date();
				var expires = new Date();
				
				if (!(datePattern.test(expiration)))
					expiration = $(this).attr("title");
				
				if (datePattern.test(expiration))
				{
					var arrDate = expiration.split("/");
					
					for(var i=0;i<arrDate.length;i++)
					{
						if (numberPattern.test(arrDate[i]))
							arrDate[i] = arrDate[i].match(numberPattern)[0].substr(1);
					}
					
					var iMonth = parseInt(arrDate[0]) - 1;
					var iDay = parseInt(arrDate[1]);
					var iYear = parseInt(arrDate[2]);
					
					expires.setFullYear(iYear,iMonth,iDay);
					
					if (expires < today)
					{
						$(this).hide().addClass("expired");
						return true;
					}
				}
				return false;
			});
		},
		
		stripe: function(cssclass)
		{
			return this.each (function(i) {
				if(!(i % 2))
				{
					//alert(cssclass);
					$(this).addClass(cssclass);
				}
			});
		}
	});
	
	$.fn.initialize = function()
	{
		return this.each (function() {
			var page = document.location.href;
			var nav = $(this);
			
			nav.find("li a:only-child").addClass("none");
			/*
			nav.find("li a[href='" + page + "']").each(function() {
				if (!($(this).hasClass("none")) && !($(this).hasSiblings("ul")))
					$(this).addClass("closed");
				
				$(this).parentsUntil(nav.attr("id")).addClass("open");
			});
			*/
			nav.find("li a").each(function() {
				if ($(this).attr("href").charAt(0) == "#")
				{
					$(this).attr("onclick", "");
					
					$(this).click(function() {
						$(this).parent().toggleClass("open");
					});
				}
				else
				{
					if (page.indexOf($(this).attr("href")) > -1)
					{
						if (!($(this).hasClass("none")) && !($(this).hasSiblings("ul")))
							$(this).addClass("closed");
						else
							$(this).parentsUntil(nav.attr("id")).addClass("open");
					}
				}
			});
		});
	}
	
	$.fn.dropRegionNav = function()
	{
		return this.each (function() {
			$(this).change(function() {
				var selectedCountry = $(this).find("option:selected");
				var countryURL = selectedCountry.val();
				var countryName = selectedCountry.text();
				
				$.setCountry(countryName);
				
				window.location.href = countryURL;
			});
		});
	}
	
	/*
	 *  Fancy Box Functions
	*/
	$.fn.fancybox = function(boxType)
	{
		if (boxType == null)
			boxType = "expander";
		
		return this.each (function() {
			$(this).hide();
			
			switch (boxType)
			{
				case "globe":
					$(this).each(function(i) {
						var $box_text = $(this).html();
						$(this).html("<div class='section1'><div class='section2'><div class='section3'><div class='section4'>" + $box_text + 
						  "</div></div></div></div>");
					});
					break;
				
				case "small":
					$(this).each(function(i) {
						var $box_text = $(this).html();;
						$(this).html("<div class='section1'><div class='section2'><div class='section3'><div class='section4'>" + $box_text + 
						  "</div></div></div></div>");
					});
					break;
				
				case "title":
					$(this).each(function(i) {
						var $header_text = "";
						var $body_text = "";
						
						$(this).find("h3").each(function(i) {
							$header_text = $(this).html();
						});
						$(this).find("div").each(function(i) {
							$body_text = $(this).html();
						});
						
						$(this).html("<div class='section1'><h3>" + $header_text + "</h3></div><div class='section2'><div class='section2a'>" + $body_text + "</div><div class='section2b'></div></div>");
					});
					break;
				
				case "title_auto":
					$(this).each(function(i) {
						var $header_text = "";
						var $box_text = "";
						
						$(this).find(".box_heading").each(function(i) {
							$header_text = $(this).html();
							$(this).html("<div class='section1'><div class='section2'>" + $header_text + "</div></div>");
						});

						$(this).find(".box_text").each(function(i) {
							$box_text = $(this).html();
							$(this).html("<div class='section3'><div class='section4'><div class='section5'><div class='section6'>" + $box_text + "</div></div></div></div>");
						});
					});
					break;
				
				default: // Expander
					$(this).each(function(i) {
						var $trigger_text = "";
						var $toggle_text = "";
						
						$(this).find(".trigger").each(function(i) {
							$trigger_text = $(this).html();
							$(this).addClass("section1").html("<a href='javascript:void()'><div class='section2'><div class='section3'><div class='section4'>" + $trigger_text + "</div></div></div></a>")
						});
						$(this).find(".toggle_container").each(function(i) {
							$toggle_text = $(this).html();
							$(this).html("<div class='section5'><div class='section6'><div class='section7'><div class='section8'>" + $toggle_text + "</div></div></div></div>");
						});
						
						switch($(this).attr('class'))
						{
							case "expander faq" || "faq expander" :
							$(this).addClass("a_box_auto2");
  							break;
							case "expander expand2" || "expand2 expander":
							$(this).addClass("expand_auto2");
  							break;
							case "expander expand3" || "expand3 expander":
							$(this).addClass("expand_auto3");
  							break;
							default:
							$(this).addClass("expand_auto");
						}
						
						
						
						
						
	//					if ($(this).hasClass("faq"))
	//					{
	//						$(this).addClass("a_box_auto2");
	//					} 
	//					else if ($(this).hasClass("expand2"))
	//					{
	//						$(this).addClass("expand_auto2");
		//				} else {
		//					$(this).addClass("expand_auto");
	//					}
						
						
						
						$(this).find(".toggle_container").hide();
						
						$(this).find("div.trigger").toggle(function(){
							$(this).addClass("active"); 
						  }, function () {
							$(this).removeClass("active");
						});
						
						$(this).find("div.trigger").click(function(){
							$(this).next(".toggle_container").slideToggle("fast,");
						});
						
					});
			}
			
			$(this).show();
		});
	}
	
	$.fn.scrollbox = function()
	{
		return this.each (function() {
			$(this).find(".scrollable").each(function() {
				$(this).find(".items div").find(".item").each(function(i) {
					$(this).click(function() {
						$(this).parents(".scrollable").prev().find("div.on").fadeOut().removeClass("on");
						$(this).parents(".items").find(".item").each(function() {
							$(this).removeClass("active");
						});
						
						$(this).parents(".scrollable").prev(".panes").find("div:eq(" + i + ")").fadeIn().addClass("on");
						$(this).addClass("active");
					});
				});
				
				$(this).find(".item:first").addClass("active");
			});
			
			$(this).find(".panes div:first").fadeIn().addClass("on");
			$(this).find(".scrollable .item:first").addClass("active");
		});
	}
	
	
	/*!
	 * Onload functions
	 */
	$("#nav").initialize();
	$("#rcolumn form[name='regiondropdown'] select").dropRegionNav();
	
	$(".expander").fancybox();
	$(".globe_box_auto").fancybox("globe");
	$(".smallbox").fancybox("small");
	$(".title_box_210").fancybox("title");
	$(".title_box_auto").fancybox("title_auto");
	$(".articlebox").scrollbox();
	
	if ($("#EventTable").exists())
	{
		$("#EventTable tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTable tr[title]:visible").stripe("bggray1");
		
		$("#EventTable").show();
	}
	
	
	if ($("#EventTableJan").exists())
	{
		$("#EventTableJan tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableJan tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableJan").show();
	}
	
	if ($("#EventTableFeb").exists())
	{
		$("#EventTableFeb tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableFeb tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableFeb").show();
	}
	
	if ($("#EventTableMar").exists())
	{
		$("#EventTableMar tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableMar tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableMar").show();
	}
	
	if ($("#EventTableApr").exists())
	{
		$("#EventTableApr tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableApr tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableApr").show();
	}
	
	if ($("#EventTableMay").exists())
	{
		$("#EventTableMay tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableMay tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableMay").show();
	}
	
	if ($("#EventTableJun").exists())
	{
		$("#EventTableJun tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableJun tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableJun").show();
	}
	
	if ($("#EventTableJul").exists())
	{
		$("#EventTableJul tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableJul tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableJul").show();
	}
	
	if ($("#EventTableAug").exists())
	{
		$("#EventTableAug tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableAug tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableAug").show();
	}
	
	if ($("#EventTableSep").exists())
	{
		$("#EventTableSep tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableSep tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableSep").show();
	}
	
	if ($("#EventTableOct").exists())
	{
		$("#EventTableOct tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableOct tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableOct").show();
	}
	
	if ($("#EventTableNov").exists())
	{
		$("#EventTableNov tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableNov tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableNov").show();
	}
	
	if ($("#EventTableDec").exists())
	{
		$("#EventTableDec tr[title]").each(function() {
			$(this).expire();
		});
		
		$("#EventTableDec tr[title]:visible").stripe("bgwhite");
		
		$("#EventTableDec").show();
	}
	
	
});


// Breadcrumb Functions
jQuery(function($) {
	var delim = '|';
	var trailLength = 5;
	var textLimit = 50;
	
	var HERE = document.location;
	var TITLE = document.title;
	
	function setCrumbs()
	{
		var Cookie = new $.Cookie();
		
		if(Cookie.test())
		{
			crumbList = new CrumbList();
			crumbList.generate();
		}
	}
	
	function truncText(strText)
	{
		if (strText.length > textLimit)
		{
			//return strText.slice(0, textLimit).slice(0, strText.lastIndexOf(" ")) + "...";
			var strSlice = strText.slice(0, textLimit)
			
			while (strSlice.charAt(strSlice.length - 1) != " ")
			{
				strSlice = strSlice.slice(0, strSlice.length - 1);
			}
			
			return strSlice = strSlice.slice(0, strSlice.length - 1) + "...";
		}
		else
		{
			return strText;
		}
	}
	
	
	function CrumbList()
	{
		this.links = new Array();
		this.texts = new Array();
		
		this.rawLinks = new String;
		this.rawTexts = new String;
		
		var Cookie = new $.Cookie();
		
		this.generate = function()
		{
			if(Cookie.read('TrailLinks')) //If the cookie exists
			{
				this.rawLinks = unescape(Cookie.read("TrailLinks"));
				this.rawTexts = Cookie.read("TrailText").decode();
				
				this.links = this.rawLinks.split(delim);
				this.texts = this.rawTexts.split(delim);
			}
			
			this.add();
			this.dropCrumbs();
		}
		
		this.add = function ()
		{
			if (this.links[this.links.length-1] != HERE)
			{
				if (this.check() < 0)
				{
					this.links.push(HERE);
					this.texts.push(truncText(TITLE));
				}
				else
				{
					this.links.splice(this.check()+1, this.links.length-this.check());
					this.texts.splice(this.check()+1, this.texts.length-this.check());
				}
			}
		}
		
		this.check = function ()
		{
			var iCounter = 0;
			
			while (iCounter < this.links.length)
			{
				if (this.links[iCounter] == HERE)
				{
					return iCounter;
				}
				
				iCounter++
			}
			
			return -1;
		}
		
		this.dropCrumbs = function ()
		{
			var crumbString = "<a href='/transactionservices/home/'>Home</a>";
			
			if (this.links.length > trailLength)
			{
				this.links.shift();
				this.texts.shift();
			}
			
			for(var iLink in this.links)
			{
				if (iLink <= trailLength)
				{
					if(iLink == this.links.length-1)
					{
						crumbString = crumbString + "&nbsp;&gt;&nbsp;" + this.texts[iLink];
					}
					else
					{
						crumbString = crumbString + "&nbsp;&gt;&nbsp;<a href='" + this.links[iLink] + "'>" + this.texts[iLink] + "</a>";
					}
				}
			}
			
			$("#breadCrumb").html(crumbString);
			
			Cookie.erase("TrailLinks");
			Cookie.erase("TrailText");
			
			Cookie.create("TrailLinks",escape(this.links.join(delim)),1);
			Cookie.create("TrailText",this.texts.join(delim).encode(),1);
			
			this.links = [];
			this.texts = [];
			this.rawLinks = "";
			this.rawTexts = "";
		}
	}
	
	setCrumbs();
});
