﻿	//--------------------------------------------------------
	//功能：在指定的节点下查找指定标签类型和ID的节点
	//参数：div_id -- 外层DIV ID
	//      target_tagame -- 要找的元素的TAGNAME
	//      target_id -- 要找元素的ID
	//返回：目标节点，如果没找到返回null
	//---------------------------------------------------------
	function _getElementById(div_id, target_tagame, target_id){
    var div_node = document.getElementById(div_id);
		var ret_node = null;
		function travel(node, target_tagame, target_id){
			if(node == null || target_tagame == null || target_id == null)
		    return null;
		  
		  for(var i=0; i<node.childNodes.length; i++){
		    var child = node.childNodes[i];
		    if(child.tagName == null || child.tagName == "undefined")
		      continue;
		    var tagname = child.tagName.toLowerCase();
		    target_tagame = target_tagame.toLowerCase();
		    if(target_tagame == tagname && target_id == child.id){
		      ret_node = child;
		      break;
		    }		 
		    travel(child, target_tagame, target_id);
		  }
		}		
		travel(div_node, target_tagame, target_id);
		return ret_node;
	}

	/*
	* 功能：向cookies中添加商品ID
	* 参数：cookieName cookies的名字
	*      productid   商品ID
	*/
	function addFavorites(cookieName,productid){
		var cookieValue = productid;
	    var cookieString = document.cookie;
        var start = cookieString.indexOf(cookieName + '=');
        if (start == -1){ // 找不到这个cookie，那么新增一个
             document.cookie = cookieName+'=' + escape(cookieValue)+';path=/';
        }else{//找到了那就提取出来
            start += cookieName.length + 1;
            var end = cookieString.indexOf(';', start);
            var cookieContentWrited = '';
            if (end == -1){  //cookieName所对应的项已经在cookie字符串的最后了
                cookieContentWrited = unescape(cookieString.substring(start));
            }else{    //cookieName所对应的项不在cookie字符串的最后
                cookieContentWrited = unescape(cookieString.substring(start, end));
            }

            if(cookieContentWrited.indexOf(cookieValue)==-1){
                //如果用户是选中并且用户选择的内容没有被保存过,那么把记录保存
               	if(cookieContentWrited=="")
				{
                document.cookie = cookieName+'=' + escape(cookieValue)+';path=/';
				}else
				{
					document.cookie = cookieName+'=' + escape(cookieContentWrited+'_'+cookieValue)+';path=/';
				}
            }
            /*if(cookieContentWrited.indexOf(cookieValue)!=-1 && _getElementById(compId,"input",idCheckbox).checked==false){
                //如果checked==false但是用户选择的内容已经被保存过,那么把记录保存移出

                //如果在cookie里只有一个ID了，并且这个ID还要被删除，那么也一并删除cookie的名称
                if(cookieContentWrited==cookieValue){
                    var exp = new Date();
                    exp.setTime (exp.getTime() - 1);
                    document.cookie = cookieName + "=" +  "; expires="+ exp.toGMTString()+';path=/';
					return;
                }else if(cookieContentWrited.indexOf(cookieValue)!=0){//如果这个cookie不排在第一位
                    cookieContentWrited = cookieContentWrited.replace(':'+cookieValue,'');
                }else{//如果这个cookie排在第一位，直接去掉cookieValue+':'
                     cookieContentWrited = cookieContentWrited.replace(cookieValue+':','');
                }
                document.cookie = cookieName+'=' + escape(cookieContentWrited)+';path=/';
            }*/

        }
	}
	
