﻿/*
Copyright(c) 2009 Gilmore Software Development.
All rights reserved.
*/
/* Javascript compress
http://www.creativyst.com/Prod/3
http://javascriptcompressor.com/
*/

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
String.prototype.striptags = function() {
	var s = this.replace(/<\/?[^>]+(>|$)/g, "");
	return s.replace(/[&]/g, "%26");
};

function hide(i) { document.getElementById(i).style.display = "none"; }
function show(i) { document.getElementById(i).style.display = ""; }

function getEO(i) { return document.getElementById(i); }
function getEV(i) { return document.getElementById(i).value; }

function CenterToPage(div) {
	var l, h, d = getEO(div),
	    w = parseInt(d.style.width);

	if (isNaN(w))
		w = d.offsetWidth;

	l = (document.body.offsetWidth / 2) - (parseInt(w) / 2);
	d.style.left = l + "px";

	h = parseInt(d.style.height);
	if (isNaN(h))
		h = d.offsetHeight;

	h = (document.body.offsetHeight / 2) - (h / 2);

	d.style.top = h + "px";
	return true;
}

function Center(i) {
	CenterInViewArea(i);
}

function CenterInViewArea(i) {
	var scrolledX, scrolledY, centerX, centerY, o, Xwidth, Yheight, leftOffset, topOffset;
	if (self.pageYOffset) {//How much of the page has been scrolled?
		scrolledX = self.pageXOffset;
		scrolledY = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		scrolledX = document.documentElement.scrollLeft;
		scrolledY = document.documentElement.scrollTop;
	} else if (document.body) {
		scrolledX = document.body.scrollLeft;
		scrolledY = document.body.scrollTop;
	}
	//determine the coordinates of the center of browser's window
	if (self.innerHeight) {
		centerX = self.innerWidth;
		centerY = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		centerX = document.documentElement.clientWidth;
		centerY = document.documentElement.clientHeight;
	} else if (document.body) {
		centerX = document.body.clientWidth;
		centerY = document.body.clientHeight;
	}
	o = document.getElementById(i);
	Xwidth = parseInt(o.style.width);
	if (isNaN(Xwidth))
		Xwidth = o.offsetWidth;
	Yheight = parseInt(o.style.height);
	if (isNaN(Yheight))
		Yheight = o.offsetHeight;
	leftOffset = scrolledX + (centerX - Xwidth) / 2;
	topOffset = scrolledY + (centerY - Yheight) / 2;
	if (topOffset < 0) topOffset = 0;
	o.style.top = topOffset + 'px';
	o.style.left = leftOffset + 'px';
}

function findCtrl(name) {
	var i, o = document.getElementById(name);
	if (o !== null) {
		return o;
	}
	o = document.getElementsByTagName("input");

	i = 0;
	for (i = 0; i < o.length; i++)
		if (o[i].name.indexOf("$" + name) > 0) {
		return o[i];
	}
	o = document.getElementsByTagName("select");
	for (i = 0; i < o.length; i++)
		if (o[i].name.indexOf("$" + name) > 0) {
		return o[i];
	}
	return null;
}

function getCtrlVal(name) {
	var o = findCtrl(name);
	if(o===null)
	  return "";
	try {
		return o.value;
	} catch (ex) { }

	try {
		return o.options[o.selectedIndex].value;
	} catch (ex) { }
	return "";
}

function makeHttpRequest(url, callback_function, return_xml) {
	var http_request = false, timestamp = new Date();

	url += "&" + timestamp;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
			http_request.overrideMimeType('text/xml');
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}
	if (!http_request) {
		NECAlert("Unfortunatelly your browser doesn't support AJAX.");
		return false;
	}
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (return_xml)
					eval(callback_function + '(http_request.responseXML)');
				else
					eval(callback_function + '(http_request.responseText)');
			} else
				NECAlert("Holy strawberries Batman! We're in a jam! We need AJAX (" + http_request.status + ")");
		}
	};
	http_request.open('GET', url, true);
	http_request.send(null);

	return true;
}
function AJAXPOST(url, callback_function, params) {
	var http_request = false, timestamp = new Date(),
	    return_xml = false; 
	
	url += "&" + timestamp;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType)
			http_request.overrideMimeType('text/xml');
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}
	if (!http_request) {
		NECAlert("Unfortunatelly your browser doesn't support AJAX.");
		return false;
	}
	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (return_xml)
					eval(callback_function + '(http_request.responseXML)');
				else
					eval(callback_function + '(http_request.responseText)');
			} else
				NECAlert("Holy strawberries Batman! We're in a jam! We need AJAX (" + http_request.status + ")");
		}
	};
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", params.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(params);
	return true;
}
/*
var myIP = "72.74.255.249";

function GeoLocate()
{
   makeHttpRequest("http://api.hostip.info/get_html.php?ip="+myIP,"geocb",false);
}
function geocb(s)
{
  alert(s);
}
*/

