/***************************************************************************************************
* Random functions                                                                                 *
***************************************************************************************************/
var hideDemo;
function displayPopup(_popup) {
	if (pageTracker) {
		pageTracker._trackPageview(window.location.href + '#demo');
	}
	_popup = document.getElementById(_popup);
	if(!_popup) return;
	var _fader = document.getElementById('fader');
	if(!_fader){
		_fader = document.createElement('div');
		_fader.id = 'fader';
		document.body.appendChild(_fader);
		
		// jQuery fix for select element in ie6
		$('#fader').bgiframe();
	}
	var f_h = document.getElementById('page').offsetHeight;
	if(f_h < getWindH()) f_h = getWindH();
	_fader.style.height = f_h + 'px';
	_fader.style.opacity = '0.75';
	_fader.style.MozOpacity = '0.75';
	_fader.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + 75 + ')';
	
	var _t = 200;
	if(_t < 0) _t = getCurrentYPos();
	else _t += getCurrentYPos();
	_popup.style.top = _t + 'px';
	
	_fader.style.display = 'block';
	_popup.style.display = 'block';
	
	// Write the flash to the placeholder
	var so = new SWFObject(DFF.ServerBaseUri + "flash/demo.swf?rnd="+Math.random(), "pop-up-flash-flash", "660", "280", "8");
	so.addParam("wmode", "transparent");
	so.write('pop-up-flash');
	
	// Create close button
	hideDemo = function(){
		_popup.style.display = 'none';
		_fader.style.display = 'none';
		if (window.event) { window.event.cancelBubble = true; }
		return false;
	}
	
	var btn_close = _popup.getElementsByTagName('a');
	for(var i = 0; i < btn_close.length; i++){
		if(btn_close[i].className.indexOf('btn-popup-close') != -1){
			btn_close[i].onclick = hideDemo;
		}
	}
	
	if (window.event) { window.event.cancelBubble = true; }
	return false;
}

function getWindH(){
	if (window.innerHeight) _height = window.innerHeight;
	else _height = document.documentElement.clientHeight;
	return _height;
}

function getCurrentYPos(){
	var _offset = 0;
	if (document.body && document.body.scrollTop) _offset =  document.body.scrollTop;
	if (document.documentElement && document.documentElement.scrollTop) _offset = document.documentElement.scrollTop;
	if (window.pageYOffset) _offset =  window.pageYOffset;
	return _offset;
}

function showHelpArticle(key) {
	var dd = document.getElementById('article_' + key);
	if (dd) {
		var originalDisplay = dd.style.display;
		hideHelpArticles();
		if (originalDisplay == 'none') {
			dd.style.display = '';
		} else {
			dd.style.display = 'none';
		}
	}
	if (window.event) { window.event.cancelBubble = true; }
	return false;
}

function hideHelpArticles() {
	var dds = document.getElementsByTagName('dd');
	for (var i = 0; i < dds.length; i++) {
		if (dds[i].id.match(/^article_/)) {
			dds[i].style.display = 'none';
		}
	}
}

function redirectToBank(selectElement, formElement, currentUrl) {
	if (selectElement.options[selectElement.selectedIndex].value != '0') {
		if (typeof pageTracker == 'object') {
			pageTracker._trackPageview(currentUrl+"?bank="+selectElement.options[selectElement.selectedIndex].value);
		}
		return formElement.submit();
	}
	if (window.event) { window.event.cancelBubble = true; }
	return false;
}

function showWithTheseBanksBanner() {
	// Write the flash to the placeholder
	var so = new SWFObject("./flash/banks.swf?rnd="+Math.random(), "banks-flash", "220", "163", "8");
	so.addParam("wmode", "transparent");
	so.addVariable("moreLink", "./register/");
	so.write('banks-flash');
}

function getAnchor() {
	if (window.location.hash) {
		return window.location.hash.substr(1);
	}
	return false;
}

function showMessageBox(section, alias) {
	document.getElementById('messageBox_'+section+'_'+alias).style.display = 'block';
}

function hideMessageBox(section, alias) {
	document.getElementById('messageBox_'+section+'_'+alias).style.display = 'none';
}

function getRadioValue(form, name) {
	var radios = form.getElementsByTagName('input');
	for (var i = 0; i < radios.length; i++) {
		if (radios[i].name == name && radios[i].checked) {
			return radios[i].value;
		}
	}
	return null;
}

function handleVote(form, section, alias) {
	var helpful = getRadioValue(form, 'helpful');
	switch (helpful) {
		case '1':
			voteHelpful(form, section, alias);
			window.setTimeout(
				function () {
					form.innerHTML = '<p><strong>Submitting your feedback...</strong> <img src="' + DFF.ServerBaseUri + 'images/wait.gif" alt="Waiting" /></p>';
					window.setTimeout(
						function () {
							form.innerHTML = '<p>Thank you for your feedback.</p>';
						}
					, 1500);
				}
			, 250);
			break;
		case '0':
			voteUnhelpful(form, section, alias);
			window.setTimeout(
				function () {
					form.innerHTML = '<p><strong>Submitting your vote...</strong> <img src="' + DFF.ServerBaseUri + 'images/wait.gif" alt="Waiting" /></p>';
					window.setTimeout(
						function () {
							form.innerHTML = '<p><strong>Thank you for voting.</strong></p>';
						}
					, 1500);
				}
			, 250);
			break;
		case null:
		default:
			alert('Please tell us if you found this article helpful');	
			break;
	}
	
	if (window.event) { window.event.cancelBubble = true; }
	return false;
}

