/* Author: nolan b smith
** this is tailored to the specifically for the jQuery javascript framework
*/

var path = document.URL.split('/')[0] + "//" + document.URL.split('/')[2] + "/" + document.URL.split('/')[3] + "/" + document.URL.split('/')[4];

//alert( path + "/admin" );

if ( window.debug == undefined )
{	
	$( document ).ready( function( ) {
		$.getScript( path + "/js/debug.js" );
	} );
}

var ie = ( document.all ) ? true : false;

var ESCAPE = 27;
var ENTER = 13;
var TAB = 9;
var DELETE = 46;
var BACKSPACE = 8;
var LEFT = 37;
var RIGHT = 39;
var UP = 38;
var DOWN = 40;
var CR = 13; // carriage return
var CHR_s = 115;
var CHR_S = 83;

var fieldArray = new Array( );

/* Helper functions for keeping bad stuff being saved into the database that wrecks things...
**
*/
var _escANDset = {"&":"~-_","&amp;":"~-__"};
function _escAND( text )
{
	var temp = text;
	
	
	for(var i in _escANDset) temp=temp.replace(new RegExp(i,'g'), _escANDset[i]);
	
	//alert(temp);
	
	return temp;
}

function _unescAND( text )
{
	var temp = text;
	
	for(var i in _escANDset) temp=temp.replace(new RegExp(_escANDset[i],'g'), i);
	
	//alert(temp);
	
	return temp;
}
//,"'":"\u2019","'":"\u2032"
var _escHTMLset = {
	'"':'\u0022',
	'\u201C':'\u0022',
	'\u201D':'\u0022',
	"'":'\u0027',
	'\u2018':"\u0027",
	'\u2019':"\u0027",
	"\u2032":"\u0027"
}//&lt; and &gt;
function _escHTML(text)
{
	var temp = text;
	
	for(var i in _escHTMLset) {
		//errMsg( "i"+i+" _escHTMLset[i]"+_escHTMLset[i], true );
		temp=temp.replace(new RegExp(i,'g'), _escHTMLset[i]);
	}
	
	//alert(temp);
	//alert(escape(temp));
	return temp;
}

function _unescHTML(text)
{
	var temp = text;

	for(var i in _escHTMLset) temp=temp.replace(new RegExp(_escHTMLset[i],'g'), i);
	
	//alert(temp);
	//alert(escape(temp));
	return temp;
}

/*
possible attrs
id: span/td in document
url: processing function to call on lose focus
idField: field on the page that contains the single record key
name: name of the form field to be passed
elementInputType: 'textarea', 'checkbox', defaults to 'text'
elementType: 'span', defaults to 'td'
elementWidth: in pixels for text, columns for textarea
elementStyle: style to be put into element
blrSave: true/false switch will make bluring the element save, default false
btnSave: true/false switch will put save image to left of element, default false
chgSave: true/false switch will make changing the element save, default false

[NOTE]: default behavior for saving is by CTRL-S and TAB, blrSave, btnSave, chgSave 
		do not override this function, instead they supplement it
*/
function makeEditable( attrs )
{
	if ( !$( attrs.id ) ) return false;
	
	if ( !attrs.elementType ) attrs.elementType = "td";
	if ( !attrs.elementInputType ) attrs.elementInputType = "text";
	if ( !attrs.elementWidth ) attrs.elementWidth = "100";
	if ( !attrs.blrSave ) attrs.blrSave = false;
	if ( !attrs.btnSave ) attrs.btnSave = false;
	if ( !attrs.chgSave ) attrs.btnSave = false;

	errMsg( attrs.id + " " + attrs.elementInputType + " " + attrs.url, true );

	$( attrs.id ).attr( "_url", attrs.url );
	$( attrs.id ).attr( "_idField", attrs.idField );
	$( attrs.id ).attr( "_name", attrs.name );
	$( attrs.id ).attr( "_elementInputType", attrs.elementInputType );
	$( attrs.id ).attr( "_elementType", attrs.elementType );
	$( attrs.id ).attr( "_elementWidth", attrs.elementWidth );
	
	if ( attrs.elementInputType == "checkbox" )
	{
		$( attrs.id ).change( function( ){ saveSpecial( attrs ); } );
	}
	else if ( attrs.elementInputType == "radio" )
	{
		$( attrs.id ).change( function( ){ saveSpecial( attrs ); } );
	}
	else if ( attrs.elementInputType == "select" )
	{
		$( attrs.id ).change( function( ){ saveSpecial( attrs ); } );
	}
	else if ( attrs.elementInputType == "button" )
	{
		$( attrs.id ).click( function( ){ saveSpecial( attrs ); return false; } );
	}
	else
	{
		$( attrs.id ).click( function( ){ edit( attrs ); } );
		$( attrs.id ).mouseover( function( ){ $( this ).toggleClass( "editable" ); } );
		$( attrs.id ).mouseout( function( ){ $( this ).toggleClass( "editable" ); } );
	}

	fieldArray[ fieldArray.length ] = attrs.id;
}

