function toggleNavMenu(elemId, anchor) {
	var elem = document.getElementById(elemId);
	if (elem && elem.style.display == 'none') {
		elem.style.display = "block";
		if (anchor) anchor.className = 'ptup nodeco';
		document.onmousedown = closeMenu;
	} else {
		elem.style.display = "none";
		if (anchor) anchor.className = 'ptdown nodeco';
	}
}
function closeMenu(e) {
	if (!e) var e = window.event;
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;

	if (targ && !targ.getAttribute('href') && targ.id != 'NavDropDown') {
		var elem = document.getElementById('NavDropDown');
		if (elem) {
			elem.style.display = "none";
			if (document.getElementById('NavMenu')) {
				document.getElementById('NavMenu').className = 'ptdown nodeco';
			}
		}
	}
}

function openTip(tip) {
	// close opening tip bubble
	$(".tip").hide();
	tipElem = 'D' + tip;
	var elem = document.getElementById(tipElem);
	if (elem) {
		elem.style.display = "inline";
		$('#Dm'+tip).load('/static/html/help/'+tip+'.htm');
		$(document).bind("click", {tipid: tip}, closeTip);
	}
}

function closeTip(e) {
	var targ;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	if (targ && targ.id.indexOf(e.data.tipid)==-1) {
		$('#D'+e.data.tipid).hide();
		$(document).unbind("click");
	}
}

// get the new location link based on the current category code and the state code
function getStateLocation(baseUri, state) {
	if (state.length > 0) {
		// the state needs to be either "all" or one of the 50 states
		return '/' + state.toLowerCase() + baseUri;
	} else {
		// if no state is defined, let the cookie take care
		return baseUri;
	}
}

// focus the first field of the form in the page
function setFocus() {
	var formName = document.getElementById('RegisterForm');
	if (formName) formName.Email.focus();
	formName = document.getElementById('LoginForm');
	if (formName) formName.Email.focus();
	formName = document.getElementById('ResetForm');
	if (formName) formName.Email.focus();
}

function blankit(elem, value) {
	if (!elem) return;
	if (elem.value == 'min' || elem.value == 'max' || elem.value == value) {
		elem.value = '';
	}
}

function toggleElem(elemId, anchor) {
	var elem = document.getElementById(elemId);
	if (elem && elem.style.display == 'none') {
		elem.style.display = "block";
		if (anchor) anchor.className = 'ptup nodeco';
	} else {
		elem.style.display = "none";
		if (anchor) anchor.className = 'ptdown nodeco';
	}
}

// only used in mypost page
function toggleForm(url, elemId, anchor) {
	if (anchor.className == 'ptup nodeco') {
		$('#'+elemId).hide();
		anchor.className = 'ptdown nodeco';
	} else {
		$('#'+elemId).show();
		$('#'+elemId).html('<div style="text-align:right;">Opening...</div>');
		$.ajax({ url: url, success: function(rc) { $('#'+elemId).html(rc); } });
		anchor.className = 'ptup nodeco';
	}
}

function bookmarkpage(title, url) {
	if (document.all)
		window.external.AddFavorite(url, title);
	else if (window.sidebar)
		window.sidebar.addPanel(title, url, "")
}

function processForm(action, dataForm, statusDiv, targetDiv) {
	var params = {};
	if (dataForm != '') { 
		$('#'+dataForm)
		.find("input[checked], input[type='text'], input[type='hidden'], input[type='submit'], option[selected], textarea").each(
			function() {
				params[ this.name || this.parentNode.name ] = this.value;
			}
		);
	}
	if (statusDiv != '') {
		$('#' + statusDiv).html('Submitting...');
	}
	$.ajax({
		url: action,
		data: $.param(params),
		type: "POST",
		success: function(rc) {
			$('#'+targetDiv).html(rc);
		}
	});
}

function saveThis(id, noti) {
	$.ajax({ url: '/ad/'+id+'/Save', success: function(rc) {$('#'+noti).html("<i>saved</i>");} });
}