function voteUnhelpful_handler(request, nothingImportant) {
	if (request.readyState == 4) {
		//alert('Thank you for voting');
	}
}

function voteUnhelpful(form, section, alias) {
	var data = new Array();
	
	data['ajax'] = '1';
	data['section'] = section;
	data['alias'] = alias;
	data['helpful'] = '0';
	data['message'] = form.getElementsByTagName('textarea')[0].value;
	
	DFF.SendData(data, voteUnhelpful_handler, 'help-centre/vote/');
	
	if (window.event) { window.event.cancelBubble = true; }
	return false;
}

function voteHelpful_handler(request, nothingImportant) {
	if (request.readyState == 4) {
		//alert('Thank you for voting');
	}
}

function voteHelpful(form, section, alias) {
	var data = new Array();
	
	data['ajax'] = '1';
	data['section'] = section;
	data['alias'] = alias;
	data['helpful'] = '1';
	
	DFF.SendData(data, voteHelpful_handler, 'help-centre/vote/');
	
	if (window.event) { window.event.cancelBubble = true; }
	return false;
}

/***************************************************************************************************
* Startup functions                                                                                *
***************************************************************************************************/
function initPage(){
	initMenu();
	//initRandom();
	initPopup();
	initImageButtons('input');
	initImageButtons('a');
	initHomeButton();
	//initIe6ZindexFix();
}

function initMenu(){
	var _menu = document.getElementById('nav');
	if(_menu){
		_menu = _menu.getElementsByTagName('li');
		for(var i = 0; i < _menu.length; i++){
			_menu[i].ind = i;
			_menu[i].onmouseover = function(){
				if(this.ind > 0){
					if(_menu[this.ind - 1].className.indexOf('active') != -1) this.className += ' a-prev';
				}
				if(this.ind < _menu.length - 1){
					if(_menu[this.ind + 1].className.indexOf('active') != -1) this.className += ' a-next';
				}
				if(this.className.indexOf('hover') == -1) this.className += ' hover';
			}
			_menu[i].onmouseout = function(){
				if(this.className.indexOf('a-prev') != -1){ this.className = this.className.replace('a-prev','');}
				if(this.className.indexOf('a-nex') != -1){ this.className = this.className.replace('a-next','');}
				this.className = this.className.replace('hover','');
			}
		}
	}
}

function initRandom(){
	var t_box = document.getElementsByTagName('div');
	var _box = [];
	for(var i = 0; i < t_box.length; i++){
		if(t_box[i].className.indexOf('img-holder') != -1){
			_box.push(t_box[i]);
		}
	}
	if(_box.length > 0){
		var _a = Math.floor(Math.random()*_box.length);
		for(var i = 0; i < _box.length; i++){
			if(i == _a) _box[i].style.display = 'block';
			else _box[i].style.display = 'none';
		}
	}
}

function initPopup(){
	var _btn = document.getElementsByTagName('a');
	for(var i = 0; i < _btn.length; i++){
		if(_btn[i].className.indexOf('btn-demo-popup') != -1){
			_btn[i].onclick = function(){
				return displayPopup('pop-up');
			}
		}
	}
}

function initImageButtons(tag) {
	var inputs = document.getElementsByTagName(tag);
	for (var i=0; i < inputs.length; i++) {
		if (((tag == 'input' && inputs[i].type == 'image') || (tag == 'a')) && (inputs[i].className.indexOf('image_button') != -1)) {
			inputs[i].onmousedown = function () {
				this.style.backgroundPosition = 'left bottom';
			}
			inputs[i].onmouseup = function () {
				this.style.backgroundPosition = 'left top';
			}
			inputs[i].onmouseout = inputs[i].onmouseup;
		}
	}
}

function initHomeButton() {
	//find_out_more.swf
	var toReplace = document.getElementsByTagName('a');
	for (var i = 0; i < toReplace.length; i++) {
		if (toReplace[i].className == 'find-out') {
			var so = new SWFObject("./flash/find_out_more.swf?rnd="+Math.random(), "find_out_more", "142", "38", "8");
			so.addParam("wmode", "transparent");
			so.addVariable("btnLink", "./services/");
			var div = document.createElement('div');
			toReplace[i].parentNode.appendChild(div);
			if (so.write(div)) {
				toReplace[i].style.display = 'none';
			}
		}
	}
}

function initIe6ZindexFix() {
	alert($('body').bgiframe());
}

if (window.addEventListener) {
	window.addEventListener("load", initPage, false);
} else if (window.attachEvent) {
	window.attachEvent("onload", initPage);
}