function makeEditableOld( id, url, idField, name, elementInputType, elementType, elementWidth, elementStyle )
{
	//errMsg( id, true );
	
	if ( elementType == null ) elementType = "td";
	
	if ( elementInputType == null ) elementInputType = "text";
	
	if ( elementWidth == null ) elementWidth = "100";
	
	if ( !$( id ) ) return false;
	
	$( id ).attr( "_url", url );
	$( id ).attr( "_idField", idField );
	$( id ).attr( "_name", name );
	$( id ).attr( "_elementInputType", elementInputType );
	$( id ).attr( "_elementType", elementType );
	$( id ).attr( "_elementWidth", elementWidth );
	
	$( id ).click( function( ){ editOld( id, url, idField, name, elementInputType, elementType, elementWidth ); } );
	$( id ).mouseover( function( ){ $( this ).toggleClass( "editable" ); } );
	$( id ).mouseout( function( ){ $( this ).toggleClass( "editable" ); } );

	fieldArray[ fieldArray.length ] = id;
}

function makeNewField( id, url, name, elementInputType, elementType, elementWidth )
{
	//errMsg( url + " " + id, true );
	
	if ( elementType == null ) elementType = "td";
		
	if ( elementInputType == null ) elementInputType = "text";
		
	if ( elementWidth == null ) elementWidth = "100";
	
	if ( !$( id ) ) { error( "no such id exists", true ); return false; }
	
	$( id ).attr( "_url", url );
	$( id ).attr( "_name", name );
	$( id ).attr( "_elementInputType", elementInputType );
	$( id ).attr( "_elementType", elementType );
	$( id ).attr( "_elementWidth", elementWidth );
	
	$( id ).click( function( ){ newField( id, url, name, elementInputType, elementType, elementWidth ); } );
	$( id ).mouseover( function( ){ $( this ).toggleClass( "editable" ); } );
	$( id ).mouseout( function( ){ $( this ).toggleClass( "editable" ); } );
}

function setBeforeHandler( id, func )
{
	try
	{
		if ( typeof( func ) == "function" ) $( id )[ 0 ]._beforeFunction = func;
		else $( id )[ 0 ]._beforeFunction = eval( func );
	}
	catch( e )
	{
	}
};

function setAfterHandler( id, func )
{
	try
	{
		if ( typeof( func ) == "function" ) $( id )[ 0 ]._afterFunction = func;
		else $( id )[ 0 ]._afterFunction = eval( func );
	}
	catch( e )
	{
	}
};

