//alert('global.js');

//=========================
function AddEvent(oElement, sEventName, fnFunction)
//=========================
// for example:  AddEvent(window, 'load', jsExtLinks);
{
//alert('function AddEvent(oElement, sEventName, fnFunction)');

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


//=========================
function getElementsByClassName(sRequestedClass)		// sRequestedElement = optional second argument
//=========================
{
	//alert('function getElementsByClassName(' + sRequestedClass + ')');
	
	aResult = new Array();
	
		if (document.getElementsByTagName)
		{
				if (arguments.length > 1)
				{
					sRequestedElement = arguments[1];
				}
				else
				{
					sRequestedElement = '*';
				}

	//alert('sRequestedElement = ' + sRequestedElement);
			var aElements = document.getElementsByTagName(sRequestedElement);
	//alert('Found ' + aElements.length + ' ' + sRequestedElement);

					var rRegexp = new RegExp('\\b' + sRequestedClass + '\\b'); 
//alert('rRegexp.source = ' + rRegexp.source);
/*					
	if (rRegexp.test('tabctl'))
	{
		alert('found');
	}
	else
	{
		alert('not found');
	}
*/	
			
				if (aElements && aElements.length > 0)
				{			
					for (var iEl = 0; iEl < aElements.length; iEl++)
					{
						var sClass = aElements[iEl].className;
						//alert('sClass = ' + sClass);
						
							if (sClass)
							{
								if (rRegexp.test(sClass))
								{
//alert('found');
									aResult[aResult.length] = aElements[iEl];
								}
								else
								{
									//alert('not found');
								}
							}//if
					}//for
				}//if
		}//if
		
	//alert('Found ' + aResult.length + ' ' + sRequestedElement + '.' + sRequestedClass);
	return aResult;
}


/*		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()");

	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("//mirviss.com") >= 0
					 || sHref.indexOf("//www.mirviss.com") >= 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;
}






/*
		validate-input.js		
		by Paul Novitski www.juniperwebcraft.com
		last revised November 2007
		
		This script validates an input form based on input field class names:
			vReq			validateRequired()		numeric
			vEmail			validateEmail()			email address
			vPostalCode		validatePostalCode()	Canadian postal code
			vPhone			validatePhone()			phone number (digits + -().)
			vNums			validateNums()			numeric digits only

*/

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

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

	// look for the form
	var aForms = document.getElementsByTagName('form');
		if (!aForms) return;

	for (var iForm=0; iForm < aForms.length; iForm++)
	{
		aForms[iForm].onsubmit = ValidateForm;
	}
}


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

//debugAlert('validating form');

	// 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];
//debugAlert('oControl.style.display = ' + oControl.style.display);
			if (oControl.style.display == 'none') continue;
		
		var sClass = oControl.className;
		
			if (sClass && sClass > '')
			{
				
				var aMatches = sClass.match(rClasses);
				
				for (var iMatch = 0; iMatch < aMatches.length; iMatch++)
				{
					//debugAlert(sClass + '\n' + aMatches[iMatch]);
					switch (aMatches[iMatch])
					{
						case 'vReq':		[bStatus, sMsg] = validateRequired(oControl);		break;
						case 'vEmail':		[bStatus, sMsg] = validateEmail(oControl);			break;
						case 'vPostalCode':	[bStatus, sMsg] = validatePostalCode(oControl);		break;
						case 'vPhone':		[bStatus, sMsg] = validatePhone(oControl);			break;
						case 'vNums':		[bStatus, sMsg] = validateNums(oControl);			break;
					}

//debugAlert('bStatus = ' + bStatus);

						if (!bStatus)
						{
//debugAlert('Displaying error because bStatus = ' + bStatus);
							displayErrorMessage(oControl, sMsg);
							oControl.focus();
							return bStatus;
							break;
						}
				}//for
					if (!bStatus) break;
			}//if

			if (!bStatus) break;
	}

	return bStatus;
}


//=========================
function validateRequired(oControl)
//=========================
{
	//debugAlert('validateRequired(): ' + oControl.tagName + '.value = ' + oControl.value);

	switch (oControl.tagName)
	{
		case 'INPUT':
			bStatus = (oControl.value > '');
			break;

		case 'SELECT':
//debugAlert('SELECT #' + oControl.id + ' selectedIndex = ' + oControl.selectedIndex);
			bStatus = (oControl.selectedIndex >= 0);
//debugAlert('bStatus = ' + bStatus);
			break;
	}
	return [bStatus, "'@' is required"];
}



//=========================
function 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 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 validatePhone(oControl)
//=========================
{
	var rPhone = /^[- 0-9()]+$/;

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

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


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

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

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


//=========================
function displayErrorMessage(oControl, sMsgTemplate)
//=========================
{
//debugAlert('function displayErrorMessage(' + oControl + ', ' + sMsgTemplate + ')');

	// find label for this input control
	var oLabel = null;

		if (oControl.id)
		{
			var aLabels = document.getElementsByTagName('label');
			
			for (var iLabel = 0; iLabel < aLabels.length; iLabel++)
			{
				sFor = aLabels[iLabel].getAttribute('for');
					if (sFor && sFor == oControl.id)
					{
						oLabel = aLabels[iLabel];
						break;
					}
			}
		}


		if (oLabel)
		{
//debugAlert('Using the label ' + oLabel.textContent);
			var sId = oLabel.textContent;
			sId = sId.replace('* ', '');
			sId = sId.replace(':', '');
		}
		else if (oControl.id)
		{
//debugAlert('Using the control id ' + oControl.id);
			var sId = oControl.id.replace(/[_-]/, ' ');
		}
		else if (oControl.name)
		{
//debugAlert('Using the control name ' + oControl.name);
			var sId = oControl.name.replace(/[_-]/, ' ');
		}
		else
		{
//debugAlert('Using the control tagName ' + oControl.tagName);
			var sId = oControl.tagName;
		}
		
	var sMsg = sMsgTemplate.replace('@', sId);
	alert(sMsg);
}
