﻿$(document).ready(function () {
    
   
    //Login dropdown
    //    $(".toggle_container").hide();

    //    $(".myAccount").click(function () {
    //        $(this).toggleClass("active").next().slideToggle("slow");
    //    });

    //    $(".close").click(function () {
    //        $(".toggle_container").slideUp('slow');
    //    });

    //    var wrapperHeight = $('.advertiseWrapper').height();
    //    $('.AdvertiseRight').height(wrapperHeight);

    var height = $('.OneColumnLeft').height();
    var height1 = $('.OneColumnRight').height();
    if (height1 < height) {
        $('.OneColumnRight').height(height);
    }


    var h1 = $('.directoryInfo').height();
    var h2 = $('.directorycontactWrapper').height();
    if (h1 > h2) {
        $('.directorycontactWrapper').height(h1+10);
    }

    $("#AccordianArticle").accordion();

    // Create Advert - Validation

    $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbSerial').alphanumeric();
    $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbPrice').numeric({ allow: ".," });
    $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbYear').numeric();

    $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_btnContinue').click(function (event) {

        var manu = $("#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_ddlManufacturer :selected").text();
        var model = $("#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_ddlModel :selected").text();
        var year = $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbYear').val();
        var ttaf = $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbTTAF').val();
        var serial = $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbSerial').val();
        var reg = $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbRegistration').val();
        var location = $("#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_ddlLocation :selected").text();
        var type = $("#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_ddlCurrency :selected").text();
        var price = $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbPrice').val();
        var action = $("#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_ddlAction :selected").text();
        var short = $('#MainContent_cphMain_CreateAdvertListing1_AdvertDetails1_AdvertTabs_pnlAdvertInfo_txbInShort').val();
        if (manu == "Select Manufacturer") {
            alert("Please Select a Manufacturer");
            event.preventDefault();
        }
        else if (model == "Select Model") {
            alert("Please Select a Model");
            event.preventDefault();
        }
        else if (year == null || year == "") {
            alert("Please enter year");
            event.preventDefault();
        }
        else if (ttaf == null || ttaf == "") {
            alert("Please enter TTAF");
            event.preventDefault();
        }
        else if (serial == null || serial == "") {
            alert("Please enter serial number");
            event.preventDefault();
        }
        else if (reg == null || reg == "") {
            alert("Please enter registration");
            event.preventDefault();
        }
        else if (location == "Select") {
            alert("Please Select a country");
            event.preventDefault();
        }
        else if (type == "Select Country") {
            alert("Please Select a country");
            event.preventDefault();
        }
//        else if (price == null || price == "") {
//            alert("Please enter price");
//            event.preventDefault();
//        }
        else if (action == "Select action") {
            alert("Please Select a action");
            event.preventDefault();
        }
        else if (short == null || short == "") {
            alert("Please enter short description");
            event.preventDefault();
        }
    });





    //// Checkout Validations Begins

    $('#MainContent_cphMain_Payment_txbCardNum').numeric();
    $('#MainContent_cphMain_Payment_txbStartDate').numeric();
    $('#MainContent_cphMain_Payment_txbExpiryDate').numeric();
    $('#MainContent_cphMain_Payment_txbCV2').numeric();

    $("#MainContent_cphMain_Payment_lnkPlaceOrder").click(function (event) {
        var number = $("#MainContent_cphMain_Payment_txbCardNum").val();
        if (number == null || number == "") {
            alert("Please Enter Card Number");
            event.preventDefault();
        }

        var type = $("#MainContent_cphMain_Payment_ddlCardType :selected").text();
        if (type == "Select Card") {
            alert("Please Select a type");
            event.preventDefault();
        }
        var sdate = $("#MainContent_cphMain_Payment_txbStartDate").val();
        if (sdate == null || sdate == "") {
            alert("Please Enter start date");
            event.preventDefault();
        }

        var edate = $("#MainContent_cphMain_Payment_txbExpiryDate").val();
        if (edate == null || edate == "") {
            alert("Please Enter expiry date");
            event.preventDefault();
        }
        var cv2 = $("#MainContent_cphMain_Payment_txbCV2").val();
        if (cv2 == null || cv2 == "") {
            alert("Please Enter security code");
            event.preventDefault();
        }

        var cname = $("#MainContent_cphMain_Payment_txbCardName").val();
        if (cname == null || cname == "") {
            alert("Please Enter name on card");
            event.preventDefault();
        }

        var name = $("#MainContent_cphMain_Payment_txbFirstName").val();
        if (name == null || name == "") {
            alert("Please Enter first name");
            event.preventDefault();
        }

        var sname = $("#MainContent_cphMain_Payment_txbLastName").val();
        if (sname == null || sname == "") {
            alert("Please Enter surname");
            event.preventDefault();
        }
        var address = $("#MainContent_cphMain_Payment_txbBillingAddress1").val();
        if (address == null || address == "") {
            alert("Please Enter address");
            event.preventDefault();
        }
        var town = $("#MainContent_cphMain_Payment_txbBillingCity").val();
        if (town == null || town == "") {
            alert("Please Enter town/city");
            event.preventDefault();
        }
        var postcode = $("#MainContent_cphMain_Payment_txbBillingPostcode").val();
        if (postcode == null || postcode == "") {
            alert("Please Enter postcode");
            event.preventDefault();
        }

        var country = $("#MainContent_cphMain_Payment_ddlCountry :selected").text();
        if (country == "United States") {
            var state = $("#MainContent_cphMain_Payment_txbBillingStatecode").val();
            {
                if (state == null || state == "") {
                    alert("Please Enter state code");
                    event.preventDefault();
                }
            }

        }

        //  testCreditCard();
    });


    function testCreditCard() {
        myCardNo = document.getElementById('MainContent_cphMain_Payment_txbCardNum').value;
        myCardType = document.getElementById('MainContent_cphMain_Payment_ddlCardType').value;
        if (checkCreditCard(myCardNo, myCardType)) {
            alert("Credit card has a valid format")
        }
        else { alert(ccErrors[ccErrorNo]) };
    }


    //// Checkout Validations Ends

    //Pop -up box - contact seller
    $('.contactLink').click(function (e) {
        var value = $(this).closest("table")
                       .siblings(":hidden")
                       .val();
        $('#MainContent_cphMain_DisplaySearchResults1_txbListingid').val(value);
        $('#MainContent_cphMain_DisplaySearchResults1_txbListingid').hide();
        $('#MainContent_cphMain_DealerProfile1_txbListingid').val(value);
        $('#MainContent_cphMain_DealerProfile1_txbListingid').hide();
    });


    //Pop -up box - dealer
    $('.lnkContact').click(function (e) {
        var value = $(this).closest("div")
                       .siblings(":hidden")
                       .val();
        $('#MainContent_cphMain_DealerSearch_DealerSearchResult1_txbDealerid').val(value);
        $('#MainContent_cphMain_DealerSearch_DealerSearchResult1_txbDealerid').hide();

    });


    $('.lnkDirectoryContact').click(function (e) {
        var value = $(this).closest("div")
                       .siblings(":hidden")
                       .val();
        $('#MainContent_cphMain_AviationDirectory1_DirectorySearchResult1_txbDirectoryid').val(value);
        $('#MainContent_cphMain_AviationDirectory1_DirectorySearchResult1_txbDirectoryid').hide();
    });



    //Homepage Banner
    $('#slides').slides({
        preload: true,
        play: 5000,
        pause: 2500,
        hoverPause: true,
        animationStart: function (current) {
            if (window.console && console.log) {
                console.log('animationStart on slide: ', current);
            };
        }
    });

    //Hide devider from last li item in menu - Top & Footer
    $('.toprow').last().css('background', 'none');
    $('.footerNav li').last().css('background', 'none');


    //Header Menu

    function megaHoverOver() {
        $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
        (function ($) {
            //Function to calculate total width of all ul's
            jQuery.fn.calcSubWidth = function () {
                rowWidth = 20;
                //Calculate row
                $(this).find("ul").each(function () { //for each ul...
                    rowWidth += $(this).width(); //Add each ul's width together
                });
            };
        })(jQuery);

        if ($(this).find(".row").length > 0) { //If row exists...

            var biggestRow = 0;

            $(this).find(".row").each(function () {	//for each row...
                $(this).calcSubWidth(); //Call function to calculate width of all ul's
                //Find biggest row
                if (rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });

            $(this).find(".sub").css({ 'width': biggestRow }); //Set width
            $(this).find(".row:last").css({ 'margin': '0' });  //Kill last row's margin

        } else { //If row does not exist...

            $(this).calcSubWidth();  //Call function to calculate width of all ul's
            $(this).find(".sub").css({ 'width': rowWidth }); //Set Width
        }
    }
    //On Hover Out
    function megaHoverOut() {
        $(this).find(".sub").stop().fadeTo('fast', 0, function () { //Fade to 0 opactiy
            $(this).hide();  //after fading, hide it
        });
    }

    var config = {
        sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
        interval: 100, // number = milliseconds for onMouseOver polling interval
        over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
        timeout: 500, // number = milliseconds delay before onMouseOut
        out: megaHoverOut // function = onMouseOut callback (REQUIRED)
    };

    $("ul#topnav li .sub").css({ 'opacity': '0' }); //Fade sub nav to 0 opacity on default
    $("ul#topnav li").hoverIntent(config);
});




$('dt').live('click', function () {
    var dd = $(this).next();

    if (!dd.is(':animated')) {
        dd.slideToggle();
        $(this).toggleClass('opened');
    }
});


