//******************************************************************************************
// Global Scope Variables
//******************************************************************************************
var selectsHidden;
var heightDiff = 0;
var widthDiff = 0;

//--------------------------------------------------
//  IMAGE ROLLOVERS
//--------------------------------------------------

function rollover(tagName, fileName) {
	document.images[tagName].src = fileName;
}

//handles errors
window.onerror = errorHandler;
function errorHandler(msg,url,lno)
{
	return (true);
}

if(typeof(tamar_rollover_warnings) == 'undefined') {
  tamar_rollover_warnings = false;
}

tamar_rollovers = new Array();

function tamar_rollover(obj, onSrc, offSrc) {

  //if the objects already in the array, leave it
  for (var i=0; i<tamar_rollovers.length; i++) {
    if (obj == tamar_rollovers[i].obj) return
  }

  //assign image object to this object and create image objects
  this.obj     = obj;
  this.off     = new Image();
  this.off.src = offSrc || obj.src;
  this.on      = new Image();

  //see if an onSrc image was supplied
  if (onSrc) {
    //it was so set the mouseover image source the supplied one
	this.on.src = onSrc;

    //see if the on and off images supplied are the same. If they are fail. We only need to check here
    //as if an onSrc wasn't supplied we know they should take the form _on/_off
    if (this.off.src == this.on.src) {
      //this.error_msg('image with tamar_rollover has the same mouseover/mouseout image');
      return;
    }
  } else {
    //a mouseover image was not supplied, so we're going to work it out
    //the original image should have the format imagename_off.ext
    //the new image will have the format imagename_on.ext

    //onSrc = /(\w*)Off\.(\w\w\w\w?)$/.exec(this.off.src)

	//Some images end in 'On' and some in 'on' etc therefore need to check cases
	if (this.off.src.indexOf('off') > -1) {
		newString = this.off.src.replace(/off/,"on");
		this.on.src = newString;
	}
	else if (this.off.src.indexOf('Off') > -1) {
		newString = this.off.src.replace(/Off/,"On");
		this.on.src = newString;
	}
	else{
		//the image didn't follow the pattern imagename_off.ext
        //this.error_msg('image with tamar_rollover failed to load a correct filename.\n(file should be called [name]_off.gif)');
	    return;
    }
  }

  //check the mouseover images filesize to see if it has been loaded properly
  /*
  if (this.on.fileSize <= 0) {
    //this.error_msg('image with tamar_rollover failed to load mouseover (on) image. '+this.on.src+' could not be found');
    return;
  }
  */
  if ((this.on.width != this.off.width || this.on.height != this.off.height) && tamar_rollover_warnings) {
    //this.error_msg('warning: image with tamar_rollover, dimensions of rollover images don\'t match');
  }

  //assign this object (tamar_rollover) to variable so we can assign it in functions below
  var thisObject  = this;

  //create handlers for image that change the image
  obj.onmouseover = function() { obj.src = thisObject.on.src  }
  obj.onmouseout  = function() { obj.src = thisObject.off.src }

  //push this object into the rollovers array
  tamar_rollovers[tamar_rollovers.length] = this;
}

tamar_rollover.prototype.error_msg = function(message) {
  //alert(message);
  this.obj.style.border = '3px solid #ff3333';
}


//--------------------------------------------------
//  END IMAGE ROLLOVERS
//--------------------------------------------------



//--------------------------------------------------
//  PHONE ME
//--------------------------------------------------

function popup(url, aname, theheight, thewidth, scrollers) {
  //var aname = 'popup' ;
  var winX = 0;
  var winY = 0;
  var winnX =  (screen.width-thewidth)/2;
  var winnY = (screen.height-theheight)/2;
  if (!scrollers)
	scrollers = 'no';
  str_WinParams = 'top=' + winnY+',left=' + winnX +',toolbar=no,location=no,status=no,menubar=no,scrollbars=' + scrollers + ',resizable=no,width='+thewidth+',height='+theheight;
  thewindowobject = window.open(url,aname, str_WinParams);
  if (window.focus) thewindowobject.focus()
}

//--------------------------------------------------
//  END PHONE ME
//--------------------------------------------------



//--------------------------------------------------
//  FAQ
//--------------------------------------------------

