// JavaScript Document
var transDiv = null;
var onloadCalled = false;

function doOnLoad()
{
	onloadCalled = true;
	
	commonPageLoad();
	
	if (typeof pageShowOrLoad == 'function')
		pageShowOrLoad(false, arguments);
		
	/*$('div.confim_password').hide();
	$('div.new_password').hide();
	$('div.password_error').hide();*/
}

function doPageShow()
{
	if (onloadCalled)
	{
		onloadCalled = false;
		return;
	}
	
	if (typeof pageShowOrLoad == 'function')
		pageShowOrLoad(true, arguments);
}

function commonPageLoad()
{
	//Look for all external links
	var links = document.getElementsByTagName('a');
	if (links.length > 0)	/* Ensure there are any links found at all */
	{
		for (var r = 0; r < links.length; r++)
		{
			/* Calculate target to know whether to add the message on or not */
			var target = document.getElementsByTagName('a').item(r).target;
			if(target == '_blank')
			{
				//Append onclick event
				var el = document.getElementsByTagName('a').item(r);
				
				el.onclick = function() {return externalLink();};
			}
		}
	}
}

function externalLink()
{
	return confirm("You are now leaving the regulatory site of Flexible Health Insurance Brokers Ltd. Flexible Health is not responsible for the accuracy of the information contained within the linked site.");
}

/* Used in conjunction with the keyup event */
function delayTimer(delay)
{
	var timer;
	return function(fn)
	{
		timer=clearTimeout(timer);
		if(fn)
		timer=setTimeout(function()
		{
			fn();
		},
		delay);
	  return timer;
	 }
}

function cleanCurrency(value)
{
	regexp = new RegExp(/[^0-9\.]/g);
	value = value.replace(regexp, '');
	regexp = new RegExp(/\.[0-9]$/);
	
	if (regexp.test(value))
	{
		value = value + '0';
	}
	
	regexp = new RegExp(/\.$/);
	
	if (regexp.test(value))
	{
		value = value + '00';
	}
	
	var iDecimalIndex = value.indexOf('.');
	
	if (iDecimalIndex > -1)
	{
		if (iDecimalIndex+3 < value.length)
		{
			value = value.substr(0, iDecimalIndex + 3);
		}
		else if (iDecimalIndex == value.length)
		{
			value += '00';
		}
	}
	
	return value;
}

function currencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function hide(id, mode, callback)
{
	var e = document.getElementById(id);
	
	if (e != null)
	{
		if (mode == undefined || mode == 'none')
			$("#" + id).hide('0', callback);
		else if (mode == "slide")
			$("#" + id).slideUp('normal', callback);
		else if (mode == "fade")
			$("#" + id).fadeOut('normal', callback);
	}
}

function show(id, mode, callback)
{
	var e = document.getElementById(id);
	
	if (e != null)
	{
		if (mode == undefined || mode == 'none')
			$("#" + id).show('0', callback);
		else if (mode == "slide")
			$("#" + id).slideDown('normal', callback);
		else if (mode == "fade")
			$("#" + id).fadeIn('normal', callback);
	}
}

function toggle(id, mode, callback)
{
	var e = document.getElementById(id);
	
	if (e.style.display == "none")
		show(id, mode, callback);
	else
		hide(id, mode, callback);
}



function getXmlValue(xmlDoc, tagName, defaultVal)
{
	var node = xmlDoc.getElementsByTagName(tagName)[0];
	
	if (defaultVal == undefined)
		defaultVal = "";
	
	if (node == null)
		return defaultVal;
	
	if (node.childNodes.length > 0)
		return node.childNodes[0].nodeValue;
	else
		return defaultVal;
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};
