// blanche macdonald form processing javascript functions

// wrap the html contained in the passed id with an error class (make errors red)
function redText(idText) {
	// retrieve the current text in the passed id
	var currentText = document.getElementById(idText).innerHTML;
	
	// format the current text wrapped in a span
	var newText = "<span class=\"error\">" + currentText + "</span>";
	
	// replace the current text in the document with our newly formatted text
	document.getElementById(idText).innerHTML = newText;
}

// take a passed array and process through redText
function processErrors(errorArray) {
	// loop through the given array
	for (var i = 0; i < errorArray.length; i++) {
		// feed the current id to redText
		redText(errorArray[i]);
	}
}

// take a passed array populate all elements of a form on a page.  meant to be used by setting array in php with all _POST vars as items
function populateFormPosts(formArray) {
	// loop through the given form (TODO: we are hard coding form to 'application', need to figure a way to make this more dynamic)
	for (var i = 0; i < document.application.elements.length; i++) {
		// first, grab the name of this input
		var inputName = document.application.elements[i].name;
		
		// check what type of input this is
		switch(document.application.elements[i].type) {
			// text field
			case "text":
				document.application.elements[i].value = formArray[inputName];
				break;
			// dropdown list
			case "select-one":
				document.application.elements[i].value = formArray[inputName];
				break;
			// radio button
			case "radio":
				// check if the value of this radio button matches the inputted value
				if (document.application.elements[i].value == formArray[inputName]){
					// there's a match, set it to true
					document.application.elements[i].checked = true;
				}
				break;
			// checkbox
			case "checkbox":
				// check if the value of this checkbox matches the inputted value
				if (document.application.elements[i].value == formArray[inputName]){
					// there's a match, set it to true
					document.application.elements[i].checked = true;
				}
				break;
			// textarea
			case "textarea":
				document.application.elements[i].value = formArray[inputName];
				break;
		}
	}
}