//-- PShark - xWidgets v1.0 client-side ---------------------------------------------------

dAllEnabled = new Array();

dAllEnabled.mess = 		"window.status = message;";
dAllEnabled.wrongoutput = 	"ww = window.open ('about:blank', 'ff', 'scrollbars=yes');wd = ww.document;wd.write( 'Server Error: wrong output ' + HTTP.bridge.responseText );";
dAllEnabled.objID =		"window.status = obj.id";
dAllEnabled.clearing =		"window.status = '';";

dAllDisabled = new Array();

dAllDisabled.mess = 		"//window.status = message;";
dAllDisabled.wrongoutput = 	"//ww = window.open ('about:blank', 'ff', 'scrollbars=yes');wd = ww.document;wd.write( 'Server Error: wrong output ' + HTTP.bridge.responseText );";
dAllDisabled.objID =		"//window.status = obj.id";
dAllDisabled.clearing =		"//window.status = '';";

debug = dAllDisabled;

//-----------------------------------------------------------------------------------------

HTTP = new Array();

HTTP.init = function ( url ) {

	HTTP.url = url;
	if ( prev_handler = document.onclick ) {
		document.onclick = function (e) { prevhandler(e); widgets.clickhandler(e); }
	} else {
		document.onclick = function (e) { widgets.clickhandler(e); }
	}

};

