function baseImgRev(imgSrc,imgState,imgType) {
	if (!imgType) { imgType = 'gif'; }
	document[imgSrc].src = '/images/'+imgSrc+'_'+imgState+'.'+imgType;
}

function getItem(id) {
  var itm = false;
  if (document.getElementById) {
    itm = document.getElementById(id);
  } else if (document.all) {
    itm = document.all[id];
  } else if (document.layers) {
    itm = document.layers[id];
  }
  return itm;
}

var imgImg = 0;

function rotSplash(divIDArray,imgSrcArray,imgType,uLimit) {
	if (imgImg > uLimit) {
		imgImg = 0;
	}
	for (var i=0; i < divIDArray.length; i++) {
		var divID = getItem(divIDArray[i]);
		var curImg = imgSrcArray[imgImg]+'_'+divIDArray[i]+'.'+imgType;
		divID.style.background='#b3b3b3 url(/images/'+curImg+')';
	}
	imgImg = imgImg + 1;
}

function autoTab(theField,theForm) {
		if(theField.value.length == theField.getAttribute("maxlength")) {
						var elementArray = document.getElementsByTagName('input');
            for(i = 0; i < elementArray.length; i++) {
            	var element = elementArray[i];
            	if (element.getAttribute("tabindex") == (theField.tabIndex + 1)) {
            		var selElement = element;
            	}
            }
           selElement.focus();
        }
}

function satellite(srcFile,winHeight) {
	window.open(srcFile,null,'height='+winHeight+',width=685,status=no,toolbar=no,menubar=no,location=no,top=0,left=0,scrollbars=yes');
}

function lengthValidator(theField,maxLength,eXact) {
        newNumber = "" // string resulting from stripping away all non integers
        for (var i = 0; i < theField.value.length; i++) {
            parseResut = "" + (parseInt(theField.value.charAt(i))) // makes sure it is a string
            if (parseResut.length == 1) {
                newNumber = newNumber + "" + parseResut;
            }
        }   
        if (eXact == 1) {
	        if (newNumber.length == maxLength) {
	        		return true;
	            
	            } else {
	            		return false;
	        }
        } else {
        	if (newNumber.length >= maxLength) {
        			return true;
	            
	            } else {
	            		return false;
	        }
        }
    }