function edit( attrs )
{
	try
	{
	// if there is a call-back function to run before
		if ( $( attrs.id )[ 0 ]._beforeFunction ) if ( !$( id )[ 0 ]._beforeFunction( ) ) return false;
	}
	catch( e )
	{
	}

	$( attrs.id ).hide( );
	
	var outerTag = null;
	var input = null;

	try
	{
		outerTag = $( document.createElement( attrs.elementType ) );
		if ( attrs.elementType == "div" )
		{
			outerTag.addClass( "td" );
			outerTag.addClass( "borderRBWhite" );
		}
	} catch( err ) { 
		alert('Element of type '+attrs.elementType+' cannot be created');
		cleanUp( attrs.id, true );
		return;
	}
	
	outerTag.attr( "id", $( attrs.id ).attr( "id" ) + "_editor" );
	
	var editReplace = new RegExp( "Click Here To Edit", "gi" );
	
	switch( attrs.elementInputType )
	{
		case "textarea":
			input = $( document.createElement( attrs.elementInputType ) );
			errMsg( "before:"+encodeURI($( attrs.id ).html( )), true );
			errMsg( "after:"+encodeURI($( attrs.id ).html( ).replace( editReplace, "" ).replace( /<!--br--><br>/gi, "\n" )), true );
			input.attr( { 
				"id":$( attrs.id ).attr( "id" ) + "_edit", 
				"name":$( attrs.id ).attr( "id" ), 
				"value":$( attrs.id ).html( ).replace( editReplace, "" ).replace( /<!--br--><br>/gi, "\n" )
			} );
			if ( !input.attr( "value" ) || Math.ceil( input.attr( "value" ).length / 200 ) == 0 || Math.ceil( input.attr( "value" ).length / 200 ) <= 4 )
				input.attr( { "rows":4 } );
			else if ( Math.ceil( input.attr( "value" ).length / 200 ) > 20 )
				input.attr( { "rows":20 } );
			else
				input.attr( { "rows":Math.ceil( input.attr( "value" ).length / 200 ) } );
			
			if ( typeof( attrs.elementWidth ) == "number" )
				input.css( "width", attrs.elementWidth + "px" );
			else if ( attrs.elementWidth.indexOf( "%" ) >= 0 || attrs.elementWidth.indexOf( "px" ) )
				input.css( "width", attrs.elementWidth );
				/*, 
				"cols":elementWidth*/
			break;
		case "checkbox":
			input = $( document.createElement( "input" ) );
			input.attr( {
				"type":attrs.elementInputType, 
				"id":$( attrs.id ).attr( "id" ) + "_edit", 
				"name":$( attrs.id ).attr( "id" ), 
				"value":1 
			} );
			if ( $( attrs.id ).attr( "type" ) == "checkbox" )
				if ( $( attrs.id ).attr( "checked" ) ) input.attr( "checked", true );
			
			if ( $( attrs.id ).html( ) == "YES" ) input.attr( "checked", true );
			break;
		case "text":
			input = $( document.createElement( "input" ) );
			input.attr( {
				"id":$( attrs.id ).attr( "id" ) + "_edit", 
				"name":$( attrs.id ).attr( "id" ), 
				"value":$( attrs.id ).html( ).replace( editReplace, "" ), 
				"autocomplete":"off"
			} );
			errMsg( "eleWid:" + typeof( attrs.elementWidth ), true );
			if ( typeof( attrs.elementWidth ) == "number" )
				input.css( "width", attrs.elementWidth + "px" );
			else if ( attrs.elementWidth.indexOf( "%" ) >= 0 || attrs.elementWidth.indexOf( "px" ) )
				input.css( "width", attrs.elementWidth );
			break;
		default:
			input = $( document.createElement( attrs.elementInputType ) );
			input.attr( {
				"id":$( attrs.id ).attr( "id" ) + "_edit", 
				"name":$( attrs.id ).attr( "id" ), 
				"value":$( attrs.id ).html( ).replace( "Click Here To Edit", "" ) 
			} );
			input.css( "width", attrs.elementWidth + "px" );
			break;
	}
	
	if ( attrs.btnSave )
	{
		saveBtn = $( document.createElement( "img" ) );
		saveBtn.attr( { 
			id:$( attrs.id ).attr( "id" ) + "_editSaveBtn",
			src:path+"/images/widgets/plain/disk.png"
		} );
		saveBtn.toggleClass( "clickable" );
	}
	
	$( attrs.id ).after( outerTag );
	if ( attrs.btnSave )
	{
		outerTag.append( saveBtn );
	}
	outerTag.append( input );
	
	errMsg( "#" + $( attrs.id ).attr( "id" ) + "_edit", true );
	var thisBox = $( "#" + $( attrs.id ).attr( "id" ) + "_edit" );

	thisBox.abandonChanges = false;
	
	thisBox.focus( );
	
	errMsg( $( attrs.id ).attr( "id" ).replace( "text_", "" ) , true );
	try
	{
		enableBtns( $( attrs.id ).attr( "id" ).replace( "text_", "" ) );
	}
	catch( e ) { /*does not exist*/errMsg( "unable to enable buttons", true ); }
	
	thisBox.keyup( function( e )
	{
		if ( e.which == ESCAPE )
		{
			thisBox.abandonChanges = true;
			finishBox( attrs.id, attrs.url, attrs.idField, attrs.name, thisBox );
			return false;
		}
	
		return true;
	} );
	
	thisBox.keydown( function( e )
	{
		if ( e.keyCode == TAB )
		{
			errMsg( "key:" + e.keyCode + " == " + TAB + "(TAB) and shift == " + e.shiftKey, true );
			if ( e.shiftKey )
			{
				finishBox( attrs.id, attrs.url, attrs.idField, attrs.name, thisBox );
				indexOfBox = indexField( attrs.id ) - 1;
				if ( indexOfBox < 0  )
					indexOfBox = fieldArray.length-1;
				var thisNewBox = $( fieldArray[ indexOfBox ] );
				var thisNewBoxEdit = $( "#" + $( thisNewBox ).attr( "id" ) + "_edit" );
				errMsg( thisNewBoxEdit.val( ), true );
				if ( thisNewBoxEdit.val( ) != undefined )
				{
					errMsg( "already exists", true );
					thisNewBoxEdit.focus( );
				}
				else
				{
					newAttrs = { 
						 id:"#" + thisNewBox.attr( "id" ), 
						 url:thisNewBox.attr( "_url" ), 
						 idField:thisNewBox.attr( "_idField" ), 
						 name:thisNewBox.attr( "_name" ), 
						 elementInputType:thisNewBox.attr( "_elementInputType" ), 
						 elementType:thisNewBox.attr( "_elementType" ), 
						 elementWidth:thisNewBox.attr( "_elementWidth" ) 
					};
					if ( attrs.btnSave )
						newAttrs.btnSave = attrs.btnSave;
					edit( newAttrs );
					theNewBox = $( "#" + thisNewBox.attr( "id" ) + "_edit" ); 
					theNewBox.select( );
				}
			}
			else
			{
				finishBox( attrs.id, attrs.url, attrs.idField, attrs.name, thisBox );
				indexOfBox = indexField( attrs.id ) + 1;
				if ( indexOfBox >= fieldArray.length )
					indexOfBox = 0;
				var thisNewBox = $( fieldArray[ indexOfBox ] );
				var thisNewBoxEdit = $( "#" + $( thisNewBox ).attr( "id" ) + "_edit" );
				errMsg( thisNewBoxEdit.val( ), true );
				if ( thisNewBoxEdit.val( ) != undefined )
				{
					errMsg( "already exists", true );
					thisNewBoxEdit.focus( );
				}
				else
				{
					newAttrs = { 
						 id:"#" + thisNewBox.attr( "id" ), 
						 url:thisNewBox.attr( "_url" ), 
						 idField:thisNewBox.attr( "_idField" ), 
						 name:thisNewBox.attr( "_name" ), 
						 elementInputType:thisNewBox.attr( "_elementInputType" ), 
						 elementType:thisNewBox.attr( "_elementType" ), 
						 elementWidth:thisNewBox.attr( "_elementWidth" ) 
					};
					if ( attrs.btnSave )
						newAttrs.btnSave = attrs.btnSave;
					edit( newAttrs );
					theNewBox = $( "#" + thisNewBox.attr( "id" ) + "_edit" ); 
					theNewBox.select( );
				}
					//edit( "#" + thisNewBox.attr( "id" ), thisNewBox.attr( "_url" ), thisNewBox.attr( "_idField" ), thisNewBox.attr( "_name" ), thisNewBox.attr( "_elementInputType" ), thisNewBox.attr( "_elementType" ), thisNewBox.attr( "_elementWidth" ) );
			}
			return false;
		}
		else if ( e.keyCode == CHR_s || e.keyCode == CHR_S )
		{
			errMsg( "key:" + e.keyCode + " == S and ctrl == " + e.ctrlKey, true );
			if ( e.ctrlKey )
			{
				finishBox( attrs.id, attrs.url, attrs.idField, attrs.name, thisBox );
				return false;
			}
		}
	} );
	
	if ( attrs.blrSave )
	{
		thisBox.blur( function( )
		{
			finishBox( attrs.id, attrs.url, attrs.idField, attrs.name, thisBox );
		} );
	}
	
	if ( attrs.btnSave )
	{
		saveBtn.click( function( ) {
			finishBox( attrs.id, attrs.url, attrs.idField, attrs.name, thisBox );
		} );
	}
}

