function validate(form) {
var passed = false;
    if ((form.width.value=="")||(form.width.value=="0")) {
    fixElement(form.width, "Please include the width.");
    }
    else if ((form.height.value=="")||(form.height.value=="0")) {
    fixElement(form.height, "Please include the height.");
    }
    else if ((form.quantity.value=="")||(form.quantity.value=="0")) {
    fixElement(form.quantity, "Please include the quantity.");
    }
    else if (orders.btype.value=="c") {
    passed = curtainCk();
    }
    else {
    passed = true;
    }
    if (passed == true) {
    calculate();
    }
return passed;
}

function fixElement(element, message) {
alert(message);
element.focus();
}

function curtainCk() {
var passed = false;
if ((orders.fabric.value=="None")&&(orders.sheer.value=="None")) {
fixElement(orders.fabric, "Please select either a fabric or a sheer.");
}
else {
passed = true;
}
return passed;
}

function NumbCk(myobj) {
var myval=myobj.value;
var numChars="0123456789";
    for(var i=0; i<myval.length; i++) {
    for(var j=0; j<numChars.length; j++) {
    if(numChars.charAt(j)==myval.charAt(i)) {
    break;
    }
    else if(j==(numChars.length-1)) {
    myval = 0;
    }
    }
    }
    myval= +myval;
    myobj.value=myval;
     ck_dims();
     calculate();
}

function NumbCk2(myobj) {
     ck_dims();
     calculate();
}

function ck_dims() {
var w = parseInt(orders.width.value);
var h = parseInt(orders.height.value);
var q = parseInt(orders.quantity.value);
var type = orders.btype.value;
if (w < 3) {
orders.width.value=0;
} else if (w < 10) {
orders.width.value=w*100;
} else if (w < 30) {
orders.width.value=0;
} else if (w < 100){
orders.width.value=w*10;
} else if (w < 300) {
orders.width.value=0;
}

if (type=="c") {
if (w > 6000) {
orders.width.value=0;
}
if (h < 6) {
orders.height.value=0;
} else if (h<10) {
orders.height.value=h*100;
} else if (h<60) {
orders.height.value=0;
} else if (h<100) {
orders.height.value=h*10;
} else if (h<600) {
orders.height.value=0;
}
if (h > 6000) {
orders.height.value=0;
}

} else if (type=="b") {
if (w > 1700) {
orders.width.value=0;
}

if (h < 6) {
orders.height.value=0;
} else if (h<10) {
orders.height.value=h*100;
} else if (h<60) {
orders.height.value=0;
} else if (h<100) {
orders.height.value=h*10;
} else if (h<600) {
orders.height.value=0;
}
if (h > 3000) {
orders.height.value=0;
}
} else if (type="r") {
if (w > 1700) {
orders.width.value=0;
}

if (h < 6) {
orders.height.value=0;
} else if (h<10) {
orders.height.value=h*100;
} else if (h<60) {
orders.height.value=0;
} else if (h<100) {
orders.height.value=h*10;
} else if (h<600) {
orders.height.value=0;
}
if (h > 3000) {
orders.height.value=0;
}
}

}

function calculate() {
var w = parseInt(orders.width.value);
var h = parseInt(orders.height.value);
var q = parseInt(orders.quantity.value);
var type = orders.btype.value;
    if (isNaN(w)) {
    w=0;
    }
    if (isNaN(h)) {
    h=0;
    }
    if (isNaN(q)) {
    q=0;
    }
if ((w!=0)&&(h!=0)&&(q!=0)) {
    if (type=="c") {
    var baseCost = 96;
    var blockoutCost=0;
    if (orders.blockout.value=="YES") {
    var blockoutCost = 36;
    }
    var sheerCost = 45;
    if (orders.sheer.value=="None") {
    var sheerCost = 0;
    }
    var trimCost = 0;
    var rods_tracks = orders.rods_tracks.value;
    if (rods_tracks=="0") {
    var randtCost = 0;
    } else if (rods_tracks=="7") {
    var randtCost = 0.025*w*q;
    if (randtCost>69) {
    randtCost = 69;
    }
    } else {
    var randtCost = (18 + 0.048*w)*q;
    if (randtCost>280) {
    randtCost = 280;
    }
    }
    var tassels = orders.tassels.value;
    if (tassels=="None") {
    var tasselCost = 0;
    } else {
    var tasselCost = 180*q;
    }
    var ttlCost = baseCost + blockoutCost + sheerCost;

    var Numb = (w*h*q*ttlCost/1000000) + randtCost*q + tasselCost;
    
    } else if (type=="b") {
    var baseCost = 46;
    var material = orders.material.value;
    if (material=="Office") {
    var colour = orders.colour.value;
    if ((colour=="ML4001")||(colour=="ML4002")||(colour=="ML4003")) { 
    baseCost = 31;
    }
    
    } else if (material=="Block Out") {
    var blockout = orders.blockout.value;
        if ((blockout=="ML8901")||(blockout=="ML8904")) { 
    baseCost = 31;
    }

    } else if (material=="Silver Backed") {    
    var silverback = orders.silverback.value;
    if ((silverback=="ML8801")||(silverback=="ML8804")) { 
    baseCost = 31;
    }
    
    } else if (material=="Sunscreen") {
    var sunscreen = orders.sunscreen.value;
    if ((sunscreen=="ML4001")||(sunscreen=="ML4002")||(sunscreen=="ML4003")) { 
    baseCost = 31;
    } else {
    baseCost = 68;
    }

    }

    var Numb = (w*h*q*baseCost/1000000);
  
    } else {
    var baseCost = 60;
    var trimCost = 0;

    var Numb = 38 + (w*h*q*baseCost/1000000);

    }

    var NumbT = ""+Numb
    var NumLen = (NumbT).length;
    var atPos = NumbT.indexOf(".",0);
    if (atPos==-1) {
    Numb=Numb+".00";
    } else if (atPos==(NumLen-1)) {
    Numb=Numb+"00";
    } else if (atPos==(NumLen-2)) {
    Numb=Numb+"0";
    } else if (atPos<(NumLen-2)) {
    Numb=Math.round(Numb*100)/100;
    }
    orders.sub_total.value="$ "+Numb;
    } else {
    orders.sub_total.value="$ 0.00";
    }
}

function DisplaySwitching() {
var w0 = new getObj('myswitch0');
var w1 = new getObj('myswitch1');
var w2 = new getObj('myswitch2');
var w3 = new getObj('myswitch3');
var w4 = new getObj('myswitch4');
w0.style.display = 'none';
w1.style.display = 'none';
w2.style.display = 'none';
w3.style.display = 'none';
w4.style.display = 'none';

if (orders.material.selectedIndex==0) {
w0.style.display = 'block';
} else if (orders.material.selectedIndex==1) {
w1.style.display = 'block';
} else if (orders.material.selectedIndex==2) {
w2.style.display = 'block';
} else if (orders.material.selectedIndex==3) {
w3.style.display = 'block';
} else if (orders.material.selectedIndex==4) {
w4.style.display = 'block';
}
if (orders.btype.value != "sblinds") {
orders.sub_total.value="$ 0.00";
}
}

function getObj(name)
{
  if (document.getElementById)
  {
  	this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all)
  {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers)
  {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}
