﻿//清除两边空格
String.prototype.trim = function() 
{ 
	return this.replace( /(^\s*)|(\s*$)/g , "" );
}

//清除左边空格
String.prototype.ltrim = function() 
{ 
	return this.replace( /(^\s*)/g, "" );
}

//清除右边空格
String.prototype.rtrim = function() 
{ 
	 return this.replace( /(\s*$)/g, "" );
}

//截取左边字符
String.prototype.lsubstr = function( len ) 
{ 
	return this.substr( 0, len );
}

//截取右边字符
String.prototype.rsubstr = function( len ) 
{ 
	return this.substr( this.length - len, this.length );
}

//擦除左边字符
String.prototype.lwipe = function( len ) 
{ 
	return this.substr( len, this.length );
}

//擦除右边字符
String.prototype.rwipe = function( len ) 
{ 
	return this.substr( 0, this.length - len );
}

//全部替换
String.prototype.replaceAll = function( old, news ) 
{ 
	return this.replace( new RegExp( old, "gm" ), news );
}

//检测是否字符串
String.prototype.isString = true; 

//根据ID获取控件对象
String.prototype.getById = function() 
{ 
	return $id( this );
}

//根据TagName获取控件对象
String.prototype.getByName = function() 
{ 
	return $tag( this );
}

//根据Name获取控件对象
String.prototype.getByTagName = function() 
{ 
	return $name( this );
}

//根据Name获取控件对象
String.prototype.isBound = function( min, max ) 
{ 
	return isBound( this.length, min, max );
}

//某些字符在字符串中的总数
String.prototype.charTotal = function( char ) 
{ 
	if ( isEmpty(char) ) return 0;
	var tmpNum = 0;
	if ( isArray(char) ) {
		for ( var i = 0; i < char.length; i++ ) 
		{
			tmpNum += this.split(char[i]).length - 1; 
		}
	} else {
		tmpNum += this.split( char ).length - 1;
	}
	return tmpNum;
}

//尾部添加值
Array.prototype.push = function( value ) 
{ 
	this[this.length] = value;
}

//与单个数组合并
Array.prototype.unite = function( ary ) 
{ 
	if ( !isArray(ary) )return this;
	var tmpary = this;
	for ( var i = 0; i < ary.length; i++ ) 
	{
		tmpary[tmpary.length] = ary[i];
	}
	return tmpary;
}

//检测是否数组
Array.prototype.isArray = true; 

//简单格式化日期
Date.prototype.formatDate = function( text ) 
{ 
	return text.replaceAll( '{Y}', this.getFullYear() ).replaceAll( '{M}', this.getMonth() + 1 ).replaceAll( '{D}', this.getDate() ).replaceAll( '{h}', this.getHours() ).replaceAll( '{m}', this.getMinutes() ).replaceAll( '{s}', this.getSeconds() ).replaceAll( '{w}', this.getDay() );
}

//flash全局通用参数
var swf_globalparams = [ "wmode", "Transparent" ]; 
//flash参数变量
var swf_params = []; 
//flash的ID
var swf_id = null; 
//flash播放品质
var swf_quality = "hign"; 
//flash安装插件地址
var swf_codebase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0"; 
//flash安装插件地址
var swf_pluginspage = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash"; 
//跳转对象
var jump_method = window; 
//剪切板的方法
var clipboard_method = null; 
//浏览器变量
var ie, firefox, opera; 
//是否支持cookies
var cookies; 
//是否支持flash
var swf; 
//判断本页是否被frame引用
var isframe = window.top.location != window.self.location; 
//初始化时间对象
var date = new Date(); 
//状态栏文本
var window_status = ''; 
//离开页面时的提示信息
var beforeunloadhint = ''; 
//初始化浏览器版本变量
ie = getOs( "msie", 3 );
firefox = getOs( "firefox", 8 );
opera = getOs( "opera", 4 );
//初始化浏览器是否支持cookies
cookies = isCookiesEnable(); 
//初始化浏览器是否支持flash
swf = swfVersion(); 

//通过ID获取对象
function $id( id ) 
{ 
	if ( document.getElementById )
	{
		$id = function( id ) { return document.getElementById( id ); }
		return $id( id );
	}
	if ( document.all ){
		$id = function( id ) { return document.all[ id ]; }
		return $id( id );
	}
	if ( document.layers[ id ] ){
		$id = function( id ) { return document.layers[ id ]; }
		return $id(id);
	}
	alert( "Error: Lack of access to HTML object, upgrade your browser to IE6 or later!" );
	$id = function( id ){ return null; }
	return null;
}

