// Global Functions
// global.js

function printerFriendly() {
// generates a printer friendly version of a page by reading
// the HTML between a defined tag
// by Jay Reid - Jan. 2002
// Modified CRP 2004

    if (document.getElementById("s4-bodyContainer"))
	{
	    var contentLink = document.getElementById("s4-bodyContainer")
		var sHTMLToPrint = contentLink.innerHTML
		var sTitle = document.title
		//var sLocation = document.location

		if (sHTMLToPrint != "")
		{
			printWindow = window.open("", null, "location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,titlebar=yes,toolbar=yes");
			printWindow.document.writeln("<HTML>\n<HEAD>\n");
			printWindow.document.writeln("<LINK HREF=\"/_layouts/EpcorInternetPublishing/en-ca/css/epcorInternet.css\" REL=\"stylesheet\" TYPE=\"text/css\">\n");
			printWindow.document.writeln("<LINK HREF=\"/_layouts/EpcorInternetPublishing/en-ca/css/print.css\" REL=\"stylesheet\" TYPE=\"text/css\">\n");
            printWindow.document.writeln("<script type=\"text/javascript\" language=\"javascript\" src=\"/_layouts/EpcorInternetPublishing/en-ca/scripts/jquery-1.3.2.min.js\"></script>\n");
//			printWindow.document.writeln("<style type=\"text/css\">@import url( /internet/Styles/advanced.css );</style>\n");
			printWindow.document.writeln("</HEAD>\n<BODY>\n");
			printWindow.document.writeln("<table cellspacing=\"0\" cellpadding=\"0\" width=\"700\" border=\"0\"><tr><td>\n");
			printWindow.document.writeln(sHTMLToPrint);
			printWindow.document.writeln("</td></tr></table>\n");
			//printWindow.document.writeln("<br><br>Originating Page:&nbsp;" + sLocation);
			printWindow.document.writeln("\n</BODY>\n</HTML>");
			printWindow.document.title = sTitle + " (Printer Friendly)";
			printWindow.focus();
		}
		
		else
		{
			alert("An error occurred attempting to create a printer friendly page from the original document.\nThe original page may not be formatted properly or it's layout has changed.");
		}
	}
}

function EpcorInternet() { }

/* Used by the Twitter User Control */
EpcorInternet.FormatTweet = function (text) {
    if (text.indexOf('EPCOR: ') === 0) {
        text = text.substring(7);
    }
    allWords = text.split(' ');
    for (i = 0; i < allWords.length; i++) {
        if (allWords[i].indexOf('#') === 0) {
            allWords[i] = '<a href="http://twitter.com/search?q=%23' + allWords[i].substring(1) + '">' + allWords[i] + '</a>';
            continue;
        }

        atMatch = allWords[i].match(/^[@][a-zA-Z0-9_]+/)
        if (atMatch) {
            at = atMatch[0];
            allWords[i] = '@<a href="http://twitter.com/' + at.substring(1) + '">' + at.substring(1) + '</a>' + allWords[i].substring(at.length);
            continue;
        }

        if (allWords[i].indexOf('http://') === 0) {
            allWords[i] = '<a href="' + allWords[i] + '">' + allWords[i] + '</a>';
            continue;
        }
    }
    return allWords.join(' ');
};

/* Loads an XML Doc from a Web Request. Taken from w3schools */
EpcorInternet.LoadXMLDoc = function (dname) {
    if (window.XMLHttpRequest) {
        xhttp = new XMLHttpRequest();
    }
    else {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xhttp.open("GET", dname, false);
    xhttp.send("");
    if (xhttp.responseXML) {
        return xhttp.responseXML;
    } else {
        return xhttp.responseText;
    }
};
