function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop]; 
}

function handle() { 
    return true;
}

function emailSignUp(form) {
	var email = trim(form.email.value);
	var first = trim(form.first.value);
	if(!isValidEmail(email)) {
		alert('Invalid Email');
	} else {
		var curl=""+document.location;
		var url;
		if(curl.indexOf('wishlist.com') != -1) {
			url='http://www.stacksandstacks.com/add-email-ajax.html';
		} else {
			url='/add-email-ajax.html';
		}
		url = url +'?first='+first+'&email='+email;
		getPage(url,function () {
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
				document.getElementById('emailSignUpHTML').innerHTML='<b><br><font color=red>Thank you for newsletter email sign up</font><br><br>';
			}		
		});
	}
	return false;
}

function isValidEmail(email) {
	var format = /^[0-9a-zA-Z]+([-_.\&]?[0-9a-zA-Z])*@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4}$)/;
	return format.test(email);
}

function trim(str) {
   	var s = str.replace(/^(\s)*/, '');
   	s = s.replace(/(\s)*$/, '');
 	return s;
}

function isValidZip(zip) {
	var format = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	return format.test(zip);
}


function closePopup() {
	document.body.removeChild(document.getElementById('dhtmlPopup'));
	return false;
}

function showPopupHTML(html) {
	var div = document.getElementById("dhtmlPopup");
	if(div != null) {
		div.lastChild.innerHTML=html;
	}
}

function showPopup(x, y, width, height, html) {
	var id = "dhtmlPopup";
	var div = document.getElementById(id);
	if(div == null) {
		div = document.createElement('DIV');
		div.id = id;
	}
		
	div.style.cssText='position:absolute; width:'+width+'px;height:'+height+'px;background-color:#FFFFFF;border:2px solid #999999;overflow:hidden;z-index:100';		
	div.style.left=(x)+'px';
	div.style.top=(y)+'px';
	var table = '<div><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="99%" bgcolor="#34878D" style="padding:1px;color:white;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" height="19" align="center"><a href="#" onclick="closePopup(); return false;" style="color:white">[CLOSE]</a></td><td width="1%" bgcolor="#34878D" valign="top" align="right"><a href="#" onclick="closePopup(); return false;"><img src="/images/close.gif" style="border:1px solid #999999"></a></td></tr></table></div><div style="width:'+(width-10)+'px;padding:5px; height:'+(parseInt(height)-30)+'px; scrollbar-face-color: #DEE3E7;scrollbar-highlight-color: #FFFFFF;scrollbar-shadow-color: #DEE3E7;scrollbar-3dlight-color: #D1D7DC;scrollbar-arrow-color: #006699;scrollbar-track-color: #EFEFEF; scrollbar-darkshadow-color: #98AAB1; overflow:auto;"></div>';
	div.innerHTML = table;
	document.body.appendChild(div);
	showPopupHTML(html);
	return false;
}

function openLiveChat () {
	window.open("http://www.stacksandstacks.com/help.html","none",'resizable=yes, toolbar=no, status=no, width=400, height=420');
	return false;
}

function openBML() {				
	window.open("https://www.securecheckout.billmelater.com/paycapture-content/fetch?hash=WGU35W2U&content=/bmlweb/np90drollingiw.html","none",'scrollbars=1,resizable=yes, toolbar=no, status=no, width=505, height=620');
	return false;
}

function openMoneta() {				
	window.open("/why-moneta.htm","none",'scrollbars=1,resizable=yes, toolbar=no, status=no, width=505, height=520');
	return false;
}


function showFirstTime() {
	var obj = document.getElementById('closetlaundry');
	if(obj != null) {	
		pos = findPos(obj);		
		showPopup(pos[0],pos[1],360,230,"<img src='/images/first-time.gif'>");
	}
}

function bookmark(title) {
	if (window.sidebar) {
		window.sidebar.addPanel(title,document.location,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite(document.location, title);
	} 
}

function displayVideo(url) {
	var scrolledX, scrolledY;
	var centerX, centerY;
  	var Xwidth=780;
	var Yheight=520;
	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;
	}
	if( self.pageYoffset ) {
		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;
	}	
	leftoffset = scrolledX + (centerX - Xwidth) / 2;
	topoffset = scrolledY + (centerY - Yheight) / 2;
	return getPage('/product/video-iframe.html?url='+url,function () {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
			showPopupWithGrayOut(leftoffset,topoffset,Xwidth,Yheight,xmlHttp.responseText);
		}
	});

}

window.onerror=handle;
var xmlHttp;

