// sitescript.js
// Copyright © 2009-2010, Forest Park Lab. All rights reserved.
window.onload = pageLoadInit;

var a = Math.ceil(Math.random() * 10);
var b = Math.ceil(Math.random() * 10);       
var c = a + b;

function pageLoadInit()
{
    if (document.getElementById("contact_form"))
    {
        // add the 'action' and 'onsubmit' attributes to the form element
        var elementForm = document.getElementById("contact_form");
        elementForm.setAttribute("action", "/cgi-bin/cgiemail/contactform.txt");
        elementForm.onsubmit = function() { return validSubFrm(); }
    }
}

function namtomal(adress,subj)
{ 
	window.location.replace("mailto:" + adress + "@forestparklab.com?" + subj); 
}

function drawBotEntry()
{
    document.write("<P align='right'>What is "+ a + " + " + b +"?</p>");
    document.write("</td>");
    document.write("<td><input id='entry_valid_input' type='text' maxlength='2' size='2'/>");
}


// Validate the submitted form
//
function validSubFrm()
{
    var f = document.getElementById("query").value;
    if (invalidComments(f))
    {
        alert("Invalid html tag text in comments.");
        return false;
    }
    var d = document.getElementById("entry_valid_input").value;
    if (d != c)
    {
        alert("Incorrect math problem answer.");
        return false;
    }
    var e = document.getElementById("fromaddress").value;
    if (!validEmail(e))
    {
        alert("Invalid email address format.");
        return false;
    }
    return true;
}

// Validate the email address syntax
function validEmail(email)
{
	var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	return re.test(email);
}

// Make sure no HTML link tags are in the message
function invalidComments(text)
{
	var re = /^\s*.*\s*<a\s*.*\s*href="[^"]+"\s*.*\s*>[^<]*<\/a>/i;
	return re.test(text);
}