﻿// #############################
// #  submission methods
// #############################

// User to submit the options to the cart
function addOptionsToCart()
{
    // validate the options
    if (!validateOptions())
    {
        return;    
    }
    
    // if a prescription is present then go directly to the cart
    var hasPrescriptionValue = document.getElementById("hasPrescription").value
    if (hasPrescriptionValue == "false")
    {
        obj = document.getElementById('frameOptions');
        obj.style.display='block';
                
        AjaxQueue.push(jsWebRoot + 'ajax/addOptionsToCart.aspx?nextStep=1&' + makeOptionQueryString(), {onSucess: ajaxResponse, onFailure: ajaxResponse}, "frameOptions"); 
        setTimeout('showMiniCart()', 1000);

    }
    else
    {
        AjaxQueue.push(jsWebRoot + 'ajax/addOptionsToCart.aspx?nextStep=1&' + makeOptionQueryString(), {onSucess: ajaxResponse, onFailure: ajaxResponse}); 
        document.location.href = jsWebRoot + "checkout.aspx";
    }
    
}

function updateAndProceed()
{
    document.getElementById('hasPrescription').value='false';
    addOptionsToCart();
}

function goToOptions()
{
    AjaxQueue.push(jsWebRoot + 'ajax/addOptionsToCart.aspx?nextStep=0&' + makeKvp("frameUid", document.getElementById("frameUid").value), {onSucess: ajaxResponse, onFailure: ajaxResponse}, "frameOptions"); 
    
    setTimeout('filterAllOptions()', 1000);   
}

// Used to load the options data from the cart
function loadOptionsFromCart()
{
    obj = document.getElementById('frameOptions');
    obj.style.display='block';
        
    AjaxQueue.push(jsWebRoot + 'ajax/addOptionsToCart.aspx?nextStep=0&frameUid=' + frameUid,{onSucess: ajaxResponse, onFailure: ajaxResponse}, "frameOptions"); 

    setTimeout('filterAllOptions()', 1000);
}

// Validated the required fields
function validateOptions()
{

    var lenseType = document.getElementById('lensType');
    //alert ('inside validate() : ' + lenseType.selectedIndex);
    if (lenseType.selectedIndex == 4){
        alert("Please select a glasses type before continuing.");
        return false;
    }else{
        return true;
    }
}
// trap ajax success or failuure
function ajaxResponse()
{

}

// determines the selected options and returns a formatted querystring
function makeOptionQueryString()
{
    // hold the price as it is calculated
    var querystring;
    querystring = "";
    
    // Get frame UID
    querystring += makeKvp("frameUid", document.getElementById("frameUid").value); 
    
    // Lense Type
    querystring += makeKvp("lenseType", getSelectData("lensType").ID); 
    
    // Thin Lense
    if (isChecked("lensThin"))
    {
        querystring += makeKvp("lensThin", getCheckData("lensThin").ID); 
    }
    else
    {
        querystring += makeKvp("lensThin", 0); 
    }
    
    // Thin Lense
    if (isChecked("lensUltraThin"))
    {
        querystring += makeKvp("lensUltraThin", getCheckData("lensUltraThin").ID); 
    }
    else
    {
        querystring += makeKvp("lensUltraThin", 0); 
    }
    
    // Lense Tint
    querystring += makeKvp("lensTint", getSelectData("lensTint").ID); 
    
    // Lense Coatings Scratch
    if (isChecked("lensCoatingAntiScratch"))
    {
        querystring += makeKvp("lensCoatingAntiScratch", getCheckData("lensCoatingAntiScratch").ID); 
    }
    else
    {
        querystring += makeKvp("lensCoatingAntiScratch", 0); 
    }
    
    // Lense Coatings Reflect
    if (isChecked("lensCoatingAntiReflect"))
    {
        querystring += makeKvp("lensCoatingAntiReflect", getCheckData("lensCoatingAntiReflect").ID); 
    }
    else
    {
        querystring += makeKvp("lensCoatingAntiReflect", 0); 
    }
    
    // Polorisation    
    //querystring += makeKvp("lensPolarised", getSelectData("lensPolarised").ID); 
  
    // Transition    
    querystring += makeKvp("lensPhotochromic", getSelectData("lensPhotochromic").ID); 
  
    // Complimentary Glasses Case
    if (isChecked("accessoriesCase"))
    {
        querystring += makeKvp("accessoriesCase", getCheckData("accessoriesCase").ID); 
    }
    else
    {
        querystring += makeKvp("accessoriesCase", 0); 
    }

    // Complimentary Lens Cloth
    if (isChecked("accessoriesCloth"))
    {
        querystring += makeKvp("accessoriesCloth", getCheckData("accessoriesCloth").ID); 
    }
    else
    {
        querystring += makeKvp("accessoriesCloth", 0); 
    }

    return querystring;
}

