$(document).ready(function() {
    $('.productpop').click(function() { ShowPop('') });




    InitValidations();

    // console.log($('#timers').html()) //doesn't work on IE.

    InitRollover();

    //submitbutton
    $('.submitbutton').click(function() {
        $(this).parents('form').submit()
    });
    //backbutton
    $('.backbutton').click(function() {
        history.back();
    });

    //slideshow
    try {
        $('.slideshow').cycle({});
    } catch (err) { }

    //txtCCNumber
    $('#txtCCNumber').change(function() {
        var t = GetCCID($(this).val());
        if (t > 0) $('#drpCCType').val(t + '');
    });




    //imglogo4
    $('.imglogo4').mouseover(function() { $('.logo_home').show(); MouseIsOnLogo = true; });
    $('.logo_fm').mousemove(function() { MouseIsOnLogo = true; });
    $('.logo_fm').mouseout(function() { MouseIsOnLogo = false; setTimeout('HideLogoHome()', 500); });

    //txtEmail

    $('.graytext').focus(function() {
        var v = $(this).val();
        $(this).removeClass('graytext');
        if (v == 'ENTER E-MAIL') {
            $(this).val('');
        } else {

        }
    });

    $('.graytext').blur(function() {
        var v = $(this).val();
        if (v == 'ENTER E-MAIL') {
            $(this).addClass('graytext');
        }
    });

    //ppdetails
    $('.ibtnproductdescription').click(function() {
        $('.product_detail').removeClass('product_detail2'); $('.plproductdetails').hide(); $('.plproductdescription').show()
    });
    $('.ibtnproductdetails').click(function() {
        $('.product_detail').addClass('product_detail2'); $('.plproductdetails').show(); $('.plproductdescription').hide()
    });


    //shop_side_title
    $('.shop_side_title').click(function() {

        // shop_side_title($('.shop_side_title').index(this));
    });

    //home_splash_close_img
    if ($('.home_splash').height() < 825) {
        $('.home_splash').height(825);
    }
    $('.home_splash_close_img').click(function() { $('.home_splash').remove(); });

    $('.home_splash').click(function() {
        if (goingremove == -1) goingremove = 1;
        setTimeout('home_splash_remove()', 500);
    });
    $('.home_splash a').click(function() {
        goingremove = 0;
    });

    //autopage

    InitAutoPages();
});


function InitAutoPages() {
    $('.autopage').each(function(id) {
        InitAutoPage($(this), id);
    });
}



function InitAutoPage(p, id) {
    p.addClass('autopage_' + id);
    var c = p.find('.page_item').length;
    var pagesize = parseInt(p.find('.pages_count').text());

    var ps = Math.ceil((c * 1.0) / pagesize);
    p.find('.pages_count').html('<span class="page_size none">' + pagesize + '</span> <span class="page_current none">1</span> <span class="page_all none">' + ps + '</span> <span class="page_previous">PREVIOUS</span> &bull;  <span class="page_next">NEXT</span> : <span class="page_begin">1</span>~<span class="page_end">' + c + '</span> OF <span class="page_allitems">' + c + '</span> ITEM' + (c > 1 ? 'S' : ''));
    ShowCurrentPage(p, id);
}

function ShowCurrentPage(p, id) {
    var c = parseInt(p.find('.page_current').text());
    var ps = parseInt(p.find('.page_all').text());
    var t = parseInt(p.find('.page_allitems').text());
    var sz = parseInt(p.find('.page_size').text());
    var b = (c - 1) * sz;
    var e = c * sz;
    if (e > t) e = t;

    p.find('.page_item').hide();
    for (var i = b; (i < e && i < t); i++) {
        p.find('.page_item').eq(i).show();
    }
    p.find('.page_begin').html((b + 1) + '');
    p.find('.page_end').html(e + '');

    if (c > 1) {
        p.find('.page_previous').html('<a href="#" onclick="return page_PREVIOUS(' + (c - 1) + ',' + id + ');">PREVIOUS</a>');
    } else {
        p.find('.page_previous').html('PREVIOUS');
    }

    if (c < ps) {
        p.find('.page_next').html('<a href="#" onclick="return page_PREVIOUS(' + (c + 1) + ',' + id + ');">NEXT</a>');
    } else {
        p.find('.page_next').html('NEXT');
    }

}

