/**
 * Cross-platform xml parser instantion code
 * 
 * This file has been created based on blogpost that can be found on
 * http://www.mindlence.com/WP/?p=308 . -your name- has kindly provided
 * the code under New BSD License.
 *
 * Copyright (c) 2008, -your name-
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * - Redistributions of source code must retain the above copyright notice,
 *   this list of conditions and the following disclaimer.
 * 
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 *   
 *   - Neither the name of the <ORGANIZATION> nor the names of its contributors
 *   may be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 *   
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

/**
 * Stores XML Loader
 */
var xmlLoader2;

/**
 * Stores XML Parser Object
 */
var xmlParser2;

/**
 * Stores xml content
 */
var xmlContent2;

/**
 * Stores the callback that should be called with the loadXML function
 */
var _callbackFunction2 = null;

/**
 * Called, when the XML content is recieved.
 * 
 * This function then creates the xml parser with appropiate method.
 * If you want to do something else, this function is worth editing.
 */
function XMLReceive2() {
	// Loaded? Then create the parser!
	if (xmlLoader2.readyState == 4) {
		xmlContent2 = xmlLoader2.responseText;
		xmlParser2  = CreateXMLStringParser2(xmlLoader2.responseText);
		
		// And, if we have callback set, call it!
		if(_callbackFunction2 != null) {
			_callbackFunction2(xmlParser2);
		}
	}
}

/**
 * This function creates xml parser and returns it.
 * 
 * @param xmlContent takes the content from which the parser is created
 * @return
 */
function CreateXMLStringParser2(xmlContent) {
	try {
		var parser = new DOMParser();
		var xmlDoc = parser.parseFromString(xmlContent, "text/xml");
	} catch (Err) {
		try {
			var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async = "false";
			xmlDoc.loadXML(xmlContent);
		} catch (Err) {
			window.alert("Browser does not support XML parsing.");
			return false;
		}
	}

	return xmlDoc;
}

/**
 * This function creates ajax object and returns it
 * 
 * This ajax object is then used for loading of the xml parser contents
 */
function CreateAJAXObject2() {
	// Mozilla, Opera, and Safari
	try {
		AJAXObj = new XMLHttpRequest();
	}
	
	// Internet Explorer
	catch (err) {
		try {
			AJAXObj = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (err) {
			try {
				AJAXObj = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err) {
				alert("Your browser does not support AJAX!");
			}
		}
	}

	// And return
	return AJAXObj;
}

/**
 * Loads specified xml file and stores the contents
 * within xmlContent variable. 
 * 
 * After succesfull load it calls function given in the callback parameter.
 * The only parameter given is created DOMParser or it's IE equivalent
 * 
 * @param fileToLoad url of the file that is supposed to be loaded
 * @param callback the function that should be called after creating the xml parser
 * @return the xml parser
 */
function LoadXML2(fileToLoad, callback) {
	// Store the callback
	_callbackFunction2 = callback;
	
	// Now create ajax request object
	xmlLoader2 = CreateAJAXObject2();

	// Set the callback function and load the xml contents
	xmlLoader2.onreadystatechange = XMLReceive2;
	xmlLoader2.open("GET", fileToLoad, true);
	xmlLoader2.send(null);
}


