// JavaScript Document
sfHover = function() {
	var sfEls = document.getElementById("mainNav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}

if (window.attachEvent) window.attachEvent("onload", sfHover);



// AJAX functions

// displays the email form to send a page link
function showEmailForm() {

	// editable string containing HTML for email form
    var html = "<form name=\"PageLinkForm\"><ul><li>Your Name</li>";
    html += "<li><input class=\"fields\" name=\"name\"/></li>";
    html += "<li>Recipient Email Address</li><li><input class=\"fields\" name=\"email\"/></li>";
    html += "<li><input class=\"submit\" type=\"button\" value=\"Send Email\" onclick=\"sendPageLink()\"></li>";
    html += "</ul></form>";

    document.getElementById("right_menu_email").innerHTML = html;
}


// checks form inputs and calls php script to send page link email
function sendPageLink() {

    var xmlHTTP, html, link, name, email, url;

    // trap any potential errors from accessing values of dynamic elements
    try {
        link = location.href;
        name = document.PageLinkForm.name.value;
        email = document.PageLinkForm.email.value;
    }
    catch (e) {
        return;
    }

    // check name and email fields for valid input
    if (name.length > 0 && name != "" && name != " " && isValidEmail(email)) {

        // create HTTP request object
  	    xmlHttp = GetXmlHttpObject();

        // handle failure to create HTTP request object
        if (xmlHttp == null) {
            html = "<p>An error occurred and the page link could not be sent.";
            html += "<br />We apologize for the inconvenience.</p>";
            document.getElementById("right_menu_email").innerHTML = html;
  	    }
        else {
  	        xmlHttp.onreadystatechange = function () {
      	        if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
      		        document.getElementById("right_menu_email").innerHTML = xmlHttp.responseText;
            };
            url = "scripts/sendPageLink.php?n=" + name + "&a=" + email + "&l=" + link;
  	        xmlHttp.open("GET", url, true);
  	        xmlHttp.send(null);
        }
    }
    else {
        alert("Please enter your name and a valid email address.");
    }
}


// creates HTTP request object
function GetXmlHttpObject() {

	// cascading try-catch blocks to create object for different browsers
	try {
		// Firefox, Opera 8.0+, Safari
	 	xmlHttp = new XMLHttpRequest();
	}
	catch (e) {

		// Internet Explorer
	 	try {
	  		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); // IE 6.0+
	  	}
	 	catch (e) {
	  		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE 5.0+
	  	}
	}
	return xmlHttp;
}


// returns true or false whether a string is a valid formatted email address
function isValidEmail (str) {
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    return str.match(re);
}

