﻿$(document).ready(function () {

    $('ul.menu > li').not('.menuActive').children('a').prepend('<div class="menuLeft"></div><div class="menuRight"></div>');

    $('.menuActive').children('a').prepend('<div class="menuActiveL"></div><div class="menuActiveR"></div>');


    // menu hover
    $('ul.menu > li').hover(
    function () {
        $(this).find('div.menuRight').css('background-image', 'url(/_Images/menu_active_r.jpg)');
        $(this).find('div.menuLeft').css('background-image', 'url(/_Images/menu_active_l.jpg)');
        $(this).addClass('menuHover');
        $(this).find('div.absMenuDiv').show();
    },
    function () {
        $(this).removeClass('menuHover');
        $(this).find('div.menuRight').css('background-image', 'url(/_Images/li_right.jpg)');
        $(this).find('div.menuLeft').css('background-image', 'url(/_Images/li_left.jpg)');
        $('div.absMenuDiv').hide();
    });


    // search clear text
    $('div.searchInput input').focus(function () {
        $(this).val('');
    });

    // product page nav hover
    $('ul.prodPage li').hover(
    function () {
        $(this).addClass('prodPageHover');
    },
    function () {
        $(this).removeClass('prodPageHover');
    });

    // product tabs hover
    $('div.prodTabs ul.prodTabsList li').hover(
    function () {
        $(this).css('background-color', '#F1F7F9');
    },
    function () {
        $(this).css('background-color', '#FAFBFB');
    });


    $('table.prodTable tr:even').addClass('prodTrEven');
    $('table.prodTable tr td:first').css('font-weight', 'bold');

    // product tabs hover
    $('.loginEnter , .loginReg , .loginRegForm , .loginLostPass , .goBasket , .checkOutBut').hover(
    function () {
        $(this).css('background-position', 'center bottom');
    },
    function () {
        $(this).css('background-position', 'center top');
    });

    // members page tabs hover    

    $('ul.membersTabs li').hover(
    function () {
        $(this).css('background-color', '#F7FBFE');
    },
    function () {
        $(this).css('background-color', 'white');
    });



    //basket table functions
    //$('table.basketTable tr.topBasket td:last').css('border-left', '0');
    $('table.basketTable tr td:eq(0)').css('width', '42%');
    $('table.basketTable tr td:gt(0)').css('width', '14%');

    $('table.basketTable tr td:eq(0)').addClass('basketTdF');
    $('table.basketTable tr td table tr td:eq(0)').removeClass('basketTdF');


    /* checout*/
    $('input.radio').click(function () {
        var checkRadio = this;
        $('input.radio').each(function () {
            if (checkRadio != this) {
                $(this).attr('checked', '');
            }
        });


    });

    $("a.cvvHelp").click(function () {
        window.open("/Medical/CheckOut/CVVHelp.htm", "CVV Help", "width=510,height=400");
    });

    $("a#lbAttAdress").click(function () {
        $('div.addNewAdress').toggle();
    });

    $('#ctl00_ContentPlaceHolder1_butPlaceOrder').click(function () {
        var check = true;
        var focus = false;
        var thisCheck;
        //dropyear
        thisCheck = $('#ctl00_ContentPlaceHolder1_dropYear');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == 0) {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        //dropMonth
        thisCheck = $('#ctl00_ContentPlaceHolder1_dropMonth');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == 0) {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        //cvv
        thisCheck = $('#ctl00_ContentPlaceHolder1_tbCardCVV');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == "") {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        //card number
        thisCheck = $('#ctl00_ContentPlaceHolder1_tbCardNumber');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == "") {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        //drop type
        thisCheck = $('#ctl00_ContentPlaceHolder1_dropCardType');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == 0) {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        //card holder name
        thisCheck = $('#ctl00_ContentPlaceHolder1_tbCardHolderName');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == "") {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        return check;

    });

    $("#ctl00_ContentPlaceHolder1_butAddShipmmentAdress").click(function () {
        var check = true;
        var focus = false;
        var thisCheck;
        //dropcountry
        thisCheck = $('#ctl00_ContentPlaceHolder1_dropShipCountry');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == 0) {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        // zipcode
        thisCheck = $('#ctl00_ContentPlaceHolder1_tbShipZip');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == "") {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        // city
        thisCheck = $('#ctl00_ContentPlaceHolder1_tbShipCity');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == "") {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        // adress
        thisCheck = $('#ctl00_ContentPlaceHolder1_tbShipAdress');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == "") {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        // fullname
        thisCheck = $('#ctl00_ContentPlaceHolder1_tbShipName');
        thisCheck.parent().prev('span').removeClass('fieldhover');
        if (thisCheck.val() == "") {
            thisCheck.parent().prev('span').addClass('fieldhover');
            if (!focus) { thisCheck.focus(); focus = true; }
            check = false;
        }
        return check;

    });

    // form validation
    $(".loginLostPass , .loginRegForm ,  .checkOutBut, .formCheck").click(function () {

        return checkForm();
    });



    function checkForm() {

        var check = true;
        var focus = false;
        $('div.regLine, div.regOneLine, div.regLineS').each(function () {

            var thisId = $(this).find('input').attr('id');
            var thisSelectId = $(this).find('select').attr('id');

            var thisIdInput = '#' + thisId;
            var thisIdSelect = '#' + thisSelectId;

            if ($(thisIdInput).parent('div').parent('div.regLine, div.regOneLine, div.regLineS').find('span').children('em').size() == 0) {
            }
            else {
                $(this).find('span').removeClass('fieldhover');
                if ($(thisIdInput).val() == "") {
                    $(this).find('span').addClass('fieldhover');
                    if (!focus) { $(thisIdInput).focus(); focus = true; }
                    check = false;
                }
            }

            if ($(thisIdInput).hasClass('phoneCheck')) {
                $(this).find('span').removeClass('fieldhover');
                var phoneCheck = $(thisIdInput).val();
                if (phoneCheck.length < 8 || !(IsNumeric(phoneCheck))) {
                    $(this).find('span').addClass('fieldhover');
                    if (!focus) { $(thisIdInput).focus(); focus = true; }
                    check = false;
                }
            }

            if ($(thisIdInput).hasClass('emailCheck')) {
                $(this).find('span').removeClass('fieldhover');
                var mailCheck = $(thisIdInput).val();
                if (mailCheck == "" || !(emailChek(mailCheck))) {
                    $(this).find('span').addClass('fieldhover');
                    if (!focus) { $(thisIdInput).focus(); focus = true; }
                    check = false;
                }
            }

            if ($(thisIdSelect).hasClass('selectCheck')) {
                $(this).find('span').removeClass('fieldhover');
                var selectCheck = ($(thisIdSelect).val())
                if (selectCheck == 0) {
                    $(this).find('span').addClass('fieldhover');
                    if (!focus) { $(thisIdInput).focus(); focus = true; }
                    check = false;
                }
            }

            if ($(thisIdInput).hasClass('IDCheck')) {
                $(this).find('span').removeClass('fieldhover');
                var phoneCheck = $(thisIdInput).val();
                if (phoneCheck.length < 9 || !(IsNumeric(phoneCheck))) {
                    $(this).find('span').addClass('fieldhover');
                    if (!focus) { $(thisIdInput).focus(); focus = true; }
                    check = false;
                }
            }

            if ($(thisIdInput).hasClass('VisaNumberCheck')) {
                $(this).find('span').removeClass('fieldhover');
                var phoneCheck = $(thisIdInput).val();
                if (phoneCheck.length < 16 || !(IsNumeric(phoneCheck))) {
                    $(this).find('span').addClass('fieldhover');
                    if (!focus) { $(thisIdInput).focus(); focus = true; }
                    check = false;
                }
            }


        });
        return check;
    };



    function emailChek(str) {

        var at = "@"
        var dot = "."
        var lat = str.indexOf(at)
        var lstr = str.length
        var ldot = str.indexOf(dot)
        if (str.indexOf(at) == -1) {

            return false
        }

        if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {

            return false
        }

        if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {

            return false
        }

        if (str.indexOf(at, (lat + 1)) != -1) {

            return false
        }

        if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {

            return false
        }

        if (str.indexOf(dot, (lat + 2)) == -1) {

            return false
        }

        if (str.indexOf(" ") != -1) {

            return false
        }

        return true
    }

    function IsNumeric(inputVal) {
        if (isNaN(parseFloat(inputVal))) {
            return false;
        }
        else {
            return true;
        }
    }

    $('ul.membersTabs li:first').addClass('regTabsActive').prepend('<div class="regTabsAr"><img src="/_Images/Shop/members/reg_tab_ar.jpg" alt="arrow" /></div>');
    $('ul.membersTabs li:last').css('border-right', '1px solid #E8E8E8');

    var memberIndex = 0;

    $('#tab1').css('display', 'block');
    $('ul.membersTabs li:first').addClass('regTabsActive');

    $('ul.membersTabs li.logout').click(function () {
        window.location = "/Medical/Logout/.aspx";
    });

    $('ul.membersTabs li').click(function () {

        var clickIndex = $("ul.membersTabs li").index(this);
        if (memberIndex != clickIndex) {
            $('ul.membersTabs li').removeClass('regTabsActive');
            $('ul.membersTabs li:eq(' + clickIndex + ')').addClass('regTabsActive');
            $('#tab' + (clickIndex + 1)).show();
            $('#tab' + (memberIndex + 1)).hide();
            memberIndex = clickIndex;

            $('ul.membersTabs li:eq(' + clickIndex + ')').addClass('regTabsActive')
            .prepend('<div class="regTabsAr"><img src="/_Images/Shop/members/reg_tab_ar.jpg" alt="arrow" /></div>');

            $('ul.membersTabs li').each(function () {

                if ($(this).hasClass('regTabsActive')) {
                    var rplcHover = $(this).children('img').attr('src').replace('normal', 'hover');
                    $(this).children('img').attr('src', rplcHover);
                }
                else {
                    var rplcHover = $(this).children('img').attr('src').replace('hover', 'normal');
                    $(this).children('img').attr('src', rplcHover);
                }
            });
        }

    });



});
