// General JavaScript
// toggle visibility 
function toggle( targetId ){
list = new Array("school", "recreation", "volunteers", "medical", "general");

  if (document.getElementById){
		target = document.getElementById( targetId );
		if (target.style.display == "none")
		{
		for(i in list)
		{
			if (list[i] == targetId)
			{
				target = document.getElementById( list[i] );
				target.style.display = "";
			} else {
				target = document.getElementById( list[i] );
				target.style.display = "none";
			}
		}
		} else {
			target.style.display = "none";
		}
  	}
}

function openWin(URL,w,h) {
var winame='window';

myWin = window.open(URL,winame,'scrollbars,width='+w+',height='+h);
}
//End General JavaScript



//Contact Script
var url = "contact.aspx?"; // The server-side script
textfields = new Array("tbName", "tbEmail", "tbSubject", "tbMessage");
reqfields = new Array("Name", "Message");
reqfieldsText = new Array("Your Name", "Message");
function validateEntry()
{
	valid = new Boolean(true);
	first = new Boolean(true);

	document.getElementById("msg").innerHTML = "Important Message<ul>";
	for (var s in reqfields)
	{
		var tid = "tb" + reqfields[s];
		var lid = "l" + reqfields[s];

		var checkVal = document.getElementById(tid).value;
		if ((checkVal==null) || (checkVal==""))
		{
			if (first)
			{
				document.getElementById(tid).focus();
				first = false;
			}
			document.getElementById("msg").style.display = "";
			document.getElementById("msg").innerHTML += "<li>Please Enter Your " + reqfieldsText[s] + ".</li>";
			document.getElementById(lid).style.color = "#aa0000";
			document.getElementById(tid).style.border = "solid 2px #aa0000";
			valid = false;
		} else 
		{ 
			document.getElementById(tid).style.border = "solid 1px #333333";
			document.getElementById(lid).style.color = "#333333"; 
		}
	}

	//Validate Email Address
	str = document.getElementById("tbEmail").value;
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1){
		showEmailError();
		valid = false;
	} else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		showEmailError();
		valid = false;
	} else if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		showEmailError();
		valid = false;
	} else if (str.indexOf(at,(lat+1))!=-1){
		showEmailError();
		valid = false;
	} else if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		showEmailError();
		valid = false;
	} else if (str.indexOf(dot,(lat+2))==-1){
		showEmailError();
		valid = false;
	} else if (str.indexOf(" ")!=-1){
		showEmailError();
		valid = false;
	}
	document.getElementById("msg").innerHTML += "</ul>";

	if (valid)
		insertEntry();
}

function handleHttpResponse() {
  if (http.readyState == 4) {
    // Split the comma delimited response into an array
	results = http.responseText.split(",");
	if (results[0] == "success")
	{
		document.getElementById("frm").style.display = "none";
		document.getElementById("msg").innerHTML = "Success!<p>Thank you for sending us a message.  You should receive a copy of the email we received.  We will be in touch soon.</p><p>Thanks and have a great day.</p>";
		document.getElementById("msg").style.display = "";
		document.getElementById("msg").style.border = "solid 1px #036";
		document.getElementById("msg").style.color = "#036";
		document.getElementById("msg").style.background = "#FFFFD5 url(/_i/ico-msg.gif) no-repeat top left";
	} else if (results[0] == "error") {
		document.getElementById("msg").innerHTML = "Important Message<p>An error occurred at no fault of your own.  Please try again.</p><p>We apologize for the error.</p>";
		document.getElementById("msg").style.display = "";
	} else {
		document.getElementById("msg").innerHTML = "Important Message<ul><li>Please enter " + results[0] + "</li></ul>";
		document.getElementById("msg").style.display = "";
	}
  }
}

function insertEntry()
{
	nameVal = document.getElementById(textfields[0]).value;
	emailVal = document.getElementById(textfields[1]).value;
	subjectVal = document.getElementById(textfields[2]).value;
	messageVal = document.getElementById(textfields[3]).value;

	http.open("GET", url + "name=" + escape(nameVal) + "&email=" + escape(emailVal) + "&subject=" + escape(subjectVal) + "&message=" + escape(messageVal), true); 
	http.onreadystatechange = handleHttpResponse; 
	http.send(null);
}

function showEmailError()
{
		document.getElementById("msg").style.display = "";
		document.getElementById("msg").innerHTML += "<li>Please Enter Valid Email (Ex: sample@sample.com)</li>";
		document.getElementById("lEmail").style.color = "#aa0000";
		document.getElementById("tbEmail").style.border = "solid 2px #aa0000";
}

function getHTTPObject() { 
	var xmlhttp; 
	/*@cc_on 
	@if (@_jscript_version >= 5) 
	try { 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} catch (e) { 
		try { 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
		} catch (E) { 
			xmlhttp = false; 
		} 
	} 
	@else 
	xmlhttp = false; 
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
		try { 
			xmlhttp = new XMLHttpRequest(); 
		} catch (e) { 
			xmlhttp = false; 
		} 
	} 
	return xmlhttp; 
} 
var http = getHTTPObject(); // We create the HTTP Object 