// #############################
// #  in page dynamics
// #############################
function filterAllOptions()
{
   
        //alert("Filtering options");
        if (document.getElementById("hasPrescription"))
        {
            filterThinLensOptions();
            filterUltraThinLensOptions();
            filterLensTypeOptions();
        }
  
}
    

function filterLensTypeOptions()
{
    //alert(isAuthenticatedAsAdministrator);
    if (!isAuthenticatedAsAdministrator)
    {
        var selIndex = document.getElementById('lensType').selectedIndex;
        //alert ('inside filterOptions():' + selIndex);
        //return new productData(tag.options[tag.selectedIndex].value);

        // reading, distance and intermediate all options are available
        // DO NOTHING

        // bifocals and varifocals - all options off, Home Visit required
        if ((selIndex == 2) || (selIndex == 3))
        {
            //alert ('inside filterOptions()XXXXX');
            // disable thin lenses
            document.getElementById("lensThin").checked = false;
            document.getElementById("lensThin").disabled = true;
            document.getElementById("labelThinLenses").style.color = '#999999';
            
            // disable ultra-thin lenses
            document.getElementById("lensUltraThin").checked = false;
            document.getElementById("lensUltraThin").disabled = true;
            document.getElementById("labelUltraThinLenses").style.color = '#999999';
            
            // disable lens tints
            document.getElementById("lensTint").options[document.getElementById("lensTint").options.length-1].selected = true;
            document.getElementById("lensTint").disabled = true;

            // disable lens tints
            document.getElementById("lensCoatingAntiReflect").checked = false;
            document.getElementById("lensCoatingAntiReflect").disabled = true;
            document.getElementById("labelCoatingAntiReflect").style.color = '#999999';
            
            // disable lens tints
            document.getElementById("lensPhotochromic").options[document.getElementById("lensPhotochromic").options.length-1].selected = true;
            document.getElementById("lensPhotochromic").disabled = true;
            
            // disable standard $0 options
            document.getElementById("labelCoatingAntiScratch").style.color = '#999999';
            document.getElementById("labelAccessoriesGlassCase").style.color = '#999999';
            document.getElementById("labelAccessoriesLensCloth").style.color = '#999999';        
            
        } else {
            // enable thin lenses
            document.getElementById("lensThin").disabled = false;
            document.getElementById("labelThinLenses").style.color = '#000000';
            
            // enable ultra-thin lenses
            document.getElementById("lensUltraThin").disabled = false;
            document.getElementById("labelUltraThinLenses").style.color = '#000000';
            
            // enable lens tints
            document.getElementById("lensTint").disabled = false;

            // enable lens tints
            document.getElementById("lensCoatingAntiReflect").disabled = false;
            document.getElementById("labelCoatingAntiReflect").style.color = '#000000';
            
            // enable lens tints
            document.getElementById("lensPhotochromic").disabled = false;

            // enable standard $0 options
            document.getElementById("labelCoatingAntiScratch").style.color = '#000000';
            document.getElementById("labelAccessoriesGlassCase").style.color = '#000000';        
            document.getElementById("labelAccessoriesLensCloth").style.color = '#000000'; 
            
        }
    }
     

}

