var strErrorMsg;

function check_age(age_lower, age_upper){
	if (document.main.age.value < age_lower || document.main.age.value > age_upper) {
    	strErrorMsg += " - Please enter an age between " + age_lower + " and " + age_upper + " \n";
    	correct = false;
  	}
}

function check_waist_size(){
	if (document.main.wu.selectedIndex == 0) {
	    if (document.main.ws.value < 10 || document.main.ws.value > 250) {
      		strErrorMsg += " - Waist Size: Please enter a value between 10 inches and 250 inches. \n";
      		correct = false;
    	}
	}
  	else {
		if (document.main.ws.value < 26 || document.main.ws.value > (250*2.54)) {
			strErrorMsg += " - Waist Size: Please enter a value between 26 cm and 635 cm. \n";
      		correct = false;
    	}
  	}
}

function check_hip_size(){
	if (document.main.hu.selectedIndex == 0) {
    	if (document.main.hs.value < 10 || document.main.hs.value > 250) {
	   		strErrorMsg += " - Hip Size: Please enter a value between 10 inches and 250 inches. \n";
    		correct = false;
    	}
	}
  	else {
	    if (document.main.hs.value < 26 || document.main.hs.value > (250*2.54)) {
    		strErrorMsg += " - Hip Size: Please enter a value between 26 cm and 635 cm. \n";
      		correct = false;
    	}
  	}
}

function check_weight() {
	if (document.main.wu.selectedIndex == 0) {
    	if (document.main.w.value < 40 || document.main.w.value > 500) {
     		strErrorMsg += " - Weight: Please enter a value between 40 lbs and 500 lbs. \n";
      		correct = false;
    	}
  	}
  	else {
    	if (document.main.w.value < 18 || document.main.w.value > 227) {
      		strErrorMsg += " - Weight: Please enter a value between 18 kg and 227 kg. \n";
      		correct = false;
    	}
  	}
}

function check_height() {
	if (document.main.hu1.selectedIndex == 0 && document.main.hu2.selectedIndex == 0) {
    	if ((((document.main.h1.value * 12)+(document.main.h2.value * 1)) < 58) || (((document.main.h1.value * 12) + (document.main.h2.value * 1)) > 84)) {
     		strErrorMsg += " - Height: Please enter a value between 58 inches and 84 inches. \n";
      		correct = false;
    	}
  	}
  	else {
    	if ((((document.main.h1.value * 100)+(document.main.h2.value * 1)) < 147) || (((document.main.h1.value * 100) + (document.main.h2.value * 1)) > 214)) {
      		strErrorMsg += " - Height: Please enter a value between 147 cm and 214 cm. \n";
      		correct = false;
    	}
  	}
}

function check_workout_duration() {
	if ((document.main.wd.value <= 1 || document.main.wd.value == "") && (document.main.wdu.selectedIndex == 0)) {
		strErrorMsg += " - Duration of Workout: Please enter a value greater than 1 minute.";
		correct = false;
	}
	else if (document.main.wd.value == "") {
		strErrorMsg += " - Duration of Workout: Please enter a value.";
		correct = false;
	}
}

function showCalcHelpWindow(openFile) {
	window.open(openFile,"CalcHelpWindow","toolbar=yes,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=375,height=375");
}

function submitPage(f1,f2,action) {
	strErrorMsg = "Please correct the following errors with your submission: \n";
	correct = true;
	if (f1 == "h") {
		check_height();
	}
	else if (f1 == "a") {
		var age_lower = 18;
		var age_upper = 120;
		check_age(age_lower, age_upper);
	}
	else if (f1 == "wt") {
		check_waist_size();
	}
	else if (f1 == "w") {
		check_weight();
	}
	
	
	
	if (f2 == "w") {
		check_weight();
	}
	else if (f2 == "hp") {
		check_hip_size();
	}
	else if (f2 == "wd") {
		check_workout_duration();
		setHidden();
	}
			
	if (correct == false) {
		alert(strErrorMsg);
	}
	else {
		document.main.action = action;
		document.main.submit();
	}
}

//Creates an object with all url params.
var urlParams = new Object();
var urlQstring = location.search.substring(1);
var urlPairs = urlQstring.split("&");
for (var i=0; i<urlPairs.length; i++) {
	var thisPos = urlPairs[i].indexOf('=');
	if (thisPos == -1) continue;
	var urlParamName = urlPairs[i].substring(0,thisPos);
	var urlParamVal = urlPairs[i].substring(thisPos+1);
	urlParams[urlParamName] = unescape(urlParamVal);
}