function editOld( id, url, idField, name, elementInputType, elementType, elementWidth )
{
	// if there is a call-back function to run before
	if ( $( id )[ 0 ]._beforeFunction ) if ( !$( id )[ 0 ]._beforeFunction( ) ) return false;

	$( id ).hide( );
	
	var outerTag = null;
	var input = null;

	try
	{
		outerTag = $( document.createElement( elementType ) );
		if ( elementType == "div" )
		{
			outerTag.addClass( "td" );
			outerTag.addClass( "borderRBWhite" );
		}
	} catch( err ) { 
		alert('Element of type '+elementType+' cannot be created');
		cleanUp( id, true );
		return;
	}
	
	outerTag.attr( "id", $( id ).attr( "id" ) + "_editor" );
	
	switch( elementInputType )
	{
		case "textarea":
			input = $( document.createElement( elementInputType ) );
			input.attr( { 
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":$( id ).html( ).replace( "Click Here To Edit", "" ).replace( /<!--br--><br>/gi, "\n" ).replace( /<!--br--><BR>/gi, "\n" )
			} );
			if ( !input.attr( "value" ) || Math.ceil( input.attr( "value" ).length / 200 ) == 0 || Math.ceil( input.attr( "value" ).length / 200 ) <= 4 )
				input.attr( { "rows":4 } );
			else if ( Math.ceil( input.attr( "value" ).length / 200 ) > 20 )
				input.attr( { "rows":20 } );
			else
				input.attr( { "rows":Math.ceil( input.attr( "value" ).length / 200 ) } );
			
			if ( elementWidth.indexOf( "%" ) >= 0 || elementWidth.indexOf( "px" ) )
				input.css( "width", elementWidth );
			else
				input.css( "width", elementWidth + "px" );
				/*, 
				"cols":elementWidth*/
			break;
		case "checkbox":
			input = $( document.createElement( "input" ) );
			input.attr( {
				"type":elementInputType, 
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":1 
			} );
			if ( $( id ).attr( "type" ) == "checkbox" )
				if ( $( id ).attr( "checked" ) ) input.attr( "checked", true );
			
			if ( $( id ).html( ) == "YES" ) input.attr( "checked", true );
			break;
		case "text":
			input = $( document.createElement( "input" ) );
			input.attr( {
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":$( id ).html( ).replace( "Click Here To Edit", "" ), 
				"autocomplete":"off"
			} );
			errMsg( "eleWid:" + typeof( elementWidth ), true );
			if ( typeof( elementWidth ) == "number" )
				input.css( "width", elementWidth + "px" );
			else if ( elementWidth.indexOf( "%" ) >= 0 || elementWidth.indexOf( "px" ) )
				input.css( "width", elementWidth );
			break;
		default:
			input = $( document.createElement( elementInputType ) );
			input.attr( {
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":$( id ).html( ).replace( "Click Here To Edit", "" ) 
			} );
			input.css( "width", elementWidth + "px" );
			break;
	}
	
	$( id ).after( outerTag );
	outerTag.append( input );
	
	errMsg( "#" + $( id ).attr( "id" ) + "_edit", true );
	var thisBox = $( "#" + $( id ).attr( "id" ) + "_edit" );

	thisBox.abandonChanges = false;
	
	thisBox.focus( );
	
	errMsg( $( id ).attr( "id" ).replace( "text_", "" ) , true );
	try
	{
		enableBtns( $( id ).attr( "id" ).replace( "text_", "" ) );
	}
	catch( e ) { /*does not exist*/ }
	
	thisBox.keyup( function( e )
	{
		if ( e.which == ESCAPE )
		{
			thisBox.abandonChanges = true;
			finishBox( id, url, idField, name, thisBox );
			return false;
		}
	
		return true;
	} );
	
	thisBox.keydown( function( e )
	{
		if ( e.keyCode == TAB )
		{
			errMsg( "key:" + e.keyCode + " == " + TAB + "(TAB) and shift == " + e.shiftKey, true );
			if ( e.shiftKey )
			{
				finishBox( id, url, idField, name, thisBox );
				indexOfBox = indexField( id ) - 1;
				if ( indexOfBox < 0  )
					indexOfBox = fieldArray.length-1;
				var thisNewBox = $( fieldArray[ indexOfBox ] );
				var thisNewBoxEdit = $( "#" + $( thisNewBox ).attr( "id" ) + "_edit" );
				errMsg( thisNewBoxEdit.val( ), true );
				if ( thisNewBoxEdit.val( ) != undefined )
				{
					errMsg( "already exists", true );
					thisNewBoxEdit.focus( );
				}
				else
				{
					edit( "#" + thisNewBox.attr( "id" ), thisNewBox.attr( "_url" ), thisNewBox.attr( "_idField" ), thisNewBox.attr( "_name" ), thisNewBox.attr( "_elementInputType" ), thisNewBox.attr( "_elementType" ), thisNewBox.attr( "_elementWidth" ) );
				}
			}
			else
			{
				finishBox( id, url, idField, name, thisBox );
				indexOfBox = indexField( id ) + 1;
				if ( indexOfBox >= fieldArray.length )
					indexOfBox = 0;
				var thisNewBox = $( fieldArray[ indexOfBox ] );
				var thisNewBoxEdit = $( "#" + $( thisNewBox ).attr( "id" ) + "_edit" );
				errMsg( thisNewBoxEdit.val( ), true );
				if ( thisNewBoxEdit.val( ) != undefined )
				{
					errMsg( "already exists", true );
					thisNewBoxEdit.focus( );
				}
				else
					edit( "#" + thisNewBox.attr( "id" ), thisNewBox.attr( "_url" ), thisNewBox.attr( "_idField" ), thisNewBox.attr( "_name" ), thisNewBox.attr( "_elementInputType" ), thisNewBox.attr( "_elementType" ), thisNewBox.attr( "_elementWidth" ) );
			}
			return false;
		}
		else if ( e.keyCode == CHR_s || e.keyCode == CHR_S )
		{
			errMsg( "key:" + e.keyCode + " == S and ctrl == " + e.ctrlKey, true );
			if ( e.ctrlKey )
			{
				finishBox( id, url, idField, name, thisBox );
				return false;
			}
		}
	} );
	
	thisBox.blur( function( e )
	{
		
	} );
}

