var menu = new Array("dropdownhome", "dropdown1", "dropdown2", "dropdown3", "dropdown4", "dropdown5", "dropdown6", "dropdown7", "dropdown8");
var HideDelay;
var ShowDelay;
var clickCheck = false;

/* changeColor - changes the color of the element with id "main" + idName to newColor
    parameters: 	string idName - HTML element ID;
			string newColor - new color to be set
    return: void
*/
function changeColor(idName, newColor) {
	var f = document.getElementById("main"+idName);
	if(f) {
		f.style.color = newColor;
	}
}

/* setVisibility - sets/resets visibility of the element with id idName
    parameters: 	string idName - HTML element ID;
			boolean isVisible - true if the element should be visible, false if the element should be hidden
    return: void
*/

function setVisibility(idName, isVis) {
	var e = document.getElementById(idName);

	if(e) {
		if(isVis) {
			e.style.visibility = "visible";
			changeColor(idName, "#FFFFFF");
		}
		else {
			e.style.visibility = "hidden";
			if(idName == "dropdownhome" || idName == "dropdown08") {
				changeColor(idName, "#7a7974");
			} else {
				changeColor(idName, "#333333");
			}
		}
	}
}


/* isVisible - checks whether the visibility of an element with id idName is set
    parameters: 	string idName - HTML element ID;
			boolean isVisible - true if the element should be visible, false if the element should be hidden
    return: false if element has it's visibility set to hidden, true otherwise
*/
function isVisible(idName) {
	var isVis = true; /* let's assume that the element is visible if it is not hidden */
	var e = document.getElementById(idName);
	if(e) {
		if(e.style.visibility == "hidden") {
			isVis = false;
		}
	}
	return isVis;
}


function HideContent(idName) {
	setVisibility(idName, false);
}


function ShowContent(idName) {
	setVisibility(idName, true);
}


function ReverseDisplay(idName) {
	if(!isVisible(idName)) {
		setVisibility(idName, true);
	} else {
		if (clickCheck != true) {
			HideDelay = setTimeout("setVisibility('"+idName+"', false)", 500);
		}
	}
}


function hideMenus(except) {
	for(i = 0; i < menu.length; i++) {
		if(menu[i] != except) {
			setVisibility(menu[i], false);
		}
	}
}


function submenuOver(idName) {
	clearTimeout(HideDelay);
	clearTimeout(ShowDelay);
	hideMenus(idName);
	setVisibility(idName, true);
}


function submenuHide(d) {
	if (clickCheck != true) {
		HideDelay = setTimeout("HideContent('"+d+"')", 500);
	}
}


function submenuShow(d) {
	if (clickCheck != true) {
		ShowDelay = setTimeout("ShowContent('"+d+"')", 500);
	}
}


function submenuClick() {
	clickCheck = true;
}


// Show / Hide
function HideAll(d) {
	var menu = new Array("dropdown01", "dropdown02", "dropdown03"); //separate with a comma

	for(i = 0; i < menu.length; i++) {
		if (menu[i] != d) {
			Hide(menu[i]);
		}
	}
}

function Hide(d) {
	if(d.length < 1) { return; }
	document.getElementById(d).style.display = "none";
}


function Show(d) {
	if(d.length < 1) { return; }
		document.getElementById(d).style.display = "block";
}


function Reverse(d) {
	if(d.length < 1) { return; }
	if(document.getElementById(d).style.display == "none") { document.getElementById(d).style.display = "block"; }
	else { document.getElementById(d).style.display = "none"; }
}
// END Show / Hide



// Function for populating all fields on Brochure Request form
function ChooseAllPrograms() {
	document.forms['bmcform'].MakeupArtistry_info.checked=true;
	document.forms['bmcform'].IntMakeupStudio_info.checked=true;
	document.forms['bmcform'].FreelanceMakeup_info.checked=true;
	document.forms['bmcform'].FashionDesign_info.checked=true;
	document.forms['bmcform'].FashionMerchandising_info.checked=true;
	document.forms['bmcform'].Hair_info.checked=true;
	document.forms['bmcform'].Esthetics_info.checked=true;
	document.forms['bmcform'].NailTechnology_info.checked=true;
	document.forms['bmcform'].SignatureSeries_info.checked=true;
}


// 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;
		}
	}
}