// JavaScript Document
var userAgent = navigator.userAgent.toLowerCase();
function get_browser() {
	var b;
	if (window.ActiveXObject) {
		v = userAgent.substr(userAgent.indexOf('msie') + 5, 3);
		b = 'ie';
	} else if (userAgent.indexOf("firefox") != - 1) {
		v = userAgent.substr(userAgent.indexOf('gecko/') + 6, 8);
		b = 'firefox';
	} else if (userAgent.indexOf("safari") != - 1) {
		v = -1;
		b = 'safari';
	} else if (userAgent.indexOf("opera") != - 1) {
		v = opera.version();
		b = 'opera';
	} else {
		v = -1;
		b = -1;
	}

	return {
		name : b,
		version: v
	};
}

var browser = get_browser();

function CreateEl(t,c,i){
		var x=document.createElement(t);
		x.className=c;
		x.id=i;
		x.style.display="block";
		return(x);
}

function formatfloat(f,size){  
	var tf=f*Math.pow(10,size);  
	tf=Math.round(tf+0.000000001);  
	tf=tf/Math.pow(10,size); 
	return tf;  
}  

function html2text(text){
   text = text.replace(/&/g, "&amp;") ;
   text = text.replace(/"/g, "&quot;") ;
   text = text.replace(/</g, "&lt;") ;
   text = text.replace(/>/g, "&gt;") ;
   text = text.replace(/'/g, "&#146;") ;
   text = text.replace(/\n/g,"<br/>");
   text = text.replace(/\t/g,"&nbsp;&nbsp;&nbsp;&nbsp;");
   text = text.replace(/(^\s+)|(\s+$)/g,"");
   return text;
}

function trim(text){
        text = text.replace(/(^\s+)|(\s+$)/g,"");
        return text;
}


function chineseTest(test){
	 var re=/^[\u4e00-\u9fa5]+$/;
	 return re.test(test);	
}

function EnglishTest(test){
	 var re=/^[a-zA-Z]+$/;
	 return re.test(test);	
}

function EnglishAndChineseTest(test){
	 var re=/^[a-zA-Z\u4e00-\u9fa5]+$/;
	 return re.test(test);	
}

function wordTest(test){
	 var re=/^[a-zA-Z\u4e00-\u9fa50-9\s]+$/;
	 return re.test(test);
}

function numTest(test){
	 var re=/^[0-9]+$/;
	 return re.test(test);			
}

function checkEmail(test){
	 var re=/^[0-9a-zA-Z\-_\.]+@[0-9a-zA-Z\-_]+\.[a-zA-Z]+\.*[a-zA-Z]*$/;
	 return re.test(test);	
}

function notSpecialWord(test){
	 var re=/^[a-zA-Z\u4e00-\u9fa50-9\s\-\.]+$/;
	 return re.test(test);	
}

function checkDateInput(test){
	 var re=/^\d\d\d\d\-\d{1,2}\-\d{1,2}$/;
	 return re.test(test);
}

function checkDateTimeInput(test){
	 var re=/^\d\d\d\d\-\d{1,2}\-\d{1,2}\s\d{}$/;
	 return re.test(test);
}

function firmTime(time){
     var re=/^\d\d\d\d\-((0\d)|(1[0-2]))\-(([0-2]\d)|(3[01]))\s(([01]\d)|(2[0-3])):[0-5]\d:[0-5]\d$/;
	 return re.test(time);
}

function checkIsImage(test){
	 var re=/\.(jpg)|(gif)|(bmp)|(png)|(jpeg)$/;
	 return re.test(test);		
}
function checkDecimal(test){
	var re=/(^[0-9]+$)|(^\d+[\.]\d+$)/;
	return re.test(test);		
}
function getViewportInfo() {     
     var w = (window.innerWidth) ? window.innerWidth : (document.documentElement && document.documentElement.clientWidth) ? document.documentElement.clientWidth : document.body.offsetWidth;     
	 var h = (window.innerHeight) ? window.innerHeight : (document.documentElement && document.documentElement.clientHeight) ? document.documentElement.clientHeight : document.body.offsetHeight;     
	 return {w:w,h:h}; 
}; 

function setOpacity(el){
		el.style.filter="alpha(opacity:70)";
		el.style.KHTMLOpacity="0.70";
		el.style.MozOpacity="0.70";
		el.style.opacity="0.70";
}


function showBack(){
	var id='';
	if(arguments[0]==''){
		id="thisToumingDiv";
	}else{
		id=arguments[0];
	}
	if(document.documentElement.scrollTop){
			var scrollpositionLeft=document.documentElement.scrollLeft;
			var scrollpositionTop=document.documentElement.scrollTop;
	}
	else{
			var scrollpositionLeft=document.body.scrollLeft;
			var scrollpositionTop=document.body.scrollTop;				
	}
	var thisToumingDiv=CreateEl("div","",id);
	var oWin=getViewportInfo();
	thisToumingDiv.style.position="absolute";
	thisToumingDiv.style.width="100%";
	thisToumingDiv.style.height=document.body.scrollHeight+"px";
	thisToumingDiv.style.left="0px";
	thisToumingDiv.style.top="0px";		
	thisToumingDiv.style.backgroundColor='#999999';
	setOpacity(thisToumingDiv);
	
	document.getElementsByTagName("body")[0].appendChild(thisToumingDiv);

}

function shutBack(){
	var id='';
	if(arguments[0]==''){
		id="thisToumingDiv";
	}else{
		id=arguments[0];
	}
	if(document.getElementById(id)){
		document.getElementsByTagName("body")[0].removeChild(document.getElementById(id));
	}
}


function getCenterDiv(width,height,left,top,id){
		var oWin=getViewportInfo();
		if(document.documentElement.scrollTop){
				var scrollpositionLeft=document.documentElement.scrollLeft;
				var scrollpositionTop=document.documentElement.scrollTop;
		}
		else{
				var scrollpositionLeft=document.body.scrollLeft;
				var scrollpositionTop=document.body.scrollTop;				
		}

		var thisTishiKuang=CreateEl("div","",id);
		thisTishiKuang.style.position="absolute";
		thisTishiKuang.style.width=width+"px";
		thisTishiKuang.style.height=height+"px";
		thisTishiKuang.style.backgroundColor='#fff';
		thisTishiKuang.style.display='none';
		

		thisTishiKuang.style.left=(oWin.w/2-left+scrollpositionLeft)+"px";
		thisTishiKuang.style.top=(oWin.h/2-top+scrollpositionTop)+"px";		
		
		thisTishiKuang.style.textAlign='center';
		thisTishiKuang.style.paddingTop='30px';
		thisTishiKuang.style.border='1px solid #000';
		
		return thisTishiKuang;
}

function showMsg(thismsg){
	
		var thisTishiKuang=getCenterDiv(350,80,175,35,'thisTishiKuang');
		
		showBack();
		
		document.getElementsByTagName("body")[0].appendChild(thisTishiKuang);
		
		$('#thisTishiKuang').fadeIn('slow');
		
		thisTishiKuang.innerHTML='<div align=center>'+thismsg+'</div><br /><div align=center><a onclick=shutMsg()><img src="html/images/shutdown.gif" /></a></div>';
		
		$('select').css('visibility','hidden');
}

function shutMsg(){
		if(document.getElementById("thisTishiKuang")||document.getElementById("thisToumingDiv")){
				document.getElementsByTagName("body")[0].removeChild(document.getElementById("thisTishiKuang"));
				$('select').css('visibility','visible');
				shutBack();
		}
}

function alterMsg(msg){
		document.getElementById("thisTishiKuang").innerHTML='<div align=center>'+msg+'</div><br /><div align=center><a onclick=shutMsg()><img src="html/images/shutdown.gif" /></a></div>';
}



function getRandomNum(lbound, ubound) {
	return (Math.floor(Math.random() * (ubound - lbound)) + lbound);
}

function getRandom(length){
	var thisStr="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	var thisArray=thisStr.split("");
	var str=""
	for(var i=0;i<length;i++){
		str+=thisArray[getRandomNum(1,61)];
	
	}
	return str;
}

function Locate(e,thisId){
		var posx=0,posy=0;
		if(e==null) e=window.event;
		if(e.pageX || e.pageY){
    			posx=e.pageX; posy=e.pageY;
    	}
		else if(e.clientX || e.clientY){
    			if(document.documentElement.scrollTop){
        				posx=e.clientX+document.documentElement.scrollLeft;
        				posy=e.clientY+document.documentElement.scrollTop;
        		}
    			else{
        				posx=e.clientX+document.body.scrollLeft;
        				posy=e.clientY+document.body.scrollTop;
       			}
   	    }
		document.getElementById(thisId).style.top=(posy+10)+"px";
		document.getElementById(thisId).style.left=(posx-20)+"px";
}

function fGetXY(aTag)
{
    var oTmp = aTag;
    var pt = new Point(0,0);
    do 
    {
		if(browser.name=="ie"){
        	pt.x += oTmp.offsetLeft;
        	pt.y += oTmp.offsetTop;
		}
		else{
        	pt.x += oTmp.offsetLeft+"px";
        	pt.y += oTmp.offsetTop+"px";				
		}
        oTmp = oTmp.offsetParent;
    } while(oTmp.tagName!="BODY");
    return pt;
}

function Point(iX, iY)
{
    this.x = iX;
    this.y = iY;
}

function subStrs(text,thislength){
	if(text.length>thislength){
		return text.substring(0,thislength)+"..";
	}
	else{
		return text;	
	}
}

function selectAll(thisObject,formId,inputName){
	var thisForm=document.getElementById(formId);
	var inputArray=thisForm.getElementsByTagName('input');
	if(thisObject.checked==true){
		for(var i=0;i<inputArray.length;i++){
			if(inputArray[i].name==inputName){
				inputArray[i].checked=true;	
			}
		}
	}
	else{
		for(var i=0;i<inputArray.length;i++){
			if(inputArray[i].name==inputName){
				inputArray[i].checked=false;	
			}
		}
	}
}

function reverseSelect(){
	var objectNum=document.userdisplay.elements.length;
	objectNum=objectNum-3;
	for(var i=0;i<objectNum;i++){
		if(document.userdisplay.elements[i].checked==true){
			document.userdisplay.elements[i].checked=false;
		}
		else{
			document.userdisplay.elements[i].checked=true;
		}

	}
}


function setCookie(name,newvalue,path,domain,expire){
   var expireDate=new Date();
   expireDate.setTime(expireDate.getTime() + expire);
   if(domain==""){
    	domainString="";
   }
   else{
	   	domainString="domain="+domain+";";
   }
   document.cookie=name+"="+newvalue+";path="+path+";"+domainString+"expires="+expireDate.toGMTString();
}

function getCookie(name){
   var sRE="(?:; )?"+name+"=([^;]*);?";
   var oRE=new RegExp(sRE);
   
   if(oRE.test(document.cookie)){
        return decodeURIComponent(RegExp["$1"]);
   }
   else{
        return "";
   }
}

//复制功能
  function   copyToClipboard(txt)   {   
            if(window.clipboardData)   {   
                            //window.clipboardData.clearData();   
                            window.clipboardData.setData("Text",   txt);   
							alert("Successful reproduction！");
            }   else   if(navigator.userAgent.indexOf("Opera")   !=   -1)   {   
                      window.location   =   txt;   
            }   else   if   (window.netscape)   {   
                      try   {   
                                netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");   
                      }   catch   (e)   {   
                                alert("被火狐瀏覽器拒絕！\n請在瀏覽器地址欄輸入'about:config'并回車\n然后將'signed.applets.codebase_principal_support'設置為'true', 並且建議使用IE瀏覽器!");   
                      }   
                      var   clip   =   Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);   
                      if   (!clip)   
                                return;   
                      var   trans   =   Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);   
                      if   (!trans)   
                                return;   
                      trans.addDataFlavor('text/unicode');   
                      var   str   =   new   Object();   
                      var   len   =   new   Object();   
                      var   str   =   Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);   
                      var   copytext   =   txt;   
                      str.data   =   copytext;   
                      trans.setTransferData("text/unicode",str,copytext.length*2);   
                      var   clipid   =   Components.interfaces.nsIClipboard;   
                      if   (!clip)   
                                return   false;   
                      clip.setData(trans,null,clipid.kGlobalClipboard);   
            }   
  }
  
