$(document).ready(function() {
    $('.productpop').click(function() { ShowPop('') });
    //   InitSize();
    //  InitColor();

    SortSize();


    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

    $('#txtEmail').focus(function() {
        var v = $('#txtEmail').val();
        $('#txtEmail').removeClass('graytext');
        if (v == 'ENTER E-MAIL') {
            $('#txtEmail').val('');
        } else {

        }
    });

    $('#txtEmail').blur(function() {
        var v = $('#txtEmail').val();

        if (v == 'ENTER E-MAIL') {
            $('#txtEmail').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
    $('.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;
    });


    /*
We need to do this on server side.
	$('img[@src^="/userfiles/"]').attr('src',function(){
    $(this).attr('src',$(this).attr('src').replace('/userfiles/','http://occdn.s3.amazonaws.com/userfiles/'))
    });
    */
});




function ToggleSE2(id, obj) {
    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).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) {
    $('.dialog').html('<div class="dialog_f"><img src="/images/buttons/featured_' + menutoColor + '.png" width="91" height="82" alt="" class="png" id="png_f" /><\/div><div class="dialog_just_fm"><img src="/images/flags/oc_' + menutoColor + '_just.png" width="93" height="96" alt="" class="png" id="png_just" /><\/div><div class="dialog_staff_fm"><a href="/staffs.asp?name=' + StaffName + '"><img src="' + StaffImg + '"  width="104"  alt="" class="png" id="png_staff"  /></a><\/div><div class="dialog_xclusive"><img src="/images/buttons/xclusive_' + menutoColor + '.png" width="95" height="95" alt="" class="png" id="png_xclusive" /><\/div>');
            
}

function dialogHTML2(menutoColor) {
    $('.dialog').html('<div class="dialog_f"><img src="/images/buttons/featured_' + menutoColor + '.png" width="91" height="82" alt="" class="png" id="png_f" /><\/div><div class="dialog_just_fm"><img src="/images/flags/oc_' + menutoColor + '_just.png" width="93" height="96" alt="" class="png" id="png_just" /><\/div><div class="dialog_staff_fm"><img src="/images/flags/oc_' + menutoColor + '_staff.png" width="91"  height="83" alt="" class="png" id="png_staff"  /><\/div><div class="dialog_xclusive"><img src="/images/buttons/xclusive_' + menutoColor + '.png" width="95" height="95" alt="" class="png" id="png_xclusive" /><\/div>');
               
}
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 InitColor() {
    var drp = '';
    var id = '.li_color_' + productID;
    $(id).each(function(i) {
        if (drp.indexOf('>' + $(id).eq(i).html() + '<') == -1) {
            drp += '<option value="' + $(id).eq(i).html() + '" title="' + $(id).eq(i).attr('title') + '">' + $(id).eq(i).html() + '</option>';
        }
    });

    $('#drpColor').html(drp);
}

function InitSize() {
    var drp = '';
    var id = '.li_size'
    $(id).each(function(i) {
        if (drp.indexOf('>' + $(id).eq(i).html() + '<') == -1) {
            var pid = $(id).eq(i).attr('title');
            drp += '<option value="' + $(id).eq(i).html() + '" title="' + pid + '"' + ((productID + '') == pid ? ' selected="selected"' : '') + '>' + $(id).eq(i).html() + '</option>';
        }
    });
    $('#drpSize').html(drp);
}

function SortSize() {
    var drp = $('#drpSize').html();
    //Sort Size
    if ($('#drpSize option').length > 0) {
        var arrHTML = drp.split('</option>');
        var arr = new Array($('#drpSize option').length);
        for (var i = 0; i < $('#drpSize option').length; i++) {
            arr[i] = new MOption(arrHTML[i], $('#drpSize option').eq(i).attr('value').toUpperCase());
        }
        arr.sort(MOption_Sort);
        drp = '';
        for (var i = 0; i < arr.length; i++) {
            drp += arr[i].html + '</option>';
        }
        $('#drpSize').html(drp);
    }
}

var OptionsForSort = ','; OptionsForSort += 'os,xs,p, s, s/m,m, m/l,l, xl, xxl,';
for (var i = 0; i < 47; i = i + 0.5) {
    OptionsForSort += i + ',';
}
OptionsForSort = OptionsForSort.replace(/ /g, '').toUpperCase();


function MOption_Sort(a, b) {
    return OptionsForSort.indexOf(',' + a.value + ',') - OptionsForSort.indexOf(',' + b.value + ',');
}

function MOption(html, value) {
    this.html = html;
    this.value = value;
}
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 drpColor_Size_Change(url) {

    url = url.replace(/&amp;/g, '&');

    var selectedSize = $('#drpSize').val();
    var selectedColor = $('#drpColor').val();
    var pid = '';
    var allProductIDs = '';
    var matchedProduct = '';
    $('.li_size[title="' + selectedSize + '"]').each(function(i) {
        allProductIDs += '[' + $.trim($('.li_size[title="' + selectedSize + '"]').eq(i).html()) + ']';
    });

    $('.li_color[title="' + selectedColor + '"]').each(function(i) {
        pid = $.trim($('.li_color[title="' + selectedColor + '"]').eq(i).html());
        if (allProductIDs.indexOf('[' + pid + ']') != -1) {
            matchedProduct = pid;
        }
    });

    if ((matchedProduct + '').length > 0) {
        $('#txtProductID').val(matchedProduct + '');
    }



    if (matchedProduct.length > 0 && matchedProduct != productID) {
        location.href = url + '&sproductid=' + matchedProduct + '&size=' + $('#drpSize').val() + '&color=' + $('#drpColor').val() ;
        return;
    }


    if (selectedSize.length == 0 && selectedColor.length > 0) {
        pid = $.trim($('.li_color[title="' + selectedColor + '"]').eq(0).html());

        if (pid != productID && $('#drpColor option').length > 2) {

            location.href = url + '&sproductid=' + pid + '&color=' + $('#drpColor').val()  ;
        }
        return;
    }

    if (allProductIDs.length == 0) return;

    var rmdisabled = '';
    $('#drpColor option').removeAttr('disabled');
    $('.li_color').each(function(i) {
        pid = $.trim($('.li_color').eq(i).html());

        if (allProductIDs.indexOf('[' + pid + ']') == -1) {
            if (rmdisabled.indexOf($('.li_color').eq(i).attr('title')) == -1)
                $('#drpColor option[value="' + $('.li_color').eq(i).attr('title') + '"]').attr('disabled', 'disabled');
        } else {
            rmdisabled += '[' + $('.li_color').eq(i).attr('title') + ']';
            $('#drpColor option[value="' + $('.li_color').eq(i).attr('title') + '"]').removeAttr('disabled');
        }
    });

    if ($('#drpColor option[value="' + selectedColor + '"]').attr('disabled')) { $('#drpColor').val(''); }


    return;
    //ok enough?

    var needchange = true;

    if (selectedSize.length == 0 && selectedColor.length > 0) {
        $('.li_color[title="' + selectedColor + '"]').each(function(i) {
            matchedProduct = $.trim($('.li_color[title="' + selectedColor + '"]').eq(i).html());
            if (matchedProduct == (productID + '')) {
                needchange = false;
            }
        });

        if (needchange) {
            $('#txtProductID').val(matchedProduct + '');
            location.href = url + '&sproductid=' + matchedProduct + '&size=' + selectedSize + '&color=' + selectedColor;
        }
    }

    if ((matchedProduct + '').length > 0) {
        $('#txtProductID').val(matchedProduct + '');
    } else {
        if (selectedSize.length > 0 && selectedColor.length > 0) {
            // alert(selectedSize + ' + ' + selectedColor + ' is not available.');
            $('#drpSize').val('');
            $('#drpColor').val('');
        }
    }
}


//backup
function drpColor_Size_Change_old(url) {

    var selectedSize = $('#drpSize').val();
    var selectedColor = $('#drpColor').val();
    var pid = '';
    var allProductIDs = '';
    $('.li_size[title="' + selectedSize + '"]').each(function(i) {
        allProductIDs += '[' + $.trim($('.li_size[title="' + selectedSize + '"]').eq(i).html()) + ']';
    });

    var matchedProduct = '';
    $('.li_color[title="' + selectedColor + '"]').each(function(i) {
        pid = $.trim($('.li_color[title="' + selectedColor + '"]').eq(i).html());
        if (allProductIDs.indexOf('[' + pid + ']') != -1) {
            matchedProduct = pid;
        }
    });

    if (matchedProduct.length > 0 && matchedProduct != productID) {
        $('#txtProductID').val(matchedProduct + '');
        location.href = url + '&sproductid=' + matchedProduct + '&size=' + $('#drpSize').val() + '&color=' + $('#drpColor').val()  ;
        return;
    }
    var needchange = true;

    if (selectedSize.length == 0 && selectedColor.length > 0) {
        $('.li_color[title="' + selectedColor + '"]').each(function(i) {
            matchedProduct = $.trim($('.li_color[title="' + selectedColor + '"]').eq(i).html());
            if (matchedProduct == (productID + '')) {
                needchange = false;
            }
        });

        if (needchange) {
            $('#txtProductID').val(matchedProduct + '');

            location.href = url + '&sproductid=' + matchedProduct + '&size=' + selectedSize + '&color=' + selectedColor  ;

        }
    }

    if ((matchedProduct + '').length > 0) {
        $('#txtProductID').val(matchedProduct + '');
    } else {
        if (selectedSize.length > 0 && selectedColor.length > 0) {
            // alert(selectedSize + ' + ' + selectedColor + ' is not available.');
            $('#drpSize').val('');
            $('#drpColor').val('');
        }
    }
}

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() {
    //$('.center_fm_content').width($('.center_fm').width() -$('.center_fm_left').width() -80); 
    //alert($('.center_fm_content').width())
}

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;
}                    