/* a function to check for a particular class */
function elementHasClass(elem, n) {
    var c = elem.className.split(" ");
    // alert("has? " + elem + ", " + n);
    for (var i = 0; i < c.length; i++)
	if (c[i] == n)
	    return 1;
    return 0;
}

/* switch to edit mode... */
function editMode() {
    var e = document.body.getElementsByTagName('span');
    var s = '';
    // I'm not going to rely on editHandlers to make all text and pictures editable any more.
    for (var i=0; i<e.length; i++)
	if (elementHasClass(e.item(i), "editHandler") ||
	    elementHasClass(e.item(i), "editHandler"))
	    s = s + e.item(i).firstChild.id + ';';
	    //	if (e.item(i).className == 'editHandler' ||
	    //	    e.item(i).className == 'saveEditHandler')

    rcall('cgi-bin/editMode.pl',s);
}

/* switch out of edit mode... */
function normalMode() {
    var e = document.body.getElementsByTagName('span');
    var s = '';
    for (var i=0; i<e.length; i++)
	if (elementHasClass(e.item(i), 'editHandler') ||
	    elementHasClass(e.item(i), 'saveEditHandler'))
	    s = s + e.item(i).firstChild.id + ';';

    rcall('cgi-bin/normalMode.pl',s);
}

// I have changed this a bit now since I've changed the way things are saved.
function savePage() {
    var e = document.body.getElementsByTagName('span');
    var s = '';
    var s2 = '';

    // display the 'saving' thing
    document.getElementById('appArea').innerHTML+='<div id="saveNotify" style="text-align:center;"><div style="background-color: black; opacity:0.5;position:fixed;top:0;left:0;height:100%;width:100%;text-align:center;">&nbsp;</div><div style="position:fixed; top:50%;width:100%;text-align:center;background-color: white; border: solid black 1px;"><h1>Saving. One moment please.</h1></h1></div>';

    for (var i=0; i<e.length; i++) {
	if (elementHasClass(e.item(i), 'saveHandler') ||
	    elementHasClass(e.item(i), 'saveEditHandler')) {
	    // I'm going to clump these all together with nulls. That'll be much gooderer and make it easy for perl to parse. Trivially so.
	    s = s + e.item(i).firstChild.id + String.fromCharCode(1) + e.item(i).innerHTML + String.fromCharCode(1);
	    // rcall('cgi-bin/savePage.pl?' + e.item(i).firstChild.id, e.item(i).innerHTML);
	    // We could do with a notification. We'll have to make a
	    // list of elements that need saving and have the save.pl
	    // script tell us when each is done. that wouldn't be too
	    // hard.
	}
	// This is for the new way of saving...
	// basically we send a stream of (id, method, data) to the server. This is how we'll do other changes too.
	if (elementHasClass(e.item(i), 'save')) {
	    var h = e.item(i).innerHTML;
	    // strip out any 1s if there are any...
	    s2 += e.item(i).id + String.fromCharCode(1) + e.item(i).className + String.fromCharCode(1) + h + String.fromCharCode(1);
	}
    }
    // alert(s.indexOf(String.fromCharCode(0)));
    // now send the whole job lot
    if (s != '') 
	rcall('cgi-bin/savePage.pl', s);

    if (s2 != '') 
	rcall('cgi-bin/update.pl', s2);
}

