function isValidForm1()
{
	var height, weight;
	var f = document.forms["fsignin"];	
	if (f.txtage.value=='')
	{
		alert('Please enter your age.');
		return false
	}

	if (!(f.optsex[0].checked || f.optsex[1].checked))
	{ 
		alert('Please enter your sex');
		return false;
	}
	if(parseInt(f.txtage.value)<18)
	{
		alert('you must be over 18.');
		return false;
	}

	height= getHeight(f);
	weight = getWeight(f);
	if(!isMetric(f))
	{
		if((height <51)||(height>94))
		{
			alert("Please re-enter height!");
			return false;
		}
		if(weight>560)
		{
			alert("Based on the targets you have entered, it is advisable to consult with your GP before starting a weight loss and/or exercise programme, therefore our system will not allow you to continue registering at this time.");			
			return false;
		}
		if(weight<88)
		{
			alert("Your weight is too low!");
			return false;
		}
	}
	else
	{
		if(f.txtheight1.value=="")
		{
			alert("Please re-enter height!");
			return false;
		}
		if((height<130)||(height>240))
		{
			alert("Please re-enter height!");
			return false;
		}
		if((weight>254000))
		{
			alert("Based on the targets you have entered, it is advisable to consult with your GP before starting a weight loss and/or exercise programme, therefore our system will not allow you to continue registering at this time.");			
			return false;
		}

		if((weight<40000)||(weight>254000))
		{
			alert("Please re-enter weight!");
			return false;
		}

	}
	if (!(f.optgoal[0].checked || f.optgoal[1].checked))
	{
		alert('Please enter your goal!');
		return false;
	}
	
 
	if (!(f.C1[0].checked || f.C1[1].checked || f.C1[2].checked || f.C1[3].checked))
	{
		if (!(f.optgoal[1].checked))
		{
			alert('Please enter your weekly weight loss!');
			return false;
		}
	}
	if(f.cmbtarget.length==1)
	{
		alert("Our system has been unable to calculate a target weight. This is usually due to you entering an incorrect weight/height or to your Body Mass Index being lower than the acceptable level for further weight loss to be recommended, hence why registration with our service is prevented. If you have an issue with your weight, please consult your doctor. ");
		return false;
	}
	if(f.cmbtarget.selectedIndex<=0)
	{
		alert('Please enter a target weight');
		return false;
	}
	var bmi,targetweight,targetbmi;
	targetweight = f.cmbtarget.value;
	if(!isMetric(f))
	{
		bmi = (weight * 703 * 10 / height/ height) / 10;	
		targetbmi = (targetweight * 703 * 10 / height/ height) / 10;	
	}
	else
	{
		bmi = (weight * 10) / (height * height);
		targetbmi = (targetweight * 10) / (height * height);
	}

	if(bmi<=18.5 || targetbmi<=18.5)
	{
		alert("Our system has calculated that your Body Mass Index is lower than the acceptable level for further weight loss to be recommended, hence why registration with our service is prevented. If you have an issue with your weight, please consult your doctor.");
		return false;
	}
	document.getElementById('page_1').style.visibility = 'hidden';
	document.getElementById('page_2').style.visibility = 'visible';
	return false;
}
function isValidForm2()
{
	var f = document.forms["fsignin"];	
	     if(f.chkbreast.checked)
        {
                alert('You are advised to consult your GP before starting any weight loss and/or exercise programme, therefore our system cannot allow you to continue with the registration process at this time.');
                return false;
        }
        if(f.chkpregnant.checked)
        {
                alert('You are advised to consult your GP before starting any weight loss and/or exercise programme, therefore our system cannot allow you to continue with the registration process at this time.');
                return false;
        }
        

        if(f.eatingdisorder.checked)
        {
                alert('You are advised to consult your GP before starting any weight loss and/or exercise programme, therefore our system cannot allow you to continue with the registration process at this time.');
                return false;
        }
		return true;
}
function isMetric(f)
{

	var obj =f.ismetric; 
	if (obj!=null)
	{
		if(obj.value=="1")
			return true;
		else
			return false;
	}
	alert("not found");
	return false;
}


function loadTargetWeight()
{
	var f;
	var minTarget;
	var maxTarget;
    var numItems = 0; 
	var selectednum=-1;
	var weight, percentage;
	f = document.forms["fsignin"]
	listClear(f.cmbtarget) 

	minTarget=parseInt(minWeight(getHeight(f),isMetric(f)));
	
	maxTarget=parseInt(maxWeight(getHeight(f),isMetric(f)));
	
	
    addOption = new Option("Select Goal Weight" ,"Select Goal Weight" );
    f.cmbtarget.options[numItems++] = addOption; 
	
	if(minTarget==0||maxTarget==0) return;
	var i,kg,g;
	weight = getWeight(f);

	if(isMetric(f))
	{
		minTarget = minTarget * 1000;
		maxTarget = maxTarget * 1000;
		if((weight - weight/5)>minTarget)
		{
			minTarget = weight - weight/5;
			maxTarget = weight;
		}
		if((weight)<maxTarget)
			maxTarget = weight 

		

		for (i = minTarget; i <= maxTarget; i=i+200){
			percentage = -Math.floor((weight-i) * 100 /weight);
			kg = i/1000;
			addOption = new Option(kg + ' Kg   (' + percentage + '% loss)',i );
		    f.cmbtarget.options[numItems++] = addOption; 
			if(i==tweightg)
				selectednum = numItems-1
	  	}
		i--
	}
	else
	{		
		minTarget = Convert_kg_lbs(minTarget);
		maxTarget = Convert_kg_lbs(maxTarget);

		if((weight - weight/5)>minTarget)
		{
			minTarget = weight - weight/5;
			maxTarget = weight;
		}
		if((weight)<maxTarget)
			maxTarget = weight 
		minTarget = Math.floor(minTarget)
		maxTarget = Math.floor(maxTarget)
		
		for (i = minTarget; i <= maxTarget; i++){
			percentage = Math.floor((weight - i) * 100 /weight );
			addOption = new Option(Math.floor(i/14) + 'st ' + (i % 14) + 'lbs   (' + percentage + '% loss)',i );
		    f.cmbtarget.options[numItems++] = addOption; 

			if(i==tweightlbs)
				selectednum = numItems-1
	  	}
		i--;

	}
	f.min_weight.value=minTarget;
	f.max_weight.value=i;
	if(selectednum==-1) selectednum=0;
	f.cmbtarget.selectedIndex= selectednum;	
	
}
function listClear(cmb)
{
	for (var i = cmb.options.length - 1; i >= 0; i--){
    cmb.options[i] = null;
  	}
	cmb.selectedIndex = -1;

}


