// Define that which I'd normally pull from prototype
    function $(id) {
        if (typeof id != 'string') return id;
        if (document.getElementById)
            return document.getElementById(id);
        if (document.all)
            return document.all[id];
        return null;
    }

// Add a css class to a specified element
    function add_class(id, classname) {
        var field = $(id);
    // No field
        if (!field)
            return;
    // Field already has this class, don't bother to add it again
        if (field.className && (new RegExp('\\b'+classname+'\\b')).test(field.className))
            return;
    // Add the class
        if (field.className)
            field.className = field.className + ' ' + classname;
        else
            field.className = classname;
    }

// Remove a css class from a particular element
    function remove_class(id, classname) {
        var field = $(id);
        if (!field)
            return;
        field.className = field.className.replace(RegExp('\\b'+classname+'\\s*\\b|\\b\\s*'+classname+'\\b', 'g'), '') ;
    }

// Global variables to track the upload progress
    var bytes           = 0;
    var speed           = 0;
    var total           = 0;
    var tries           = 0;
    var upload_id       = '';

    function do_upload(id) {
        upload_id = id;
		var ok = upload_validate();
		if (!ok) {
			event.returnValue=false;
			return false;
		}
        $('upload_form').submit();
        // We can't use display:none or Safari will cancel the upload!
        add_class('upload_form', '_offscreen');
        $('upload_progress').style.display = 'block';
        $('darkBackgroundLayer').style.display = 'block';
        setTimeout(do_upload_progress, 2000);
        return true;
    }

    function do_upload_complete() {
        $('upload_progress').style.display = 'none';
        $('darkBackgroundLayer').style.display = 'none';
        $('upload_post').style.display     = 'block';
    }

    function do_upload_error() {
        $('upload_progress').style.display = 'none';
        $('darkBackgroundLayer').style.display = 'none';
        $('upload_error').style.display    = 'block';
    }

    function do_upload_progress() {
        tries++;
    //    $('tries').innerHTML = tries;
    // Create a new connection object
        var httpobj;
        try {
            httpobj = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e) {
            try {
                httpobj = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (oc) {
                httpobj = null;
            }
        }
        if (!httpobj && typeof XMLHttpRequest != 'undefined')
            httpobj = new XMLHttpRequest();
    // Set up the query
        httpobj.open('GET', 'status.php?id='+upload_id, false);
    // Prevent the browser cache from being used [for documents post-1994]
    // Safari Bug: Safari will mark the satus as 'undefined' instead of 200 if a page is cached [bad]
        httpobj.setRequestHeader('If-Modified-Since', 'Wed, 15 Nov 1995 00:00:00 GMT');
    // Submit the query
        httpobj.send(null);
    // Return the data
    //    $('retcode').innerHTML = httpobj.responseText;
        var ret = httpobj.responseText.split("\n");
    // No response?
        if (ret[0] == '') { /* Nothing to actually do here. */ }
    // Done?
        if (ret[0] == 'done') {
        // Standard complete
            if (total && bytes >= total) {
                do_upload_complete();
                return;
            }
        // Something is wrong if we've been trying for 10 seconds with luck.
            else if (tries > 10 || (total && bytes < total)) {
                do_upload_error();
                return;
            }
        // Probably uploaded faster than we could detect
            else if (!total && !bytes) {
                do_upload_complete();
                return;
            }
        }
    // Show progress
        else if (!isNaN(ret[0]) && !isNaN(ret[1])) {
        // Show the progress indicator?
            if (bytes < 1) {
                $('progress_pending').style.display = 'none';
                $('progress_data').style.display    = 'block';
            }
        // Update the progress indicator
            bytes = parseInt(ret[0]);
            total = parseInt(ret[1]);
            var timer = parseInt(ret[2]);
            speed = (timer > 0) ? (bytes / timer) : bytes;
        //    $('progress_pct').innerHTML   = round(100 * (bytes / total), 0) + '%';
        //    $('progress_pct').style.width = $('progress_pct').innerHTML;
            $('progress_pct').style.width = round(100 * (bytes / total), 0) + '%';
        //    $('progress_bytes').innerHTML = bytes;
        //    $('progress_total').innerHTML = total;
            $('progress_speed').innerHTML = parseInt(speed / 1024)+ ' Kbps';
        }
    // Loop again
        setTimeout(do_upload_progress, 1000);
    }

// Reset the uploader to send another file
    function upload_another(reset_form) {
        if (reset_form) {
            $('upload_file').value = '';
        }
    // Reset the progress variables
        $('progress_pct').style.width         = 0;
        //$('progress_bytes').innerHTML       = 0;
        //$('progress_total').innerHTML       = 0;
        tries                               = 0;
        speed                               = 0;
        bytes                               = 0;
        total                               = 0;
        upload_id                           = '';
    // Reset the display blocks
        $('upload_progress').style.display  = 'none';
        $('upload_post').style.display      = 'none';
        $('upload_error').style.display     = 'none';
        remove_class('upload_form', '_offscreen');
        //$('upload_pre').style.display       = 'block';
        $('progress_pending').style.display = 'inline';
        $('progress_data').style.display    = 'none';
		$('upload_form').file = '';
    }

// Round a number to a specified number of decimal places (default: 2)
    function round(num, places) {
        places = (!isNaN(places) ? parseInt(places) : 2);
        return '' + Math.round(num * Math.pow(10, places)) / Math.pow(10, places);
    }

// Form verification routine
	function upload_validate() {
		if ( $('upload_form').file.value == '' ) {
			alert("Please select a file to upload.")
			return false;
		}
		if ( $('upload_form').name.value == '' ) {
			alert("You must provide your name.")
			return false;
		}
		if ( $('upload_form').email.value == '' ) {
			alert("You must provide your email address.")
			return false;
		}
		if ( $('upload_form').phone.value == '' ) {
			alert("You must provide your phone number.")
			return false;
		}
		return true;
	}
