﻿function $( ID )
{
    var Cor = document.getElementById( ID );
    if( Cor != null )
    {
        return Cor;
    }
    else{
        return null;
    }
}
String.prototype.Trim = function()   
{
    var str=this;
    while (str.substring(0,1) == ' ')
    {
        str = str.substring(1, str.length);
    }
    while (str.substring(str.length-1, str.length) == ' ')
    {
        str = str.substring(0,str.length-1);
    }
    return str;
}

// 获取cookie的值
function get_Cookie(Name)
{
   return GetCookieValue(Name,";");
}

function GetAspNetCookie(Name)
{
    return GetCookieValue(Name,"&");
}

function GetCookieValue(Name,str1)
{
    var ckstr=new String(document.cookie);
    var str="";
    var ckname=Name+"=";
    var begin=ckstr.indexOf(ckname);
    if(begin != -1 )
    {
        var endstr=ckstr.indexOf(str1,begin);
        if(endstr!=-1)
        {
            str=ckstr.substring(begin+ckname.length,endstr);
        }
        else
        {
            str=ckstr.substring(begin+ckname.length,ckstr.length);
        }
    }
    else
    {
        str=begin;
    }
    return str;
} 

function TextboxAtt(objID,maxlength)
{
    obj=$(objID);
    obj.onmousedown=function(){CheckTextboxLength(obj,maxlength)};
    obj.onkeyup=function(){CheckTextboxLength(obj,maxlength)};
    obj.onmouseout=function(){CheckTextboxLength(obj,maxlength)};
}

function CheckTextboxLength(obj,maxlength)
{
       var len = obj.value.length;
       if(len > maxlength)
       {
            obj.value = obj.value.substring(0,maxlength);
            alert("The content of '"+obj.title+"' cannot be more than "+maxlength+" characters. The superfluous content will be deleted.");
       }

}

function OpenPage(url)
{
    var webform=window.open(url);
    if(webform==null)
    {
        alert("预览效果失败，因为弹出窗口已被屏蔽。");
    }
    else
    {webform.focus();}
}

//判断图片格式
function Imgpreview(x)
{
    if(!x || !x.value) return false; 
    var patn = /\.jpg$|\.jpeg$|\.bmp$|\.gif$/i; 
    if(patn.test(x.value)){ 
    return true;
    }else{ 
        return false;
    }
}

//设置图片宽
function SW(Cots,width)
{
        if(Cots.width>width || Cots.width==0)
        {
            Cots.width=width;
        }
}

//设置图片宽,高度
function SWH(Cots,width,height)
{
        if(Cots.width>width || Cots.width==0)
        {
            Cots.width=width;
        }
        if(Cots.height>height || Cots.height==0)
        {
            Cots.height=height;
        }
}

function GetTraffic( ConID ,Class , ID )
{
    var xmlhttp;
    try
    {
        xmlhttp = new XMLHttpRequest();
    }
    catch( e )
    {
        xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
    }
    
    xmlhttp.onreadystatechange=function()
    {
        if( 4 == xmlhttp.readyState )
        {
            if( 200 == xmlhttp.status )
            {
               
                var value=xmlhttp.responseText;
                $(ConID).innerHTML=value;
            }
            else
            {
                return null;
            }
        }
    }
    xmlhttp.open( "post" , "/GetTraffic.aspx" , true );
    xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    xmlhttp.send("Class="+escape(Class)+"&ID="+escape(ID));
    
}

function Search()
{
    var value=$("txtName").value;
    var Class=$("SelItem").value;
    if(value=="")
    {return;}
     var hrefs= Class + "?key=" + value;
    window.location.href=hrefs;
}

var Web=
{
    myAddPanel:function(title,url)
    {
        if ((typeof window.sidebar == 'object') && (typeof window.sidebar.addPanel == 'function'))
        {
            window.sidebar.addPanel(title,url,"");
        }
        else
        {
            window.external.AddFavorite(url,title);
        }
    }
}
//用户是否登录:已登录返回 "true",未登陆返回 "false"
function ClientLog()
{
    if ( GetAspNetCookie( "UserID" ) != -1 )
    {
        return true;
    }
    return false;
}