function page_PREVIOUS(c, id) {
    var p = $('.autopage_' + id);
    p.find('.page_current').html(c + '');
    ShowCurrentPage(p, id);
    return false;
}


function InitDrpColor() {
    var colors = ',';
    $('.li_color').each(function(i) {
        if (colors.indexOf(',' + $('.li_color').eq(i).attr('title') + ',') == -1) {
            colors += $('.li_color').eq(i).attr('title') + ',';
        }
    });

    var html = '<option value="">Color</option>';

    var arr = colors.split(',');
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].length > 0) {
            html += '<option value="' + arr[i] + '">' + arr[i] + '</option>';
        }
    }

    $('#drpColor').html(html);


    if ($('#drpColor option').length == 2) {
        $('#drpColor').val($('#drpColor option').eq(1).attr('value'));
    }

    if (SELECTED_COLOR.length > 0) {
        $('#drpColor').val(SELECTED_COLOR);
        SELECTED_COLOR = '';
    }

    drpColor_change();
}

function drpColor_change() {
    var oldSize = $('#drpSize').val();
    var color = $('#drpColor').val();

    var html = '<option value="">Size</option>';
    if (color.length > 0) {
        var inproductids = '';

        $('.li_color[title="' + color + '"] .productid').each(function() {
            inproductids += ', .li_size_' + $(this).html();
        });


        if (inproductids.length > 0) {
            inproductids = inproductids.substr(1);
            $(inproductids).each(function() {
                var v = $(this).attr('title');
                var t = $(this).find('.msg').html();
                var cl = '';
                if (t.length == 0) {
                    t = v;
                } else {
                    t = v + ' (' + t + ')';
                    cl = ' class="red"';
                }

                html += '<option value="' + v + '"' + cl + '>' + t + '</option>';
            });
        }
    }
    else {

        if ($('#drpColor option').length > 1) {

            html += '<option value="" disabled="disabled">Please select color first</option>';
        }
    }
    $('#drpSize').html(html);




    $('#drpSize').val(oldSize);


    if (SELECTED_SIZE.length > 0) {
        $('#drpSize').val(SELECTED_SIZE);
        SELECTED_SIZE = '';
    }

    drpSize_change();

}

function drpSize_change() {
    var color = $('#drpColor').val();
    var size = $('#drpSize').val();
    if (color.length > 0 && size.length > 0) {
        var productids = ' ';
        var selectedproductid = '';
        $('.li_color[title="' + color + '"] .productid').each(function() {
            productids += $(this).html() + ' ';
        });

        $('.li_size[title="' + size + '"] .productid').each(function() {
            if (productids.indexOf(' ' + $(this).html() + ' ') != -1) {
                selectedproductid = $(this).html();
            }
        });

        if (PRODUCTID != selectedproductid) {
            $('#txtProductID').val(selectedproductid);
            location.href = PRODUCT_URL.replace(/&amp;/g, '&') + '&sproductid=' + selectedproductid + '&color=' + color + '&size=' + size;
        }



    }
}

function InitPrintContent() {
    $('#txtPrintTitle').val(document.title);
    var v = '';
    $('.print_content').each(function(i) {
        v += $('.print_content').eq(i).html();
    });

    $('#txtPrintContent').val(v);
}

function tab_side_head(i) {
    $('.tab_side_head').attr('class', 'tab_side_head');
    $('.tab_side_head').addClass('tab_side_head_' + i);
    $('.tab_side_holder_' + ((i + 1) % 2)).hide();
    $('.tab_side_holder_' + i).show();
}
function InitLeftScroll() {
    var done = false;
    var h = 353;
    h = 331;
    if ($('.panel').length > 0) {

        $('.panel').each(function(i) {
            if ($('.panel').eq(i).height() > h) {
                $('.panel').eq(i).height(h);
                $('.panel').eq(i).parent().height(h);
                $('.panel').eq(i).jScrollPane({ showArrows: false, dragMinHeight: 60, dragMaxHeight: 60 });
                done = true;
            }
        });

    }
    if ($('.panel .on').length > 0 && done) {
        try {

            $('.panel')[0].scrollTo('.on');
        } catch (err) { }
    }
}


