estring=""
function validate(field,type,dir) {
var valid = ".0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "yes") {
calculate(dir);}
if (ok == "no") {
alert("Your input was not a number!");
if (type=="w")
{
swidth=document.controlform.swidth.value;
document.controlform.width.value=swidth;
}

if (type=="h")
{
sheight=document.controlform.sheight.value;
document.controlform.height.value=sheight;
}

field.focus();
//field.select();

   }
}


function  calculate(dir) {


var height=0;
  var coof=0;
  var spheight=0;
  var spwidth=0;

pheight=document.controlform.pheight.value;
pwidth=document.controlform.pwidth.value;

spheight=pheight;
spwidth=pwidth ;
realcoof=pwidth/pheight;
realcoof=Math.round(realcoof*100)/100

var compare=22;
height=document.controlform.height.value;
mheight=Math.round(height*100)/100

mwidth=height*realcoof
mwidth=Math.round(mwidth*100)/100
document.controlform.mheight.value=mheight;
document.controlform.mwidth.value=mwidth;



 if (height>compare) 
  {
   height=compare;
alert("Max Height should be "+ compare+ " inch")
document.controlform.height.value=height;
document.controlform.width.value=height;
  }

 if (height<2) 
  {
   height=2;
alert("Min Height should be 2"+ " inch")
document.controlform.height.value=height;
document.controlform.width.value=height;
  }

  finalwidth=document.controlform.width.value;
  finalheight=document.controlform.height.value;
  coof=finalwidth/finalheight
  spheight=pwidth/coof
  spwidth=pwidth
  realcoof=coof



height=finalheight;
width=finalwidth;

var stb = new Array(100);
var sti = new Array(100);
var rtb = new Array(100);



sti[1]=1.965;
rtb[1]=5.32;


sti[2]=7.86;
rtb[2]=6.656;


sti[3]=31.42;
rtb[3]=11.84;


sti[4]=168.81;
rtb[4]=20.848;


sti[5]=300.08;
rtb[5]=27.928;


sti[6]=468.9;
rtb[6]=34.328;


sti[7]=675.18;
rtb[7]=41.568;


sti[8]=919.03;
rtb[8]=46.072;


sti[9]=1200.32;
rtb[9]=57.176;


sti[10]=1519.2;
rtb[10]=69.144;


sti[11]=1875.5;
rtb[11]=83.2;


sti[12]=2269.3;
rtb[12]=95.152;


sti[13]=2700.72;
rtb[13]=101.576;


sti[14]=3169.66;
rtb[14]=113.512;


sti[15]=3675.98;
rtb[15]=138.672;


sti[16]=4219.95;
rtb[16]=155.52;


sti[17]=4801.28;
rtb[17]=171.336;


sti[18]=5420.11;
rtb[18]=191.784;


sti[19]=6770.65;
rtb[19]=213.688;

effect=1;


var finalcoof=31.685;
firstcoof=finalcoof

var pricestring="";   
var materialcoof=1;



materialcoof=1
//alert(materialcoof)

   
var owidth = width;
var oheight= height;



var sp=0;
var fp=0;
var test_c=0;
calc=oheight*owidth
 

if (calc<=1000000 )
sp=0;fp=0;a=0;

for ( s = 1; s < 19; s++ )
{
if ((calc<=sti[s]) && a==0) 
 { a=a+1;sp=s-1;fp=s; }
}  

test_c=1;

if (fp==0 && sp==0)
{test_c=0;}

if (sp==0 && fp==1)  
{test_c=2;}



var  calc_price=0;
var  tpi=1;   
var  bti=1;
var adjust=0;

if (test_c==1) 
{ 
 for ( m=sp ;m<=fp ;m++)
  {
  tpi=1;
  bti=1;
  stb[m]=0;
  for (k=sp; k<=fp;k++)
   {if (k!=m) 
    { tpi=tpi*(calc-sti[k])
      bti=bti*(sti[m]-sti[k])
    }
   }
   stb[m]=rtb[m]*(tpi/bti);
   calc_price=calc_price+stb[m];
  }       
calc_price=calc_price*materialcoof;
realprice=(calc*6.45)/100

if (Math.round(calc_price)<=Math.round(realprice)) 
{         
calc_price=realprice}


if (oheight<14)
{calc_price=calc_price*1}

if (oheight>14 && oheight<17) 
{calc_price=calc_price*1}

if (oheight>17 && oheight<20) 
{calc_price=calc_price*0.99}

if (oheight>20) 
{calc_price=calc_price*0.98}

calc_price=calc_price*effect;

}


if (test_c==0) 
{

calc_price=calc/finalcoof;
calc_price=calc_price*materialcoof;

calc_price=calc_price*materialcoof;
realprice=(calc*6.45)/100

if (Math.round(calc_price)<=Math.round(realprice)) 
{         
calc_price=realprice}


if (oheight<14)
{calc_price=calc_price*1}

if (oheight>14 && oheight<17) 
{calc_price=calc_price*1}

if (oheight>17 && oheight<20) 
{calc_price=calc_price*0.9}

if (oheight>20) 
{calc_price=calc_price*0.8}

calc_price=calc_price*effect;

}

if (test_c==2) 
{

calc_price=calc/firstcoof;
calc_price=calc_price*materialcoof;
calc_price=calc_price*materialcoof;
realprice=(calc*6.45)/100

realprice=(calc*6.45)/100

if (Math.round(calc_price)<=Math.round(realprice)) 
{         
calc_price=realprice}


if (oheight<14)
{calc_price=calc_price*1}

if (oheight>14 && oheight<17) 
{calc_price=calc_price*1}

if (oheight>17 && oheight<20) 
{calc_price=calc_price*0.96}

if (oheight>20) 
{calc_price=calc_price*0.9}

calc_price=calc_price*effect;
}


currency_symbol='$';
pricestring='$'+Math.round(calc_price*100)/100;
pos=pricestring.indexOf(".");
len=pricestring.length;
if ((len-pos)==2)
pricestring=pricestring+'0';
if (pos==-1)
pricestring=pricestring+'.00';

document.controlform.coofw.value=realcoof;


document.controlform.price_.value=pricestring;
document.controlform.price_s.value=Math.round(calc_price*100)/100;




}

