/*
 *
 * Poup AJAX Window Functions
 *
 * S.Manning Sept 2007
 *
 * =============================================================================
 */


	/*
	 *
	 * Generic functions and request handler
	 *
	 */
	var xmlHttp=false;
	var insertTxt="";

	/*@cc_on @*/
	/*@if (@_jscript_version >= 5)
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlHttp = false;
			}
		}
	@end @*/

	if (!xmlHttp && typeof XMLHttpRequest!='undefined') {
		xmlHttp = new XMLHttpRequest();
	};

	function Trim(str) {
	  //str = this != window? this : str;
	  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	};

	/*
	 *
	 * Get the requested URL content
	 *
	 */
	function getUrl(url, ctrlId) {
		insertTxt="";
		xmlHttp.open("GET", url,true);
		xmlHttp.onreadystatechange=function() {
			if (xmlHttp.readyState==4) {
				if (xmlHttp.status==200) {
					var txt="";
					//txt=txt+"<div id='close'><a href=\"javascript:hidePopup('"+ctrlId+"')\"><img src='/resources/images/close.jpg'></a></div>";
					txt=txt+xmlHttp.responseText;
					document.getElementById(ctrlId).innerHTML=txt;
				}
			}
		};
		xmlHttp.send(null);
	};


	/*
	 *
	 * Display popup window for customer facing calling the required url asynchronously
	 *
	 */
	function showPopup(pic_id, height, width, event, ctrlId, path) {
		var div=document.getElementById(ctrlId);
		var url=path+"product_cust_pic?pic="+pic_id+"_popup.jpg";
		
		var x=(document.documentElement.offsetWidth/2) -(width/2);;
		var y=document.documentElement.scrollTop;
		
		div.style.top=y+"px";
		div.style.left=x+"px";

		getUrl(url, ctrlId);

		var boxWidth=width+"px"
		div.style.width=boxWidth;

		var boxHeight=Number(height)+30+"px";
		div.style.height=boxHeight;

		div.setAttribute("class","show");
		div.className="show";
		
	};

	/*
	 *
	 * Display popup window for admin calling the required url asynchronously
	 *
	 */
	function showAdminPopup(pic_id, height, width, event, ctrlId) {
		var div=document.getElementById(ctrlId);
		var url="cust_pic_popup?pic="+pic_id+"_popup.jpg";
		
		var x=document.documentElement.offsetWidth -(width+100);		
		var y=document.documentElement.scrollTop;
		
		div.style.top=y+"px";
		div.style.left=x+"px";
		
		getUrl(url, ctrlId);

		var boxWidth=width+"px"
		div.style.width=boxWidth;

		var boxHeight=Number(height)+30+"px";
		div.style.height=boxHeight;

		div.setAttribute("class","show");
		div.className="show";	
	};
	
	/*
	 *
	 * Hide the popup window
	 *
	 */
	function hidePopup(ctrlId) {
		var div=document.getElementById(ctrlId);
		div.setAttribute("class","");
		div.setAttribute("style","");
		div.className="";

		div.innerHTML="";
	};

	/*
	 *
	 * Drag and drop for the window
	 *
	 */
	function beginDrag(event, ctrlId) {
		var div=document.getElementById(ctrlId);
		var x=parseInt(div.style.left);
		var y=parseInt(div.style.top);
		var deltaX=event.clientX - x;
		var deltaY=event.clientY - y;

		if (document.addEventListener) {
			// DOM Level 2
			document.addEventListener("mousemove", moveHandler, true);
			document.addEventListener("mouseup", upHandler, true);
		} else {
			// IE5+
			document.attachEvent("onmousemove", moveHandler);
			document.attachEvent("onmouseup", upHandler);
		};

		if (event.stopPropagation) event.stopPropagation(); //DOM Level2
		else event.cancelBubble=true;						// IE5+

		if (event.preventDefault) event.preventDefault();	//DOM Level 2
		else event.returnValue=false;						// IE5+

		function moveHandler(event) {
			if (!event) event=window.event;						// IE5+
			div.style.left=(event.clientX - deltaX)+"px";
			div.style.top=(event.clientY- deltaY)+"px";

			if (event.stopPropagation) event.stopPropagation();	//DOM Level2
			else event.cancelBubble=true;						// IE5+

			if (event.preventDefault) event.preventDefault();	//DOM Level 2
			else event.returnValue=false;						// IE5+
		};

		function upHandler(event) {
			if (!event) event=window.event;						// IE5+

			if (document.removeEventListener) {
				// DOM Level 2
				document.removeEventListener("mouseup",upHandler, true);
				document.removeEventListener("mousemove",moveHandler, true);
			} else {
				// IE5+
				document.detachEvent("onmousemove", moveHandler);
				document.detachEvent("onmouseup", upHandler);
			};

			if (event.stopPropagation) event.stopPropagation(); //DOM Level2
			else event.cancelBubble=true;						// IE5+

			if (event.preventDefault) event.preventDefault();	//DOM Level 2
			else event.returnValue=false;						// IE5+
		};

	};