function ShowFAQ()
{
    if (navigator.appName == "Netscape")
    {
        var Benefits_win = window.open("wcHouseholdQuote.asp?WCI=FAQ&amp;WCE=Show&amp;WCU", "FAQ", "toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=400");
           Benefits_win.focus();
    }

    if (navigator.appName == "Microsoft Internet Explorer")
    {
        var Benefits_win = window.open("wcHouseholdQuote.asp?WCI=FAQ&amp;WCE=Show&amp;WCU", "FAQ", "toolbar=0,location=0,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=500,height=400");
    }
}
//--------------------------------------------------
//  END FAQ
//--------------------------------------------------




//
//  STYLE SHEET SWAPPER
//
/*
function styleSwap(object, state){
  if (state == 'on'){
    object.style.backgroundColor = '#fff89d'
    object.style.color = '#000000'
  }else{
    object.style.backgroundColor = '#FFFFCC'//.className ='inputBody'
    object.style.color = '#666666'
  }
}
*/

function styleSwap(object, state){
  if (state == 'on'){
    object.className ='inputBodyFocus';
  }else{
    object.className ='inputBody';
  }
}




//
//  FRAMESET ALIGNMENT
//

function windowSize() {
		var myWidth = 0;
		var myHeight = 0;
		if (document.getElementById('footballer_nav')){
			var extraSpace = document.body.clientWidth;
			var imagePos = 770 + ((extraSpace -770)/2);
			document.getElementById('footballer_nav').style.left = imagePos-200 + 'px';
			document.getElementById('footballer_nav').style.visibility = 'visible';
		}
}


//
//  HELP
//

  function startHelp() {
    showDiv('Default',1); //show a div on page load (and init with a fresh value of 1)
  }

  function showDiv(id,fresh) {
  if (fresh==1) {
    prevId = 'Default';
  }
  if (typeof(prevId) != "undefined") {
    if (prevId){
      document.getElementById('lyr_'+prevId).style.visibility = 'hidden';
    }

    document.getElementById('lyr_'+id).style.visibility = 'visible';
    prevId = id;
  }
  }

  function newWindow(url)
  {
  	theWindow = window.open(url,'rollover','width=650,height=465,toolbar=no,location=no,scrollbars=yes,status=no,resizable=no')
  }

  function startUp(fld){
	  document.getElementById(fld).focus();
  }

  function printIt()
  {
	if (window.print) {
		window.print() ;
	}
	else
	{
    	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    	WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";
	}
  }


  function openWindow(url){
	window.open(url,null,'menubar=no,toolbar=no,directories=no,status=yes,scrollbars=yes,resizable=no,resize=no,width=750,height=550,top=50,left=50');
  }

////////////////////////////
//Help popup functionality//
////////////////////////////
var selectsHidden;

function displayPopUpHelp(icon, popUpID, xShift, yShift, inPopUp) 
{
	icon.style.cursor = 'pointer';

	// Set Pop Up text and display
	if (inPopUp == null)
	    var inPopUp = false;
	else
	    inPopUp = true;
		
	var popUp = document.getElementById(popUpID);
	popUp.style.display = 'block';
	popUp.style.position = 'absolute';
	popUp.style.zIndex = '9999';

	// Set Pop Up position below icon image
	var xPos = getAscendingLefts(icon);
	var yPos = getAscendingTops(icon);
	
	if (xShift)
		xPos = xPos + xShift;
		
	if (yShift)
		yPos = yPos + yShift;
	
	popUp.style.top = (yPos + (icon.offsetHeight + 3)) + 'px';
	popUp.style.left = (xPos + (icon.offsetHeight + 3)) + 'px';

	if ( scrollRequired(popUp) )
	{
		var newTop = parseInt(popUp.style.top.substr(0, (popUp.style.top.length - 2))) - parseInt(heightDiff) - 5;
		popUp.style.top = newTop + 'px';
	}

	if ( horizontalScrollRequired(popUp) )
	{
		var newLeft = parseInt(popUp.style.left.substr(0, (popUp.style.left.length - 2))) - parseInt(widthDiff) - 5;
	    popUp.style.left = newLeft + 'px';

	    // Reset variable
	    widthDiff = 0;
	}

	// Hide selects overlapping pop up
    checkSelectOverlap(popUp, inPopUp);

	// Fade in pop up
	fadePopUp(popUp, 0, 100, 500);

	// Hide selects overlapping pop up
    checkSelectOverlap(popUp, inPopUp);
}

