﻿var heightCmSelect = $('heightCmSelect_reflected');
var heightFeetSelect = $('heightCmFeet_reflected');
var weightDecSelect = $('weightKg_reflected');
var weightImpSelect  = $('weightlbs_reflected');
var hipDecSelect = $('roster3$radio_hipsMeasureQ_System.Random_0_reflected');
var waistDecSelect = $('roster4$radio_waistMeasureQ_System.Random_0_reflected');

var finalScore = 0;
var user = 0;

var emailField = $('Email');
var sexField = $('sex');

heightCmSelect.addEvent('click', function() {             
    heightDecSelected();
    });
heightFeetSelect.addEvent('click', function() {             
    heightFeetSelected();
    });
weightDecSelect.addEvent('click', function() {             
    weightDecSelected();
    });
weightImpSelect.addEvent('click', function() {             
    weightImpSelected();
    });

function completeExpressSignup()
{
    if (emailField.value == "")
    {
        if(confirm("Entering your email address is optional, but it will let you retrieve your score if you come back to repeat the test. Click enter to continue, or cancel to enter your email address."))
        {
           fScrollTo(4); 
        }
    }
    else
    {
        // get the final score!
        var url = "services/expressTestUserReg.aspx?e=" + emailField.value + "&BREAKER" + randy();
        var req = new Request.HTML({
                method: 'get',
                url: url,
                onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript ) { getUser(responseHTML); }
                }).send();   
        fScrollTo(4);
    }
}

function getUser(responseHTML)
{
    user = responseHTML;
}
 
function completeExpressForm()
{

    // we should clear out the errors... 
    var err = false;
    
    var weight = 0; var weightMin = 40; var weightMax = 450;
    var height = 0; var heightMin = 120; var heightMax = 220;
    var hips = 0; var hipsMin = 25; var hipsMax = 400;
    var waist = 0; var waistMin = 25; var waistMax = 304;
    var sex = "male";

    sex = sexField.value;
    
    // we should go for it... 
    if (weightDecSelect.checked)
    {
        weight = Number($('weightcm_reflected').value);
        if (weight < weightMin || weight > weightMax)
        {
            err = true;
            $('weightcm_reflected').setStyle("background-color","red");
        }
        else
        {
            $('weightcm_reflected').setStyle("background-color","white");
        }
    }
    if (weightImpSelect.checked)
    {
        weight = Number($('weightst_reflected').value)*14;
        weight = weight + Number($('weightlb_reflected').value);
        weight = weight / 2.2;
        
        if (weight < weightMin || weight > weightMax)
        {
            err = true;
            $('weightst_reflected').setStyle("background-color","red");
            $('weightlb_reflected').setStyle("background-color","red");
        }
        else
        {
            $('weightst_reflected').setStyle("background-color","white");
            $('weightlb_reflected').setStyle("background-color","white");
        }
    }
    
    // height
    if (heightCmSelect.checked)
    {
        height = Number($('heightDecCm_reflected').value);
        if (height < heightMin || height > heightMax)
        {
            err = true;
            $('heightDecCm_reflected').setStyle("background-color","red");
        }
        else
        {
            $('heightDecCm_reflected').setStyle("background-color","white");
        }
    }
    if (heightFeetSelect.checked)
    {
        height = Number($('heightFeet_reflected').value)*12;
        height = height + Number($('heightIn_reflected').value);
        height = height * 2.54;
        
        if (height < heightMin || height > heightMax)
        {
            err = true;
            $('heightFeet_reflected').setStyle("background-color","red");
            $('heightIn_reflected').setStyle("background-color","red");
        }
        else
        {
            $('heightFeet_reflected').setStyle("background-color","white");
            $('heightIn_reflected').setStyle("background-color","white");
        }
    }
    
    hips = Number($('roster3_t_hipsMeasureDetail_System.Random_reflected').value);
    waist = Number($('roster4_t_waistMeasureDetail_System.Random_reflected').value);
    
    if (hips == 0)
    {
        err = true;
        $('roster3_t_hipsMeasureDetail_System.Random_reflected').setStyle("background-color","red");
    }
    else
    {
        $('roster3_t_hipsMeasureDetail_System.Random_reflected').setStyle("background-color","white");
    }
    
    if (waist == 0)
    {
        err = true;
        $('roster4_t_waistMeasureDetail_System.Random_reflected').setStyle("background-color","red");
    }
    else
    {
        $('roster4_t_waistMeasureDetail_System.Random_reflected').setStyle("background-color","white");
    }
    
    
    
    var hipwaist = waist / hips;
    
    // we should work out the details... 
    // pass these details to the form... bring back some info... 
    if (err)
    {
        alert("There seems to be a problem with the information that you have entered. Please check and try again.");
    }
    else
    {
        // get the final score!
        var url = "services/expressTest.aspx?w=" + weight + "&h=" + height + "&wh=" + hipwaist + "&s=" + sex + "&user=" + user + "&BREAKER" + randy();
        var req = new Request.HTML({
                method: 'get',
                url: url,
                onComplete: function(responseTree, responseElements, responseHTML, responseJavaScript ) { getScore(responseHTML, 2); }
                }).send();
    }
}

