window.onload = initAll;

var xhr = false;
var xPos, yPos;

// Preview Window Functionality

function initAll() {
	var allLinks = document.getElementsByName("freeform");

	for (var i=0; i< allLinks.length; i++) {
		allLinks[i].onmouseover = showPreview;
	}

}

function showPreview(evt) {
	getPreview(evt);
	return false;
}

function hidePreview() {
	document.getElementById("myfreeform").style.visibility = "hidden";
}

function getPreview(evt) {
	if (evt) {
		var url = evt.target;
	}
	else {
		evt = window.event;
		var url = evt.srcElement;
	}
	url = "http://www.sewandquiltstore.com/common/freeform.htm"
	xPos = evt.screenX;
	yPos = evt.screenY;
	
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	}
	else {
		if (window.ActiveXObject) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) { }
		}
	}

	if (xhr) {
		xhr.onreadystatechange = showContents;
		xhr.open("GET", url, true);
		xhr.send(null);
	}
	else {
		alert("Sorry, but I couldn't create an XMLHttpRequest");
	}
}

function showContents() {
	var prevWin = document.getElementById("myfreeform");

	if (xhr.readyState == 4) {
		prevWin.innerHTML = (xhr.status == 200) ? xhr.responseText : "There was a problem with the request " + xhr.status;
		prevWin.style.top = parseInt(yPos) + "px";
		prevWin.style.left = parseInt(xPos) + "px";
		prevWin.style.position = "absolute";
		prevWin.style.border = "1px #CC0 solid";
		prevWin.style.visibility = "visible";	
		prevWin.style.background = "#777";	
		var hideWin = document.getElementById("hide");
		hideWin.onclick = hidePreview;
		//prevWin.onmouseout = hidePreview;
	}
}

function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}

function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}
