
$(document).ready(function() {

    // $("ul.colapseExpandArea").css({ 'display': 'none' });
    
    var elUl = $('#countryContact').find('ul.colapseExpandArea').not($("li").parent('ul').hasClass('first'));    
    $(elUl).css({ 'display': 'none' }); ; //colapsing other item in Country Contact
    $("ul.first").css({ 'display': 'block' }); //expanding first item in Country Contact

    //jQuery Left Menu OnClick
    $("a:[rel=parent]").click(function(event) {
        event.preventDefault();
        //$(".colapseExpandArea").slideToggle();
        var whichUlColapse = $(this).parent('li').find('ul.colapseExpandArea');

        //alert(whichUlColapse);
        $(whichUlColapse).slideToggle(250);
        if ($(this).parent('li').hasClass('parentPage')) {
            //alert("has class selected");
            $(this).parent('li').toggleClass("parentPageDown");
        } else {
            $(this).parent('li').toggleClass("parentPage");
        }
    });
    // jquery tabs background change
    $("#tabsNav div.unselected").hover(
      function() {
          $(this).addClass("hovering");
      },
      function() {
          $(this).removeClass("hovering");
      }
    );

    //jQuery Left Menu OnLoad
    if ($("ul.colapseExpandArea").find('li').hasClass('selected') || $("ul.colapseExpandArea").find('li').hasClass('lastSelected')) {
        //alert($("ul.colapseExpandArea li.selected").parent().parent().hasClass);
        $("ul.colapseExpandArea li.selected").parent().parent().toggleClass("parentPageDown");
        $("ul.colapseExpandArea li.lastSelected").parent().parent().toggleClass("parentPageDown");
        $("ul.colapseExpandArea li.selected").parent().css({ 'display': 'block' });
        $("ul.colapseExpandArea li.lastSelected").parent().css({ 'display': 'block' });
    }
});

function CleanupTopRightMenuSelectedItem(){

    $(document).ready(function() {

    //jQuery Top Right Menu ItemSelected - replaces "a href" with span

        if ($(".supportLinkFirstActive").length > 0)
            $(".supportLinkFirstActive").replaceWith("<span class='supportLinkFirstActive'>" + $(".supportLinkFirstActive")[0].innerHTML + "</span>");
        if ($(".supportLinkActive").length > 0)
            $(".supportLinkActive").replaceWith("<span class='supportLinkActive'>" + $(".supportLinkActive")[0].innerHTML + "</span>");

    });
}
//all services menu opening / hiding login textboxes
$(document).ready(function() {
    /*$("li#allServices a, div#servicesMenu").click(function() {
    $("div#login input").toggleClass("hidden");
    $("div#servicesMenu").toggle();
    $("div.wideColumn object").toggle();
    });*/
/*    $("li#allServices a, div#servicesMenu").hover(function() {
        $("div#login input, div.wideColumn object").hide();
        $("div#servicesMenu").fadeIn();
    });
*/
    var inTimer = {};
    var outTimer = {};
    var menuOutTimer = {};
    $("li#allServices a").hover(function() {
        inTimer = $.timer(500, function() {
            $("div#login input, div.wideColumn object").hide();
            $("div#servicesMenu").fadeIn();
        });
    });
    // canceling the timer
    $('li#allServices a').mouseleave(function() {
        $.clearTimer(inTimer);
    });

    $("div#servicesMenu").mouseleave(function() {
        outTimer = $.timer(300, function() {
            $("div#servicesMenu").fadeOut();
            $("div#login input, div.wideColumn object").fadeIn();
        });
    });
    // canceling the timer
    $('li#allServices a').hover(function() {
        $.clearTimer(outTimer);
    });

    $("li#allServices a").mouseleave(function() {
        menuOutTimer = $.timer(300, function() {
            $("div#servicesMenu").fadeOut();
            $("div#login input, div.wideColumn object").fadeIn();
        });
    });
    // canceling the timer
    $('div#servicesMenu').hover(function() {
        $.clearTimer(menuOutTimer);
    });

    $("body").click(function() {
        $("div#servicesMenu").fadeOut();
        $("div#login input, div.wideColumn object").show();
    });    
});
//adding class to homepage left menu
$(document).ready(function() {
if ($(".startFullWidth").length > 0)
    $("#leftColumn").addClass("startLeft");
});

function BusinessLogin(username, password, loginPage) {
    $(document).ready(function() {
        $('#username').val($('#'+ username).val());
        $('#password').val($('#' + password).val());
        $("form:first").attr("action", loginPage);
        $("form:first").submit();
    });                            
}

function BusinessLogin1(username, password) {
    $(document).ready(function() {
        $('#username').val($('#' + username).val());
        $('#password').val($('#' + password).val());       
    });
}

