function addEvent(object, userAction, func, useCapture){
			if(!document.getElementById || !document.createTextNode){return;}
			if(object.addEventListener){
				object.addEventListener(userAction, func, useCapture);
				return true;
			}else if(object.attachEvent){
					var result=object.attachEvent('on'+userAction, func);
					return result;
			}else{
					object['on'+userAction]=func;
			}

		}
		
function setupLinks(containerDivId){//
	var container = document.getElementById(containerDivId);
	var allTheLinks = container.getElementsByTagName('a');
		var activeLinks = getElementsByClassName(allTheLinks, 'active');
		if(activeLinks){//because an empty array is false
			for(var linkCounter = 0; linkCounter<activeLinks.length; linkCounter++){
					addEvent(activeLinks[linkCounter], 'click', cancelClick, true);
			}
		}
	
}


 function cancelClick(e){
 	e = e ? e : window.event;
	var targ = e.target ? e.target : e.srcElement;

	if(window.event){
		window.event.cancelBubble = true;
		//window.event.returnValue = false;//this did not work in IE6
		return;
	}

	if (e.stopPropagation){
	 e.stopPropagation();
	 e.preventDefault();
	 }
}




//YO!  there is a better version of this now.  see fundamentals.js on another domain...
function getElementsByClassName(nodeArray, target){//to four levels...  a bit wordy.
	var found = new Array();
	for (var h=0; h<nodeArray.length; h++){
	var node = nodeArray[h];
	if(node.getAttribute('class')==target){found.push(node);}
	for(var i=0; i<node.childNodes.length; i++){
		var firstGenNode = node.childNodes[i];
		if(firstGenNode.nodeType == 1){
			if(firstGenNode.getAttribute('class')==target){
				found.push(node.childNodes[i]);
			}
		}
		if(firstGenNode.hasChildNodes()){
			for(var j=0; j<firstGenNode.childNodes.length; j++){
				var secondGenNode = firstGenNode.childNodes[j];
				if(secondGenNode.nodeType == 1){
					if(secondGenNode.getAttribute('class')==target){
						found.push(secondGenNode);
					}
				}
				if(secondGenNode.hasChildNodes()){
					for(var k=0; k<secondGenNode.childNodes.length; k++){
						var thirdGenNode = secondGenNode.childNodes[k];
						if(thirdGenNode.nodeType == 1){
							if(thirdGenNode.getAttribute('class')==target){
								found.push(thirdGenNode);
							}
						}
						if(thirdGenNode.hasChildNodes()){
							for(var ell=0; ell<thirdGenNode.childNodes.length; ell++){
								var fourthGenNode = thirdGenNode.childNodes[ell];
								if(fourthGenNode.nodeType == 1){
									if(fourthGenNode.getAttribute('class')==target){
										found.push(fourthGenNode);
									}
								}
							}
						}
					}
				}
			}
		}//end descendent nodes of the firstGenNode
	}
	}
	//alert(found);
	return found;

}



function loadXml(path){

	try{
      xml = new XMLHttpRequest();
	  var IE = false;
    }catch(error){
      try{
        xml = new ActiveXObject("Microsoft.XMLHTTP");
		var IE = true;
      }catch(error){
        return true;
      }
    }

	
	
	xml.open('GET', path, true);

	xml.onreadystatechange = function(){
		if(xml.readyState == 4){
			if(!xml.responseXML){alert('browser error!'); return true;}
			if(typeof(xml.responseXML) == "undefined"){alert('undefined!'); return true;}
			var xmlData = xml.responseXML;
			getReadyToFindPrice( xmlData );
			}
		}
	if(IE){
	xml.send();
	}else{
		xml.send(null);
		}
	return xml.responseXML;
	}

function getReadyToFindPrice(data){
	//alert('ready');
	var parameters = new readForm();
	findPrice(data, parameters);
	}