function AddShipping() {
	var t = "",
	  d = "",
	  o = getEO("ddshipF1"),
	  v = "";

	if (o !== null) {
	   v = o.options[o.selectedIndex].value;
		d = getEO("shipdesc1").innerHTML + " " + o.options[o.selectedIndex].text;
		t += v + "|" + d + "~";
	}
	o = getEO("ddshipF2");
	if (o !== null) {
	   v = o.options[o.selectedIndex].value;
	   d = getEO("shipdesc2").innerHTML + " " + o.options[o.selectedIndex].text;
		t += v + "|" + d + "~";
	}
	o = getEO("ddshipF3");

	if (o !== null) {
	   v = o.options[o.selectedIndex].value;
	   d = getEO("shipdesc3").innerHTML + " " + o.options[o.selectedIndex].text;
		t += v + "|" + d + "~";
	}
	o = getEO("ddship");

	if (o !== null) {
	   v = o.options[o.selectedIndex].value;
	   t += v + "|Shipping for non subscription comics " + o.options[o.selectedIndex].text;
	}
		makeHttpRequest("commands.aspx?cmd=addship&param="+t, "AddShipCallBack", false);
 }

 function AddShipCallBack(s) {
 	//document.location = "checkout.aspx";
 	document.location = "ViewCart.aspx?checkout=y";
 }
 
function AddCart(id, itm, qty, prc, title, ship, store, tax) {
	show("Added");
	CenterInViewArea("Added");
	makeHttpRequest("commands.aspx?cmd=add&itm=" + itm + "&prc=" + prc + "&title=" + title.striptags() + "&qty=" + qty + "&ship=" + ship + "&store=" + store+"&tax="+tax, "AddCartCallback", false);
	return true;
}
function AddCartCallback(s) {
	getEO("cartstatus").innerHTML = s;
	setTimeout("hideAdded()", 2000);
}
function hideAdded() {
	hide("Added");
}
function DelItem(itm) {
	makeHttpRequest("commands.aspx?cmd=del&item=" + itm, "void", false);
	return true;
}
function UpdItem(itm, qty) {
	makeHttpRequest("commands.aspx?cmd=upd&item=" + itm + "&qty=" + qty, "void", false);
	return true;
}
function ClearCart() {
	makeHttpRequest("commands.aspx?cmd=clear", "void", false);
	return true;
}

function TransferCart() {
   alert("Oops. Not functional");
	//makeHttpRequest("commands.aspx?cmd=xfer", "void", false);
}

function necSearch(id) {
	SrchO.Search = getEV(id);
	SrchO.GoTo();
}
function ffastSearch(id) {
	window.location = "FFastSearch.aspx?q=" + getEV(id);
}
function tickSearch(id) {
	window.location = "TickSearch.aspx?q=" + getEV(id);
}

function Login(u, p) {
	if (u.length < 3)
		NECAlert("Please enter an email first");
	else
		makeHttpRequest("login.aspx?id=" + u + "&p=" + p, "getuserid", false);
}

function Join() {
	var errMsg = "",
	  str = getEV("tbEmail2");

	if ((str.indexOf(".") < 3) || (str.indexOf("@") < 1))
		errMsg += "Please enter a valid email address\r";

	if (getEV("tbPass1") != getEV("tbPass2"))
		errMsg += "Passwords don't match.\r";

	if (getEV("tbPass1").length < 4)
		errMsg += "Password too short. Please enter at least 4 digits or letters.\r";

	if (errMsg.length === 0) {
		makeHttpRequest("login.aspx?cmd=join&id=" + str + "&p=" + getEV("tbPass1"), "getuserid", false);
		hide('login');
	} else
		NECAlert(errMsg);
}

// Callback from logon & join
function getuserid(s) {
	if (s.length === 0) {
		NECAlert("Login failed.");
		return;
	}
	hide('login');
	l1.innerHTML = "Logged in as " + s;
}