function ToggleSE2(id, obj) {
    if (!$(id).html()) return;
    if ($(id).css('display') == 'none') {
        $(id).show();
        $(obj).removeClass('bold');
        $(obj).removeClass('ibtnadd');
        $(obj).addClass('ibtndelete');
    } else {
        $(id).hide();
        $(obj).addClass('bold');
        $(obj).addClass('ibtnadd');
        $(obj).removeClass('ibtndelete');
    }
}
function ToggleSE(id, obj) {
    if (!$(id).html()) return;
    if ($(id).css('display') == 'none') {
        $(id).show();
        $(obj).removeClass('bold');
        $(obj).removeClass('ibtnadd');
        $(obj).addClass('ibtndelete');
        $(id).find('a:last').focus();
    } else {
        $(id).hide();
        $(obj).addClass('bold');
        $(obj).addClass('ibtnadd');
        $(obj).removeClass('ibtndelete');
    }
}


function ajaxlink_click(obj) {
    var href = $(obj).attr('href')
    var page = href + '&';
    if (page.indexOf('view=all') != -1) {
        page = 'all';
    } else {
        if (page.indexOf('page=') != -1) {
            page = page.substr(page.indexOf('page=') + 5);
            page = page.substr(0, page.indexOf('&'));
        }
    }

    var arr = pages.split(',');
    if (href.indexOf('cat=women') != -1) {
        arr[0] = page;
    }
    if (href.indexOf('cat=men') != -1) {
        arr[1] = page;
    }

    pages = arr[0] + ',' + arr[1] + ',' + arr[2] + ',' + arr[3];

    var loc = location.href;
    if (loc.indexOf('#') != -1) {
        loc = loc.substr(0, loc.indexOf('#'));
    }





    $(obj).text('Loading ...');
    $(obj).parents('.ajaxholder').load($(obj).attr('href'));
    location.href = loc + '#' + pages;
    return false;
}

function home_splash_remove() {
    if (goingremove == 1)
        $('.home_splash').remove();
}

var goingremove = -1;


function shop_side_title(i) {
    var show = $('.shopby_sub').eq(i);
    $('.shopby_sub').not(show).hide();
    show.show();
}




var MouseIsOnLogo = false;
function HideLogoHome() {
    if (MouseIsOnLogo) return;
    $('.logo_home').hide();
}

var noneedtoroll = false;

function InitRollover() {
    $('.rollover').mouseover(
    function() {

        if ($(this).attr('src')) {
            if ($(this).attr('src').indexOf('_button') != -1) {
                noneedtoroll = false;
                $(this).attr('src', $(this).attr('src').replace('_button', '_rollover'));
            } else {
                noneedtoroll = true;
            }
        }
    }
    );

    $('.rollover').mouseout(
    function() {
        if ($(this).attr('src')) {
            //  alert($(this).attr('src'));
            if (!noneedtoroll)
                $(this).attr('src', $(this).attr('src').replace('_rollover', '_button'));
        }
    }
    );
}


function dialogHTML(menutoColor, StaffName, StaffImg) {
    var html = '';
    html += '<div class="dialog_f"><img src="/images/buttons/featured_' + menutoColor + '.png" width="91" height="82" alt="" class="png" id="png_f" /><\/div>';
    html += '<div class="dialog_just_fm"><img src="/images/flags/oc_' + menutoColor + '_just.png" width="93" height="96" alt="" class="png" id="png_just" /><\/div>';
    if (StaffImg.length > 0) {
        html += '<div class="dialog_staff_fm"><a href="/staffs.asp?name=' + StaffName + '"><img src="' + StaffImg + '"  width="104"  alt="" class="png" id="png_staff"  /></a><\/div>';

    } else {
        html += '<div class="dialog_staff_fm"><img src="/images/flags/oc_' + menutoColor + '_staff.png" width="91"  height="83" alt="" class="png" id="png_staff"  /><\/div>';
    }
    html += '<div class="dialog_xclusive"><img src="/images/buttons/xclusive_' + menutoColor + '.png" width="95" height="95" alt="" class="png" id="png_xclusive" /><\/div>';

    if ($('.product_right_flag').length > 0) {
        html += '<div>' + $('.product_right_flag').html() + '</div>';
    }

    if (html.length > 0) {
        $('.dialog').html(html);
    }

}
function showdialogHTML(mc, sn, si, t5, t4, t116, tf) {
    $('.dialog').css({ 'top': '300px', 'left': '234px', 'width': '104px', 'height': 'auto' });
    dialogHTML(mc, sn, si);
    $('.dialog').show();
    if (t5 != '1') $('.dialog_just_fm').remove();
    if (t4 != '1') $('.dialog_staff_fm').remove();
    if (t116 != '1') $('.dialog_xclusive').remove();
    if (tf != '1') $('.dialog_f').remove();
}