function getDataBrowser(thisArray){
	var thisAjaxText="";
	if(thisArray[0].text){
		thisAjaxText=thisArray[0].text;
	}
	else{
		thisAjaxText=thisArray[0].textContent;	
	}
	return thisAjaxText;
}

function refreshItemYz(thisA){
	thisA.src=thisA.src.split('?').shift()+"?rand="+Math.random();
}

function shouldDel(){
	if(!confirm("确定要删除条目吗?")){
		return false;	
	}
	return true;
}
function checkShouldDelItems(formId,inputName,isparent){
	var thisForm=document.getElementById(formId);
	var flag=0;
	var inputArray=thisForm.getElementsByTagName('input');
	for(var i=0;i<inputArray.length;i++){
		if(inputArray[i].name==inputName){
			if(inputArray[i].checked==true){
				flag++;	
			}
		}
	}
	if(flag==0){
		if(isparent==1){
			parent.showMsg("您没有选择任何条目，请返回修改！");
		}else{
			showMsg("您没有选择任何条目，请返回修改！");	
		}
		return false;
	}
	else{
		if(confirm("确定选中这些条目吗？")){
			return true;
		}
		else{
			return false;	
		}
	}	
}

function changeLanguage(){
	showMsg("<img src='"+ROOT_PATH+"/html/images/loading.gif' />");
	if(getCookie(COOKIE_PRE+'language')=="GB"){
		setCookie(COOKIE_PRE+'language','BIG5','/','',3600*24*365);
	}else if(getCookie(COOKIE_PRE+'language')=="BIG5"){
		setCookie(COOKIE_PRE+'language','GB','/','',3600*24*365);
	}else{
		setCookie(COOKIE_PRE+'language','BIG5','/','',3600*24*365);
	}
	window.location.reload();
}
var locationFl={
	inputid:'flid',
	getChildFenlei:function(thisSelect){
		$('#'+locationFl.inputid).val(0);
		$(thisSelect).after('<img id="flloading" style="margin-left:5px" src="html/images/admins/fl_loading.gif" />');
		var pid=$(thisSelect).val();
		if(pid!=0){
			$.post(ROOT_PATH+"/seller"+CHOSING_DE+"index"+CHOSING_DE+"getlocationajax",{pid:pid},function(data) {
					$('#flloading').remove();
					eval("var flArray="+data);
					if(flArray.length!=0){
						var selectString=' <select onchange="locationFl.getChildFenlei(this);"><option value="0">请选择地区</option>';
						for(var i=0;i<flArray.length;i++){
							selectString+="<option value="+flArray[i].flid+">"+flArray[i].flname+"</option>";
						}
						selectString+='</select> ';
						locationFl.removeSelect(thisSelect);
						$(thisSelect).after(selectString);
					}else{
						locationFl.removeSelect(thisSelect);
						$('#'+locationFl.inputid).val(pid);
					}
			});	
		}else{
			$('#flloading').remove();
			locationFl.removeSelect(thisSelect);
		}
	},
	removeSelect:function(thisSelect){
		if($(thisSelect).next().is('select')){
			var nextSelect=$(thisSelect).next();
			var removeSelect=new Array();
			removeSelect.push(nextSelect);
			while(nextSelect.next().is('select')){
				nextSelect=nextSelect.next();
				removeSelect.push(nextSelect);
			}
			for(var i in removeSelect){
				removeSelect[i].remove();
			}
		}			
	}
}