function filterThinLensOptions() {
    //alert('inside filterThinLensOptions()');
    if ( document.getElementById("lensThin").checked == true ) {
        // disable ultra-thin lenses
        document.getElementById("lensUltraThin").disabled = true;
        document.getElementById("labelUltraThinLenses").style.color = '#999999';
    } else {
        // enable ultra-thin lenses
        document.getElementById("lensUltraThin").disabled = false;
        document.getElementById("labelUltraThinLenses").style.color = '#000000';
    }
}

function filterUltraThinLensOptions() {
    //alert('inside filterThinLensOptions()');
    if ( document.getElementById("lensUltraThin").checked == true ) {
        // disable thin lenses
        document.getElementById("lensThin").disabled = true;
        document.getElementById("labelThinLenses").style.color = '#999999';
    } else {
        // enable thin lenses
        document.getElementById("lensThin").disabled = false;
        document.getElementById("labelThinLenses").style.color = '#000000';
    }
}

// recalculates the price for the selected options and displays it the page
function reCalcPrice()
{    
    // price holder
    var price;
    price = 0;
    
    // Lense Type
    price += getSelectData("lensType").RRP; 

    // Thin Lenses
    if (isChecked("lensThin"))
    {
        price += getCheckData("lensThin").RRP;
    }
    
    // Ultra Thin Lenses
    if (isChecked("lensUltraThin"))
    {
        price += getCheckData("lensUltraThin").RRP;
    }
    
    // Lense Tint
    price += getSelectData("lensTint").RRP;
    
    // Lense Coatings Scratch
    if (isChecked("lensCoatingAntiScratch"))
    {
        price += getCheckData("lensCoatingAntiScratch").RRP;
    }
    
    // Lense Coatings Reflect
    if (isChecked("lensCoatingAntiReflect"))
    {
        price += getCheckData("lensCoatingAntiReflect").RRP;
    }
    
    // Polorisation    
    //price += getSelectData("lensPolarised").RRP;
  
    // Transition    
    price += getSelectData("lensPhotochromic").RRP;
  
    // Accessory Case
    if (isChecked("accessoriesCase"))
    {
        price += getCheckData("accessoriesCase").RRP;
    }
    
    // Complimentary Lens Cloth
    if (isChecked("accessoriesCloth"))
    {
        price += getCheckData("accessoriesCloth").RRP;
    }
    
    // Set price in page
    var framePrice = document.getElementById("framePrice");
    var optionPrice = document.getElementById("optionPrice");
    optionPrice.innerHTML = price.toFixed(2);
    
    var totalPrice = document.getElementById("totalPrice");
    var total = parseFloat(optionPrice.innerHTML) + parseFloat(framePrice.innerHTML);
    totalPrice.innerHTML = "$" + total.toFixed(2);
}

// #############################
// #  Objects
// #############################

// sets the passed data into a product data object
// the input format is "ID:RRP"
function productData(data)
{
    var splitData = data.split(':');
    this.ID = parseInt(splitData[0]);
    this.RRP = parseFloat(splitData[1]);
}

// #############################
// #####  Helper functions
// #############################

// retrieves a productData object from a select list 
function getSelectData(id)
{
    var tag = document.getElementById(id);
    return new productData(tag.options[tag.selectedIndex].value);
}

// retrieves a productData object from a check box
function getCheckData(id)
{
    var tag = document.getElementById("hid" + id);
    return new productData(tag.value);   
}

// determines if a checkbox is checked
function isChecked(id)
{
    return document.getElementById(id).checked;
}

// formats up a querystring kvp's
function makeKvp(key, value)
{
    return key + "=" + value + "&";
}