//=========================
// execute on page load
//=========================
AddEvent(window, "load", jsExtLinks);
AddEvent(window, "load", phpcaptcha_makeLink);


//=========================
function AddEvent(oElement, sEventName, fnFunction)
//=========================
{
	if (oElement)
	{
		if (oElement.attachEvent)
		{
			oElement.attachEvent("on" + sEventName, fnFunction);
		}
		else
		{
			oElement.addEventListener(sEventName, fnFunction, true);
		}
	}
}


/*
		Form Validation
		by Paul Novitski
		www.juniperwebcraft.com
		last revised July 2008
*/

//=========================
// execute on page load
//=========================
//AddEvent(window, "load", FormValid_Init);

//var sFormValid_FormId = 'blah-blah';

// set behaviors
//=========================
function FormValid_Init()
//=========================
{
		if (!document.getElementById) return;
		if (!document.getElementsByTagName) return;

	// look for the form
	var oForm = document.getElementById(sFormValid_FormId);
		if (!oForm) return;

	oForm.onsubmit = FormValid_ValidateForm;
}


//=========================
function FormValid_ValidateForm(evt)
//=========================
{
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;

	// default status is OK
	bStatus = true;
	
	// regular expression to collect class names
	var rClasses = /\b\w+\b/g;

	// collection of controls in this form
	var aControls = this.elements;
	
	for (var iCtl = 0; iCtl < aControls.length; iCtl++)
	{
		var oControl = aControls[iCtl];
		var sClass = oControl.className;
		
			if (sClass && sClass > '')
			{
				
				var aMatches = sClass.match(rClasses);
				
				for (var iMatch = 0; iMatch < aMatches.length; iMatch++)
				{
					//alert(sClass + '\n' + aMatches[iMatch]);
					switch (aMatches[iMatch])
					{
						case 'vReq':		[bStatus, sMsg] = FormValid_ValidateRequired(oControl);		break;
						case 'vEmail':		[bStatus, sMsg] = FormValid_ValidateEmail(oControl);			break;
						case 'vPostalCode':	[bStatus, sMsg] = FormValid_ValidatePostalCode(oControl);		break;
						case 'vPhone':		[bStatus, sMsg] = FormValid_ValidatePhone(oControl);			break;
						case 'vNums':		[bStatus, sMsg] = FormValid_ValidateNums(oControl);			break;
					}
					
						if (!bStatus)
						{
							FormValid_DisplayErrorMessage(oControl, sMsg);
							oControl.focus();
							break;
						}
				}//for
					if (!bStatus) break;
			}//if

			if (!bStatus) break;
	}

	return bStatus;
}


//=========================
function FormValid_ValidateRequired(oControl)
//=========================
{
	//alert(oControl.tagName + '.value = ' + oControl.value);

	switch (oControl.tagName)
	{
		case 'INPUT':
		case 'TEXTAREA':
			bStatus = (oControl.value > '');
			break;
	}
	return [bStatus, '@ is required'];
}



//=========================
function FormValid_ValidateEmail(oControl)
//=========================
{
	var rEmail = /^[a-z0-9._-]+@[a-z0-9._-]+(\.[a-z]+)+$/i;

		// blank?
		if (oControl.value == '') return [true, ''];

	return [rEmail.test(oControl.value), 'Invalid @'];
}


//=========================
function FormValid_ValidatePostalCode(oControl)
//=========================
{
	var rPCCA = /^[a-z][0-9][a-z] *[0-9][a-z][0-9]$/i;

		// blank?
		if (oControl.value == '') return [true, ''];

	return [rPCCA.test(oControl.value), 'Invalid @'];
}


//=========================
function FormValid_ValidatePhone(oControl)
//=========================
{
	var rPhone = /^[+-. 0-9()]+$/;

		// blank?
		if (oControl.value == '') return [true, ''];

	return [rPhone.test(oControl.value), 'Invalid @'];
}


//=========================
function FormValid_ValidateNums(oControl)
//=========================
{
	var rNums = /^[- 0-9]+$/;

		// blank?
		if (oControl.value == '') return [true, ''];

	return [rNums.test(oControl.value), 'Invalid @'];
}


//=========================
function FormValid_DisplayErrorMessage(oControl, sMsgTemplate)
//=========================
{
	// default value = control id
	var sId = oControl.id.replace(/_/, ' ');
	
	var oForm = document.getElementById(sFormValid_FormId);

	var aLabels = oForm.getElementsByTagName('LABEL');
	
	for (var iLabel=0; iLabel < aLabels.length; iLabel++)
	{
		var sFor = aLabels[iLabel].getAttribute('for');
		
			if (sFor && sFor == oControl.id)
			{
				sId = aLabels[iLabel].textContent;
				var iEnd = sId.indexOf(':');
					if (iEnd > 0) sId = sId.substr(0,iEnd);
				sId = sId.replace('* ', '');
				sId = sId.replace(' (required)', '');
				//sId = sId.replace(':', '');
				break;
			}
	}

/*
	// find label for this input control
	var oLabel = oControl.previousSibling;
	
	while (oLabel)
	{
			if (oLabel.tagName && oLabel.tagName == 'LABEL') break;
		oLabel = oLabel.previousSibling;
	}

		if (oLabel && oLabel.tagName && oLabel.tagName == 'LABEL')
		{
			var sId = oLabel.textContent;
			sId = sId.replace('* ', '');
			sId = sId.replace(':', '');
		}
		else
		{
			var sId = oControl.id.replace(/_/, ' ');
		}
*/
		
	var sMsg = sMsgTemplate.replace('@', sId);
	alert(sMsg);
}