function CheckCountry() {
    $.get('/ajax_ip2country.asp', function(data) {
        if (data != 'US' && data != '-') {
            $('.product_addcart').html('this product can only be shipped within the US');
        }
    });
}

function ShowFeaturedIcon(color) {
    $(document).ready(function() {
        $('.dialog').css({ 'top': '261px', 'left': '234px', 'width': '97px', 'height': '296px' });
        $('.dialog').html('<img src="/images/buttons/featured_' + color + '.png" width="91" height="82" class="png" id="png_featured" />');
        $('.dialog').show();
        if (document.all) {
            fixPng(document.getElementById('png_featured'));
        }
    });
}
function CheckOption(id) {
    if ($('#' + id + ' option').length > 1) return true;
    return false;
}
function InitValidations() {

    try {
        //checksize and color

        var AddToCartForm = $("#AddToCartForm").validate({
            rules: {
                Size: { required: CheckOption('drpSize') },
                Color: { required: CheckOption('drpColor') }
            }
        });

        var giftform = $("#giftform").validate({
            rules: {
                FirstName_For: "required",
                LastName_For: "required",
                Email_For: { required: true, email: true },
                FirstName_From: "required",
                LastName_From: "required",
                Email_From: { required: true, email: true }
            }
        });

        var login_form = $("#login_form").validate({
            rules: {
                UserName: { required: true, email: true },
                Password: "required"
            }
        });

        var email_form = $("#email_form").validate({
            rules: {
                Name: "required",
                Email: { required: true, email: true }
            }
        });
        var formcomment = $(".formcomment").validate();

        var payment_form = $("#payment_form").validate({
            rules: {

                FirstName: "required",
                LastName: "required",
                CCType: "required",
                CCNumber: "required",
                CSC: "required"
            }
        });

        var fmregister = $("#fmregister").validate({
            rules: {
                Email: { required: true, email: true },
                Password: { required: true, minlength: 5 },
                Password2: { required: true, minlength: 5, equalTo: "#txtPassword" },
                FirstName: "required",
                LastName: "required"

            }
        });

        var accountinfo_form = $("#accountinfo_form").validate({
            rules: {
                Email: { required: true, email: true },
                Password: { required: true, minlength: 5 },
                Password2: { required: true, minlength: 5, equalTo: "#txtAccountInfoPassword" },
                FirstName: "required",
                LastName: "required",
                Address1: "required",
                City: "required",
                State: "required",
                Country: "required",
                PostalCode: "required",

                Phone: "required",
                ShipFirstName: "required",
                ShipLastName: "required",
                ShipAddress1: "required",
                ShipCity: "required",
                ShipState: "required",
                ShipCountry: "required",
                ShipPostalCode: "required"

            }
        });

        $('.validate').each(function(i) { $(this).validate(); });
    } catch (err) { }
}


//fixpng
var blank = new Image();
blank.src = '/images/1.gif';
$(document).ready(function() {
    var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
    if (badBrowser) {

        $('.png').each(function() {
            if ($(this).attr('class').indexOf('rollover') == -1) {
                if (!this.complete) {
                    this.onload = function() { fixPng(this) };
                } else {
                    fixPng(this);
                }
            }
        });
    }
});






function blog_entry_linkimg_click(pid) {
    $('#blog_entry_link_' + pid).toggle();
    $('#blog_entry_link_' + pid).focus();
    $('#blog_entry_link_' + pid).select()
}

//blog
var curpage_th = 0;
var curpage_sd = 0;
var pages_th = 0;
var pages_sd = 0;

function InitPopSlide() {
    $(document).ready(function() {
        var html_th = '';
        var html_sd = '';
        var j = 0;
        pages_th = 0;
        curpage_th = 0;
        pages_sd = 0;
        curpage_sd = 0;


        pages_th = $('.slide_th .slide_page').length;
        pages_sd = $('.slide_sd .slide_page').length;
        CheckSlideLinks();
        Show_sd_i(curpage_sd);
        Show_th_i(curpage_th);



        $('.lbtn_view_slideshow').focus();


    });
}