function ctFormValidator(theForm)
{
	if (theForm.altadd.checked == true) {
  	if (theForm.sstreet.value == "")
	  {
	    alert("Please enter your shipping address, or uncheck the box that indicates you want us to ship to an address other than the one associated with your credit card.");
	    theForm.sstreet.focus();
	    return (false);
	  }
	
	  if (theForm.scity.value == "")
	  {
	    alert("Please enter your shipping city, or uncheck the box that indicates you want us to ship to an address other than the one associated with your credit card.");
	    theForm.scity.focus();
	    return (false);
	  }
	
	  if (theForm.sstate.value == "")
	  {
	    alert("Please select your shipping state, or uncheck the box that indicates you want us to ship to an address other than the one associated with your credit card.");
	    theForm.sstate.focus();
	    return (false);
	  }
	
	  if (theForm.szip.value == "")
	  {
	    alert("Please enter your shipping zip code, or uncheck the box that indicates you want us to ship to an address other than the one associated with your credit card.");
	    theForm.szip.focus();
	    return (false);
	  }
  }

	if (theForm.email.value == "")
  {
    alert("Please enter your email address.");
    theForm.email.focus();
    return (false);
  }

  if (theForm.fname.value == "")
  {
    alert("Please enter your first name.");
    theForm.fname.focus();
    return (false);
  }

  if (theForm.lname.value == "")
  {
    alert("Please enter your last name.");
    theForm.lname.focus();
    return (false);
  }

  if (theForm.bstreet.value == "")
  {
    alert("Please enter your billing address.");
    theForm.bstreet.focus();
    return (false);
  }

  if (theForm.bcity.value == "")
  {
    alert("Please enter your billing city.");
    theForm.bcity.focus();
    return (false);
  }

  if (theForm.bstate.value == "")
  {
    alert("Please select your billing state.");
    theForm.bstate.focus();
    return (false);
  }

  if (theForm.bzip.value == "")
  {
    alert("Please enter your billing zip code.");
    theForm.bzip.focus();
    return (false);
  }

  if (theForm.parea.value == "")
  {
    alert("Please enter your phone number area code.");
    theForm.parea.focus();
    return (false);
  }

	if (theForm.pprefix.value == "")
  {
    alert("Please enter your phone number prefix.");
    theForm.pprefix.focus();
    return (false);
  }

  if (theForm.psuffix.value == "")
  {
    alert("Please enter your phone number suffix.");
    theForm.psuffix.focus();
    return (false);
  }

  if (theForm.cctype.options[theForm.cctype.selectedIndex].value == "")
  {
    alert("Please select your credit card type.");
    theForm.cctype.focus();
    return (false);
  }

	if (theForm.cctype.options[theForm.cctype.selectedIndex].value == "American Express") {
		if (lengthValidator(theForm.ccnumber,15,1) == false) {
	    alert("Please enter a valid American Express credit card number.");
	    theForm.ccnumber.focus();
	    return (false);
		}
	} else if (theForm.cctype.options[theForm.cctype.selectedIndex].value != "") {
		if (lengthValidator(theForm.ccnumber,16,1) == false) {
	    alert("Please enter a valid credit card number.");
	    theForm.ccnumber.focus();
	    return (false);
		}
	}

  if (theForm.ccmonth.options[theForm.ccmonth.selectedIndex].value == "")
  {
    alert("Please enter your credit card expiration month.");
    theForm.ccmonth.focus();
    return (false);
  }

  if (theForm.ccyear.options[theForm.ccyear.selectedIndex].value == "")
  {
    alert("Please enter your credit card expiration year.");
    theForm.ccyear.focus();
    return (false);
  }

  if (lengthValidator(theForm.ccccv,3) == false)
  {
    alert("Please enter a valid credit card CCV code.");
    theForm.ccccv.focus();
    return (false);
  }

  if (theForm.agree.checked == false) {
  	alert("You must first agree to our Terms of Service and Privacy Policy notices before you can purchase bodLab(TM) products.");
    theForm.agree.focus();
    return (false);
  }

	if (theForm.r_group.options[theForm.r_group.selectedIndex].value == "")
  {
    alert("Please choose which gym, club or fitness group referred you to us (if any).");
    theForm.r_group.focus();
    return (false);
  }

  theForm.submit();
}

function strpos( haystack, needle, offset) {
    var i = haystack.indexOf( needle, offset );
    return i >= 0 ? i : false;
}

function switchOut(visible,invisible,oSource,oVal) {
	if (visible != '') {
		var visSrc = getItem(visible);
	}
	if (invisible != '') {
		var invisSrc = getItem(invisible);
	}
	if (oSource != '') {
		var oSrc = getItem(oSource);
	}
	if (visSrc) {
		visSrc.style.display = '';
	}
	if (invisSrc) {
		invisSrc.style.display = 'none';
	}
	if (oVal) {
		oSrc.style.height = oVal;
	}
}

function non() {
}

function isDefined(variable) {
  return (typeof(window[variable]) == "undefined")?  false: true;
}

function bmFormValidator(theForm)
{

	if (!strpos(theForm.i.options[theForm.i.selectedIndex].text, 'Sample')) {

		var theTurkeys = 1;
		var theChickens = 1;
		var theBars = 1;
		for (i = 0; i < theForm.elements.length; i++) {
			if (typeof(theForm.elements[i].name) == 'string') { 
				var varNme = theForm.elements[i].name;
				if (varNme.indexOf('turkey') >= 0) {
					theTurkeys = theTurkeys + parseInt(theForm.elements[i].options[theForm.elements[i].selectedIndex].value);
				} else if (varNme.indexOf('chicken') >= 0) {
					theChickens = theChickens + parseInt(theForm.elements[i].options[theForm.elements[i].selectedIndex].value);
				} else if (varNme.indexOf('bar') >= 0) {
					theBars = theBars + parseInt(theForm.elements[i].options[theForm.elements[i].selectedIndex].value);
				}
			}
		}
		if (theTurkeys != 13) {
			alert ('Please choose no more or fewer than 12 BOD-MEAL turkey entrees.');
			return (false);
		} else if (theChickens != 9) {
			alert ('Please choose no more or fewer than 8 BOD-MEAL chicken entrees.');
			return (false);
		} else if (theBars != 4) {
			alert ('Please choose no more or fewer than 3 boxes of BOD-BARS.');
			return (false);
		}

		theForm.submit();
  }

  theForm.submit();
}
