var isIE = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
var isOpera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1);
var isFireFox = (navigator.userAgent.toLowerCase().indexOf("firefox") != -1);
var isMozilla = (navigator.userAgent.toLowerCase().indexOf("mozilla") != -1);

/*
* Add style sheet to the page dynamically
*/
function addStyleSheet(styleSheet) {
	// Load the css associated with this item	
	var documentHead = document.getElementsByTagName("head")[0];         
	var cssNode = document.createElement("link");
	cssNode.type = "text/css";
	cssNode.rel = "stylesheet";
	cssNode.href = styleSheet;
	documentHead.appendChild(cssNode);
}

/*
* Add a Javascript script to the page dynamically
*/
function addJavaScript(script, onLoadFunction) {
	var headElement = document.getElementsByTagName("head")[0];         
	var newScript = document.createElement("script");
	newScript.type = "text/javascript";
	
	if(onLoadFunction != undefined && isIE == false) {
		newScript.onload = onLoadFunction;
	}
	
	newScript.src = script;
	headElement.appendChild(newScript);
	/*
	* Special case for IE, call once added
	* doesn't call the onload function in IE so wel have to load it and then call it.
	* May have to put in a timer to give it time to load, or may load and then continue?
	*/
	if(onLoadFunction != undefined && isIE == true) {
		onLoadFunction.call();
	}
}

/*
* Converts a string into an XML Dom
* @param text The responseText of the request object
*/
function convertStringToXMLDom(text) {
	// replace all invalid characters with there encoded values
	text = text.replace(eval("/&/g"), "&amp;");
	var doc = undefined;
	// code for IE
	if (window.ActiveXObject) {
		doc = new ActiveXObject("Microsoft.XMLDOM");
  		doc.async = "false";
  		doc.loadXML(text);
  	}
	// code for Mozilla, Firefox, Opera, etc.
	else {
  		var parser = new DOMParser();
  		doc = parser.parseFromString(text,"text/xml");
  	}
	return doc;
}

/**
* Function to retrieve XML node content as a string
* @param {XMLNode} xmlNode The node to retieve the data from
* @return String - The html extracted from the xml node.
*/
function getXMLNodeSerialisation(xmlNode) {
  var txt = false;
  try {
    // Gecko-based browsers, Safari, Opera.
    var serializer = new XMLSerializer();
    txt = serializer.serializeToString(xmlNode);
  }
  catch (e) {
    try {
      // Internet Explorer.
      txt = xmlNode.xml;
    }
    catch (e) {}
  }
  return txt;
}


/**
* A Method that centers the component in the browser window. Cycles through parent nodes to find any offset's that may interfear with alignment.
* @param {Object} el The element to center in the brower window e.g. a div element
*/
function centerInWindow(el) {
    if(el) {
        var elWidth = el.style.width;
        var elHeight = el.style.height;
        var bWidth = browserWidth();
        var bHeight = browserHeight();
        var leftOffset = 0;
        var topOffset = 0;
        var xParent = el.parentNode;
        while(xParent) {
            if(xParent.offsetLeft) {
                leftOffset += parseInt(xParent.offsetLeft);
            }
            if(xParent.offsetTop) {
                topOffset += parseInt(xParent.offsetTop);
            }
            xParent = xParent.parentNode;
        }
        el.style.left = ((parseInt(bWidth) / 2) - (parseInt(elWidth) / 2)) > leftOffset ? 
        ((parseInt(bWidth) / 2) - (parseInt(elWidth) / 2)) - leftOffset + "px" : 
        leftOffset - ((parseInt(bWidth) / 2) - (parseInt(elWidth) / 2)) + "px";
        
        el.style.top = ((parseInt(bHeight) / 2) - (parseInt(elHeight) / 2)) > topOffset ? 
        ((parseInt(bHeight) / 2) - (parseInt(elHeight) / 2)) - topOffset + "px" :
        topOffset - ((parseInt(bHeight) / 2) - (parseInt(elHeight) / 2)) + "px";     
    } else {
        alert("Element does not exist!");
    }
}

/**
* Gets the current browsers width
* @return Number - The browsers width
*/
function browserWidth() {
    if(document.body.clientWidth) {
        return document.body.clientWidth;
    } else {
        return windowWidth = document.width;
    }   
}

/**
* Gets the current browsers height
* @return Number - The browsers height
*/
function browserHeight() {
    if(document.body.clientHeight) {
        return document.body.clientHeight;
    } else {
        return windowWidth = document.height;
    }   
}

/**
* A simple method that center a component in it's parent
* @param {Object} el The element to center in the parent window
*/
function centerInParent(el) {
    if(el) {
        var elWidth = el.style.width;
        var elHeight = el.style.height;
        var bWidth = browserWidth();
        var bHeight = browserHeight();
        var xParent = el.parentNode;
        el.style.left = ((parseInt(xParent.style.width) / 2) - (parseInt(elWidth) / 2)) + "px";
        
        el.style.top = ((parseInt(xParent.style.height) / 2) - (parseInt(elHeight) / 2)) + "px";     
    } else {
        alert("Element does not exist!");
    }
}

/*
* Change the style for an element
*/
function mouseStyle(el, className) {
	el.className = className;
}