// Ascertains the correct left position...
function getAscendingLefts(elem, inPopUp)
{
	if (elem == null)
		return 0;
    else if (elem.style.position=='absolute' && inPopUp)
        return 0;
	else
		return elem.offsetLeft + getAscendingLefts(elem.offsetParent, inPopUp);
}

// Ascertains the correct top position...
function getAscendingTops(elem, inPopUp)
{
	if (elem == null)
		return 0;
    else if (elem.style.position=='absolute' && inPopUp)
        return 0;
	else
		return elem.offsetTop + getAscendingTops(elem.offsetParent, inPopUp);
}

// Work out whether the pop up window when opened will be taller than the screen available
function scrollRequired(popUp)
{
    var scrollY, screenHeight;

    if (document.all) {
        if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
        else
            scrollY = document.documentElement.scrollTop;
    }
    else
        scrollY = window.pageYOffset;

    if ( document.all )
       screenHeight = document.documentElement.clientHeight;
    else
       screenHeight = window.innerHeight;

    var bottomPos = parseInt(popUp.style.top.substr(0, (popUp.style.top.length - 2))) + parseInt(popUp.clientHeight);
    var totalHeight = parseInt(scrollY) + parseInt(screenHeight);

    if ( bottomPos > totalHeight )
    {
        heightDiff = parseInt(bottomPos) - parseInt(totalHeight);
        return true;
    }
    else
        return false;
}

// Work out whether the pop up window when opened will be wider than the screen available
function horizontalScrollRequired(popUp)
{
	var scrollX, screenWidth;

    if (document.all) {
        if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
        else
            scrollX = document.documentElement.scrollLeft;
    }
    else
        scrollX = window.pageXOffset;

    if ( document.all )
       screenWidth = document.documentElement.clientWidth;
    else
       screenWidth = window.innerWidth;

    var rightPos = parseInt(popUp.style.left.substr(0, (popUp.style.left.length - popUp.style.left.indexOf("px")) + 1)) + parseInt(popUp.clientWidth);
    var totalWidth = parseInt(scrollX) + parseInt(screenWidth);

    if ( rightPos > totalWidth )
    {
        widthDiff = parseInt(rightPos) - parseInt(totalWidth);
        return true;
    }
    else
        return false;
}

function fadePopUp(popUp, opacStart, opacEnd, millisec)
{
    // Speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    // Set the opacity
    if ( opacStart < opacEnd ) {
		for(i = opacStart; i <= opacEnd; i++) {
			setTimeout("setOpacity(" + i + ", '" + popUp.id + "')", (timer * speed));
			timer++;
		}
    }
    else {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("setOpacity(" + i + ", '" + popUp.id + "')", (timer * speed));
			timer++;
		}
    }
}

// Set the opacity of a pop up control
function setOpacity(opacity, popUpID)
{
    var popUp = document.getElementById(popUpID);

    // Set opacity values to work in all browsers
    popUp.style.opacity = (opacity / 100);
    popUp.style.MozOpacity = (opacity / 100);
    popUp.style.KhtmlOpacity = (opacity / 100);
    popUp.style.filter = 'alpha(opacity=' + opacity + ')';
}

function hidePopUpHelp(popUp)
{
	popUp = document.getElementById(popUp);
    popUp.style.opacity = (0);
    popUp.style.MozOpacity = (0);
    popUp.style.KhtmlOpacity = (0);
    popUp.style.filter = 'alpha(opacity=0)';
    popUp.style.display = 'none';

    // Show all select controls
    showSelect();
}

function showSelect()
{
    // Show all select controls as they may have been hidden
    for ( var x = 0; x < selectsHidden.length; x++ ) {
        selectsHidden[x].style.visibility = 'visible';
    }

    selectsHidden.length = 0;
}

