// ===================================================================
// DECOSTA Enterprises Inc.
// Profile Analysis
// ===================================================================

function isBlank(val){if(val==null){return true;}for(var i=0;i<val.length;i++){if((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}}return true;}
function isInteger(val){if(isBlank(val)){return false;}for(var i=0;i<val.length;i++){if(!isDigit(val.charAt(i))){return false;}}return true;}
function isNumeric(val){return(parseFloat(val,10)==(val*1));}
function isDigit(num){if(num.length>1){return false;}var string="1234567890";if(string.indexOf(num)!=-1){return true;}return false;}
function isRadioChecked(obj){
	var radio_choice = false;
	for (counter = 0; counter < obj.length; counter++){
		if (obj[counter].checked)
			radio_choice = true; 
		}
	if (!radio_choice){
		return (false);
	}
	return (true);
}
function checkForm(){
	hideResults();
	error = '';
	formobj = document.profile;
	if(formobj.age.value.length == 0){
		error += 'Please enter Age\n';
	}else if(!isInteger(formobj.age.value)){
		error += 'Age must be an integer\n';
	}else if(formobj.age.value < 12 || formobj.age.value > 100){
		error += 'Age must be between 12 and 100\n';
	}
	if(!isRadioChecked(formobj.sex)){
		error += 'Please select a sex\n';
	}
	if(formobj.weight.value.length == 0){
		error += 'Please enter Weight\n';
	}else if (!isInteger(formobj.weight.value)){
		error += 'Weight must be an integer\n';
	}else if(formobj.weight.value < 90 || formobj.weight.value > 400){
		error += 'Weight must be between 90 and 400 lbs.\n';
	}
	if(formobj.height_ft.value.length == 0){
		error += 'Please enter Height ft.\n';
	}else if (!isInteger(formobj.height_ft.value)){
		error += 'Height ft. must be an integer\n';
	}else if (formobj.height_ft.value < 4 || formobj.height_ft.value > 9){
		error += 'Height ft. must be between 4 and 9 feet\n';
	}
	if(formobj.height_in.value.length == 0){
		error += 'Please enter Height in.\n';
	}else if(!isInteger(formobj.height_in.value)){
		error += 'Height in. must be an integer\n';
	}else if(formobj.height_in.value > 11){
		error += 'Height in. must be less than 12\n';
	}
	if(!isRadioChecked(formobj.goal)){
		error += 'Please select your goal\n';
	}
	if(formobj.target_weight.value.length == 0){
		error += 'Please enter Target Weight\n';
	}else if(!isInteger(formobj.target_weight.value)){
		error += 'Target Weight must be an integer\n';
	}else if(formobj.target_weight.value < 90 || formobj.target_weight.value > 400){
		error += 'Target Weight must be between 90 and 400 lbs.\n';
	}else if(formobj.goal[0].checked && formobj.target_weight.value >= formobj.weight.value){
		error += 'Target Weight must be less than current weight\n';
	}
	if(!isRadioChecked(formobj.lifestyle)){
		error += 'Please select your lifestyle\n';
	}
	if(!formobj.confirm.checked){
		error += 'You must agree to the terms and conditions\n';
	}
	if (error != ''){
		error = 'The following error(s) occurred\n' + error;
		alert(error);
	}else{
		showResults();
	}

}
function hideResults(){
	hideLifestyle('1');
	hideLifestyle('2');
	hideLifestyle('3');
	hideLifestyle('4');
	hideTerms();
	temp = document.getElementById('results');
	temp.style.left = '-8000px';
	temp.style.top = '-8000px';
	temp = document.getElementById('todd');
	temp.style.left = '400px';
	temp.style.top = '20px';
}
function showLifestyle(lyrnum){
	hideLifestyle('1');
	hideLifestyle('2');
	hideLifestyle('3');
	hideLifestyle('4');
	temp = document.getElementById('life' + lyrnum);
	temp.style.left = '290px';
	temp.style.top = '330px';
}
function hideLifestyle(lyrnum){
	temp = document.getElementById('life' + lyrnum);
	temp.style.left = '-8000px';
	temp.style.top = '-8000px';
}
function showTerms(){
	temp = document.getElementById('terms');
	temp.style.left = '160px';
	temp.style.top = '400px';
}
function hideTerms(){
	temp = document.getElementById('terms');
	temp.style.left = '-8000px';
	temp.style.top = '-8000px';
}
function showBMI(){
	temp = document.getElementById('bmiCalculated');
	temp.style.left = '160px';
	temp.style.top = '130px';
}
function hideBMI(){
	temp = document.getElementById('bmiCalculated');
	temp.style.left = '-8000px';
	temp.style.top = '-8000px';
}
function showResults(){
	
	if(document.profile.goal[1].checked){
		document.profile.target_weight.value= document.profile.weight.value;
	}
	
	var height = (document.profile.height_in.value*1 + (document.profile.height_ft.value * 12));
	var weight = document.profile.weight.value;
	var goal = document.profile.goal[0].checked?document.profile.goal[0].value:document.profile.goal[1].value;
	var goal_weight = document.profile.target_weight.value;
	var activity = document.profile.lifestyle[0].checked?document.profile.lifestyle[0].value:document.profile.lifestyle[1].checked?document.profile.lifestyle[1].value:document.profile.lifestyle[2].checked?document.profile.lifestyle[2].value:document.profile.lifestyle[3].value;
	var age = document.profile.age.value;
	var sex = document.profile.sex[0].checked?document.profile.sex[0].value:document.profile.sex[1].value;
	var bmi = calcBMI(weight,height);
	var gbmi = calcBMI(goal_weight,height);
	var weight185 = calcWeight('18.5',height);
	var weight25 = calcWeight('25',height);
	var weight30 = calcWeight('30',height);
	var calories = calcCalories(sex,weight,height,age,activity,goal);
	if (calories < 1200){
		calories = 1200;
	}
	if(gbmi < 18.5){
		hideResults();
		alert('WARNING\nYou have selected a goal which is under the recommended weight for the height entered.\nYour minimum target weight should be greater than ' + Math.round(weight185) + ' lbs.\nPlease make the appropriate changes and try again');
		document.profile.target_weight.value = Math.round(weight185 + 1);
		return;
	}
	temp = document.getElementById('bmivalue');
	temp.innerHTML = Math.round(bmi*10)/10;
	temp = document.getElementById('bmiweight185');
	temp.innerHTML = Math.round(weight185) + ' lbs.';
	temp = document.getElementById('bmiweight25');
	temp.innerHTML = Math.round(weight25)  + ' lbs.';
	temp = document.getElementById('bmiweight30');
	temp.innerHTML = Math.round(weight30)  + ' lbs.';
	var d = new Date();
	var todaysDate = Math.round(d.getMonth()+1) + '/' + d.getDate() + '/' + d.getFullYear();
	temp = document.getElementById('todaysDate');
	temp.innerHTML = todaysDate;
	var myDate=new Date();
	myDate.setDate(myDate.getDate()+49);
	var laterDate = Math.round(myDate.getMonth()+1) + '/' + myDate.getDate() + '/' + myDate.getFullYear();
	temp = document.getElementById('laterDate');
	temp.innerHTML = laterDate;
	temp = document.getElementById('height');
	temp.innerHTML = document.profile.height_ft.value + '&prime; ' + document.profile.height_in.value + '&Prime;';
	temp = document.getElementById('currentWeight');
	temp.innerHTML = document.profile.weight.value;
	temp = document.getElementById('ageLyr');
	temp.innerHTML = document.profile.age.value;
	temp = document.getElementById('goalWeight');
	temp.innerHTML = document.profile.target_weight.value;
	temp = document.getElementById('calories');
	temp.innerHTML = Math.round(calories);
	bmi_start_pos = -15;
	bmi_start = 14.25;
	bmi_ratio = 16;
	if(bmi > 40){
		bmi = 40;
	}
	bmi_now_pos = (bmi_start_pos * 1) + ((bmi - bmi_start) * bmi_ratio)
	temp = document.getElementById('bminow');
	temp.style.left = bmi_now_pos + 'px';
	if(gbmi > 40){
		gbmi = 40;
	}
	bmi_goal_pos = (bmi_start_pos * 1) + ((gbmi - bmi_start) * bmi_ratio)
	temp = document.getElementById('bmigoal');
	temp.style.left = bmi_goal_pos + 'px';

	temp = document.getElementById('todd');
	temp.style.left = '-8000px';
	temp.style.top = '-8000px';


	temp = document.getElementById('results');
	temp.style.left = '0px';
	temp.style.top = '0px';
	
	document.profile.submit_btn.value = 'Recalculate your results';
	
}
function calcBMI(weight,height){
	vbmi = ((weight * 703) / height) / height;
	return (vbmi);
}
function calcWeight(bmi,height){
	vweight = (bmi / 703) * height * height;
	return (vweight);
}
function calcCalories(sex,weight,height,age,activity,goal){
	if(sex == 'M'){
		vcal = (66 + (6.23 * weight) + (12.7 * height) - (6.8 * age)) * activity - goal;
	}else{
		vcal = (655 + (4.35 * weight) + (4.7 * height) - (4.7 * age)) * activity - goal;
	}
	return (vcal);
}
function setTarget(){
	document.profile.target_weight.disabled = true;
	document.profile.target_weight.value = document.profile.weight.value;
}
function unsetTarget(){
	document.profile.target_weight.disabled = false;
}



 function verify_required(field){
      var ok = field.value.length != 0;
      if (! ok) {
         error_alert(field.name,'This field is required '+(arguments.length > 1 ? arguments[1] : field.name));
         return false;
      }
      else no_error(field.name);
      return ok;
   }


 function verify_email(field){
      var atsign = field.value.indexOf("@");
      var period = field.value.lastIndexOf(".");
      var ok = (atsign > 0 && period > 2 &&  (period - atsign) > 1);
      if (! ok) {
         error_alert(field.name,'bad e-mail address in field '+(arguments.length > 1 ? arguments[1] : field.name));
         field.focus();
         field.select();
      }
      else no_error(field.name);
      return ok;
   }

 function no_error(name) {
      var id = name + "-error";
      if (document.layers && document.layers[id])           document.layers[id].visibility = "hide";
      else if (document.getElementById && document.getElementById(id))  document.getElementById(id).style.display = "none";
      else if (document.all && document.all[id])            document.all[id].style.display = "none";
   }

 function error_alert(name,message){
      var id = name + "-error";
      if (document.layers && document.layers[id])           document.layers[id].visibility = "show";
      else if (document.getElementById && document.getElementById(id))  document.getElementById(id).style.display = "block";
      else if (document.all && document.all[id])            document.all[id].style.display = "block";
      else                                               alert("ERROR:" + message);
   }



