/**
 *	Allows the user to submit their email address to the o-news section
 *	@access	public
 *	@return	void
 */
function showGalleryItem(categoryId, imageId){

	//grab email entered into the form
	var httpRequest;

	//create the http request checking for mozilla vs ie
	if (window.XMLHttpRequest) {
	    httpRequest = new XMLHttpRequest();
		if (httpRequest.overrideMimeType) {
		    httpRequest.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
        try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
        }
	}

	//make sure the request was created properly
    if (!httpRequest) {
        //changed to return true so that the form submits
        return true;
    }

    //set our call back function to be called once the info is processed
	httpRequest.onreadystatechange = function() { showItemProcess(httpRequest); };

	//post the data
	//get the current host (since serveral domain forwards will be taking place
	var url = 'http://' + location.host + location.pathname.replace("photo_gallery.php", "") + 'get_image_xml.php?imageId=' + encodeURI(imageId);
	try{
		httpRequest.open('GET', url, true);
	} catch(e) {
		//if there is any kind of error opening the request we will
		//return true so that the form submits via normal http
		return true;
	}

	//send the request
	httpRequest.send(null);

	//set the return to false so that the form is not submitted via html
	return false;

}

function displayGalleryItem(title, imageUrl){

	var img = document.getElementById('photoImage');
	var titleDiv = document.getElementById('photoTitle');

	//load the image, save it to buffer
	var newImage = new Image();
	newImage.src = imageUrl;
	img.src = newImage.src;

	//show the title
	titleDiv.innerHTML = title;

}

function showItemProcess(httpRequest) {

    if (httpRequest.readyState == 4) {
        if (httpRequest.status == 200) {

        	//for debugging uncomment the following line
        	//alert(httpRequest.responseText);

			var xmldoc = httpRequest.responseXML;
			var rootNode = xmldoc.getElementsByTagName('root').item(0);
			var completedNode = rootNode.getElementsByTagName('completed').item(0);
			var errorNode = rootNode.getElementsByTagName('error').item(0);
			var completed = parseInt(completedNode.firstChild.data);
			var errorCode = parseInt(errorNode.getAttribute('id'));
			var errorString;
			var title = rootNode.getElementsByTagName('title').item(0).firstChild.data;
			var imageUrl = rootNode.getElementsByTagName('image').item(0).firstChild.data;

			//check if there was an error, if not show the 'completed' message
			if((completed == 1) && (errorCode == 0)){
				displayGalleryItem(title, imageUrl);
			} else {

				//get the error string
				try{
					errorString = errorNode.firstChild.data;
				} catch(e) {
					errorString = '';
				}

				//there was an error, so display it
				showGalleryItemError(errorString);
				return false;
			}

        } else {
            showGalleryItemError('There was a problem with the request.');
        }
    }

}

function showGalleryItemError(error){

	//get the error div
	var div = document.getElementById('errorBox');

	//make sure the error text has been set
	if(error.length == 0){
		error = 'An unexpected error has occured.';
	}

	//put the error text in the error div and show it
	div.innerHTML = decodeURI(escape(error));
	div.style.display = '';


}



//     Example File From "JavaScript and DHTML Cookbook"
//     Published by O'Reilly & Associates
//    Copyright 2003 Danny Goodman

function formatCommas(numString) {
    var re = /(-?\d+)(\d{3})/;
    while (re.test(numString)) {
        numString = numString.replace(re, "$1,$2");
    }
    return numString;
}

function formatNumber (num, decplaces) {
    // convert in case it arrives as a string value
    num = parseFloat(num);
    // make sure it passes conversion
    if (!isNaN(num)) {
        // multiply value by 10 to the decplaces power;
        // round the result to the nearest integer;
        // convert the result to a string
        var str = "" + Math.round (eval(num) * Math.pow(10,decplaces));
        // exponent means value is too big or small for this routine
        if (str.indexOf("e") != -1) {
            return "Out of Range";
        }
        // if needed for small values, pad zeros
        // to the left of the number
        while (str.length <= decplaces) {
            str = "0" + str;
        }
        // calculate decimal point position
        var decpoint = str.length - decplaces;
        // assemble final result from: (a) the string up to the position of
        // the decimal point; (b) the decimal point; and (c) the balance
        // of the string. Return finished product.
        return formatCommas(str.substring(0,decpoint)) + "." + str.substring(decpoint,str.length);

    } else {
        return "NaN";
    }
}

