function checkCharsLeft(textArea,charsLeft,maxChars){
if (textArea.value.length > maxChars){
textArea.value = textArea.value.substring(0, maxChars);}
else{ 
charsLeft.value = maxChars - textArea.value.length;}

}

function clearTextArea(textArea)
{

if (textArea.value == "Enter all size, color, fabric, style, and customizations info here:"){
textArea.value = "";}

}

function clearTextArea2(textArea)
{
if(textArea.value == "Enter all size, color, fabric, style, and customizations info here:"){
textArea.value = " none";}
}

function calcFabricPrice(yardage, amount, price, newprice, os1){

if (yardage.value > 0){
//round to the nearest cent
calcPrice = yardage.value * price.value * 100;
calcPrice = Math.round(calcPrice);
calcPrice = calcPrice/100;
amount.value = calcPrice;
if (yardage.value == 1) {os1.value = yardage.value + ' yard';}
else {
os1.value = yardage.value + ' yards';}}
else {
if (yardage.value != ""){
yardage.value = 1.0;
amount.value = price.value;}}
newprice.value = amount.value;
os1.value = yardave.value + ' yard';
}

function formatOrderTotal(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
{
num = "0";
alert("Error: Order Total is not in the correct format. \n Please input a number and click \"Calculate\"");
}
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 currencyToNum(num)
{
num = num.toString().replace("$","").replace(",","");
return num;
}

function calcShippingInsurance(ordertotal, amount, price,os0, addInsBtn)
{
addInsBtn.disabled = true;

ordertotal.value = formatOrderTotal(ordertotal.value);
var ordertotal2 = currencyToNum(ordertotal.value);

var ins_per_dollar = 0.009;

//in case of overflow do the following:
if(ordertotal2 <= 0) {
ordertotal2="0";
amount.value = "0.00"; 
price.value = "$0.00";
alert("Warning: Your Order Total must be between $0.01 and $999,999.99\r\nPlease correct your Order Total and click \"Calculate\".\r\nIf you have any concerns please contact Madam Selander at:\r\nMadamSelander@TheQuartermasterGeneral.com");}

if(ordertotal2 <= 600) {amount.value = "7.30"; price.value = "$7.30";}
if(ordertotal2 <= 500) {amount.value = "6.40"; price.value = "$6.40";}
if(ordertotal2 <= 400) {amount.value = "5.50"; price.value = "$5.50";}
if(ordertotal2 <= 300) {amount.value = "4.60"; price.value = "$4.60";}
if(ordertotal2 <= 200) {amount.value = "2.45"; price.value = "$2.45";}
if(ordertotal2 <= 100) {amount.value = "2.05"; price.value = "$2.05";}
if(ordertotal2 <= 50) {amount.value = "1.65"; price.value = "$1.65";}
if(ordertotal2 == 0.00) {amount.value = "0.00"; price.value = "$0.00";}
if(ordertotal2 > 600) {
var above600 = ordertotal2 - 600;
if(ordertotal2 > 5000) { 
above600 = 5000;
alert("Your Order exceeds $5000. There is a maximum of $5000 Insurance Coverage for orders.");
}
var shipins = above600*ins_per_dollar+7.30;
shipins = Math.round(shipins*100)/100;
amount.value = shipins;
price.value = formatOrderTotal(shipins);
}
if(ordertotal2 < 5000){
os0.value="Insurance Fee: "+price.value+"   Insured Amount: "+ordertotal.value;}
else
{os0.value="Insurance Fee: "+price.value+"   Insured Amount: $5000.00"; }
//enable button if all goes well
if(ordertotal2 > 0){
addInsBtn.disabled = false;}
else{
addInsBtn.disabled = true;}
}