
// Cross-browser event handling, by Scott Andrew
function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

// Clear Default Text: functions for clearing and replacing default text in <input> elements.
function cleardefaulttext() {
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        
        if (theInput.type == 'text' && theInput.className.match(/\bcleardefault\b/)) {  
            /* Add event handlers */          
            addEvent(theInput, 'focus', clearDefaultText, false);
            addEvent(theInput, 'blur', replaceDefaultText, false);
            
            /* Save the current value */
            if (theInput.value != '') {
                theInput.defaultText = theInput.value;
            }
        }
    }
}

function clearDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) {
        target.value = '';
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        target.value = target.defaultText;
    }
}

/* Needs Question Box script. Hiding question box on page load already, then have swapping function. */
document.write('<style type="text/css">#needsQuestionBox{display:none;}</style>');

function needsQuestionBox() {
	if (!$('needsTextBox')) return;
	if (!$('needsQuestionOpen')) return;
	var questionBoxOpen = $('needsQuestionOpen');
	var questionBoxClose = $('needsQuestionClose');
	var needsTextBox = $('needsTextBox');
	var needsQuestionBox = $('needsQuestionBox');

    if (questionBoxOpen.tagName == 'A'){
	    questionBoxOpen.onclick = function(){
		    needsTextBox.style.display = 'none';
		    needsQuestionBox.style.display = 'block';
	    }
	}
	questionBoxClose.onclick = function(){
		needsTextBox.style.display = 'block';
		needsQuestionBox.style.display = 'none';
	}
}

Event.observe(window, 'load', needsQuestionBox, false);

/* Scripts to hide calculator and show text on Guide-me pages */
function closeCalculator() {
	if (!$('flashcontent')) return;
	if (!$('flashincomplete')) return;
	var flashCalculatorOverlay = $('flashcontent');
	var flashIncompleteText = $('flashincomplete');
	flashCalculatorOverlay.style.visibility = 'hidden';
	flashIncompleteText.style.display = 'block';
}

function closeCalculatorComplete() {
	if (!$('flashcontent')) return;
	if (!$('flashcomplete')) return;
	var flashCalculatorOverlay = $('flashcontent');
	var flashCompleteText = $('flashcomplete');
	flashCalculatorOverlay.style.visibility = 'hidden';
	flashCompleteText.style.display = 'block';
}

/* Fixes positioning issues with Safari on Needs Calculator Pages */
function calculatorPosition() {
	if (!$('flashcontent')) return;
	if (!$('flashcomplete')) return;
	if (!$('flashincomplete')) return;
	var flashObject = document.getElementsByTagName('object')[0];
	if (flashObject) {
		var flashcontentSafari = $('flashcontent');
		flashcontentSafari.style.position = 'absolute';
	}
}
Event.observe(window, 'load', calculatorPosition, false);

Event.observe(window, 'load', cleardefaulttext, false);

/* sifr function */

function doSifr(){
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({sSelector:"#content h2", sFlashSrc:"http://www.makesenseofit.com/flash/din.swf", sColor:"#454545", sWmode:"transparent"}));
	};
}

String.prototype.nthIndexOf = function(searchStr, n) {
	var len = this.length;
	var foundCount = 0;
	for (i=0; i<len; i++) {
		subStr = this.substring(i, len);
		if (subStr.indexOf(searchStr) == 0) {
			foundCount ++;
			if (foundCount == n) {
				return i;
			}
		}
	}
	return -1;
}
String.prototype.baseUrl = function() {
	var endOfBase = this.nthIndexOf('/', 3);
	return this.substring(0, endOfBase);
}


/* 
Exclude these urls from sirf initialisation to prevent IE6 crash 
They don't need it anyway!
*/
var noSifrUrls = new Array(
	'/',
	'/default.aspx',
	'/investments.aspx',
	'/pensions.aspx',
	'/protection.aspx',
	'/retirement.aspx'	
);

var fullUrl = String(window.location);
var sifrUrl = fullUrl.replace(fullUrl.baseUrl(), '').toLowerCase();

var urlIsOk = true;

for (var i in noSifrUrls) {
	var noSifrUrl = noSifrUrls[i];
	if (noSifrUrl == sifrUrl) {
		urlIsOk = false;
		break;
	}
}

if (urlIsOk) {
	Event.observe(window, 'load', doSifr, false);
}