// Check if the pop up control will overlap any select elements & hide them
function checkSelectOverlap(popUp, inPopUp) 
{
	selectsHidden = new Array();
    var selectControls = new Array();
    selectControls = document.getElementsByTagName('select');
    
    var popUpxPos = getAscendingLefts(popUp, inPopUp);
    var popUpyPos = getAscendingTops(popUp, inPopUp);


    for ( var x = 0; x < selectControls.length; x++ ) 
    {
        var selectControl = selectControls[x];
        
        var selectxPos = getAscendingLefts(selectControl, inPopUp);
		var selectyPos = getAscendingTops(selectControl, inPopUp);
		
		var selectxPosMax = selectxPos + selectControl.offsetWidth;
		var selectyPosMax = selectyPos + selectControl.offsetHeight;
        
        // Work out if select control sits within the dimensions of the pop up and hide if it does
        if ( selectxPos >= popUpxPos && selectxPosMax <= (popUpxPos + popUp.offsetWidth) ) {
            if (selectyPosMax >= popUpyPos && selectyPos <= (popUpyPos + popUp.offsetHeight) ) 
            {
                selectControl.style.visibility = 'hidden';
                selectsHidden[selectsHidden.length] = selectControl;
            }
        }
    }
}

//******************************************************************************************
// Restrict Key Strokes to Input Controls
//******************************************************************************************
// Restrict user entry to numbers only for an input control
function checkNum(e)
{
	var characterCode;
	var isValid = false;

	if ( e && e.which )
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

    // Allow numbers
    if ((characterCode >= 48) && (characterCode <= 57))
        isValid = true;

    // Allow Tab & Backspace
    if ((characterCode == 8) || (characterCode == 9))
        isValid = true;

    if (!isValid)
    {
        event.cancelBubble = true;
        event.returnValue = false;
    }
}

// Restrict user entry to numbers + spaces only for an input control
function checkNumSpaces(e)
{
	var characterCode;
	var isValid = false;

	if ( e && e.which )
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

    // Allow numbers
    if ((characterCode >= 48) && (characterCode <= 57))
        isValid = true;

    // Allow Spaces, Tab & Backspace
    if ((characterCode == 32) || (characterCode == 8) || (characterCode == 9))
        isValid = true;

    if (!isValid)
    {
        event.cancelBubble = true;
        event.returnValue = false;
    }
}

// Restrict user entry to numbers, letters and [-&,] characters only for an input control
function checkAlphaNum(e)
{
	var characterCode;
	var isValid = false;

	if ( e && e.which )
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

    // Allow numbers
    if ((characterCode >= 48) && (characterCode <= 57))
        isValid = true;

    // Allow upper case letters
    if ((characterCode >= 65) && (characterCode <= 90))
        isValid = true;

    // Allow lower case letters
    if ((characterCode >= 97) && (characterCode <= 122))
        isValid = true;

    // Allow the characters [-&, '] + Tab & Backspace
    if ((characterCode == 45) || (characterCode == 38) || (characterCode == 44) ||
        (characterCode == 32) || (characterCode == 39) || (characterCode == 8) ||
        (characterCode == 9))
        isValid = true;

    if (!isValid)
    {
        event.cancelBubble = true;
        event.returnValue = false;
    }
}

// Restrict user entry to numbers, letters and spaces characters only for an input control
function checkAlphaNumPostcode(e)
{
	var characterCode;
	var isValid = false;

	if ( e && e.which )
	{
		e = e;
		characterCode = e.which;
	}
	else
	{
		e = event;
		characterCode = e.keyCode;
	}

    // Allow numbers
    if ((characterCode >= 48) && (characterCode <= 57))
        isValid = true;

    // Allow upper case letters
    if ((characterCode >= 65) && (characterCode <= 90))
        isValid = true;

    // Allow lower case letters
    if ((characterCode >= 97) && (characterCode <= 122))
        isValid = true;

    // Allow the characters [-&, '] + Tab & Backspace
    if ((characterCode == 32) || (characterCode == 8) || (characterCode == 9))
        isValid = true;

    if (!isValid)
    {
        event.cancelBubble = true;
        event.returnValue = false;
    }
}

//******************************************************************************************
// Attach Event Handlers to Elements
//******************************************************************************************
function addEvent(obj, type, fn)
{
	if (obj.addEventListener)
		obj.addEventListener(type, fn, false);
	else if (obj.attachEvent)
		obj.attachEvent("on" + type, fn);
}