function ShowTitleAlt(obj) {
    var alt = $(obj).attr('alt');
    var title = $(obj).attr('title');
    if (alt == title) alt = '';
    $('.slide_img_alt').html(alt.replace(/&lt;/g, '<').replace(/&gt;/g, '>'));
    $('.slide_img_title').html(title);



    var src = $('.slide_sd .slide_page_' + curpage_sd + ' img:eq(0)').attr('src');

}
function CheckSlideLinks() {
    $('.lblpages_th').html((pages_th + ''));
    $('.lblcurpage_th').html(((curpage_th + 1) + ''));

    $('.lbtnPrevious_th').removeClass('lbtn');
    $('.lbtnNext_th').removeClass('lbtn');

    if (curpage_th > 0) {
        $('.lbtnPrevious_th').addClass('lbtn');
    }
    if (curpage_th < (pages_th - 1)) {
        $('.lbtnNext_th').addClass('lbtn');
    }

    //sd
    $('.lblpages_sd').html((pages_sd + ''));
    $('.lblcurpage_sd').html(((curpage_sd + 1) + ''));

    $('.lbtnPrevious_sd').removeClass('lbtn');
    $('.lbtnNext_sd').removeClass('lbtn');

    if (curpage_sd > 0) {
        $('.lbtnPrevious_sd').addClass('lbtn');
    }
    if (curpage_sd < (pages_sd - 1)) {
        $('.lbtnNext_sd').addClass('lbtn');
    }

    //

}

function ShowPopSlide(url, pageid) {
    //alert(url)
    if ($.trim($('.dlgPop').html()).length == 0 || $.trim($('.dlgPop').html()).toLowerCase().indexOf('class="slideshow_fm"') == -1 || $.trim($('.dlgPop').html()).toLowerCase().indexOf('id="slideshow_fm_' + pageid + '"') == -1) {
        $('.dlgPop').load('ajax_slideshow.aspx?pageid=' + pageid, function() {
            InitPopSlide();
            SelectSlide(url);
            $('.dlgPop').show(500);
        });
    }
    else {
        SelectSlide(url);
        $('.dlgPop').show(500);
    }

    /*
    1. var posX = 0;
    2. var posy = 0;
    3. var event = event ? event : window.event;
    4. if(event.pageX || event.pageY) {
    5.     posX = event.pageX;
    6.     posY = event.pageY;
    7. } else if (event.clientX || event.clientY) {
    8.     posX = event.clientX + document.documentElement.scrolLeft +
    9.           document.body.scrolLeft;
    10.     posY = event.clientY + document.documentElement.scrolTop +
    11.           document.body.scrolTop;
    12.
    13. }*/

    var t = 0;
    if (document.documentElement && document.documentElement.scrollTop) {
        t = document.documentElement.scrollTop;
    } else if (document.body) {
        t = document.body.scrollTop;
    }

    $('.dlgPop').css('top', (t + 66) + 'px');

    return false;
}

function SelectSlide(url) {

    //select th
    var sel = $('.slide_th img[src$="' + url + '"]');
    if (!sel.attr('src')) {
        sel = $('.slide_th .undownloaded[title*="' + url + '"]').find('img');
    }

    if (sel.attr('src')) {
        var id = sel.parents('.slide_page').attr('class');
        if (id.indexOf('_') != -1) {
            id = id.substr(id.lastIndexOf('_') + 1);

            Show_th_i(id);
        }
    }

    //select sd

    // alert($('.slide_sd').html());

    sel = $('.slide_sd img[src$="' + url + '"]');
    if (!sel.attr('src')) {

        sel = $('.slide_sd .undownloaded[title*="' + url + '"]').find('img');

    }

    if (sel.attr('src')) {
        var id = sel.parents('.slide_page').attr('class');
        if (id.indexOf('_') != -1) {
            id = id.substr(id.lastIndexOf('_') + 1);
            Show_sd_i(id);
        }
    }



    SwtichVIEW_th();
    $('.slideshow_mainimg').html(url);
}

function SwtichVIEW_th() {
    $('.slide_links_th').hide();
    $('.slide_links_sd').show();
    $('.slide_th').hide(); $('.slide_sd').show();

    $('.slide_img_alt').html('');
    $('.slide_img_title').html('');
    ShowTitleAlt($('.slide_sd .slide_page_' + curpage_sd + ' img:eq(0)'));
}

function SwtichVIEW_sd() {
    $('.slide_links_th').show();
    $('.slide_links_sd').hide();
    $('.slide_th').show();
    $('.slide_sd').hide();
    ShowTitleAlt($('.slide_th .slide_page_' + curpage_sd + ' img:eq(0)'));

    Show_th_i(curpage_th);
}

