var vSubTotal = null;
var iSubTotal = null;
var vTotal = null;
var vShipping = null;

function my_getElementsByTagName(el,str) {
	if (document.all){
		if (str=="*"){
			return el.all
	  		}
		else{
			return el.all.tags(str)
		  	}
		}
	else{
		return el.getElementsByTagName(str)
	}
} 
	
function initAll() {
	vSubTotal = document.getElementById('subtotal')
	iSubTotal = 0
	vShipping = document.getElementById('shipping')
	vTotal = document.getElementById('total')
	}

var aPrices = new Array(3)
aPrices[0] = new Array(3)
	aPrices[0][0]=3.5
	aPrices[0][1]=6.5
	aPrices[0][2]=12
aPrices[1] = new Array(3)	
	aPrices[1][0]=5.5
	aPrices[1][1]=10
	aPrices[1][2]=18
aPrices[2] = new Array(3)
	aPrices[2][0]=7
	aPrices[2][1]=13
	aPrices[2][2]=24


	
function updatePrice(el){
	//from el get the row
	//go through each item and if its a select check its value, if all values equal something then set the prices
	var curCell = el.parentNode;
	var curRow = curCell.parentNode;
	var itemColor = '';
	var itemSize = '';
	var itemQuantity = '';
	curRowAll = my_getElementsByTagName(curRow,"*")
	//curRowAll = curRow.getElementsByTagName("*")
	for(i=0;i<curRowAll.length;i++){
		if(curRowAll[i].getAttribute('class')!=null){
			if(curRowAll[i].getAttribute('class')=='itemTotal'){
				totalDiv = curRowAll[i]
			}
		}
		if(curRowAll[i].getAttribute('class')!=null){
			if(curRowAll[i].getAttribute('class')=='hidinput'){
				hidInput = curRowAll[i]
			}
		}
		if(curRowAll[i].getAttribute('name')!=null){
			elName = curRowAll[i].getAttribute('name').toLowerCase()
			if(elName=='color'){
				itemColor = curRowAll[i].value
				}
			if(elName=='size'){
				itemSize = curRowAll[i].value
				}
			if(elName=='quantity'){
				itemQuantity = curRowAll[i].value
				}
			if(elName=='ply'){
				itemPly = curRowAll[i].value
				}
			}
		}
	if(itemColor!=''&&itemSize!=''&&itemQuantity!=''&&itemPly!=''){
		itemColorNdx = 1
		if(itemColor.toLowerCase()=='natural'){
			itemColorNdx=0
			}
		if(itemColor.toLowerCase()=='rainbow'){
			itemColorNdx=2
			}
		if(itemColor.toLowerCase()=='rasta'){
			itemColorNdx=2
			}
		var price = (aPrices[itemColorNdx][itemSize])*itemQuantity
		//this next line only works as long as the total div is the second to last element in the row, adn the hidden input is the last
		curRowAll[curRowAll.length-2].innerHTML =  formatCurrency(price)
		curRowAll[curRowAll.length-1].value =  formatCurrency(price)
		updateTotals()
	}
}



function updateTotals(){
	iSubTotal = 0
	aDivs = document.body.getElementsByTagName("DIV")
	for(i=0;i<aDivs.length;i++){
		t = aDivs[i].innerHTML.replace(/\$|\,/g,'')
		if(!isNaN(parseFloat(t))){
			iSubTotal=iSubTotal+parseFloat(t)
			}
		}
		//trouble clearing the &nbsp from the cell.
	vShipping.innerHTML=''
	vSubTotal.innerHTML='' 
	vTotal.innerHTML=''
	
	vSubTotal.innerHTML = formatCurrency(iSubTotal)
	if(iSubTotal>200){
		vShipping.innerHTML='Call'
		vTotal.innerHTML = 'Call'
		}
	else{
		if(iSubTotal >100.01){
			vShipping.innerHTML='$35.00'
			vTotal.innerHTML =  formatCurrency(iSubTotal + 35)
		}
		else{
			if(iSubTotal >50.01){
				vShipping.innerHTML='$20.00'
				vTotal.innerHTML =  formatCurrency(iSubTotal + 20)
			}
			else{
				vShipping.innerHTML='$10.00'
				vTotal.innerHTML =  '<b>' + formatCurrency(iSubTotal + 10) + '</b>'
			}
		}
	}
}



//Remove the $ sign if you wish the parse number to NOT include it
function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

//The definitive table row adder yeaa!!

function removeRow(btn){
	if(numDataRows!=0){
		var curCell = btn.parentNode;
		var curRow = curCell.parentNode;
		var curTbdy = curRow.parentNode;
		var curTbl = curTbdy.parentNode;
		curTbdy.deleteRow(curRow.rowIndex);
		numDataRows--
		updateTotals()
	}
}




