//Name:  specialtyform.js
//Created By:  Jarrid Mills
//Company:  Square Root Solutions, Inc.
//Date:     March 30, 2006
//Description: This file holds all the javascript used for the Specialty Item Form .

function filterNum(str) {
re = /^\$|,/g;
// remove "$" and ","
return str.replace(re, "");
}

function checkTotal(whichbox,fieldname) {
var vpriorvaluename = (fieldname.name + "priorvalue")
var vpricename = (fieldname.name + "price")
var vtotal = (fieldname.name + "total")
var vhiddentotal = (fieldname.name + "hiddentotal")
var vpriorvalue = document.aspnetForm.elements[vpriorvaluename].value
vpriorvalue = filterNum(vpriorvalue)
var vprice = document.aspnetForm.elements[vpricename].value
vprice = filterNum(vprice)
with (whichbox.form) {
if (isNaN(whichbox.value)) { 
whichbox.value = vprice;
whichbox.focus();
}
whichbox.value = Math.abs(whichbox.value);
var dec = whichbox.value.indexOf('.', 1)
if (dec > 0) { 
alert('No decimal places allowed for quantity!');
whichbox.value = vpriorvalue;
whichbox.focus();
}
hiddenfieldtotal.value = eval(vprice * whichbox.value);
hiddenfieldtotal.value = formatCurrency(hiddenfieldtotal.value);
document.aspnetForm.elements[vtotal].value = hiddenfieldtotal.value;
document.aspnetForm.elements[vhiddentotal].value = hiddenfieldtotal.value;

hiddentotal.value = filterNum(hiddentotal.value)
hiddentotal.value = eval(hiddentotal.value) - eval(vprice * vpriorvalue);
document.aspnetForm.elements[vpriorvaluename].value = whichbox.value;
hiddentotal.value = eval(hiddentotal.value) + eval(vprice * whichbox.value);
hiddentotal.value = formatCurrency(hiddentotal.value);
total.value = hiddentotal.value;
   }
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num)) num = "0";
cents = Math.floor((num * 100 + 0.5) % 100);
num = Math.floor((num * 100 + 0.5) / 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 ("$" + num + "." + cents);
}
function InitForm() {
document.aspnetForm.total.value = '$0';
document.aspnetForm.hiddentotal.value = 0;
for (xx = 0; xx < document.aspnetForm.elements.length; xx++) {
if (document.aspnetForm.elements[xx].type == 'text') { 
document.aspnetForm.elements[xx].value = 0;
   }
}

}

function putFocus(forminst,fieldname) {
document.forms[forminst].elements[fieldname].focus();
}