function GetXmlHttpObject(handler) { 
	var objXmlHttp=null;
	if (navigator.userAgent.indexOf("MSIE")>=0) {  
       	try {
        	objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			objXmlHttp.onreadystatechange=handler; 
			return objXmlHttp;
      	} catch(e) {
        	try {
          		objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
				objXmlHttp.onreadystatechange=handler; 
				return objXmlHttp;
        	} catch(e) {
				alert("Error. Scripting for ActiveX might be disabled"); 
          		return null;
        	}
		}
	} else {
	//if (navigator.userAgent.indexOf("Mozilla")>=0) {
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler;
		return objXmlHttp;
	} 
}

function getPage(url,mod) {
	xmlHttp=GetXmlHttpObject(mod);
	if(url.indexOf('?') != -1) {
		url = url+"&timestamp="+escape(""+(new Date()).toGMTString());
	} else {
		url = url+"?timestamp="+escape(""+(new Date()).toGMTString());
	}
	if(xmlHttp != null) {
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	}  
	return false; 
}

function formSubmitInPage(form,url,containerId) {
	var params="";
	for(var i=0;i<form.elements.length;i++) {
		params+=form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
	}
	getPagePost(url,params,function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
			var node = document.getElementById('dp_suggestion');
			node.innerHTML=xmlHttp.responseText;
		}
	});
	return false;		
}

function formSubmit(form,url) {
	var params="";
	for(var i=0;i<form.elements.length;i++) {
		params+=form.elements[i].name+"="+encodeURIComponent(form.elements[i].value)+"&";
	}
	getPagePost(url,params,function() {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
			showPopupHTML(xmlHttp.responseText);
		}
	});
	return false;	
}