var userPmInfo={
	kuang:new Object(),
	innerHTML:"",
	sendTo:"",
	mustFlid:'',
	orderid:'',
	subject:'',
	caseitemid:'',
	show:function(){
		if(getCookie(COOKIE_PRE+"member_admin")==''){
			if(confirm("Do you want to login? Or you can't send messages!")){
				window.location.href=$('.login_action').attr('href');
			}
			return false;
		}
		this.createKuang();
		this.setKuangContent();
	},
	createKuang:function(){
		var thisPmKuang=getCenterDiv(700,480,350,240,'thisPmKuang');
		showBack('pmBackDiv');
		document.getElementsByTagName("body")[0].appendChild(thisPmKuang);
		$('#thisPmKuang').fadeIn('slow');
		$('#thisPmKuang').css('paddingTop','0px');
		this.kuang=thisPmKuang;
	},
	shutKuang:function(){
		shutBack('pmBackDiv');
		$(this.kuang).remove();
	},
	setKuangContent:function(){
		var member_admin=getCookie(COOKIE_PRE+"member_admin");
		this.innerHTML='<div class="floatPmTop"><a style="float:right" onclick="userPmInfo.shutKuang();">shut</a>Message Form</div>';
		this.innerHTML+='<div class="floatPmCenter"><iframe width="700px" height="450px" src="'+member_admin+'-pm-add?fromto=to&flid=add&mustFlid='+this.mustFlid+'&sendto='+this.sendTo+'&orderid='+this.orderid+'&subject='+this.subject+'&caseitemid='+this.caseitemid+'"></iframe></div>';
		this.innerHTML+='<div class="floatPmBottom"></div>';
		$(this.kuang).html(this.innerHTML);
		this.mustFlid='';
		this.sendTo='';
		this.orderid='';
		this.subject='';
		this.caseitemid='';
	}
}

