var loaded = 0;
var fancyposts = preferences.fancypost.value;
var socketserver = preferences.socketserver.value;
var appkey = preferences.appkey.value;
var blogid = preferences.blogid.value;
var blogus = preferences.uname.value;
var blogpw = preferences.pword.value;
var blogtype = preferences.blogtype.value;

var bloghost = preferences.bloghost.value;
var blogurl = preferences.blogurl.value;
blogurl = escape(blogurl);
var blogserverip = preferences.blogserverip.value;
var blogserverport = preferences.blogserverport.value;
var title = "NULL";
var linkurl = preferences.linkurl.value;
linkurl = escape(linkurl);
var author = preferences.author.value;
author = escape(author);

var ssObj = new SocketServer();
ssObj.processQueue();

if ((appkey == "") || (blogid == "") || (blogus == "") || (blogpw == ""))
{
    if (appkey == "")
    {
	alert(ssObj.getErrortext("-4"));
    }
    else if (blogid == "")
    {
	alert(ssObj.getErrortext("-5"));
    }
    else if (blogus == "")
    {
	alert(ssObj.getErrortext("-6"));
    }
    else if (blogpw == "")
    {
	alert(ssObj.getErrortext("-7"));
    }
    else
    {
	alert(ssObj.getErrortext("-13"));
    }
}
else
{

if (fancyposts < 1)
{
    // first, prepare and display the form:

    var formfields = new Array();

    formfields[0] = new FormField();
    formfields[0].name = "blogmsg";
    formfields[0].type = "text";
    formfields[0].title = "Your Blog Message:";
    formfields[0].description = "Type the message you would like posted to your blog in the textbox provided.  I apologize for the fact that all you get is this one line.  But check it out, you can type a *whole* lot into this one line! :) \n\nLobby the makers of Konfabulator: \n\n              http://www.konfabulator.org\n\nfor a proper form textarea!  For now, feel free to compose elsewhere then paste here if you like.";

    if (blogtype == "metaweblog")
    {
        formfields[1] = new FormField();
        formfields[1].name = "title";
        formfields[1].type = "text";
        formfields[1].title = "The title of this post:";
        formfields[1].description = "Enter the title of this post here.";

        formfields[2] = new FormField();
        formfields[2].name = "link";
        formfields[2].type = "text";
        formfields[2].title = "The link URL of this post:";
        formfields[2].description = "Enter the link URL of this post here.";
        formfields[2].defaultValue = preferences.linkurl.value;

        formfields[3] = new FormField();
        formfields[3].name = "author";
        formfields[3].type = "text";
        formfields[3].title = "The author of this post:";
        formfields[3].description = "Enter the author of this post here.";
        formfields[3].defaultValue = preferences.author.value;
    }

    var results = new Array();
    results = form(formfields, 'BlogPost', 'Post to your blog', 'Cancel');

    if (results == null)
    {
        // do nothing
    }
    else 
    {
        var message = "";
        message = results[0];
        message = escape(message);
        var title = "NULL";
        var link = "NULL";
        var author = "NULL";

        if (blogtype == "metaweblog")
        {
            title = results[1];
            title = escape(title);
            link = results[2];
            link = escape(link);
            author = results[3];
            author = escape(author);
        }

	if (socketserver < 1)
	{
            // appkey, blogid, user, pass, title, link, author, message, blogtype, bloghost, blogurl, blogserverip, blogserverport
            var stringCmd = "Resources/BlogPost/Perls/guestpost.pl appkey=" + appkey + " blogid=" + blogid + " blogus=" + blogus + " blogpw=" + blogpw + " title=" + title + " link=" + link + " author=" + author + " message='" + message + "' blogtype=" + blogtype + " bloghost=" + bloghost + " blogurl=" + blogurl + " blogserverip=" + blogserverip + " blogserverport=" + blogserverport;
            //alert(stringCmd);
            var kresult = runCommand(stringCmd);
            if ((kresult == "1") || (kresult == "1\n"))
            {
		alert("Success -- your entry was posted!");
            }
            else if ((kresult == "2") || (kresult == "2\n"))
            {
	         alert("You are not connected to the internet, or a connection failed.  Your Blog entry was queued.  Next time the SocketServer restarts, if it can establish a connection, your entry will be re-sent.  Otherwise it will continue to be queued.");
            }
	    else
	    {
                 //alert(kresult);
		 try {
 		     alert("Error: " + ssObj.getErrortext(kresult));
                 } catch (e) {
		     alert(e);
	 	 }
            }
	}
	else
	{
	    var result = ssObj.sendBlog(appkey, blogid, blogus, blogpw, title, link, author, message, blogtype, bloghost, blogurl, blogserverip, blogserverport);
	    if ((result == "1") || (result == "1\n"))
	    {
		alert("Success -- your entry was posted!");
	    }
	    else if ((result == "2") || (result == "2\n"))
	    {
	         alert("You are not connected to the internet, or a connection failed.  Your Blog entry was queued.  Next time the SocketServer restarts, if it can establish a connection, your entry will be re-sent.  Otherwise it will continue to be queued.");
	    }
	    else
            {
		alert("Error: " + ssObj.getErrortext(result));
            }
        }
    }
} // if fancyposts < 1
else
{
    // fancy posts:
    var secretpath = resolvePath("~/Documents/Widgets/BlogPost.widget/Contents/Resources/BlogPost/HTML/blogpost.html");
    var browserStr = preferences.browserpref.value; 
    // appkey, blogid, user, pass, title, link, author, message, blogtype, bloghost, blogurl, blogserverip, blogserverport
    var url = "file://" + secretpath + "?appkey=" + appkey + "&blogid=" + blogid + "&blogus=" + blogus + "&blogpw=" + blogpw + "&sockservip=127.0.0.1" + "&sockservport=15432&link=" + linkurl + "&author=" + author + "&blogtype=" + blogtype + "&bloghost=" + bloghost + "&blogurl=" + blogurl + "&blogserverip=" + blogserverip + "&blogserverport=" + blogserverport; 
    var ureturn = 0;
    //alert(url);
    //alert(browserStr);
    if (browserStr == "Safari")
    {
        ureturn = appleScript('tell application "' + browserStr + '"\n    set the URL of document 1 to "' + url + '"\nend tell\n');
    }
    else if ((browserStr == "Mozilla") || (browserStr == "Camino"))
    {
	// there is a bug in konfabulator that causes this to not work in Camino and Mozilla (running this AppleScript
	// from AppleScript Studio works just fine)
	//var opStr = 'tell application "' + browserStr + '"\n\topen url "' + url + '"\nend tell\n';
	//alert(opStr);
        //ureturn = appleScript(opStr);	
	alert("Unfortunately, Mozilla and Camino do not work correctly with Konfabulator's AppleScript implementation.  I verified this by using a Camino AppleScript in AppleScript Studio to do what I wanted, and it worked; but then when I used it in Konfabulator, it did not.  As soon as this can be fixed, either by an AppleScript guru who knows how to work around this problem, or by Konfabulator, these browsers will be usable here.  Otherwise, you have to use Internet Explorer or Safari.  Please reset this preference as soon as you can.");
    }
    else if (browserStr == "Internet Explorer")
    {
        ureturn = appleScript('tell application "' + browserStr + '"\n    OpenURL ("' + url + '")\nend tell\n');
    }
    else
    {
	alert("You selected a browser that is not set up to function in this Widget.  If you want to add this browser to BlogPost's list of supported browsers, send an email to 0@zeroaltitude.org specifying the browser's name and version, and an AppleScript that will open the browser to a particular webpage.  Please set your browser preference to Internet Explorer or Safari.");
    }
    //alert(ureturn);
}

} // else we have all the data
