function getElem( idString ) {
	var obj = null;
	if ( document.getElementById ) {
		obj = document.getElementById( idString );
	} else if ( document.layers
				&& document.layers[idString] ) {
		obj = document.layers[idString]
	} else if ( document.all
				&& document.all[idString] ) {
		obj = document.all[idString]
	}
	return obj;
}

function showById(menuId) {
	getElem(menuId).style.display="block";
}

function hideById(menuId) {
	getElem(menuId).style.display="none";
}

function selectByValue(id,v)
{
	if( v == null ) return;
	e = document.getElementById(id).parentNode;
	for( var i=0; i < e.options.length; i++ )
	{
		if( e.options[i].value == v )
		{
			e.options[i].selected = true; break;
		}
	}
}

function parseQs()
{
	var qs    = new Array();
	if ( window.location.search && window.location.search.length )
	{
		var pairs = new Array();
		pairs = window.location.search.substring(1).split('&');
		for ( var i=0; i < pairs.length; i++ )
		{
			qs[ pairs[i].split('=')[0] ] = pairs[i].split('=')[1];
		}
	}
	return qs;
}

function article(newUrl)
{
	opener.window.location.href = newUrl;
}

function joe(newWin) {
	var joeWin=window.open(newWin,"","width=350, height=350, scrollbars=yes, menubar=no, resizable=yes");
	joeWin.focus();
	return;
}

function isUndefined(v) {
	var undef;
	return v===undefined;
}

/*
 * Example:
 * <a href="http://www.villagevoice.com/classifieds/oaefaq.php" onClick="popper(this,700,480,'scrollbars=1');return false;" target="winFAQ">
 *
 */
function popper(src, w, h, features) {
	var _POPUP_FEATURES = 'location=0,statusbar=1,menubar=0';
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	if (isUndefined(features)) {
		features = _POPUP_FEATURES;
	}
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+','+features;
	win = window.open( src.getAttribute('href'), src.getAttribute('target') || '_blank', winprops);
	if (win && win.focus) { win.focus(); }
	return win;
}

function fixDate(date) {
	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0)
		date.setTime(date.getTime() - skew);
}

function setCookie(name, value, expires, path, domain, secure) {
	var curCookie = name + "=" + escape(value) +
			((expires) ? "; expires=" + expires.toGMTString() : "") +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
	if (getCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function validateEmail(email, msg, optional)
{
	if (!email.value && optional)
	{
		return true;
	}
	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
	if (!re_mail.test(email.value))
	{
		alert(msg);
		email.focus();
		email.select();
		return false;
	}
	return true;
}

function val_generic_form(theForm,fieldArray)
{
	var x = 0;

	for(x=0;x<=fieldArray.length;x++)
	{
		if(!theForm.elements[fieldArray[x][0]].value)
		{
			alert("You need to fill in " + fieldArray[x][1]);
			return false;
		}

		if (fieldArray[x][2] && theForm.elements[fieldArray[x][0]].value.length > fieldArray[x][2])
		{
		alert("You are over the limit of " + fieldArray[x][2] + " characters. You typed " + theForm.elements[fieldArray[x][0]].value.length + " characters.");
		return false;
		}
	}
	return true;
}

function addLoadEvent(funcToAdd) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = funcToAdd;
	} else {
		window.onload = function() {
			oldonload();
			funcToAdd();
		}
	}
}
	
function getHost()
{
	var h = window.location.host.toString();
	if ( /^[\d\.]+$/.test(h) )
	{
		return h;
	}
	else
	{
		return window.location.host.match(/(\w+\.\w+)$/)[1];
	}
}