function finishBox( id, url, idField, name, thisBox )
{
	errMsg( id + " " + url + " " + idField + " " + name, true );
	if ( thisBox.abandonChanges )
	{
		errMsg( "cleaning...", true );
		cleanUp( id, true );
	}
	else
	{
		errMsg( "trying to save...", true );
		try
		{
			errMsg( "saving...", true );
			saveChanges( id, url, idField, name );
		}
		catch( e )
		{
			errMsg( e, true );
		}
	}
	try
	{
		disableBtns( $( id ).attr( "id" ).replace( "text_", "" ) );
	}
	catch( e ) { /*does not exist*/ }
	return true;
}

function newField( id, url, name, elementInputType, elementType, elementWidth )
{
	// if there is a call-back function to run before
	if ( $( id )[ 0 ]._beforeFunction ) if ( !$( id )[ 0 ]._beforeFunction( ) ) return false;
	
	$( id ).hide( );
	
	var outerTag = null;
	var input = null;
	
	try
	{
		outerTag = $( document.createElement( elementType ) );
		if ( elementType == "div" )
		{
			outerTag.addClass( "td" );
			outerTag.addClass( "borderRBWhite" );
		}
	} catch( err ) { 
		alert('Element of type '+elementType+' cannot be created');
		cleanUp( id, true );
		return;
	}

	outerTag.attr( "id", $( id ).attr( "id" ) + '_editor' );
	
	switch( elementInputType )
	{
		case "textarea":
			input = $( document.createElement( elementInputType ) );
			input.attr( { 
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":$( id ).html( ), 
				"rows":4, 
				"cols":elementWidth
			} );
			break;
		case "checkbox":
			input = $( document.createElement( "input" ) );
			input.attr( {
				"type":elementInputType, 
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":1 
			} );
			if ( $( id ).attr( "type" ) == "checkbox" )
				if ( $( id ).attr( "checked" ) ) input.attr( "checked", true );
			
			if ( $( id ).html( ) == "YES" ) input.attr( "checked", true );
			break;
		case "text":
			input = $( document.createElement( "input" ) );
			input.attr( {
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":$( id ).html( ), 
				"autocomplete":"off" 
			} );
			input.css( "width", elementWidth + "px" );
			break;
		default:
			input = $( document.createElement( elementInputType ) );
			input.attr( {
				"id":$( id ).attr( "id" ) + "_edit", 
				"name":$( id ).attr( "id" ), 
				"value":$( id ).html( ) 
			} );
			input.css( "width", elementWidth + "px" );
			break;
	}
	
	$( id ).after( outerTag );
	outerTag.append( input );
	
	var thisBox = $( "#" + $( id ).attr( "id" ) + "_edit" );
	thisBox.abandonChanges = false;
	
	thisBox.focus( );
	
	thisBox.keyup( function( e )
	{
		if ( e.which == ESCAPE )
		{
			thisBox.abandonChanges = true;
			thisBox.blur( );
			return false;
		}
		else if ( e.which == CR )
		{
		  this.blur( );
		  return false;
		}
		
		return true;
	} );
	
	thisBox.blur( function( e )
	{
		if ( !thisBox.abandonChanges )
		{
			//saveChanges( id, url, idField, name );
		}
		else
		{
			cleanUp( id, true );
			thisBox.abandonChanges = false;
		}
		return true;
	} );
}