function TellaFriend(f1, f2, f3) {
	if (getEV(f1).length === 0)
		NECAlert("Please enter at least 1 email address.");
	else
		makeHttpRequest("commands.aspx?cmd=tell&f1=" + getEV(f1) + "&f2=" + getEV(f2) + "&f3=" + getEV(f3), "void", false);
}

function tellcallback(s) {
	if (s == "OK") {
		NECAlert("Your friends have been invited.");
		hide('login');
	} else
		NECAlert(s);
}

function FixEmail() {
	var e = document.getElementsByTagName("a");
	for (var i = 0; i < e.length; i++)
		if (e[i].href.indexOf("mailto:") >= 0) {
		e[i].href = e[i].href + "@newenglandcomics.com";
		e[i].innerHTML = e[i].innerHTML + "@newenglandcomics.com";
	}
}

function Subscribe() {
	if (getEV("subemail").length < 4) {
		NECAlert("Please enter your email address");
		return;
	}
	makeHttpRequest("commands.aspx?cmd=sub&email=" + getEV("subemail") +
	"&p1=" + (getEO("substore").checked ? getEV("substore") : "") +
	"&p2=" + (getEO("subtick").checked ? getEV("subtick") : "") +
	"&p3=" + (getEO("subnew").checked ? getEV("subnew") : "") +
	"&action=Subscribe", "void", false);
	NECAlert("Your email address has been removed from the specified lists");
	hide("subscribe");
	hide("unsubscribe");
}

function unSubscribe() {
	if (getEV("subemail").length < 4) {
		NECAlert("Please enter your email address");
		return;
	}
	makeHttpRequest("commands.aspx?cmd=sub&email=" + getEV("usubemail") +
	"&p1=" + (getEO("usubstore").checked ? getEV("usubstore") : "") +
	"&p2=" + (getEO("usubtick").checked ? getEV("usubtick") : "") +
	"&p3=" + (getEO("usubnew").checked ? getEV("usubnew") : "") +
	"&action=Unsubscribe", "void", false);
	NECAlert("Your email address has been removed from the specified lists");
	hide("subscribe");
	hide("unsubscribe");
}

function SearchObj()
{
 this.Page = 1;
 this.Title = "";
 this.Search = "";
 this.m_Sort = "";
 this.m_tem = "";
 this.NewA = "";
 this.Location = "";
}

SearchObj.prototype.Go = function()
{
  if(this.Location.length>0)
    makeHttpRequest(this.Location+"?t=a&q="+this.Search+"&p="+this.Page+"&o="+this.m_Sort+"&n="+this.NewA+"&tem="+this.m_tem,"FillSearch",false);
}
SearchObj.prototype.GoTo = function() {
	this.Page = 1;
	window.location = this.Location + "?q="+this.Search + "&p=" + this.Page + "&o=" + this.m_Sort + "&n=" + this.NewA + "&tem=" + this.m_tem;
}
SearchObj.prototype.Next = function()
{
  this.Page++;
  this.Go();
}
SearchObj.prototype.Prev = function()
{
  if(this.Page>1)
    this.Page--;
  this.Go();
}
SearchObj.prototype.Sort = function(i)
{
  this.m_Sort = i;
  this.Go();
}
SearchObj.prototype.Template = function(t)
{
  if(t=="text" || t=="t")
    this.m_tem = "t";
  else
    this.m_tem = "";
  this.Go();
}
function SrchSort(i)
{
  SrchO.Sort(i);
}

// Callback from search
function FillSearch(s) {
  var nav = "<table width=\"95%\"><tr><td align=\"left\" style=\"padding-left:10px\"><u onclick=\"SrchO.Prev()\" style=\"cursor:pointer;\">Previous</u></td><td align=\"center\"><h2>" + SrchO.Title + "</h2></td><td align=\"right\"><u onclick=\"SrchO.Next()\" style=\"cursor:pointer;\">Next</u></td></tr></table>";
  
  getEO('MainContent').innerHTML = nav + s + nav;
  hide('Loading');
  document.body.style.cursor = 'auto';
}
function DetailFFast(cat) {
	makeHttpRequest("commands.aspx?cmd=detail&qry=qryDetail&id=" + cat, "DetailCallback", false);
}
function DetailTick(cat) {
	makeHttpRequest("commands.aspx?cmd=detail&qry=qryTickDetail&id=" + cat, "DetailCallback", false);
}
function DetailNEC(cat) {
	makeHttpRequest("commands.aspx?cmd=detail&qry=qryNECDetail&id=" + cat, "DetailCallback", false);
}
function DetailNewArrive(cat,t) {
    makeHttpRequest("commands.aspx?cmd=detail&qry=qryNewArriveDetail&id=" + cat+"&t="+t, "DetailCallback", false);
}
function DetailCallback(s) {
	if (s.length == 0)
		s = "<h2 style=\"text-align:center\"><br/>Sorry item temporarily unavailable. Please check back soon.<br/><br/>&nbsp;</h2>";
		
	getEO("buydetail").innerHTML = s;
	show("layerBuyDetail");
	CenterInViewArea("layerBuyDetail");
}

