/*
		addlisting.js
		for Kootenay Wedding Services
*/

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

var sFormId = 'addListing';

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

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

	oForm.onsubmit = ValidateForm;
}


//=========================
function 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] = 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;
					}
					
						if (!bStatus)
						{
							displayErrorMessage(oControl, sMsg);
							oControl.focus();
							break;
						}
				}//for
					if (!bStatus) break;
			}//if

			if (!bStatus) break;
	}

	return bStatus;
}


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

	switch (oControl.tagName)
	{
		case 'INPUT':
			bStatus = (oControl.value > '');
			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)
//=========================
{
	// find label for this input control
	var oLabel = oControl.previousSibling;
	
	while (oLabel)
	{
			if (oLabel.tagName && oLabel.tagName == 'LABEL') break;
		oLabel = oLabel.previousSibling;
	}

		if (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);
}