HTTP.request = function ( request_string ) {
	var bridge;
	if( window.ActiveXObject ) {
		bridge = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if( window.XMLHttpRequest ) {
		bridge = new XMLHttpRequest();
	}

	var wDebugStack = getobj( "wDebugStack" );
	var wDebugCStack = getobj( "wDebugCurrentStack" );
	if (wDebugStack) {
		var message = "Making request: " + request_string + "<br>";
		wDebugCStack.innerHTML = message;
		wDebugStack.innerHTML += message + "<br>";
		wDebugStack.innerHTML += "Bridge = " + HTTP.bridge + "<br>";
	}

	bridge.onreadystatechange = function () { handleMessage(bridge); };
	try {
		var query_string = "WE&" + request_string + "&" + GLOBAL.string();
		bridge.open("GET", HTTP.url + "?" + query_string, true);
		if (wDebugStack) {
			wDebugStack.innerHTML += "Bridge opened with no error: " + query_string + "<br>";
		}
	} catch(e) {
		window.status = "Offline mode.";
		if (wDebugStack)
			wDebugStack.innerHTML += "Error = " + e + "<br>";
	}
	bridge.send(null);
	if (wDebugStack)
		wDebugStack.innerHTML += "Sent!<br>";
}

function handleMessage (bridge) {
	if( bridge.readyState == 4 ) {
		if( bridge.status == 200 ) {
			message = bridge.responseText;
			if( message )
				try {
					eval( message );
				} catch ( err ) {
					window.status = "Message error: " + err.description;
				}
			eval( debug.clearing );
			eval( debug.mess );
			var wDebugStack = getobj( "wDebugStack" );
			var wDebugCStack = getobj( "wDebugCurrentStack" );
			if( wDebugStack ) {
				wDebugCStack.innerHTML = message;
				wDebugStack.innerHTML += message.substring(0, 100) + "<br>";
			}
		} else {
			window.status = "HTTP error " + bridge.status + " " + bridge.statusText;
		}
	}
	GLOBAL.string();
}


/////////////////////////////////////////
widgets = new Array();

widgets.el = new Array();

widgets.reserveID = 0;
widgets.reserveIDname = "widget_R";

widgets.clickhandler = function (e) {

	if( !e ) e = window.event;
	obj = ( e.target ) ? e.target : e.srcElement;
	obj = ( obj.tagName != "OPTION" ) ? obj : obj.parentNode;

	h = obj.id.substr( 0, 2 );
	eval( debug.objID );
	if( h == "wb") 	button.click(e);
	if( h == "ws") 	submitbutton.click(e);
	if( h == "wc") 	checkbox.changed(e);
	if( h == "wd" ) dropdown.change(e);
	if( h == "wl")	link.click(e);
	if( h == "wi")	image.click(e);
}

/////////////////////////////////////////
textblock = new Array();

textblock.init = function ( objID, html, parentID ) {

	parentOBJ = getobj ( parentID );
	obj = getobj( objID );

	if( parentOBJ ) {
		if ( !obj ) {
			var dummy = document.createElement( "span" );
			parentOBJ.appendChild( dummy );
			dummy.innerHTML = html;
			var obj = getobj( objID );
			}
		}
	return obj;
}

upperCase = function ( ch ) {
	lower = 'abcdefghijklmnopqrstuvwxyz';
	upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
	index = lower.indexOf( ch );
	if( index >-1 ) {
		return upper.charAt( index );
	} else {
		return ch;
	}
}

textblock.set_style = function ( objID, style_string ) {

	var obj = getobj( objID ); if( !obj ) return -1;

	elements = style_string.split( ";" );
	for (i = 0; i < elements.length; i++ ) {
		element = elements[i].split( ":" );
		name = element[0];
		value = element[1];

		while( name.charAt(0) == " " ) { name = name.substr( 1, name.length - 1 ) }
		while( value.charAt(0) == " " ) { value = value.substr( 1, value.length - 1 ) }

		complexName = name.split( "-" );
		if ( complexName.length > 1) {
			name = complexName[0];
			for( j=1; j < complexName.length; j++ ) {
				complexName[j] = upperCase( complexName[j].charAt( 0 ) ) 
					+ complexName[j].substr( 1, complexName[j].length - 1);
				name += complexName[j];
			}
		}
		if( value ) {
			if( name != "class" ) {
				cmd = "obj.style." + name + " = " + "\"" + value + "\"";
				eval( cmd );
			}
			else {
				eval( "widgets.el[ objID ].className = " + "\"" + value + "\"" );
			}
		}
	}

}

textblock.set_html = function ( objID, html ) {
	var obj = getobj( objID ); if( !obj ) return -1;
	obj.innerHTML = html;
	
	// the callback and _UTIL_ stuff relates to code in /includes/master.js
	// but I don't think we ever use it, so commented out
	// since it caused a separate call to # every time
	// JK 9/18/07
	//	var callback = "<img src=\"\" onerror=\"_UTIL_AJAXEvent('"+objID+"','load','');\" style=\"width:1px;height:1px;\" />";
	//	_UTIL_AJAXEvent(objID,"response",'');
	//	obj.innerHTML = html+callback;
}

textblock.add_html = function ( objID, html ) {
	var obj = getobj( objID ); if( !obj ) return -1;
	obj.innerHTML += html;
}

textblock.hide = function ( objID ) {
	var obj = getobj( objID ); if( !obj ) return -1;
	obj.style.display = 'none';
}

textblock.unhide = function ( objID ) {
	var obj = getobj( objID ); if( !obj ) return -1;
	obj.style.display = '';
}

textblock.changeParent = function ( objID, parentID ) {
	var obj = getobj( objID ); if( !obj ) return 0;
	var parentOBJ = getobj( parentID ); if( !obj ) return 0;
	if( parentOBJ ) {
		dummy = document.createElement("span");
		parentOBJ.appendChild( dummy );

		parentOBJ.replaceChild( obj, dummy );
	} else {
		window.status = "Scrip Error: invalid parentID '" + parentID + "'";
	}
}

textblock.evaporate = function ( objID ) {
	var obj = getobj( objID ); if( !obj ) return -1;
	obj.parentNode.removeChild( obj );
}
/////////////////////////////////////////


/////////////////////////////////////////
button = new Array();

button.init = function ( objID, html, parentID ) {
	var obj = textblock.init( objID, html, parentID );
}

button.set_html = function ( objID, html ) {
	var obj = getobj( objID ); if( !obj ) return -1;
	obj.value = html;
}

button.click = function ( e ) {
	oid = ( e.target ) ? e.target.id : e.srcElement.id;
	HTTP.request( "oid=" + oid + "&event=click" );
}


submitbutton = new Array();

submitbutton.click = function ( e ) {
	obj = ( e.target ) ? e.target : e.srcElement;
	oid = obj.id;
	var request_str = "oid=" + oid;
	// get the form object containing the button
	fobj = obj.form;
	if ( fobj ) {
		str = getFormValues(fobj);
		if ( str ) {
			request_str += "&" + str;
		}
	}
	request_str += "&event=click";
	HTTP.request( request_str );
}

button.enable = submitbutton.enable = function ( objID ) {
	var obj = getobj( objID ); if( !obj ) return -1;
	obj.disabled = false;
}


/////////////////////////////////////////

/////////////////////////////////////////
link = new Array();

link.init = function (objID, html, parentID ) {
	var obj = textblock.init( objID, html, parentID );
}

link.set_html = function ( objID, html ) {
	var obj = getobj( objID ); if ( !obj ) return -1;
	obj.innerHTML = html;
}

link.click = function ( e ) {
	oid = ( e.target ) ? e.target.id : e.srcElement.id;
	HTTP.request( "oid=" + oid + "&event=click" );
}

/////////////////////////////////////////
image = new Array();

image.init = function (objID, html, parentID ) {
	var obj = textblock.init( objID, html, parentID );
}

image.set_html = function ( objID, html ) {
	var obj = getobj( objID ); if ( !obj ) return -1;
	obj.innerHTML = html;
}

image.click = function ( e ) {
	oid = ( e.target ) ? e.target.id : e.srcElement.id;
	HTTP.request( "oid=" + oid + "&event=click" );
}

/////////////////////////////////////////
update = new Array();

update.init = function( objID, interval, started ) {

	if( !widgets.el[ objID ] ){
		widgets.el[ objID ] = new Array();
		widgets.el[ objID ].interval = interval;
		if( started )
			update.start( objID );
		}

}

update.onupdate = function( objID ) {

	HTTP.request('oid=' + objID + '&event=update');

}

update.stop = function( objID ) {

	if( widgets.el[ objID ] )
		clearTimeout( widgets.el[ objID ].timer );

}

update.start = function( objID ) {

	if( widgets.el[ objID ] ) {
		clearTimeout( widgets.el[ objID ].timer );
		widgets.el[ objID ].timer = setInterval( "update.onupdate(\"" + objID + "\")", widgets.el[ objID ].interval );
		}
}

update.set_interval = function ( objID, interval ) {

	if( widgets.el[ objID ] ) {
		widgets.el[ objID ].interval = interval;
		}

}
/////////////////////////////////////////


/////////////////////////////////////////

GLOBAL = new Array();
GLOBAL.vars = new Array();

GLOBAL.set_var = function ( name, newval ) {
	GLOBAL.vars[name] = newval;
}

GLOBAL.get_var = function ( name ) {
	return GLOBAL.vars[name];
}

GLOBAL.delete_var = function ( name ) {
	delete GLOBAL.vars[name];
}

GLOBAL.string = function (  ) {
	var str = "";
	for (var key in GLOBAL.vars) {
		var value = GLOBAL.vars[key];
		// XXX: hack to get scriptaculous/prototype cooperating with maxwidgets.
		/// dont know how a function ends up in here.
		if ( typeof(value) == 'function' ) {
			continue;
		}
		str += key + "=" + value + "&";
	}
	return str;
}
/////////////////////////////////////////


/////////////////////////////////////////
checkbox = new Array();

checkbox.init = function ( objID, html, parentID ) {

	var obj = textblock.init( objID, html, parentID );

}

checkbox.changed = function (e) {

	if (!e) e = window.event;
	obj = e.target ? e.target : e.srcElement;
	objID = obj.id;
	state = obj.checked;
	HTTP.request('&oid=' + objID + '&event=change&state=' + state);

}

checkbox.set_html = function ( objID, html ) {

	var obj_label = getobj( objID + "_label" ); if( !obj_label ) return -1;
	obj_label.innerHTML = html;

}

checkbox.set_value = function ( objID, value ) {

	var obj = getobj( objID ); if( !obj ) return -1;
	obj.checked = value;

}

checkbox.enable = function ( objID ) {

	var obj = getobj( objID ); if( !obj ) return -1;
	obj.disabled = '';

}

checkbox.disable = function ( objID ) {

	var obj = getobj( objID ); if( !obj ) return -1;
	obj.disabled = 'true';

}
/////////////////////////////////////////


/////////////////////////////////////////

dropdown = new Array();

dropdown.init = function ( objID, html, parentID ) {

	var obj = textblock.init( objID, html, parentID );

}

dropdown.change = function (e) {

	if(!e) e = window.event;
	obj = ( e.target ) ? e.target : e.srcElement;
	obj = ( obj.tagName == "SELECT") ? obj : obj.parentNode;
	objID = obj.id;

	oldInd = getobj( objID ).old
	if ( oldInd != -1 )
		if ( oldInd - obj.selectedIndex != 0) {
			HTTP.request( "oid=" + objID + "&event=change&olditem=" + oldInd + "&newitem=" + getobj( objID ).selectedIndex );
			}
	getobj( objID ).old = getobj( objID ).selectedIndex;

}

dropdown.set_value = function ( objID, index ) {

	var obj = getobj( objID ); if( !obj ) return -1;
	obj.old = index;
	obj.childNodes[ index ].selected = true;

}

dropdown.set_html = function ( objID ) {

	var obj = getobj( objID ); if( !obj ) return -1;
	arg = 1;
	while( arguments[ arg ] && arguments[ arg + 1 ] ) {
		val = arguments[ arg ];
		html = arguments[ arg + 1 ];
		obj.childNodes[ val ].childNodes[0].nodeValue = html;
		arg += 2;
		}


}

/////////////////////////////////////////


/////////////////////////////////////////
listing = new Array();

listing.init = function( objID, html, parentID ) {

	var obj = textblock.init( objID, html, parentID );

}

listing.add_item = function ( objID, position, html ) {

	var obj = getobj( objID ); if( !obj ) return -1;

	if( position > -1 ) {
		item_list = document.createElement( "li" );
		item_list.innerHTML = html;
		obj.insertBefore( item_list, obj.childNodes[ position ] );
	} else {
	obj.innerHTML += html;
	}

}

listing.delete_item = function ( objID, position ) {

	var obj = getobj( objID ); if( !obj ) return -1;
	obj = obj.childNodes[ position ];
	obj.parentNode.removeChild( obj );

}


/////////////////////////////////////////


/////////////////////////////////////////

collection = new Array();

collection.init = function ( objID, html, parentID ) {

	var obj = textblock.init( objID, html, parentID );

}

collection.set_order = function ( objID ) {

	var main_obj = getobj( objID ); if( !main_obj ) return 0;
	obj = main_obj.childNodes[ findNode( main_obj, 0 ) ];
	objN = main_obj.getElementsByTagName( obj.tagName );
	arg = 0;
	while( argument = arguments[ ++arg ] ) {
		id = argument;
		cn = arg - 1;
		dest_obj = objN [ cn ];
		src_obj =  getobj( id )

		if( dest_obj && src_obj ) {
			tmpID = dest_obj.id;
			if( !tmpID ) {
				dest_obj.id = widgets.reserveIDname + ( widgets.reserveID++ );
				tmpID = dest_obj.id;
				}
			dest_obj.id = src_obj.id;
			tmp = " " + dest_obj.innerHTML;
			dest_obj.innerHTML = " " + src_obj.innerHTML;
			src_obj.innerHTML = " " + tmp;
			src_obj.id = tmpID;

			tmpStyle = dest_obj.style.display;
			dest_obj.style.display = src_obj.style.display;
			src_obj.style.display = tmpStyle;
			}
		}

}

collection.add_elements = function ( objID ) {

	var main_obj = getobj( objID ); if( !main_obj ) return 0;
	arg = 0;
	while ( argument = arguments[ ++arg ] ) {
		html = argument;
		main_obj.innerHTML += html;
		}


}

collection.delete_elements = function (  ) {

	arg = -1;
	while ( argument = arguments[ ++arg ] ) {
		id_a = argument;
		textblock.evaporate( id_a );
		}
}

findNode = function ( parentOBJ, node ) {


	wx = -1;
	i = -1;
	for( wx; i != node && wx < parentOBJ.childNodes.length; ) {
			wx++;
			if( parentOBJ.childNodes[ wx ].tagName ) i++;
		}
	if( obj = parentOBJ.childNodes[ wx ] ) return wx; else return -1;

}

findLast = function ( parentOBJ, node ) {


	wx = parentOBJ.childNodes.length;
	i = 0;
	for( wx; i != node && wx >= 0; ) {
			wx--;
			if( parentOBJ.childNodes[ wx ].tagName ) i++;
		}
	if( obj = parentOBJ.childNodes[ wx ] ) return obj; else return -1;

}

collection.move_to_end = function ( objID ) {

	var main_obj = getobj( objID ); if( !main_obj ) return 0;
	for( wx=0; wx < main_obj.childNodes.length; wx++ ) {
		if( !main_obj.childNodes[0].id )
			main_obj.removeChild( main_obj.childNodes[0] );
		}
	objN = main_obj.childNodes;
	arg = 0;
	while( argument = arguments[ ++arg ] ) {
		id = argument;
		dest_obj = findLast( main_obj, arg );
		src_obj =  getobj( id );

		if( dest_obj && src_obj ) {
			tmp = " " + dest_obj.innerHTML;
			tmpID = dest_obj.id;
			if( !tmpID ) {
				dest_obj.id = widgets.reserveIDname + ( widgets.reserveID++ );
				tmpID = dest_obj.id;
				}
			dest_obj.innerHTML = " " + src_obj.innerHTML;
			dest_obj.id = src_obj.id;
			src_obj.innerHTML = " " + tmp;
			src_obj.id = tmpID;

			tmpStyle = dest_obj.style.display;
			dest_obj.style.display = src_obj.style.display
			src_obj.style.display = tmpStyle

			}
		}


}

collection.set_visible = function ( objID ) {

	var main_obj = getobj( objID ); if( !main_obj ) return 0;
	arg = 0;
	while( argument = arguments[ ++arg ] ) {
		id_a = argument;
		textblock.unhide( id_a );
		}

}

collection.all_visible = function ( objID ) {

	var main_obj = getobj( objID ); if( !main_obj ) return 0;
	for( wx=0; wx < main_obj.childNodes.length; wx++ ) {
		id_a = main_obj.childNodes[ wx ].id;
		textblock.unhide( id_a );
		}

}

collection.set_visible_to_only = function ( objID ) {

	var main_obj = getobj( objID ); if( !main_obj ) return 0;
	for( wx = 0; wx < main_obj.childNodes.length; wx++ )
		if( main_obj.childNodes[ wx ].tagName ) {
			id_b = main_obj.childNodes[ wx ].id;
			if( main_obj.childNodes[ wx ].tagName ) {
				if( !id_b ) {
					main_obj.childNodes[ wx ].id = widgets.reserveIDname + ( widgets.reserveID++ );
					id_b = main_obj.childNodes[ wx ].id;
					}
				inarr = 0;
				for( wy = 1; wy < arguments.length; wy++ ) {
					if ( arguments[ wy ] == id_b ) inarr = 1;
					}
				if( inarr ) {
					textblock.unhide( id_b );
					} else {
					textblock.hide( id_b );
					}
				}
		}

}

collection.set_invisible = function ( objID ) {

	var main_obj = getobj( objID ); if( !main_obj ) return 0;
	arg = 0;
	while( argument = arguments[ ++arg ] ) {
		id_a = argument;
		textblock.hide( id_a );
		}


}

/////////////////////////////////////////


/////////////////////////////////////////

getobj = function ( objname ) {

	return document.getElementById ( objname );

}

/////////////////////////////////////////


/////////////////////////////////////////

// Str class
function Str () {
	this.str = "";

	this.addNamePair = function (name, val) {
		// GLOBAL var takes precedence over form var
		if ( GLOBAL.get_var(name) == null ) {
			this.str += name + "=" + val + "&";	
		}
	}

	this.getStr = function () {
		return this.str.substr(0,(this.str.length - 1));
	}
}

function getFormValues(fobj) {
	var str = new Str();
	for(var i = 0;i < fobj.elements.length;i++) {
		switch(fobj.elements[i].type) {
			case "text":
				str.addNamePair(fobj.elements[i].name, fobj.elements[i].value);
				break;
			case "select-one":
				str.addNamePair(fobj.elements[i].name, 
						fobj.elements[i].options[fobj.elements[i].selectedIndex].value);
				break;
			case "checkbox":
				if ( fobj.elements[i].checked ) {
					str.addNamePair(fobj.elements[i].name, fobj.elements[i].value);
				}
				break;
		   }
	}

	return str.getStr();
}
