Code:
=========================================
/********** VARIABLES **********/
var szMsg;
var fWaist;
var fHip;
var str_ind;
var str;
/********** VALIDATION **********/
//FUNCTION TO VALIDATE RANGES FOR NUMERIC ENTRIES
function verNumber(szValue,szQuestName,nMin,nMax,bOptional) {
var bIsNum = false;
//DETERMINE IF BOX IS EMPTY
if (szValue.length == 0 && !bOptional) {
szMsg += ' ' + szQuestName + ' requires a numeric value with no letters or spaces.\n';
}
//DETERMINE IF BOX IS NOT A NUMBER
if(str == "inc"){
for (var i=0; i< szValue.length; i++) {
if ((szValue.substring(i,i+1) != "0") &&
(szValue.substring(i,i+1) != "1") &&
(szValue.substring(i,i+1) != "2") &&
(szValue.substring(i,i+1) != "3") &&
(szValue.substring(i,i+1) != "4") &&
(szValue.substring(i,i+1) != "5") &&
(szValue.substring(i,i+1) != "6") &&
(szValue.substring(i,i+1) != "7") &&
(szValue.substring(i,i+1) != "8") &&
(szValue.substring(i,i+1) != "9") &&
(szValue.substring(i,i+1) != ".")) {
bIsNum = false;
szMsg += ' ' + szQuestName + ' requires a positive numeric
value with no letters or spaces.Please take care of format.\n';
break;
}
else {
bIsNum = true;
}
}
}else{
for (var i=0; i< szValue.length; i++) {
if ((szValue.substring(i,i+1) != "0") &&
(szValue.substring(i,i+1) != "1") &&
(szValue.substring(i,i+1) != "2") &&
(szValue.substring(i,i+1) != "3") &&
(szValue.substring(i,i+1) != "4") &&
(szValue.substring(i,i+1) != "5") &&
(szValue.substring(i,i+1) != "6") &&
(szValue.substring(i,i+1) != "7") &&
(szValue.substring(i,i+1) != "8") &&
(szValue.substring(i,i+1) != "9") &&
(szValue.substring(i,i+1) != ".")) {
bIsNum = false;
szMsg += ' ' + szQuestName + ' requires a positive numeric value with no letters or spaces.Please take care of format\n';
break;
}
else {
bIsNum = true;
}
}
}
if (bIsNum) {
if ((szValue > nMax) || (szValue < nMin)) {
szMsg += ' ' + szQuestName + ' requires a number between ' + nMin + ' and ' + nMax + '.\n';
}
}
}
/********** OTHER FUNCTIONS **********/
//FUNCTION TO HALT SUBMISSION AND GENERATE ALERT MESSAGE
//IF GLOBAL VARIABLE SZMSG IS NOT EMPTY.
function checkIt() {
//CHECK GLOBAL VAR SZMSG.
//IF NOT EMPTY PRINT CONTENTS AND RETURN VALUE OF FALSE
if (szMsg != "") {
//PRINT ALERT BOX
alert('________________________________________________________\n\n' +
'The following question(s) was not completed properly:\n\n' + szMsg +
'\n________________________________________________________\n\n' +
'Please correct and re-submit form.');
return false;
}
else {
return true;
}
}
/********** CALCULATION **********/
//FUNCTION TO CALCULATE WHR
function calcWHR() {
szMsg = "";
var xSize = 0;
fWaist = document.size.txtWaist.value;
fHip = document.size.txtHip.value;
str_ind = window.document.size.waist.selectedIndex;
str = window.document.size.waist.options[str_ind].value;
verNumber(fWaist,"Waist measurement",0,999,false);
verNumber(fHip,"Hip measurement",0,999,false);
if (checkIt()) {
if(str == "inc"){
fWaistMo = parseFloat(fWaist.substring(0, 2));
fHipMo = parseFloat(fHip.substring(0, 2));
fWaistBal = fWaist.substring(2,5);
if(fWaistBal != ""){
fWaistP = parseFloat(fWaist.substring(2,3));
fWaistP1 = parseFloat(fWaist.substring(4,5));
decPoint = fWaistP/fWaistP1;
fWaistMo = fWaistMo + decPoint;
}
fHipBal = fHip.substring(2,5);
if(fHipBal != ""){
fHipP = parseFloat(fHip.substring(2,3));
fHipP1 = parseFloat(fHip.substring(4,5));
decPoint = fHipP/fHipP1;
fHipMo = fHipMo + decPoint;
}
if(fWaistMo >= 1 && fWaistMo < 38.5 && fHipMo >= 36.5 && fHipMo < 38.5){
xSize = "XS";
}
if(fWaistMo >= 1 && fWaistMo < 40.5 && fHipMo >= 38.5 && fHipMo < 40.5){
xSize = "S";
}
if(fWaistMo >= 1 && fWaistMo < 42.5 && fHipMo >= 40.5 && fHipMo < 42.5){
xSize = "M";
}
if(fWaistMo >= 1 && fWaistMo < 44.5 && fHipMo >= 42.5 && fHipMo < 44.5){
xSize = "L";
}
if(fWaistMo >= 1 && fWaistMo < 46.5 && fHipMo >= 44.5 && fHipMo < 46.5){
xSize = "XL";
}
if(fWaistMo >= 1 && fWaistMo < 48.5 && fHipMo >= 46.5 && fHipMo < 48.5){
xSize = "2XL";
}
if(fWaistMo >= 1 && fWaistMo < 50.5 && fHipMo >= 48.5 && fHipMo < 50.5){
xSize = "3XL";
}
if(fWaistMo >= 1 && fWaistMo < 52.5 && fHipMo >= 50.5 && fHipMo < 52.5){
xSize = "4XL";
}
}else{
fWaistMo = parseFloat(fWaist);
fHipMo = parseFloat(fHip);
if(fWaistMo >= 1 && fWaistMo < 96.50 && fHipMo >= 91.50 && fHipMo < 96.50){
xSize = "XS";
}
if(fWaistMo >= 1 && fWaistMo < 101.50 && fHipMo >= 96.50 && fHipMo < 101.50){
xSize = "S";
}
if(fWaistMo >= 1 && fWaistMo < 106.50 && fHipMo >= 101.50 && fHipMo < 106.50){
xSize = "M";
}
if(fWaistMo >= 1 && fWaistMo < 111.50 && fHipMo >= 106.50 && fHipMo < 111.50){
xSize = "L";
}
if(fWaistMo >= 1 && fWaistMo < 116.50 && fHipMo >= 111.50 && fHipMo < 116.50){
xSize = "XL";
}
if(fWaistMo >= 1 && fWaistMo < 121.50 && fHipMo >= 116.50 && fHipMo < 121.50){
xSize = "2XL";
}
if(fWaistMo >= 1 && fWaistMo < 126.50 && fHipMo >= 121.50 && fHipMo < 126.50){
xSize = "3XL";
}
if(fWaistMo >= 1 && fWaistMo < 131.50 && fHipMo >= 126.50 && fHipMo < 131.50){
xSize = "4XL";
}
}
if(xSize == 0){
alert("We apologize but it appears we were unable to calculate your size.");
}
size.txtWHR.value = xSize;
}
}
function FillHip()
{
str_ind = window.document.size.waist.selectedIndex;
str = window.document.size.waist.options[str_ind].value;
if(str == "inc"){
window.document.size.hip.options[0].value = "inc";
window.document.size.hip.options[0].text = "inches";
}else{
window.document.size.hip.options[0].value = "cms";
window.document.size.hip.options[0].text = "cms";
}
}
=========================================
The calculator works fine but obviously I am missing something because
Bookmarks