// TOOLTIPS - PRODUCT DISPLAY //
// this function handles the display and position of popup information in the product display jsp //

var offsetxpoint=12
var offsetypoint=23
var ie=document.all
var ns6=document.getElementById && !document.all
var enabletip=false
var hiresURL = "http://www.tandus.com/Tandus/Vstudio/visualizer.html"

function ietruebody() {
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function tooltip(text,color,width) {
	if (ns6||ie){
		if (typeof width!="undefined") document.getElementById("tooltip").style.width=width+"px"
		if (typeof color!="undefined" && color!="") document.getElementById("tooltip").style.backgroundColor=color
		document.getElementById("tooltip").innerHTML=text
		enabletip=true
		return false
	}
}

function positiontip(e) {
	if (enabletip) {
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		var rightedge=ie&&!window.opera? ietruebody().clientWidth-event.clientX-offsetxpoint : window.innerWidth-e.clientX-offsetxpoint-20
		var bottomedge=ie&&!window.opera? ietruebody().clientHeight-event.clientY-offsetypoint : window.innerHeight-e.clientY-offsetypoint-20
		var leftedge=(offsetxpoint<0)? offsetxpoint*(-1) : -1000
		if (rightedge<document.getElementById("tooltip").offsetWidth)
		document.getElementById("tooltip").style.left=ie? ietruebody().scrollLeft+event.clientX-document.getElementById("tooltip").offsetWidth+"px" : window.pageXOffset+e.clientX-document.getElementById("tooltip").offsetWidth+"px"
		else if (curX<leftedge)
		document.getElementById("tooltip").style.left="5px"
		else
		document.getElementById("tooltip").style.left=curX+offsetxpoint+"px"
		if (bottomedge<document.getElementById("tooltip").offsetHeight)
		document.getElementById("tooltip").style.top=ie? ietruebody().scrollTop+event.clientY-document.getElementById("tooltip").offsetHeight-offsetypoint+"px" : window.pageYOffset+e.clientY-document.getElementById("tooltip").offsetHeight-offsetypoint+"px"
	else
	document.getElementById("tooltip").style.top=curY+offsetypoint+"px"
	document.getElementById("tooltip").style.visibility="visible"
	}
}

function hidetooltip() {
	if (ns6||ie){
	enabletip=false
	document.getElementById("tooltip").style.visibility="hidden"
	document.getElementById("tooltip").style.left="-1000px"
	document.getElementById("tooltip").style.backgroundColor=''
	document.getElementById("tooltip").style.width=''
	}
}

document.onmousemove=positiontip

// MULTIPLE INITIALIZATIONS - ALL PAGES //
// each page calls this function to aid in the multiple onload initializations, the top two dropdown scrollbars are initialized in this file, for other pages (quick order and product page) additional onload events are defined  //

function initOn() {
	window.onload = function() {
		for(var i = 0;i < inititems.length;i++)
		eval(inititems[i]);
	}
}

function initAdd(func) {
	inititems[inititems.length] = func;
}

var inititems = new Array();

initAdd('scroller.init()'); 
initAdd('sscroller.init()');
initOn();

// ENLARGE THUMBNAIL - PRODUCT PAGE //
// this function loads the large product image into the room view container //

function changeThumb(id,image) {
	if (document.getElementById) {
		document.getElementById(id).innerHTML = "<img src='"+image+"' class='preimage' width='288' height='190' alt='' />";
	} 
	else if (document.all) {
		document.all[id].innerHTML = image;
	}
}

// CHANGE THUMBNAIL DESCRIP - PRODUCT PAGE //
// this function loads product descriptions into the room view container //

function changeThumbDescrip(id,value) {
	if (document.getElementById) {
		document.getElementById(id).innerHTML = value;
	} 
	else if (document.all) {
		document.all[id].innerHTML = value;
	}
}

// CHANGE HIRESLINK - PRODUCT PAGE //
// this function loads the hires link into the room view container //

function changeHiResLink(id,value) {
    if (value.substring(23) =='00000' || value.substring(23) =='36X36') {
      value = value.substring(0,14);
    }  
	if (document.getElementById) {
		document.getElementById(id).innerHTML = "&nbsp;&nbsp;<a href='"+hiresURL+value+"'>High Resolution Image</a>";
	} 
	else if (document.all) {
		document.all[id].innerHTML = "<a href='"+hiresURL+value+"'>High Resolution Image</a>";
	}
	 
}

// RESET HIRESLINK - PRODUCT PAGE //
// this function loads the hires link into the room view container //

function resetHiResLink(id,value) {
	if (document.getElementById) {
		document.getElementById(id).innerHTML = "&nbsp;&nbsp;<a href='"+hiresURL+"?styleId="+value+"'>High Resolution Image</a>";
	} 
	else if (document.all) {
		document.all[id].innerHTML = "<a href='http://hires.com/?style="+value+"'>High Resolution Image</a>";
	}
}



// UPDATE QUANTITY - PRODUCT PAGE //
// this function handles the actions after a user selects a quantity in the dropdown, the quantity is updated in the quantity box and the dropdown is hidden onclick //

function updateQty(Id,update,current) {
  var o = document.getElementById(Id);
  	if (o) {
  		document.getElementById(Id).innerHTML = update;
  		document.getElementById(current).style.visibility = "hidden";
  }
}

// DIV VISIBILITY - ALL PAGES //
// this code handles the visiblity of the top navigation dropdown menus //

function toggleDiv(id,flagit) {
	if (flagit=="1") {
		if (document.layers) document.layers[''+id+''].visibility = "show"
		else if (document.all) document.all[''+id+''].style.visibility = "visible"
		else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "visible"
	}
	else if (flagit=="0") {
		if (document.layers) document.layers[''+id+''].visibility = "hide"
		else if (document.all) document.all[''+id+''].style.visibility = "hidden"
		else if (document.getElementById) document.getElementById(''+id+'').style.visibility = "hidden"
	}
}

// SCROLLBAR DISPLAY - ALL PAGES //

// this javascript handles the collection dropdown scolling //

var scroller = {
  init:   function() {
    scroller.docH = document.getElementById("content").offsetHeight;
    scroller.contH = document.getElementById("container").offsetHeight;
    scroller.scrollAreaH = document.getElementById("scrollArea").offsetHeight;
    scroller.scrollH = (scroller.contH * scroller.scrollAreaH) / scroller.docH;
    document.getElementById("scroller").style.height = Math.round(scroller.scrollH) + "px";
    scroller.scrollDist = Math.round(scroller.scrollAreaH-scroller.scrollH);
    Drag.init(document.getElementById("scroller"),document.getElementById("content"),null,0,0,0,scroller.scrollDist);
    document.getElementById("scroller").onDrag = function (x,y) {
    	var scrollY = parseInt(document.getElementById("scroller").style.top);
    	var docY = 0 - (scrollY * (scroller.docH - scroller.contH) / scroller.scrollDist);
    	document.getElementById("content").style.top = docY + "px";
    }
  }
}

// this javascript handles the mini-cart scolling //

var cscroller = {
  init:   function() {
    cscroller.docH = document.getElementById("ccontent").offsetHeight;
    cscroller.contH = document.getElementById("ccontainer").offsetHeight;
    cscroller.scrollAreaH = document.getElementById("cscrollArea").offsetHeight;
    cscroller.scrollH = (cscroller.contH * cscroller.scrollAreaH) / cscroller.docH;
    if (cscroller.contH >= cscroller.scrollH) {
		document.getElementById("cscroller").style.visibility = "visible";
		document.getElementById("cscroller").style.height = Math.round(cscroller.scrollH) + "px";
    	cscroller.scrollDist = Math.round(cscroller.scrollAreaH-cscroller.scrollH);
    	Drag.init(document.getElementById("cscroller"),document.getElementById("ccontent"),null,0,0,0,cscroller.scrollDist);
	}
	else {
		document.getElementById("cscroller").style.visibility = "hidden";
	}
    document.getElementById("cscroller").onDrag = function (x,y) {
    	var scrollY = parseInt(document.getElementById("cscroller").style.top);
    	var docY = 0 - (scrollY * (cscroller.docH - cscroller.contH) / cscroller.scrollDist);
    	document.getElementById("ccontent").style.top = docY + "px";
    }
  }
}

// this javascript handles the color box scolling on the quick order page //

var colorscroller = {
  init:   function() {
    colorscroller.docH = document.getElementById("colorcontent").offsetHeight;
    colorscroller.contH = document.getElementById("colorcontainer").offsetHeight;
    colorscroller.scrollAreaH = document.getElementById("colorscrollArea").offsetHeight;
    colorscroller.scrollH = (colorscroller.contH * colorscroller.scrollAreaH) / colorscroller.docH;
    if (colorscroller.contH >= colorscroller.scrollH) {
		document.getElementById("colorscroller").style.visibility = "visible";
		document.getElementById("colorscroller").style.height = Math.round(colorscroller.scrollH) + "px";
    	colorscroller.scrollDist = Math.round(colorscroller.scrollAreaH-colorscroller.scrollH);
    	Drag.init(document.getElementById("colorscroller"),document.getElementById("colorcontent"),null,0,0,0,colorscroller.scrollDist);
    }
	else {
		document.getElementById("colorscroller").style.visibility = "hidden";
	}
    document.getElementById("colorscroller").onDrag = function (x,y) {
    	var scrollY = parseInt(document.getElementById("colorscroller").style.top);
    	var docY = 0 - (scrollY * (colorscroller.docH - colorscroller.contH) / colorscroller.scrollDist);
    	document.getElementById("colorcontent").style.top = docY + "px";
    }
  }
}

// this javascript handles the quick cart scolling on the product page and this quick order page //

var qcscroller = {
  init:   function() {
    qcscroller.docH = document.getElementById("qccontent").offsetHeight;
    qcscroller.contH = document.getElementById("qccontainer").offsetHeight;
    qcscroller.scrollAreaH = document.getElementById("qcscrollArea").offsetHeight;
    qcscroller.scrollH = (qcscroller.contH * qcscroller.scrollAreaH) / qcscroller.docH;
    
    if (qcscroller.contH >= qcscroller.scrollH) {
		document.getElementById("qcscroller").style.visibility = "visible";
		document.getElementById("qcscroller").style.height = Math.round(qcscroller.scrollH) + "px";
	    qcscroller.scrollDist = Math.round(qcscroller.scrollAreaH-qcscroller.scrollH);
	    Drag.init(document.getElementById("qcscroller"),document.getElementById("qccontent"),null,0,0,0,qcscroller.scrollDist);
	} else {
		document.getElementById("qcscroller").style.visibility = "hidden";
	}
	
    document.getElementById("qcscroller").onDrag = function (x,y) {
	    var scrollY = parseInt(document.getElementById("qcscroller").style.top);
	    var docY = 0 - (scrollY * (qcscroller.docH - qcscroller.contH) / qcscroller.scrollDist);
	    document.getElementById("qccontent").style.top = docY + "px";
    }
  }
}

// this javascript handles the style scolling in the top navigation dropdown //

var sscroller = {
  init:   function() {
    sscroller.docH = document.getElementById("scontent").offsetHeight;
    sscroller.contH = document.getElementById("scontainer").offsetHeight;
    sscroller.scrollAreaH = document.getElementById("sscrollArea").offsetHeight;
    sscroller.scrollH = (sscroller.contH * sscroller.scrollAreaH) / sscroller.docH;
	document.getElementById("sscroller").style.height = Math.round(sscroller.scrollH) + "px";
    sscroller.scrollDist = Math.round(sscroller.scrollAreaH-sscroller.scrollH);
    Drag.init(document.getElementById("sscroller"),document.getElementById("scontent"),null,0,0,0,sscroller.scrollDist);
    document.getElementById("sscroller").onDrag = function (x,y) {
    var scrollY = parseInt(document.getElementById("sscroller").style.top);
    var docY = 0 - (scrollY * (sscroller.docH - sscroller.contH) / sscroller.scrollDist);
    document.getElementById("scontent").style.top = docY + "px";
    }
  }
}

// this javascript handles the style scolling in the quantity menu in the "add to cart" pane //

var qscroller = {
  MAX_HEIGHT: 106,  // maximum height of the quantity dropdown
  
  init: function() {
    // height of the list of entries
    this.docH = document.getElementById("qquantity").offsetHeight;
	document.getElementById('qmenuAnchor').style.height = (this.MAX_HEIGHT < this.docH ? this.MAX_HEIGHT : this.docH) + 'px';
	document.getElementById('qscrollArea').style.height = (this.MAX_HEIGHT < this.docH ? this.MAX_HEIGHT : this.docH) + 'px';
	    
    // height of the list container
    this.contH = document.getElementById("qcontainer").offsetHeight;
    
    // height of the scrollbar
    this.scrollAreaH = document.getElementById("qscrollArea").offsetHeight;
    
    // height of the scroll handle (i.e., what you drag to scroll)
    this.scrollH = (this.contH * this.scrollAreaH) / this.docH;
    
    // set the size of the DOM element that is the scroll handle
	document.getElementById("qscroller").style.height = Math.round(this.scrollH) + "px";
	
	// height of the scrollbar not occupied by the scroll handle
    this.scrollDist = Math.round(this.scrollAreaH - this.scrollH);
    
    // initialize the dragging behavior
    Drag.init(document.getElementById("qscroller"), document.getElementById("qquantity"), null, 0, 0, 0, qscroller.scrollDist);
    
    // set the drag handler
    document.getElementById("qscroller").onDrag = function (x,y) {
      // top of the scroll handle
	  var scrollY = parseInt(document.getElementById("qscroller").style.top);
	  
	  // new height of the entry list
	  var docY = 0 - (scrollY * (qscroller.docH - qscroller.contH) / qscroller.scrollDist);
	    
	  // move the entry list up/down
	  document.getElementById("qquantity").style.top = docY + "px";
    }
  }
}

// this javascript calculates the corresponding position of the srollable content in relationship to the scrollbar //

var Drag = {
	obj : null,
	init : function(o,m,oRoot,minX,maxX,minY,maxY,bSwapHorzRef,bSwapVertRef,fXMapper,fYMapper)
	{	
		o.style.top = 0 + "px";
		m.style.top = 0 + "px";
		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;
	}
};

// QUANITITY VALIDATION - PRODUCT PAGE/QUICK ORDER PAGE //
// this function checks for a valid quanitity between 0 and the maximum value specified be the 'terms and consitions', if a number is entered outside that range, but not above, it is returned to the original value. if a number is entered above the maximum value passed through 'terms and conditions' then the maxmimum value allowed for that item is displayed //

function validateqty(Id,qty,max) {
	var authentry=/^\d{1,2}$/
	var qty2;
	var max2;
	var Id2;
	
	qty2 = parseInt(qty);
	max2 = parseInt(max);
	Id2 = parseInt(Id.value);
	
	if (Id.value.search(authentry) == -1) {
		Id2 = qty2
 	}

	if (Id2 == '') {
		Id.value = qty2
 	}

 	if (Id2 > max2) {
 		Id.value = max2
 	}
}

// CLEAR QUANITITY - PRODUCT PAGE/QUICK ORDER PAGE //
// this function initially clears the value of quantity boxes on the product and quick order pages when a user first clicks the input area //

function clearqty(Id) {
	Id.value = ''
}

// DYNAMIC AJAX UPDATE - PRODUCT PAGE/QUICK ORDER PAGE //
// loads external content dynamically into a div container //

function dynamicupdate2(content,target,initialize) { 
	try {
		document.getElementById(target).innerHTML = content;	
		return;
	} catch (e) {}
}

function dynamicupdate(content,target,initialize) { 
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest():
		new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch (e) {}
	xmlhttp.onreadystatechange = function() {
 		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
			document.getElementById(target).innerHTML = xmlhttp.responseText;
			if (initialize == 'cscroller') {
				cscroller.init();
			}
			else if (initialize == 'colorscroller') {
				colorscroller.init();
			}
			else if (initialize == 'stylescroller') {
				stylescroller.init();
			}
			else if (initialize == 'qcscroller') {
				qcscroller.init();
			}
			else if ('qscroller' == initialize) {
				qscroller.init();
			}
  		}
 	}
	xmlhttp.open("GET", content);
	xmlhttp.send(null);
}

