﻿// JScript File

// gives up and down scroll buttons to images, spans, ... named up_name, down_name, respectively.
// will keep the default scroll_box's style overflow if it encounters errors (so make overflow: auto;)

// usage: put this after the scrollbox div:  var div_scroll1 = new TextScroll('div_scroll1', 'scroll_box');
function TextScroll(scrollname, div_name, up_name, down_name)
{
    this.div_name = div_name;
    this.name = scrollname;
    this.scrollCursor = 0;
    this.speed = 5;
    this.timeoutID = 0;
    this.div_obj = null;
    this.up_name = up_name;
    this.dn_name = down_name;

{

        if (document.getElementById) {
            div_obj = document.getElementById(this.div_name);
            if (div_obj) {
                this.div_obj = div_obj;
                this.div_obj.style.overflow = 'hidden';
            }
            div_up_obj = document.getElementById(this.up_name);
            div_dn_obj = document.getElementById(this.dn_name);
            if (div_up_obj && div_dn_obj) {
                
                div_up_obj.onmouseover = function() { eval(scrollname + ".scrollUp();") };
                div_up_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };
                div_dn_obj.onmouseover = function() { eval(scrollname + ".scrollDown();") };
                div_dn_obj.onmouseout = function() { eval(scrollname + ".stopScroll();") };
            }
        }
    }

this.stopScroll = function() {
        clearTimeout(this.timeoutID);
    }

this.scrollUp = function() {
        if (this.div_obj) {
            this.scrollCursor = (this.scrollCursor - this.speed) < 0 ? 0 : this.scrollCursor - this.speed;
            this.div_obj.scrollTop = this.scrollCursor;
            this.timeoutID = setTimeout(this.name + ".scrollUp()", 60);
        }
    }


    
this.scrollDown = function() {
    if (this.div_obj) {
        this.scrollCursor += this.speed;
        this.div_obj.scrollTop = this.scrollCursor;
        if (this.div_obj.scrollTop == this.scrollCursor) {
            this.timeoutID = setTimeout(this.name + ".scrollDown()", 60);
        } else {
            this.scrollCursor = this.div_obj.scrollTop;
        }
    }
}    

this.resetScroll = function() {
        if (this.div_obj) {
            this.div_obj.scrollTop = 0;
            this.scrollCursor = 0;
        }
    }
}


function newWindow(layerToOpen) {
    
    hiddenWhiteLayerObj = document.getElementById(layerToOpen);
    hiddenWhiteLayerObj.style.display = "block";
}

function closeWindow(layerToClose) {
    hiddenWhiteLayerObj = document.getElementById(layerToClose);
    hiddenWhiteLayerObj.style.display = "none";
}

function updateAltAddress1() {
    ShipToObj = document.getElementById("ddlShipTo");
   
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        Address1Obj = document.getElementById("tbxAddress1");
        AltAddress1Obj = document.getElementById("tbxAltAddress1");
        AltAddress1Obj.value=Address1Obj.value;
    }   
}

function updateAltAddress2() {
    ShipToObj = document.getElementById("ddlShipTo");
   
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        Address2Obj = document.getElementById("tbxAddress2");
        AltAddress2Obj = document.getElementById("tbxAltAddress2");
        AltAddress2Obj.value=Address2Obj.value;
    }   
}

function updateAltCity() {
    ShipToObj = document.getElementById("ddlShipTo");
   
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        CityObj = document.getElementById("tbxCity");
        AltCityObj = document.getElementById("tbxAltCity");
        AltCityObj.value=CityObj.value;
    }   
}

function updateAltState() {
    ShipToObj = document.getElementById("ddlShipTo");
   
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        StateObj = document.getElementById("tbxState");
        AltStateObj = document.getElementById("tbxAltState");
        AltStateObj.value=StateObj.value;
    }   
}

function updateAltPostcode() {
    ShipToObj = document.getElementById("ddlShipTo");
   
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        PostcodeObj = document.getElementById("tbxPostcode");
        AltPostcodeObj = document.getElementById("tbxAltPostcode");
        AltPostcodeObj.value=PostcodeObj.value;
    }   
}

