var isIE6 	= navigator.userAgent.indexOf("MSIE 6.0")>0;
var isIE7 	= navigator.userAgent.indexOf("MSIE 7.0")>0;
var isIE8 	= navigator.userAgent.indexOf("MSIE 8.0")>0;
var isFF	= navigator.userAgent.indexOf("Firefox") >0;
var isWorld	= navigator.userAgent.indexOf("TheWorld") >0;
var isIE	= navigator.userAgent.indexOf("MSIE")>0;		
		function GetCheckValue(name){			
			var chkValue="";
			var chk=document.getElementsByName(name);			
			
			for(var i=0;i<chk.length;i++){
				if(chk[i].checked==true){
					chkValue+=chk[i].value+",";
				}
			}
			return chkValue;			
		}
		
		function toQzoneLogin()
        {
        	var A=window.open("/tencent/oauth/redirect_to_login.php","TencentLogin","width=450,height=320,menubar=0,scrollbars=1, resizable=1,status=1,titlebar=0,toolbar=0,location=1");
        }
		
		function SetCheckboxs(name,values){
			var chk=document.getElementsByName(name);	
			for(var i=0;i<chk.length;i++){
				chk[i].checked=false;
			}
			for(var i=0;i<chk.length;i++){
				var vs=values.split(",");
				
				for(var m=0;m<vs.length;m++){
					if(vs[m]==chk[i].value)chk[i].checked=true;
				}
			}	
		}		
		function GetRadioValue(name)
		{
			var rad= document.getElementsByName(name);			
		    for(var i=0;i<rad.length;i++)
			{
		    	if(rad[i].checked==true)
				{
					return rad[i].value;
		            break;
				}
			}  
		    return "";
		}
		function SetRadioState(name,value){
			var rad= document.getElementsByName(name);	
			// alert(rad.length);
		    for(var i=0;i<rad.length;i++)
			{
		    	rad[i].disabled =value;  
			}	
		}
		function SetElementEnable(name,value)
		{
			var rad= document.getElementsByName(name);			
		    for(var i=0;i<rad.length;i++)
			{
		    	if(rad[i].value==value)
				{
		    		rad[i].disabled =true;
		            break;
				}
			}  
		} 
		function SetRadio(name,value)
		{
			var rad= document.getElementsByName(name);			
		    for(var i=0;i<rad.length;i++)
			{
		    	if(rad[i].value==value)
				{
		    		rad[i].checked=true;
		            break;
				}
			}  
		}
		function SetElementValue(name,value){
			var eles=document.getElementsByName(name);
			for(var i=0;i<eles.length;i++){
				eles[i].value=value;
			}
		}
		
		// func= { obj:obj,callback:callback}
		function TPostData(str){  	  	
	  	  	var s1=str.split('&');
	    	var obj= {};
	  	  	for(var i=0;i<s1.length;i++){
	  	  	  	if(s1[i]=='')continue;  	  	  	
	  	  	  	var s2=s1[i].split('=');  	  	  	
	  	  	  	obj[s2[0]]=s2[1];
	  	  	}  	  	
	  	  	return obj;
	  	}
		function ajaxCall(form,uri,method,func,_async){
 			if(_async == null || typeof _async == 'undefined' || _async!=false)
				_async		= true;
  			var jsonRequest = new Request.JSON({
				url			: uri,
				async		: _async,
				onComplete	: function(res, text){
  					if((typeof res) == 'undefined' || res == null){
						alert('ajax错误');
						return;
					}
 					if((typeof func).toLowerCase() == 'function'){
						func(res);	
					}else if((typeof func).toLowerCase() == 'object'){
						func.obj[func.callback](res);
					}
				}
			})[method](form );
		};	
		
		function AjaxExtend(methodtype,urladdress,updateid,postdata,onSuccee,returnScript)
		{ 			
			var req=new Request({
				method:methodtype,
				url:urladdress+'&t='+$time(),
				data:postdata,
				onSuccess:function(responseText){					
					if(returnScript==true){						
						$exec(responseText);
						return; 
					}
					if($(updateid)!=null){
				 		if(responseText!=""){				 			
				 			$(updateid).innerHTML=responseText;
				 		}
					}
					if(onSuccee!=null){				 
						if(typeof onSuccee=='function') onSuccee(responseText);
						else $exec(onSuccee);
					}
				},
				onFailure:function (res){
					alert(res);
				}
			} );
			req.send();
		}	
		function LightBox(url,title,method,funcSuccess){
			LightBoxExtend(url,title,method,2,funcSuccess,650,450);						
		}
		function LightBox2(url,title,method,funcSuccess,w,h){
			LightBoxExtend(url,title,method,2,funcSuccess,w,h);	
		}
		function LightBoxExtend(url,title,method,mode,funcSuccess,w,h){
			var aj= new Ajax();
			aj.options.url=url;	
			aj.options.onSuccess=funcSuccess;			
			var dia=aj.options.dialoge;
			dia.mode=mode;
			dia.w=w;
			dia.h=h;
			dia.title=title;
			if(method==null)aj.options.method="get";			
			aj.send();
		}
		function ViewCart2(res){
			$('cartgname').innerHTML	= res.AddProductToCart;
			addcartback(_gpelem);
		}
		function ViewCart(){
			var url="cart.php?act=ajaxviewcart";
			LightBox2(url,'查看购物车','get',null,900,400);
		}
		var _gpid;var _gpelem;
		function AddCart(productid,productscope,amount,productprice,fatherid,fathertype,pro_str,elem){
			if(typeof pro_str=='undefined')pro_str='';
			if(typeof elem =='undefined') elem=null;
			var url="cart.php?act=ajaxaddproduct";
			url+="&pid="+productid+"&pscope="+productscope+"&amount="+amount;
			url+="&pprice="+productprice+"&fatherid="+fatherid+"&fathertype="+fathertype+'&pro_str='+encodeURI(pro_str);
			_gpid	= productid;_gpelem=elem;
			// AjaxExtend('get',url,'',null,ViewCart2);
			ajaxCall({},url,'get', ViewCart2,false);
			// AjaxExtend(methodtype,urladdress,updateid,postdata,onSuccee,returnScript)
		}	
		function CartAmountKeyDown(){			
			if(event.keyCode == 13) event.returnValue = false;
		}
		function changeBar(act,cartid,productid,amount,orderid){
	 	 	if(amount<1){
	 	 		amount=1;	 	 	 	
	 	 	}
	 	 	if(act=='remove'){
	 	 		if(!confirm('你确定删除本商品?'))return;
	 	 	}
	 	 	var url="cart.php?act=ajaxadjustamount&adusttype="+act+"&cartitemid="+cartid+"&productid="+productid+"&amount="+amount+"&orderid="+orderid;
	 	 	// 暂时通过reload方式处理数据传输的问题
	 		// AjaxExtend('get',url,'productList',null,"window.location.reload();");
	 		ajaxCall({},url,'get', ChangeBarBack,false);
	 		// ajaxCall({'tid':'<%$tid%>','mid':'<%$mid%>','cid':'<%$cid%>'},'?act=getitem','get',
			// sendback,false);
	 	}	
		function TGOCart(pid){
	 	 	var url='cart.php?act=tgo&pid='+pid;
	 	 	ajaxCall({},url,'get', ChangeBarBack,false);
	 	 	// AjaxExtend('get',url,null,'','',true);
	 	 	// AjaxExtend(methodtype,urladdress,updateid,postdata,onSuccee,returnScript)
	 	}
		function ChangeBarBack(res){
			$('h_cart_t').innerHTML=res.CartListHtml;
			$('divTGO').innerHTML=res.TGOProductHtml;
			cartinit();
		}
		function GotoOrder(orderid){
			var url="cart.php?act=gotoorder&orderid="+orderid ;
			AjaxExtend('get',url,'',null,null,true);
		}
		function ChkPayinfo(){ 	 	
	 	 	var _shiptype='';
	 	 	if($('shiptype')!=null){
	 	 		_shiptype=$('shiptype').value;		 	 		  	
		 	 	if(_shiptype==""||_shiptype==0){
		 	 	 	alert('请选择配送和支付方式.');
		 	 	 	return false;
		 	 	}	
	 	 	}
	 	 	if($('expressid')!=null){
		 	 	var deliveryid=GetRadioValue('expressid');
		 	 	if(deliveryid==''){
		 	 		alert('请选择快递公司');return false;
		 	 	}
	 	 	}
	 	 	// if(deliveryid)
	 	 	var delidatetype='';	 	 	
	 	 	if($('deliverydatetype')!=null){
	 	 		delidatetype=GetRadioValue('deliverydatetype');
	 	 		if(delidatetype==""){
		 	 	 	alert('请选择配送时间');return false;
		 	 	}
	 	 	}
	 	 	return true;
	 	}
		
		function search_goods(){
			if($('keywords').value.trim()!=''){
				return true;
			}	else return false;
		}


