/**
 * Toggles visibility of hidden elements.
 * 
 * 
 * 
 */
function toggle(targetID) {
    if (document.getElementById && document.getElementById(targetID)){
        target = document.getElementById(targetID);
        if (target.style.display == 'none'){
            target.style.display = "block";
        } else {
            target.style.display = "none";
        }
    }
}

/**
 * Prevents some browsers from showing that ugly border
 * around focussed elements.
 * 
 * 
 */
function killNastyLinkBorders() {
    if (document.getElementsByTagName) {
        aElements = document.getElementsByTagName('A');
        for (var i = 0; i < aElements.length; i++) {
            aElements[i].onfocus = function() { this.blur(); };
        }
    }
}

/**
 * Auto-updates cart when quantities are changed.
 * 
 * 
 * 
 */
function magicCartInputs() {
    if (document.getElementById && document.getElementsByTagName &&
    document.getElementById('cart')) {
        var cartInputs = document.getElementById('cart').getElementsByTagName('INPUT');
        for (var i = 0; i < cartInputs.length; i++) {
            cartInputs[i].setAttribute("autocomplete","OFF"); 
            cartInputs[i].onkeyup = function() {
                if (this.value.length >= 1) {
                    validateCartInput(this);
                }
            }; 
            cartInputs[i].onfocus = function() { this.select(); };
            cartInputs[i].onblur = function() {
                this.form.submit();
            };
        }
    }
}

/**
 * Makes sure only numbers are input into the qty field in the cart.
 * 
 * 
 * 
 */
function validateCartInput(str) {
    newstr = str.value.replace(/([^0-9]*)/g, '');
    str.value = newstr;
}

/**
 * Copies billing fields over to shipping fields.
 * 
 * 
 * 
 */
function billingToShipping(obj) {
    if (obj.sameInfo.checked) {
        obj.shippingname.value = obj.billingname.value;
        obj.shippingaddress1.value = obj.billingaddress1.value;
        obj.shippingaddress2.value = obj.billingaddress2.value;
        obj.shippingcity.value = obj.billingcity.value;
        obj.shippingstate.value = obj.billingstate.value;
        obj.shippingzip.value = obj.billingzip.value;
        obj.shippingcountry.value = obj.billingcountry.value;
        obj.shippingphone.value = obj.billingphone.value;
    } else {
        obj.shippingname.value = '';
        obj.shippingaddress1.value = '';
        obj.shippingaddress2.value = '';
        obj.shippingcity.value = '';
        obj.shippingstate.value = '';
        obj.shippingzip.value = '';
        obj.shippingcountry.value = '';
        obj.shippingphone.value = '';
    }
}


/**
 * Generic function that checks input lengths.
 * 
 * 
 * 
 */
function checkTextField(obj, msg) {
    if (!obj.value.length) {
        alert(msg);
        obj.focus();
        return false;
    } else {
        return true;
    }
}

/**
 * Submits the order form.
 * 
 * 
 * 
 */
function orderFormSubmit(obj, BASE_URL) {
    obj.action = BASE_URL + 'purchase/submit/';
    obj.submit();
}