function minWeight(height, metric)
{
	/*height should be eith in inchs or cm */
	
	if(metric)
		return parseInt((18.5 * height * height /10000) + 0.99) /*1 added to protect rounding*/
	else
		return parseInt((18.5 * Convert_in_cm(height) * Convert_in_cm(height) /10000 + 0.99))
}
function maxWeight(height, metric)
{
	/*height should be eith in inchs or cm */
	if(metric)
		return parseInt(24.99 * height * height /10000)
	else
		return parseInt(24.99 * Convert_in_cm(height) * Convert_in_cm(height) /10000)
}
function Convert_lbs_kg(lbs)
{
	return parseInt(lbs * 0.4536);
}
function Convert_kg_lbs(kg)
{
	return parseInt(kg / 0.4536);
}

function Convert_in_cm(inchs)
{
	return parseInt(inchs * 2.54);
}
function changeMeasure(resetdata, imperial)
{
	var f;
	f = document.forms["fsignin"];
	
	if (imperial==null)
	{
		
		if(f.ismetric.value=="1")
			imperial=false;
		else
			imperial=true;
	}
	
	
	nu_changeMeasure(f,imperial);
	if(resetdata)
	{
		if(imperial)
		{
			//imperial to metric 
			var h = parseInt(f.txtheight1.value) * 12 + parseInt(f.txtheight2.value);
			var w = parseInt(f.txtweight1.value) * 14 + parseInt(f.txtweight2.value==''?0:f.txtweight2.value);
			var mh1, mh2;
			h = h * 2.54;
			h = h +.5;
	
			mh1 = h / 100;
			mh2 = h % 100;
	
			w = w * 0.45359237;
			
			f.txtweight1.value=w;
			f.txtweight2.value="";		
			f.txtheight1.value=parseInt(mh1);
			f.txtheight2.value=parseInt(mh2);
		}
		
		else{
			//metric to imperial
			var h = parseInt(f.txtheight1.value) * 100 + parseInt(f.txtheight2.value);
			var w = f.txtweight1.value * 2.204622622 + .5;
			var ih1, ih2, iw1, iw2;
			
			h = (h * 0.393700787);
			ih1 = h / 12;
			ih2 = h % 12;
			iw1 = parseInt(w / 14);
			
			iw2 = w - (14 * iw1);
			
			f.txtheight1.value=parseInt(ih1);
			f.txtheight2.value=parseInt(ih2);
			f.txtweight1.value=parseInt(iw1);
			f.txtweight2.value=parseInt(iw2);
	
		}
	}
	loadTargetWeight();
	
}

function changeTarget()
{
	var f;
	var height,weight;

	
	f = document.forms["fsignin"]
	if(f.optgoal[1].checked)
	{
		listClear(f.cmbtarget) 
		addOption = new Option("Select Goal Weight" ,"Select Goal Weight" );
		f.cmbtarget.options[0] = addOption;
		weight= getWeight(f);
		if(weight!=0)
		{
			if(isMetric(f))
			{
				addOption = new Option(f.txtweight1.value + 'kg ',weight);
				f.cmbtarget.options[1] = addOption; 
			}
			else
			{
					addOption = new Option(f.txtweight1.value + 'st ' + f.txtweight2.value + 'lbs',weight);
					f.cmbtarget.options[1] = addOption; 
			}
			f.cmbtarget.selectedIndex=1;
		}
		else
			f.cmbtarget.selectedIndex=0;
		f.C1[0].disabled=true;
		f.C1[1].disabled=true;
		f.C1[2].disabled=true;
		f.C1[3].disabled=true;
	}
	else
	{
		f.C1[0].disabled=false;
		f.C1[1].disabled=false;
		f.C1[2].disabled=false;
		f.C1[3].disabled=false;
		loadTargetWeight()		
	}
}
function getWeight(f)
{
	
	if(isMetric(f))
	{
		if(f.txtweight1.value=='') return 0
		return (parseInt(f.txtweight1.value) * 1000) 
		
	}
	else
	{

		if(f.txtweight1.value=='') return 0
		return (parseInt(f.txtweight1.value) * 14) + parseInt(f.txtweight2.value==''?0:f.txtweight2.value);
	}
}
function getHeight(f)
{
	if(f.txtheight1.value=='') f.txtheight1.value="0"
	if(f.txtheight2.value=='') f.txtheight2.value="0"
		
	if(isMetric(f))
	{
		return (parseInt(f.txtheight1.value) * 100) + parseInt(f.txtheight2.value)
	}
	else
	{
		return (parseInt(f.txtheight1.value) * 12) + parseInt(f.txtheight2.value);
	}
}