//******************************************************************************************
// ImageButton MouseOver Effects = Correction of PNG Images
//******************************************************************************************
var arrButtonMouseOvers = new Array();
var arrButtonMouseOversPNG = new Array();
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function setUpMouseOvers(e)
{
    var arrInputs = document.getElementsByTagName('input');
    
    for(var x = 0; x < arrInputs.length; x++)
    {
        var img = arrInputs[x];

        if (img.type != 'undefined')
            if (img.type == 'image')
            {
                if ((version >= 5.5) && (version < 7) && (img.src.toLowerCase().indexOf('.png') > -1))
                    new buttonMouseOverPNG(img);
                else
                    new buttonMouseOver(img);
            }
    }
}

function buttonMouseOver(obj) 
{
    // If the objects already in the array, leave it
    for (var i = 0; i < arrButtonMouseOvers.length; i++) 
        if (obj == arrButtonMouseOvers[i].obj) 
            return;

    // Assign image object to this object and create image objects
    this.obj = obj;
    this.off = new Image();
    this.off.src = obj.src;
    this.on = new Image();

    // Use regex to create the on image SRC and assign to this object
    if (this.off.src.toLowerCase().indexOf('_off') > -1) 
    {
        var onSrc = this.off.src.replace(/_[Oo][Ff]{2}/, "_On");
        this.on.src = onSrc;
    }
    else
        return;

    // Assign this object to a variable so we can assign it in functions below
    var thisObject  = this;

    // Create event handlers to change the image on mouse over
    var fnMouseOver = function() { obj.src = thisObject.on.src;  };
    var fnMouseOut = function() { obj.src = thisObject.off.src;  };
    
    addEvent(obj, "mouseover", fnMouseOver);
    addEvent(obj, "mouseout", fnMouseOut);

    // Put this object into the rollovers array
    arrButtonMouseOvers[arrButtonMouseOvers.length] = this;
}

function buttonMouseOverPNG(img)
{
    if (img.src.toLowerCase().indexOf('_off') > -1) 
    {   
        var imgSrc = img.src;
        var imageName = imgSrc.substring(imgSrc.lastIndexOf("/") + 1);
        img.src = imgSrc.replace(imageName, "spacer.gif");
        
        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" +  imgSrc + "', sizingMethod='image')"
        
        var offImg = new Image();
        offImg.style.filter = img.style.filter;
        
        var onImg = new Image();
        onImg.style.filter = img.style.filter.replace(/_[Oo][Ff]{2}/, "_On");

        // Assign image object to this object and create image objects
        this.obj = img;
        this.off = offImg;
        this.on = onImg;

        // Assign this object to a variable so we can assign it in functions below
        var thisObject  = this;

        // Create event handlers to change the image on mouse over
        var fnMouseOver = function() { img.style.filter = thisObject.on.style.filter;  };
        var fnMouseOut = function() { img.style.filter = thisObject.off.style.filter;  };
        
        addEvent(img, "mouseover", fnMouseOver);
        addEvent(img, "mouseout", fnMouseOut);

        // Put this object into the rollovers array
        arrButtonMouseOversPNG[arrButtonMouseOversPNG.length] = this;
    }
}

function correctPNGImages()
{
    if ((version >= 5.5) && (version < 7))
    {
        for (var x = 0; x < document.images.length; x++ )
        {
            var img = document.images[x];
            
            if (img.src.indexOf('.png') > -1)
                if (img.src.toLowerCase().indexOf('_off') == -1)
                { 
                    var imgID = (img.id) ? "id='" + img.id + "' " : ""
                    var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                    var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                    var imgStyle = "display:inline-block;" + img.style.cssText 
                    if (img.align == "left") imgStyle = "float:left;" + imgStyle
                    if (img.align == "right") imgStyle = "float:right;" + imgStyle
                    if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                    var strNewHTML = "<span " + imgID + imgClass + imgTitle
                    + " style=\"" + "width:" + img.offsetWidth + "px; height:" + img.offsetHeight + "px;" + imgStyle + ";"
                    + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                    + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
                    img.outerHTML = strNewHTML
                }
        }
    }
}

//******************************************************************************************
// Set up MouseOver effects for all buttons on the page + Fix PNG images
//******************************************************************************************
addEvent(window, "load", setUpMouseOvers);
addEvent(window, "load", correctPNGImages);