/*		externallinks.js
		by Paul Novitski - www.juniperwebcraft.com
		February 2007

This script converts all absolute URL hyperlinks to open in a new window.
19 April 2007 - also any link with the class "external"
*/

//=========================
function jsExtLinks()
//=========================
{
	//alert("function jsExtLinks()");

		// check for DOM-awareness
		if (!document.getElementById) return;
		if (!document.getElementsByTagName) return;

	var sPrompt = "(Opens in a new window)";

	var aLinks = document.getElementsByTagName("A");
	
	//alert("aLinks.length = " + aLinks.length);
	
	for (var iLink = 0; iLink < aLinks.length; iLink++)
	{
		var bExternal = false;
		var bPopup = false;
		
		var sClass = aLinks[iLink].className;		
		//var sClass = aLinks[iLink].getAttribute("class");
			if (sClass && sClass == 'popup')
			{
				bPopup = true;
			}
			else if (sClass && sClass == 'external')
			{
				bExternal = true;
			}
			else
			{		
				var sHref = aLinks[iLink].getAttribute("href");
				//alert(iLink + ": " + sHref);
				
					if (!sHref) continue;
					if (sHref == "") continue;

					if (sHref.substring(0, 4) == "http") bExternal = true;
					
					// don't open new window for any page in this website family
					if (sHref.indexOf("//vran.org") >= 0
					 || sHref.indexOf("//www.vran.org") >= 0
					)
					{
						bExternal = false;
					}
			}
		
			if (!bExternal && !bPopup) continue;
		
			if (bExternal)
			{
				var sClickFunction = jsExtLinksClick;
			}
			else if (bPopup)
			{
				var sClickFunction = jsPopupLinksClick;
			}
			else
			{
				continue;
			}
		
		aLinks[iLink].onclick = sClickFunction;

		var sTitle = aLinks[iLink].getAttribute("title");
		
			if (!sTitle) sTitle = '';
			if (sTitle.indexOf("new window") >= 0) continue;

			if (sTitle == "")
			{
				sTitle = sPrompt;
			}
			else
			{
				sTitle = sTitle + " " + sPrompt;
			}
		
		aLinks[iLink].setAttribute("title", sTitle);
		//alert("title: " + iLink + ": " + sTitle);
	}
}


//=========================
function jsExtLinksClick(evt)
//=========================
{
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;

	var sHref = this.getAttribute("href");

	window.open (sHref, "_blank");	

	return false;
}



//=========================
function jsPopupLinksClick(evt)
//=========================
{
	// cancel event-bubbling
		if (evt) { event = evt; }
	event.cancelBubble = true;
	
	// see if it's an image
	var rCheckImage = /\.(jpg|jpeg|gif|png)$/i;
	var rGetDims = /-(\d+)x(\d+)\.(jpg|jpeg|gif|png)$/i;
	
	var sPopupHref = 'popupImage.php?img=';
	
	var sHref = this.getAttribute("href");

	var bIsImage = rCheckImage.test(sHref);
	
	//alert((bIsImage) ? 'An image' : 'Not an image');

		if (bIsImage)
		{
			var aMatches = rGetDims.exec(sHref);
			//alert(aMatches.join('\n'));
			sHref = sPopupHref + encodeURIComponent(sHref);
			var sWindowName = "Image Pop-up";
			var sFeatures = "menubar=no,toolbar=no,directories=no,personalbar=no,scrollbars=no";
				if (aMatches) sFeatures += ",width=" + aMatches[1] + ",height=" + aMatches[2];
		}
		else
		{
			var sWindowName = "New Window";
			var sFeatures = '';
		}

	window.open(sHref, sWindowName, sFeatures);

	return false;
}


//=========================
// Free Mailto Spam Protection by www.phpcaptcha.org
//=========================
	var j7ea5 = '%72&#97;n&#46;o%72';
	var ze9663 = '%6da&#105;&#108;%74o%3a';
	var mee6eee = '&#105;%6e%66&#111;%40v';
	var t8e96 = '&#103;';
	
    var v7e0aa52d0 = '%6da%69%6ct&#111;%3a';
    var h0aa52d0 = '&#104;aw%2eca';
    var g8358dd = '&#116;&#106;&#97;&#109;&#101;&#115;&#52;@s';

//=========================
function phpcaptcha_makeLink()
//=========================
{
	var a = document.getElementById('mt');
		if (!a) a = document.getElementById('mt1');
		if (a)
		{
			a.href = phpcaptcha_decode(ze9663 + mee6eee + j7ea5 + t8e96);
			a.innerHTML = phpcaptcha_decode(mee6eee + j7ea5 + t8e96);
		}

	a = document.getElementById('mt2');
		if (a)
		{
			a.href = phpcaptcha_decode(v7e0aa52d0 + g8358dd + h0aa52d0);
			a.innerHTML = phpcaptcha_decode(g8358dd + h0aa52d0);
		}
}

//=========================
function phpcaptcha_decode(s)
//=========================
{
	var l = s.length, n = '', d = '';

	for (var i = 0; i < l; ++i)
	{
		if (s.charAt(i) == '') { continue; }

		if (s.charAt(i) == '%')
		{
			n = s.charAt(++i) + '' + s.charAt(++i);
			d += String.fromCharCode(parseInt(n, 16));
			continue;
		}
		else if (s.charAt(i) == '&')
		{
			i+=2;
			n = '';
			while (s.charAt(i) != ';' && i < l)
			{
				n += '' + s.charAt(i); i++;
			}
		d += String.fromCharCode(parseInt(n));
		continue;
		}
		else
		{
			d += s.charAt(i);
		}
	}

	return d;
}