function indexField( id )
{
	for ( i = 0; i < fieldArray.length; i++ )
	{
		if ( id == fieldArray[ i ] ) return i;
	}
	
	return null;
}

function cleanUp( id, keepEditable )
{
	$( id + "_editor" ).remove( );
	$( id ).show( );
	if ( !keepEditable ) showAsEditable( id, true );

	errMsg( "trying after function for:" + id, true );
	
	// if there is a call-back function to run before
	errMsg( "type of " + typeof( $( id )[ 0 ]._afterFunction ), true );
	if ( $( id )[ 0 ]._afterFunction ) if ( !$( id )[ 0 ]._afterFunction( ) ) return true;
}

function saveChanges( id, url, idField, name )
{
	var blah = $( "#" + $( id ).attr( "id" ) + '_edit' );

	if ( blah.val( ) == "Saving..." || $( id ).html( ) == "Saving..." )
	{
		cleanUp( id, true );
		return false;
	}
	
	errMsg( "before:"+blah.val( ), true );
	errMsg( "after:"+_escHTML(blah.val( )), true );
	errMsg( "encoded:"+encodeURI(_escHTML(_escAND($.trim( blah.val( ) )))), true );

	if ( blah.attr( "type" ) == "checkbox" )
	{
		if ( blah.attr( "checked" ) == true )
		{
			var new_content = 1;
		}
		else
		{
			var new_content = 0;
		}
		blah.blur();
	}
	else
		var new_content = encodeURI(_escHTML(_escAND($.trim( blah.val( ) ))));
	 
	$( id ).html( "Saving..." );
	var thisFieldID = $( id ).attr( "id" ).split( "_" )[ 1 ];
	if ( thisFieldID == undefined )
	{
		errMsg( thisFieldID, true );
		return;
	}
	//errMsg( thisFieldID, true );
	
	var pars = idField + '=' + thisFieldID + '&action=update' + '&' + name + '=' + new_content;

	errMsg( pars, true );
	$.ajax( {
		type: "POST",
		url: url,
		data: pars,
		success: function( t )
		{
			try
			{
				errMsg( "success " + t + id, true  );
				$( id ).html( t );
				cleanUp( id, true );
			}
			catch( e )
			{
				errMsg( "thrown on success", true );
				errMsg( e, true );
			}
		},
		error: function( t, status, theErr )
		{
			try
			{
				errMsg( "failure " + t.responseText, true  );
			}
			catch( e )
			{
				errMsg( "thrown on failure", true );
				errMsg( e, true );
			}
			cleanUp(id);
		}
	} );
	errMsg( "done", true  );
}

