﻿// JScript 文件
cookieName = "offerId";
  //domainName = ".<% = Request.ServerVariables["HTTP_HOST"] %>";
  prefix = "";
  bgColor = "FEEEE0";
  
  var categoryId = document.getElementById("CatSId").value;
  
  var c = getCookie("categoryId");
  if (c == null || c == "null" ){
      addCookie("categoryId",categoryId); 
      if (categoryId !=null && categoryId!=""){
        clearCookie(cookieName);    
      }
  }else if(c !=categoryId){
      clearCookie("categoryId");  
      clearCookie(cookieName);
  }
     
   function addCookie(name,value) {
     value = value+prefix;
    if(categoryId =="")
        return;
    document.cookie = name + "=" + value +";path=/;";//+ "domain="+domainName;
   }
   
  function clearCookie(name) {
	  var value = "null";
	  cookieValue= name + "=" + value + ";path=/;" ;//+ "domain="+domainName;
	  document.cookie = cookieValue;
  }
    
  function addOfferId(arg) {
	arg  = arg + prefix;
	
	if(hasOfferId(arg)) {
		return;
	}
	if(arg == "") {
		return;
	}
	value = getCookie(cookieName);
	if(value == null || value == "null" ) {
		value = arg ;
	} else {
	   value=value+","+ arg;
	}
	cookieValue = cookieName + "=" + value + ";path=/;";// + "domain="+domainName;
	document.cookie = cookieValue;
	//alert(cookieValue);
	//alert(document.cookie);
  }

  function delOfferId(arg) {
	arg = arg + prefix;

	if(!hasOfferId(arg)) {
	   alert("要删除的商机id不存在！");
	   return;
	}

	value = getCookie(cookieName);
	var offerIds = stringToArray(value);
	var len = offerIds.length;
	var result = "";
	var counter = 0;
	for(var i=0;i<len;i++) {
	  if(offerIds[i] != arg) {
			if(counter == 0) {
			  result = offerIds[i];
			} else {
			  result = result + "," + offerIds[i];
			}
			counter++;
	  }
	}
	cookieValue = cookieName + "=" + result + ";path=/;";// + "domain="+domainName;
	document.cookie = cookieValue;
  }
  
  
  function getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i=0;
	while(i<clen) {
		var j=i+alen;
		if(document.cookie.substring(i,j)==arg)
			return www_helpor_net(j);
		i=document.cookie.indexOf(" ",i)+1;if(i==0)
			break;
	}
	return null;
  }

  function www_helpor_net(offset) {
	  var endstr=document.cookie.indexOf(";",offset);
	  if(endstr==-1)
	  endstr=document.cookie.length;
	  return unescape(document.cookie.substring(offset,endstr));
  }

  function hasOfferId(arg) {
	var value = getCookie(cookieName);
	if(value == null || value == "null" || value.indexOf(arg) == -1 ) {
		return false;
	} else {
		return true;
	}
  }

  function getOfferIdCount() {

	value = getCookie(cookieName);
	
	if(value == null || value == 'null') {
	  return 0;
	}
	
	var count = 1;
	while(value.indexOf(",") != -1) {
	   var position = value.indexOf(",");
	   var len = value.length;
	   value =  value.substring(position+1,len);
	   count++;
	}
	return count;
  }
  
  function stringToArray(str) {
	   var temp = str;
	   var strArray = new Array();
	   var count = 0;
	   while(temp.indexOf(",") != -1) {

		 var position = temp.indexOf(",");
		 
		 strArray[count] = temp.substring(0,position);
		 //alert( strArray[count] );
		 count++;
		 var len = temp.length;
		 temp =  temp.substring(position+1,len);

	   }
	   strArray[count] = temp;
	   //alert(strArray[count]);
	   return strArray;
  }
  
  function clickcompareBox(id,box) {
	if(box.checked){
	  if(getOfferIdCount()>=20){
		alert('最多只能选在20条商机！');
		box.checked=false;
	  }else{
		addOfferId(id);
		var countId = box.id.substring("compareBox".length,box.id.length);
		//alert(countId);
		if(countId <= 0){
	        return;
	    }
		var trTag = document.getElementById("compareColor"+countId);
		trTag.style.background=bgColor;
	  }
	} else {
	  delOfferId(id);
	  var countId = box.id.substring("compareBox".length,box.id.length);
	  if(countId <= 0){
	    return;
	   }
	  var trTag = document.getElementById("compareColor"+countId);
	  if(countId%2!= 0){
		trTag.style.background="ffffff";
	  }else{
		trTag.style.background="EFEFEF";
	  }
	}
  }
//  function doCompare(){
//    if(getOfferIdCount()<=1){
//        alert('至少选择2条商机进行比较！');
//    }else{
//        window.open('http://applicationch.168ec.com/app_web/Contrast/index.aspx');
//    }
//  }
  
  function cleanCookie() {
  //var pcs=document.getElementById("pcs").value;
	var pcs=50;
	if(confirm("你确定要清空全部选择吗?")){
	  var value = "null";
	  cookieValue= cookieName + "=" + value + ";path=/;";// + "domain="+domainName;
	  document.cookie = cookieValue;
	  for(var i=1;i<=pcs;i++){
		var elem = document.getElementById("compareBox"+i);
		if(elem != null && elem.checked){
		elem.checked = false;
		 if(i <= 0){
	        return;
	      }
		  elem = document.getElementById("compareColor"+i);
		  if(i%2 != 0){
			elem.style.background="ffffff";
		  }else{
			elem.style.background="EFEFEF";
		  }
		}
	  }
	}
  }
  

  for(var i = 1;i < 31 ;i++){
	var cpbox = document.getElementById("compareBox"+i);
	var cpcolor = document.getElementById("compareColor"+i);
	if(cpbox!= null){
	  if(hasOfferId(cpbox.name)){
		  cpbox.checked = true;
		  if(cpcolor != null && i > 0){
			cpcolor.style.background= bgColor;
		  }
	  }else{
		  cpbox.checked = false;
	  }
	}
  }