//通过Name获取对象
function $name( name ) 
{ 
	if ( document.getElementsByName ) 
	{
		$name = function( name ){
			var tmpobj = document.getElementsByName(name);
			return tmpobj.length == 1 ? tmpobj[0] : tmpobj;
		}
		return $name(name);
	}
	alert( "Error: Lack of access to HTML object By Name, upgrade your browser to IE6 or later!" );
	$name = function( name ) { return null; }
	return $name(name);
}

//通过TagName获取对象
function $tag( tag ) 
{ 
	if ( document.getElementsByTagName ) {
		$tag = function( tag ){
			var tmpobj = document.getElementsByTagName(tag);
			return tmpobj && tmpobj.length == 1 ? tmpobj[0] : tmpobj;
		}
		return $tag(tag);
	}
	alert("Error: Lack of access to HTML object By Tag, upgrade your browser to IE6 or later!");
	$tag = function( tag ) { return null; }
	return $tag(tag);
}

//检测是否为数字
function isNum( value ) 
{ 
	return !isNaN( value );
}

//检测是否为无效对象
function isNull( o ) 
{ 
	return o == null || o == 'undefined';
}

//检测是否为空
function isEmpty( value ) 
{ 
	return isNull( value ) || value == '' || value.length == 0;
}

//检测是否数组
function isArray( value ) 
{ 
	return isNull( value ) ? false : value.isArray;
}

//检测是否字符串
function isString( value ) 
{ 
	return value.isString;
}

//检测数字范围 (如需检测长度，直接赋入长度)
function isBound( value, min, max ) 
{ 
	return value > min && value < max;
}

//页面输出flash
function swfWrite( URL, width, height ) 
{ 
	document.write( swfGetCode( URL, width, height ) );
}

//生成flash代码
function swfGetCode( URL, width, height ) 
{ 
	var tmpstr = isEmpty( swf_id ) ? '' : "id=\"" + swf_id + "\"";
	var tmpembedparams = '';
	tmpstr = "<object " + tmpstr + " classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"" + swf_codebase + "\" width=\"" + width + "\" height=\"" + height + "\">\n<param name=\"movie\" value=\"" + URL + "\" />\n<param name=\"quality\" value=\"" + swf_quality + "\" />\n";
	swf_params = isArray(swf_params) ? swf_params.concat( swf_globalparams ) : swf_globalparams;
	for ( var i = 0; i < swf_params.length; i += 2 ) {
  		tmpstr += "<param name=\"" + swf_params[i] + "\" value=\"" + swf_params[i+1] + "\" />\n";
		tmpembedparams += swf_params[i] + "=\"" + swf_params[i+1] + "\" "
	}
	tmpstr += "<embed src=\"" + URL+ "\" quality=\"" + swf_quality + "\" pluginspage=\"" + swf_pluginspage + "\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\" " + tmpembedparams + "></embed></object>";
	
	//回收内存
	tmpembedparams = null, swf_params = [], swf_id = null, swf_quality = "hign";
	return tmpstr;
}

//检测flash版本
function swfVersion() 
{ 
	var tmpobj = navigator.plugins;
	if ( !tmpobj || tmpobj.length == 0 ) {
		for ( var i = 12; i > 5; i-- ) {
			try {
				var tmpobj = new ActiveXObject( "ShockwaveFlash.ShockwaveFlash." + i.toString() );
				tmpobj = null;
				return i.toString() + ".0";
			} catch( e ) { continue; }
		}
		tmpobj = null;
		return null;
	} else {
		for ( var i = 0; i < tmpobj.length; i++ ) {
			if ( tmpobj[i].name.indexOf("Shockwave Flash") == -1 ) continue;
			return tmpobj[i].description.substr(16, 3);
		}
		tmpobj = null;
		return null;
	}
}

function Mathr() 
{
	return Math.random();
}

//获取select选中的值
function selectedValue( o ) 
{ 
	return o.options[o.selectedIndex].value;
}

//获取select选中的HTML代码
function selectedHtml( o ) 
{ 
	return o.options[o.selectedIndex].innerHTML;
}

//select全选
function selectedAll( o ) 
{ 
	for ( var i = 0; i < o.options.length; i++ ) { o.options[i].selected = true; }
}

//select反选
function selectedReverse( o ) 
{ 
	for ( var i = 0; i < o.options.length; i++ ) { o.options[i].selected = !o.options[i].selected; }
}

