var isie=navigator.userAgent.indexOf("MSIE")>-1,zi=999,showCode=false;

function readCookie(name){
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end));
    }
  }
  return cookieValue;
}

function writeCookie(name, value,second)
{
  var expire = "";
    expire = new Date((new Date()).getTime() +second);
    expire = "; expires=" + expire.toGMTString();
  document.cookie = name + "=" + escape(value) + expire + "; path=/;";
}


function addCompare(c,sn,n){
  if(readCookie('compare_c')=='') writeCookie('compare_c',c,10*3600000);
  if(readCookie('compare_c')!=c){
     if(confirm('所选产品分类跟对比篮内产品分类不一致？\r\n是否清空对比篮？')) cleanCompare(); else return;
  }
  var str=readCookie('compare_i');
  if(str.split('#').length>5){
    alert('最多能对5个产品进行比较!');
	return;
  }
 
  var pro=sn+'|'+n+'#';
  if(str.indexOf(pro)==-1){
      writeCookie('compare_i',str+pro,10*3600000);
	  changeItem();
  }else{
    alert('该产品已经在对比篮，无须重复添加!');
	return;
  } 

}


//清除对比篮
function cleanCompare(){
  writeCookie('compare_c','',-1);
  writeCookie('compare_i','',-1);
  document.getElementById('compare_div').innerHTML='';
}

function changeItem(){
 var str=readCookie('compare_i');
 str=str.substr(0,str.length-1);
 if(str==''){
	 return;
 }
 var arr=str.split('#');
 html='<ul>';
  for(i=0;i<arr.length;i++){
      var _item=arr[i].split('|');
      html+='<li>&middot;<input type="hidden" name="sn[]" value="'+_item[0]+'" />'+_item[1]+'</li>';
  }
  html+='</ul>';
 document.getElementById('compare_div').innerHTML=html;
}

window.onscroll=function(){
 if(o=document.getElementById('compare_box')){
   o.style.top=document.documentElement.scrollTop+document.documentElement.clientHeight/4+'px'; 
 }
}

function checkCompare(f){
	if(!f['sn[]']){
		return false;
	}
   var l=f['sn[]'].length;
   if(l<2||typeof l =='undefined'){
	  alert('最少需要两个产品才能进行比较!');
	 return false;
	}
	if(l>5){
	  alert('最多能对5个产品进行比较!');
	  return false;
	}
	f.target="_blank";
	return true;
}

function addOption(o,t,v)
 {
  var op= document.createElement("OPTION");
  op.text= t;
  op.value=v;
  o.options.add(op);
 }

function fillSub(index,tar,selected){
	var oTar=document.getElementById(tar);
	oTar.options.length=1;
	if(index==0) return;
	var data=s_c[index-1];

	for(i=0;i<data.length;i++){
		addOption(oTar,data[i][1],data[i][0]);
		if(selected==data[i][0]) {
		  oTar.options.selectedIndex=i+1;
		}
	}
}

function changeSelected(src,v){
	var o=document.getElementById(src);
	var ops=o.options;
	for(i=0;i<ops.length;i++){
	   if(ops[i].value==v) {
		   o.selectedIndex=i;	
	       return;
	   }
	}
}

function checkSearch(f){
  var v=f.q.value.replace(/\040/g, "");
  /*
  if(v.length<3){
	 alert('关键词太不能少于3位字符');
	 return false;
  }	
  */
  if(v=='请输入关键词'){
	  return false;
  }
  return true;
}

function offs(o,p){
 var x=0,y=0;
 while((o!=null)&&(o!=p)){
  x+=o.offsetLeft;y+=o.offsetTop;o=o.offsetParent;
 }
 return{x:x,y:y};
 }
 function attr(o,a){
  var v=o[a];
  if(v==null) v=o.getAttribute(a);
  if(v==="")v=null;return v;
}

function showElement(o,t){
 //if('none'==o.style.display){
  var off=offs(t,document.body);
  var i=document.getElementById('resizeMonitor');
  var x=0;
  var y=0;
  
  if(attr(t,'pos')==1){
      x=off.x+t.offsetWidth;
	  y=off.y;
  }else{
	 x=(off.x + 265> document.body.offsetWidth)?(document.body.offsetWidth-265):off.x;
	 y=off.y+parseInt(t.offsetHeight);
  }
 
  o.style.top = y+'px';
  o.style.left = x+'px';
  
  i.style.zIndex = zi;
  o.style.zIndex = ++zi;
  
  o.style.display = 'block';
  
  i.style.width = o.offsetWidth+'px';
  i.style.height = o.offsetHeight+'px';
  i.style.top = y+'px';
  i.style.left = x+'px';

  //}
}

function hideElement(o){
 if('none'!=o.style.display){
  o.style.display='none';
  with(document.getElementById('resizeMonitor'))
   {
    style.width='0px';
	style.height='0px';
	style.top='0px';
	style.left="0px"
   }
  }
}
function init(o,a){
  return isie?a.contains(o):objo(o,a)!=null;
}
function objo(o,a,v,c){
	var m;
	while((m==null)&&(o!=document.body)){
		if(((v==null)&&(o==a))||((v!=null)&&(attr(o,a)==v))){
			m=o;if(c!=null){
				if(typeof(c)=="function")c(o); else c.push(o);
			}
			break;
			}
		o=o.parentNode;
		}
    return m;
}

document.onmouseover=function(e){
  var e=e?e:event;
  e.cancelBubble=true;
  var o=e.target?e.target:e.srcElement;
  if(null!=attr(o,'show')){
      if(t=document.getElementById(attr(o,'show'))){
	    t.onmouseover=null;
		 t.onmouseout=o.onmouseout=function(e){
		     var e=e?e:event;
			 e.cancelBubble=true;
		     if(!init((e.relatedTarget||e.toElement),t)){
			     hideElement(t);
			  }
		  } 
		  showElement(t,o);
	  }
   }

}



function updateseccode(width, height) {
 if(showCode) return; 
	showCode=true;
	document.getElementById('yanzhengma').innerHTML = '<img id="seccode" onclick="updateImages(' + width + ', '+ height + ')" width="' + width + '" style="cursor:pointer" height="' + height + '" src="/include/getcode.inc.php?update=' + Math.random() + '" class="absmiddle" alt="" />';
}

function updateImages(width, height){
	document.getElementById('yanzhengma').innerHTML = '<img id="seccode" onclick="updateImages(' + width + ', '+ height + ')" width="' + width + '" style="cursor:pointer" height="' + height + '" src="/include/getcode.inc.php?update=' + Math.random() + '" class="absmiddle" alt="" />'
}

function checkComment(f){
  if(f.username.value==''){
	 alert('昵称不能为空');
	 f.username.focus();
	 return false;
   }
   if(f.content.value.length<4||f.content.value.length>500){
	  alert('评论字数超出范围!');
	  f.content.focus();
	  return false;
	}
   if(!(/^[0-9]{4}$/.test(f.code.value))){
	 alert('验证码由4个数字组成!');
	 f.code.focus();
	 return false;
   }
	return true;
}
function open_modify_div(){ return void(0);}