function getPagePost(url,params,mod) {
	xmlHttp=GetXmlHttpObject(mod);
	if(url.indexOf('?') != -1) {
		url = url+"&timestamp="+(new Date()).toGMTString();
	} else {
		url = url+"?timestamp="+(new Date()).toGMTString();
	}
	if(xmlHttp != null) {
		xmlHttp.open("POST", url , true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");
		xmlHttp.send(params);
	}  
	return false; 
}

function registerOnClickPopup(url,id,posId,popupWidth,popupHeight,left,top) {
	var node = document.getElementById(id);
	if(node != null) {
		node.onclick = function() {
			return getPage(url,function () {
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
					var position = findPos(document.getElementById(posId));
					showPopup(position[0]+left,position[1]+top,popupWidth,popupHeight,xmlHttp.responseText);
				}
			});
		}
	}
}
function repositionUp() {
	var node = document.getElementById('scrollId');
	var h = parseFloat(node.firstChild.style.height);
	var top = parseFloat(node.style.top);
	var child;
	while (top < 0) {;
		child = node.firstChild;
		node.removeChild(child);
		node.appendChild(child);
		top = top+h;
	}
	node.style.top=top+'px';
}
function repositionDown() {
	var node = document.getElementById('scrollId');
	var h = parseFloat(node.firstChild.style.height);
	var top = parseFloat(node.style.top);
	var height = parseFloat(node.style.height);
	height = height+top-h*3;
	var child;
	while (height > 0) {
		child = node.lastChild;
		node.removeChild(child);
		node.insertBefore(child,node.firstChild);
		height -= h;
		top -=h;
	}
	node.style.top=top+'px';
}
function scrollingUp() {
	var node = document.getElementById('scrollId');
	var h = parseFloat(node.firstChild.style.height);
	var top = parseFloat(node.style.top);
	var height = parseFloat(node.style.height);
	height -=h*3;
	if(height > h*3) height=-h*3;
	else height = -height;
	node.style.top=(top-h/2)+'px';
	if(top-h/2 > height) {
		setTimeout("scrollingUp()",100);
	}
}
function scrollingDown() {
	var node = document.getElementById('scrollId');
	var h = parseFloat(node.firstChild.style.height);
	var top = parseFloat(node.style.top);
	var height = parseFloat(node.style.height);
	var bottom = height+top-h*3;
	height -=h*3;
	if(height > h*3) height=h*3;
	node.style.top=(top+h/2)+'px';
	if(bottom+h/2 < height) {
		setTimeout("scrollingDown()",100);
	}
}
function scrollUp() {
	repositionUp();
	scrollingUp();
}
function scrollDown() {
	repositionDown();
	scrollingDown();
}
function grayOut(vis, options) {
  // Pass true to gray out screen, false to ungray
  // options are optional.  This is a JSON object with the following (optional) properties
  // opacity:0-100         // Lower number = less grayout higher = more of a blackout 
  // zindex: #             // HTML elements with a higher zindex appear on top of the gray out
  // bgcolor: (#xxxxxx)    // Standard RGB Hex color code
  // grayOut(true, {'zindex':'50', 'bgcolor':'#0000FF', 'opacity':'70'});
  // Because options is JSON opacity/zindex/bgcolor are all optional and can appear
  // in any order.  Pass only the properties you need to set.
  var options = options || {}; 
  var zindex = options.zindex || 50;
  var opacity = options.opacity || 70;
  var opaque = (opacity / 100);
  var bgcolor = options.bgcolor || '#000000';
  var dark=document.getElementById('darkenScreenObject');
  if (!dark) {
    // The dark layer doesn't exist, it's never been created.  So we'll
    // create it here and apply some basic styles.
    // If you are getting errors in IE see: http://support.microsoft.com/default.aspx/kb/927917
    var tbody = document.getElementsByTagName("body")[0];
    var tnode = document.createElement('div');           // Create the layer.
        tnode.style.position='absolute';                 // Position absolutely
        tnode.style.top='0px';                           // In the top
        tnode.style.left='0px';                          // Left corner of the page
        tnode.style.overflow='hidden';                   // Try to avoid making scroll bars            
        tnode.style.display='none';                      // Start out Hidden
        tnode.id='darkenScreenObject';                   // Name it so we can find it later
    tbody.appendChild(tnode);                            // Add it to the web page
    dark=document.getElementById('darkenScreenObject');  // Get the object.
  }
  if (vis) {
    // Calculate the page width and height 
    if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
        var pageWidth = document.body.scrollWidth+'px';
        var pageHeight = document.body.scrollHeight+'px';
    } else if( document.body.offsetWidth ) {
      var pageWidth = document.body.offsetWidth+'px';
      var pageHeight = document.body.offsetHeight+'px';
    } else {
       var pageWidth='100%';
       var pageHeight='100%';
    }   
    //set the shader to cover the entire page and make it visible.
    dark.style.opacity=opaque;                      
    dark.style.MozOpacity=opaque;                   
    dark.style.filter='alpha(opacity='+opacity+')'; 
    dark.style.zIndex=zindex;        
    dark.style.backgroundColor=bgcolor;  
    dark.style.width= pageWidth;
    dark.style.height= pageHeight;
    dark.style.display='block';                          
  } else {
     dark.style.display='none';
  }
}
function showPopupWithGrayOut(x, y, width, height, html) {
	var id = "dhtmlPopup";
	var div = document.getElementById(id);
	if(div == null) {
		div = document.createElement('DIV');
		div.id = id;
	}
		
	div.style.cssText='position:absolute; width:'+width+'px;height:'+height+'px;background-color:#FFFFFF;border:2px solid #999999;overflow:hidden;z-index:100';		
	div.style.left=(x)+'px';
	div.style.top=(y)+'px';
	var table = '<div><table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="99%" bgcolor="#34878D" style="padding:1px;color:white;font-family:Arial, Helvetica, sans-serif;font-size:12px;font-weight:bold" height="19" align="center"><a href="#" onclick="closePopupWithGrayOut(); return false;" style="color:white">[CLOSE]</a></td><td width="1%" bgcolor="#34878D" valign="top" align="right"><a href="#" onclick="closePopupWithGrayOut(); return false;"><img src="/images/close.gif" style="border:1px solid #999999"></a></td></tr></table></div><div style="width:'+(width-10)+'px;padding:5px; height:'+(parseInt(height)-30)+'px; scrollbar-face-color: #DEE3E7;scrollbar-highlight-color: #FFFFFF;scrollbar-shadow-color: #DEE3E7;scrollbar-3dlight-color: #D1D7DC;scrollbar-arrow-color: #006699;scrollbar-track-color: #EFEFEF; scrollbar-darkshadow-color: #98AAB1; overflow:auto;"></div>';
	div.innerHTML = table;
	document.body.appendChild(div);
	showPopupHTMLWithGrayOut(html);
	return false;
}

function closePopupWithGrayOut() {
	document.body.removeChild(document.getElementById('dhtmlPopup'));
	grayOut(false);
	return false;
}

function showPopupHTMLWithGrayOut(html) {
	var div = document.getElementById("dhtmlPopup");
	if(div != null) {
		div.lastChild.innerHTML=html;
	}
	grayOut(true);
}


function registerOnClickPopupGrayOut(url,id,posId,popupWidth,popupHeight,left,top) {
	var node = document.getElementById(id);
	if(node != null) {
		node.onclick = function() {
			return getPage(url,function () {
				if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
					var position = findPos(document.getElementById(posId));
					showPopup(position[0]+left,position[1]+top,popupWidth,popupHeight,xmlHttp.responseText);
				}
			});
		}
	}
}menuPositionLeft = new Array();
menuPositionLeft['dropmenu1'] = 4; 
menuPositionLeft['dropmenu2'] = 3; 
menuPositionLeft['dropmenu3'] = 4; 
menuPositionLeft['dropmenu4'] = 4; 
menuPositionLeft['dropmenu5'] = 8; 
menuPositionLeft['dropmenu6'] = 4; 
menuPositionLeft['dropmenu7'] = 4; 
menuPositionLeft['dropmenu8'] = 4; 
menuPositionLeft['dropmenu9'] = 5; 
menuPositionLeft['dropmenu10'] = 138; 
menuPositionLeft['dropmenu11'] = 4; 
menuPositionLeft['dropmenu12'] = 228; 
menuPositionLeft['dropmenu13'] = 4; 