function PlayWav(n) {
	//http://joliclic.free.fr/html/object-tag/en/object-audio.html
	var o = getEO("Sound");
	o.src = "audio/" + n;
	o = getEO("SndEmb");
	o.src = "audio/" + n;
}

function ValidateEmail(source, clientside_arguments) {
	var errmsg = "",
	  o = findCtrl("Email");
	  
	if (o.value.length < 3) {
		errmsg += "Please enter an email address.<br/>";
		o.focus();
	}
	o = findCtrl("Phone");
    if(!validatePhone(o.value)) {
       errmsg+= "Please enter a valid phone # in case we need to contact you.<br/>";
       o.focus();
    }
	o = findCtrl("Fname");
	if (o.value.length < 2) {
		errmsg += "Please enter your first name.<br/>";
		o.focus();
	}
	o = findCtrl("Lname");
	if (o.value.length < 2) {
		errmsg += "Please enter your last name.<br/>";
		o.focus();
	}
	o = findCtrl("Address1");
	if (o.value.length < 3) {
		errmsg += "Please enter a street address.<br/>";
		o.focus();
	}
	o = findCtrl("City");
	if (o.value.length < 3) {
		errmsg += "Please enter a city.<br/>";
		o.focus();
	}
	o = findCtrl("country");
	if (o.value == "United States") {
		o = findCtrl("State");
		if (o.value.length < 2) {
			errmsg += "Please enter a state.<br/>";
			o.focus();
        }
        o = findCtrl("Zip");
	    if (o.value.length < 5) {
		  errmsg += "Please enter a valid zip code.<br/>";
		  o.focus();
	    }
	}
	
	o = getCtrlVal("CardType");
	if (o.length < 4) {
		errmsg += "Please select a payment method.<br/>";
		findCtrl("CardType").focus();
	}

	if (o == "MasterCard" ||
	o == "Visa" ||
	o == "Amex") {
		var n = findCtrl("CardNumber");
		if (!ValidateCC(n.value))
			errmsg += "Please enter a valid Credit card number";
	}
	o = findCtrl("Shipping2");
	if(o!== null) {
		if (o.value.length < 3) {
			errmsg += "Please select a shipping method for the suplemental comics<br/>";
			o.focus();
		}
	}
	o = findCtrl("cbAgree");
	if (o!==null && !o.checked)
		errmsg += "Check the box that you understand about the shipping.<br/>";

	o = findCtrl("cbCert");
	if (o !== null) {
		if (!o.checked)
			errmsg += "You need to certify you are 18 or older beause there is at least 1 mature item in your cart.<br/>";
	}

	if (errmsg.length > 0) {
		clientside_arguments.IsValid = false;
		NECAlert(errmsg);
	}
	else
		clientside_arguments.IsValid = true;
}

function ValidateFindBtn() {
	if (findCtrl("tbfndPass").value === "") {
		NECAlert("You must enter a password to lookup your information.");
		return false;
	}
	return true;
}

function getCountry() {
if(getEO("cbus").checked) return "us";
if(getEO("cbca").checked) return "ca";
return getCtrlVal("ddCountry");
}

function setShipping()
{
  makeHttpRequest("viewcart.aspx?country="+getCountry(), "RenderShipping", false);
}

function RenderShipping(s)
{
  getEO("dShip").innerHTML = s;
}

function NECAlert(Msg) {
	//PlayWav("holy_nightmare.wav");
	getEO("MsgBoxTxt").innerHTML = Msg;
	show("MessageBox");
	CenterInViewArea("MessageBox");
}

function NECMsgContent(Msg) {
	makeHttpRequest("commands.aspx?cmd=content&id=" + Msg, "NECMessage", false);
}

