﻿// JScript 文件
//0-超出总容量  1-超出容量 2-插入失败 3-插入成功 4-有重复值 5-没有登录 6 同上

var arry=["超出总容量","超出容量h","添加失败","添加成功","有重复值","请您登录后，再添加到收藏夹","您登录后才能使用此功能"];
var search=["publishname","isbn","title","author"];
  $(function(){
  $("#advancesearchss").click(function(){
   if(!isLogin())
  {
  alert(arry[6]);
  return false;
  }
  });
 
 
$(".btnso").click(function(){
var form=$("#search");
var type=$("#where").val();
var publish=$("#pubkey").val();
if(!isLogin())
{
 alert(arry[6]);
 return;
}
else
{
if(type==1)
{
 if(publish.length<10)
 {
   alert("ISBN不能少于10位");
   return;
 }
}
if(type==3)
{
if(publish.length<2)
 {
   alert("作者名称不能少于2位");
   return;
 }
 }
 }
var key=search[type];
var src=_root+"SearchBooks.aspx?"+key+"="+escape($.trim(publish));
window.open(src,"_blank","location=yes,fullscreen=no,height=300,width=600,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes,menubar=yes",false);
});
$("#ctl00_Y_ordertype").change(function(){
var ordertype=$(this).val();
if(ordertype<0)
{
ordertype=0;
}
var url=window.location.href;
var newurl;
if(url.indexOf('ordertype=')>-1)
{
    //newurl=url.substr(0,url.indexOf('ordertype=')+10)+ordertype;
    newurl=url.replace(/ordertype=\d/,"ordertype="+ordertype+"") 
}
else if(url.indexOf('?')>-1)
{
 newurl=url+'&ordertype='+ordertype;
}
else
{
 newurl=url+'?ordertype='+ordertype;
}
 window.location.href=newurl;

});
$("#checkall").click(function(){
if($(this).attr("checked"))
{//$(this).attr("checked")=='undefined'? '':'true'
$(".solist :checkbox").attr("checked",true);
}
else
{
$(".solist :checkbox").attr("checked",false);
}
});
//0-超出总容量  1-超出容量 2-插入失败 3-插入成功 4-有重复值
$("#addTofav").click(function(){
var ids='';
var count=$(".solist :checkbox:checked").length;
if(count>0){
$(".solist :checkbox:checked").each(function(index,dom){
ids+=$(dom).attr("value");
var inde=index+1;
if(inde!=count)
{
ids+="_";
}
});
postFav(ids);
}else
{
alert("请选择要加入收藏夹的书");
}
return false;
});

$("#cycfav").click(function(){

addFav();
});
$(".btnsc").click(function(){
var id=$(this).parent().parent().find("#favid").val();//alert("here");
postFav(id);
return false;

});
GetWeek($("#ctl00_Y_pubeginyear").val(),$("#ctl00_Y_pubbeginmonth"));
GetWeek($("#ctl00_Y_pubendyear").val(),$("#ctl00_Y_pubendmonth"));

$("#ctl00_Y_pubeginyear").change(function(){
var year=$(this).val();
var select=$("#ctl00_Y_pubbeginmonth");
GetWeek(year,select);
});

$("#ctl00_Y_pubendyear").change(function(){
var year=$(this).val();
var select=$("#ctl00_Y_pubendmonth");
GetWeek(year,select);
});

$("#advancesearch").click(function(){

var src=_root+"SearchBooks.aspx?";
var para=GetPara();
if(para!=0)
{
window.open(src+para,"_blank","location=yes,fullscreen=no,height=450,width=600,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes,menubar=yes",false);
}
});

$("#savewhere").click(function(){
if(!isLogin())
{alert(arry[5]);
 return;
}
try{
   // var name=window.prompt("请输入要保存的查询名称","查询["+(new Date()).getTime()+"]");
    var para=GetPara();
    if(para!=0)
  {
  var re= window.showModalDialog(_root+"UserSpace/SaveSearch.aspx",para,"dialogHeight=100px;dialogWidth=100px;");
   if(re=='yes')
   {
   window.location.reload();
   }
   else
   {
   alert("保存出错");
   }
    }
}
catch(ex){
    alert(ex);
}

});

$("#saveser").click(function(){
var name=$("#nameser").val();
if(name!='')
{
  var para=window.dialogArguments;
  try{
   $.post(_root+"handlers/SaveSearchConditionHandler.ashx",{name:name,where:para},function(re){
    
    if(re==1)
    {
      if(confirm("超过容量，是否要删除以前的条件，以保存现在的条件？"))
      {
         $.post(_root+"handlers/SaveSearchConditionHandler.ashx",{name:name,where:para,flag:1},function(re){
         alert(re);
         alert(arry[re]);
         });
      }
    }
    else
    {
    alert(arry[re]);
    }
    window.close();
    window.returnValue='yes';
    });}
    catch(ex)
    {
     alert("保存出错");
    }
}
else
{
alert("名称不能为空");
}  
});

$("#clearwhere").click(function(){
// var seriestitle=$("#seriestitle").val("");
//var title=$("#title").val("");
//var isbn=$("#isbn").val("");
//var author=$("#author").val("");
//var publishname=$("#publishname").val("");
//$("#ctl00_Y_beginyear option:first").attr("selected","selected");
//$("#ctl00_Y_beginmonth option:first").attr("selected","selected");
//$("#ctl00_Y_endyear option:first").attr("selected","selected");
//$("#ctl00_Y_endmonth option:first").attr("selected","selected");
//$("#ctl00_Y_pubbeginmonth option:first").attr("selected","selected");
//$("#ctl00_Y_pubendmonth option:first").attr("selected","selected");
//$("#ctl00_Y_pubeginyear option:first").attr("selected","selected");
//$("#ctl00_Y_pubendyear option:first").attr("selected","selected");
//$("#ctl00_Y_kindid option:first").attr("selected","selected");
window.location.reload();
});

$("#ctl00_Y_ordertypes").change(changeurl);
//$("#ctl00_Y_day").change(changeurl);

$("#addfavsigle").click(function(){
var ids=$("#ctl00_Y_hidbookid").val();
postFav(ids);
return false;
});

})

