/*
	getElementsByClassName
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/	
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

function openSubmenu(arrow, replacement, menu) {
	
	var over_arrows = getElementsByClassName('arrow_over');
	for (i = 0; i < over_arrows.length ; i++) {
		over_arrows[i].style.display = 'none';
	}
	var norm_arrows = getElementsByClassName('arrow_normal');
	for (i = 0; i < norm_arrows.length ; i++) {
		norm_arrows[i].style.display = 'block';
	}
	document.getElementById(arrow).style.display = 'none';
	document.getElementById(replacement).style.display = 'block';
	var popups = getElementsByClassName('popup_links');
	for (i = 0; i < popups.length ; i++) {
		popups[i].style.display = 'none';
	}
	document.getElementById(menu).style.display = 'block'
	
}

function closeSubmenus() {
	
	var over_arrows = getElementsByClassName('arrow_over');
	for (i = 0; i < over_arrows.length ; i++) {
		over_arrows[i].style.display = 'none';
	}
	var norm_arrows = getElementsByClassName('arrow_normal');
	for (i = 0; i < norm_arrows.length ; i++) {
		norm_arrows[i].style.display = 'block';
	}
	var popups = getElementsByClassName('popup_links');
	for (i = 0; i < popups.length ; i++) {
		popups[i].style.display = 'none';
	}
	
}

function activateSearchBox(inputid){
	
	if (document.getElementById(inputid).value == 'Search classifieds, events and more...') {
		document.getElementById(inputid).value = '';
	}
	
}

function clearSearchBox(inputid){
	
	if (document.getElementById(inputid).value == '') {
		document.getElementById(inputid).value = 'Search classifieds, events and more...';
	}
	
}

function activateDesc(inputid){
	
	if (document.getElementById(inputid).value == 'Enter your description here.') {
		document.getElementById(inputid).value = '';
	}
	
}

function clearDesc(inputid){
	
	if (document.getElementById(inputid).value == '') {
		document.getElementById(inputid).value = 'Enter your description here.';
	}
	
}

function activatePrice(inputid){
	
	if (document.getElementById(inputid).value == '0.00') {
		document.getElementById(inputid).value = '';
	}
	
}

function clearPrice(inputid){
	
	if (document.getElementById(inputid).value == '') {
		document.getElementById(inputid).value = '0.00';
	}
	
}

function countdown(field,total) {
	
	current = document.getElementById(field).value.length;
	remaining = total - current;
	document.getElementById(field+'_count').innerHTML = remaining;
	if (remaining < 6) {
		document.getElementById(field+'_count').style.background = '#ff0000';
	} else {
		document.getElementById(field+'_count').style.background = '#1c497f';
	}
	
}

function limit(field,total) {
	
	current = document.getElementById(field).value;
	if (current.length > total) {
		limited = current.substring(0,total);
		document.getElementById(field).value = limited;
		document.getElementById(field+'_count').innerHTML = '0';
		document.getElementById(field+'_count').style.background = '#ff0000';
	} else {
		remaining = total - current.length;
		document.getElementById(field+'_count').innerHTML = remaining;
		if (remaining <= 5) {
			document.getElementById(field+'_count').style.background = '#ff0000';
		} else {
			document.getElementById(field+'_count').style.background = '#1c497f';
		}
	}
	
	
}

function createBookmark(url) {
	
	title = "SimplyLocal Online"; 
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if ( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); 
	} else if (window.opera && window.print) { // Opera Hotlist
		return true;
	}
	
}

function displayFeature(num,media,tot) {
	
	for (i=1;i<=tot;i++) {
		document.getElementById('f'+i).className = '';
	}
	document.getElementById('f'+num).className = 'active';
	document.getElementById('feature_display').innerHTML = media;
		
}

function pop(url) {
	window.open(url, 'popWin', 'top=100,left=200,width=480,height=360,resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
}
