var isIE = navigator.userAgent.toLowerCase().indexOf("msie") > -1;
var isMoz = document.implementation && document.implementation.createDocument;
var url = document.location.href;
var xmlhttp = CreateXMLHttpRequest();
if (!xmlhttp) alert("XMLHttpRequest object cannot be created.");

function CreateXMLHttpRequest()
{
	return xmlhttpreq = window.XMLHttpRequest ? new XMLHttpRequest : new ActiveXObject("Microsoft.XMLHTTP");
}

function setPageSize(width, height)
{
	window.innerWidth = width;
	window.innerHeight = height;
}

function moveCenter()
{
	window.moveTo((screen.availWidth - window.outerWidth) / 2, (screen.availHeight - window.outerHeight) / 2);
}

function GetToken(str, token)
{
	var idx = str.indexOf(token + '=');
	if (idx <= 0) return null;
	var argstr = str.substring(idx + token.length + 1);
	idx = argstr.indexOf('&');
	return idx >=0 ? argstr.substring(0, idx) : argstr;
}

function QueryPref(path, opts)
{
	var node;
	var requrl = "/prefs/prefs.xml?type&";
	if (opts)
		requrl += opts + "&";
	if (path)
		requrl += "path=" + path;
	xmlhttp.open("GET", requrl, false);
	xmlhttp.send(null);
	node = xmlhttp.responseXML.firstChild;
	if (!node) {
		alert("Failed to retrieve preference data from MediaCoder.");
		return null;
	}
	return getChildNode(node);
}

function QueryPlugin(type, option)
{
	var node;
	var requrl = "/prefs/plugin.xml?type=" + type;
	if (option) requrl += "&" + option;
	xmlhttp.open("GET", requrl, false);
	xmlhttp.send(null);
	node = xmlhttp.responseXML.firstChild;
	if (!node) {
		alert("Failed to connect to MediaCoder.");
		return null;
	}
	node = getChildNode(node);
	if (type) node = getNodeByName(node, type);
	return node;
}

function QueryString(url, async)
{
	try { 
		xmlhttp.open("GET", url, async);
		xmlhttp.send(null);
		return xmlhttp.responseText;
	}
	catch (ex) {
		return null;
	}
}

function SetPrefValue(param, val)
{
	xmlhttp.open("GET", "/prefs/set?" + param + "=" + val, true);
	xmlhttp.send(null);
}

function GetPrefValue(param)
{
	xmlhttp.open("GET", "/prefs/get?path=" + param, false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}

function RevertPrefValue(param)
{
	xmlhttp.open("GET", "/prefs/revert?path=" + param, false);
	xmlhttp.send(null);
	return xmlhttp.responseText;
}

function OpenURL(urlstring) {
	xmlhttp.open('GET', '/openurl?url=' + urlstring, false);
	xmlhttp.send(null);
}

function CreateDoc()
{
	var xmlDoc;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", "", null);
		//xmlDoc.onload = callback;
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function () {
			//if (xmlDoc.readyState == 4) callback()
		};
 	}
	else
	{
		return null;
	}
	return xmlDoc;	
}

function LoadXMLFile(xmlfile)
{
	var xmlDoc = CreateDoc();
	xmlDoc.async = false;
	xmlDoc.load(xmlfile);
	return xmlDoc;
}

function NewXML(rootkey)
{
	var xmlDoc;
	if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc = document.implementation.createDocument("", rootkey, null);
	}
	else if (window.ActiveXObject)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
 	}
	else
	{
		return;
	}
	return xmlDoc;
}

function PostData(requrl, data)
{
	xmlhttp.open("POST", requrl, false);
	//xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(data);
}

/******************************** DOM helper functions ********************************/

function getNextNode(node)
{
	while ((node = node.nextSibling) && node.nodeType!=1);
	return node;
}

function getChildNode(node)
{
	node = node.firstChild;
	while (node && node.nodeType != 1) {
		node = node.nextSibling;
	} 
	return node;
}

function getLastChildNode(node)
{
	node=node.lastChild;
	while (node && x.nodeType!=1) {
		node=node.previousSibling;
	}
	return node;
}

function getNodeValue(node)
{
	return node.firstChild ? node.firstChild.nodeValue : null
}

function getParentNode(node)
{
	return node.parentNode;
}

function getNodeByName(node, name)
{
	for (; node && node.nodeName != name; node = getNextNode(node));
	return node;
}

function getChildNodeValue(node, name)
{
	for (node = getChildNode(node); node && node.nodeName != name; node = getNextNode(node));
	return node ? getNodeValue(node) : null;
}

function getNodeByAttribute(node, name, attrname, attrvalue)
{
	for (; node; node = getNextNode(node)) {
		if  (node.nodeName == name && node.getAttribute(attrname) == attrvalue)	 return node;
	}
	return null;
}

function addAttribute(doc, node, attrname, attrvalue)
{
	var attr = doc.createAttribute(attrname);
	attr.value= attrvalue;
	node.setAttributeNode(attr);
	return attr;
}

function addElementNode(doc, node, name)
{
	var elementNode = doc.createElement(name);
	node.appendChild(elementNode);
	return 	elementNode;
}

function addTextNode(doc, node, text)
{
	var textNode = doc.createTextNode(text);
	node.appendChild(textNode);
	return 	textNode;
}

function removeFirstChild(node)
{
	var c = getChildNode(node);
	return c?node.removeChild(c):null;
}

function removeLastChild(node)
{
	var c = getLastChildNode(node);
	return c?node.removeChild(c):null;
}

function removeChildren(node)
{
	var c = getChildNode(node);
	while(c) {
		var n=getNextNode(c);
		node.removeChild(c);
		c=n;
	}
}

function isMozilla()
{
	return navigator.userAgent.indexOf("Mozilla") >= 0;
}
