/*
		global.js
		for Shakti Gawain website
		created June 2006 by Paul Novitski, juniperwebcraft.com
		last revised:
*/


//-------------------------
// set initializer to execute on page load
//-------------------------
//alert("hi");
var loadGlobal = window.onload;

window.onload = function()
{
		if (loadGlobal) loadGlobal();
	jsGlobalInit();
}

//=========================
function jsGlobalInit()
//=========================
{
		if (!document.getElementById) return;
		if (!document.getElementsByTagName) return;

	jsContactInit();
	jsJoinListInit();
}


//=========================
function jsContactInit()
//=========================
{
		if (document.body.id != "contact") return;

	var aForms = document.getElementsByTagName("form");
		if (aForms) aForms[0].onsubmit = jsContactValidate;
	
}


//=========================
function jsContactValidate(evt)
//=========================
{
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;

	var bValid = jsValidate("email", "contactEmail");
	
		if (!bValid) return false;

	var oCtl = document.getElementById("contactEmail");
	var oCtl2 = document.getElementById("contactEmailConfirm");
	
		if (!oCtl2.value)
		{
			alert("Please confirm your email address by typing it again.");
			oCtl2.focus();
			return false;
		}
			
		if (oCtl.value != oCtl2.value)
		{
			alert("Your confirmation email address does not match your original.\n\n:" + oCtl.value + "\n:" + oCtl2.value + "\n\nPlease confirm your email address by typing it again.");
			oCtl2.focus();
			return false;
		}
		
	var oCtl = document.getElementById("contactMessage");
		if (!oCtl.value)
		{
			alert("Please type your message to us in the 'Comments/Questions' field.");
			oCtl.focus();
			return false;
		}
		
	var oCtl = document.getElementById("contactSubject");
		if (!oCtl.value)
		{
			alert("Please enter the subject of your message.");
			oCtl.focus();
			return false;
		}		
	
	return true;
}








//=========================
function jsJoinListInit()
//=========================
{
		if (document.body.id != "joinlist") return;
	
	var aForms = document.getElementsByTagName("form");
		if (aForms) aForms[0].onsubmit = jsJoinListValidate;
	
//alert("jsJoinListInit() finished");
}


//=========================
function jsJoinListValidate(evt)
//=========================
{
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;

	var bValid = jsValidate("email", "joinlistEmail");
	
	return bValid;
}


//=========================
function jsValidate(argControlType, argControlName)
//=========================
{
	var oCtl = document.getElementById(argControlName);
		if (!oCtl) return;
	
	var bStatus = true;
	
	switch (argControlType)
	{
		case "email":	bStatus = jsValidateEmail(oCtl);	break;
	}
	
	return bStatus;
}


//=========================
function jsValidateEmail(argControl)
//=========================
{
	var regEmail = /^[a-z0-9-_.]+@[a-z0-9-_]+(\.[a-z]+)+/i;
	
	var bStatus = regEmail.test(argControl.value);
	
		if (!bStatus)
		{
			if (!argControl.value)
			{
				alert("Please enter your email address.");
			}
			else
			{
				alert('"' + argControl.value + '" is not a valid email address.\n\nPlease try again.');
			}
			argControl.focus();
		}
	
	return bStatus;
}