// ADD ITEM - PRODUCT PAGE/QUICK ORDER PAGE //
// this code checks whether the item is currently present in the order or not and processes the actions on the font-end and the back-end to add the item to the cart and order //

function addItem(itemnum,color,parent,qty,storeid,target,initialize,add,remove,quickorder,max) {
	if (quickorder) {
		document.getElementsByTagName("body").item(0).style.cursor="wait";
	} else {
		document.getElementById("hcontainer").style.cursor="wait";
	}

	setTimeout("addItem2('"+itemnum+"','"+escape(color)+"','"+parent+"','"+qty+"','"+storeid+"','"+target+"','"+initialize+"','"+add+"','"+remove+"','"+quickorder+"','"+max+"')",50);

}

function addItem2(itemnum,fmtcolor,parent,qty,storeid,target,initialize,add,remove,quickorder,max) {

	var color = unescape(fmtcolor);
	var itemNumQty = itemnum + "_qty";
	var o = document.getElementById(itemNumQty);
	
	if (o) {
		var orderItemId = o.getAttribute('name');
		var updURL = 'OrderItemUpdate';
		var updData = '';
		if (color == '36X36 Sample' || color == '36x36 Sample') {
			document.getElementById(itemnum).innerHTML = "<input class='qtyform' size='2' maxlength='2' id='"+itemnum+"_qty' name='"+orderItemId+"' value='"+qty+"' onfocus='clearqty(this);' onblur='validateqty(this,\""+qty+"\",\""+max+"\");' />";
			updData = 'URL=&orderItemId_1=' + orderItemId + '&quantity_1=1';
			updateItem(updURL,updData,null,quickorder);
		}
		else {
			document.getElementById(itemnum).innerHTML = "<input class='qtyform' size='2' maxlength='2' id='"+itemnum+"_qty' name='"+orderItemId+"' value='"+qty+"' onfocus='clearqty(this);' onblur='validateqty(this,\""+qty+"\",\""+max+"\");' />";
			updData = 'URL=&orderItemId_1=' + orderItemId + '&quantity_1=' + qty;
			updateItem(updURL,updData,null,quickorder);
		}
	}
	else {
		
		addition = add + 'quantity=' + qty;
		var addReq = new addXMLReq();
		addReq.submit(addition, quickorder, color, parent, qty, storeid, max, initialize, remove, itemnum);
		
		if (initialize == 'cscroller') {
			cscroller.init();
		}
		else if (initialize == 'qcscroller') {
			qcscroller.init();
		}
		
	}
	
}

