﻿
    function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
  
function HideContent(d) {
if(d.length < 1) { return; }
if(document.getElementById(d))
{
document.getElementById(d).style.display = "none";
}
}

//-- Show or hide the passed object name.
function ShowHideObject(objectName, isVisible)
{
    if(objectName.length < 1) { return; }
    if(document.getElementById(objectName))
    {
           if(isVisible)
            {
                document.getElementById(objectName).style.display = "block";
            }
            else
            {
                document.getElementById(objectName).style.display = "none";
            }
    }
}

function ShowContentCart(d, i) {
if(d.length < 1) { return; }
if(document.getElementById(d))
{
var div = document.getElementById(d);
var obj = document.getElementById(i);
var xPos = findPosX(obj)
var yPos = findPosY(obj)

div.style.left = xPos + 5 + "px";
div.style.top = yPos - 170 + "px";
div.style.display = "block";
}}

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function ValidateCreditCardNumber(source, arguments)
{
	var numberRegEx = new RegExp('^[0-9]*$');
	var isMatch = numberRegEx.exec(arguments.Value);
	if (trim(arguments.Value) == "")
	{
		arguments.IsValid = false;
	}
	else if(isMatch == null)
	{
		arguments.IsValid = false;
	}
	else if((arguments.Value.length < 12) || (arguments.Value.length > 16))
	{
		arguments.IsValid = false;
	}
	else
	{
		//-- Perform the Luhn test.
		var digit = 0;
		var checkDigit = 0;
		var modDigit = 0;
		var cardNumber = arguments.Value;
		for (var i = 1; i <= arguments.Value.length; i++)
		{
			digit = parseInt(cardNumber.substring(i - 1, i));
			//-- If the number of card digits is even and we are at an odd digit, or
			//-- if the number of card digits is odd, and we are at an even digit
			
			if(cardNumber.length % 2 == 1)
			{
				modDigit = 0;
			}
			else
			{
				modDigit = 1;
			}
		
			if( i % 2 == modDigit)
			{
				//-- Double the digit and if greater than 9, subtract 9
				digit = digit * 2;
				if(digit > 9)
				{
					digit = digit - 9;
				}
		
				//-- Add to total
				checkDigit = checkDigit + digit;
			}
			else
			{
				//-- Add to total
				checkDigit = checkDigit + digit;
			}
		}

		if(checkDigit % 10 == 0)
		{
			arguments.IsValid = true;
		}
		else
		{
			arguments.IsValid = false;
		}
	}
	return;
}

function ValidateRequiredDropDown(source, arguments) 
{ 
//-- This function checks to see if the user has selected a value from the drop-down.
//-- This method assumes that the top-most value in the drop-down, with a value of zero,
//-- represents the "unselected" item.
if (arguments.Value == 0) 
{
arguments.IsValid = false;
}
else 
{
arguments.IsValid = true;
}
}

function RecalculateRecommendedTotal(chkBx, recLbl, prcLbl, nstpTbl, cicLbl, upsellSkuID)
{
 var checkbox = document.getElementById(chkBx);
 var recommendedTotalLabel = document.getElementById(recLbl);
 var nextStepButtonTable = document.getElementById(nstpTbl);
 var priceLabel = document.getElementById(prcLbl);
 var cartItemCountLabel = document.getElementById(cicLbl);
 var currentTotal;
 var price;
 var cartItemCount;
 var hasInnerText = (document.getElementsByTagName("body")[0].innerText != undefined) ? true : false;
 if(!hasInnerText)
	 {
	 currentTotal = Number(recommendedTotalLabel.textContent, 2);
	 price = Number(priceLabel.textContent, 2);
	 cartItemCount = Number(cartItemCountLabel.textContent, 2);
	 }
 else
	 {
	 currentTotal = Number(recommendedTotalLabel.innerText, 2);
	 price = Number(priceLabel.innerText, 2);
	 cartItemCount = Number(cartItemCountLabel.innerText, 2);
	 }
	 
 var newTotal;
 
 if(checkbox.checked)
	 {
		 if(upsellSkuID == 211 && price == 240)
		 {
			newTotal = currentTotal + 274.98;
		 }
		 else if(upsellSkuID == 212 && price == 300)
		 {
			newTotal = currentTotal + 330.00;
		 }
		 else
		 {
			newTotal = currentTotal + price;
		 }
	 if(!hasInnerText)
	 {
	 recommendedTotalLabel.textContent = newTotal.toFixed(2);
	 }
	 else
	 {
	 recommendedTotalLabel.innerText = newTotal.toFixed(2);
	 }
	 }
 else
	 {
	 if(upsellSkuID == 211 && price == 240)
		 {
			newTotal = currentTotal - 274.98;
		 }
		 else if(upsellSkuID == 212 && price == 300)
		 {
		    newTotal = currentTotal - 330.00;
		 }
		 else
		 {
			newTotal = currentTotal - price;
		 }
	 if(!hasInnerText)
	 {
	 recommendedTotalLabel.textContent = newTotal.toFixed(2);
	 }
	 else
	 {
     recommendedTotalLabel.innerText = newTotal.toFixed(2);
	 }
	 }

  if (newTotal == 0 && cartItemCount == 0)
    {
     nextStepButtonTable.style.display='none';
    } 
  else
    {
     nextStepButtonTable.style.display='block';
    }
 
}

      // -- RAD Window functions.
