var width;
var drop;
var wood;

var counter=0;
var beginRow = 0;
var rate = 0;// rate for per m2

function getQuote() 
{
width=document.GetQuote.width.value;
drop=document.GetQuote.drop.value;
wood=document.GetQuote.wood.value;

if (isNaN(width))
	{
		alert('Please enter a Width');
	}
else if (isNaN(drop))
	{
		alert('Please enter a Drop');
	}
else 
	{
if ((wood == "vertical_dark") || (wood == "vertical_medium") || (wood == "vertical_light"))
{
	// TODO:  confirm lowest values for width and drop
	if ((width < 300 ) || (width > 3000) || (drop < 200) || (drop > 2250))
	{
				document.GetQuote.price.value = "Non-standard size:  call 1 300 55 66 75";
	}
	else
	{
		// round up width 
		if (width < 600) { width = 600 }
		else if ((width%100) > 0)
			{ 
				width = 100 - +(width%100) + +width; 
			}
		// round up drop
		if (drop > 1500) { drop = 2250; }
		else if (drop >= 200) { drop = 1500; }

			loadXMLData("vertical_89x4.xml");
	}
}
else if(wood == "basswood")//for basswood 50mm*3mm
{
	//confirm lowest values for width and drop
	if ((width < 220 ) || (width > 2410) || (drop < 100) || (drop > 3600))
	{
				document.GetQuote.price.value = "Non-standard size:  call 1 300 55 66 75";
	}
	else
	{
		if(width<450)//rate = $100/m2
		{
			rate = 100;
			document.GetQuote.price.value = Cal_Price(width,drop,rate);
		}
		else// width => 450
		{
			if(drop<2300)//rate = $75/m2
			{
				rate = 75;
				document.GetQuote.price.value = Cal_Price(width,drop,rate);
			}
			else//rate = $90/m2
			{
				rate = 90;
				document.GetQuote.price.value = Cal_Price(width,drop,rate);
			}
			
		}
			
	}
}
else if(wood == "basswoodwide")//for basswood 60mm*3mm
{
	//confirm lowest values for width and drop
	if ((width < 220 ) || (width > 2410) || (drop < 100) || (drop > 3600))
	{
				document.GetQuote.price.value = "Non-standard size:  call 1 300 55 66 75";
	}
	else
	{
		if(width<450)//rate = $110/m2
		{
			rate = 110;
			document.GetQuote.price.value = Cal_Price(width,drop,rate);
		}
		else// width => 450
		{
			if(drop<2300)//rate = $95/m2
			{
				rate = 95;
				document.GetQuote.price.value = Cal_Price(width,drop,rate);
			}
			else//rate = $100/m2
			{
				rate = 100;
				document.GetQuote.price.value = Cal_Price(width,drop,rate);
			}
			
		}
			
	}
}
else  // cedar, rustic, cedar wide
{
	if ((width < 280 ) || (width > 2410) || (drop < 200) || (drop > 3000))
		{
				document.GetQuote.price.value = "Non-standard size:  call 1 300 55 66 75";
		}
	else {
		// round up width 
		if (width > 2310) { width = 2410;}
		else if (width < 410) {width = 410;}
		else if (((width - 10)%100) > 0)
			{ 
				width = 100 - +((width - 10)%100) + +width; 
			}


		// round up drop
		if (drop > 2700) { drop = 3000; }
		else if (drop > 2500) { drop = 2700; }
		else if (drop > 2400) { drop = 2500; }
		else if (drop > 2300) { drop = 2400; }
		else if (drop > 2200) { drop = 2300; }
		else if (drop > 2100) { drop = 2200; }
		else if (drop > 2000) { drop = 2100; }
		else if (drop > 1900) { drop = 2000; }
		else if (drop > 1800) { drop = 1900; }
		else if (drop > 1700) { drop = 1800; }
		else if (drop > 1600) { drop = 1700; }
		else if (drop > 1500) { drop = 1600; }
		else if (drop > 1400) { drop = 1500; }
		else if (drop > 1300) { drop = 1400; }
		else if (drop > 1200) { drop = 1300; }
		else if (drop > 1100) { drop = 1200; }
		else if (drop > 1000) { drop = 1100; }
		else if (drop > 900) { drop = 1000; }
		else if (drop > 600) { drop = 900; }
              else if (drop >= 200) { drop = 600; }


	if (wood == "cedar")
		{
			loadXMLData("cedar.xml");
		}
       else if (wood == "cedarwide")
              {
                     loadXMLData("cedarwide.xml");
              }
       else if (wood == "rustic_cedar")
              {
                     loadXMLData("rustic_cedar.xml");
              }
	}
 
}
		}
}