function addXMLReq () { }

addXMLReq.prototype.submit = function(_url, _quickorder, _color, _parent, _qty, _storeid, _max, _initialize, _remove, _itemnum){

	this.quickOrder = _quickorder;
	this.color = _color;
	this.parent = _parent;
	this.qty = _qty;
	this.max = _max;
	this.initialize = _initialize;
	this.remove = _remove;
	this.itemnum = _itemnum;
	this.storeid = _storeid;

	//this.url = _url;	
	this.url = 'OrderItemAdd';
	this.addData = 'catEntryId=' + this.itemnum + '&quantity=' + this.qty + '&storeId=' + this.storeid + '&URL=OrderItemAddRespView';
	this.status = null;
	this.inProgress = true;
	this.isComplete = false;
		
    var _this = this;
    
    this.req = false;
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			this.req = new XMLHttpRequest();
       	} 
       	catch(e) {
			this.req = false;
      	}
    } 
    else if(window.ActiveXObject) {
     	try {
       		this.req = new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch(e) {
       		try {
       			this.req = new ActiveXObject("Microsoft.XMLHTTP");
       		} 
       		catch(e) {
       			this.req = false;
       		}
		}
 	}
 	
 	// Make request asynchronous if browser is Firefox due to onreadystatechange bug found in Firefox when making sychronous requests.
 	var agent = navigator.userAgent;
 	var agentIdx = agent.search(/gecko/i);
 	if (agentIdx != -1) {	
 		this.req.open("POST", this.url, true);
 	} else {
 		this.req.open("POST", this.url, false);
 	}
 	
 	this.req.onreadystatechange = function() { _this.updateCart(); };
 	this.req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
   	this.req.send(this.addData);
}