//批量添加option元素
function appendOptions( o, value, text, defaultselected, selected ) 
{ 
	for ( var i = 0; i < value.length; i++ ) 
	{
		o.options[o.options.length] = new Option( value[i], text[i], defaultselected[i], selected[i] );
	}
}

//获取checkbox集合选中的值
function checkedValue( o ) 
{ 
	var tmpary = [];
	for ( var i = 0; i < o.length; i++ ) 
	{
		if ( o[i].checked ) tmpary[tmpary.length] = o[i].value;
	}
	return tmpary;
}

//获取checkbox集合选中的对象
function checkedObject( o ) 
{ 
	var tmpary = [];
	for ( var i = 0; i < o.length; i++ ) 
	{
		if ( o[i].checked ) tmpary[tmpary.length] = o[i];
	}
	return tmpary;
}

//checkbox全选
function checkedAll( o ) 
{ 
	for ( var i = 0; i < o.options.length; i++ ) 
	{
		o.options[i].checked = true;
	}
}

//checkbox反选
function checkedReverse( o ) 
{ 
	for ( var i = 0; i < o.length; i++ ) 
	{
		o.checked = o.checked ? false : true;
	}	
}

//图片缩略
function imgZoom(imageDest_i, W, H)
{
    // 显示框宽度W,高度H
    var image = new Image();
    var imageDest = {};
    image = imageDest_i;
    if(image.width>0 && image.height>0)
    {
        // 比较纵横比
        if(image.width/image.height >= W/H) // 相对显示框：宽>高
        {
			
            if(image.width > W) // 宽度大于显示框宽度W，应压缩高度
            {
                imageDest.width = W;
                imageDest.height = (image.height*W)/image.width;
            }else{ // 宽度少于或等于显示框宽度W，图片完全显示
                imageDest.width = W;
                imageDest.height = (image.height*W)/image.width;
            }
        }else{ // 同理
            if(image.height > H)
            {
                imageDest.height = H;
                imageDest.width = (image.width*H)/image.height;
            }else{
                imageDest.height = H;
                imageDest.width = (image.width*H)/image.height;
            }
        }

		imageDest_i.style.width = Math.round(imageDest.width) + 'px';
		imageDest_i.style.height = Math.round(imageDest.height) + 'px';
    }
	imageDest = null;
}

//页面输出
function write( txt ) 
{ 
	document.write( txt );
}

//随即数字
function randomNum( min, max ) 
{ 
	return Math.round( Math.random() * 100 );
}

//获取浏览器的版本
function getOs( key, count ) 
{ 
	var tmpstr = navigator.userAgent.toLowerCase().trim();
	key = key.toLowerCase().trim();
	if ( tmpstr.indexOf(key) > -1 ) 
	{
	    return tmpstr.substr(  tmpstr.indexOf(key) + key.length + 1, count );
	}
	tmpstr = null;
	return null;
}

//跳转页面
function jump( URL ) 
{ 
	var o = isEmpty(jump_method) ? window : jump_method;
	switch( URL ) 
	{
		case 'back':
			o.history.back(-1);
			break;
		case 'reload':
			o.location.reload();
			break;
		default:
			o.location.href = URL;
			break;
	}
	o = null;
	jump_method = window;
}

//关闭窗体
function wclose() 
{ 
	window.open( '', '_parent', '');
	window.close();
}

//检测是否支持cookies
function isCookiesEnable() 
{ 
	if( navigator.cookiesEnabled ) 
	{
		isCookiesEnable = function() { return true; }
		return true;
	} else {
		document.cookie = "testcookie=yes;";
		if( document.cookie.indexOf("testcookie=yes") > -1 ) 
		{
			document.cookie = "";
			isCookiesEnable = function() { return true; }
			return true;
		}
		isCookiesEnable = function() { return false; }
		return false;
	}
}

// 设置Cookies
function setCookie( key, value ) 
{ 
	if ( !cookies ) {
		setCookie = function() { return false; }
		return false;
	}
	var exp  = new Date();
	exp.setTime( exp.getTime() + 9999999 * 1000 );
	document.cookie = key + "="+ escape ( value ) + ";expires=" + exp.toGMTString() + ";";
	exp = null;
}

// 获取Cookies的值
function getCookieValue( key ) 
{ 
	if ( !cookies ) 
	{
		getCookieValue = function() { return null; }
		return null;
	}
	var tmpstr, rege = new RegExp( "(^| )" + key + "=([^;]*)(;|$)" );
	return tmpstr = document.cookie.match(reg) ? unescape( tmpstr[2] ) : null;
}

