// contact  ../constituency/contact.cfm

function popupAttachments(iLink,iWindowName) {
	if (! window.focus)return true;
	var href = iLink;
	window.open(href, iWindowName, 'width=800,height=600,scrollbars=yes,resizable=yes,menubar=yes');	
	return false;
}

// Performs prep work, then submits a form.
function constituencySubmit(theForm, formAction, urlVars) {
	theForm.formaction.value = formAction;
	
	// Special tasks
	switch (formAction) {
		case 'Search':
			theForm.action = "contact_search.cfm";
		break;
		case 'NewSearch':
			theForm.action = "contact_search.cfm";
		break;		

		case 'newTopic':
			theForm.action = "contact_new.cfm" + urlVars;
		break;
			
		case 'preUpdateNote':
			theForm.action = "contact_update.cfm" + urlVars;
		break;
	}
	
	theForm.submit();
}

// Erases all form elements passed in (by ID).
function clearFormFields() {
	for (var i = 0, a = arguments.length, el; i < a; i++) {
		el = document.getElementById(arguments[i]);
		if (el.type != "radio" && el.type != "checkbox")
			el.value = "";
		else
			el.checked = false;
	}
	document.getElementById(arguments[0]).focus();
}

// Resets all form elements passed in (by ID) to original values.
function revertFormFields(theForm) {
	var elementArray = theForm.elements;
	var firstElementID = "";
	
	for (i = 0; i < elementArray.length; i++) {
		var saveElementID = elementArray[i].id;
		
		// Is it a save field?
		if (saveElementID.indexOf('SaveID') >= 0) {
			// Remove 'Save' from the ID to derive ID of main element.
			var mainElementID = saveElementID.replace("SaveID", "ID");
			document.getElementById(mainElementID).value = document.getElementById(saveElementID).value;
			
			// Use a flag to set focus on first element.
			if (firstElementID == "")
				firstElementID = mainElementID;
		}
	}
	
	document.getElementById(firstElementID).focus();
}

// Sets a series of fields to the specified values.
// Pass in pipe delimited lists (lists are delimited from each other with '~' (tilde).
// 1st list: IDs of fields to be set. 2nd List: string values to set those fields to.
function populateConvFields(popString)
{
	var i;
	
	// Grab the form element IDs.
	var idArray = popString.split('~')[0].split('|');
	// Grab the strings.
	var valArray = popString.split('~')[1].split('|');
	
	if (idArray.length == valArray.length)
		for (i = 0; i < idArray.length; i++)
			document.getElementById(idArray[i]).value = valArray[i];
	else
		alert(valArray + "\n" + idArray);
}

// Validates that there's data in the passed-in fields.
// Pass any number of IDs of the fields to check into the function.
function fieldValidate()
{
	var i;
	var errorText = "";

	for (i = 0; i < arguments.length; i = i + 2)
		if (document.getElementById(arguments[i]).value.length < 1)
			errorText = errorText + "Missing Field: " + arguments[i+1] + "\n";
	
	if (errorText.length > 0)
	{
		alert(errorText);
		return false;
	}
	else
		return true;
}

function send_email_handler(e) {
	var responder = $('responderID');
	if (!/^[\w]+$/.test(responder.value)) {
		this.checked = false;
		alert('Please enter an email address in the Responder field and then check the Send Email box again.');
		responder.select();
	}
}

function toggleOptions(theElementID, displayMode)
	{
		var kShow = "";
		var kHide = "none";
		
		document.getElementById(theElementID).style.display = displayMode;
	//	alert(theElementID, displayMode);
		if (displayMode == kShow)
		{
				document.getElementById(theElementID).style.display = kShow;
	//			document.form1.show_(theElementID).value = true;
		}
		else
		{
			document.getElementById(theElementID).style.display = kHide;
	//		document.form1.show_(theElementID).value = false;
		}
	}

// Form element events, array of objects
/*  ================== Object Properties =======================
 *
 *  -   id: target element's id (string)
 *
 *  - type: event name (string);  no "on" prefix,
 *          i.e. 'focus','blur','keydown','keyup', etc.
 *
 *  -   fn: function (anonymous [function () {}] to pass arguments,
 *          or variable reference [no trailing parentheses])
 *
 *  ============================================================
 */
var form_event = [
    { id: 'sendEmail', type: 'click', fn: send_email_handler }
];

// Load events
// These depend on destiny.js and events.js, which have been included previously
addEvent(window, 'load', riff_init);