addXMLReq.prototype.updateCart = function() {
	
	if (this.req.readyState == 4) {
		if (this.req.status == 200) {
				
			this.xmlDoc;
			this.orderItemIdElements; 
			this.xmlOrderItemId;
			this.orderItemId;
	 		
	 		try {
		 		this.xmlDoc = this.req.responseXML;
				this.orderItemIdElements = this.xmlDoc.getElementsByTagName("order-item-id"); 
				this.xmlOrderItemId = this.orderItemIdElements [0];
				this.orderItemId = this.xmlOrderItemId.firstChild.nodeValue;
			}
			catch(e) {}
	 		
	 		var p = document.getElementById("emptymessage");
			if (p) {
				document.getElementById("emptymessage").style.display = "none";
				document.getElementById("emptymessage").style.height = "0px";
			}
			
			if (this.orderItemId) {
				rowIndex = 0;
				var a=document.getElementById("qcart").insertRow(rowIndex)
				var b=a.insertCell(0)
				var c=a.insertCell(1)
				var d=a.insertCell(2)
				a.className = 'cartdiv';
				if (this.quickOrder) {
				b.className = 'qocartoption';
				}
				else {
				b.className = 'cartoption';
				}
				c.className = 'cartqty';
				c.style.width = '39px';
				c.align = 'center';
				c.id = this.itemnum;
				d.style.width = '31px';
				d.align = 'center';
				var one = ""+this.parent+"<br /><span class='smalltext'>"+this.color+"</span>";
				var two = "<input class='qtyform' size='2' maxlength='2' id='"+this.itemnum+"_qty' name='"+this.orderItemId+"' value='"+this.qty+"' onfocus='clearqty(this);' onblur='validateqty(this,\""+this.qty+"\",\""+this.max+"\");' />";
				if (this.color == '36X36 Sample') {
				var two = "<input class='qtyform' size='2' maxlength='2' id='"+this.itemnum+"_qty' name='"+this.orderItemId+"' value='"+this.qty+"' onfocus='clearqty(this);' onblur='validateqty(this,\""+this.qty+"\",\""+this.max+"\");' />";
				}
				var three = "<img src='../../../../wcsstore/CollinsAndAikmanAssetStore/images/cart_delete.gif' width='21' height='17' alt='Delete Item' onclick='deleteItem(this,\""+this.remove+"\",\""+this.initialize+"\");' style='cursor:pointer' />";
				b.innerHTML= one
				c.innerHTML= two
				d.innerHTML= three
			}
			
			if (this.quickOrder) {
				document.getElementsByTagName("body").item(0).style.cursor="auto";
			} else {
				document.getElementById("hcontainer").style.cursor="auto";
			}

	 	}
	}
}


