﻿var CalculatorPage = function() {
    CalculatorPage.Instance = this;
    this.CalculationIsValid = true;
    this.IsServiceAvailable = true;
    this.FinalStep;
}
    
CalculatorPage.prototype.ClearContainer = function(container){
    container.innerHTML = "";
}    

CalculatorPage.prototype.SetActiveNavigationLink= function(){
    var listItems = document.getElementById("calc-navigation-mainNavigation").getElementsByTagName('li');
    for(var i=0; i<listItems.length; i++){
        var str = location.href;
        var regExp = new RegExp(listItems[i].firstChild.href,"i")
        if(str.search(regExp) != -1){
            listItems[i].innerHTML = listItems[i].firstChild.innerHTML;
            listItems[i].className = "activeCalculator";
            break;
        }    
    }
}

CalculatorPage.prototype.KeyEventIsValid = function(e){
    var k = (window.ActiveXObject) ? event.keyCode : e.which;
    return (k == 13);
}

CalculatorPage.prototype.DisplayProcessingInContainer = function(container){
    container.innerHTML = "<img id=\"processing\" src=\"/Assets/Media/Images/1.0.0/loading_bar.gif\" />";
}
  
CalculatorPage.prototype.DisplayStep = function(n){
    if(typeof n == 'object'){
        n.style.display = "block";
    }
    else{
        var step = (typeof n == 'string' && !parseInt(n) > 0) ? n :'calc-calculator-step'+n;
        $get(step).style.display = "block";
        window.scrollTo(0,$get(step).offsetTop + 50);
    }
}

CalculatorPage.prototype.HideStep = function(n){
    var currentStep;
    if(typeof n == 'object'){
        currentStep = n;
        n.style.display = "none";
    }
    else{
        var step = (typeof n == 'string' && !parseInt(n) > 0) ? n :'calc-calculator-step'+n;
        currentStep = $get(step);
        currentStep.style.display = "none";
    }
    while(currentStep.nextSibling != undefined || currentStep.nextSibling != null){
        currentStep = currentStep.nextSibling
        if(currentStep.className == "calc-calculator-step")
            currentStep.style.display = "none";
    }
    this.SetCalculationIsValid(true);
}

CalculatorPage.HideStep = function(n){
    CalculatorPage.Instance.HideStep(n)
}

CalculatorPage.prototype.SetCalculationIsValid = function(isValid){
    var message = $get('calc-invalidCalculationMessage').style;
    
    switch (isValid){
        case true:
            $get('calculateButton').value = "Calculate";
            message.display = "none";
            break;
            
        case false:
            if(!(this.FinalStep.style.display == "none" || this.FinalStep.style.display == "")){
                $get('calculateButton').value = "Re-Calculate";
                message.display = "block";
            }
            break;
    }
}
 
CalculatorPage.prototype.SetPageWrapper = function(pageName){
    $get("calc-mainContainer").
        className = "calc-calculator-"+pageName;
}    