﻿function checkisNaN(doc)
{
     var value = doc.value;
     var v = /^[\d]+$/;
     for(var i=0;i<value.length;i++)
         {
            if(!v.test(value.charAt(i)))
            {
                 value =  value.replace(value.charAt(i),"");
                 i--;
            }
     } 
     doc.value = value;
        
}

function checkisNaNOrNeg(doc)
{
     var value = doc.value;
     var v = /^[-|\d][\d]*$/;
     for(var i=0;i<value.length;i++)
     {
            if(!v.test(doc.value))
            {
                 doc.value =  doc.value.substring(0,doc.value.length-1); 
            }
     }
}
        
function Wconfirm(msg)
{
    return window.confirm(""+msg+"\r\n\r\n\t[是]或[否]");
} 

function Favorites(id,type)
{
    var v = type=="ecoupon"?"优惠券 ":"商家";
    if(Wconfirm("是否收藏此"+v))
    {
        var url = "../../HttpHandler/Favoriten.ashx";
        var data = "ID="+id+"&type="+type;
    
        var info= $.ajax(
		{	
			mode : "abort",
			type : "post", 
			url : url, 
			data: data,
			dataType : "json",
	        async: false
  
		}).responseText;
	    if(info=="0")
	    {
	        $("#ShowLogin").click();
	    }
	    else if(info=="1")
	    {
	        alert("此"+v+"已经收藏");
	    }
	    else if(info=="2")
	    {
	        alert("收藏成功");
	    }
	    else
	    {
	        alert("收藏失败");
	    }
	}
	
}

function DeleteFavorites(id,type,title)
{
    var v = type=="ecoupon"?"优惠券 ":"商家";
    if(Wconfirm("是否删除"+v+"["+title+"]"))
    {
        var url = "../../HttpHandler/DeleteFavoriten.ashx";
        var data = "ID="+id;
    
        var info= $.ajax(
		{	
			mode : "abort",
			type : "post", 
			url : url, 
			data: data,
			dataType : "json",
	        async: false
  
		}).responseText;
	    if(info=="1")
	    {
	        alert(v+"["+title+"]删除成功");
	        var search = type=="ecoupon"?"?fav=0":"?fav=1";
	        document.location.href = location.protocol+"//"+location.host+location.pathname+search;
	    }
	    else
	    {
	        alert("删除失败");
	    }
	}
	
}



function StrLeng(id,leng)
{
    var dom = $("#"+id);
    var left = SL_getLeft(dom[0])+dom.width()+20;
    var top = SL_getTop(dom[0])+dom.height()-10;
    
    $(document.body).append("<div id=\"strLeng"+id+"\"  style=\"position:absolute;top:"+top+"px; left:"+left+"px;\"><strong id=\"strongColor"+id+"\" style=\"color:Fuchsia\">"+getBytesLength(dom.val())+"</strong><strong style=\"color:Gray\">/"+leng+"</strong></div>");
       
    dom.keyup(function(){
        $("#strongColor"+id).text(getBytesLength(this.value));
        if(getBytesLength(this.value)>leng)
        {
            $("#strongColor"+id).attr("style","color:Red");
        }
        else
        {
            $("#strongColor"+id).attr("style","color:Fuchsia");
        }
    });
}

function SL_getLeft(obj) {
    var left = obj.offsetLeft;
    var parent = obj.offsetParent;

    while (parent) {
        left += parent.offsetLeft;
        parent = parent.offsetParent;
    }

    return left
}

function SL_getTop(obj) {
	var top = obj.offsetTop;
	var parent = obj.offsetParent;
	
	while(parent) {
		top += parent.offsetTop;
		parent = parent.offsetParent;
	}
	
	return top;
}

function getBytesLength(str) {
		// 在GBK编码里，除了ASCII字符，其它都占两个字符宽
		//return str.replace(/[^\x00-\xff]/g, 'xx').length;
		return str.length;
	}


function LoadScroll()
        {
            var l=document.getElementById("page_left").scrollHeight;
            var m=document.getElementById("page_right").scrollHeight;
            layoutHeight=Math.max(l,m);
            layoutHeight = layoutHeight==l?layoutHeight+100:layoutHeight;
            document.getElementById("page_left").style.height=layoutHeight+"px";
            document.getElementById("page_right").style.height=layoutHeight+"px";
            var b =  window.screen.width;
            b = b-20;
            document.getElementById("page_main").style.width = b +"px";
            document.getElementById("pageheader").style.width = b +"px";
            document.getElementById("page_footer").style.width = b +"px";
        }