//检测文件格式
function checkExtend( URL, extend ) 
{ 
	var fileExtend = null;
	for ( var i = 0; i < URL.length; i++ ) 
	{
		fileExtend = URL[i].split('.').lastValue().toLowerCase();
		for ( var n = 0; n < extend.length; n++ ) {
			if ( fileExtend != extend[n].toLowerCase() ) return false;
		}
	}
	fileExtend = null
	return true;
}

//添加收藏夹
function addFavorite( sTitle, sURL )
{
    try
    {
        window.external.addFavorite(sURL, sTitle);
    }
    catch (e)
    {
        try
        {
            window.sidebar.addPanel(sTitle, sURL, "");
        }
        catch (e)
        {
            alert("加入收藏失败，请使用Ctrl+D进行添加");
        }
    }
}

// 设为首页
function setHome(obj, vrl)
{
	try
	{
		obj.style.behavior='url(#default#homepage)';obj.setHomePage(vrl);
	}
	catch(e)
	{
		if(window.netscape) 
		{
			try 
			{
			netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
			}  
			catch (e)  
			{ 
			alert("此操作被浏览器拒绝！\n请在浏览器地址栏输入\"about:config\"并回车\n然后将[signed.applets.codebase_principal_support]设置为'true'");
			return false;
			}
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',vrl);
		}
	}
}

// 获取网址上的参数(区分大小写)
function getUrlParam( key ) 
{ 
	var key = key.toLowerCase();
	if ( location.search ) return null;
	var tmparams = location.search.toString().lwipe(1).split('&');
	var tmparamsValue = [];
	var tmpary = null;
	for ( var i = 0; i < tmparams.length; i++ ) 
	{
		tmpary = tmparams[i].split('=');
		if ( tmpary[0] == key ) tmparamsValue[tmparamsValue.length] = tmpary[1];
	}
	//释放内存
	tmparams = null;
	tmpary = null;
	return tmparamsValue;
}

//获取剪切板信息
function getClipBoard( text ) 
{ 
	if ( window.clipboardData ) 
	{
		getClipBoard = function( text ) {
			var tmpmethod = clipboard_method ? clipboard_method : 'text';
			clipboard_method = null;
			return window.clipboardData.getData( tmpmethod, text );
		}
		return getClipBoard( text );
	} else {
		getClipBoard = function() { return false; }
		return false;
	}
}

//复制字符串到剪切板
function setClipBoard( text ) 
{ 
	if ( window.clipboardData ) 
	{
		setClipBoard = function( text ) {
			var tmpmethod = clipboard_method ? clipboard_method : 'text';
			clipboard_method = null;
			window.clipboardData.clearData();
			window.clipboardData.setData( tmpmethod, text );
			tmpmethod = null;
			return true;
		}
		return setClipBoard( text );
	} else {
		setClipBoard = function() { return false; }
		return false;
	}  
}

//状态栏文字
function winStatus( text ) 
{ 
	window_status = text;
	window.status = window_status;
}

//离开页面时提示确认
function beforeunloadEvent( text )
 { 
	beforeunloadhint = text;
	window.onbeforeunload = function() {
		return beforeunloadhint;
	}
}

//获取客户端高度
function clientHeight() 
{ 
	return ( document.documentElement.clientHeight || document.body.clientHeight );
}

//获取客户端宽度
function clientWidth()
{ 
	return ( document.documentElement.clientWidth || document.body.clientWidth );
}

/* 搜索条 */
function beforeSubmit()
{
	var url = "";
	
	if ( $id('keyword').value.trim() == "" || $id('keyword').value.trim() == "请输入关键字" )
	{
		alert("请输入一个关键字进行搜索。");
		return false;
	}	
	
	if( $id('mtype').value == 0 )
	{
		url = "/product/advsearch.php";
	} else if( $id('mtype').value == 1 ) {
		url = "/company/advsearch.php";
	} else if( $id('mtype').value == 2 ) {
		url = "/supply/advsearch.php";
	} else if( $id('mtype').value == 3 ) {
		url = "/news/advsearch.php";
	}
	
	document.postat.action = url;
	document.postat.submit();
	
	return true;   
}

/* 热门搜索 */
var autoSearch = function(channel, keywords)
{
	window.open('/' + channel + ',' + keywords + ',,,,,REVTQw,1-15.html', '_blank');
	return ;
}