function Previous_th() {
    if (curpage_th < 1) {
        curpage_th = pages_th;
    }
    curpage_th--;

    Show_th_i(curpage_th);
}
function Next_th() {
    if (curpage_th >= (pages_th - 1)) {
        curpage_th = -1;
    }
    curpage_th++;

    Show_th_i(curpage_th);
}

function Show_th_i(i) {
    curpage_th = parseInt(i);

    $('.slide_th .slide_page').hide();



    if ($('.slide_th .slide_page_' + curpage_th + ' .undownloaded').length > 0) {
        $('.slide_th .slide_page_' + curpage_th + ' .undownloaded').each(function(j) {
            var title = $('.slide_th .slide_page_' + curpage_th + ' .undownloaded').eq(j).attr('title');
            $('.slide_th .slide_page_' + curpage_th + ' .undownloaded').eq(j).attr('title', '');
            if (title.indexOf('background') != -1) {
                $('.slide_th .slide_page_' + curpage_th + ' .undownloaded').eq(j).attr('style', title);
            } else {
                $('.slide_th .slide_page_' + curpage_th + ' .undownloaded').eq(j).find('img').attr('src', title);
            }
        });
        $('.slide_th .slide_page_' + curpage_th + ' .undownloaded').removeClass('undownloaded');
    }

    $('.slide_th .slide_page_' + curpage_th).show();
    ShowTitleAlt($('.slide_th .slide_page_' + curpage_th + ' img'));
    CheckSlideLinks();
}



function Show_sd_i(i) {
    curpage_sd = parseInt(i);


    curpage_th = parseInt(curpage_sd / 25.0);


    $('.slide_sd .slide_page').hide();


    if ($('.slide_sd .slide_page_' + curpage_sd + ' .undownloaded').length > 0) {
        $('.slide_sd .slide_page_' + curpage_sd + ' .undownloaded').each(function(j) {
            var title = $('.slide_sd .slide_page_' + curpage_sd + ' .undownloaded').eq(j).attr('title');

            $('.slide_sd .slide_page_' + curpage_sd + ' .undownloaded').eq(j).attr('title', '');
            $('.slide_sd .slide_page_' + curpage_sd + ' .undownloaded').eq(j).find('img').attr('src', title);
        });
        $('.slide_sd .slide_page_' + curpage_sd + ' .undownloaded').removeClass('undownloaded');
    }


    $('.slide_sd .slide_page_' + curpage_sd).show();
    ShowTitleAlt($('.slide_sd .slide_page_' + curpage_sd + ' img'));
    CheckSlideLinks();
}

function Previous_sd() {
    if (curpage_sd < 1) {
        curpage_sd = pages_sd;
    }
    curpage_sd--;
    Show_sd_i(curpage_sd);

}
function Next_sd() {
    if (curpage_sd >= (pages_sd - 1)) {
        curpage_sd = -1;
    }
    curpage_sd++;

    Show_sd_i(curpage_sd);

}


//

function ShowPopSizeChart(chartid) {
    $('.dlgPop').load('ajax_page.asp?pageid=' + chartid + '&t=' + (new Date()).valueOf(), function() {
        //  InitEmailVaildate();
        $('.dlgPop').show(500);
    });
}
function ShowPopEmail_main(url) {
    if ($.trim($('.dlgPop').html()).length == 0 || $.trim($('.dlgPop').html()).toLowerCase().indexOf('id="fmEmailToFriend"') == -1) {
        $('.dlgPop').load('ajax_friend.asp?t=' + (new Date()).valueOf(), function() {
            //  InitEmailVaildate();
            $('.dlgPop').show(500);
            InitEmailVaildate_main(url);
        });
    }
    else {
        $('.dlgPop').show(500);
    }
    return false;
}

function ShowPopEmail() {
    ShowPopEmail_main('');
}