function changeurl()
{
var ordertype=$("#ctl00_Y_ordertypes").val();
if(ordertype<0)
{
 ordertype=0;
}

var index=$("#ctl00_Y_day")[0].selectedIndex;
var src=_root+"CurrentNewBookByDay.aspx?ordertype="+ordertype+"&index="+index;
window.location.href=src;
}

function GetPara()
{
 var seriestitle=$("#seriestitle").val();
var title=$("#title").val();
var isbn=$("#isbn").val();
var author=$("#author").val();
var kindid=$("#ctl00_Y_kindid").val();
var publishname=$("#publishname").val();
var publishdate=$("#ctl00_Y_beginyear").val()+"-"+$("#ctl00_Y_beginmonth").val()+"-01";
var publishdate_end=$("#ctl00_Y_endyear").val()+"-"+$("#ctl00_Y_endmonth").val()+"-01";
var begin=$("#ctl00_Y_pubbeginmonth").val();
var end=$("#ctl00_Y_pubendmonth").val();
var bookdate_begin='';
var bookdate_end='';
var i=0;
if(isbn!='')
{
 if(isbn.length<9)
 {
  alert('isbn不能少于10位 ');
  return 0;
 }
 else
 {
 i++;
 }
}
if(author!='')
{
 i++;
}
if(seriestitle!='')
{
 i++;
}
if(title!='')
{
 i++;
}
if(i<2)
{
if(isbn.length<10&&author.length<2&&seriestitle.length<3&&title.length<3&&publishname=='')
{
alert("搜索条件过短，结果太多请重新填写");
return 0;
}
}
//var pubdays=DateDiff(publishdate_end,publishdate);
//alert(parseInt(pubdays));
//if(dayspub>365)
//{
// alert('选择的出版时间不能超过一年');
// return 0;
//}
if(begin!=null)
{
 bookdate_begin=$("#ctl00_Y_pubbeginmonth").val().split('|')[0];
}
if(end!=null)
{
 bookdate_end=$("#ctl00_Y_pubendmonth").val().split('|')[1];
}
//var bookdatebegin=Date.parse(bookdate_begin);
//var bookdateend=Date.parse(bookdate_end);
//if((bookdateend-bookdatebegin)/60*60*1000*24>365)
//{
// alert('选择的发布时间不能超过一年');
// return 0;
//}

var para="seriestitle="+escape(seriestitle)+"&title="+escape(title)+"&isbn="+escape(isbn)+"&author="+escape(author)+"&publishname="+escape(publishname)+"&publishdate="+escape(publishdate);
para+="&publishdate_end="+escape(publishdate_end)+"&bookdate_begin="+escape(bookdate_begin)+"&bookdate_end="+escape(bookdate_end)+"&kindid="+escape(kindid);
return para;
}

function GetWeek(year,select)
{
select.empty();
$.post(_root+"handlers/GetWeekByYearHandler.ashx",{year:year,dt:new Date()},function(result){
var re=eval("("+result+")");
var json=re["week"];
var html='';
if(json.length>1)
{
$.each(json,function(index,dom){

html+="<option value='"+json[index].val+"' >"+json[index].num+"</option>";

});
}
$(html).appendTo(select);
});
}


function postFav(ids)
{
if(!isLogin())
{alert(arry[5]);
 return;
}
  $.post(_root+"handlers/SaveFavHandler.ashx",{ids:ids},function(result){
var json=eval("("+result+")");
//alert(arry[json["flag"]]+"\n超出数量："+json["overCount"]+"\n已存在的书："+json["bookname"]);
if(json["flag"]=="0")
{
  alert(arry[0]);
}
else if(json["flag"]==1)
{
 if(confirm("超出容量，超出:"+json["overCount"]+"您是否要删除最早的收藏，以容纳本次收藏"))
 {
   $.post(_root+"handlers/SaveFavHandler.ashx",{ids:ids,confirm:1},function(result){
   var json=eval("("+result+")");
   alert(arry[json["flag"]]);
   });

 }
}
else if(json["flag"]==2)
{
  alert(arry[2]);
}
else if(json["flag"]==3)
{
  alert(arry[3]);
}
else if(json["flag"]==4)
{
  alert("收藏夹已存在的书：\n"+json["bookname"]);
}
else if(json["flag"]==5)
{ alert(arry[5]);}
});

}


function addFav()
{
var ids='';
var count=$(".solist :checkbox:checked").length;
if(count>0){
$(".solist :checkbox:checked").each(function(index,dom){
ids+=$(dom).attr("value");
var inde=index+1;
if(inde!=count)
{
ids+="_";
}
});
postFav(ids);
}else
{
alert("请选择要加入收藏夹的书");
}
return false;
}
  function DateDiff(sDate1, sDate2){  //sDate1和sDate2是2002-12-18格式
        var aDate, oDate1, oDate2, iDays;
        aDate = sDate1.split("-");
        oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);  //转换为12-18-2002格式
        aDate = sDate2.split("-");
        oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]);
        var returns=Math.ceil((oDate2-oDate1)/(24*60*60*1000));
        
        iDays = parseInt(returns);  //把相差的毫秒数转换为天数
        
        return iDays;
}
