function updateCart(orderCode) {
	if (document.all.item('itemColor_' + orderCode) == null) var strItemColor = 'N';
	else var strItemColor = document.all.item('itemColor_' + orderCode).value;
	if (document.all.item('itemSize_' + orderCode) == null) var strItemSize = 'N';
	else var strItemSize = document.all.item('itemSize_' + orderCode).value;
	if (strItemColor =='') {
		alert('Please select a color before adding to the shopping cart.');
		document.all.item('itemColor_' + orderCode).select();
	}
	else if (strItemSize =='') {
		alert('Please select a size before adding to the shopping cart.');
		document.all.item('itemSize_' + orderCode).select();
	}
	else if (document.all.item('unitQty_' + orderCode).value =='') {
		alert('Please select a quantity before adding to the shopping cart.');
		document.all.item('unitQty_' + orderCode).select();
	}
	else {
		//Set the item configuration values
		var strItemConfig = '';
		if (strItemColor != 'N') strItemConfig = strItemConfig + 'COLOR: ' + strItemColor + '. ';
		if (strItemSize != 'N') strItemConfig = strItemConfig + 'SIZE: ' + strItemSize + '. ';
		document.all.item('itemConfig_' + orderCode).value = strItemConfig;
		//Build the item XML string
		var collProduct = document.all.item('prod_' + orderCode.toString());
		var xmlCurrentOrder = document.frmCustomerOrder.xmlOrder.value;
		var checkString = '<item><orderCode>' + orderCode + '</orderCode><itemConfig>' + strItemConfig + '</itemConfig>';
		//alert(checkString);
		var xmlProduct = '<item>';
		
		for (i=0;i<collProduct.length;i++){
			var strNodeName = splitLeft(collProduct[i].name,'_');
			xmlProduct = xmlProduct + '<' + strNodeName + '>' + convertHTMLProblemChars(collProduct[i].value) + '</' + strNodeName + '>';
		}
		xmlProduct = xmlProduct + '</item>';
		
		//Check for existence of product in the current order.  Update if exists, add if not.
		var startIndex = xmlCurrentOrder.indexOf(checkString);
		if (startIndex > -1) {
			var endIndex = xmlCurrentOrder.indexOf('</item>',startIndex)+7;
			var xmlItem = xmlCurrentOrder.substring(startIndex,endIndex);
			xmlCurrentOrder = splitLeft(xmlCurrentOrder,xmlItem) + xmlProduct + splitRight(xmlCurrentOrder,xmlItem);
		}
		else xmlCurrentOrder = xmlCurrentOrder + xmlProduct;
	
		document.frmCustomerOrder.xmlOrder.value = xmlCurrentOrder;
		//alert(frmCustomerOrder.xmlOrder.value);
		alert('Product added to cart.');
	}
}

function updateCustomerXML() {
	var collPurchaser = frmPurchaserInfo.elements
	var xmlPurchaser = '<ROW>';
	for (i=0;i<collPurchaser.length;i++){
		var strNodeName = collPurchaser[i].name;
		xmlPurchaser = xmlPurchaser + '<' + strNodeName + '>' + collPurchaser[i].value + '</' + strNodeName + '>';
	}
	xmlPurchaser = xmlPurchaser + '</ROW>';
	frmCustomerXML.xmlPurchaser.value = xmlPurchaser;
	
	var collInvoice = frmReceiverInfo.elements
	var xmlInvoice = frmCustomerXML.xmlInvoice.value;
	for (i=0;i<collInvoice.length;i++){
		var strNodeName = collInvoice[i].name;
		if (xmlInvoice.indexOf('<' + strNodeName + '>') > -1) {
			xmlInvoice = updateXMLNode(xmlInvoice,strNodeName,collInvoice[i].value);
		}
		else {
			xmlInvoice = xmlInvoice + '<' + strNodeName + '>' + collInvoice[i].value + '</' + strNodeName + '>';
		}
	}
	frmCustomerXML.xmlInvoice.value = xmlInvoice;
}

function deleteItem(xmlStartScope,xmlEndScope) {
	var xmlCurrentOrder = document.frmCustomerXML.xmlOrder.value;
	var xmlDivider = getXMLSegment(xmlCurrentOrder,xmlStartScope,xmlEndScope);
	if(xmlDivider!='') {
		xmlCurrentOrder = splitLeft(xmlCurrentOrder,xmlDivider) + splitRight(xmlCurrentOrder,xmlDivider);
		document.frmCustomerXML.xmlOrder.value = xmlCurrentOrder;
	}
}

