﻿
/**
    
*/

var viewHistoryBookIDs ="";
		

function handleViewHistory() { new Ajax.Updater('viewhistory', '/product/viewhistory.aspx', { method: 'get',  parameters: 'ch=1',onComplete: addViewHistoryItem });}
function clearViewHistory() { 
	var bookIdStr = getViewHistoryBookIDs();
	if(bookIdStr=="") {
		 alert("请选择您要清除的记录！");
	}else {
		new Ajax.Request('/product/viewhistory.aspx', { method: 'get', parameters: 'ac=cl&bookId='+bookIdStr,onComplete: AfterClearViewHistory});
	}
}
function addViewHistoryItem() {
	var vh = document.getElementById("viewhistory");
	
	if(vh.innerHTML !="<OL></OL>") {
		if(typeof(buyImageUrl)=='undefined' || buyImageUrl==null || buyImageUrl=="") 
			buyImageUrl ="/images/product/gm.gif";
		if(typeof(collectImageUrl)=='undefined' || collectImageUrl==null || collectImageUrl=="") 
			collectImageUrl ="/images/product/sc.gif";
		if(typeof(clearImageUrl)=='undefined' || clearImageUrl==null || clearImageUrl=="") 
			clearImageUrl ="/images/product/searclear.jpg";
		
		var content ='<center><input type="button" onclick="javascript:buyViewHistory();" value="购买" class="lishi_naniu" />&nbsp;<input onclick="javascript:clearViewHistory()" type="button" value="清除历史" class="lishi_naniu"/>';
		new Insertion.Bottom('viewhistory', content); 
	}
}

function AfterClearViewHistory() {
	var vh = document.getElementById("viewhistory");
	 vh.innerHTML="";
	 handleViewHistory();
}

function buyViewHistory() {
    var bookIdStr = getViewHistoryBookIDs();
    
    if(bookIdStr!="") {
        AddToCart(bookIdStr);
        clearViewHistoryBookIDsCheck();
    }else {
        alert("请选择您要购买的商品！");
    }
}

function collectViewHistory(){
    var bookIdStr = getViewHistoryBookIDs();
    
    if(bookIdStr!="") {
        addToFavorites(bookIdStr);
    }else {
        alert("请选择您要收藏的商品！");
    }
}

function addToFavorites(bookIds) { 
   viewHistoryBookIDs = bookIds;
   new Ajax.Request('/usercenter/favorites/add.aspx', { method: 'get', parameters: 'bookid=' + bookIds, onComplete: bookmarkResult }); 
}

function bookmarkResult(r) { 
    if (r.responseText == '未登录') {
       alert("请先登录！");
       window.location.href='/usercenter/favorites/bookmark.aspx?bookid=' + viewHistoryBookIDs;
    }else {
        alert("所选商品已经收藏成功！\n您可以进入\"我的帐户\"查看您已经收藏的商品");
        clearViewHistoryBookIDsCheck();
        //window.open("/usercenter/favorites/");
    }
}

function clearViewHistoryBookIDsCheck() {
    var bookIdList = document.getElementsByName("viewhistoryBookID");
    if(bookIdList !=null && bookIdList.length >0) {
        for(var n=0;n < bookIdList.length;n++) {
            bookIdList[n].checked =false;
        }        
    }
}

function getViewHistoryBookIDs() {
    var bookIdStr="0";
    var bookIdList = document.getElementsByName("viewhistoryBookID");
    if(bookIdList !=null && bookIdList.length >0) {
        for(var n=0;n < bookIdList.length;n++) {
            if(bookIdList[n].checked) {
                bookIdStr += "," + bookIdList[n].value;
            }
        }
        if(bookIdStr!="0") {
            bookIdStr = bookIdStr.substring(2);
        }else {
            bookIdStr=""
        }
    }else {
        bookIdStr=""
    }
    return bookIdStr;
}

function addViewHistory(bookId,bookName) {
	//add view history
	new Ajax.Request('/product/viewhistory.aspx', { method: 'get', parameters: 'ac=ad&bookid='+bookId+'&bookname=' + bookName}); 
}

Event.observe(window, 'load', handleViewHistory, false);