﻿var CurrentRowOn = null;
var selectBoxOpen = false;

function ChangeGradType(me,row)
{
	var cSizeValue = GetSizeSB(me.id).value;
	if(cSizeValue.indexOf('#') != -1)
	{
		// Bogen Size is selected
		var r = GetBogenCodeAndPrice(me.id);
		SetPriceAndBuyButton(me.id, r.Price, r.Code, row);
	}
};

function ChangeDensity(me,row)
{
	var cSizeValue = GetSizeSB(me.id).value;
	if(cSizeValue.indexOf('#') != -1)
	{
		// Bogen Size is selected
		var r = GetBogenCodeAndPrice(me.id);
		SetPriceAndBuyButton(me.id, r.Price, r.Code, row);
	}
};

function ChangePriceAndPower(newVal, idPrice, idPower)
{
    var parts = newVal.toString().split("#");
    getElement(idPrice).innerHTML = parts[1];
    getElement(idPower).innerHTML = parts[2];
};

function GetBuyButton(idOfItemOnSameWebForm, row)
{
	var wfName = idOfItemOnSameWebForm.substring(0, idOfItemOnSameWebForm.indexOf('_'));
	if(row != null)
	{
		return $(wfName + "_hdBuy" + row);
	}
	else
	{
		return $(wfName + "_hdBuy-1"); // -1 to go
	}
};

function GetPriceAndSizeHidden(idOfItemOnSameWebForm)
{
	var wfName = idOfItemOnSameWebForm.substring(0, idOfItemOnSameWebForm.indexOf('_'));
	return $$(wfName + "_SizePriceH"); // $$ form element
};

function GetDensitySB(idOfItemOnSameWebForm)
{
	var wfName = idOfItemOnSameWebForm.substring(0, idOfItemOnSameWebForm.indexOf('_'));
	return $(wfName + "_Density");
};

function GetSizeSB(idOfItemOnSameWebForm)
{
	var wfName = idOfItemOnSameWebForm.substring(0, idOfItemOnSameWebForm.indexOf('_'));
	return $(wfName + "_SizeAndPrice");
};

function GetGradTypeSB(idOfItemOnSameWebForm)
{
	var wfName = idOfItemOnSameWebForm.substring(0, idOfItemOnSameWebForm.indexOf('_'));
	return $(wfName + "_GradType");
};

function GetBogenCodeAndPrice(idOfItemOnSameWebForm)
{
	var ret = new Object();
	
	// Must select the correct price and code based
	// on which Grad Type and Density are selected
	var GradTypeSB = GetGradTypeSB(idOfItemOnSameWebForm);
	var GradTypeValue = "None"; // common case
	
	if(GradTypeSB != null)
	{
		GradTypeValue = GradTypeSB.value;
	}
	
	var Size;
	var SizeSB = GetSizeSB(idOfItemOnSameWebForm);
	if(SizeSB.selectedIndex >= 0)
	{
		Size = SizeSB.options[SizeSB.selectedIndex].text;
	}
	
	// There will allways be a density
	var DensityValue = GetDensitySB(idOfItemOnSameWebForm).value
	
	// We can now recreate the entire first part of the Bogan code Density#Size#Edge#
	// so that we can find the correct SKUCode and Price.
	var bCodeFirstPart = DensityValue + "#" + Size + "#" + GradTypeValue + "#";
	var ind = SizeSB.value.indexOf("::");
	var FormattPriceForSize = SizeSB.value.substring(0, ind); // prefixed as Price::
	var SizeWithOutFormattPrefix = SizeSB.value.substring(ind+2); // take end part
	
	// Split all the bogan code/price values using the *
	var codePriceParts = SizeWithOutFormattPrefix.split("*");
	
	var matched = false;
	for(var i=0; i < codePriceParts.length; i++)
	{
		if(StartsWith(codePriceParts[i], bCodeFirstPart))
		{
			// Found the code we need
			var PriceAndCode = leftTrimVal(codePriceParts[i], bCodeFirstPart);
			
			// Finally split the last part and retreive the values we need
			var parts = PriceAndCode.split("#");
			ret.Price = FormatUSD(parts[0]);
			ret.Code = parts[1];
			matched = true;
			break;
		}
	}
	
	if( ! matched )
	{
		// Need to rollback to the format price
		ret.Price = FormatGBP(FormattPriceForSize);
	}
	return ret;
};

function ChangePrice(me, row)
{
	var price = null; 
	var code = null; // bogan only
	if(me.value.indexOf('#') != -1)
	{
		var r = GetBogenCodeAndPrice(me.id);
		price = r.Price;
		code = r.Code;
	}
	else
	{
		// default case
		price = me.value;
	}
	
	// Finally perform the modifications
	SetPriceAndBuyButton(me.id, price, code, row);
};