// DELETE ITEM - PRODUCT PAGE/QUICK ORDER PAGE //

// this function processes a delete request in the backend and called the deleteRow function to remove it from the front-end //

function deleteItem(row,url,initialize) {
	row.parentNode.parentNode.parentNode.deleteRow(row.parentNode.parentNode.rowIndex);
	req = false;
    if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    	try {
			req = new XMLHttpRequest();
        } 
        catch(e) {
			req = false;
      	}
    } 
    else if(window.ActiveXObject) {
      	 try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} 
      	catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} 
        	catch(e) {
          		req = false;
        	}
		}
 	}
	req.open("GET", url, true);
	req.send(null);
	if (initialize == 'cscroller') {
		cscroller.init();
	}
	else if (initialize == 'qcscroller') {
		qcscroller.init();
	}
}

// this code removes the row from the cart table //

function deleteRow(i){
    document.getElementById('qcart').deleteRow(i)
}

// CART FORM UPDATE - PRODUCT PAGE/QUICK ORDER PAGE //
// this function loops through the form and deletes all items in it and then adds them back with the updated quantities, once completed it passes information to the updateItem function to finish the actions and redirection //

function updateItemForm(update,storeid,catalogid,redirect,quickorder) {
 
	var form = document.ShopCartForm;
	var formsize = form.length;
	
	//var orderid = form.activeOrder.value;
	//var elemnt = document.getElementById('activeOrder');
	//var parent = elemnt.parentNode;
	//parent.removeChild(elemnt);
	
	if (formsize <= '0') {
		if (redirect) {
	   		window.location = redirect;
	   	}
	}
	else {
		//var upddata = "&orderId=" + orderid + "&URL=&";
		var upddata = "&storeId=" + storeid + "&catalogId=" + catalogid + "&URL=&";
		//var upddata = "&URL=&";
		var updnum = 0;
		var removedata = "";
		for (i=0; i < form.length; i++) {
			var updnum = updnum + 1;
			upddata = upddata + "orderItemId_" + updnum + "=" + form.elements[i].name + "&quantity_" + updnum + "=" + escape(form.elements[i].value);
			if (i + 1 < form.length) {
				upddata = upddata + "&";
			}
		}
		
	    if (redirect) {
			updateItem(update,upddata,redirect,quickorder);
		}
		else {
			updateItem(update,upddata,null,quickorder);
		}
	}
	
}