function loadXMLData(pricesheet)
{

   if( ! CJL_loadXmlDocument(pricesheet, processXMLData) )
   {
	document.GetQuote.price.value = "Non-standard size:  call 1 300 55 66 75";
   }
}


function loadXMLDataForPrice(pricesheet)
{
    CJL_loadXmlDocument(pricesheet, processXMLDataForPrice); 
}

function processXMLData(xmlDoc)
{

var widths = xmlDoc.getElementsByTagName('width');

for (i=0;i<widths.length;i++)
{
	var widthValue = widths[i].getAttribute('value');
	if (width == widthValue) 
	{
		var drops =  widths[i].getElementsByTagName('drop');
		
		for (j=0;j<drops.length;j++)
		{
			var dropValue =  widths[i].getElementsByTagName('drop')[j].getAttribute('value');
			if (drop == dropValue)
			{
				var priceValue = widths[i].getElementsByTagName('drop')[j].getAttribute('price');
			
				document.GetQuote.price.value = priceValue;
				//break;
			}
		}
	}
}
	}

function processXMLDataForPrice(xmlDoc)
{
var widths = xmlDoc.getElementsByTagName('width');

for (i=0;i<widths.length;i++)
{
	var widthValue = widths[i].getAttribute('value');
	if (width == widthValue) 
	{
		
		var drops =  widths[i].getElementsByTagName('drop');
		
		for (j=0;j<drops.length;j++)
		{
			var dropValue =  widths[i].getElementsByTagName('drop')[j].getAttribute('value');
			if (drop == dropValue)
			{
				var priceValue = widths[i].getElementsByTagName('drop')[j].getAttribute('price');
			
				document.order_form.elements[beginRow+4].value = priceValue;
			}
		}
	}
}
//alert ('about to add 4');
//counter = counter + 4;
//alert ('counter now is '+counter);
	}

