//----------------------------------------------------------------------
// Function to validate the contents of our form before it is submitted  
//----------------------------------------------------------------------
function test()
{
    alert('test');
    return false; 
}

function ValidateContactForm( form )
{
    var errors = new Array(); 

    //------------------------------------------------------------------
    //name
    //------------------------------------------------------------------
    var name = form.name;
    if ( name.value == "" || (/^.{1,200}$/.test(name.value) == false) ) 
    {  
        // not valid, so add an error message to our array and change 
        // the background colour accordingly
        errors.push('You must specify your name');
        name.style.backgroundColor = '#FFCCCC';
    }	
    else 
    {
        // valid, so change the background colour accordingly
        name.style.backgroundColor = '#CCFFCC';
    }

    //------------------------------------------------------------------
    //hyperlink
    //------------------------------------------------------------------
    var email = document.getElementById('email');
    var phone = document.getElementById('phone');

    if ( (email.value == "") && (phone.value == "") )     
    {  
        // The hyperlink does not comply to the required format so add 
        // an error message to our array and change the background 
        // colour accordingly 
        errors.push('You must specify either a phone number or email address');
        email.style.backgroundColor = '#FFCCCC';
        phone.style.backgroundColor = '#FFCCCC';
    }
    else if ( email.value != "" && (/^[^@]+@[^@]+$/.test(email.value) == false) ) 
    {
        errors.push('You must specify a valid email address');
        email.style.backgroundColor = '#FFCCCC';
    }
    else if ( phone.value != "" && (/^.{5,50}$/.test(phone.value) == false) ) 
    {
        errors.push('You must specify a valid phone number');
        phone.style.backgroundColor = '#FFCCCC';
    }
    else 
    {
        // valid, so change the background colour accordingly
        email.style.backgroundColor = '#CCFFCC';
        phone.style.backgroundColor = '#CCFFCC';
    }
    
    //------------------------------------------------------------------
    //message
    //------------------------------------------------------------------
    var message = document.getElementById('enquiry');
    if ( message.value == "" )     
    {  
        // The hyperlink does not comply to the required format so add 
        // an error message to our array and change the background 
        // colour accordingly 
        errors.push('You must specify an enquiry');
        message.style.backgroundColor = '#FFCCCC';
     }
    else 
    {
        // valid, so change the background colour accordingly
        message.style.backgroundColor = '#CCFFCC';
    }

    //------------------------------------------------------------------
    // Only if all the checks above are ok do we return true
    //------------------------------------------------------------------
    if ( errors.length > 0 ) 
    {
        var error_message = 'The following errors were found with your submission:' + "\n";

        // Update our message if there is only one error
        if (errors.length == 1) 
        {
            error_message = 'The following error was found with your submission:' + "\n";
        }

        // Append the error message(s)
        for ( var i in errors) 
        {
            error_message = error_message + '- ' + errors[i] + "\n"; 
        }
        alert(error_message);
        return false;
    }
    else
    {
        // Let the user know that the form will now be submitted
        // ( Good practice because the data then disappears from the form!)
        //alert('Thank you for your enquiry!');
        return true;
    }
}