function updateAltUSAState() {
    CountriesObj = document.getElementById("ddlCountries");
    USAStatesObj = document.getElementById("ddlUSAStates");    
    regularStatesObj = document.getElementById("tbxState");    
    
    if ((CountriesObj[CountriesObj.selectedIndex].value != "US") && (CountriesObj[CountriesObj.selectedIndex].value != "CA"))
    {
        USAStatesObj.style.display = "none";
        regularStatesObj.value = "";
        regularStatesObj.style.display = "";
    }    
    else
    {
        USAStatesObj.style.display = "";    
        regularStatesObj.value = USAStatesObj[USAStatesObj.selectedIndex].value;
        regularStatesObj.style.display = "none";
    }
    
    ShipToObj = document.getElementById("ddlShipTo");
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        USAAltStatesObj = document.getElementById("ddlAltUSAStates"); 
        USAAltStatesObj.value = USAStatesObj[USAStatesObj.selectedIndex].value;
    }    
    
    //Finally update cart in case need to add tax;
    if ((CountriesObj[CountriesObj.selectedIndex].value == "US") || (CountriesObj[CountriesObj.selectedIndex].value == "CA"))
    {
        USAAltStatesObj = document.getElementById("ddlAltUSAStates"); 
        updateShoppingCartStates(USAAltStatesObj[USAAltStatesObj.selectedIndex].value, USAStatesObj[USAStatesObj.selectedIndex].value);
    }
}

function updateAltUSAState2() {
    AltCountriesObj = document.getElementById("ddlAltCountries");
    AltUSAStatesObj = document.getElementById("ddlAltUSAStates");    
    regularAltStatesObj = document.getElementById("tbxAltState");    
    
    if ((AltCountriesObj[AltCountriesObj.selectedIndex].value != "US") && (AltCountriesObj[AltCountriesObj.selectedIndex].value != "CA"))
    {
        AltUSAStatesObj.style.display = "none";
        regularAltStatesObj.value = "";
        regularAltStatesObj.style.display = "";
    }    
    else
    {
        AltUSAStatesObj.style.display = "";    
        regularAltStatesObj.value = AltUSAStatesObj[AltUSAStatesObj.selectedIndex].value;
        regularAltStatesObj.style.display = "none";
    }
    
    //Finally update cart in case need to add overseas shipping;
    updateShoppingCartStates('MA', 'MA');
}


function updateAltCountries() {
    ShipToObj = document.getElementById("ddlShipTo");
   
    CountriesObj = document.getElementById("ddlCountries");
    USAStatesObj = document.getElementById("ddlUSAStates");    
    regularStatesObj = document.getElementById("tbxState");    
    
    if ((CountriesObj[CountriesObj.selectedIndex].value != "US") && (CountriesObj[CountriesObj.selectedIndex].value != "CA"))
    {
        USAStatesObj.style.display = "none";
        regularStatesObj.value = "";
        regularStatesObj.style.display = "";
    }    
    else
    {
        USAStatesObj.style.display = "";    
        regularStatesObj.value = USAStatesObj[USAStatesObj.selectedIndex].value;
        regularStatesObj.style.display = "none";
    }
    
    
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        AltCountriesObj = document.getElementById("ddlAltCountries");
        AltCountriesObj.selectedIndex=CountriesObj.selectedIndex;
        
        USAAltStatesObj = document.getElementById("ddlAltUSAStates");    
        regularAltStatesObj = document.getElementById("tbxAltState");    

        if ((CountriesObj[CountriesObj.selectedIndex].value != "US") && (CountriesObj[CountriesObj.selectedIndex].value != "CA"))
        {
            USAAltStatesObj.style.display = "none";
            regularAltStatesObj.value = "";
            regularAltStatesObj.style.display = "";
        }    
        else
        {
            USAAltStatesObj.style.display = "";    
            USAAltStatesObj.value = USAStatesObj[USAStatesObj.selectedIndex].value;
            regularAltStatesObj.value = USAStatesObj[USAStatesObj.selectedIndex].value;
            regularAltStatesObj.style.display = "none";
        }        
    }   
    
    //Finally update cart in case need to add tax;
    if ((CountriesObj[CountriesObj.selectedIndex] == "US") || (CountriesObj[CountriesObj.selectedIndex] == "CA"))
    {
        updateShoppingCartStates(USAAltStatesObj[USAAltStatesObj.selectedIndex].value,USAStatesObj[USAStatesObj.selectedIndex].value);
    }  
    else
    {
        updateShoppingCartStates("0", "0");
    }  
}

