function init_viewmode(evt)
{
    document.switches = [];
    document.viewmode = 'view';

    elts = document.getElementsByTagName('*');
    for(i=0; i<elts.length; i++) {
	if( HasClass(elts[i], 'editswitch') ) {
	    document.switches.push(elts[i]);
	    sp = document.createElement('span');
	    sp.appendChild(document.createTextNode('View'));
	    elts[i].appendChild(sp);
	    AttachEvent(sp, 'click', switch_viewmode, false);
	    sp2 = document.createElement('span');
	    sp2.appendChild(document.createTextNode(' | '));
	    elts[i].appendChild(sp2);
	    sp3 = document.createElement('span');
	    sp3.appendChild(document.createTextNode('Edit'));
	    sp3.className='notpikd';
	    elts[i].appendChild(sp3);
	    AttachEvent(sp3, 'click', switch_editmode, false);
	} // if it's the edit switch
    } // for all the switches

    viewmode_fn();
} // function init_viewmode

function viewmode_fn(evt)
{
    document.harboring_editmodes = [];
    document.viewmode='view';
    var zaplist = [];

    elts = document.getElementsByTagName('*');
    for(i=0; i<elts.length; i++) {
	if( HasClass(elts[i], 'viewmode') ) {
	    desc = elts[i].getElementsByTagName('*');
	    for(j=0; j<desc.length; j++ ) { 
		if( (desc[j].tagName=='INPUT'&&desc[j].type=='text') ||
		    desc[j].tagName=='TEXTAREA' || desc[j].tagName=='SELECT' ||
		    HasClass(desc[j], 'input')  )
		{ 
		    var doit = true;
		    for(k=0; k<zaplist.length; k++) {
			if( HasParent(desc[j], zaplist[k]) ) { doit = false; }
		    } // for all zaplisted items
		    if( doit ) { zaplist.push(desc[j]); }
		} // if we should push it
	    } // for all descendents
	} // if it's a viewmode
    } // for all the elts

    // Now we have the zaplist
    for(i=0; i<zaplist.length; i++) {
	viewmode_zap(zaplist[i]);
    } // for all the zaplisted items
} //. function viewmode
AttachEvent(window, 'load', init_viewmode, false);

function viewmode_zap(elt)
{
    sp = document.createElement('span');
    doit=false;
    switch(elt.tagName) {
    case 'INPUT':
	if( elt.type=='text' ) {
	    tx = elt.value;
	    doit=true;
	} 
	break;
    case 'TEXTAREA':
	if( elt.firstChild ) { tx = elt.firstChild.nodeValue; }
	else { tx = ''; }
	doit=true;
	break;
    case 'SELECT':
	tx = elt.options[elt.selectedIndex].text;
	doit=true;
	break;
    default:
	if( elt.getValue ) {
	    tx = elt.getValue();
	    doit=true;
	} // if there's a way to get the value
    } // switch tagName

    if( doit ) {
	document.harboring_editmodes.push(sp);
	sp.appendChild(document.createTextNode(tx));
	sp.hidden_child = elt;
	elt.parentNode.insertBefore(sp, elt );
	elt.parentNode.removeChild(elt);
    } // if doit
} // function viewmode_label

function editmode_fn(evt)
{
    document.viewmode = 'edit';
    for(i=0; i<document.harboring_editmodes.length; i++) {
	elt = document.harboring_editmodes[i];
	elt.parentNode.insertBefore(elt.hidden_child, elt);
	elt.parentNode.removeChild(elt);
    } // for all the hidden stuff
} // function editmode

function switch_viewmode(evt)
{
    if( document.viewmode == 'view' ) { return; }
    for(i=0; i<document.switches.length; i++) {
	KillClass(document.switches[i].childNodes[0], 'notpikd');
	AddClass(document.switches[i].childNodes[2], 'notpikd');
    } // for all the switches

    viewmode_fn();
} // function switch_viewmode

function switch_editmode(evt)
{
    if( document.viewmode == 'edit' ) { return; }
    for(i=0; i<document.switches.length; i++) {
	KillClass(document.switches[i].childNodes[2], 'notpikd');
	AddClass(document.switches[i].childNodes[0], 'notpikd');
    } // for all the switches

    editmode_fn();
} // function switch_viewmode

