function unescapeXML(xml) {
	var gt; 
    var str = xml;

    //replace &lt; with <
    gt = -1;
    while (str.indexOf("&lt;", gt + 1) > -1) {
        var gt = str.indexOf("&lt;", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "<";
        newStr = newStr + str.substr(gt + 4, str.length);
        str = newStr;
    }
    //replace &gt; with >
    gt = -1;
    while (str.indexOf("&gt;", gt + 1) > -1) {
        var gt = str.indexOf("&gt;", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += ">";
        newStr = newStr + str.substr(gt + 4, str.length);
        str = newStr;
    }
	//replace &apos; or &#039; with '
	gt = -1;
    while (str.indexOf("&apos;", gt + 1) > -1) {
        var gt = str.indexOf("&apos;", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "'";
        newStr = newStr + str.substr(gt + 6, str.length);
        str = newStr;
    }
	gt = -1;
    while (str.indexOf("&#039;", gt + 1) > -1) {
        var gt = str.indexOf("&#039;", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "'";
        newStr = newStr + str.substr(gt + 6, str.length);
        str = newStr;
    }
	//replace &quot; with "
	gt = -1;
    while (str.indexOf("&quot;", gt + 1) > -1) {
        var gt = str.indexOf("&quot;", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "\"";
        newStr = newStr + str.substr(gt + 6, str.length);
        str = newStr;
    }
	
	return str;
}

function escapeXML(str) {
	var gt;

    //replace < with &lt;
    gt = -1;
    while (str.indexOf("<", gt + 1) > -1) {
        var gt = str.indexOf("<", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "&lt;";
        newStr = newStr + str.substr(gt + 1, str.length);
        str = newStr;
    }
    //replace > with &gt;
    gt = -1;
    while (str.indexOf(">", gt + 1) > -1) {
        var gt = str.indexOf(">", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "&gt;";
        newStr = newStr + str.substr(gt + 1, str.length);
        str = newStr;
    }
	//replace ' with &#039;
	gt = -1;
    while (str.indexOf("'", gt + 1) > -1) {
        var gt = str.indexOf("'", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "&#039;";
        newStr = newStr + str.substr(gt + 1, str.length);
        str = newStr;
    }
	//replace " with &quot;
	gt = -1;
    while (str.indexOf("\"", gt + 1) > -1) {
        var gt = str.indexOf("\"", gt + 1);
        var newStr = str.substr(0, gt);
        newStr += "&quot;";
        newStr = newStr + str.substr(gt + 1, str.length);
        str = newStr;
    }
	
	return str;
}

//Return the value of the specified parameter in the url/querystring
function gup(href, name) {
  if (href.indexOf("?")==-1 && href.charAt(0)!="?") {
	  href = "?"+href;
  }
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( href );
  if( results == null )
    return "";
  else
    return results[1];
}

//Crude function to return an array of the specified parameter in the url/querystring
function gup2(href,name) {
	href = href.replace("?", "&");
	a = href.split("&"+name+"=");
	var ubound = a[a.length-1];
	var l = ubound.indexOf("&");
	if (l==-1) {
		a[a.length-1]=ubound.substr(0);
	} else {
		a[a.length-1]=ubound.substr(0,l);
	}
	return a.slice(1);
}

//Another crappy function, this one returns all the params that match the name as a querystring
//Could be done better but time is money so will revisit when i have one or the other
function gup3(href,name) {
	var a = gup2(href, name);
	var ret = "";
	for (i=0;i<a.length;i++) {
		ret += "&"+name+"="+a[i];
	}
	return ret;
}

function formatDate(dt) {
	var y = dt.getFullYear();
	var m = dt.getMonth()+1;
	var d = dt.getDate();
	return d+"."+m+"."+y;
}

function isDate(sDate) {
    var scratch = new Date(sDate);
    if (scratch.toString() == "NaN" || scratch.toString() == "Invalid Date") {
        return false;
    } else {
        return true;
    }
}

function getTimeStamp() {
	var d = new Date();
	return d.getTime().toString();
}

function getDateTime() {
	var d = new Date();
	var ret = formatDate(d).replace(/\./g, "/");
	ret = ret + " " + d.getHours() + ":" + d.getMinutes();
	return ret;
}

function clipboardEnabled() {
	var result = false;
	var oldCB;
	if (window.clipboardData) {
		oldCB = window.clipboardData.getData('Text');
		result = window.clipboardData.setData('Text', 'WM');
		if (result && oldCB!=null) {
			window.clipboardData.setData('Text', oldCB);
		}
	}
	return result;
}

//Returns path without protocol and filename
function getPath() {
	path = location.pathname.substr(0, location.pathname.lastIndexOf("/"));
	return document.domain+path+"/";
}

//These characters in the passed String need to be encoded
function specialEncodeURI(sURI) {
	sURI = sURI.replace(/%/g, "%25");
	sURI = sURI.replace(/&/g, "%26");
	sURI = sURI.replace(/;/g, "%3B");
	sURI = sURI.replace(/ /g, "%20");
	return sURI;
}
