﻿var patentId = 0;
var searchTypeId = 0;
var queryString = '';
var detailDivObj = null;
var isCheckAll = false;

function DeserializeResponse(response)   //反序列化返回的字符串为JS对象。
{
    return Sys.Serialization.JavaScriptSerializer.deserialize(response);
}

function simpleBtClick(obj) //点击基本信息改变样式。
{
    obj.parentNode.parentNode.parentNode.parentNode.className="searchresultheadSimple";
    if(navigator.userAgent.indexOf("Firefox")>0)
    {
        obj.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling.style.display='block';
        obj.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling.nextSibling.nextSibling.style.display='none';
    }
    else
    {
        obj.parentNode.parentNode.parentNode.parentNode.nextSibling.style.display='block';
        obj.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling.style.display='none';
    }
}

function detailBtClick(obj,isNeedQueryString) //点击详细信息改变样式。
{
    obj.parentNode.parentNode.parentNode.parentNode.className="searchresultheadDetail";
    if(navigator.userAgent.indexOf("Firefox")>0)
    {
        obj.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling.style.display = 'none';
        detailDivObj = obj.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling.nextSibling.nextSibling;
    }
    else
    {
        obj.parentNode.parentNode.parentNode.parentNode.nextSibling.style.display = 'none';
        detailDivObj = obj.parentNode.parentNode.parentNode.parentNode.nextSibling.nextSibling;
    }
    detailDivObj.style.display = 'block';    
    if(detailDivObj.innerHTML.indexOf('【') == -1)
    {   
        patentId = $get(obj.id.replace('detailBt','patendId')).innerHTML;
        if(isNeedQueryString == true)
        {
            SearchService.GetSearchDetailInfo(patentId, searchTypeId, queryString, getSearchDetailInfoComplete);
        }
        else
        {
            SearchService.GetSearchDetailInfoWithoutQueryString(patentId, searchTypeId, getSearchDetailInfoComplete);
        }
        detailDivObj.innerHTML = '请稍候...';
    }
}

function getSearchDetailInfoComplete(result) //获取详细信息成功。
{
    var patentDetailObj = DeserializeResponse(result);
    
    var targetArr = null;
    if(searchTypeId == 0)
    {
        targetArr = chineseDetailFields;
    }
    else if(searchTypeId == 1)
    {
        targetArr = foreignDetailFields;
    }
    else if(searchTypeId == 2)
    {
        targetArr = invalidDetailFields;
    }
    var returnStr = '<table style="width:665px; margin:3px 0px 3px 3px; text-align:left;">';
    returnStr += '<tr style="font-weight:bold; padding-left:10px;"><td colspan="2" align="left">' + patentDetailObj.ti + '</td></tr>';
    for(var i=1;i<targetArr.length;i++)
    {
        var lineArr = targetArr[i].split(':');
        if(eval('patentDetailObj.'+ lineArr[0]) != null && eval('patentDetailObj.'+ lineArr[0]) != '')
        {
            returnStr += '<tr><td width="116px" align="left" valign="top">【' + lineArr[1] + '】：' + '</td><td align="left" style="word-break:break-all;">' + eval('patentDetailObj.'+ lineArr[0]) + '</td></tr>';
        }
    }
    returnStr += '</table>';
    detailDivObj.innerHTML = returnStr;
}

function checkAll(isButtonClick)   //控制复选框。
{
    var objs = document.getElementsByTagName('input');
    if($get('viewCheckAll') == null || isButtonClick == true)
    {
        for(var i=0;i < objs.length; i++) 
        {
            if(objs[i].type.toLowerCase() == "checkbox" && objs[i].id.indexOf('viewCheck') != -1)
            {
                objs[i].checked = !isCheckAll;
            }
        }
        isCheckAll = !isCheckAll;
    }
    else
    {
        if($get('viewCheckAll').checked == true)
        {
            for(var i=0;i < objs.length; i++) 
            {
                if(objs[i].type.toLowerCase() == "checkbox" && objs[i].id.indexOf('viewCheck') != -1)
                {
                    objs[i].checked = true;
                }
            }
        }
        else
        {
            for(var i=0;i < objs.length; i++) 
            {
                if(objs[i].type.toLowerCase() == "checkbox" && objs[i].id.indexOf('viewCheck') != -1)
                {
                    objs[i].checked = false;
                }
            }
        }
        isCheckAll = !isCheckAll;
    }
}