function NECShipping() {
	makeHttpRequest("commands.aspx?cmd=shipping", "NECMessage", false);
}
function NECDispPostage() {
	makeHttpRequest("commands.aspx?cmd=shipping", "NECDispPostagecb", false);
}
function NECDispPostagecb(s) {
	getEO("postagerates").innerHTML = s;
}
function NECMessage(Msg) {
	//PlayWav("holy_nightmare.wav");
	getEO("MsgBoxTxt2").innerHTML = Msg;
	show("MessageBox2");
	CenterInViewArea("MessageBox2");
}

function btnMouseover(o) {o.className="raised";}
function btnMouseout(o) {o.className="button";}
function btnMousedown(o) {o.className="pressed";}
function btnMouseup(o) {o.className="raised";}
function tag_text( tag ) {document.selection.createRange().pasteHTML("<"+tag+">"+document.selection.createRange().text+"</"+tag+">");}
function insert_text( txt ) {document.selection.createRange().text = txt;}

function onBeforeUnload() {
  show('Loading');
  CenterInViewArea("Loading");
  document.body.style.cursor = 'progress';
 }

var lastpos = -1,
  scrollheight = -1;

function scrollinit() {
	var o = document.getElementById("sbox");
	scrollheight = o.scrollHeight;
	o = document.getElementById("scroll1");
	document.getElementById("scroll2").innerHTML = o.innerHTML;
	window.setTimeout("smoothscroll()", 100);
}

function smoothscroll() {
	var o = document.getElementById("sbox");
	lastpos++;
	if (lastpos >= scrollheight)
		lastpos = 0;
	o.scrollTop = lastpos;
	window.setTimeout("smoothscroll()", 100);
}

function getOLeft(o) {
	var xPos = o.offsetLeft,
	   tempEl = o.offsetParent;
	while (tempEl !== null) {
		xPos += tempEl.offsetLeft;
		tempEl = tempEl.offsetParent;
	}
	return xPos;
}

function getOTop(o) {
	var yPos = o.offsetTop,
	   tempEl = o.offsetParent;
	while (tempEl !== null) {
		yPos += tempEl.offsetTop;
		tempEl = tempEl.offsetParent;
	}
	return yPos;
}

var menuLast = null, menuNew = null, menuSub = null, menutmr = null;

function menuMouseover(o, i) {
	try {
		setTimeout("menuDropdown()", 1000);
		menutmr = null;
		menuLast = menuNew;
		menuNew = document.getElementById("m" + i);
		menuNew.style.top = getOTop(o) + "px";
		menuNew.style.left = (getOLeft(o) + 100) + "px";
	} catch (e) { }
}

function submenuMouseover(o) {
	clearTimeout(menutmr);
}

function menuMouseout(o) {
    if (event.fromElement === o && (event.toElement === null || event.toElement.parentElement != o))
		menutmr = setTimeout("menuClose()", 1000);
}

function menuMouseclick(o) {
}

function menuDropdown() {
	menuClose();
	if (menuNew === null) return;

	try {
		menuNew.style.display = "";
	} catch (e) { }
}

function menuClose() {
  for (var i = 1; i< menuMax+1; i++)
    try {
	  document.getElementById("m" + i).style.display = "none";
	} catch (e) { }
}

// a=destination, b=session ID
function sendto(a, b) {
	var x = "";
	for (var i = b.length - 1; i >= 0; i--)
		x += b.charAt(i);
	return a + x;
}

function SendTo(i) {
  makeHttpRequest("commands.aspx?cmd=sendto&to=" + sendto(i, xSess) + "&f=" + getEO("from").value + "&m=" + getEO("msg").innerText, "sendtoCB", false);
 }
 function sendtoCB(s) {
 	if (s == "OK")
 		NECMessage("Message sent");
 		else
 			NECMessage("There was a problem sending your message.");
 }
 
function cpy(dst, src)
{
	findCtrl(dst).value = findCtrl(src).value;
}

function CopyAddress() {

	findCtrl("CardName").value = findCtrl("Fname").value + " " + findCtrl("Lname").value;
 	cpy("CCAddress", "Address1");
 	//cpy("CCAddress", "Address2");
 	cpy("CCCity", "City");
 	cpy("CCState", "State");
 	cpy("CCZip", "Zip");
 }

 function onKeypress() {
 	if (event.keyCode == 27)
 		hide("layerBuyDetail");}