function mkCoverDiv(opacity,zindex){
	var div = document.createElement('div');
	var pos	= getPos();
	div.style.cssText	= 'position:absolute;background:#FFF; top:0;left:0;width:'+ (pos.W-20) +'px;height:'+ (pos.H-20) +'px;z-index:'+zindex;
	setOpacity(div,opacity);
	document.body.appendChild(div); 
	return div;	
}

function mkLoading(str,zindex1){
	var _id='tip_'+zindex1;
	if($(_id)!=null){
		$(_id).style.display='block';
		return $(_id);
	}
	var div1 = document.createElement('div');
	var pos1 = getPos(); 

	div1.className	= 'loading clearfix';div1.style.height	= 32+'px';
	div1.innerHTML	= ' <span><img src="images/Loading.gif" /></span><span>'+str+'</span>';
	 document.body.appendChild(div1);
	 div1.style.left	= (pos1.W-pos1.x-div1.offsetWidth)/2 + 'px';
	 div1.style.top		= ( pos1.y- div1.offsetHeight  +300 )+'px';
	 div1.style.zindex	= zindex1;
	 div1.id=_id;
	 //alert(div1.id);
	return div1;
}
function addEventHandler(oTarget, sEventType, fnHandler) {
	if (oTarget.addEventListener) {
		oTarget.addEventListener(sEventType, fnHandler, false);
	} else if (oTarget.attachEvent) {
		oTarget.attachEvent("on" + sEventType, fnHandler);
	} else {
		oTarget["on" + sEventType] = fnHandler;
	}
};
 
