var page=1;

function changePage()
{

    Effect.Fade('splash' + page);
    setTimeout(Effect.Appear('splash' + (page==3 ? 1 : page+1)),1000);

    if(page==3)
        page=1;
    else
        page++;

    setTimeout(changePage,11000);
}

function formSubmit(form,func)
      {
        if(checkHtWt(form))
            $(form).submit();

      }

      function checkHtWt(f){
	height= getHeight(f);
	weight = getWeight(f);
	if(!isMetric(f))
	{
		if((height <51)||(height>94))
		{
			alert("The minimum and maximum heights allowed are 4ft 3in and 7ft 10in. Please make sure you are within these bounds!");
			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("The minimum and maximum weights allowed are 6st 4lbs and 40st. Please make sure you are within these bounds!");
			return false;
		}
	}
	else
	{
		//if(f.txtheight1.value=="")(Commenting out as the form uses height1 not txtheight1)
                if(height=="")
		{
			alert("Please re-enter height!");
			return false;
		}
		if((height<130)||(height>240))
		{
			alert("The minimum and maximum heights allowed are 1m 30cm and 2m 40cm. Please make sure you are within these bounds!");
			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("The minimum and maximum weights allowed are 40kg and 254kg. Please make sure you are within these bounds!");
			return false;
		}

	}
	return true;
}

      function getWeight(f)
       {
        var w1 = document.getElementById(f).elements['weight1'].value;
	var w2 = document.getElementById(f).elements['weight2'].value;
	if(isMetric(f))
	{
		if(w1=='') return 0
		return (parseInt(w1) * 1000)

	}
	else
	{

		if(w1 == '') return 0
		return (parseInt(w1) * 14) + parseInt(w2==''?0:w2);
	}
      }
     function getHeight(f)
      {
        var h1 = document.getElementById(f).elements['height1'].value;
	var h2 = document.getElementById(f).elements['height2'].value;
	if(h1=='') h1="0";
	if(h2=='') h2="0";

	if(isMetric(f))
	{
		return (parseInt(h1) * 100) + parseInt(h2);
	}
	else
	{
		return (parseInt(h1) * 12) + parseInt(h2);
	}
    }

    function isMetric(f)
    {

	var obj =document.getElementById(f).elements['measure'];
	if (obj!=null)
	{
		if(obj.value=="1")
			return true;
		else
			return false;
	}
	alert("not found");
	return false;
    }

    function tools_changeMeasure (id,imperial){

	var obj;
        var f;
        f = document.forms[id];

        if (f.measure.value=='1') imperial = false;

	if(!imperial)
	{
             //metric to imperial
             var h = parseInt(f.height1.value) * 100 + parseInt(f.height2.value);
             var w = f.weight1.value * 2.204622622 + .5;
             var ih1, ih2, iw1, iw2;

             if(isNaN(h)) h=0;
             if(isNaN(w)) w=0;

             h = (h * 0.393700787);
             ih1 = h / 12;
             ih2 = h % 12;
             iw1 = parseInt(w / 14);

             iw2 = w - (14 * iw1);

            f.height1.value=parseInt(ih1);
            f.height2.value=parseInt(ih2);
            f.weight1.value=parseInt(iw1);
            f.weight2.value=parseInt(iw2);

            nu_changeText('heightlabel1','ft');
            nu_changeText('heightlabel2','ins');
            nu_changeText('weightlabel1','st');
            nu_changeText('weightlabel2','lbs');
            showHideLayers('weightlabel2','show');
            showHideLayers('weight2','show');
            nu_changeText('measurechange','Click here to change to metric');
            f.measure.value="0"
	}
	else
	{

                //imperial to metric
                var h = parseInt(f.height1.value) * 12 + parseInt(f.height2.value==''?0:f.height2.value);
                var w = parseInt(f.weight1.value) * 14 + parseInt(f.weight2.value==''?0:f.weight2.value);

                if(isNaN(h)) h=0;
                if(isNaN(w)) w=0;

                var mh1, mh2;
                h = h * 2.54;
                h = h +.5;

                mh1 = h / 100;
                mh2 = h % 100;

                w = w * 0.45359237;
                //w = w * 0.45;

                f.weight1.value=FormatNumber(w,0,false,false,false);
                f.weight2.value="";
                f.height1.value=parseInt(mh1);
                f.height2.value=parseInt(mh2);

                nu_changeText('heightlabel1','m');
		nu_changeText('heightlabel2','cm');
		nu_changeText('weightlabel1','kg');
		showHideLayers('weightlabel2','hide');
		showHideLayers('weight2','hide');
//		nu_changeText('weightlabel2','&nbsp;g&nbsp;');
		nu_changeText('measurechange','Click here to change to imperial');
		f.measure.value="1"
	}



}
