//global vars
var client = new HttpClient();

//download.html functions
/**
 * addItem - adds a item to the virtual shopping cart and re-displays minicart
 * PRECONDITION: product to add
 * POSTCONDITION: places the minicart in the first position of the element with id="sideNavMainTd"
 * 				  if the first element of the side nav has id="cart" it will replace this else insert before this
 * DEV-DATE: 12-13-07 JWS
 */
function addItem(itemId){
	client.requestType = 'POST';
	client.isAsync = false;
	var payload = 'pid='+itemId;
	client.callback = function(response) {
		var mainCell = document.getElementById('sideNavMainTd');	//left side nav
		var firstCell = mainCell.firstChild;	//first cell in the nav
		//hack/workaround for mozilla regarding every line break as a text node
		while(firstCell.nodeName == '#text'){
			firstCell = firstCell.nextSibling;
		}

		cartTable = buildCartTable(response, mainCell, firstCell);	//build shopping cart

		//if first element is the cart replace the cart otherwise insert before element
		if(firstCell.id == 'cart'){
			mainCell.replaceChild(cartTable, firstCell);
		}else{
			mainCell.insertBefore(cartTable, firstCell);
			mainCell.insertBefore(document.createElement('div'), firstCell);
		}
		alert('Product has been added to your shopping cart');
	}

	client.makeRequest('download.html', payload);
}

/**
 * buildCartTable - builds mini shopping cart
 * PRECONDITION: wraps cartData (minicart) in its display table
 * POSTCONDITION: returns cartTable - a complete shopping cart for the side nav
 * DEV-DATE: 12-13-07 JWS
 */
function buildCartTable(cartData)
{
	//create main shopping cart table/tbody and set attributes
	cartTable = document.createElement('table');
	cartTable.className = 'sideNavContainerTbl';
	cartTable.id = 'cart';
	var cartTableTbody = document.createElement('tbody');

	//create title row and cell as well as text that goes in them
	var titleTr = document.createElement('tr');
	var titleTd = document.createElement('td');
	titleTd.className = 'sideNavTitleTd';
	var cTextSpan = document.createElement('span');
	cTextSpan.className = 'style3';
	var cText = document.createTextNode(' C');//need a fix for space not showing JWS
	var artTextSpan = document.createElement('span');
	artTextSpan.className = 'style4';
	var artText = document.createTextNode(' A R T');

	//create row, cell, and table for cart
	var cartTr = document.createElement('tr');
	var cartTd = document.createElement('td');
	cartTd.innerHTML = cartData;

	//put it all together :)
	cartTable.appendChild(cartTableTbody);
	cTextSpan.appendChild(cText);
	artTextSpan.appendChild(artText);
	titleTd.appendChild(cTextSpan);
	titleTd.appendChild(artTextSpan);
	titleTr.appendChild(titleTd);
	cartTr.appendChild(cartTd);
	cartTableTbody.appendChild(titleTr);
	cartTableTbody.appendChild(cartTr);

	return cartTable;
}


//end download.html functions
//view_cart.html functions

/**
 * buildCartTable - builds mini shopping cart
 * PRECONDITION: wraps cartData (minicart) in its display table
 * POSTCONDITION: returns cartTable - a complete shopping cart for the side nav
 * DEV-DATE: 12-13-07 JWS
 */
function removeProduct(removeEle, pid)
{
	var payload = 'pid='+pid;
	var ie=true;
	client.isAsync = false;
	client.callback = function(response) {
		/*
		remember to remove response div from html structure JWS
		document.getElementById('response').innerHTML = response;
		*/
		var mainCell = document.getElementById('sideNavMainTd');//where to place shopping cart
		var firstCell = mainCell.firstChild;
		while(firstCell.nodeName == '#text'){
			ie = false;
			firstCell = firstCell.nextSibling;
		}
		//if first element is the cart replace the cart otherwise insert before element
		var cartTable = buildCartTable(response, ie);
		if(firstCell.id == 'cart'){
			mainCell.replaceChild(cartTable, firstCell);
		}else{
			mainCell.insertBefore(cartTable, firstCell);
			mainCell.insertBefore(document.createElement('div'), firstCell);
		}
		var prodTr = removeEle.parentNode.parentNode;
		prodTr.parentNode.removeChild(prodTr);
	}
	client.requestType = 'POST';
	client.makeRequest('view_cart.html', payload);
}
//end view_cart.html functions
//checkout.html functions

/**
 * buildCartTable - builds mini shopping cart
 * PRECONDITION: wraps cartData (minicart) in its display table
 * POSTCONDITION: returns cartTable - a complete shopping cart for the side nav
 * DEV-DATE: 12-13-07 JWS
 */
function showProdImg(genc, width, height)
{
	var url = 'http://www.scrapola.com/viewExample.html?genc='+genc;
	openWindow(url, width, height);
}

//end checkout.html functions

//pattern_view.html functions
function switchOrder(invoice)
{
	var payload = 'invoice='+invoice;
	client.isAsync = true;
	client.callback = function(response){
		document.getElementById('currentOrd').innerHTML = response;
		var currentTxt = document.getElementById('current');
		var currentRow = currentTxt.parentNode.parentNode;
		var currentInvoice = currentRow.getElementsByTagName('td')[0].innerHTML;
		currentInvoice = currentInvoice.trim();
		var newCurrent = document.getElementById(invoice+'_view');
		if(currentInvoice != invoice){
			currentTxt.innerHTML = 'View';
			currentTxt.id = currentInvoice+'_view';
			newCurrent.innerHTML = 'Current';
			newCurrent.id = 'current';
			//put the focus back on the currentOrdersBox
			var anchorsArr = document.getElementsByTagName('a');
			var a;
			var count = 0;
			a = anchorsArr[count];
			while(a.name != 'currentOrdersBox'){
				count++;
				a = anchorsArr[count];
			}
			a.focus();
		}

	}
	client.requestType = 'POST';
	client.makeRequest('pattern_view_JWS.html', payload);
}


//end pattern_view.html functions
