/*
  $Id: lc1.js,v 1.7 2008/07/21 14:13:06 pmorrill Exp $
*/

//
// global variables
//
var	winRight	 = 0;
var 	winBottom	 = 0;
var	ctrlPanel	 = null;
var	panelUrl	 = '';
var	g_mod		 = 0;

function popwin(url,name,width,height,options) {
	win_size();

	var x = ( width < winRight ? (winRight - width)/2 : 50 );
	var y = ( height < winBottom ? (winBottom-height)/2 : 50 );

	//
	// default options:
	//
	if ( ! options || options == '' ) options = 'scrollbars=yes,resizable=yes';

	popup = window.open(url,name,'width='+width+',height='+height+',screenx='+x+',screeny='+y+',left='+x+',top='+y+','+options);
	popup.focus();
}

function popup_window(url,name,width,height,options) {
	win_size();

	var x = ( width < winRight ? (winRight - width)/2 : 50 );
	var y = ( height < winBottom ? (winBottom-height)/2 : 50 );

	//
	// to initiate correct sub-nesting of templates, we add a parameter
	//
	var pi = new RegExp(/\?.+=/);
	if ( pi.test(url) ) var tp = '&type=pop';
	else {
		var rg = new RegExp(/\/$/);
		var tp = ( rg.test(url) ? 'index.php/pop' : '/pop' );
	}

	//
	// default options:
	//
	if ( ! options || options == '' ) options = 'scrollbars=yes,resizable=yes';

	popup = window.open(url+tp,name,'width='+width+',height='+height+',screenx='+x+',screeny='+y+',left='+x+',top='+y+','+options);
	popup.focus();
}

function close_popup() {
	if ( confirm('Any changes that you have not yet saved will be lost') ) window.close();
}

function jumpTo(list,refer) {
	if ( list.options[list.selectedIndex].value == 0 ) return;
	location.href=refer+list.options[list.selectedIndex].value;
}

function select_checks_on_form(form,on) {
	if ( document.forms[form] ) return select_checks(document.forms[form],on);
}

function select_checks(form,on) {
	for (var i = 0; i < form.elements.length; i++) {
		var s = form.elements[i];
		if ( s.type == 'checkbox' ) { s.checked = on; }
	}
}

function select_all(on) {
	select_checks(document.theform,on);
}

function set_focus(f,e) {
	if ( f && f.elements[e] ) {
		f.elements[e].focus();
		if ( f.elements[e].select ) f.elements[e].select();
	}
}

//
// just try to get the window size
//
function win_size() {
	if ( window.innerWidth ) {
		winRight = window.innerWidth;
		winBottom = window.innerHeight;
	} else if ( document.documentElement.clientWidth ) {
		winRight = document.documentElement.clientWidth;
		winBottom = document.documentElement.clientHeight;
	} else {
		winRight = document.body.clientWidth;
		winBottom = document.body.clientHeight;
	}

	//
	// sanity
	//
	if ( !winBottom || winBottom > 800 ) winBottom = 800;
}

function control_panel(url,keep) {
	if ( ctrlPanel && panelUrl == url && keep == 0 ) { ctrlPanel.close(); ctrlPanel = null; return; }
	popup = window.open(url,'pControl','width=750,height=150,screenx=20,screeny=30,left=20,top=30,modal=yes,scrollbars=yes,resizable=yes,toolbar=no');
	popup.focus();
	panelUrl = url;
	ctrlPanel = popup;
}

function destroy_panel() {
	if ( ctrlPanel ) ctrlPanel.close();
}

function href_parent(url) {
	if ( opener ) opener.document.location.href=url;
}

//
// pull another jscript file in to the global scope, but only after the file that calls
// this func has finished execution.
//
function IncludeJavaScript(jsURL,id) {
	var head = document.getElementsByTagName("head").item(0);
	if ( !head ) return;
	var s = document.createElement("script");
	if ( !s ) return;
	s.setAttribute("type", "text/javascript");
	s.setAttribute("charset", "utf-8");
	s.setAttribute("src", jsURL);
	s.setAttribute("id", id);
	head.appendChild(s);
}

//
// remove a dynamically appended child - referenced by its id
//
function RemoveJavaScript(id) {
	var head = document.getElementsByTagName("head").item(0);
	var s = document.getElementById(id);
	if ( s ) head.removeChild(s);
}

//
// send a request to our json server
//
function fire_json(host,request,cb) {
	IncludeJavaScript(host+'/srv/json.php/'+request+'/0/'+cb,cb);
}

//
// simple test function to work against the standard srv at pmorrill.net
//
function json_test(json,out) {
	if ( out ) { fire_json('http://www.pmorrill.net','SRV_TEST','json_test'); return; }
	if ( json.Error ) { alert(json.Error); return; }
	alert(json.Info);
	RemoveJavaScript('json_test');
}

//
// initialize a div with the contents of the data structure
//
function init_div(json) {
	if ( json.Error ) { alert(json.Error); return; }
	if ( dd = document.getElementById(json.div_id) ) {
		dd.innerHTML = json.Content;
	}
}

//
// respond to a tab click: save current form, and pass new url into the
// script for referring; global g_mod is to signal a dirty form;
//
function tab_over(url,s,r) {
	if ( g_mod && s ) {
		var submit = document.getElementById(s);
		var refer = document.getElementById(r);
		if ( submit && refer ) {
			refer.value = url;
			submit.click();
		} else return false;
	} else document.location.href='/'+url+(opener ? '/pop' : '')+'#tabs';
}


function popup_dom(what) {
	popup_window('/admin/dumpdom.php?dom='+what+'&type=popup','dViewer',700,600,'scrollbars=yes,resizable=yes');
}

function dump(theObj,nodestr) {
	var tx = '';
	var c = 0;
	if ( theObj.tagName ) {
		tx = "&nbsp;Tag: "+theObj.tagName+"&nbsp;|&nbsp;";
	}
	if ( theObj.firstChild ) {
		var ch = theObj.firstChild;
		var n = 0;
		for ( var j = 0; j < theObj.childNodes.length; j++ ) {
			ch = theObj.childNodes[j];
			if ( ch.tagName )	{ cx += " <a href=\"javascript:document.location.href='/admin/dumpdom.php?dom="+nodestr+".childNodes["+j+"]'\" >"+ch.tagName+"</a>"; n++; }
		}
		tx += "&nbsp;"+ n +" children: "+cx;
		tx += '<p />';
	}
	tx += "<table width=\"80%\" align=\"center\" bgcolor=\"#000000\" cellspacing=\"1\" cellpadding=\"2\">";
	tx += "<tr bgcolor=\"#efefde\"><td align=\"center\"><b>Property</b></td><td align=\"center\"><b>Descend</b></td></tr>";
	var props = new Array();
	for (var i in theObj) { props.push(i); }
	props.sort();
	for (var i = 0; i < props.length; i++) {
		str = props[i];
		tx += "<tr  bgcolor=\"#ffffff\"><td><a href=\"javascript:alert(eval('opener"+nodestr+'.'+str+"'))\">"+str+"</a></td><td><a href=\"javascript:document.location.href='/admin/dumpdom.php?dom="+nodestr+'.'+str+"'\">Descend</a></td></tr>";
	}
	tx+="</table>"

	return tx;
}
