﻿$(document).ready(function() {
    $('#header a:not(#hotKeywords a)').focus(function(){
        this.blur();
    });
    $('.mouseover li').mouseover(function() {
        $(this).parent().find('.current').removeClass("current");
        $(this).addClass("current");
    }).eq(0).addClass("current");
    $('.hover li').hover(function() {
        $(this).addClass("hover");
    },function(){
        $(this).removeClass("hover");
    });
    //去除最后一项的下边框
    $('.box li:last-child:not(.lastbg li)').css('border-bottom-color', '#fff');
    $('.nolastbg li:last-child').css('background', 'none');
    $('a.mail').each(function() {
        e = this.rel.replace('&', '@');
        this.href = 'mailto:' + e;
        $(this).text(e);
    });
});
//var isLogin=false;
function popLogin()
{
    var username = $.trim($('#txtUserName').val());
    if (username.replace(/[\u4e00-\u9fa5]/g,'zz').length < 4) {
        $('#txtUserName').focus();
        if(username.length == 0)
            alert("请输入您的昵称或Email地址！");
        else
            alert("昵称长度必须在4-20个字符内！");
        return false;
    }
    var password = $.trim($('#txtPassword').val());
    if (password.length < 6) {
        $('#txtPassword').focus();
        if(password.length == 0)
            alert("请输入您的登录密码！");
        else
            alert("密码长度必须在6-25个字符内！");
        return false;
    }
    try{
        $.getJSON('http://user.mdooo.com/login.aspx?callback=?',{from:'post',txtUserName:username,txtPassword:password, t:Math.random().toString()},function(data){
            if(data.Status==1){ 
                isLogin=true;
                $('#popLogin').jqmHide();
            }
            else{
                isLogin=false;
                alert(data.Message);
            }
        });
    }
    catch(ex)
    {
        alert(ex.message);
    }
    return false;
}
function doSearch(frmSearch) {
    if (!document.getElementById('key') || document.getElementById('key').value.length == 0) {
        document.getElementById('key').focus();
        alert('请填写搜索关键字！');
        return false;
    }
    else if (document.getElementById('key').value.length < 2) {
        document.getElementById('key').focus();
        alert('搜索关键字过短，请重新填写！');
        return false;
    }
    else {
        if (document.location.href.indexOf('/search.aspx') > -1)
            frmSearch.target = "_self";
        else
            frmSearch.target = "_blank";
        return true;
    }
}
function AddToCart(pid) {
    var url = null;
    if (pid == null || pid < 1)
        url = "http://www.mdooo.com/ShoppingCart.aspx";
    else {
        if(pid.toString().indexOf(',')==-1)
            url = "http://www.mdooo.com/ShoppingCart.aspx?id=" + pid + "&r=" + Math.random() + "&referenceurl=" + escape(window.location.href);
        else
            url = "http://www.mdooo.com/ShoppingCart.aspx?ids=" + pid + "&r=" + Math.random() + "&referenceurl=" + escape(window.location.href);
    } 
    //var win = window.open(url, "mdshoppingcart");
    var win = window.open(url);
    if (win != null)
        win.focus();
}