function removeEventHandler(oTarget, sEventType, fnHandler) {
    if (oTarget.removeEventListener) {
        oTarget.removeEventListener(sEventType, fnHandler, false);
    } else if (oTarget.detachEvent) {
        oTarget.detachEvent("on" + sEventType, fnHandler);
    } else { 
        oTarget["on" + sEventType] = null;
    }
};
function setOpacity(elem,n){
	document.all ? elem.style.filter = 'alpha(opacity=' + n + ')' :
    elem.style.opacity = n/100;
}

function getOpacity(obj){
	return document.all ? parseInt(obj.filters["alpha"].opacity) : 100 * parseFloat(obj.style.opacity); 
};
function getOffset(o){
		var t,l,w,h;
		t=l=0; w=o.offsetWidth;h=o.offsetHeight;	 
		while(o){
			t += o.offsetTop;
			l += o.offsetLeft;
			o = o.offsetParent;
		}
		while(o&&o!=document.body){ 
			t -= o.scrollTop;
			l -= o.scrollLeft;
			o = o.parentNode;
		}
    	return {top:t,left:l,height:h,width:w};     
}
function getPos(){
	var myw,myh,myX,myY,myW,myH;
	if(window.innerWidth){
		myw = window.innerWidth;
		myh = window.innerHeight;
		myX = window.pageXOffset;
		myY = window.pageYOffset;
	}else{
		myw = document.documentElement.clientWidth;
		myh = document.documentElement.clientHeight;
		myY = document.documentElement.scrollTop;
		myX = document.documentElement.scrollLeft;
	}
	
	if(window.innerHeight && window.scrollMaxY){
		myW = window.innerWidth + window.scrollMaxX;
		myH = window.innerHeight + window.scrollMaxY;
	}else {
		myW = document.documentElement.scrollWidth ;
		myH = document.documentElement.scrollHeight;
	} 
	myH	= myH > myh ? myH : myh;
	return {w:myw,h:myh,x:myX,y:myY,W:myW,H:myH};
}
 
function trim(str){return str.replace(/^\s*|\s*$/g,'');}
function getkey(e){
	e	= getEvent(e);
	if(isFF)
		return e.which;
	else
		return e.keyCode;
}
function srcElement(e){
  if (typeof e == "undefined") e = window.event;
  var src = document.all ? e.srcElement : e.target;
  return src;
}

function getEvent(e){if (typeof e == "undefined") e = window.event; return e;};
 
 
var _class	= {
	create: function() {
		return function() { this.initialize.apply(this, arguments); };
	}
};
var _extend 	= function(destination, source) {
	for (var property in source) {
		destination[property] = source[property];
	}
};
var _each	= function(list, fun){
	for (var i = 0, len = list.length; i < len; i++) { fun(list[i], i); }
};
 
function getClientXY(e){
	e	= getEvent(e);
	return {'x':e.pageX||(e.clientX+(document.body.scrollLeft||document.documentElement.scrollLeft)),'y':e.pageY||(e.clientY+(document.body.scrollTop||document.documentElement.scrollTop))};	
}
function bind(object, fun) {
	return function() {
		return fun.apply(object, arguments);
	};
}
function bindAsEventListener(object, fun) {
	return function(event) {
		return fun.call(object, (event || window.event));
	};
}


