﻿function popup_show(showButtonId)
{
    // FireFox
    if ( document.createEvent )
    {
        var e = document.createEvent("MouseEvents");
        e.initEvent("click", true, true);
        document.getElementById(showButtonId).dispatchEvent(e);
    }
    // IE
    else
    {
        document.getElementById(showButtonId).click();
    }
}
    
function popup_onOk(hiddenId)
{   
    document.getElementById('popupArea').style.display = "none"; 
    var select =  $get(hiddenId);
    select.value = "selected" ;
    __doPostBack('', '');
}

function popup_onOkWizValidate(showButtonId, hiddenId, textValidateId)
{   
    if(popup_weightValidate(textValidateId)){
        document.getElementById('popupArea').style.display = "none"; 
        var select =  $get(hiddenId);
        select.value = "selected" ;
        __doPostBack('', '');
    }
    else
    {
        document.getElementById('popupError').style.display = "block"; 
        popup_show(showButtonId);
    }
}        
function popup_custom_onOkWizValidate(showButtonId, hiddenId, textValidateId1, textValidateId2, isWalking)
{   
    if(popup_bpmValidate(textValidateId1,textValidateId2,isWalking)){
        document.getElementById('popupArea').style.display = "none"; 
        var select =  $get(hiddenId);
        select.value = "selected" ;
        __doPostBack('', '');
    }
    else
    {
        document.getElementById('popupError').style.display = "block"; 
        popup_show(showButtonId);
    }
}        
function popup_weightValidate(textId)
{
    var weight = $get(textId).value;
    if (weight.match (/^[1-9][0-9]*$/))
    {
        w = parseInt(weight);
        if((20 <= weight) && (weight <= 200))
        {
            return true;
        }
    }
    return false;
}

function popup_bpmValidate(textId1,textId2, isWalking)
{
    var bpm1 = $get(textId1).value;
    var bpm2 = $get(textId2).value;
    
    if (bpm1.match (/^[1-9][0-9]*$/) && bpm2.match (/^[1-9][0-9]*$/))
    {
        bpm1Int = parseInt(bpm1);
        bpm2Int = parseInt(bpm2);
        
        bpmMin = (isWalking) ? 100 : 140;
        bpmMax = (isWalking) ? 140 : 200;
        
        
        if(bpm1Int <= bpmMax && bpm1Int >= bpmMin && bpm2Int <= bpmMax && bpm2Int >= bpmMin)
        {
            return true;
        }
    }
    return false;
}