var userChatBox={
	kuang:new Object(),
	innerHTML:"",
	to_uid:0,
	flid:'',
	show:function(){
		this.createKuang();
		this.setKuangContent();
	},
	createKuang:function(){
		var thisPmKuang=getCenterDiv(650,460,325,230,'thisChatKuang');
		showBack('chatBackDiv');
		document.getElementsByTagName("body")[0].appendChild(thisPmKuang);
		$('#thisChatKuang').fadeIn('slow');
		$('#thisChatKuang').css('paddingTop','0px');
		this.kuang=thisPmKuang;
	},
	shutKuang:function(){
		shutBack('chatBackDiv');
		$(this.kuang).remove();
	},
	setKuangContent:function(){
		var member_admin=getCookie(COOKIE_PRE+"member_admin");
		this.innerHTML='<div class="floatPmTop"><a style="float:right" onclick="userChatBox.shutKuang();">shut</a>Chat box</div>';
		this.innerHTML+='<div class="floatPmCenter"><iframe id="chatBoxFrame" width="645px" height="420px" src="chat?to_uid='+this.to_uid+'"></iframe></div>';
		this.innerHTML+='<div class="floatPmBottom"></div>';
		$(this.kuang).html(this.innerHTML);
		this.mustFlid='';
		this.sendTo='';
		this.orderid='';
		this.subject='';
		this.caseitemid='';
	}
}