function getScore(xml)
{
    try
    {
        var score = Number(xml);
        if (score > 0)
        {
            finalScore = score;
            fScrollTo(6);   
            doResults();
        }
        else
        {
            alert("I'm sorry, the details you provided meant we couldn't work out a score. If you feel this is in error, please email support@testmyfitness.co.uk");
        }
    }
    catch(e){}
}

function doResults()
{
    // build the scores required... 
    // first, let's do the chart...
    $('NFTScore').innerHTML = finalScore;
    scorePerc = Math.round(finalScore / 10);
    $('percBar_totalPercentDisplay').innerHTML = "1000";
    $('percBar_percentage').innerHTML = scorePerc + "%";
    gr = Math.round(scorePerc * 3.5);
    bl = Math.round((100-scorePerc) * 3.5)-1;
    $('pp1').setStyle("width", gr + "px");
    $('pp2').setStyle("width", bl + "px");
    $('ppp').setStyle("margin-left", gr-12 + "px");
    style = "left" + getPercBand();
    style2 = "mid1" + getPercBand();
    $('ppB').set("class",style);    
    $('pp1').set("class",style2);
    scBand = 6 - getPercBand();
    $('scoreHolder1').set("id","scoreHolder" + scBand);
    scoreCol = getScoreCol();
    
    chart1.setDataXML("<chart canvasBgColor ='0E4C9F,272E62' canvasBgAngle='90' caption='' chartBottomMargin='0' showYAxisValues='0' showBorder='1' borderThickness='2' borderColor='ffffff' canvasBorderColor='ffffff' canvasBorderThickness='2' chartTopMargin='0' chartLeftMargin='0' chartRightMargin='0' xAxisName='' yAxisName='' showValues='1' placeValuesInside='1' yAxisMaxValue='1000' yAxisMinValue='0' setAdaptiveYMin='0' numDivLines='5' divLineColor='#05BCE8' showAlternateHGridColor='0' ChartNoDataText='NOTHING' showPlotBorder='0' plotGradientColor='' ><categories>    <category label='' />    <category label='' />    <category label='' />    <category label='' /></categories><dataset seriesName=''>  <set color='" + scoreCol + "' displayValue='" + finalScore + "' value='" + finalScore + "' toolText='Current Result - " + finalScore + "' />  <set showPlotBorder='0' color='#FFF04F' displayValue='0' value='0' toolText='Current Result - None' />  <set showPlotBorder='0' color='#FFF04F' displayValue='0' value='0' toolText='Current Result - None' />  <set showPlotBorder='0' color='#FFF04F' displayValue='0' value='0' toolText='Current Result - None' /></dataset></chart>");
    chart1.updateChart();
}

function getScoreCol()
{
        returnValue = "#A5E44F";

        if (finalScore < 1000)
        {
            returnValue = "#A5E44F";
        }
        if (finalScore < 800)
        {
            returnValue = "#D7F84F";
        }
        if (finalScore < 600)
        {
            returnValue = "#FFF04F";
        }
        if (finalScore < 400)
        {
            returnValue = "#FF9C00";
        }
        if (finalScore < 200)
        {
            returnValue = "#F64349";
        }

        return returnValue;
}

function getPercBand()
{
    returnValue = 5;
    scorePerc = Math.round(finalScore / 10);
    if (scorePerc < 100)
    {
        returnValue = 5;
    }
    if (scorePerc < 80)
        returnValue = 4;
    if (scorePerc < 60)
        returnValue = 3;
    if (scorePerc < 40)
        returnValue = 2;
    if (scorePerc < 20)
        returnValue = 1;
        
    return returnValue;
}
 
function heightDecSelected()
{
    $('heightDec_reflected').setStyle('display','inline');
    $('heightImp_reflected').setStyle('display','none');
}
function heightFeetSelected()
{
    $('heightDec_reflected').setStyle('display','none');
    $('heightImp_reflected').setStyle('display','inline');
}
function weightDecSelected()
{
    $('weightDec_reflected').setStyle('display','inline');
    $('weightImp_reflected').setStyle('display','none');
}
function weightImpSelected()
{
    $('weightDec_reflected').setStyle('display','none');
    $('weightImp_reflected').setStyle('display','inline');
}

function randy()
{
    return Math.random() * 99999999;
}