﻿/*************************
* Order Entry Scripts
**************************/
function cart(ele)
{
    var _eleFld = ele;
    var _items = new Array();
    
    if (_eleFld.value!="")
    {
        
        var lines = _eleFld.value.split('^');
        for(var i = 0; i<lines.length; i++)
        {
            var line = lines[i].split('|');
            var itm  = new item(line[0], line[1], line[2], line[3], line[4]);
            _items.push(itm);
        }
        
       //alert(_items.toString());
    }   
    

  
    this.getItems = function()
    {
        return _items;
    }
    
    this.clear = function()
    {
        _items = new Array();
        this.save();
    }
    
    this.add = function(itemCode, desription, qty, priceEach, cvEach)
    {
        var itm     = new item(itemCode, desription, qty, priceEach, cvEach);
        var found   = false;
        
        for (i=0;i<_items.length;i++)
        {
            //replace it if found
            if (itm.itemCode == _items[i].itemCode)
            {
                _items[i] = itm;
                found=true;
                break;
            }
        }
        
        //add it in if not found
        if (!found) _items.push(itm);
        
        this.save();
    }
    
    this.remove = function(itemCode)
    {
        foundI = -1;
        
        for(i=0; i<_items.length;i++)
        {
            if (itemCode == _items[i].itemCode)
            {
                foundI = i;
                break;
            }
        }
        
        if (foundI>-1)
        {
            newItems = new Array();
            
            for(i=0; i<_items.length;i++)
            {
                if (i!=foundI)
                    newItems.push(_items[i]);
            }
            
            _items = newItems;
        }
        this.save();
    }
    
    this.getSubTotal = function()
    {
        var num = 0.0;
                
        if (_eleFld.value!="")
        {
            var lines = _eleFld.value.split('^');
            var line = lines[0].split('|');
            var itm  = new item(line[0], line[1], line[2], line[3], line[4]);
           // if(line[0] == "MPP") num = -30.0;  
        }    
         
        for(i=0; i<_items.length;i++)
        {   
            num += (_items[i].qty * _items[i].priceEach);
        }
        
        return num.toFixed(2);
    }
    
    this.getCvTotal = function()
    {
        var num = 0.0;
        for(i=0; i<_items.length;i++)
        {   
            num += (_items[i].qty * _items[i].cvEach);
        }
        return num.toFixed(2);
    }
    
    this.save = function()
    {
        if (_items.length==0)
            _eleFld.value = "";
        else
            _eleFld.value = this.toString();
    }
    
    this.toHTML = function()
    {
        return this.toString();
    }
    
    this.toString = function()
    {
        return _items.join("^");
    }
    
    this.CvNeeded = 0.0;
    this.requireMissingCv = true;
    
    this.getMissingCv = function()
    {
        var cv = this.getCvTotal();
        var missing = 0.0;
        
        if (cv < this.CvNeeded)
            missing = this.CvNeeded - cv;
        
        return missing.toFixed(2);
    }

    this.PriceNeeded = 0.0;
    this.requireMissingPrice = true;

    this.getMissingPrice = function () {
        var TotPrice = this.getSubTotal();
        var missingPr = 0.0;

        if (TotPrice < this.PriceNeeded)
            missingPr = this.PriceNeeded - TotPrice;

        return missingPr.toFixed(2);
    }

}

function item(itemCode, desription, qty, priceEach, cvEach)
{
    this.itemCode       = itemCode;
    this.description    = desription;    
    this.qty            = parseFloat(qty);          if (isNaN(this.qty))        this.qty = 1;
    this.priceEach      = parseFloat(priceEach);    if (isNaN(this.priceEach))  this.priceEach = 0;
    this.cvEach         = parseFloat(cvEach);       if (isNaN(this.cvEach))     this.cvEach = 0;
    
    this.getFormattedTotal = function()
    {
        var num = 0.0;
        num = priceEach*qty;
        return "$" + num.toFixed(2);
    }
    
    this.toString = function()
    {
         return itemCode + '|' + desription + "|" + qty + "|" + priceEach + "|" + cvEach;
    }
}