var expressEffection={
	checkForm:function(lang){
		if($('select[name="from_station_'+lang+'"]').val()=='0'){
			alert('Please select which place you want to ship from!');
			return false;
		}
		if($('select[name="to_station_'+lang+'"]').val()=='0'){
			alert('Please select which place you want to ship to!');
			return false;
		}
		if(!checkDecimal($('input[name="weight"]').val())||$('input[name="weight"]').val()==0){
			alert('Please check the weight you just input!');
			return false;				
		}
		if($('input[name="size1"]').val()!=''||$('input[name="size2"]').val()!=''||$('input[name="size3"]').val()!=''){
			if(!checkDecimal($('input[name="size1"]').val())||!checkDecimal($('input[name="size2"]').val())||!checkDecimal($('input[name="size3"]').val())){
				alert('Please check the size you just input!');
				return false;		
			}
		}else{
			$('input[name="size1"]').val(0);
			$('input[name="size2"]').val(0);
			$('input[name="size3"]').val(0);
		}
		$('input[name="volume"]').val($('input[name="size1"]').val()+'*'+$('input[name="size2"]').val()+'*'+$('input[name="size3"]').val());
		$('#interExpress').submit();
	},
	changExpress:function(mode,carrier){
		if(mode=='1'){
			$('#firstDiv').css('display','none');
			$('#secondDiv').css('display','block');
			$('#CarriersDiv').html(carrier);
		}else{
			$('#firstDiv').css('display','block');
			$('#secondDiv').css('display','none');					
		}
	}
}