var loginFlag	= '';
function showbDom(event,id){
 if($(id).style.display=='block') return false;
	$(id).style.left	= getOffset($('pbrand')).left-296+'px';
	setOpacity($(id),80);
  $(id).style.display	= 'block';
  $('brand_ay').className	= "brand_ay2";
}
function showDom(event,id){
  if($('cartinfo').style.display=='block' ||　/cart.php/.test(window.location)) return false;
  
  var url	= '/cart.php?act=getcartlist';
  AjaxExtend('get',url,'cartinfo',null);
  var pcart	= $('pcart');
  
  $(id).style.left	= getOffset($('pcart')).left-107+'px';
  $(id).style.display	= 'block';	
  
} 
function DomNone(id){
  $(id).style.display	= 'none';
}
function showNoDom(event,elem,p,c ){	
	var obj	= document.all? window.event.toElement  : event.relatedTarget ;
	if(ischild(obj,p) ) return false; 
	DomNone(c); $('brand_ay').className	= "brand_ay1";
}
function ischild(elem,id){
	while(elem){ 
		  if(elem.id == id){		   
			  return true;
		  }
		  elem	= elem.parentNode;
	}
	return false;
}
function deldom(id){
  var url	= '/cart.php?act=getcartlist';
  AjaxExtend('get',url,'cartinfo',null);
  cartinit();
  $(id).style.display	= 'block';	
}
function isemail(email){
  	var reg1 = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/;
  	return reg1.test( email );
}

function AlertMsg(alertid,closeid){
	
	var myAlert=$(alertid);//var _close=$(closeid);
	//alert(document.body.clientHeight);
	
	mybg = document.createElement("div"); 
	mybg.setAttribute("id","mybg"); 
	mybg.style.background = "#000"; 
	mybg.style.width = document.body.clientWidth; 
	mybg.style.height =document.body.clientHeight;
	mybg.style.position = "absolute"; 
	mybg.style.top = "0"; 
	mybg.style.left = "0"; 
	mybg.style.zIndex = "500"; 
	mybg.style.opacity = "0.3"; 
	mybg.style.filter = "Alpha(opacity=30)"; 
	document.body.appendChild(mybg);
	myAlert.style.display = "block"; 
	myAlert.style.position = "absolute";
	myAlert.style.top =(screen.height-myAlert.offsetHeight)/2+"px";
	myAlert.style.left =(screen.width-myAlert.offsetWidth)/2+"px" ;	
	document.body.style.overflow = "hidden";
	var mClose = $(closeid); 
	mClose.onclick = function() 
	{ 
		myAlert.style.display = "none"; 
		mybg.style.display = "none"; 
	}
}


//----------------------------
var _regionid=null;
	function GetRegions(selfobj,childid,regiontype){ 	 
		var url="orderinfo2.php?act=ajaxgetregions";
		url+="&parentid="+selfobj.options[selfobj.selectedIndex].value;
		url+="&regiontype="+regiontype; 		
		//AjaxExtend('get',url,childid,'',"GetExAddress()",false);
		_regionid=childid;
		ajaxCall({},url,'get', GetExAddress,false);
	}
	function GetExAddress(res){
	 	if(res!=null){ 
	 		if(res.RegionInfo!='')$(_regionid).innerHTML=res.RegionInfo;
	 	}	 	
		var exAddress=''; 	
		if($('region').selectedIndex>0){
	 		exAddress=$('region').options[$('region').selectedIndex].text;
		}
		else{return;} 		
		$('exAddress').innerHTML=exAddress;
		if($('city').selectedIndex>0){
	 		exAddress+=' '+$('city').options[$('city').selectedIndex].text;	
		}
		else{return;}
		$('exAddress').innerHTML=exAddress;
		if($('area').selectedIndex>0){
	 		exAddress+=' '+$('area').options[$('area').selectedIndex].text;
	 	 	GetAreaId('area');
		}
		else{GetAreaId('city'); return;} 	
		$('exAddress').innerHTML=exAddress;	
	
	}
	// 获得邮政编码 	 
	function GetAreaId(str){
		var areaid	= $(str).options[$(str).selectedIndex].value;	
	 	if(parseInt(areaid)<=0) return false;
	var url	= "orderinfo2.php?act=ajaxgetzip&rid="+areaid; 		 
	//AjaxExtend('get',url,'','', SetZip,false);
	ajaxCall({},url,'get', SetZip,false);
	}
	function SetZip(res){
		if((typeof res).toLowerCase() == 'object'){
			if(!/^\d{6}$/.test(res.postcode)) res.postcode='';
			$('zip').value = res.postcode;
			if(/^\d+$/.test(res.telecode)) $('areacode').value = res.telecode;	
		}
	}
	function ShowBrandImg(){
		var imgs	= $('con2').getElementsByTagName('img');
		for(i=0;i<imgs.length;i++){ 
		 	//if(imgs[i].src!='p_loading.gif') continue;		 	
		 	imgs[i].src	= imgs[i].getAttribute('_src');			
		}
	}