function MergeCarts() {

    var items = shapingCart.getItems();

    for (x = 0; x < items.length; x++) {
        currentCart.add(
            items[x].itemCode,
            items[x].description,
            items[x].qty,
            items[x].priceEach,
            items[x].cvEach);
    }
}

function processItem(i)
{
    //eleTD1      = document.getElementById('ca' + i);
    //eleTD2      = document.getElementById('cb' + i);
    eleItemCode = document.getElementById('itemCode' + i);
    eleQty      = document.getElementById('qty' + i);
    
    qty = parseInt(eleQty.value);
    
    //hey if we are in a drop down, take no chances.
    if (eleItemCode.tagName=="SELECT")
    {
       for (x=0; x<eleItemCode.options.length;x++)
       {
        currentCart.remove(eleItemCode.options[x].value);
       }
        
    }
    
    
    
    if (isNaN(qty) || qty==0)
    {
        eleQty.value='';
        
        
        
        currentCart.remove(eleItemCode.value);
 
        
        //eleTD1.style.background   = '#e2cc92';
        //eleTD2.style.background   = '#e2cc92';        
    }
    else
    {
        eleDescription  = document.getElementById('itemD' + i);
        eleQty          = document.getElementById('qty' + i);
        elePrice        = document.getElementById('prc' + i);
        eleCv           = document.getElementById('cv' + i);

        
        var description;
        
        if (eleItemCode.tagName=="SELECT")
        {
            description = eleDescription.innerHTML + ' - ' + eleItemCode.options[eleItemCode.selectedIndex].innerHTML;
        }
        else
        {
            description = eleDescription.innerHTML;
        }
        
        currentCart.add(
            eleItemCode.value,
            description,
            qty,
            elePrice.innerHTML,
            eleCv.innerHTML);        
        
        //eleTD1.style.background   = 'yellow';
        //eleTD2.style.background   = 'yellow';
    }

    displaycurrentCart();
    GetTotalCV();
}



function processshapingItem(i) {

    
        eleItemCode = document.getElementById('itemCode' + i);
        eleQty = document.getElementById('qty' + i);

        qty = eleQty.value;


        eleDescription = document.getElementById('itemD' + i);
        elesizeDesc = document.getElementById('itemCode' + i);

        eleQty = document.getElementById('qty' + i);
        elePrice = document.getElementById('prc' + i);
        eleCv = document.getElementById('cv' + i);

        if (isNaN(qty) || qty <= 0) {
            eleQty.value = '';



            currentCart.remove(eleItemCode.value);


            //eleTD1.style.background   = '#e2cc92';
            //eleTD2.style.background   = '#e2cc92';        
        }
        else 
        {
        
            shapingCart.add(
            eleItemCode.value,
            eleDescription.innerHTML + " " + elesizeDesc.options[elesizeDesc.selectedIndex].text,
            eleQty.value,
            elePrice.value,
            eleCv.value);


            MergeCarts();
            displaycurrentCart();
        }
  
}

function displaycurrentCart()
{
    eleCart = document.getElementById("cartPreview");
  
    var s = "";
  
    if (currentCart.getMissingCv() > 0 && currentCart.requireMissingCv)
    {
        s+= "<div class='nextButton disabled'>" + labelYouNeed1 + "<b> " + currentCart.getMissingCv() + "</b> " + labelYouNeed2 + "</div>";  
    }
    else
    {      
        s+= "<a href='javascript:gotoNext()' class='nextButton'>" + labelNextStep + "</a>";
    }
    s += "<a href='javascript:gotoBack()'>Go back to the previous step</a><br /><br />";
    s += "<b>" + labelSelected + "</b>";
    s += "<table width='205px'>";
    
    var items = currentCart.getItems();
    
    for (x = 0; x < items.length; x++)
    {
        if(x > 0)
        {
            s+= "<tr><td colspan='2'><div style='display: block; height: 1px; width: 100%; background-color: #CCC;'></div></td></tr>";
        }
        s+= "<tr>" +
        "<td class='quantity' valign='top'>" + items[x].qty + "</td>" +
        "<td class='description' valign='top'>" + items[x].description + "</td>" + 
        "</tr>";
    }
    
    s+="</table>";
    s+="<hr />";
    
    s+= "<table width='205px'><tr><td align='right' class='subtotalLabel' valign='top'>" + labelSubTotal + ":</td><td align='right' class='subtotalValue'>$" +
        currentCart.getSubTotal() +
        "</td></tr>";

    s+= "<tr><td align='right' class='qpLabel' valign='top'>" + labelCV + ":</td><td align='right' class='qpValue'>" +
        currentCart.getCvTotal() +
        "</td></tr>";
        
    
    s += "</table>";
    
    if (cartFooter!="")
    {
        s+="<br /><div width='205px'>" + cartFooter + "</div>";
    }

    eleCart.innerHTML = s;



}