function getPrice(cellNumber) 
{

beginRow = cellNumber - (cellNumber%6);
	wood=document.order_form.elements[beginRow].value;
	width=document.order_form.elements[beginRow+1].value;
	drop=document.order_form.elements[beginRow+2].value;

	if (wood == "Empty")
		{
			document.order_form.elements[beginRow+4].value = 0;
			return;
		}

	if ((wood != "Empty") && (width > 0) && (drop > 0)) 
	{
		if((wood == "50mm Basswood Light Oak") || (wood == "50mm Basswood Cedar Dark") ||(wood == "50mm Basswood Vivid White") || (wood == "50mm Basswood Chalk USA"))
		{
			//confirm lowest values for width and drop
			if ((width < 220 ) || (width > 2410) || (drop < 100) || (drop > 3600))
			{
				document.order_form.elements[beginRow+4].value = "CALL";
				document.getElementById('checkout').setAttribute('disabled', 'true');
				document.getElementById('checkout').value='Please Call 1 300 55 66 75';
			}
			else
			{
				if(width<450)//rate = $100/m2
				{
					rate = 100;
					document.order_form.elements[beginRow+4].value = Cal_Price(width,drop,rate);
				}
				else// width => 450
				{
					if(drop<2300)//rate = $75/m2
					{
						rate = 75;
						document.order_form.elements[beginRow+4].value = Cal_Price(width,drop,rate);
					}
					else//rate = $90/m2
					{
						rate = 90;
						document.order_form.elements[beginRow+4].value = Cal_Price(width,drop,rate);
					}
					
				}
					
			}		
			
		}
		else if((wood == "63mm Basswood Light Oak") || (wood == "63mm Basswood Cedar Dark") ||(wood == "63mm Basswood Vivid White") || (wood == "63mm Basswood Chalk USA"))
		{
			//confirm lowest values for width and drop
			if ((width < 220 ) || (width > 2410) || (drop < 100) || (drop > 3600))
			{
				document.order_form.elements[beginRow+4].value = "CALL";
				document.getElementById('checkout').setAttribute('disabled', 'true');
				document.getElementById('checkout').value='Please Call 1 300 55 66 75';
			}
			else
			{
				if(width<450)//rate = $110/m2
				{
					rate = 110;
					document.order_form.elements[beginRow+4].value = Cal_Price(width,drop,rate);
				}
				else// width => 450
				{
					if(drop<2300)//rate = $95/m2
					{
						rate = 95;
						document.order_form.elements[beginRow+4].value = Cal_Price(width,drop,rate);
					}
					else//rate = $100/m2
					{
						rate = 100;
						document.order_form.elements[beginRow+4].value = Cal_Price(width,drop,rate);
					}
					
				}
					
			}		
			
		}
		else if ((wood == "Vertical Cedar - Dark") || (wood == "Vertical Cedar - Medium") || (wood == "Vertical Cedar - Light" ))
		{
			// TODO:  confirm lowest values for width and drop
			if ((width < 300 ) || (width > 3000) || (drop < 200) || (drop > 2250))
			{
				document.order_form.elements[beginRow+4].value = "CALL";
				document.getElementById('checkout').setAttribute('disabled', 'true');
				document.getElementById('checkout').value='Please Call 1 300 55 66 75';
			}
			else
			{
				// round up width 
				if (width < 600) { width = 600 }
				else if ((width%100) > 0)
				{ 
					width = 100 - +(width%100) + +width; 
				}
				// round up drop
				if (drop > 1500) { drop = 2250; }
				else if (drop >= 200) { drop = 1500; }

				loadXMLDataForPrice("vertical_89x4.xml");
			}
		}
		else 
		{

	if ((width < 280 ) || (width > 2410) || (drop < 200) || (drop > 3000))
		{

			document.order_form.elements[beginRow+4].value = "CALL";
				document.getElementById('checkout').setAttribute('disabled', 'true');
				document.getElementById('checkout').value='Please Call 1 300 55 66 75';
		}
	else {

		// round up width 
		if (width > 2310) { width = 2410;}
		else if (width < 410) {width = 410;}
		else if (((width - 10)%100) > 0)
			{ 
				width = 100 - +((width - 10)%100) + +width; 
			}

		// round up drop
		if (drop > 2700) { drop = 3000; }
		else if (drop > 2500) { drop = 2700; }
		else if (drop > 2400) { drop = 2500; }
		else if (drop > 2300) { drop = 2400; }
		else if (drop > 2200) { drop = 2300; }
		else if (drop > 2100) { drop = 2200; }
		else if (drop > 2000) { drop = 2100; }
		else if (drop > 1900) { drop = 2000; }
		else if (drop > 1800) { drop = 1900; }
		else if (drop > 1700) { drop = 1800; }
		else if (drop > 1600) { drop = 1700; }
		else if (drop > 1500) { drop = 1600; }
		else if (drop > 1400) { drop = 1500; }
		else if (drop > 1300) { drop = 1400; }
		else if (drop > 1200) { drop = 1300; }
		else if (drop > 1100) { drop = 1200; }
		else if (drop > 1000) { drop = 1100; }
		else if (drop > 900) { drop = 1000; }
		else if (drop > 600) { drop = 900; }
              else if (drop >= 200) { drop = 600; }


	if ( (wood == "46mm Cedar Med to Light") || (wood == "46mm Cedar Med to Dark"))
		{
			loadXMLDataForPrice("cedar.xml");
		}
       else if ((wood == "60mm Cedar Med to Light") || (wood == "60mm Cedar Med to Dark"))
              {
                     loadXMLDataForPrice("cedarwide.xml");
              } 
        else if (wood == "Rustic Cedar")
              {
                     loadXMLDataForPrice("rustic_cedar.xml");
              }
	else if (wood == "Empty")
		{
			document.order_form.elements[beginRow+4].value = 0;
		}
	}
		}
}

	
	calc_total(); 
}

function calc_total() 
{
total = 0;
i = 0;

while (i < 10)
{
	if (document.order_form.elements[(6*i)+4].value == "CALL")
	{
				document.getElementById('checkout').value='Please Call 1 300 55 66 75';
				document.getElementById('checkout').setAttribute('disabled', 'true');
	total = "Call for Price";
	break;
	}
	else 
	{
		total = total + +document.order_form.elements[(6*i)+4].value;

	}
i++;
}
	 	document.order_form.sub_total.value = total;
		document.order_form.grand_total.value = total;
}



function reset_submit()
	{
				document.getElementById('checkout').value='Proceed to Checkout';
				document.getElementById('checkout').removeAttribute('disabled');
	}
function Cal_Price(width,drop,rate)
{
	if((width * drop / 1000000)<1)
	{
		return rate;
	}
	else
	{
		var c = (width * drop / 1000000) * rate;
		return c = Math.round(c*100)/100; 
	}
}