function ChangePriceO(newVal, id)
{
	var name = id.toString();
	var val = newVal.toString().split("#")[1]; 

	var price = null; 
	var code = null;
		
	if (val.indexOf('*') >= 0)
	{			
		price = val.substring(0, val.indexOf('*'));
		code = val.substring(val.indexOf('*') + 1);
	}
	else 
		price = val; 
			
	getElement(id).innerHTML = price;
	
	var row = name.substring(11);
	
	var buyButtons = document.getElementsByTagName('INPUT'); 
	
	for (var i = 0, e = buyButtons.length; i < e; i++) 
	{			
		if (buyButtons[i].name.length > 39)
		{
			var name = buyButtons[i].name.substring(39); 

			if (name == "hdBuy" + row)
			{			
				if (code != null) 
					buyButtons[i].onclick = function() { manu_CDRuleGroupLink('bogen',code,'-1',null,null,null,'formattUK'); return false; };
				else 
					buyButtons[i].onclick = function() { return true; }; 
					
				return; 
			}
		} 
	}		
};

function SetPrice(idOfItemOnSameWebForm, price, row)
{
	if(price != null)
	{
		// Set the price
		if(row != null)
		{
			$('hdItemPrice' + row.toString()).innerHTML = price;
		}
		else
		{
			$('PriceL').innerHTML = price;
		}
	}
	
	var SizeSB = GetSizeSB(idOfItemOnSameWebForm);
	if(SizeSB.selectedIndex >= 0)
	{
		var Size = SizeSB.options[SizeSB.selectedIndex].text;
		GetPriceAndSizeHidden(idOfItemOnSameWebForm).value = Size + "#" + price; // used by the postback
	}
};

function SetPriceAndBuyButton(idSB, price, code, row)
{
	// Set the price
	SetPrice(idSB, price, row);
	
	// Set the buyButton onclick action
	var buyButton = GetBuyButton(idSB, row);
	
	if (buyButton != null)
	{ 		
		if (code != null)
		{
			buyButton.onclick = function() { manu_CDRuleGroupLink('bogen',code,'-1',null,null,null,'formattUK'); return false; };
		}
		else
		{
			buyButton.onclick = function() { return true; };
		}
	}
	else
	{
		alert("Cannot find buy button");
	}
};

function hProductOn(div)
{
    var arrowImg = findHighlightArrow(div);
    if(arrowImg != null)
    {
        arrowImg.parentElement.className = 'visible';
    }
    if(CurrentRowOn != null)
    {
        hProductOff(CurrentRowOn);
    }
    CurrentRowOn = div;
};

function findHighlightArrow(elem)
{
    if(elem.tagName == 'IMG' && elem.src.indexOf('highlightarrow.gif') != -1)
    {
        return elem;
    }
    else
    {
        for(var i=0; i<elem.childNodes.length; i++)
        {
            var t = findHighlightArrow(elem.childNodes[i]);
            if(t != null)
            {
                return t;
            }
        }
    }
    return null;
};

function hProductOff(div)
{
    var arrowImg = findHighlightArrow(div);
    if(arrowImg != null)
    {
        arrowImg.parentElement.className = 'invisible';
    }
    //div.className = div.oClassName;
    CurrentRowOn = null;
};


function hProductOffSel(sel)
{
    var element = sel;
    while(element != null)
    {
        if(element.onmouseout != null && element.onmouseout.toString().indexOf('hProductOff') != -1)
        {
            // Found the event so fire it to turn the row selection off
            element.fireEvent('onmouseout');
            break;
        }
        element = element.parentElement;
    }
    selectBoxOpen=false;
};

function Toggle(b)
{
    return !b;
};

function DisableEvent()
{
    window.event.cancelBubble = true;
    return false;
};

function EnableEvent(sel)
{
    var element = sel;
    while(element != null)
    {
        if(element.onmouseout != null && element.onmouseout.toString().indexOf('hProductOff') != -1)
        {
            // Found the event so fire it to turn the row selection off
            element.fireEvent('onfocus');
            break;
        }
        element = element.parentElement;
    }
};

// Code from the CMS
function StartsWith(str, prefix)
{
    return (str.substr(0,prefix.length)==prefix);
};

function leftTrimVal(sString, sTrimPart) 
{
    var index = sString.indexOf(sTrimPart);
    if(index == 0)
    {
        return sString.substr(sTrimPart.length, sString.length-1);
    }
    return sString;
};

function FormatUSD(val)
{
	var dotIndex = val.indexOf(".");
	if(dotIndex != -1 && ! StartsWith(val, "$"))
	{
		// We want to trim 2 dp after the .
		if(val.length >= dotIndex + 3)
		{
			return "$" + val.substring(0, dotIndex + 3);
		}
	}
	return val;
};

function FormatGBP(val)
{
	var dotIndex = val.indexOf(".");
	if(dotIndex != -1 && ! StartsWith(val, "£"))
	{
		// We want to trim 2 dp after the .
		if(val.length >= dotIndex + 3)
		{
			return "£" + val.substring(0, dotIndex + 3);
		}
	}
	return val;
};