function confirmSubmit()
{
var agree=confirm("Are you sure you wish to continue?");
if (agree)
	return true ;
else
	return false ;
}
function confirmDelete()
{
var agree=confirm("Are you sure you wish to delete this?");
if (agree)
	return true ;
else
	return false ;
}

var http = createRequestObject();

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

function handleSaveResponse() {
    if(http.readyState == 4){
    var response = http.responseText;
    var update = new Array();
        if(response.indexOf('|' != -1)) {
            //name the div layer after the process call
            //alert("Response is:"+response);
        	if (response == "Grood!")
        	{
	        	document.getElementById('indicator').innerHTML = "";
        	}
        	else if (response == "Removed!")
        	{
	        	document.getElementById('indicator').innerHTML = "Autosaved post removed!";
	        	document.getElementById('autosaveFound').innerHTML = "";
        	}
        	else
        	{
	        	document.getElementById('indicator').innerHTML = "!"+response;
        	}
        waiting = false;

        }
    }
}
var started = false;
function init_autoSave()
{
	if (!started)
	{
		setTimeout("autoSave()", 30000);
		started = true;
	}
}

function autoSave()
{
	subject = document.getElementById('subject').value;
	author = document.getElementById('author').value;
	content = SPAW_getHtmlValue('content', null);
	document.getElementById('indicator').innerHTML = "*";
	//There's a problem with pound signs - '#'		
	http.open('get', '/site_admin/autosave.php?subject='+subject+'&author='+author+'&content='+content.replace(/#/g, "22pound22"));
    http.onreadystatechange = handleSaveResponse;
    http.send(null);
	setTimeout("autoSave()", 20000);
}

function removeAutoSave()
{
	var agree=confirm("Are you sure you wish to continue? You cannot undo this action!");
if (agree)
	{
		http.open('get', '/site_admin/autosave.php?action=removeAutoSave');
    	http.onreadystatechange = handleSaveResponse;
    	http.send(null);
	}
}

function resumePost(author, subject)
{
	//fetch the post VIA ajax and fill it in
	document.getElementById('subject').value = subject;
	document.getElementById('author').value = author;

	http.open('get', '/site_admin/autosave.php?action=resumePost');
	http.onreadystatechange = handleGetResponse;
    http.send(null);
	//you may have to reload the page
}

function handleGetResponse() {
    if(http.readyState == 4){
    var response = http.responseText;
    var update = new Array();
        if(response.indexOf('|' != -1))
        {
            update = response.split('||!||');
        	document.getElementById(update[0]).value = update[1];
        	waiting = false;
        	SPAW_getHtmlValue(update[0], null);
        	var browser = navigator.appName;
        	var theHTML = update[1].replace(/22pound22/g, "#");
        	
			if(browser == "Microsoft Internet Explorer")
				window.frames[update[0]+'_rEdit'].document.body.innerHTML = theHTML.replace(/\\\"/g, "\"");
			else
			{
				var ed = document.getElementById(update[0]+'_rEdit');
				ed.contentDocument.body.innerHTML = theHTML.replace(/\\\"/g, "\"");
			}
        	document.getElementById('autosaveFound').innerHTML = "";
        	init_autoSave();
        }
    }
}

function loadingGif(domainName)
{
	var loadingAni = document.getElementById("loadingStatus");
	loadingAni.innerHTML = "<strong>Uploading ... </strong><br /><img src='http://www."+domainName+"/images/load_bar.gif' />";
	return true;
}

function popUp(URL, imgWidth, imgHeight) {
imgWidth = imgWidth + 25;
imgHeight = imgHeight + 25;
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=' + imgWidth + ',height=' + imgHeight + ',left = 50,top = 50');");
}

function insertToBlog(picID, fullPicLocation, picWidth, picHeight)
{	
	caption = document.getElementById("caption"+picID).value;
	justify = document.getElementById("justify"+picID).value;
	filename = document.getElementById("imagename"+picID).value;	
	append = "<table width=\"150\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" align=\""+justify+"\" style=\"border:3px #000000 solid\"><tr><td bgcolor=\"#FFFFFF\"><a href=\"#\" onclick=\"popUp('"+fullPicLocation+"', "+picWidth+", "+picHeight+");\"><img src=\""+fullPicLocation+"\" width=\"150\" border=\"0\" alt=\""+filename+"\" /></a></td></tr><tr><td bgcolor=\"#FFFFFF\"><span class=\"caption\">"+caption+"</span></td></tr></table>"
	parent.SPAW_getHtmlValue('content', null);
	var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer")
	{
		oldContent = parent.window.frames['content_rEdit'].document.body.innerHTML;
		parent.window.frames['content_rEdit'].document.body.innerHTML = oldContent+append;
	}
	else
	{
		var ed = parent.document.getElementById('content_rEdit');
		oldContent = ed.contentDocument.body.innerHTML;
		ed.contentDocument.body.innerHTML = oldContent+append;
	}
}


function insertToBlogFromArchive(filename)
{	
	http.open('get', '/site_admin/touch.php?filename='+filename);
    http.onreadystatechange = handleSaveResponse;
}