function PrivateLogin(url) {
    $(document).ready(function() {
        $('a[rel="privatelogin"]').attr("href", "#").click(function() {
            window.open(url, '', 'menubar=0,location=0,toolbar=0,width=1024,height=700');
            return false;
        });
  });
}
                       
function DisableButtons() {
    $(document).ready(function() {
        //jQuery Disable buttons removing the value of the href        
        $("a:[disabled=disabled]").attr("href", "#");

    });
}

function validCalcForm(calc1, calc2, err) {
    var calc1_length;
    calc1_length = $("#" + calc1).val().length;
    var calc2_length;
    calc2_length = $("#" + calc2).val().length;
    if (calc1_length == 0 || calc2_length == 0) {
        $("#CalculationErrMsg").html(err).show().fadeOut(6000);
        return false;
    }
    return true;
}

function Print() {
    $(document).ready(function() {
    //jQuery hide all things not need to be printed
        //$("link").attr("href", "green.css");
        //return false;
        $('link[href="/Styles/print.css"]').attr("media", "screen");
        $('div.ppBlocks, div.bulletList').hide();
        $('a.button').hide();
        $('div#contactForm').hide();
        $('div#contactFormPrint').hide();
        $('div#genForm').hide();
        $('div#centerColumn h1, div#centerColumn p:first').hide();
        window.print();
        setTimeout(function() { UnPrint(); }, 1000);
    });
}

function UnPrint() {
        //jQuery show all things be printed
        $('link[href="/Styles/print.css"]').attr("media", "print");
        $('div.ppBlocks, div.bulletList').show();
        $('a.button').show();
        $('div#contactForm').show();
        $('div#contactFormPrint').show();
        $('div#centerColumn h1, div#centerColumn p:first').show();
        $('div#genForm').show();
}

$.jPrintArea = function(el) {
    var iframe = document.createElement('IFRAME');
    var doc = null;
    $(iframe).attr('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
    document.body.appendChild(iframe);
    doc = iframe.contentWindow.document;
    
    var links = window.document.getElementsByTagName('link');
    for (var i = 0; i < links.length; i++)
        if (links[i].rel.toLowerCase() == 'stylesheet')
        doc.write('<link type="text/css" rel="stylesheet" href="' + links[i].href + '"></link>');
    doc.write('<div class="' + $(el).attr("class") + '">' + $(el).html() + '</div>');
    doc.close();
    iframe.contentWindow.focus();
    iframe.contentWindow.print();
   
    //wait(1);
    setTimeout(function() { document.body.removeChild(iframe); }, 2000);
    //document.body.removeChild(iframe);
}

function startImport() {
    $(document).ready(function() {
        if (ValidatorValidate(jQuery("#<%= RequiredFieldValidator1.ClientID %>")[0]) && ValidatorValidate(jQuery("#<%= RegularExpressionValidator1.ClientID %>")[0]))
            {
                img = "<img alt='' src='/Styles/Images/ajaxloader1.gif'/>";
                jQuery('#loader').html(img);
            }
   });
}

function stopImport() {
    jQuery(document).ready(function() {
        jQuery('#loader').html = "";
    });
}

//numberType = thousands('T') or hundreds ('H')
//minValue, maxValue = loan amount that is too low or to high for the product
function Validate(filedId, minValue, maxValue, calculatorRoundingMode) {    
    jQuery(document).ready(function() {
        var elValue = $('#' + filedId).val();

        if (elValue) {
            // First validation - check if value is greater than maxvalue
            elValue = parseInt(elValue);
            if (elValue > parseInt(maxValue) || elValue < parseInt(minValue)) {
                $("#divCalculatorValidationError").show();
                $('#txtCalculation1Error1').show();
                $("#divCalculatorResult").slideUp();
                return false;
            }
            else {
                $('#txtCalculation1Error1').hide();
            }            
            //Second validation - check if value is thousands or hundreds (finland)
            if (calculatorRoundingMode == 'T') {
                if (elValue < 1000 || elValue % 1000 != 0) {
                    $("#divCalculatorValidationError").show();
                    $('#txtCalculation1Error2').show();
                    $("#divCalculatorResult").slideUp();
                    return false;
                }
                else {
                    $('#txtCalculation1Error2').hide();
                }
            }
            if (calculatorRoundingMode == 'H') {
                if (elValue < 100 || elValue % 100 != 0) {
                    $("#divCalculatorValidationError").show();
                    $('#txtCalculation1Error2').show();
                    $("#divCalculatorResult").slideUp();
                    return false;
                }
                else {
                    $('#txtCalculation1Error2').hide();
                }
            }
            if ($('#txtCalculation1Error1').css("display") == "none" && $('#txtCalculation1Error2').css("display") == "none")
                $("#divCalculatorValidationError").hide();
        }
        return true;
    });
    
}