function deleteXMLSegment(xmlBase,xmlStartScope,xmlEndScope) {
	var xmlDivider = getXMLSegment(xmlBase,xmlStartScope,xmlEndScope);
	if(xmlDivider!='') {
		xmlBase = splitLeft(xmlBase,xmlDivider) + splitRight(xmlBase,xmlDivider);		
	}
	return xmlBase;
}

function updateXMLOrderNode(xmlStartScope,xmlEndScope,strNodeName,valNew) {
	var xmlCurrentOrder = document.frmCustomerXML.xmlOrder.value;
	var xmlItem = getXMLSegment(xmlCurrentOrder,xmlStartScope,xmlEndScope);
	var xmlItemSegment = getXMLSegment(xmlItem,'<'+ strNodeName + '>','</' + strNodeName + '>');
	var xmlItemNew = splitLeft(xmlItem,xmlItemSegment) + '<'+ strNodeName + '>' + valNew + '</' + strNodeName + '>' + splitRight(xmlItem,xmlItemSegment);
	xmlCurrentOrder = splitLeft(xmlCurrentOrder,xmlItem) + xmlItemNew + splitRight(xmlCurrentOrder,xmlItem);
	document.frmCustomerXML.xmlOrder.value = xmlCurrentOrder;
}

function updateXMLNode(xmlBase,strNodeName,valNew) {
	var xmlBaseSegment = getXMLSegment(xmlBase,'<'+ strNodeName + '>','</' + strNodeName + '>');
	var xmlBaseNew = splitLeft(xmlBase,xmlBaseSegment) + '<'+ strNodeName + '>' + valNew + '</' + strNodeName + '>' + splitRight(xmlBase,xmlBaseSegment);
	return xmlBaseNew;
}

function updateTotal() {
	var collProduct = document.frmOrderItems.elements;
	var numTotal = 0;
	for (i=0;i<collProduct.length;i++){
		if (splitLeft(collProduct[i].name,'_') == 'unitPrice') {
			numTotal = numTotal + (collProduct[i].value * document.all.item('unitQty_'+splitRight(collProduct[i].name,'_')).value);
			//alert(collProduct[i].value);
		}
	}
	document.all.item('orderTotal').value = ConvertToDollars(numTotal);
}

function updateWeight() {
	var collProduct = document.frmOrderItems.elements;
	var numTotal = 0;
	for (i=0;i<collProduct.length;i++){
		if (splitLeft(collProduct[i].name,'_') == 'unitWeight') numTotal = numTotal + (collProduct[i].value * document.all.item('unitQty_'+splitRight(collProduct[i].name,'_')).value);
	}
	//frmCustomerOrder.orderWeight.value = numTotal;  
	document.all.item('orderWeight').value = convOzToPounds(numTotal);
}

function getXMLSegment(xmlFull,xmlStart,xmlEnd) {
	var xmlSegment = '';
	var startIndex = xmlFull.indexOf(xmlStart);
	if (startIndex > -1) {
		var endIndex = xmlFull.indexOf(xmlEnd,startIndex)+xmlEnd.length;
		xmlSegment = xmlFull.substring(startIndex,endIndex);
	}
	return xmlSegment;
}

function splitLeft(xmlFull,strSplitVal) {
	if (xmlFull.indexOf(strSplitVal,0)>-1) return xmlFull.substring(0,xmlFull.indexOf(strSplitVal,0));
	else return '';
}

function splitRight(xmlFull,strSplitVal) {
	if (xmlFull.indexOf(strSplitVal,0)>-1) return xmlFull.substring(xmlFull.indexOf(strSplitVal,0) + strSplitVal.length);
	else return '';
}

function convertHTMLProblemChars(strSource) {
	strSource = strSource.replace(/</gi,'&lt;');
	strSource = strSource.replace(/>/gi,'&gt;');
	strSource = strSource.replace(/&/gi,'&amp;');
	strSource = strSource.replace(/'/gi,'&acute;');
	//strSource = strSource.replace(/"/gi,'&quot;');
	return strSource;
}
	