function updateHiddenUSAState()
{
    USAAltStatesObj = document.getElementById("ddlAltUSAStates"); 
    regularAltStatesObj = document.getElementById("tbxAltState");        
    
    regularAltStatesObj.value = USAAltStatesObj[USAAltStatesObj.selectedIndex].value;   
    
    //Finally update cart in case need to add shipping for AK and HI;
    AltCountriesObj = document.getElementById("ddlAltCountries");
    if ((AltCountriesObj[AltCountriesObj.selectedIndex].value == "US") || (AltCountriesObj[AltCountriesObj.selectedIndex].value == "CA"))
    {
        updateShoppingCartStates(USAAltStatesObj[USAAltStatesObj.selectedIndex].value, USAStatesObj[USAStatesObj.selectedIndex].value);
    }  
    else
    {
        updateShoppingCartStates("0", "0");
    }      
}

function resetAddress()
{
    ShipToObj = document.getElementById("ddlShipTo");
    
    if (ShipToObj[ShipToObj.selectedIndex].value == "billing")
    {
        Address1Obj = document.getElementById("tbxAddress1");
        AltAddress1Obj = document.getElementById("tbxAltAddress1");
        AltAddress1Obj.value=Address1Obj.value;
        AltAddress1Obj.disabled = true;        
        AltAddress1Obj.style.backgroundColor="#ebebe4";
        AltAddress1Obj.style.border="1px solid #7f9db9";
        AltAddress1Obj.style.height="17px";
        
        Address2Obj = document.getElementById("tbxAddress2");
        AltAddress2Obj = document.getElementById("tbxAltAddress2");
        AltAddress2Obj.value=Address2Obj.value;
        AltAddress2Obj.disabled=true;
        AltAddress2Obj.style.backgroundColor="#ebebe4";
        AltAddress2Obj.style.border="1px solid #7f9db9";
        AltAddress2Obj.style.height="17px";        

        CityObj = document.getElementById("tbxCity");
        AltCityObj = document.getElementById("tbxAltCity");
        AltCityObj.value=CityObj.value;
        AltCityObj.disabled=true;
        AltCityObj.style.backgroundColor="#ebebe4";
        AltCityObj.style.border="1px solid #7f9db9";
        AltCityObj.style.height="17px";
        
        StateObj = document.getElementById("tbxState");
        AltStateObj = document.getElementById("tbxAltState");
        AltStateObj.value=StateObj.value;
        AltStateObj.disabled=true;
        AltStateObj.style.backgroundColor="#ebebe4";
        AltStateObj.style.border="1px solid #7f9db9";
        AltStateObj.style.height="17px";    
        
        USAStateObj = document.getElementById("ddlUSAStates");                  
        AltUSAStateObj = document.getElementById("ddlAltUSAStates");
        AltUSAStateObj.disabled = true;
        AltUSAStateObj.selectedIndex=USAStateObj.selectedIndex;        

        CountriesObj = document.getElementById("ddlCountries");
        AltCountriesObj = document.getElementById("ddlAltCountries");
        AltCountriesObj.disabled=true;
        AltCountriesObj.selectedIndex=CountriesObj.selectedIndex;        

       if ((CountriesObj[CountriesObj.selectedIndex].value != "US") && (CountriesObj[CountriesObj.selectedIndex].value != "CA"))
        {
            AltUSAStateObj.style.display = "none";
            AltStateObj.value = StateObj.value;
            AltStateObj.style.display = "";
        }    
        else
        {
            AltUSAStateObj.style.display = "";    
            AltStateObj.value = USAStateObj[USAStateObj.selectedIndex].value;
            AltStateObj.style.display = "none";
        }  

        PostcodeObj = document.getElementById("tbxPostcode");
        AltPostcodeObj = document.getElementById("tbxAltPostcode");
        AltPostcodeObj.value=PostcodeObj.value;
        AltPostcodeObj.disabled=true;
        AltPostcodeObj.style.backgroundColor="#ebebe4";
        AltPostcodeObj.style.border="1px solid #7f9db9";
        AltPostcodeObj.style.height="17px";              
    }
    else
    {
        AltAddress1Obj = document.getElementById("tbxAltAddress1");
        AltAddress1Obj.value="";
        AltAddress1Obj.disabled = false;
        AltAddress1Obj.style.backgroundColor="#FFFFFF";
        AltAddress1Obj.style.border="1px solid #7f9db9";        
        AltAddress1Obj.style.height="17px";        
        
        AltAddress2Obj = document.getElementById("tbxAltAddress2");
        AltAddress2Obj.value="";
        AltAddress2Obj.disabled=false;
        AltAddress2Obj.style.backgroundColor="#FFFFFF";
        AltAddress2Obj.style.border="1px solid #7f9db9";        
        AltAddress2Obj.style.height="17px";        
        
        AltCityObj = document.getElementById("tbxAltCity");
        AltCityObj.value="";
        AltCityObj.disabled=false;
        AltCityObj.style.backgroundColor="#FFFFFF";
        AltCityObj.style.border="1px solid #7f9db9"; 
        AltCityObj.style.height="17px";               
        
        AltStateObj = document.getElementById("tbxAltState");
        AltStateObj.value="";
        AltStateObj.disabled=false;
        AltStateObj.style.backgroundColor="#FFFFFF";
        AltStateObj.style.border="1px solid #7f9db9";  
        AltStateObj.style.height="17px";              
        
        AltUSAStateObj = document.getElementById("ddlAltUSAStates");
        AltUSAStateObj.disabled = false;
        
        AltCountriesObj = document.getElementById("ddlAltCountries");
        AltCountriesObj.disabled=false;
        
        AltPostcodeObj = document.getElementById("tbxAltPostcode");
        AltPostcodeObj.value="";
        AltPostcodeObj.disabled=false;
        AltPostcodeObj.style.backgroundColor="#FFFFFF";
        AltPostcodeObj.style.border="1px solid #7f9db9";  
        AltPostcodeObj.style.height="17px";              
    }

    //Finally update cart in case need to add tax and/or shipping info;
    if ((AltCountriesObj[AltCountriesObj.selectedIndex].value == "US") || (AltCountriesObj[AltCountriesObj.selectedIndex].value == "CA"))
    {
        updateShoppingCartStates(AltUSAStateObj[AltUSAStateObj.selectedIndex].value, USAStateObj[USAStateObj.selectedIndex].value);
    }  
    else
    {
        updateShoppingCartStates("0", "0");
        AltUSAStateObj.selectedIndex = 0;
    } 
}