var cssdropdown={
	disappeardelay: 250,
	disablemenuclick: false,
	enableswipe: 1,
	currentCat: null,
	dropmenuobj: null, ie: document.all, firefox: document.getElementById&&!document.all, swipetimer: undefined, bottomclip:0,
		getposOffset:function(what, offsettype) {
			var totaloffset=(offsettype=="left")? what.offsetLeft : what.offsetTop;
			var parentEl=what.offsetParent;
			while (parentEl!=null) {
				totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
				parentEl=parentEl.offsetParent;
			}
			return totaloffset;
		},

		swipeeffect:function() {
			if (this.bottomclip<parseInt(this.dropmenuobj.offsetHeight)) {
				this.bottomclip+=10+(this.bottomclip/10);
				this.dropmenuobj.style.clip="rect(0 auto "+this.bottomclip+"px 0)";
			} else {
				return;
			}
			this.swipetimer=setTimeout("cssdropdown.swipeeffect()", 10);
		},

		showhide:function(obj, e) {
			if (this.ie || this.firefox) {
				this.dropmenuobj.style.left=this.dropmenuobj.style.top="-500px";
			}
			if (e.type=="click" && obj.visibility==hidden || e.type=="mouseover") {
				if (this.enableswipe==1) {
					if (typeof this.swipetimer!="undefined") {
						clearTimeout(this.swipetimer);
					}
					obj.clip="rect(0 auto 0 0)";
					this.bottomclip=0;
					this.swipeeffect();
				}
				obj.visibility="visible";
			} else if (e.type=="click") {
				obj.visibility="hidden";
			}
		},

		dropit:function(obj, e, dropmenuID) {
			if (this.dropmenuobj!=null)  {
				this.dropmenuobj.style.visibility="hidden";
				this.currentCat.style.color='#000000';
			}
			this.clearhidemenu();
			if (this.ie||this.firefox) {
				obj.onmouseout=function() {
					cssdropdown.delayhidemenu();
				}
				obj.onclick=function() { 
					return !cssdropdown.disablemenuclick;
				} 
				this.dropmenuobj=document.getElementById(dropmenuID);
				this.dropmenuobj.onmouseover=function() {
					cssdropdown.clearhidemenu();
				}
				this.dropmenuobj.onmouseout=function(e) { 
					cssdropdown.dynamichide(e);
				}
				this.dropmenuobj.onclick=function() { 
					cssdropdown.delayhidemenu();
				}
				this.showhide(this.dropmenuobj.style, e);
				this.dropmenuobj.x=this.getposOffset(obj, "left");
				this.dropmenuobj.y=this.getposOffset(obj, "top");
				this.dropmenuobj.style.left=(this.dropmenuobj.x - menuPositionLeft[dropmenuID])+'px';
				this.dropmenuobj.style.top=(this.dropmenuobj.y+18)+'px';
			}
		},

		contains_firefox:function(a, b) {
			while (b.parentNode) {
				if ((b = b.parentNode) == a) {
					return true;
				}
			}
			return false;
		},

		dynamichide:function(e) {
			var evtobj=window.event? window.event : e;
			if(this.ie&&!this.dropmenuobj.contains(evtobj.toElement)) {
				this.delayhidemenu();
			} else if (this.firefox&&e.currentTarget!= evtobj.relatedTarget&& !this.contains_firefox(evtobj.currentTarget, evtobj.relatedTarget)) {
				this.delayhidemenu();
			}
		},

		delayhidemenu:function() {
			this.delayhide=setTimeout("cssdropdown.dropmenuobj.style.visibility='hidden';cssdropdown.currentCat.style.color='#000000';",this.disappeardelay);
		},

		clearhidemenu:function() {
			if(this.delayhide!="undefined") {
				clearTimeout(this.delayhide);
			}
		},

		startchrome:function() {
			for (var ids=0; ids<arguments.length; ids++) {	
				var menuitems=document.getElementById(arguments[ids]).getElementsByTagName("a");
				for (var i=0; i<menuitems.length; i++) {
					if (menuitems[i].getAttribute("rel")) {
						var relvalue=menuitems[i].getAttribute("rel");
						menuitems[i].onmouseover=function(e) {
							var event=typeof e!="undefined"? e : window.event;
							cssdropdown.dropit(this,event,this.getAttribute("rel"));
							this.style.color="#cd1515";
							cssdropdown.currentCat=this;
						}
					}
				}
			}
		}
}