﻿var EN = 0;
var FR = 1;
var LANG = 0;

var validation = new Array("Please provide units for one of the products.","S'il vous plaît fournir des unités pour l'un des produits");
var ajaxError = new Array("Request could not be processed","Demande n'a pas pu être traitées");


$(document).ready(function(){
    //hide step 2
    $('#calculator-step2').hide();
    //Show step
    $('#calculator-step1').show();
    $('#button-step1').click(function(){
        if(validateStep1()){
            afterValidateStep1();
        }
    });
    $('#button-recalculate').click(function(){
        $('#calculator-step2').fadeOut('fast',function(){
            $('#calculator-step1').fadeIn('slow');    
        });                
    });
    
    $('#result-print').click(function(){
        PrintThisPage();
    });
});
function afterValidateStep1(){

    var cu = $('#hdnCornUnits').val();
    var sc = $('#hdnSoyConUnits').val();
    var sr = $('#hdnSoyRRUnits').val();
    var cornTotal = $('#hdnCornTotal').val();
    var soyConTotal = $('#hdnSoyConTotal').val();
    var soyRRTotal = $('#hdnSoyRRTotal').val();
    
    $('#purchaseForm .txtCornUnits').hide().html(cu).fadeIn();
    $('#purchaseForm .txtSoyConUnits').hide().html(sc).fadeIn();
    $('#purchaseForm .txtSoyRRUnits').hide().html(sr).fadeIn();
    
    //Calculate Discount
    $.ajax({
        type: 'GET',
        url:'/GrowersEdge/calculation.ashx',
        data:'ln=' + LANG + '&cu=' + cu + '&sc=' + sc + '&sr=' + sr,
        dataType: 'json',
        success: function(obj){
            drawSuggession(obj);
        },
        error: function(xhr, ajaxoptions, thrownError){                        
            var elem = $('<div>' + ajaxError[LANG] +'</div>')
            $('#discountTotal').append(elem.fadeIn('slow'));
        }
    });
}

function validateStep1(){
    var cu = $('#txtCorn').val();
    var sc = $('#txtConventional').val();
    var sr = $('#txtRR').val();
    
    cu = $.trim(cu) == '' ? 0 : cu;
    //cu = cu == 0 ? 50 : cu;
    sc = $.trim(sc) == '' ? 0 : sc;
    sr = $.trim(sr) == '' ? 0 : sr;                
    blnValid = true;
    if((cu <=0) &&  (sc <=0) && (sr <=0)) blnValid = false;                
    if(isNaN(cu) || isNaN(sc) || isNaN(sr)) blnValid = false;
    
    if(blnValid){ 
        var cornTotal = roundNumber(cornPrice * cu,2);
        var soyConTotal = roundNumber(soyConPrice * sc,2);
        var soyRRTotal = roundNumber(soyRRPrice * sr,2);
        
        $('#hdnCornUnits').val(cu);
        $('#hdnCornTotal').val(cornTotal);
        $('#hdnSoyConUnits').val(sc);
        $('#hdnSoyConTotal').val(soyConTotal);
        $('#hdnSoyRRUnits').val(sr)
        $('#hdnSoyRRTotal').val(soyRRTotal);                
    }else{
        alert(validation[LANG]);
    }                
    return blnValid;
}     

function roundNumber(num, dec) {
    return parseFloat(num).toFixed(2);
}
function drawSuggession(obj){
    //hide step 1
    $('#calculator-step1').fadeOut(function(){
        //Show step 2
        $('#calculator-step2').fadeIn();    
    });            
    
    $('#purchaseForm .subTotal,#paymentScheduleForm  .subTotal,#printEstimate  .subTotal').hide().html('$' + roundNumber(obj.CurrentSubTotal,2)).fadeIn('slow');
    $('#purchaseForm .discountPercent,#paymentScheduleForm .discountPercent,#printEstimate .discountPercent').hide().html(obj.CurrentDiscountPercent + '%').fadeIn('slow');
    $('#purchaseForm .discountTotal,#paymentScheduleForm .discountTotal,#printEstimate .discountTotal').hide().html('-$' + roundNumber(obj.CurrentSavings,2)).fadeIn('slow');
    $('#purchaseForm .finalTotal,#paymentScheduleForm .finalTotal,#printEstimate .finalTotal').hide().html('$' + roundNumber(obj.CurrentTotal,2)).fadeIn('slow');
    $('#hdnCurrentTotal').val(obj.CurrentTotal);
    
    var curCornUnits = $('#hdnCornUnits').val();
    var curSoyUnits = parseFloat($('#hdnSoyConUnits').val()) + parseFloat($('#hdnSoyRRUnits').val());
    
    showCurrentSavings(obj.CurrentSavings);
    
    $('#suggest').html('');    
    if(obj.CornUnits > 0){
        addPrompt(1,obj.CornUnits, (obj.CornSavings - obj.CurrentSavings), obj.CurrentCornUnits);
    }
    if($('#hdnSoyConUnits').val() > 0 && obj.SoyConUnits > 0){                
        addPrompt(2,obj.SoyConUnits, (obj.SoyConSavings - obj.CurrentSavings), obj.CurrentSoyConUnits);
    }
    if($('#hdnSoyRRUnits').val() > 0 && obj.SoyRRUnits > 0){                
        addPrompt(3,obj.SoyRRUnits, (obj.SoyRRSavings - obj.CurrentSavings), obj.CurrentSoyRRUnits);
    }
}


function PrintThisPage(){ 
    var sOption="toolbar=yes,location=no,directories=yes,menubar=yes,";
    sOption+="scrollbars=yes,width=761,height=600,left=100,top=25"; 

    var sWinHTML = $('#printArea').html();
    var winprint=window.open("","",sOption);
    winprint.document.open();

    winprint.document.write('<html><HEAD><LINK href="/styles/main.css" type="text/css" rel="stylesheet"><LINK href="/styles/styles.css" type="text/css" rel="stylesheet"></HEAD><body onload="print()">');
    winprint.document.write(sWinHTML);
    winprint.document.write('</body></html>');
    winprint.document.close();
    winprint.focus();
    return false;
}

function ValidateForm()
{   
    if(typeof(Page_ClientValidate) == 'function')
    {
        if(Page_ClientValidate())
        {
            var result = checkUnits();
            if(result == 0)
                return false;                    
        }
    }
}

function ClientValidate(source,arguments)
{
    if(arguments.Value==0 || arguments.Value=='')
    {
        arguments.IsValid=false;
    }
    else
    {
        arguments.IsValid=true;
    }
    
}