// UPDATE ITEM - PRODUCT PAGE/QUICK ORDER PAGE //

// this function handles the actions to update an item and redirect if that value is present //

// The variable 'busy' is used to avoid submitting the same forms multiple times when users click the button more than once.
var busy = false;

function updateItem(url,formdata,redirect,quickorder) {

	if (!busy){
    	busy = true;
		req = false;
    	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
    		try {
				req = new XMLHttpRequest();
        	} 
        	catch(e) {
				req = false;
      	  	}
    	} 
    	else if(window.ActiveXObject) {
      	 	try {
        		req = new ActiveXObject("Msxml2.XMLHTTP");
      		} 
      		catch(e) {
        		try {
          			req = new ActiveXObject("Microsoft.XMLHTTP");
        		} 
        		catch(e) {
          			req = false;
        		}
			}
 	    }
 	    
 	    if(formdata) {
			req.open("POST", url, true);
			req.onreadystatechange = resetBusy;
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.send(formdata);
			if (redirect) {
				setTimeout("processCheckout('"+redirect+"')",1000);
			}
		}
		else {
			req.open("GET", url, true);
			req.onreadystatechange = resetBusy;
			req.send(null);
		}
	}
	
	if (quickorder) {
		document.getElementsByTagName("body").item(0).style.cursor="auto";
	} else {
		document.getElementById("hcontainer").style.cursor="auto";
	}
	
}

function resetBusy() {
	busy = false;
}

// this code specifically process the redirection request //

function processCheckout(redirect) {
	window.location = redirect;
}

// QUICK ORDER BREADCRUMB - QUICK ORDER PAGE //

// this code handles the selection in the style box //

function highlightdiv(id) {
	var divs = document.getElementsByTagName('div');
	for(i=0; i < divs.length;i++) {
		if(divs[i].className == 'prodoption2') {
			divs[i].className = "prodoption";
		}
	}
	document.getElementById(id).className = "prodoption2";
}

// this code handles the selection in the color box //

function highlightcolor(id) {
	var divs = document.getElementsByTagName('div');
	for(i=0; i < divs.length;i++) {
		if(divs[i].className == 'coloroption2') {
			divs[i].className = "coloroption";
		}
	}
	document.getElementById(id).className = "coloroption2";
}