function ResizePopThs() {
    var cols = $('.pop_image_col').length;
    if (cols > 1) {
        $('.popimage_ths').width(144);
        $('.dlgPop').width(544);
    } else {
        $('.popimage_ths').width(72);
        $('.dlgPop').width(472);

    }
}
function ShowPop(url) {
    if ($.trim($('.dlgPop').html()).length == 0 || $.trim($('.dlgPop').html()).toLowerCase().indexOf('/pimg/pop_') == -1) {
        $('.dlgPop').load('ajax_pop.asp?productid=' + PRODUCTID, function() {

            if (url.length > 0) {
                $('.imgMainPop').attr('src', '/pimg/pop_' + url);
            }
            ResizePopThs();
            $('.dlgPop').show(500);

        });
    }
    else {
        if (url.length > 0) {
            $('.imgMainPop').attr('src', '/pimg/pop_' + url);
        }
        ResizePopThs();
        $('.dlgPop').show(500);
    }
}

function SelectDefaultColor(url) {
    if ($('#drpColor option').length == 2) {
        if ($('#drpColor').val() == '') {
            $('#drpColor').val($('#drpColor option').eq(1).attr('value'));
            drpColor_Size_Change(url);
        }
    }
}


function fixPng(png) {
    if (!png) return;
    if (!png.src) return;
    // get src
    var src = png.src;
    // set width and height
    if (!png.style.width) { png.style.width = $(png).width(); }
    if (!png.style.height) { png.style.height = $(png).height(); }
    // replace by blank image
    png.onload = function() { };
    png.src = blank.src;
    // set filter (display original image)
    png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}



function Search(menuid, cn, sid) {
    if (!menuid) menuid = 0;
    if ((menuid + '').length == 0) menuid = 0;
    var key = $.trim($('#txtKeyword').val());

    if (key.length > 2) {
        location.href = '/ocsearch.asp?keyword=' + key;
        /*
        if (sid == '5') {
        location.href = '/entry.asp?sid=5&key=' + key;
        }
        else {
        location.href = '/products.asp?menuid=' + menuid + '&key=' + key ;
        }*/
    }
    if (key.length > 0 && key.length < 3) {
        alert('More chars please.');
        $('#txtKeyword').focus();
    }
}

function ResizeWin() {

}

function InitEmailVaildate_main(url) {

    if (url.length == 0) url = location.href;
    else {
        if (url.indexOf('http') == -1) {
            var lc = location.href;
            lc = lc.substr(0, lc.indexOf('/', 9));
            url = lc + url;
            //  alert(lc);
        }
    }

    $('#txtURL').html(url);
    $("#fmEmailToFriend").validate({
        rules: {
            ToEmail: {
                required: true,
                email: true
            },
            FromEmail: {
                required: true,
                email: true
            }

        },
        messages: {
            ToEmail: {
                required: "*",
                email: "@"
            },
            FromEmail: {
                required: "*",
                email: "@"
            }
        },
        submitHandler: function() {

            $.post("ajax_friend.asp",
              {
                  URL: $('#txtURL').html(),
                  Note: $('#txtNote').val(),
                  To: $('#txtTo').val(),
                  ToEmail: $('#txtToEmail').val(),
                  FromEmail: $('#txtFromEmail').val(),
                  From: $('#txtFrom').val(),
                  Action: 'SendEmail'
              },
              function(data) {
                  $('.plEmailToFriend').html(data);
              }
            );
        }



    });
    setTimeout("txtTo_focus()", 1000);
}
function txtTo_focus() {
    $('#txtTo').focus();
}
function InitEmailVaildate() {
    InitEmailVaildate_main('');
}

//shop by

function getHighColor(menuid) {

    var recolor = '#ffff00';
    switch (menuid) {
        case '1':
            {

                recolor = "#CCFFFF";
                break;
            }
        case '2':
            {
                recolor = "#FFCCFF";
                break;
            }
    }


    return recolor;
}
function lbtnAllDesignerLink_click() {
    $('.sbd_sub').hide(); $('.sbd').show(); $('#lbtnAllDesignerLink').hide();
}

function sbd_click(obj, menuid, cn) {

    if ($(obj).attr('class').indexOf('_sub') == -1) {

        $('.sbd span').css('background-color', '#ffffff');
        $(obj).find('span').css('background-color', getHighColor(menuid));


    }
    var loc = 'products.asp?menuid=' + menuid + '&subcatid=0&designerid=' + $(obj).attr('id').replace('sbd_', '') + '&cn=' + cn + '&view=all';
    location.href = (loc);
}

function sbc_click(obj, menuid, cn) {
    if ($(obj).attr('class').indexOf('_sub') == -1) {
        $('.sbc span').css('background-color', '#ffffff');
        $(obj).find('span').css('background-color', getHighColor(menuid));
    }
    var loc = 'products.asp?menuid=' + menuid + '&subcatid=' + $(obj).attr('id').replace('sbc_', '') + '&designerid=0&cn=' + cn + '&view=all';
    location.href = (loc);
}