function AddBatchToCart() {
    var ids = new Array();
    var items = document.getElementsByName('chkProduct');
    for (var i = 0; i < items.length; i++) {
        if (items[i].checked) {
            ids.push(items[i].value);
        }
    }
    if (ids.length < 1)
        alert('您没有选中任何商品！');
    else {
        url = "http://www.mdooo.com/ShoppingCart.aspx?ids=" + ids + "&r="+Math.random() + "&referenceurl=" + escape(window.location.href);
        //var win = window.open(url, "mdshoppingcart");
        var win = window.open(url);
        if (win != null)
            win.focus();
    }
}
function AddToWish(pid) {
    var url = null;
    if (pid == null || pid < 1)
        url = "http://user.mdooo.com/wishlist.aspx";
    else
        url = "http://user.mdooo.com/wishlist.aspx?id=" + pid + "&referenceurl=" + escape(window.location.href);
    //var win = window.open(url, "mdwishlist");
    var win = window.open(url);
    if (win != null)
        win.focus();
}
function AddToOutOfStock(pid) {
    if (pid != null || pid > 0)
        openWin("http://www.mdooo.com/ArrivalNotice.aspx?pid=" + pid, "tpnotice", 400, 250);
}
String.prototype.trim = function() {
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
function toggle(src, args) {
    var elm = document.getElementById(args);
    if (elm.style.display == "none") {
        elm.style.display = "block";
        src.src = "http://resource.mdooo.com/images/pucker_up.gif";
        src.alt = "折叠";
    }
    else {
        elm.style.display = "none";
        src.src = "http://resource.mdooo.com/images/pucker_down.gif";
        src.alt = "展开";
    }
}
function checkeAll(src, args) {
    var o = document.getElementsByName(args);
    for (var i = 0; i < o.length; i++)
        o[i].checked = src.checked;
}
function logout() {
    var s = document.getElementById("ifrlogout");
    s.src = "http://user.mdooo.com/logout.aspx?ReturnUrl=no&t=" + Math.random();
    document.getElementById('userinfo').innerHTML = '您好，欢迎光临麦豆网。[请<a href="javascript:login();">登录</a>/<a href="http://user.mdooo.com/register.aspx" target="_blank">注册</a>]';
    if(location.href.indexOf('http://user.')>=0)
        setInterval('location.replace(location.href)', 100);
}
function login() {
    location.href='http://user.mdooo.com/login.aspx?ReturnUrl='+location.href
}
function openWin(url, name, w, h) {
    var x = (window.screen.width - w) / 2;
    var y = (window.screen.height - h) / 2;
    var win = window.open(url, name, 'toolbar=0,location=0,status=0,top=' + y + ',left=' + x + ',width=' + w + ',height=' + h);
    win.focus();
    return win;
}
function ImageViewer(url) {
    window.open(url);
}
function RandomSort(arr, len) {
    arr.sort(function() {
        return Math.random() > 0.5 ? -1 : 1;
    });
    if (len && len < arr.length) {
        return arr.slice(0, len);
    }
    return arr;
}
function addToFavorite(title, url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    }
    else if (document.all) {
        window.external.AddFavorite(url, title);
    }
    else if (window.opera && window.print) {
        return true;
    }
}
function verifyemail()
{
    $('#lnkVerifyEmail').attr("disabled",true).removeAttr('href');
    $.getJSON("http://user.mdooo.com/verifyemail.aspx?callback=?",function(data){
        //var result = data.split(':');
        switch (data.Status) {
            case 1://邮件发送成功
                alert(data.Message);
                return;
            case 0://邮件发送失败
                $('#lnkVerifyEmail').attr("disabled",false).attr('href','javascript:verifyemail()')
                alert(data.Message);
                if(location.href.indexOf('http://user.')<0)
                    window.open('http://user.mdooo.com/ChangeEmail.aspx');
                return;
            case -2://未登录
                alert(data.Message);
                logout();
                return;
        }
    });
}
function vote(id, type){
    var data = {'voteid':id,'type':'postvote'};
    var i=0;
    $('#frmVote_'+id+' *[name=voteitems]').each(function(){
        switch(this.type){
            case 'radio':
            case 'checkbox':
                if(this.checked){
                    eval('data.'+this.id+'=\'true\'');
                    i++;
                }
                break;
            case 'text':
            case 'textarea':
                if($.trim(this.value).length>0){
                    eval('data.'+this.id+'=\''+this.value.replace('\'','\\\'')+'\'');
                    i++;
                }
                break;
        }            
    });
    if(i>0){
        $.post('/RenderVote.ashx', data, function(msg){alert(msg);});
    }
    else{
        alert('请选择/填写至少一项!');
    }
    return false;
}
function vote_load(id, type){//multiple,single
    if(type!='multiple'){
        $('#frmVote_'+id+' *[name=voteitems]').click(function(){
            $('#frmVote_'+id+' *[name=voteitems]').not(this).removeAttr('checked');
        });
    }
}
/*QueryString*/
function QueryString(qs) { // optionally pass a QueryString to parse
	this.params = {};	
	if (qs == null) qs = location.search.substring(1, location.search.length);
	if (qs.length == 0) return;
// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
	qs = qs.replace(/\+/g, ' ');
	var args = qs.split('&'); // parse out name/value pairs separated via &
// split out each name=value pair
	for (var i = 0; i < args.length; i++) {
		var pair = args[i].split('=');
		var name = decodeURIComponent(pair[0]);
		
		var value = (pair.length==2)
			? decodeURIComponent(pair[1])
			: name;
		
		this.params[name] = value;
	}
}
QueryString.prototype.get = function(key, default_) {
	var value = this.params[key];
	return (value != null) ? value : default_;
}
QueryString.prototype.contains = function(key) {
	var value = this.params[key];
	return (value != null);
}
function TimeSpan(eTime) {
    var now = new Date();
    var time = eTime.getTime() - now.getTime();
    var dt = Math.floor(time / 86400000); //time / 1000 / 60 / 60 / 24 天
    var ht = Math.floor(time / 3600000); //time / 1000 / 60 / 60 时
    var mt = Math.floor(time / 60000); //time / 1000 / 60 分
    var st = Math.floor(time / 1000); //秒
    var h = ht - (dt * 24);
    var m = mt - (ht * 60);
    var s = st - (mt * 60);
    return {'d':dt,'h':h,'m':m,'s':s,'t':time};
}
// 哀悼日灰屏
//	var today = new Date();
//	var date = today.getFullYear()+'-'+(today.getMonth() + 1)+'-'+today.getDate();
//	var dateArr = new Array('2010-4-21'/*,'2008-5-19','2008-5-20','2008-5-21'*/);
//	for(var i=0;i<dateArr.length;i++){
//		if(date==dateArr[i]){
//			document.childNodes[1].style.filter="gray";//alert(dateArr[i]);
//			break;
//		}
//	}
function change_tag(name, id, obj) {
    $(obj).parent().find('li').removeClass('current');
    $(obj).addClass('current');
    $('#' + name + '_body_' + id).parent().find('ul').hide();
    $('#' + name + '_body_' + id).show();
}