function findPrice(data, parameters) {
  requiredKeys = new Array('item', 'width', 'height', 'stock', 'colors', 'quantity');
  for(i=0; i<requiredKeys.length; i++){
  	if(!parameters[ requiredKeys[i] ]){alert('missing required key: '+requiredKeys[i]); return false;}
  }
	var itemPrice = false;
	
	//are we dealing with "Greeting Cards with Envelopes"?
	var greetingCards = false;
	if(document.getElementById('epsPrint') ){ greetingCards = true;}//this test might not hold through the checkout!!

	var itemsArray = data.getElementsByTagName('product');
	for(var itemCount = 0; itemCount<itemsArray.length; itemCount++){
		var itemName = itemsArray[itemCount].getAttribute('name');
		if(itemName == parameters.item.toLowerCase() ){var itemNode = itemsArray[itemCount];}
		}
	
	if(!itemNode){itemNode = data;}
	
		sizesArray = itemNode.getElementsByTagName('size');
		
  for(i=0; i<sizesArray.length; i++){
  	itemSize = sizesArray[i];
  	itemWidth = itemSize.getAttribute('width');
  	itemHeight = itemSize.getAttribute('height');
  	if( (itemWidth == parameters.width && itemHeight == parameters.height) || ( itemHeight == parameters.width && itemWidth == parameters.height ) ){
  		allItemStocks = itemSize.getElementsByTagName('stock');
  		for(var j=0; j<allItemStocks.length; j++){
  			itemStock = allItemStocks[j].getAttribute('code');
			//alert('does '+itemStock+' match '+parameters.stock+'?');
			var stockName = allItemStocks[j].getElementsByTagName('name')[0].firstChild.nodeValue;
			//alert(stockName);
			
  			if(itemStock == parameters.stock){
  				//alert('stock matched');
				var allQtys = allItemStocks[j].getElementsByTagName('quantity');
				for(k=0; k<allQtys.length; k++){
					var tmpQty = allQtys[k];
					if(tmpQty.getAttribute('count') == parameters.quantity){
						var stdShipping = parseFloat(tmpQty.getElementsByTagName('ground')[0].firstChild.nodeValue).toFixed(2);
						var expShipping = parseFloat(tmpQty.getElementsByTagName('twoDay')[0].firstChild.nodeValue).toFixed(2);
						
						var itemColors = tmpQty.getElementsByTagName('colors');						
						for(var m=0; m<itemColors.length; m++){
							if(itemColors[m].getAttribute('opt')==parameters.colors){
								var itemPrice = parseFloat(itemColors[m].getElementsByTagName('price')[0].firstChild.nodeValue).toFixed(2);
								if(greetingCards){
									var ep=document.forms.printThis.elements.epsPrint;//this is a checkbox
									if(ep.checked){itemPrice *= 1; itemPrice += 45;}
									}
								setPrice(itemPrice, stdShipping, expShipping, parameters.quantity);
								return true;
								}
							}
						
						}
					}
				
  			}//end stock match
  		}//end stocks loop
  	}//end test for size
  }//end sizesArray for loop
setPrice('call for price', 'call for price', 'call for price');
return false;
}//end findPrice