function detailBtClick_ptclass()//点击详细信息展开
{
        document.getElementById('searchresultItemSimple1').style.display='none';
        document.getElementById('searchresultItemDetail1').style.display= 'block';
        document.getElementById('tabheader').className = 'searchresultheadDetail';
//      document.getElementById('ctl00_ctl00_MainContent_ChildContent1_PatentCardView1_cardViewRepeater_ctl00_detailBt').enable='false';
//      document.getElementById('ctl00_ctl00_MainContent_ChildContent1_PatentCardView1_cardViewRepeater_ctl00__simpleBt').enable='true';
return false;
}

function simpleBtClick_ptclass()//点击简单信息展开
{
    document.getElementById('searchresultItemDetail1').style.display='none';
    document.getElementById('searchresultItemSimple1').style.display='block';
    document.getElementById('tabheader').className = 'searchresultheadSimple';
//  document.getElementById('ctl00_ctl00_MainContent_ChildContent1_PatentCardView1_cardViewRepeater_ctl00__simpleBt').enable='false';
//  document.getElementById('ctl00_ctl00_MainContent_ChildContent1_PatentCardView1_cardViewRepeater_ctl00__detailBt').enable='true';
    return false;
}

//function downFile(obj)  //全文下载函数
//{ 
//    if (navigator.userAgent.indexOf("Firefox") > 0) {
//        var an = obj.nextSibling.nextSibling.innerHTML;
//        window.showModalDialog("DownloadFullFile.aspx?an=" + an, window, "dialogHeight:150px;dialogWidth:340px;center:yes;resizable:no;help:no;status:no;scroll:no");
//    }
//    else {
//        var an = obj.nextSibling.nextSibling.innerHTML;
//        window.showModalDialog("DownloadFullFile.aspx?an=" + an, window, "dialogHeight:150px;dialogWidth:340px;center:yes;resizable:no;help:no;status:no;scroll:no");
//    }
//}

//二次检索按钮客户端事件
function searchprogress(d1, d2) {
    document.getElementById(d1).style.display = 'none';
    document.getElementById(d2).style.display = 'block';
    return true;
}

//全文阅读区别跳转
function FulltextChange(an,pn,pd) {
    var suban = an.toString().substring(0, 2);
    var subpn = pn.toString().substring(0, 2);
    if (((suban == "cn") || (suban == "CN")) && ((subpn == "cn") || (subpn == "CN")))
    {
        window.open("http://img.5ipatent.com/FullTextViewer1.aspx?an=" + an);
    }
    else
    {
        if (((suban == "jp") || (suban == "JP")) && ((subpn == "jp") || (subpn == "JP"))) {
            if (IsNeedMoveZero(pd) == 1) {
                pn = GetModifyPn(pn);
            }
            window.open("FullTextViewer3.aspx?pn=" + pn);
        }        
        else
        {
            if (((subpn == "us") || (subpn == "US"))) {
                window.open("FullTextViewer2.aspx?pn=" + pn);
            }
            else
            {
                window.open("FullTextViewer3.aspx?pn=" + pn);
            }
        }
    }   
}
function IsNeedMoveZero(pd) {
    var datePattern = /^(\d{4})-(\d{1,2})-(\d{1,2})$/;
    var pddate = GetPdDate(pd);
    var startdate = "1989-01-06";
    var enddate = "1997-12-23";
    if (!datePattern.test(pddate)) {
        
        return 0;
    }
    else {
        var target = new Date(pddate.replace(/-/g, "/"));
        var start = new Date(startdate.replace(/-/g, "/"));
        var end = new Date(enddate.replace(/-/g, "/"));
        if (Date.parse(target) - Date.parse(start) > 0 && Date.parse(end) - Date.parse(target) > 0) {
            //alert("正好在操作区间");
            return 1;
        }
        else {
            //alert("区间之外");
            return -1;
         }
    }
}
function GetPdDate(ipd) {
    var year = ipd.toString().substr(0, 4);
    var month = ipd.toString().substr(4, 2);
    var day = ipd.toString().substr(6, 2);
    if (ipd.toString().length == 8 && year > 1000 && month > 0 && month < 13 && day > 0 && day < 32) {
        return year + "-" + month + "-" + day;
    }
    else return 0;
}
function GetModifyPn(ipn) {
    var mdf;
    mdf = ipn.toString().substr(0, 2) + ipn.toString().substr(3, ipn.toString().length - 3);
    return mdf;
}
