// sitescript.js
// Copyright © 2009-2010, Steven Houchin. All rights reserved.
window.onload = pageLoadInit

var buttonPrefixes = new Array("buthome-", "butpotp-", "butabot-", "butcont-");

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(); }
    }
    
    // initialize button rollover animation
    var butIndex = 0;
    for (var i=0; i < document.images.length; i++)
    {
        if (document.images[i].className == "hilight")
        {
            theImage = document.images[i];
            if (theImage.parentNode.tagName == "A")
            {
                theImage.onmouseover = doMoveOver;
                theImage.onmouseout = doMoveOut;
                
                theImage.overImage = new Image();
                theImage.overImage.src = buttonPrefixes[butIndex] + "over.jpg";
                theImage.overImage.className = theImage.className;
                theImage.outImage = new Image();
                theImage.outImage.src = buttonPrefixes[butIndex] + "plain.jpg";
                theImage.outImage.className = theImage.className;
                theImage.src = theImage.outImage.src;
            }
            else
            {
                theImage.src = buttonPrefixes[butIndex] + "gray.jpg";
            }
            
            butIndex++;
        }
    }
}

function doMoveOver()
{
    this.src = this.overImage.src;
}

function doMoveOut()
{
     this.src = this.outImage.src;
}

function namtomal(adress,subj)
{ 
	window.location.replace("mailto:" + adress + "@stevenhouchin.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);
}
