﻿jQuery.noConflict();
//设为首页，t：标题，u：网址
function SetPageHome(t,u){
	if(jQuery.browser.msie){
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(u);
	}else if(jQuery.browser.mozilla){
		window.sidebar.addPanel(t,u,'');
	}
}
//加入收藏，t：标题，u：网址
function AddFavourites(t,u){
	if(jQuery.browser.msie){
		window.external.AddFavorite(u,t);
	}else if(jQuery.browser.mozilla){
		window.sidebar.addPanel(t,u,'')
	}
}
//计算字符串长度，长度（汉字算1）跟实际长度（汉字算2）
function StrLenthByByte(str) 
{ 
	var len;
	var i;
	len = 0;
	for(i=0;i<str.length;i++) 
	{ 
	if (str.charCodeAt(i)>255) len+=2;else len++; 
	}
	newlen=new Array(i,len);
	return newlen; 
}
//截取字符串
function CutStrLen(str,len){
	if(StrLenthByByte(str)[1]<=len){return str;}
	var i,theLen;
	theLen=0;
	for(i=0;i<str.length;i++) 
	{ 
	if(str.charCodeAt(i)>255) theLen+=2;else theLen++;
	if(theLen>len){break;}
	}
	return str.substring(0,i);
}
//统计字数
function CalculateCharacterCount(obj,MaxCount,toObj){
    var _count=StrLenthByByte(obj.value)[1];
	if(""!=obj.value){
	    if(_count>MaxCount){
			obj.value=CutStrLen(obj.value,MaxCount);
		}
		_count=StrLenthByByte(obj.value)[1];
		if(toObj)
		    toObj.innerHTML = "当前输入"+_count+"个字符，还可输入"+(MaxCount-_count)+"个字符 ";
	}else{
		toObj.innerHTML="";
	}
}

//cookie操作
var cookie = {
    domain : "2q5q.com",
	//设置保存Cookies
	set : function (name,value,timeVal){
		var tempCookie=name + "="+ escape (value);
		if(undefined!=timeVal&&null!=timeVal){
			var tempSec = parseInt(this.expTime(timeVal));
			var exp = new Date();
			exp.setTime(exp.getTime() + tempSec);
			tempCookie += ";expires=" + exp.toGMTString();
		}
		if(this.domain)tempCookie+=";path=/;domain="+this.domain+";true";
		document.cookie = tempCookie; 
	},
	//读取cookies
	get : function (name)
	{
		var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
		if(arr=document.cookie.match(reg)) return unescape(arr[2]);
		else return null;
	},
	alert : function(v){
	    alert(v);
	},
	//删除cookies
	del : function (name)
	{
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval=this.get(name);
		if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString()+";path=/;domain="+this.domain+";true";
	},
	//转换时间
	//1s: 1秒，1n: 1分钟
	expTime : function(timeVal){
		var str1=timeVal.substring(timeVal.length-1,timeVal.length);
		var str2=timeVal.substring(0,timeVal.length-1);
		var strVal=0;
		switch(str1){
			case"s":strVal=parseInt(str2);break;
			case"n":strVal=parseInt(str2)*60;break;
			case"h":strVal=parseInt(str2)*60*60;break;
			case"d":strVal=parseInt(str2)*60*60*24;break;
			case"m":strVal=parseInt(str2)*60*60*24*30;break;
			case"y":strVal=parseInt(str2)*60*60*24*365;break;
		}
		return strVal*1000;
	}
}

//清空Select
function clearSelect(obj,str){
	jQuery("#"+obj)[0].options.length=0;
	if(null!=str)
	    jQuery("#"+obj)[0].options.add(new Option(str,"0"));
}
//装载Select
function inToSelect(obj,str){
	var theObj=jQuery("#"+obj);
	var _str=str.split("|||");
	for(var i=0;i<_str.length;i++){
		if(""!=_str[i]){
			var __str=_str[i].split(",");
			theObj[0].options.add(new Option(__str[0],__str[1]));		
		}
	}
}
//装载Select
// str  二维数组  [[,],[,]] 第一个value，第二个text
function inToSelectByArr(obj,str){
	var theObj=jQuery("#"+obj);
	for(var i=0;i<str.length;i++){
		theObj[0].options.add(new Option(str[i][1],str[i][0]));		
	}
}

//产生范围内随机数
function randomInt(Min,Max){
    var Range = Max - Min;   
    var Rand = Math.random();   
    return(Min + Math.round(Rand * Range));
}


