$(document).ready(function() {

    setWidth();
	
    var currentLocation = String(document.location);
    //alert(currentLocation);
    var showPage = /addaddress|addaddresses/g;
    match = showPage.test(currentLocation);
    //alert(match);
    //alert(currentLocation.indexOf(noShowPages));
    if (match == true) {
        $('#overseas').hide();
        var addtype = $('#type').val();
        if (addtype > 0) {
            editForm(addtype);
        }
        var country = $('#billing_country').val();
        if (country > 0) {
            editRegion(country);
        }
    }
});

function fillDel(){
    //document.getElementById.delivery_first_name.value = document.getElementById.first_name.value; // first name
    //document.getElementById.delivery_last_name.value = document.getElementById.last_name.value; // last name
    document.getElementById('delivery_street').value = document.getElementById('billing_street').value; // street_address
    document.getElementById('delivery_suburb').value = document.getElementById('billing_suburb').value; // suburb
    document.getElementById('delivery_city').value = document.getElementById('billing_city').value; // city
    document.getElementById('delivery_region').value = document.getElementById('billing_region').value; // region
    document.getElementById('delivery_post_code').value = document.getElementById('billing_post_code').value; // post code
    document.getElementById('delivery_country').selectedIndex = document.getElementById('billing_country').selectedIndex; // country
    document.getElementById('delivery_nz_region').selectedIndex = document.getElementById('billing_nz_region').selectedIndex; // country
    document.getElementById('delivery_phone_number').value = document.getElementById('billing_phone_number').value; // phone
}

function editForm(selection) {

    if (selection == 1) {
        // billing address - no need for name fields
        $('#fname').hide();
        $('#lname').hide();
    }
    else if (selection == 2) {
        $('#fname').show();
        $('#lname').show();
    }
}


function editRegion(countryid) {
    
    if (countryid == 219) {
        // chosen NZ so show the nz regions ddl
        $('#nzregion').show();
        $('#delnzregion').show();
        $('#overseas').hide();
        $('#deloverseas').hide();
        
    }
    else {
        // country is outside NZ so DONT show the nz regions ddl
        $('#nzregion').hide();
        $('#delnzregion').hide();
        $('#overseas').show();
        $('#deloverseas').show();
    }
}

function startSearch(loc) {

    var prodCode = $('#pcode').val();       // header form
    var prodCode2 = $('#pcode2').val();       // page form
    
    if(loc == 'p' && prodCode2.length < 1) {
        showError("Please enter a product code to start your search.");
        return false;
    }
    else if(loc == 'h' && prodCode.length < 1) {
        alert("Please enter a product code to start your search.");
        return false;
    }
    else {
        return true;
    }
}

function showError(errMsg) {

    var xhtml = '<p class="b">Oops, there\'s been a problem.</p>';
    xhtml += '<ul><li>'+errMsg+'</li></ul>';

    $('#error').html(xhtml);
    $('#error').show();

}

function setWidth() {

    var sw = $(window).width();
    if (sw < 1000) {
        $("#body").css('width', sw-2);
        $("#body").css('font-size', 11+'px');
        $("#tnav").css('margin-left', 30+'px');
    }
    else if (sw > 1000 && sw < 1200) {
        $("#body").css('width', sw-40);
    }
    else {
        $("#body").css('width', 1200);
    }

}