function updateShoppingCart() {
    
    QuantityDDLObj = document.getElementById("ddlQuantity");
    Quantity = QuantityDDLObj[QuantityDDLObj.selectedIndex].value;
    
    if (Quantity != 9)
    {
        cartQuantityObj = document.getElementById("cartQuantity");
        cartQuantityPluralObj = document.getElementById("cartQuantityPlural");
        cartSubTotalObj = document.getElementById("cartSubTotal");
        cartShippingObj = document.getElementById("cartShipping");
        cartTaxObj = document.getElementById("cartTax");
        cartTotalObj = document.getElementById("cartTotal");
        
        tax = 0;
        shipping = 0;
        
        cartQuantityPluralObj.style.display = "";    
        
        if (Quantity == 1)
        {
            shipping = 4.95;
            cartQuantityPluralObj.style.display = "none";
        }
        else if (Quantity == 2)
        {
            shipping = 5.95;
        }
        else if (Quantity == 3)
        {
            shipping = 6.95;
        }
        else
        {
            shipping = 8.95;
        }

        //December 2007: Add check for special offer
        //If special offer, revert shipping to zero
        fullurl = window.location.href;
        offer = fullurl.substring(fullurl.indexOf('?')+7, fullurl.length)
        if (offer == "fs")
        {
            shipping = 0;
        }
        //December 2007 End

        
        cartQuantityObj.innerHTML = Quantity;
        Subtotal = Quantity * 19.95;
        cartSubTotalObj.innerHTML = pad_with_zeros((Math.round(Subtotal*100)/100), 2);
        
        Total = Subtotal + shipping + tax;
        cartTotalObj.innerHTML = pad_with_zeros((Math.round(Total*100)/100),2);
        
        cartShippingObj.innerHTML = pad_with_zeros((Math.round(shipping*100)/100),2);
    }
    else
    {
        alert ("Thanks for your interest in the TriggerTriangle. For orders of more than 8 units, please contact customer service via the Contact Us link on the bottom of the page.");
        QuantityDDLObj.selectedIndex = 0;
    }
}

