
var icebones = {};

icebones.order = function() {
    
    /* constants */
    var NY_TAX   = 0.08125;
    var PRICE    = 8.99;
    var SHIPCOST = 3.25;
    
    /* inputs */
    var _qty, _sub, _ship, _ny, _tax, _total;
    
    var _calc = function() {
        var _v       = parseFloat(_sub.value) + parseFloat(_ship.value);
        _v           = _v.toFixed(2);
        var tax      = _ny.checked ? _v * NY_TAX: 0;
        _tax.value   = tax.toFixed(2);
        var t        = parseFloat(_v) + parseFloat(_tax.value);
        _total.value = t.toFixed(2);
        return true;
    }

    var isEmail_re       = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
    function isEmail(s) {
       return String(s).search (isEmail_re) != -1;
    }
    
    return {
    
        init: function() {
            _qty   = document.getElementById('qty');
            _sub   = document.getElementById('subtotal');
            _ship  = document.getElementById('shipping');
            _ny    = document.getElementById('ny');
            _tax   = document.getElementById('tax'); 
            _total = document.getElementById('total');
            
            /*
            _qty.onchange = function() {
                var sub     = PRICE * parseFloat(_qty.value)
                _sub.value  = sub.toFixed(2);
                var ship    = SHIPCOST * parseFloat(_qty.value)
                _ship.value = ship.toFixed(2);
                _calc();
                return true;
            }*/


            _qty.onkeyup = function(e) {
                e = e || window.event;
                if (e.keyCode === 35 || e.keyCode === 36 || e.keyCode === 37 || e.keyCode === 39 )
                    return true;
                var qty = _qty.value || 0;
                if (qty !== 0 && e.keyCode !== 8) _qty.value = parseFloat(qty);
                var sub     = PRICE * parseFloat(qty )
                _sub.value  = sub.toFixed(2);
                var ship    = SHIPCOST * parseFloat(qty)
                _ship.value = ship.toFixed(2);
                _calc();
                return true;
            }
            
            _ny.onclick = _calc;    
            
            return true;
        },
        
        submit: function() {

            var errorEl = document.getElementById('order-error');
            errorEl.innerHTML = '';

            var _name  = document.getElementById('name');
            var _add1  = document.getElementById('address1');
            var _add2  = document.getElementById('address2');
            var _email = document.getElementById('email');
            var _cap   = document.getElementById('captcha_code');
            
            var html = document.createElement('ul');
            var el;
                        
            if (_name.value == '') {
                el = document.createElement('li');
                el.innerHTML = 'Name is a required field.';
                html.appendChild(el);               
            }

            if (_add1.value == '') {
                el = document.createElement('li');
                el.innerHTML = 'Address is a required field.';
                html.appendChild(el);
            }
            
            if (_add2.value == '') {
                el = document.createElement('li');
                el.innerHTML = 'City, State, ZipCode is a required field.';
                html.appendChild(el);
            }            

            if (_email.value == '') {
                el = document.createElement('li');
                el.innerHTML = 'Email is a required field.';
                html.appendChild(el);
            } else if(!isEmail(_email.value)) {
                el = document.createElement('li');
                el.innerHTML = 'Email is not a valid address.';
                html.appendChild(el);
            }
            
            if (_cap.value == '') {
                el = document.createElement('li');
                el.innerHTML = 'Please enter the code inside the image.';
                html.appendChild(el);
            }     
                        
            if (html.childNodes.length) {
                errorEl.appendChild(html)                
                document.getElementById('order-error-container').style.display = 'block';
                location.href = '#order-error-container';
                return false;                
            }
            return true;
            
        }
        
        
    } 
    
}()