var countDown={
	trimTime:function(time){
		if(time<10){
			time='0'+time;	
		}
		return time;
	},
	setTimes:function(time){
		if(time<0){
			time=0;	
		}
		var hour=parseInt(time/3600);
		var minite=parseInt(time%3600/60);
		var seconds=parseInt(time%3600%60);
		return this.trimTime(hour)+":"+this.trimTime(minite)+":"+this.trimTime(seconds);
	}	
}

var myWebChat={
	currentUserOrderId:0,
	chatAllTimeOut:0,
	java_url:'',
	shakeTimeOut:0,
	titleMoveTimeOut:0,
	historyTitle:'',
	getAllMsg:function(){
		var from_uid=getCookie(COOKIE_PRE+'uid');
		if(numTest(from_uid)){
			currentUserOrderId=this.currentUserOrderId;
			$.getJSON(this.java_url+"/ChatGetAll?callback=?",{from_uid:from_uid,currentUserOrderId:currentUserOrderId});
		}
	},
	jsonGetAllMsg:function(json){
		clearTimeout(this.chatAllTimeOut);
		if(this.historyTitle==''){
			this.historyTitle=document.title;
		}
		if(json.has=='1'){
			$('#chatMsgCounts').html(json.res.length);
			this.shake();
			this.titleMove("_You_have_"+json.res.length+"_new_message(s)_你有"+json.res.length+"条新信息!");
		}else{
			$('#chatMsgCounts').html(0);
			clearTimeout(this.shakeTimeOut);
			clearTimeout(this.titleMoveTimeOut);
			$('#webChatA').css('color','#666666');
			$('#webChatA').css('fontWeight','normal');
			document.title=this.historyTitle;
		}
		this.chatAllTimeOut=setTimeout(function(){myWebChat.getAllMsg()},3000);
	},
	shake:function(){
		clearTimeout(this.shakeTimeOut);
		if($('#webChatA').css('color')=='#666666'){
			$('#webChatA').css('color','#ff0000');
			$('#webChatA').css('fontWeight','bold');
		}else{
			$('#webChatA').css('color','#666666');
			$('#webChatA').css('fontWeight','normal');
		}
		this.shakeTimeOut=setTimeout(function(){myWebChat.shake();},2000);
	},
	titleMove:function(text){
		clearTimeout(this.titleMoveTimeOut);
		text=text.substring(1,text.length)+text.substring(0,1);
		document.title=text.replace(/_/g,' ');
		this.titleMoveTimeOut = setTimeout(function(){myWebChat.titleMove(text);}, 100);
	}
}
