// MON: 16/04/2009
//
// --------------------------------   NOTES   --------------------------------
// USE npower.js instead - DO NOT USE THIS FILE
//
// Any new code that requires an OnLoad event should go in that file
// ---------------------------------------------------------------------------








// MON: 24/09/2008
//
// --------------------------------   NOTES   --------------------------------
// To get a number from the query string, use a query string like this:
//                
// http://www.npower.com/index.html?t=123
//                
// ...then use class="getNum" on any div or span to replace the contents of that tag with 123 - in this instance.
//                
// You can also use html spaces (%20) in the number if you want spaces. For example:
//                
// http://www.npower.com/index.html?t=0800%20123%20123
//                
// The class="getFree" will replace the contents of the tag with "Call us FREE on:" if f=true, so:
//
// http://www.npower.com/index.html?t=0800%20123%20123&f=true
//
//
//
// Amended 03/03/2009 by Adam Dalziel:
//
// Numbers are validated against a specific set of numbers (i.e. HES or Residential numbers) based on
// the span tag with class attribute set to "DynamicNumber_xxx", where xxx is, for example, "HES" or
// "Residential".  If no such span tag is found, the default HES numbers are used for backwards
// compatibility.
//
// ---------------------------------------------------------------------------

function DynamicNumber()
{
}

//remove the ? from the beginning of the query and split it into a=1 var pairs...
DynamicNumber.TheQuery = location.search.substring(1);
DynamicNumber.TheVars = DynamicNumber.TheQuery.split("&");

DynamicNumber.ValidNumbers = new Array();
DynamicNumber.ValidNumbers["Residential"] = new Array(
    "08009759300", "08009759301", "08009759302", "08009759303", "08009759304",
    "08009759305", "08009805520");
//DynamicNumber.ValidNumbers["Business"] = new Array();
DynamicNumber.ValidNumbers["Green"] = new Array("08009753199");
DynamicNumber.ValidNumbers["HES"] = new Array(
    "08009805761", "08009805762", "08009805763", "08009805764", "08009805765",
    "08009805766", "08009805767", "08009805768", "08009805769", "08009805770",
    "08009805880", "08009805881", "08009805882", "08009805883", "08009805884",
    "08009805885", "08009805886", "08009805887", "08001070479", "08001079318",
    "08001079328", "08001079329", "08001386253", "08001386274", "08002940880",
    "08003160071", "08003160554", "08003166728", "08003167264", "08003167267",
    "08001070398", "08001079322", "08001079324", "08001079325", "08001973921",
    "08001973928", "08001973930", "08002940876", "08002940878", "08002944981",
    "08002944982", "08003160344", "08003160603", "08003167270", "08009157713",
    "08009157771", "08009157718", "08009802142", "08009802143", "08000728606",
    "08001070396", "08009805888", "08009805889", "08000727609", "08000727610",
    "08009802120", "08009802121", "08009802122", "08009802124", "08009802125",
    "08009802126", "08009802128", "08009751372", "08002941655", "08009803644",
    "08009803645", "08009803646", "08009803647", "08009803639", "08009803640",
    "08009803641", "08009803642", "08009803643", "08009803650", "08002944980",
    "08009759337");

DynamicNumber.Initialise = function()
{
	if (!document.getElementsByTagName)
	    return false;

	//assign the values to each valid div...
	DynamicNumber.SetContent("div");
	
	//now assign the values to each valid span...
	DynamicNumber.SetContent("span");

	//now set the location for the anchor tag
	DynamicNumber.SetAnchor();
}

DynamicNumber.GetParam = function(id)
{
	var thePairs;
	for (var i = 0; i < DynamicNumber.TheVars.length; i++)
	{
		thePairs = DynamicNumber.TheVars[i].split("=");
		if (thePairs[0].toLowerCase() == id.toLowerCase())
		{
			return unescape(thePairs[1]);
		}
	}
}

DynamicNumber.Tel;
DynamicNumber.GetTel = function()
{
    if (DynamicNumber.Tel == null)
    {
        DynamicNumber.Tel = DynamicNumber.GetParam("tel");
        if (DynamicNumber.Tel == null)
            DynamicNumber.Tel = DynamicNumber.GetParam("t");
    }
        
    return DynamicNumber.Tel;
}

DynamicNumber.Free;
DynamicNumber.IsFree = function()
{
    if (DynamicNumber.Free == null)
    {
        var free = DynamicNumber.GetParam("free");
        if (free == null)
            free = DynamicNumber.GetParam("f");

        DynamicNumber.Free = (free == null || free == "" || free == "false" || free == "0" ? false : true);
    }
    
    return DynamicNumber.Free;
}

DynamicNumber.Site;
DynamicNumber.GetSite = function()
{
    if (DynamicNumber.Site == null)
    {
        var elements = document.getElementsByTagName("span");
        for(var i=0; i < elements.length; i++)
        {
            var element = elements[i];
            if (element.className != null && element.className.substr(0, 14) == "DynamicNumber_")
            {
                DynamicNumber.Site = element.className.substr(14);
                break;
            }
        }
        
        if (DynamicNumber.Site == null)
            DynamicNumber.Site = "HES";
    }

    return DynamicNumber.Site;
}

DynamicNumber.SetContent = function(tag)
{
	if(!DynamicNumber.IsNumberValid(DynamicNumber.GetTel()))
	{
		return;
	}
	
	var items = document.getElementsByTagName(tag);
	for(var i=0; i < items.length; i++)
	{
		if (items[i].className.match("getNum") && DynamicNumber.GetTel())
			items[i].innerHTML = DynamicNumber.GetTel();
		if (items[i].className.match("getFree") && DynamicNumber.GetTel() && DynamicNumber.IsFree())
			items[i].innerHTML = "Call us FREE on:";
	}
}

DynamicNumber.SetAnchor = function()
{
	if(!DynamicNumber.IsNumberValid(DynamicNumber.GetTel()))
	{
		//don't process anything which is not valid
		return;
	}

	var anchor = document.getElementById("CallBackLink");
	if(anchor == null)
	{
		//if anchor does not exist, bail
		return;
	}
	
	if(anchor.href.lastIndexOf("&t=") < 1)
	{	
		//if the anchor href doesn't contain a "t" key, bail
		return;
	}

	var newHrefLocation = anchor.href.slice(0, anchor.href.lastIndexOf("&t=")).concat("&t=", DynamicNumber.GetTel());
	anchor.href= newHrefLocation;

}

DynamicNumber.IsNumberValid = function(number)
{
	if (number == null)
	    return true;

    var numberNoSpaces = number.replace(/(\s|%20)/g, '');
    var validNumbers = DynamicNumber.ValidNumbers[DynamicNumber.GetSite()];
    
    for (var i = 0; i < (validNumbers == null ? 0 : validNumbers.length); i++)
        if (validNumbers[i] == numberNoSpaces)
            return true;

    return false;
}

window.onload = function()
{
    DynamicNumber.Initialise();
}