function OpenWindow (WindowName)
        {
            window.radopen(null, WindowName);            
        }
        function GetElementPosition (el)   
        {   
            var parent = null;   
            var pos = {x: 0, y: 0};   
            var box;   
       
            if (el.getBoundingClientRect)    
            {    
                // IE   
                box = el.getBoundingClientRect();   
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;   
                var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;   
       
                pos.x = box.left + scrollLeft - 2;   
                pos.y = box.top + scrollTop - 2;   
                   
                return pos;   
            }   
            else if (document.getBoxObjectFor)    
            {    
                // gecko   
                box = document.getBoxObjectFor(el);   
                pos.x = box.x - 2;   
                pos.y = box.y - 2;   
            }   
            else    
            {    
                // safari/opera   
                pos.x = el.offsetLeft;   
                pos.y = el.offsetTop;   
                parent = el.offsetParent;   
                if (parent != el)   
                {   
                    while (parent)    
                    {   
                        pos.x += parent.offsetLeft;   
                        pos.y += parent.offsetTop;   
                        parent = parent.offsetParent;   
                    }   
                }   
            }   
       
       
            if (window.opera)   
            {   
                parent = el.offsetParent;   
                   
                while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML')    
                {   
                    pos.x -= parent.scrollLeft;   
                    pos.y -= parent.scrollTop;   
                    parent = parent.offsetParent;   
                }   
            }   
            else  
            {   
                parent = el.parentNode;    
                while (parent && parent.tagName != 'BODY' && parent.tagName != 'HTML')    
                {   
                    pos.x -= parent.scrollLeft;   
                    pos.y -= parent.scrollTop;   
       
                    parent = parent.parentNode;   
                }   
            }   
            return pos;
        }
        
        var skip_close = false;
        var param_value = '';
        function OpenWindowWithParam (WindowName, ParamValue, element)
        {
            if (param_value == ParamValue) return;
            var oWindow = window.radopen(null, WindowName);
            oWindow.SetUrl(ParamValue);
            
            var pos = GetElementPosition(element);
            var X = pos.x;
            var Y = pos.y;
				
            oWindow.MoveTo(X + element.offsetWidth + 10, Y - 150)
            
            param_value = ParamValue;
            skip_close=false;
        }
        
        function OpenAccountWindowWithParam (WindowName, ParamValue, element)
        {
            if (param_value == ParamValue) return;
            var oWindow = window.radopen(null, WindowName);
            oWindow.SetUrl(ParamValue);
            oWindow.Width = 350;
            oWindow.Height = 250;
            oWindow.SetWidth(350);
            
            var pos = GetElementPosition(element);
            var X = pos.x;
            var Y = pos.y;
				
            oWindow.MoveTo(X + element.offsetWidth + 10, Y - 150)
            
            param_value = ParamValue;
            skip_close=false;
        }
        
        function OpenAccountUrlWindowWithParam (WindowName, ParamValue, element)
        {
            if (param_value == ParamValue) return;
            var oWindow = window.radopen(null, WindowName);
            oWindow.SetUrl(ParamValue);
            oWindow.Width = 350;
            oWindow.Height = 120;
            oWindow.SetWidth(350);
            oWindow.SetHeight(120);
            
            var pos = GetElementPosition(element);
            var X = pos.x;
            var Y = pos.y;
				
            oWindow.MoveTo(X + element.offsetWidth + 10, Y - 50)
            
            param_value = ParamValue;
            skip_close=false;
        }
        
        function OpenCartGroupWindowWithParam (WindowName, ParamValue, ele)
        {
            if (param_value == ParamValue) return;
            var oWindow = window.radopen(null, WindowName);
            oWindow.SetUrl(ParamValue);
            var element = document.getElementById(ele);
            
            var pos = GetElementPosition(element);
            var X = pos.x;
            var Y = pos.y;
				
            oWindow.MoveTo(X - (element.offsetWidth - 5), Y + 8);
            
            param_value = ParamValue;
            skip_close=false;
        }
        
        function HandleClick (WindowName, ParamValue)
        {
            skip_close=true;
        }
        
        function CloseWindow (WindowName) 
        {
            if (skip_close) return 
            var oManager = GetRadWindowManager();
		    var oWindow = oManager.GetWindowByName(WindowName);
		    param_value = '';
		    if (oWindow != null ) 
			{
				oWindow.Close();
			}
        }
        
		function ValidatePhoneNumberContactDetails(source, arguments)
{
	var phoneNumber = arguments.Value;
	var regExString = '^((\\([0-9]{2,3}\\))|\\+[0-9]{2,3})?([ \\-.,]?[0-9]+)*( ?(x|ext)\\.? ?[0-9]+)?$';
	if((phoneNumber.length < 6) || (phoneNumber.length > 25))
	{
		arguments.IsValid = false;
	}	
	var phoneNumberRegEx = new RegExp(regExString, "i");
	var isMatch = phoneNumberRegEx.exec(phoneNumber);
	if(isMatch == null)
	{
		arguments.IsValid = false;
	}
	return;
}


