var _affID=1;

var xmlhttp;
var kw_array = new Array();
var rawad;

function awContent(url) {
        return loadAd(url);
}

function loadXMLDoc(url)
{
	xmlhttp=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange=state_Change
		xmlhttp.open("GET",url,true)
		xmlhttp.send(null)
	}
}

function loadAd(url)
{
	xmlhttp=null
	// code for Mozilla, etc.
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest()
	}
	// code for IE
	else if (window.ActiveXObject) {
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	if (xmlhttp!=null) {
		xmlhttp.open("GET",url,false)
		xmlhttp.send(null)
		return xmlhttp.responseText;
	}
}

function state_Change()
{
	// if xmlhttp shows "loaded"
	if (xmlhttp.readyState==4) {
		// if "OK"
		if (xmlhttp.status==200) {
			var keywords=xmlhttp.responseText;
			kw_array = keywords.split(',');
			adWords(document.body);
    		}
  	}
}

function adWords(n) {
    if (n.nodeType == 3 /*Node.TEXT_NODE*/) {
	s = new String(n.data);
	for(i=0;i<kw_array.length-1;i++) {
		srch = new String(kw_array[i]);
		start = s.toLowerCase().indexOf(srch);
		if (start >= 0) {
			s1 = s.slice(0,start);
			s2 = s.slice(start+srch.length);
			var p = document.createElement("span");
			var t1 = document.createTextNode(s1+" ");
			var p1 = document.createElement("span");
			var t2 = document.createTextNode(" "+s2);
			p1.setAttribute("class","adword");
			srch = srch.replace(/^\s+|\s+$/g,"");
			var myurl='/intext/ad.php?affID='+_affID+'&word='+srch;
			p1.setAttribute("onmouseover","return escape(awContent('"+myurl+"'));");
//			alert(escape(awContent(myurl)));
			p1.setAttribute("onclick","return window.open('http://www.gsa-sport.com/intext/redir.php?u="+escape(awContent(myurl))+"');");
			p1.appendChild(document.createTextNode(srch));
			p.appendChild(t1);
			p.appendChild(p1);
			p.appendChild(t2);
			n.parentNode.replaceChild(p, n);




		};
	};
    }
    else {
        // If the node is not a Text node, loop through its children
        // and recursively call this function on each child.
        var kids = n.childNodes;
        for(var i = 0; i < kids.length; i++) adWords(kids[i]);
    }
}

loadXMLDoc('/intext/keywords.php?id='+_affID);