function setPrice(price, stdShipping, expShipping, quantity){
	var placeForPrice = document.getElementById('price');
	var placeForStdShipping = document.getElementById('stdShippingPrice');
	//var placeForTotalInclStdShipping = document.getElementById('pricePlusStdShipping');
	var placeForExpShipping = document.getElementById('expShippingPrice');
	//var placeForTotalInclExpShipping = document.getElementById('pricePlusExpShipping');
	
	if(isNaN(price)){
		placeForPrice.innerHTML = price;		
		placeForStdShipping.innerHTML = price;
		//placeForTotalInclStdShipping.innerHTML = price;
		placeForExpShipping.innerHTML = price;
		//placeForTotalInclExpShipping.innerHTML = price;
		}else{
		  placeForPrice.innerHTML = '$'+price;	
		  placeForStdShipping.innerHTML = '$'+stdShipping;
		  var stdSplusPrice = stdShipping * 1 + price * 1;
		  var totInclStdShp = stdSplusPrice.toFixed(2);
		 // placeForTotalInclStdShipping.innerHTML = '$'+totInclStdShp;
		  placeForExpShipping.innerHTML = '$'+expShipping;
		  var expSplusPrice = expShipping * 1 + price * 1;
		  totInclExpShp = expSplusPrice.toFixed(2);
		 // placeForTotalInclExpShipping.innerHTML = '$'+totInclExpShp;
		
		

	
	if(!document.getElementById('thatsJust')){
		var para = document.createElement('p');
		para.setAttribute('id', 'thatsJust');
		para.style.fontWeight = 'bold';
		para.style.marginTop = "15px";
		para.style.textAlign = 'center';
		var panel = document.getElementById('priceContainer').parentNode;
		panel.appendChild(para);
		}
	var thatsJust = document.getElementById('thatsJust');
	pricePerItem = parseFloat(price / quantity).toFixed(2);
	//alert(price +' divided by '+quantity+' equals '+pricePerItem);
	if(pricePerItem < 1.75){
	thatsJust.innerHTML = "That's just $"+pricePerItem+" each!";
	}else{
		thatsJust.innerHTML = '';
		}
		
		
		
		
		
		
		}
		
		
}

function updateForm(parameters){

	var stdForm = document.forms['standardShipping'];
	var expForm = document.forms['expeditedShipping'];
	
//	for(var i=0; i<parameters.length; i++){
		
		var requiredKeys = new Array( "item", "width", "height", "score", "stock", "colors", "quantity" );
		
		var grtingCrdKeys = new Array( 'insideMessage', 'cardColors', 'epsPrinting', 'epsName', 'epsAddr1', 'epsAddr2', 'epsZip' );
		
		//alert(parameters['item']);
		var greetingCards = false;
		if(parameters['item']=='greeting cards with envelopes'){
			greetingCards = true;
			//alert('cards!');
				for(var g in grtingCrdKeys){
					requiredKeys.push(grtingCrdKeys[g]);
					//alert('added '+g+' to the array.');
					}			
				var epsPrinting = document.getElementById('epsPrinting');
				var epsOutput = epsPrinting.checked ? 'Yes' : 'No';
				parameters['epsPrinting'] = epsOutput;
				//alert(parameters['cardColors']);
				
			}
			/*
		if(greetingCards){
		//alert(requiredKeys['insideMessage']);
		}
		*/
		//if(array_key_exists(requiredKeys['insideMessage'])){alert('everything is going to be ok.';}
		
		for (var key = 0; key<requiredKeys.length; key++){
			
			var stdInputs = stdForm.getElementsByTagName('input');
			var expInputs = expForm.getElementsByTagName('input');

			var newValue = parameters[ requiredKeys[key] ];
			
			//alert('newValue is '+newValue);
			
			for( var n = 0; n<stdInputs.length; n++){
				
				//var currentNode in stdInputs){
			//	alert('examining '+stdInputs[n]+': it is named '+stdInputs[n].getAttribute('name') +': does that match  '+requiredKeys[key]+'?');
				if(stdInputs[n].getAttribute('name') == requiredKeys[key] ){
					stdInputs[n].setAttribute('value', newValue); 
					}
				}//end for
			
			for(var n = 0; n<expInputs.length; n++){
				if(expInputs[n].getAttribute('name') == requiredKeys[key] ){
					expInputs[n].setAttribute('value', newValue); 
					}
				}//end for loop
			
			
			}//end requiredKeys
		
//		}//end parameters
	
	}