numDataRows = 0;
function addRow(btn,numCols,startingRows){
	numDataRows++
	RowNum = startingRows + numDataRows
	var cells =  new Array(numCols);
	var curCell = btn.parentNode;
	var curRow = curCell.parentNode;
	var curTbdy = curRow.parentNode;
	var curTbl = curTbdy.parentNode;
	var numRows = curTbl.rows.length;
	var newRow2 = curTbdy.getElementsByTagName('tr')[numRows-2].cloneNode(true);
	var sourceCells = curTbdy.getElementsByTagName('tr')[numRows-2].cells;
	var numCells = curTbdy.getElementsByTagName('tr')[numRows-2].cells.length
	sourceAll = my_getElementsByTagName(curTbdy.getElementsByTagName('tr')[numRows-2],'*')
	var newRow = curTbl.insertRow(numRows-1)
	for (i = 0; i < numCells; i++ ) {
		newRowCell = newRow.appendChild(curTbdy.getElementsByTagName('tr')[numRows-2].cells[i].cloneNode(true));
	}
	newRowAll = my_getElementsByTagName(newRow,"*")
	for(i = 0;i<newRowAll.length;i++){
		//try to move the attributes over to the new objects
		//sourceAll & newRowAll should have equal number of elements
		//sourceAll elements should still have their attributes sooo
		for(ai = 0;ai<sourceAll[i].attributes.length;ai++){
			try{
				newRowAll[i].attributes[ai].name = sourceAll[i].attributes[ai].name
				newRowAll[i].attributes[ai].value = sourceAll[i].attributes[ai].value
				}
			catch(er){}
			//w00t!!!
		}
	
		var tag = newRowAll[i].tagName.toLowerCase()
		if(tag == 'input'){ 
			inpt = newRowAll[i]
			switch(inpt.type){
				case 'radio':
					inpt.checked = false
					break
				case 'checkbox':
				inpt.checked = false
					inpt.value = RowNum
					break
				case 'text':
					inpt.value = "1"
					break
			}
			}
		else{
			if(tag == 'div'){ 
				newRowAll[i].innerHTML='&nbsp;'
				}
			}
	}
}

function submitForm(){
	//validate
	//hmmmm to validate I need to make sure that all text boxes contain a number and that no selects are empty
	updateTotals()
	var strMsg='';
	var sValid=1;
	var tValid=1;
	vSelects = document.getElementsByTagName("select")
	vInputs = document.getElementsByTagName("input")
	for(i=0;i<vInputs.length;i++){
		if(vInputs[i].getAttribute('type').toLowerCase()=='text'){
			if(isNaN(vInputs[i].value)){
				vInputs[i].style.backgroundColor='#ff0000'
				vInputs[i].style.color='#ffffff'
				if(tValid==1){
					strMsg=strMsg+' Please only enter numbers for quantity\n'
					}
				tValid=0
			}
		}
	}
	for(i=0;i<vSelects.length;i++){
		if(vSelects[i].value==''){
			vSelects[i].options[vSelects[i].selectedIndex].style.backgroundColor='#ff0000'
			vSelects[i].options[vSelects[i].selectedIndex].style.color='#ffffff'
			vSelects[i].focus()
			if(sValid==1){
				strMsg=strMsg+' Please complete all fields or remove incomplete rows.'
				}
			sValid=0
		}
	}
	if(sValid+tValid < 2){
		alert(strMsg)
		}
	else{
		document.forms[0].submit()
	}
	//submit
}

function makeForm(){
	updateTotals()
	var strMsg='';
	var sValid=1;
	var tValid=1;
	vSelects = document.getElementsByTagName("select")
	vInputs = document.getElementsByTagName("input")
	for(i=0;i<vInputs.length;i++){
		if(vInputs[i].getAttribute('type').toLowerCase()=='text'){
			if(isNaN(vInputs[i].value)){
				vInputs[i].style.backgroundColor='#ff0000'
				vInputs[i].style.color='#ffffff'
				if(tValid==1){
					strMsg=strMsg+' Please only enter numbers for quantity\n'
					}
				tValid=0
			}
		}
	}
	for(i=0;i<vSelects.length;i++){
		if(vSelects[i].value==''){
			vSelects[i].options[vSelects[i].selectedIndex].style.backgroundColor='#ff0000'
			vSelects[i].options[vSelects[i].selectedIndex].style.color='#ffffff'
			vSelects[i].focus()
			if(sValid==1){
				strMsg=strMsg+' Please complete all fields or remove incomplete rows.'
				}
			sValid=0
		}
	}
	if(sValid+tValid < 2){
		alert(strMsg)
		}
	else{
		var f=document.createElement('form');
		f.setAttribute('name','myForm');
		f.setAttribute('action','confirmorder.asp');
		f.setAttribute('method','post');
		var hidFld
		for(i=0;i<vSelects.length;i++){
			hidFld=document.createElement('input');
			hidFld.setAttribute('type','hidden');
			hidFld.setAttribute('name',vSelects[i].getAttribute('name'));
			hidFld.setAttribute('value',vSelects[i].options[vSelects[i].selectedIndex].getAttribute('value'));
			f.appendChild(hidFld)
		}
		for(i=0;i<vInputs.length;i++){
			if(vInputs[i].getAttribute('type').toLowerCase()=='text'){
				f.appendChild(vInputs[i].cloneNode(true))
			}
			if(vInputs[i].getAttribute('type').toLowerCase()=='hidden'){
				try{
					vInputs[i].setAttribute('type','text')
					}
				catch(er){}
				f.appendChild(vInputs[i].cloneNode(true))
				try{
					vInputs[i].setAttribute('type','hidden')
					}
				catch(er){}
			}
		}
		document.getElementById('mainpage').appendChild(f);
		document.forms[0].submit()
	}
}

var gTimer1 = null;
var publicFlag = 0;
var thisQInp = null;
function Timer1(){
	updatePrice(thisQInp)
	if(iSubTotal!=0){
		updateTotals()
	}
	if(publicFlag==1){
		gTimer1 = window.setTimeout("Timer1()",1000);
		}
}



function startPolling(qtyInput){
	thisQInp = qtyInput
	publicFlag=1
	Timer1();
}

function stopPolling(){
	publicFlag=0
}