function updateShoppingCartStates(state, billingstate) {
    cartQuantityObj = document.getElementById("cartQuantity");
    Quantity = parseFloat(cartQuantityObj.innerHTML);
    
    cartSubTotalObj = document.getElementById("cartSubTotal");
    cartShippingObj = document.getElementById("cartShipping");
    cartTaxObj = document.getElementById("cartTax");
    cartTotalObj = document.getElementById("cartTotal");
    
    CountriesAltObj = document.getElementById("ddlAltCountries");

    shipping = 0;    
    if (Quantity == 1)
    {
        shipping = 4.95;
    }
    else if (Quantity == 2)
    {
        shipping = 5.95;
    }
    else if (Quantity == 3)
    {
        shipping = 6.95;
    }
    else
    {
        shipping = 8.95;
    }   
       
       
    DeliveryDaysObj = document.getElementById("DeliveryDays");
    if ((CountriesAltObj[CountriesAltObj.selectedIndex].value != "US") || (state == "HI") || ((state == "AK")))
    {
        shipping = shipping + 3.95;
        //Jan 2007: Not needed for now
        //DeliveryDaysObj.innerHTML = "&nbsp(allow 21 days delivery):";
    }
    else
    {
        //Jan 2007: Not needed for now
        //DeliveryDaysObj.innerHTML = "&nbsp(allow 14 days delivery):";    
        
        //December 2007: Add check for special offer
        //If special offer, revert shipping to zero
        fullurl = window.location.href;
        offer = fullurl.substring(fullurl.indexOf('?')+7, fullurl.length)
        if (offer == "fs")
        {
            shipping = 0;
        }
        //December 2007 End         
    }

    TotalNoTax = (Quantity * 19.95) + shipping;
    tax = 0;
    if (billingstate == "CA")
    {
        tax = TotalNoTax * .0825;
    }
    else if (billingstate == "MI")
    {
        tax = TotalNoTax * .06;
    }
    else
    {
        tax = 0;
    }
    
    cartQuantityObj.innerHTML = Quantity;
    Subtotal = Quantity * 19.95;
    cartSubTotalObj.innerHTML = pad_with_zeros((Math.round(Subtotal*100)/100), 2);
    
    Total = Subtotal + shipping + tax;
    cartTotalObj.innerHTML = pad_with_zeros((Math.round(Total*100)/100),2);
    
    cartShippingObj.innerHTML = pad_with_zeros((Math.round(shipping*100)/100),2);
    cartTaxObj.innerHTML = pad_with_zeros((Math.round(tax*100)/100),2);
}





function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}



/* Open a new popup window */
function selectPopUpWindow(filename) {
    if (opener && opener.closed)            
    {
        // do nothing
    }
    else
    {
      	var destination = "/" + filename;	
        var windowName = window.open(destination, "windowName", "resizable=no, scrollbars=no, width=375, height=210");
        windowName.blur()
    }
}

/* Close the popup window */
function closePopUpWindow(filename)		{
    if (window.name == "windowName")            
    {
        // do nothing
    }
    else
    {
	    var destination = "/" + filename;	
        var windowName = window.open(destination,"windowName");
	    if (windowName && !windowName.closed)  {
    		windowName.close();
    	}
	}
}

/* Focus on the popup window */
function focusPopUpWindow(filename)		
{
    if (event.clientY < 0) 
    {
        var destination = "/" + filename;	
        var windowName = window.open(destination,"windowName", "resizable=yes, scrollbars=yes");
	    if (windowName && !windowName.closed)  
	    {
		    windowName.focus();		
	    }
	}
}

function preventDoubleClick()
{
    var submitButtonObj = document.getElementById("btnSubmit");
    var cbxTermsObj = document.getElementById("cbxTerms");
    
    if (cbxTermsObj.checked == true)
    {   
        submitButtonObj.disabled = true;
        return true;
    }
    else
    {
        submitButtonObj.disabled = false;
        return false;
    }
}