/* --------------------------------------------------------
   Javascript Form Functions
   forms.js
   -------------------------------------------------------- */

// set all options in a multi-select field to selected
function selectAllNone(theForm, theField, selValue) {
	//get field
	curField = eval("document." + theForm + "." + theField);
	
	//loop through field values and set all to selected
	for (i = 0; i < curField.options.length; i++) {
		curField.options[i].selected = selValue;
	}
}

// transfer all selected items from a multi-field to a comma separated hidden field
function saveSelection(theForm, theField, theHidden) {
	//get fields
	curField = eval("document." + theForm + "." + theField);
	saveField = eval("document." + theForm + "." + theHidden);
	
	//output string
	outStr = "";
	
	//loop through field options and save all selected to output string
	for (i = 0; i < curField.options.length; i++) {
		if (curField.options[i].selected == 1) {
			outStr = outStr + "," + curField.options[i].value;
		}
	}
	
	//trim first char
	outStr = outStr.substr(1);
	
	//write value to hidden field
	saveField.value = outStr;
	
	//proceed with form submit
	//return TRUE;
}

// transform a string to URL-friendly version
function strToUrl(divId, strStart, strEnd, fieldId) {
	// load and transform the field result
	theURL = document.getElementById(fieldId).value;
	theURL = strAccentsToFamily(theURL);
	theURL = theURL.toLowerCase();
	theURL = theURL.replace(/[-]/g, '_');
	theURL = theURL.replace(/(\n|\r|' '|[^a-zA-Z0-9_])/g, '');
	
	document.getElementById(divId).innerHTML = strStart + theURL + strEnd;
}

// transform accents to their equivalen char family (ex. é -> e)
function strAccentsToFamily(str) {
	//init temp str
	temp_str = str;
	
	//go thru char families and transform them
	temp_str = temp_str.replace(/[àáâãäåæ]/gi, 'a');
	temp_str = temp_str.replace(/[ç]/gi, 'c');
	temp_str = temp_str.replace(/[Ð]/gi, 'd');
	temp_str = temp_str.replace(/[èéêë]/gi, 'e');
	temp_str = temp_str.replace(/[ìíîï]/gi, 'i');
	temp_str = temp_str.replace(/[ñ]/gi, 'n');
	temp_str = temp_str.replace(/[ðòóôõöøŒ]/gi, 'o');
	temp_str = temp_str.replace(/[ßŠš]/gi, 's');
	temp_str = temp_str.replace(/[ùúûüµ]/gi, 'u');
	temp_str = temp_str.replace(/[ýÿ¥]/gi, 'y');
	temp_str = temp_str.replace(/[ž]/gi, 'z');
	
	return temp_str;
}

/* SWAP DIV OPEN/CLOSE */
function switchMenu(obj) {
	var el = document.getElementById(obj);
		if(el.style.display != "block") {
			el.style.display = "block";
		} else {
			el.style.display = "none";
		}
}

// show/hide stateObj based on certain specific values (showValues) in dropdown (countryObj)
function displayCountryState(stateObj, countryObj, showValues) {
	// get the stateObj to display / hide
	var so = document.getElementById(stateObj);
	
	// get the countryObj to read from
	var co = document.getElementById(countryObj);
	
	//get value of selected countryObj item
	var selCountry = co.options[co.selectedIndex].value;
	
	// determine if stateObj show be visible or not
	if ( showValues.search(selCountry) != -1 ) {
		//show
		so.style.display = "block";
	} else {
		//hide
		so.style.display = "none";
	}
}