// Anything that has to wait until the DOM is loaded, for example
// setting content based on an element ID, adding attributes to all 
// types of a certain element (e.g. links)
function render_page()
{
    // Add the "Quick Search" iframe
    document.getElementById('quicksearch').innerHTML = '\n\74iframe style\75\42width:150px; height:300px;\42 marginwidth\75\0420\42 marginheight\75\0420\42 frameborder\75\0420\42 src\75\42http://www.sheffieldrestaurant.co.uk/search.php\42\76\74/iframe\076';

    // Dynamically add target="_blank" to all rel="external" links
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++)
    {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && /external/.test(anchor.getAttribute("rel")))
        {
            anchor.target = "_blank";
        }
    }

    // Google Maps
    if(typeof initialize == 'function') {
        initialize();
    }
}
window.onload = render_page;

// Email validator
function validate_email(email)
{
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,})$/;
    if(reg.test(email.value) == false)
    {
        return false;
    }
    return true;
}

// Check that submitted review have all fields present
function checkreview(form)
{
    if(form.name.value == "")
    {
        alert('Please enter your name.');
        form.name.focus();
        return false;
    }

    if(!validate_email(form.email))
    {
        alert('Your email address appears to be invalid.');
        form.email.focus();
        return false;
    }

    if(form.restaurant.value == "")
    {
        alert('Please enter the restaurant name.');
        form.restaurant.focus();
        return false;
    }

    if(document.getElementById('rating').value == 0)
    {
        alert('Please choose a rating by selecting the appropriate star.');
        return false;
    }

    if(form.review.value == "")
    {
        alert('Please enter your review.');
        form.review.focus();
        return false;
    }

    return true;
}