//客户登录地址
var ClientLogURL= "/Client/Login.aspx" ;

///显示登录情况
function homeLog()
{
    if ( ClientLog() )
    {
        $("i_login2").style.display = "";
    }
    else
    {
        $("i_login").style.display = "";
    }
}

//返回 询盘总数
function BasketCount(Name)
{
    var str = get_Cookie( Name );
    if( str == -1 )
    { return 0 ; }
    var list = str.split( "," );
    return list.length;
}

//显示询盘数量
function Showbasket()
{
    if ( ! ClientLog() || $( "UserID" )==null)
    {
        return 0;
    }
    var Offer= BasketCount( "Offer" );
    var Company= BasketCount( "Company" );
    var Product= BasketCount( "Product" );
    $( "Bask" ).innerHTML= ( Offer + Company + Product ) ; 
    $( "UserID" ).innerHTML= "Welcome " + GetAspNetCookie( "Name" );
}

////添加公司询盘
function AddCompanyBasket( CompanyID )
{
    if ( ! ClientLog() )
    {
        window.location.href=(ClientLogURL + "?URL=" + escape( window.location ) );
        return;
    }
    var str = get_Cookie( "Company" );
    if( str == -1 )
    {
        document.cookie = "Company=" + CompanyID+";path=/";
    }
    else
    {
        //超数
        if( BasketCount ( "Company" ) > 9 )
        {
            alert(  "You could inquire at most 10 companies" );
            return;
        }
        //重复
        var list = str.split( "," );
        for ( i = 0 ;i < list.length ; i++ )
        {
            if( list[i] == CompanyID )
            {
                alert( " You cannot select listing posted by yourself " );
                return;
            }
        }
        document.cookie = ( "Company=" +str + "," + CompanyID+";path=/" );
    }
    Showbasket();
    alert("Inquiry has been added successfully.");
}
////添加供求询盘
function AddOfferBasket( OfferID )
{
    if ( ! ClientLog() )
    {
        window.location.href = (ClientLogURL + "?URL=" + escape( window.location ) );
    }
    var str = get_Cookie( "Offer" );
    if( str == -1 )
    {
        document.cookie = "Offer=" + OfferID+";path=/";
    }
    else
    {
        //超数
        if( BasketCount ( "Offer" ) > 9 )
        {
            alert(  "You could inquire at most 10 Offer leads" );
            return;
        }
        //重复
        var list = str.split( "," );
        for ( i = 0 ;i < list.length ; i++ )
        {
            if( list[i] == OfferID )
            {
                alert( " The listing has already been selected. " );
                return;
            }
        }
        document.cookie=( "Offer=" + str + "," + OfferID+";path=/" );
    }
    Showbasket();
    alert("Inquiry has been added successfully.");
}
////添加产品询盘
function AddProductBasket( ProductID )
{
    if ( ! ClientLog() )
    {
        window.location.href = (ClientLogURL + "?URL=" + escape( window.location ) );
    }
    var str = get_Cookie( "Product" );
    if( str == -1 )
    {
        document.cookie = "Product=" + ProductID+";path=/";
    }
    else
    {
        //超数
        if( BasketCount ( "Product" ) > 9 )
        {
            alert(  "You could inquire at most 10 Products" );
            return;
        }
        //重复
        var list = str.split( "," );
        for ( i = 0 ;i < list.length ; i++ )
        {
            if( list[i] == ProductID )
            {
                alert( " You cannot select listing posted by yourself " );
                return;
            }
        }
        document.cookie=( "Product=" + str + "," + ProductID+";path=/" );
    }
    Showbasket();
    alert("Inquiry has been added successfully.");
} 
//聚焦管理栏目
function OfferFucus(Name)
{
    var Offerlist = $( "subnav166" ).getElementsByTagName( "a" );
    for ( i = 0 ;i < Offerlist.length ; i++ )
    {
        if( Offerlist[i].innerHTML == Name )
        {
            Offerlist[i].className = "E66D02" ;
            break;
        }
    }
}