function SetSelectedAnswer(questionID, answerID, answerValue)
    {
        var cookieName = "question" + questionID;
        var expire = "";
        var value = answerID + "-" + answerValue;
        document.cookie = cookieName+"="+value+";";
        return true;
    }
    
    function SetCheckedAnswer(questionID, answerID, answerValue, obj)
    {

            //-- Because we can select more than one answer with the checkboxes, 
            //-- we need to manipulate the cookie based on user actions
            var cookieName = "question" + questionID;
            var cookie = GetCookie(cookieName)
            var value = "";
            
            //-- If there is no existing cookie for the current question, create one
            if(cookie == null)
            {
                value = answerID + "-" + answerValue;
                document.cookie = cookieName+"="+value+";";
            }
            //--- Cookie exists, so manipulate it
            else
            {
                cookie = cookie.substring(cookie.indexOf("=") + 1);
                var cookieEntry = ""
                
                //-- We need to determine if the selected answer value is at the start of the cookie string
                //-- If it is then we don't want to have the "/" delimiter at the front of the entry
                //-- If it is a second entry, then we need to add the "/" delimiter for when we split the cookie array
                if(cookie.indexOf(answerID) == 0)
                {
                    cookieEntry = answerID + "-" + answerValue;
                }
                else
                {
                    cookieEntry = "/" + answerID + "-" + answerValue;
                }

                //-- If the checkbox is being checked, then we add the answer to the cookie string
                if(obj.status)
                {
                    //-- This check is required as the .Net code does not seem to remove the cookie when
                    //-- the Previous button is clicked
                    value = cookie + cookieEntry;
                }
                //-- Checkbox is being unchecked, so we get ready to remove it from the cookie string
                else
                {
                    value = cookie.replace(cookieEntry, "");
                    //-- If there is a "/" delimiter in the string and there is only one answer, strip it
                    if(value.indexOf("/") == 0)
                    {
                        value = value.substring(1);
                    }
                }
                //-- If there are no answers selected, kill the cookie as it is empty
                if(value.length == 0)
                {
                    //Kill the cookie
                    var killTime = new Date("January 1, 2000");
                    var killString = cookieName+"=nothing;expires=" + killTime.toUTCString();
                    document.cookie = killString
                }
                else
                {
                document.cookie = cookieName+"="+value+";";
                }
            }
            return true;      
    }
    
    function GetCookie(cookieName)
    {
        var start = document.cookie.indexOf(cookieName+"=");
        if(start > -1)
        {
            //-- cookie exists
            return document.cookie.substring(start);
        }
        else
        {
            //-- cookie does not exists
            return null;
        }
    }
    
    function SetListAnswer(questionID, answerID, obj)
    {
        var cookieName = "question" + questionID;
        var dropDownSelectedText = obj.options[obj.selectedIndex].text;
        var expire = "";
        var value = answerID + "-" + obj.value + "_" + dropDownSelectedText;
        document.cookie = cookieName+"="+value+";";
        return true;
    }
    
    function BlockEnter_New()
{
    if (window.event.keyCode == 13)
    {
        window.alert("New customers, please click “Create Account” to confirm your details.")
        window.event.keyCode = 0
    }
}

function BlockEnter_Existing()
{
    if (window.event.keyCode == 13)
    {
        window.alert("Existing customers, simply click “Login” to proceed.")
        window.event.keyCode = 0
    }
}
