/*****************************************************************************/
/*                                                                           */
/* SCRIPT                                                                    */
/*   common.js                                                               */
/*                                                                           */
/* TYPE                                                                      */
/*   javascript library                                                      */
/*                                                                           */
/* DESCRIPTION                                                               */
/*   generic javascript functions                                            */
/*                                                                           */
/*****************************************************************************/

/*                                                                           */
/* display or hide error and set focus to the first field in error           */
/*                                                                           */
var formHasErrors = false;
function error(fieldName, messageName, isError) {
	fieldClasses = document.getElementById(fieldName).className.split(' ');
	if (isError) {
		if (! formHasErrors) {
			document.getElementById(fieldName).focus();
		}
		if (fieldClasses[0] == 'text' || fieldClasses[0] == 'select') {
			document.getElementById(fieldName).className = fieldClasses[0] + ' error';
		}
		document.getElementById(messageName).className = 'formErrorVisble';
		formHasErrors = true;
	} else {
		document.getElementById(fieldName).className = fieldClasses[0]
		document.getElementById(messageName).className = 'formErrorHidden';
	}
}

function showObject(obj, returnOutput) {
	var output = '';
	if (obj != null && typeof(obj) == 'object') {
		for (ele in obj) {
			output = output + '<b>' + ele + '</b> (' + typeof(obj[ele]) + ')<br />';
//			if (obj[ele] != null && returnOutput != true && typeof(obj[ele]) == 'object') {
//				tmpOutput = showObject(obj[ele], true);
//				output = output + '<div style="margin:0 0 0 64px;border-left:1px solid #000;">' + tmpOutput + '</div>';
//			} else {
				output = output + '<tt>' + obj[ele] + '</tt><hr />';
//			}
		}
	} else {
		output = typeof(obj) + ' is not an object';
	}
	if (returnOutput == true) {
		return output;
	} else {
		var outputWindow = window.open('', 'showObject', 'width=640,height=480,resizable=yes,scrollbars=yes');
		outputWindow.document.write(output);
	}
}

function textForUrl(text) {
	text = utfToLatin(text);
	text = text.replace(new RegExp('\\s+', 'g'), '_');
	text = text.toLowerCase();
	text = escape(text);
//	alert("1: " + text1 + "\n2: " + text2 + "\n3: " + text3 + "\n4: " + text4);
	return text4;
}

function submitSearch() {
	var tag = textForUrl(document.getElementById('searchTag').value);

	var newUrl;
	newUrl = '/' + seoFolderTag + '/' + tag + '.html';
	location = newUrl;
}

function utfToLatin(utfText) {
	var latinText = '';
	for (var i = 0; i <= utfText.length; i ++) {
		var code = utfText.charCodeAt(i);

		switch (code) {
			case 0:
				break;
			case 8211:
				latinText += '-';
				break;
			case 8212:
				latinText += '--';
				break;
			case 8216:
			case 8217:
				latinText += '\'';
				break;
			case 8220:
			case 8221:
				latinText += '"';
				break;
			case 8230:
				latinText += '...';
				break;
			case 8242:
				latinText += '(TM)';
				break;
			default:
				latinText += String.fromCharCode(code);
				break;
		}
	}
	return latinText;
}

/*                                                                           */
/* switch between local and global tags                                      */
/*                                                                           */
function switchLocalTags(useLocal)
{
	setCookie('tagtype', (useLocal == 'local' ? 'local' : 'global'), 30);

	showOrHideElement('populartagsShortSwitchLocal', (useLocal == 'local'));
	showOrHideElement('populartagsShortLocal', (useLocal == 'local'));
	showOrHideElement('populartagsLongSwitchLocal', (useLocal == 'local'));
	showOrHideElement('populartagsLongLocal', (useLocal == 'local'));
	showOrHideElement('populartagsRhcSwitchLocal', (useLocal == 'local'));
	showOrHideElement('populartagsRhcLocal', (useLocal == 'local'));

	showOrHideElement('populartagsShortSwitchGlobal', (useLocal != 'local'));
	showOrHideElement('populartagsShortGlobal', (useLocal != 'local'));
	showOrHideElement('populartagsLongSwitchGlobal', (useLocal != 'local'));
	showOrHideElement('populartagsLongGlobal', (useLocal != 'local'));
	showOrHideElement('populartagsRhcSwitchGlobal', (useLocal != 'local'));
	showOrHideElement('populartagsRhcGlobal', (useLocal != 'local'));
}

function setCookie(cookieName, cookieValue, expirationDays) {
	var expirationDate = new Date();
	expirationDate.setDate(expirationDate.getDate() + expirationDays);
	document.cookie = cookieName + '=' + escape(cookieValue) + ';expires=' + expirationDate.toGMTString() + ';path=/';
}

function showOrHideElement(elementId, show)
{
	if (document.getElementById(elementId) && document.getElementById(elementId).className)
	{
		document.getElementById(elementId).className = (show ? 'objectVisible' : 'objectHidden');
	}
}

function goCategory()
{
	var url = categoryUrls[document.getElementById('categoryid').selectedIndex];
	window.location = url;
	return false;
}

