var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		o.onmousedown	= Drag.start;

		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;

		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;

		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]),
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

function CloseFloatingWindowWindow(ShowDivNameP, InnerDivIdP)
{
    document.eula.agreeterms.checked = false;
    if(typeof(InnerDivIdP)!="undefined")
    {
        UnBlockPage(InnerDivIdP);
    }
    var main = document.getElementById(ShowDivNameP);
    main.style.display = "none";
}


function BlockPage(HeightP,DivIdP)
{
    if(typeof(DivIdP)!="undefined")
    {                     
        var InterDiv = document.getElementById(DivIdP);            
        InterDiv.style.width =  getClientPos()[0] + "px" ;    
        if(HeightP != null || HeightP >= 1)
        {
            InterDiv.style.height = HeightP + "px";
        }
        else
        {
            InterDiv.style.height = getClientPos()[1]+"px";
        }
        InterDiv.style.left = 0;
        InterDiv.style.top = 0;
        InterDiv.style.visibility = "visible";
        InterDiv.style.display = "block";
    }
}

function UnBlockPage(DivIdP)
{
    if(typeof(DivIdP)!="undefined")
    {
        var InterDiv = document.getElementById(DivIdP);        
        InterDiv.style.display = "none";   
        InterDiv.style.visibility="hidden";
        InterDiv.style.width="0px";
        InterDiv.style.height="0px";
        InterDiv.innerHTML="";
    }
}

function getClientPos()
{
   	var winWidth, winHeight, 
   	d=document;
   	if (typeof window.innerWidth!='undefined')
   	{
   		winWidth=window.innerWidth;
   		winHeight=window.innerHeight;
   	}
   	else
   	{
   		if (d.documentElement && typeof d.documentElement.clientWidth!='undefined' && d.documentElement.clientWidth!=0)
   		{
   			winWidth=d.documentElement.clientWidth;
   			winHeight=d.documentElement.clientHeight;
   		}
   		else
   		{
   			if (d.body && typeof d.body.clientWidth!='undefined')
   			{
   				winWidth=d.body.clientWidth;winHeight=d.body.clientHeight;
   			}
   		}
   	}
   	return [winWidth, winHeight];
}

var CurrentForm = null;

function ShowPopup(DivName,TitleBarName,DivIdP, Form1)
{
    if (Form1 != null)
    {
    	CurrentForm = Form1;
    }
    
    var clientInfo = getClientPos();

    var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
    var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
    var heightL = (clientInfo[1] + scrollTop);
    var widthL = (clientInfo[0] + scrollLeft);
    
    if(typeof(DivIdP)!="undefined")
    {
        BlockPage(heightL,DivIdP);
    }
    else
    {
        BlockPage(heightL,"InterDiv");
    }    
    
    var main = document.getElementById(DivName);
    
    var left = (widthL - parseInt(main.style.width)) / 2;
    var top = (heightL - parseInt(main.style.height)) / 2;

    main.style.left = left + "px";
    main.style.top = top + "px";
    
    var titlebar = document.getElementById(TitleBarName);
    Drag.init(titlebar, main);
    
    main.style.display = "block";
    main.style.visibility = "visible";
}

function SubmitForm()
{
    if (document.eula.agreeterms.checked)
    {
	if (CurrentForm != null)
	{
	    CloseFloatingWindowWindow('main','InterDiv');
	    CurrentForm.submit();
	}
    }
    else
    {
    	alert("Please check the terms and conditions");
    }
}

function Resize()
{
	var DivName = 'main';
	var main = document.getElementById(DivName);
	if (main.style.display == "block")
		ShowPopup(DivName,'titlebar','InterDiv', null);
}

function InitializePopup()
{
	var main = document.getElementById("main");
	main.style.left = "325px";
	main.style.top = "110px";
	main.style.display = "none";
	main.style.height = "300px";
	main.style.width = "330px";
	main.style.overflow = "hidden";
	
	var className = "class";
	if (document.all)
		className = "className";
	
	var TitleBarText = document.createElement("div");
	TitleBarText.setAttribute("id", "FloatingDivTitleBarText");
	TitleBarText.setAttribute(className, "FloatingDivTitleBarText");
	TitleBarText.innerHTML = "Agreement Required for Purchase";	// Make your custom title here
	
	var CloseButton = document.createElement("div");
	CloseButton.setAttribute(className, "FloatingDivCloseButton");
	CloseButton.setAttribute("id", "FloatingDivCloseButton");
	CloseButton.innerHTML = '<img src="images/close.gif" id="close" onclick="CloseFloatingWindowWindow(\'main\',\'InterDiv\');" alt="Click to Close." />';
	
	var TitleBar = document.createElement("div");
	TitleBar.setAttribute(className, "FloatingDivTitleBar");
	TitleBar.setAttribute("id", "titlebar");
	
	TitleBar.appendChild(TitleBarText);
	TitleBar.appendChild(CloseButton);
	
	var DivText = document.createElement("div");
	DivText.setAttribute(className, "FloatingDivText");
	DivText.setAttribute("id", "FloatingDivText");
	DivText.innerHTML = '<h3>You must accept the following condition to continue with your purchase</h3>'; // Your heading here
	DivText.innerHTML += '<p class="agree">I have read and understand fully the "Policies" page and crafting times associated with this purchase.</p>';  // Your terms here
	var eula = createNamedElement("form", "eula");
	eula.setAttribute("id", "eula");
	
	var fieldset = document.createElement("fieldset");
	fieldset.innerHTML += '<legend>Click if you agree with this statement</legend>';
	fieldset.innerHTML += '<input type="checkbox" name="agreeterms" id="agreeterms" /> I agree to the terms and conditions<br /><br />';
	fieldset.innerHTML += '<input type="button" onclick="SubmitForm();" value="Accept" name="agree" />';
	
	eula.appendChild(fieldset);
	DivText.appendChild(eula);
	
	main.appendChild(TitleBar);
	main.appendChild(DivText);
}

function createNamedElement(type, name) {
   var element = null;
   try {
      element = document.createElement('<'+type+' name="'+name+'">');
   } catch (e) {
   }
   if (!element || element.nodeName != type.toUpperCase()) {
      element = document.createElement(type);
      element.name = name;
   }
   return element;
}

window.onresize = Resize;
window.onload = InitializePopup;