// JavaScript Document

//  Function to check correct integer value
function checkInt(box)
{
    //  Setup regular expression
    var cur_re = /^\d*$/;
    
    if (!cur_re.test(box.value)) 
    {
        alert('Invalid whole number value');
        box.focus();
        box.value = "";
        return false;
    }
    return true;
}

//  Function to check correct currency input
function checkCurrency(box)
{
    //  Setup regular expression
    var cur_re = /^\d*\.?\d{1,2}$/;
    
    if (!cur_re.test(box.value)) 
    {
        alert('Invalid currecy value');
        box.focus();
        box.value = "";
        return false;
    }
    return true;
}

//  Function to check correct telephone number input
function checkTel(box)
{
    //  Setup regular expression
    var tel_re = /^[0-9\(\)\-]+$/;
    
    if (!tel_re.test(box.value)) 
    {
        alert('Invalid telephone number value');
        box.focus();
        box.value = "";
        return false;
    }
    return true;
}

//  Function to check for a valid email
function checkEmail(box)
{
    //  Setup regular expression
    var email_re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/;
    
    if (!email_re.test(box.value)) 
    {
        alert('Invalid email value');
        box.focus();
        box.value = "";
        return false;
    }
    return true;
}


//  Function to loop trough all the inputs in a form and print out the fields that need to be filled in
function checkForm(form)
{
    //  Setup error message
    var message = "The following fields are required:\n\n";
    
    //  Setup test to print error
    var error = false;

    //  Loop trough all the inputs in the form
    for (var i = 0; i < form.elements.length; i++)
    {
        //  Check for alternate value if not null check input for value
        if ((form.elements[i].alt != null) && (form.elements[i].alt != ""))
        {
            if ((form.elements[i].value == "") || (form.elements[i].value == null))
            {
                message = message + "- " + form.elements[i].alt + "\n";
                error = true;
            }
        }

        if ((form.elements[i].type == "textarea") && form.elements[i].value == "" && form.elements[i].name != "del_instr") {
            message = message + "- " + form.elements[i].id + "\n";
            error = true;
        }
    }

//    if (document.getElementById("Postage Method").selectedIndex == 0) {
//        message = message + "- Postage Method\n";
//        error = true;
//    }
    
    //  Check if message sould be printed
    if (error)
    {
        alert(message);
        return false;
    }
    else return true;   
}