function ShowTickPic(img,i) {
  getEO(i).src = "tickimg/"+img;
}

function ValidateCC(n) {
	var isValid = false,
	  ccCheckRegExp = /[^\d ]/;
	isValid = !ccCheckRegExp.test(n);
	if (!isValid)
		return false;

	var cardNumbersOnly = n.replace(/ /g, ""),
	   cardNumberLength = cardNumbersOnly.length,

	   numberProduct,
	   checkSumTotal = 0;

	for (digitCounter = cardNumberLength - 1; digitCounter >= 0; digitCounter--) {
		checkSumTotal += parseInt(cardNumbersOnly.charAt(digitCounter));
		digitCounter--;
		numberProduct = String((cardNumbersOnly.charAt(digitCounter) * 2));
		for (var productDigitCounter = 0; productDigitCounter < numberProduct.length; productDigitCounter++) {
			checkSumTotal += parseInt(numberProduct.charAt(productDigitCounter));
		}
	}
	isValid = (checkSumTotal % 10 === 0);
		return isValid;
	}

	function validatePhone(s) {
		if (s.length < 10)
			return false;
		for (var i = 0; i < s.length; i++) {
			if ("0123456789()- ".indexOf(s.substr(i, 1)) == -1)
				return false;
		}
		return true;
	}

	function Browser() {

		var ua, s, i;

		this.isIE = false;
		this.isNS = false;
		this.version = null;

		ua = navigator.userAgent;

		s = "MSIE";
		if ((i = ua.indexOf(s)) >= 0) {
			this.isIE = true;
			this.version = parseFloat(ua.substr(i + s.length));
			return;
		}

		s = "Netscape6/";
		if ((i = ua.indexOf(s)) >= 0) {
			this.isNS = true;
			this.version = parseFloat(ua.substr(i + s.length));
			return;
		}

		// Treat any other "Gecko" browser as NS 6.1.

		s = "Gecko";
		if ((i = ua.indexOf(s)) >= 0) {
			this.isNS = true;
			this.version = 6.1;
			return;
		}
	}

	var browser = new Browser();

	// Global object to hold drag information.

	var dragObj = new Object();

	function dragStart(id) {

		var el, x, y;

		dragObj.zIndex = 0;

		// If an element id was given, find it. Otherwise use the element being clicked on.
		//dragObj.elNode = o;
		if (id)
			dragObj.elNode = document.getElementById(id);
		else {
			if (browser.isIE)
				dragObj.elNode = window.event.srcElement;
			if (browser.isNS)
				dragObj.elNode = event.target;

			// If this is a text node, use its parent element.

			if (dragObj.elNode.nodeType == 3)
				dragObj.elNode = dragObj.elNode.parentNode;
		}

		// Get cursor position with respect to the page.

		if (browser.isIE) {
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		if (browser.isNS) {
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
		}

		// Save starting positions of cursor and element.

		dragObj.cursorStartX = x;
		dragObj.cursorStartY = y;
		dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
		dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 10);

		if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
		if (isNaN(dragObj.elStartTop)) dragObj.elStartTop = 0;

		// Update element's z-index.

		dragObj.elNode.style.zIndex = ++dragObj.zIndex;

		// Capture mousemove and mouseup events on the page.

		if (browser.isIE) {
			document.attachEvent("onmousemove", dragGo);
			document.attachEvent("onmouseup", dragStop);
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (browser.isNS) {
			document.addEventListener("mousemove", dragGo, true);
			document.addEventListener("mouseup", dragStop, true);
			event.preventDefault();
		}
	}

	function dragGo(event) {

		var x, y;

		// Get cursor position with respect to the page.

		if (browser.isIE) {
			x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
		}
		if (browser.isNS) {
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
		}

		// Move drag element by the same amount the cursor has moved.

		dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
		dragObj.elNode.style.top = (dragObj.elStartTop + y - dragObj.cursorStartY) + "px";

		if (browser.isIE) {
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (browser.isNS)
			event.preventDefault();
	}

	function dragStop(event) {

		// Stop capturing mousemove and mouseup events.

		if (browser.isIE) {
			document.detachEvent("onmousemove", dragGo);
			document.detachEvent("onmouseup", dragStop);
		}
		if (browser.isNS) {
			document.removeEventListener("mousemove", dragGo, true);
			document.removeEventListener("mouseup", dragStop, true);
		}
	}