function GetTotalCV() {
    var CVTotoal = currentCart.getCvTotal();
    createCookie("QPValue", CVTotoal, 1);
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date("July 21, 2100 01:15:00");
        //date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));


        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";

}


function displaycurrentCartWithPrice() {
    eleCart = document.getElementById("cartPreview");

    var s = "";

    if (currentCart.getMissingPrice() > 0 && currentCart.requireMissingPrice) {
        s += "<div class='nextButton disabled'>" + labelYouNeed1 + "<b> " + currentCart.getMissingPrice() + "</b> " + labelYouNeed2 + "</div>";
    }
    else {
        s += "<a href='javascript:gotoNext()' class='nextButton'>" + labelNextStep + "</a>";
    }
    s += "<a href='javascript:gotoBack()'>Go back to the previous step</a><br /><br />";
    s += "<b>" + labelSelected + "</b>";
    s += "<table width='205px'>";

    var items = currentCart.getItems();

    for (x = 0; x < items.length; x++) {
        if (x > 0) {
            s += "<tr><td colspan='2'><div style='display: block; height: 1px; width: 100%; background-color: #CCC;'></div></td></tr>";
        }
        s += "<tr>" +
        "<td class='quantity' valign='top'>" + items[x].qty + "</td>" +
        "<td class='description' valign='top'>" + items[x].description + "</td>" +
        "</tr>";
    }

    s += "</table>";
    s += "<hr />";

    s += "<table width='205px'><tr><td align='right' class='subtotalLabel' valign='top'>" + labelSubTotal + ":</td><td align='right' class='subtotalValue'>$" +
        currentCart.getSubTotal() +
        "</td></tr>";

    s += "<tr><td align='right' class='qpLabel' valign='top'>" + labelCV + ":</td><td align='right' class='qpValue'>" +
        currentCart.getCvTotal() +
        "</td></tr>";


    s += "</table>";

    if (cartFooter != "") {
        s += "<br /><div width='205px'>" + cartFooter + "</div>";
    }

    eleCart.innerHTML = s;
}

function processItemForPrefCust(i) {
    //eleTD1      = document.getElementById('ca' + i);
    //eleTD2      = document.getElementById('cb' + i);
    eleItemCode = document.getElementById('itemCode' + i);
    eleQty = document.getElementById('qty' + i);

    qty = parseInt(eleQty.value);

    //hey if we are in a drop down, take no chances.
    if (eleItemCode.tagName == "SELECT") {
        for (x = 0; x < eleItemCode.options.length; x++) {
            currentCart.remove(eleItemCode.options[x].value);
        }

    }



    if (isNaN(qty) || qty == 0) {
        eleQty.value = '';



        currentCart.remove(eleItemCode.value);


        //eleTD1.style.background   = '#e2cc92';
        //eleTD2.style.background   = '#e2cc92';        
    }
    else {
        eleDescription = document.getElementById('itemD' + i);
        eleQty = document.getElementById('qty' + i);
        elePrice = document.getElementById('prc' + i);
        eleCv = document.getElementById('cv' + i);


        var description;

        if (eleItemCode.tagName == "SELECT") {
            description = eleDescription.innerHTML + ' - ' + eleItemCode.options[eleItemCode.selectedIndex].innerHTML;
        }
        else {
            description = eleDescription.innerHTML;
        }

        currentCart.add(
            eleItemCode.value,
            description,
            qty,
            elePrice.innerHTML,
            eleCv.innerHTML);

        //eleTD1.style.background   = 'yellow';
        //eleTD2.style.background   = 'yellow';
    }

    displaycurrentCartWithPrice();
}