function saveSpecial( attrs )
{
	errMsg( "into saveSpecial", true );
	try
	{
		// if there is a call-back function to run before
		if ( $( attrs.id )[ 0 ]._beforeFunction ) if ( !$( attrs.id )[ 0 ]._beforeFunction( ) ) return false;
	}
	catch( e )
	{
	}
	
	var blah = $( attrs.id );
	
	errMsg( "before:"+blah.val( ), true );
	errMsg( "after:"+_escHTML(blah.val( )), true );
	errMsg( "encoded:"+encodeURI(_escHTML(_escAND($.trim( blah.val( ) )))), true );
	
	if ( attrs.elementInputType == "checkbox" || attrs.elementInputType == "radio" )
	{
		if ( blah.attr( "checked" ) == true )
		{
			var new_content = 1;
		}
		else
		{
			var new_content = 0;
		}
		blah.blur();
	}
	else
		var new_content = encodeURI(_escHTML(_escAND($.trim( blah.val( ) ))));
	
	var thisFieldID = attrs.id.split( "_" )[ 1 ];
	if ( thisFieldID == undefined )
	{
		errMsg( thisFieldID, true );
		return;
	}
	//errMsg( thisFieldID, true );
	
	var pars = attrs.idField + '=' + thisFieldID + '&action=update' + '&' + attrs.name + '=' + new_content;

	errMsg( pars, true );
	$.ajax( {
		type: "POST",
		url: attrs.url,
		data: pars,
		success: function( t )
		{
			try
			{
				errMsg( "success " + t + attrs.id, true  );
				//$( id ).html( t );
				//cleanUp( id, true );
			}
			catch( e )
			{
				errMsg( "thrown on success", true );
				errMsg( e, true );
			}
		},
		error: function( t, status, theErr )
		{
			try
			{
				errMsg( "failure " + t.responseText, true  );
			}
			catch( e )
			{
				errMsg( "thrown on failure", true );
				errMsg( e, true );
			}
			//cleanUp(id);
		}
	} );
	errMsg( "done", true  );

	try
	{
		if ( $( attrs.id )[ 0 ]._afterFunction ) if ( !$( attrs.id )[ 0 ]._afterFunction( ) ) return true;
	}
	catch( e )
	{
	}
}