//显示提示层
function showhintinfo(obj, objleftoffset,objtopoffset, title, info , objheight, showtype ,objtopfirefoxoffset)
{
   var p = getposition(obj);
   
   if((showtype==null)||(showtype =="")) 
   {
       showtype =="up";
   }
   if(!jQuery("#hintdivup").is("span"))OutHintinfo();
   jQuery('#hintinfo'+showtype).html(info);
   jQuery('#hintdiv'+showtype).show();
   
   if(objtopfirefoxoffset != null && objtopfirefoxoffset !=0 && !isie())
   {
        jQuery('#hintdiv'+showtype).css("top",p['y']+parseInt(objtopfirefoxoffset)+"px");
   }
   else
   {
        if(objtopoffset == 0)
        { 
			if(showtype=="up")
			{
				 jQuery('#hintdiv'+showtype).css("top",p['y']-document.getElementById('hintinfo'+showtype).offsetHeight-35+"px");
			}
			else
			{
				 jQuery('#hintdiv'+showtype).css("top",p['y']+obj.offsetHeight+5+"px");
			}
        }
        else
        {
			jQuery('#hintdiv'+showtype).css("top",p['y']+objtopoffset+"px");
        }
   }
   jQuery('#hintdiv'+showtype).css("left",p['x']+objleftoffset+"px");
   jQuery('#hintiframe'+showtype).css("height",(jQuery('#hintinfo'+showtype).height()+30) + "px");
}

//输入提示层信息
function OutHintinfo(){
var strHintinfoHtml="<span id=\"hintdivup\" style=\"display:none; position:absolute;z-index:500;\">"+
"<div style=\"position:absolute; visibility: visible; width: 271px;z-index:501;\">"+
"<p><img src=\""+imagepath+"images/commandbg.gif\" /></p>"+
"<div class=\"messagetext\"><img src=\""+imagepath+"images/dot.gif\" /><span id=\"hintinfoup\" ></span></div>"+
"<p><img src=\""+imagepath+"images/commandbg2.gif\" /></p>"+
"</div>"+
"<iframe id=\"hintiframeup\" style=\"position:absolute;z-index:499;width:266px;scrolling:no;\" frameborder=\"0\" src=\"about:blank\"></iframe>"+
"</span>"
jQuery("body").append(strHintinfoHtml);
}


//隐藏提示层
function hidehintinfo()
{
    jQuery('#hintdivup').hide();
}

function getposition(obj)
{
	var r = new Array();
	r['x'] = obj.offsetLeft;
	r['y'] = obj.offsetTop;
	while(obj = obj.offsetParent)
	{
		r['x'] += obj.offsetLeft;
		r['y'] += obj.offsetTop;
	}
	return r;
}

//清空file类型元素内容
function clearFileInput(file){
    var form=document.createElement('form');
    document.body.appendChild(form);
    //记住file在旧表单中的的位置
    var pos=file.nextSibling;
    form.appendChild(file);
    form.reset();
    pos.parentNode.insertBefore(file,pos);
    document.body.removeChild(form);
}
//计算费用
function CalculateCharges(total,_charges){
    total=parseFloat(total);
    var _within=parseFloat(_charges[0]);
    var _withinmoney=parseFloat(_charges[1]);
    var _incremental=parseFloat(_charges[2]);
    var _proportion=parseFloat(_charges[3]);
    var _addmoney=parseFloat(_charges[4]);
    var _ceiling=parseFloat(_charges[5]);
    if(0<_within&&total<=_within)
        return _withinmoney;
    if (0 < _incremental)
    {
        var _incrementalCount = parseInt(total / _incremental);
        if (0 < (total % _incremental))
            _incrementalCount++;
        total = _incremental * _incrementalCount;
    }
    var _getCount = total * (_proportion / 100);
    if(0<_addmoney)_getCount+=_addmoney;
    if (0 < _ceiling && _getCount > _ceiling)
        return _ceiling;
    return MathCeiling(_getCount,2);
}

function MathCeiling(x,y){
    if(0>y)y=0;
    if(3<y)y=3;
    var _y=Math.pow(10,y);
    var _x=parseFloat(x)*_y;
    var __x=parseFloat(Math.ceil(_x));
    return __x/_y;
}

function inputkeydown(e,obj){
	if(e.keyCode==13){
	    e.keyCode=9;
	    jQuery("#"+obj).click();
	    return false;
	}
	return true;
}

function ResetImage(obj,w,h){
    if(obj.width>obj.height*(w/h)){
        if(obj.width>w){
            obj.width=w;
            obj.style.cursor="pointer";
            obj.onclick=function(){
                window.open(this.src);
            }
        }
    }else{
        if(obj.height>h){
            obj.width=h;
            obj.style.cursor="pointer";
            obj.onclick=function(){
                window.open(this.src);
            }
        }
    }
}

function ShowTopTip(){
    if("no"==cookie.get("ShowWebTopTip"))return;
    jQuery("div.WebTopTip").show();
}
function CloseTopTip(b){
    jQuery("div.WebTopTip").hide();
    if(!b){cookie.set("ShowWebTopTip","no");}
}