/* 高级搜索 */
function advSearchSubmit()
{
	var url = "";
	if (document.advpostat.keyword)
	{
		if ( document.advpostat.keyword.value.trim() == "" || document.advpostat.keyword.value.trim() == "请输入关键字" )
		{
			alert("请输入一个关键字进行搜索。");
			return false;
		}
	}
		
	if( $id('mtype').value == 0 )
	{
		url = "/product/advsearch.php";
	} else if( $id('mtype').value == 1 ) {
		url = "/company/advsearch.php";
	} else if( $id('mtype').value == 2 ) {
		url = "/supply/advsearch.php";
	} else if( $id('mtype').value == 3 ) {
		url = "/news/advsearch.php";
	}
	
	document.advpostat.action = url;
	document.advpostat.submit();
	
	return true;   
}

/* 轮换开始 */
function slideLine(ul, delay, speed, lh, total, step) 
{
	var slideBox = (typeof ul == 'string')?$id(ul):ul;
	var delay = delay||1000, speed = speed||20, lh = lh||20, step = step||1;
	var tid = null, pause = false;
	var start = function() 
				{
					tid = setInterval(slide, speed);
				}
	var slide = function() 
				{
					if (pause) return;
						slideBox.scrollTop += 2;
					if (slideBox.scrollTop % lh == 0) 
					{
						clearInterval(tid);
						slideBox.appendChild(slideBox.getElementsByTagName('ul')[0]);
						slideBox.getElementsByTagName('ul')[0].style.display = 'none';
						for ( var i = 0; i < step; i++ ) 
						{
							slideBox.getElementsByTagName('ul')[i].style.display = 'block';
						}

						slideBox.getElementsByTagName('ul')[total-1].style.display = 'none';
						slideBox.scrollTop = 0;
						setTimeout(start, delay);
					}
				}
	slideBox.onmouseover = function(){ pause = true; }
	slideBox.onmouseout = function(){ pause = false; }
	setTimeout(start, delay);
}
/* 轮换结束 */

/* div 显示切换 */
function divSwitch(oid, cid, step, cls, obj)
{
	var step = step||2, cls = cls||null, obj = obj||null;
	$id(oid).style.display = 'block'; 
	
	for ( var i = 1; i <= step; i++ ) 
	{
		if (cid + i != oid)
		{
			$id(cid + i).style.display = 'none'; 
			if ($id(obj + i)) $id(obj + i).className = ''; 
		}else{
			if ($id(obj + i)) $id(obj + i).className = cls; 
		}
	}
}
/* div 显示切换结束 */

/* div 开关 */
function divShow(id)
{
	if ($id(id).style.display == 'none')
	{ 
		$id(id).style.display = 'block'; 
	} else {
		$id(id).style.display = 'none';
	}
}
/* div 开关结束 */

/* 轮换开关 */
function initBrand( pid, id,  pagesize, interval, btnid, btnmax, style ) 
{
	var __brand_cur = [];
	var __brand_handle = [];
	__brand_cur[id] = 1;
	__brand_child = $id(id).getElementsByTagName('li');
	$id(id).style.display = '';
		
	setBrandFocus( 1, id, pagesize, btnmax, btnid, style, interval );
	
	__brand_handle[id] = setInterval( function(){setBrandFocus( __brand_cur[id] = __brand_cur[id] + 1 > btnmax ? __brand_cur[id] = 1 : __brand_cur[id] = __brand_cur[id] + 1, id, pagesize, btnmax, btnid, style, interval )}, interval );
	
	$id(pid).onmouseover = function()
	{
		if ( !isNaN(__brand_handle[id]) ) 
		{
			clearInterval(__brand_handle[id]);
			__brand_handle[id] = 0;
		}
	}
	
	$id(pid).onmouseout = function()
	{
		__brand_handle[id] = setInterval( function(){setBrandFocus( __brand_cur[id] = __brand_cur[id] + 1 > btnmax ? __brand_cur[id] = 1 : __brand_cur[id] = __brand_cur[id] + 1, id, pagesize, btnmax, btnid, style, interval )}, interval );
	}
}

function setBrandFocus( num, id, pagesize, btnmax, btnid, style, interval ) 
{
	if ( num > btnmax ) num = 1;
	var tmpNum = pagesize * num;
	
	__brand_child = $id(id).getElementsByTagName('li');
	
	for ( var i = 0; i < __brand_child.length; i++ ) 
	{
		if ( i > tmpNum - 1 || i < tmpNum - pagesize ) 
		{
			__brand_child[i].style.display = 'none';
		} else {
			__brand_child[i].style.display = '';
		}
	}
	for ( var i = 1; i <= btnmax; i++ ) 
	{
		$id(btnid + i).className = style[1];
	}
	
	$id(btnid + num).className = style[0];
}
/* 轮换开关结束 */