function setupForm(){
	var myForm = document.forms['printThis'];
	
	//first: disable the submit button
	var calculatePriceButton = document.getElementById('nowIRuleTheWorldHaHa');
	calculatePriceButton.disabled = "disabled";
	calculatePriceButton.style.display = "none";
	
	//next:  set event listeners for all the form elements.
	for(var i=0; i<myForm.elements.length; i++){

		var selectionAction;
		var curElm = myForm.elements[i];
		
		switch(curElm.type){
		
		case 'radio':
		
		addEvent(myForm.elements[i], 'click', allTheAction, false);
		break;
		case 'checkbox':
		addEvent(myForm.elements[i], 'click', allTheAction, false);			
		
		break;
		case 'select-one':
			//applying a click event listener to the option elements does not fire properly in IE6.  But this seems to work:
			addEvent(myForm.elements[i], 'change', allTheAction, false);
		break;
		default:
		addEvent(myForm.elements[i], 'change', allTheAction, false);		
		break;
				
		}
	}
}

function selectOption(selectMenu, targetValue){
	//alert(selectMenu.options.length);
	for(var i = 0; i< selectMenu.options.length; i++){
		//alert(selectMenu.options[i].value);
		if(selectMenu.options[i].value == targetValue){
			selectMenu.options[i].setAttribute('selected', 'selected');
			}else{
				if(selectMenu.options[i].getAttribute('selected') != undefined){
					//alert('deselecting '+selectMenu.options[i].value);
						selectMenu.options[i].removeAttribute('selected');
						}
				}
		}
	}

function getSelectValue(selectMenu){
	for(var i in selectMenu.options.length){
		//alert(selectMenu.options[i].getAttribute('selected'));
				if(selectMenu.options[i].getAttribute('selected') != undefined){
					//alert(selectMenu.options[i].value);
					return selectMenu.options[i].value;
					}
		}
	}

function readForm(){
	var myForm = document.forms['printThis'];
	var formValues = new Array();
	
	for(var i=0; i<myForm.elements.length; i++){

		
		var currentElementName = myForm.elements[i].name;
		var currentElementValue = myForm.elements[i].value;
		var currentElementType = myForm.elements[i].type;
		
		/*
		if(currentElementName == 'insideMessage'){//should I be using textarea.innerHTML to read this? - NO
			alert('currentElementValue is '+currentElementValue);//returns correct value
			alert('innerHTML:' + myForm.elements[i].innerHTML);//returns an empty string
			}
		
				if(parameters['cardColors'] == 'fullColorInside'){
					if(parameters['colors'] != '4/4'){
						document.forms['printThis'].colors.setAttribute('value', '4/4');
						//allTheAction();
						}
					}
		*/
		if(currentElementName == 'cardColors'){
			//alert(currentElementName);
			if(myForm.elements[i].checked){
				//alert(currentElementValue);
				if(currentElementValue == 'fullColorInside'){
					//alert(formValues['colors']);
					if(formValues['colors'] != undefined){
						//alert('check');
						if(formValues['colors'] != '4/4'){
							selectOption(myForm.colors, '4/4');
							//allTheAction();
							}
						}else{
						var currentColor = getSelectValue(myForm.colors);
						if(currentColor != '4/4'){
							selectOption(myForm.colors, '4/4');
							//allTheAction();
							}
						}
					}
				}
			}
		
		
		switch(currentElementType){
		case 'radio':
		if(myForm.elements[i].checked){
			formValues[currentElementName] = currentElementValue;
		}
		break;
		case 'checkbox':
		if(myForm.elements[i].checked){
			formValues[currentElementName] = currentElementValue;
		}
		break;
		case 'select-one':
				for(j=0; j<myForm.elements[i].options.length; j++){
					var curSelOpt = myForm.elements[i].options[j];
					if(curSelOpt.selected){
						currentElementValue = curSelOpt.value;
						formValues[currentElementName] = currentElementValue;
						
					}
				}
		break;
		default:
		formValues[currentElementName] = currentElementValue;
		break;
		}
	
	}//end for loop	
		
	var itemSizes = formValues['size'].split('x');
	formValues['width'] = itemSizes[0];
	formValues['height'] = itemSizes[1];
	
	return formValues;
}		
			
