
var mintot=70;
var maxtot=114;
function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

 function validate(target) 
 {        

	if (IsNumeric(target.value))
	{
		var openwidth = document.getElementById("openwidth");
		var openlength =document.getElementById("openlength");
		var unitedinches =document.getElementById("unitedinches");
		var baseprice = document.getElementById("baseprice");
		var addtobase = document.getElementById("addtobase");
		var price = document.getElementById("price");

		var total = (openwidth.value*1) + (openlength.value*1);
		var addtopricetot = total-70;
		var totalprice = formatCurrency((baseprice.value*1) + (addtopricetot*1));
		unitedinches.value = total;
		//alert(addtobase);
		addtobase.value = addtopricetot;
		/*alert(totalprice +"  "+mintot+"   "+maxtot);
		if (totalprice >= mintot & totalprice <= maxtot)
		{	price.value = totalprice;	}
		else
		{	
			alert('Not available in this size');
			target.focus();
		}*/
		 

	}
	else
	{
		alert("You must input numbers only.");        
		target.focus();
	}
 }

 function calcTotal() 
 {        

		var openwidth = document.getElementById("openwidth");
		var openlength =document.getElementById("openlength");
		var baseprice = document.getElementById("baseprice");
		var addtobase = document.getElementById("addtobase");
		var price = document.getElementById("price");
		
		var total = (openwidth.value*1) + (openlength.value*1);
		var addtopricetot = total-70;
		var totalprice = (baseprice.value*1) + (addtopricetot*1);
		if (totalprice < 78) {
			totalprice=78;
		}
		//alert(addtobase);

		//alert(total +"  "+mintot+"   "+maxtot);
		//if (total >= mintot & total <= maxtot)
		if (total <= maxtot)
		{	price.value = formatCurrency(totalprice);	}
		else
		{	
			alert('Sorry, Inside Storm Windows are not available in the size you have entered.\n\nPlease use the Contact Us to inquire about how me may assist your special need.');
			target.focus();
		}
 }

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
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);
}

function updatetotal()
{
	starti = 0;
	stopi = document.frmcart.toti.value;
	var currtot = 0;
	for(starti=0;starti<stopi;starti++)
	{
		var tot = document.getElementById("subtotal"+starti).innerHTML;
		currtot+=(Right(tot,(String(tot).length-1))*1);
		//alert(tot+"   " + currtot);
	}
	sumtotal =  document.getElementById("total");
	sumtotal.innerHTML = formatCurrency(currtot);
}