var Inited = false;

function InitShopBy(designerid, subcatid, color) {
    if (subcatid > 0) {

        shopby_c_click(color);
        $('#sbc_' + subcatid).find('span').css('background-color', getHighColor(menuid));
    }

    if (designerid > 0) {
        shopby_d_click(color);
        $('#sbd_' + designerid).find('span').css('background-color', getHighColor(menuid));
    }

    $('#lbtnAllDesignerLink').click(lbtnAllDesignerLink_click);
    var loc = location.href;
    loc = loc.substr(0, loc.lastIndexOf('=') + 1) + 'aaaaaaaaaaaaaaaaa';

    if (location.href.indexOf('mode=splash') != -1) {
        lbtnAllDesignerLink_click();

    }

    if (loc.indexOf('products.asp?menuid=aaaaaaaaaaaaaaaaa') != -1) {

        shopby_d_click(color);
    }

    Inited = true;
}


function shopby_d_click(color) {
    //  $('.shopby_c').css('background', '#999');
    // $('.shopby_d').css('background', '#000');
    //   alert(color);

    if (Inited) { $('.sbd_sub').hide(); $('.sbd').show(); $('#lbtnAllDesignerLink').hide(); }

    // $('.shopby_d img').eq(0).attr('src', '/images/buttons/designer_' + color + '_button.png');
    //$('.shopby_c img').eq(0).attr('src', '/images/buttons/category_gray_button.png');

    $('.shopby_sub_c').hide();
    $('.shopby_sub_d').show();
}

function shopby_c_click(color) {
    // alert(color);
    // $('.shopby_d').css('background', '#999');
    //$('.shopby_c').css('background', '#000');

    //$('.shopby_d img').eq(0).attr('src', '/images/buttons/designer_gray_button.png');
    // $('.shopby_c img').eq(0).attr('src', '/images/buttons/category_' + color + '_button.png');


    $('.shopby_sub_d').hide();
    $('.shopby_sub_c').show();
}



function GetCCID(ccnumber) {
    /*
    <option selected value="1">Visa</option>
    <option value="2">MasterCard</option>
    <option value="3">AMEX</option>
    <option value="4">Discover</option>
    */
    var cc = GetCreditCardTypeByNumber(ccnumber);
    var re = 0;
    switch (cc) {
        case 'Visa':
            {
                re = 1; break;
            } case 'MasterCard':
            {
                re = 2; break;
            } case 'AMEX':
            {
                re = 3; break;
            } case 'Discover':
            {
                re = 4; break;
            }
    }
    return re;

}
function GetCreditCardTypeByNumber(ccnumber) {
    cc = (ccnumber + '').replace(/\s/g, ''); //remove space

    if ((/^(34|37)/).test(cc) && cc.length == 15) {
        return 'AMEX'; //AMEX begins with 34 or 37, and length is 15.
    } else if ((/^(51|52|53|54|55)/).test(cc) && cc.length == 16) {
        return 'MasterCard'; //MasterCard beigins with 51-55, and length is 16.
    } else if ((/^(4)/).test(cc) && (cc.length == 13 || cc.length == 16)) {
        return 'Visa'; //VISA begins with 4, and length is 13 or 16.
    } else if ((/^(300|301|302|303|304|305|36|38)/).test(cc) && cc.length == 14) {
        return 'DinersClub'; //Diners Club begins with 300-305 or 36 or 38, and length is 14.
    } else if ((/^(2014|2149)/).test(cc) && cc.length == 15) {
        return 'enRoute'; //enRoute begins with 2014 or 2149, and length is 15.
    } else if ((/^(6011)/).test(cc) && cc.length == 16) {
        return 'Discover'; //Discover begins with 6011, and length is 16.
    } else if ((/^(3)/).test(cc) && cc.length == 16) {
        return 'JCB';  //JCB begins with 3, and length is 16.
    } else if ((/^(2131|1800)/).test(cc) && cc.length == 15) {
        return 'JCB';  //JCB begins with 2131 or 1800, and length is 15.
    }
    return '?'; //unknow type
}

function IsValidCC(str) { //A boolean version
    if (GetCreditCardTypeByNumber(str) == '?') return false;
    return true;
}                    