function hideButtons()
{
    buttonList = document.getElementsByTagName('input');
    for (i = 0; i < buttonList.length; i++)
    {
        if (   buttonList[i].type      == 'submit'
            && buttonList[i].className == 'gobutton')
        {
            buttonList[i].style.display    = 'none';
            buttonList[i].style.visible    = false;
        }
    }
}

function reload_cat(e)
{
    var locationString = location.href;

    var addOn = new Array();
    var doc   = document.forms[0];

    if (e.name == 'show' || e.name == 'page')
    {
        addOn[addOn.length]
            = 'sort=' + doc.sort.options[doc.sort.selectedIndex].value;
    }

    if (e.name == 'page' || e.name == 'sort')
    {
        addOn[addOn.length]
            = 'show=' + doc.show.options[doc.show.selectedIndex].value;
    }

    if (locationString.indexOf('?' != -1))
    {
        locationString
            = locationString.substring(
                0, locationString.indexOf('?')
            );
    }

    locationString
        = locationString + '?cat=' + currentCat + '&' + e.name + '='
            + e.options[e.selectedIndex].value;

    if (addOn.length)
    {
        locationString = locationString + '&' + addOn.join('&');
    }

    location.href = locationString;
}

function zoomWin (prod_id)
{
    window.open('showpic.php?prod_id=' + prod_id, 'prodWin', 'height=270,width=220,scrollbars=no,menubar=no,toolbar=no,resizable=no');
}

function showhide ()
{
    var form    = document.forms[0];
    var country = form && form.country;

    if (country && country.options[country.selectedIndex].value)
    {
        var selected = country.options[country.selectedIndex].value;
        document.getElementById('hide1').style.display = 'none';

        if (selected == canada_name)
        {
            document.getElementById('hide2').style.display = 'none';
            document.getElementById('hide3').style.display = 'block';
        }
        else
        {
            document.getElementById('hide2').style.display = 'block';
            document.getElementById('hide3').style.display = 'none';
        }
    }
}

function set_ddm_val (ddm, value)
{
    for (var i = 0; i < ddm.options.length; i++)
    {
        if (ddm.options[i].value == value)
        {
            ddm.selectedIndex = i;
        }
    }
}

function update_ship_list()
{
    var form               = document.forms[0];
    var country            = get_ddm_value(form.country);
    var selectedmethod     = get_ddm_value(form.shipmethod);
    form.shipmethod.length = 0;

    if (country == canada_name)
    {
        var province = get_ddm_value(form.province);

        for (var i = 0; i < shipping_list.length; i += 6)
        {
            if (province == shipping_list[i + 1])
            {
                form.shipmethod.options[form.shipmethod.options.length]
                    = new Option(shipping_list[i + 5], shipping_list[i + 4]);
            }
        }

        set_ddm_val(form.shipmethod, selectedmethod);
    }
    else
    {
        if (country == us_name)
        {
            var match = 'US';
        }
        else
        {
            var match = 'INT';
        }

        for (i = 0; i < shipping_list.length; i += 6)
        {
            if (shipping_list[i] == match)
            {
                form.shipmethod.options[form.shipmethod.options.length]
                    = new Option(shipping_list[i + 5], shipping_list[i + 4]);
            }
        }
    }
}

function get_ddm_value (formElement) {
    return formElement.options[formElement.selectedIndex].value;
}

function get_ddm_text (formElement) {
    return formElement.options[formElement.selectedIndex].text;
}

function check_form() {
    var form = document.forms[0];
    var error = new Array();
    var today = new Date;

    if (!form) {
        return true;
    }

    if (   get_ddm_value(form.paymethod) == 'MC'
        || get_ddm_value(form.paymethod) == 'VISA') {
        if (!verify_card_number(form.ccnumber.value)) {
            error.push('Credit card number is invalid!');
        }
        if (   get_ddm_value(form.ccyear) < today.getYear()
            || get_ddm_value(form.ccyear) == today.getYear()
            && get_ddm_value(form.ccmon) - 1 < today.getMonth()) {
            error.push('Card expiry is invalid: ' + today.getYear());
        }
    }

    var province = get_ddm_value(form.country) == 'Canada'
                 ? get_ddm_value(form.province)
                 : form.otherprov.value;

    var missing_map = new Array(
        'First Name',      form.first_name.value,
        'Last Name',       form.last_name.value,
        'Country',         get_ddm_value(form.country),
        'Address',         form.address.value,
        'City',            form.city.value,
        'Postal/Zip Code', form.postal_code.value,
        'Province/State',  province
    );

    for (i = 0; i < missing_map.length; i += 2) {
        if (missing_map[i + 1] == '') {
            error.push(missing_map[i] + ' is missing!');
        }
    }
    if (error.length) {
        alert("Please correct the following errors before continuing:\n\n"
            + error.join("\n"));
        return false;
    }
    return true;
}

// does a mod 10 check of the card number.
function verify_card_number(number) {
    if (number.length < 12 || number.length > 16 || number.match('\D')) {
        // bad length, or has something other than digits in it.
        return false;
    }

    var count = 0;
    var xor   = 1;

    for (var i = 0; i < number.length; i++) {
        num = parseInt(number.substr(i, 1));
        if (xor) {
            num *= 2;
            if (num >= 10) {
                count++;
                num -= 10;
            }
        }

        count += num;

        xor ^= 1;
    }

    return count % 10 == 0;
}

function showCCInfo() {
    var element   = document.forms[0].paymethod;
    var ccElement = document.getElementById('ccinfo');

    if (!ccElement) {
        return;
    }

    if (   get_ddm_text(element) == 'Cheque' 
        || get_ddm_text(element) == 'Money Order') {
        ccElement.style.display = 'none';
    }
    else {
        ccElement.style.display = 'block';
    }
}
