var blnAgree   = true;
var blnRights  = true;

var FL_SILENT = 1;
var FL_DEBUG = 2;
var FL_NOUPSIZE = 4;
var FL_NOCLEANUPTMP = 8;

function PhotoUpload(sitegroup, host, imghost, photohost) {
    this.sitegroup = sitegroup;
    this.host = host;
    this.imghost = imghost;
    this.photohost = photohost;
    this.user_callback = null;
    this.applet = null;
    this.use_applet = false;
}
PhotoUpload.prototype.escape = function(strIn) {
    return escape(strIn).replace(/\+/, "%2b");
}
PhotoUpload.prototype.upload = function(intMW, intMH) {
    if (!intMW) { intMW = 400; }
    if (!intMH) { intMH = 320; }
    var loadType = "addaphoto";
    var intQ = 70;
    var plugin = null;
    try {
        plugin = new ActiveXObject("Crusher.Plugin");
        if (!plugin) {
            //alert("Null plugin");
            throw "";
        }
    } catch (error) {
        if (typeof(ActiveXObject) != "undefined") {
            alert("Please install the plugin");
            return;
        } else {
            if (navigator.javaEnabled()) {
                this.use_applet = true;
                this.createApplet();
            } else {
                alert("Please enable java in your browser");
                return;
            }
        }
    }

    if (blnAgree == true && blnRights == false) { 
        alert("You must own the rights to the photo and agree to our terms of service before uploading photos.");
        return;
    }

    document.body.style.cursor = "waiting";
    var form = this.parseForm();
    document.body.style.cursor = "";
    if (form) {
        try {
            var formdata = form.join("|\n");
            var url = "/cgi-bin/flashalbum/kiwaddaphoto.pl";
            if (this.use_applet) {
                var applet_params = {"host": this.host, "formdata": formdata, "iwidth": intMW, "iheight": intMH, "url": url};
                this.applet.create(applet_params);
            } else {
                url = this.host + url;
                var retvalue = plugin.UploadImages(url, formdata, intMW, intMH, intQ, FL_SILENT);
                retvalue = retvalue.replace(/<BODY>/i, "");
                retvalue = retvalue.replace(/<\/BODY>/i, "");
                if ((retvalue.indexOf("Error") == 0) || (!retvalue)) {
                    alert("Sorry, an error has occurred while preparing your photos to upload, please try again.  Thanks!");
                } else {
                    this.finalizeUpload(retvalue);
                }
            }
        } catch(error) {
          alert("Sorry, an error has occurred while updating your photos, please try again.  Thanks!");
        }
    }
}
PhotoUpload.prototype.createApplet = function() {
    this.applet = new CrusherApplet(this.imghost + "/applets/Crusher.jar");
    var self = this;
    this.applet.finalize_func = function(val) { return self.finalizeUpload(val); };
    this.applet.imghost = this.imghost;
    this.applet.ahost = this.host;
    this.applet.sg = this.sitegroup;
    this.applet.loadType = "addaphoto";
}
PhotoUpload.prototype.parseForm = function() {
    var form = [];
    var filename, fileext;
    var original_names = document.photoForm.OrigFiles.value.split("|");
    for (var i=0; i < original_names.length; i++) {
        original_names[i] = this.escape(original_names[i]);
    }

    var filecount = -1;
    var filename_re = /^.*(\\|\/)(.*)/;

    for (var i = 0; i < document.photoForm.elements.length; i++) {
        var e = document.photoForm.elements[i];
        if (e.type == "file") {
            filecount++;
            filename = e.value;
            if (filename != "") {
                fileext = filename.substring(filename.length - 4).toLowerCase();
                if ((fileext == ".jpg") || (fileext == ".gif") || (fileext == ".bmp") || (fileext == "jpeg")) {
                    try {
                        original_names[filecount] = this.escape(filename.match(filename_re)[2]);
                    } catch(err) {
                        alert("Please use the browse button to select your photo");
                        return;
                    }
                    form.push(e.name + ":~" + e.value);
                } else {
                    alert("You can only upload jpg/gif/bmp files, please try again");
                    return;
                }
            } else {
                return; // end filename != ""
            }
        } else if (e.name != "OrigFiles") {
            form.push(e.name + ":" + this.escape(e.value));
        }
    }
    document.photoForm.OrigFiles.value = original_names.join("|");
    form.push("OrigFiles:" + this.escape(original_names.join("|")));
    
    return form;
}
PhotoUpload.prototype.finalizeUpload = function(retvalue) {
    var PhotoData = window.eval(String(retvalue));
    if (this.user_callback) {
        this.user_callback(PhotoData);
    }
}


/*
** The Crusher Applet
*/
function CrusherApplet(applet_url)
{
    this.applet_url = applet_url;
    this.return_value = "";
    this.imghost = "";
    this.ahost = "";
    this.sg = "";
    this.loadType = "";
    this.finalize_func = null;
}
CrusherApplet.prototype.create = function(params)
{
    document.getElementsByTagName("body").item(0).style.cursor = "wait";
    param_string = "";
    for (var name in params) {
        var value = params[name];
        param_string += this._create_param(name, value);
        if (name == "this") {
            // this one is special/skipped
        } else if (name == "imghost") {
            this.imghost = value;
        } else if (name == "host") {
            this.ahost = value;
        } else if (name == "sg") {
            this.sg = value;
        }
    }
    param_string += this._create_param("js_callback", this._create_callback(this, params["this"]));
    param_string += this._create_param("debug", "true");
    applet_tag = '<applet name="Kiwee Photo Upload" code="com.ag.applets.photoupload.Crusher.class" ';
    applet_tag += 'archive="' + this.applet_url + '" width="0" height="0" mayscript="true">';
    applet_tag += param_string;
    applet_tag += "</applet>";
    var div = document.createElement("div");
    document.body.appendChild(div);
    div.innerHTML = applet_tag;
}
CrusherApplet.prototype._create_callback = function(this_, eval_this) {
    var function_name = "";
    var alphabet = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    for (var i=0; i < 15; i++) {
        function_name += alphabet.charAt(Math.floor(Math.random() * alphabet.length));
    }
    window[function_name] = function(s) {
        document.getElementsByTagName("body").item(0).style.cursor = "";
        this_.return_value = String(s);
        if (eval_this) {
            func = eval_this["getAppletCallback"](eval_this);
        } else {
            func = this_["getCallback"](this_);
        }
        setTimeout(func, 1500);
    }
    return function_name;
}
CrusherApplet.prototype.getCallback = function(this_)
{
    return function() {
        this_.finalize_func(this_.return_value);
    }
}
CrusherApplet.prototype._create_param = function(name, value)
{
    return '<param name="' + name + '" value="' + value + '"/>';
}

/* <crusherapplet> */
