function changesite(w){
	var siteid = w.options[w.selectedIndex].value;
	window.location.href = "/admin/site/change.php?ref=" + escape(window.location) + "&siteid=" + escape(siteid)
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function glossary() {
	glossaryWin = window.open('/glossary/glossary.php','remote','resizable,dependent,scrollbars,width=560,height=380,left=0,top=0');
	glossaryWin.focus();
}

function openDemoWindow() {
  demoWin = window.open('/ConversionRulerDemo.htm', 'remote', 'resizable,dependent,width=801,height=616,left=0,top=0');
  demoWin.focus();
}

function delitem(Index) {
	if (confirm("Are you sure you want to delete this account?")){
		window.location.href='delgrp.php?modgrp='+Index;
	}
}

function rdetails(URL,width,height) {
	if (width == null) var width = 840;
	if (height == null) var height = 500;
	if (navigator.userAgent.indexOf('Chrome/') > 0) {
		if (window.detwin) {
			window.detwin.close();
			window.detwin = null;
		}
	}
	window.detwin = window.open(URL, 'detwin','width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes');
	window.detwin.focus();
}


function newWindowTwo(URL,width,height) {
	if (width == null) var width = 840;
	if (height == null) var height = 500;
	var w = window.open(URL,'catWin','width=' + width + ',height=' + height + ',scrollbars=yes,resizable=yes');
	w.focus();
}


function geo_window(URL)
{
	var w = window.open(URL,'geowin','width=' + 460 + ',height=' + 380 + ',scrollbars=yes,resizable=yes');
	w.focus();
}

function newWindowCSV(URL) {
	csvWindow = window.open(URL);
	csvWindow.focus();
}


function permission_save(id, u, s, t, b)
{
	$('#status-'+id).hide();
	$.get('/admin/user/save-permission.php?User='+u+'&Site='+s+'&Action='+t+'&Value=' + b, function(data) {
		$('#status-'+id).show().empty().append(data).fadeOut(1000);
	});
}

function preference_save(n,v)
{
	$.get('/admin/user/save-preference.php?Type='+n+'&Value='+v, function (data) {
		$('#' + n).empty().append(data).fadeOut(3000);
	});
}

function keyword_update_status(e, v, icon_prefix, site_id, id)
{
	e = e || window.event;
	var delta = e.shiftKey ? -1 : 1;
	if (e.stopPropagation) {
		e.stopPropagation();
	} else {
		e.cancelBubble = true;
	}
	v = parseInt(v) + delta;
	if (v < -1) v = 1;
	if (v > 1) v = -1;
	var src;
	switch (v) {
		case -1: src = "neg"; break;
		case 0: src = "blank"; break;
		case 1: src = "pos"; break;
	}
	src = '/admin/report/keyword/update.php?Site=' + site_id + '&ID=' + id + '&Status=' + v + '&_r=' + Math.random();
	$('#'+icon_prefix+id).attr("src", src);
	return v;
}

function article_dismiss(id)
{
	$("#article-sticky-" + id).fadeOut('slow');
	var x = new Image();
	x.src = '/admin/news/dismiss.php?ID=' + id;
}

var pending_report_timer = null;

function pending_report_refresh_stop()
{
	if (pending_report_timer) {
		clearTimeout(pending_report_timer);
		pending_report_timer = null;
	}
}

function pending_report_refresh_init(qs)
{
	var delta = arguments[1] || 0;
	var nsec = Math.min(5000 + delta, 10000);
	pending_report_timer = setTimeout('pending_report_refresh(\'' + qs + '\','+delta+')', nsec);
}

function pending_report_refresh(qs, delta)
{
	var u =  QS_format('pending.php?' + qs , "_r=" + Math.random());
	$.get(u,
		function(data) {
			$('#pending-report').html(data);
			if (data.indexOf("need_refresh") >= 0) {
				pending_report_refresh_init(qs, delta + 500);
			}
		}
	);
}

function report_status_update_go(report_id, final_url)
{
	$.get('status.php?ID=' + report_id + "&_r=" + Math.random(), function (data) { if (!report_status_update_check(data, final_url)) { report_status_update(report_id, final_url); } });
}

function report_status_update_check(data, final_url)
{
	$('#report-status').html(data);
	suffix = (final_url.indexOf('?') < 0 ? '?' : '&') + '_r=' + Math.random();
	if (data.indexOf('*DONE*') >= 0) {
		document.location = final_url + suffix;
		return true;
	} else if (data.indexOf('*FAIL*') >= 0) {
		document.location = final_url + suffix;
		return true;
	}
	return false;
}

function report_status_update(report_id, final_url)
{
	setTimeout('report_status_update_go('+report_id+',\'' + final_url + '\')', 1000);
}
