function ExpandCollapse()
{
	if($("img.[@name=expand]").attr("name")=="expand")
	{
		 $("div.[@name=details]").show();
		 $("div.[@name=title]").hide();
 	     $("img.[@name=expand]").attr("src","images/2.gif");
		 $("img.[@name=expand]").attr("name","collapse");
	}
	else
	{
	 $("div.[@name=details]").hide();
	 $("div.[@name=title]").show();
 	 $("img.[@name=collapse]").attr("src","images/1.gif");
	 $("img.[@name=collapse]").attr("name","expand");
	}
}
function Expand()
{
	 $("div.[@name=details]").show();
	 $("div.[@name=title]").hide();
}
function Collapse()
{
	 $("div.[@name=details]").hide();
	 $("div.[@name=title]").show();
}

function ShowHide(id)
{
	$("#details" + id).toggle();
	$("#title" + id).toggle();
}

function CheckLen(input, length) {
  var capacity = input.value.length;
  if (capacity > length) {
    input.value=input.value.substring(0,length);
    ChooseLen(input);
    alert("Message is to long!!! \r It'll be miniaturize to 3000 chars!");
    return false;
  }
   ChooseLen(input);
  return true;
}

function ChooseLen(input) {
 var M = input.value.length;
 document.getElementById("size").innerText = M;
}

function OpenPopWin(id,type,width,height,obj)
{
    showPopWin('/emailtofriend.aspx?id='+id + '&t=' + type,width, height, obj);
}
function savecomment(dealid,t)
{
    if($.trim($("#comment").val())=="")
    {
        alert('Nothing has been submitted! Comment filed is empty!');
        return;
    }
    $.post("ajax.asmx/SaveComment",{ id: dealid, body: $("#comment").val(), type:t},
        function(xml)
        {
            document.getElementById('comment').value='';
            alert($("message", xml).text());
            $("#result").show();
            $("#result").html($("message", xml).text());
        }
   
   );
}
function Subscribe()
{
    re = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
    if($.trim($("#subemail").val())=="" || $.trim($("#subemail").val()).match(re)==null)
    {
        alert('Nothing has been submitted! Email is incorrect format!');
        return false;
    }
    $.post("ajax.asmx/Subscribe",{ email: $("#subemail").val()},
        function(xml)
        {
            $("#subresult").show();
            $("#subresult").html($("message", xml).text());
        }
   
   );
   return false;
}

function Submit()
{
	flag=true;
	error = false;
    re = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
    reg = /([^a-zA-Z_0-9])/g;
    if($.trim($("#email").val())=="" || $.trim($("#email").val()).match(re)==null)
    {
        alert('Nothing has been submitted! Email is incorrect format!');
        return false;
    }
      cat=false;
      $("input.[@name=catdeals]").each(function(){if($(this).is(":checked")){cat=true;}});
      if(!cat)
	      $("input.[@name=catcoupons]").each(function(){if($(this).is(":checked")){cat=true;}});

      if(cat==false)
      {
	      if($("#keys").val()=='')
	      {
			alert('Please select some paramert for subscribe newsletters on this form!');	      
		      return false;
	      }
	      else
	      {
		      $("input.[@name=keys]").each(
		      function()
		      {
				if($.trim($(this).val())!="")
				{
					if($(this).val().match(reg)==null)
						flag=true;
					else
						error =true;
						
				}
			  });
	      }
      }
      if(error==true)
      {
		alert('Invalid keywor format!');
	    return false;
      }
      else
	      return true;
}

function stat(dealid,tid)
{
    if(dealid!='' && tid!='')
        window.open('/stat.aspx?itemid='+dealid+'&tid=' + tid);
//    $.post("ajax.asmx/Stat",{ id: dealid},
//        function(xml)
//        {
//           window.location = $("url", xml).text();
//        }
//   
//   );
}
function Redirect(dealid,t)
{
    $.post("ajax.asmx/Stat",{ id: dealid,tid:t},
        function(xml)
        {
           if($("url", xml).text()!='')
                window.location = $("url", xml).text();
        }
   
   );
}
function expired(itemid, tid)
{
    $.post("ajax.asmx/Expired",{ id: itemid, t:tid},
        function(xml)
        {
            $("#divexpired").html($("string", xml).text());
        }
   );
}
function vote(dealid,type,f)
{
    $.post("ajax.asmx/Vote",{ id: dealid, tid:type, flag:f},
        function(xml)
        {
            //$("#rating"+dealid).find(".count").html($("count", xml).text());
            //$("#rating"+dealid).find(".btn").html($("message", xml).text());
            //setTimeout(function() {$("#rating"+dealid).parent().background("url(images/flame_static.gif)");},1000);
            $("#rating"+dealid).html($("count", xml).text());
            alert($("message", xml).text());
        }
   );
}

function memberitems(mid,page,typeitem)
{
    if($.cacheCheck(mid+"-"+page+"-"+typeitem))
    {
         document.getElementById('memberdeals').innerHTML = $.cacheGet(mid+"-"+page+"-"+typeitem);
         document.getElementById('paging').innerHTML = $.cacheGet(mid+"-"+page+"-"+typeitem+"paging");
         return;
    }
    outhtml="";
    paging="";
    $.post("ajax.asmx/memberitems",{ mid: mid, p: page,t: typeitem},
        function(xml)
        {
           _template="dealdetails";
           if(typeitem!=1)
                _template="coupondetails";
           items = $("item", xml);
           count = $("count", xml).text();
           outhtml+="<table width=\"100%\"><tr><td colspan=\"2\"><div style=\"border-top:1px solid #5e82a7;\"></div></td></tr>";
           if(items.length==0)
           {
               if(typeitem==1)
                   outhtml+="<tr><td align=\"center\" style=\"padding-top:20px;padding-bottom:20px;\"><b>This member hasn't got any deals!!!</b></td></tr>";
               else
                    outhtml+="<tr><td align=\"center\" style=\"padding-top:20px;padding-bottom:20px;\"><b>This member hasn't got any coupons!!!</b></td></tr>";
           }
           else{
               for(i=0;i<items.length;i++)
               {
                   title = $("title", items[i]).text();
                   date = $("dt", items[i]).text();
                   uid = $("uid", items[i]).text();
                   if((i%2)==0){
    		           outhtml+="<tr><td style=\"background-color:#eff0f2; padding:10px; width:20px;\"><b>"+date+"</b></td>";
			           outhtml+="<td style=\"background-color:#eff0f2; padding:10px;\"  onmouseover=\"this.style.background='#bcc1cc';this.style.cursor='pointer';\" onmouseout=\"this.style.background='#eff0f2';\" onclick=\"location='"+_template+".aspx?id="+uid+"'\"><a href=\""+_template+".aspx?id="+uid+"\">"+title+"</a></td>";
			           outhtml+="</tr>";
			       }
			       else{
    		           outhtml+="<tr><td style=\"padding:10px;\"><b>"+date+"</b></td>";
			           outhtml+="<td style=\"padding:10px;\" onmouseover=\"this.style.background='#bcc1cc';this.style.cursor='pointer';\" onmouseout=\"this.style.background='#ffffff';\" onclick=\"location='"+_template+".aspx?id="+uid+"'\"><a href=\""+_template+".aspx?id="+uid+"\">"+title+"</a></td>";
			           outhtml+="</tr>";
			       }
               }
           }
           outhtml+="</table>";
           max = parseInt(count/20)
           if(count%20>0)
               max++;
           if(max>1){
                paging+="<table style=\"width:100%;\" align=\"center\"><tr><td style=\"width:100%;\"  align=\"center\"><table><tr><td style=\"vertical-align:middle;\"><b>Pages:</b></td>"
                for(p=1;p<=max;p++){
                    if(p==page)
                        paging+="<td><a class=\"pages7\">"+p+"</a></td>";
                     else
                        paging+="<td><a href='#flag' alt=\"Page 2\" class=\"pages5\" onclick=\"memberitems("+mid+","+p+","+typeitem+")\">"+p+"</a></td>";
               }
               paging+="</tr></table></td></tr></table>";
          }
          //alert(mid+"-"+page+"-"+typeitem);
          $.cachePut(mid+"-"+page+"-"+typeitem,outhtml);
          $.cachePut(mid+"-"+page+"-"+typeitem + "paging",paging);
          document.getElementById('memberdeals').innerHTML = outhtml;
          document.getElementById('paging').innerHTML = paging;
        }
   );
}

function ShowCategory(catid,flag)
{
    if ($("#SubCat"+catid).css("display") == "none")
    {
        $("#SubCat"+catid).show();
        if(flag==1)
        {
            $("#Cat"+catid).attr("class","menu55");
        }
        else
            $("#Cat"+catid).attr("class","menu5");
        $("#a" + catid).addClass('menuactive');
        $("#img" + catid).attr('src','images/minus.gif');
    }
    else
    {
        $("#SubCat"+catid).hide();
        if(flag==1)
            $("#Cat"+catid).attr("class","menu33");
        else
            $("#Cat"+catid).attr("class","menu3");
        $("#a" + catid).removeClass('menuactive');
        $("#img" + catid).attr('src','images/plus.gif');
    }
}
var oInterval;
function SendLetterMembers(text)
{
if(!confirm('Are you sure send letter!'))
        return false;
     _memb='';
    $("input.[@type=checkbox]").each(function(){
      if ( $(this).is(":checked") )
      {
	      if($(this).val()!='on')
		      _memb+=$(this).val() + ',';
       }
    });
     $.post("/ajax.asmx/SendLetterMembers",{ body:text, members:_memb},
            function(xml)
            {
               // window.clearInterval(oInterval);
               // document.getElementById("progress").innerHTML = '<img height=\"5\" src="/images/progress.gif" width="100%" id="imgResult" />';

               //alert($("string", xml).text());
            }
       );
       oInterval = window.setInterval("GetProgress()", 2000); 
}

function GetProgress()
{
     $.get("/ajax.asmx/GetProgress", function(xml){
         document.getElementById("percent").innerHTML = '<img height=\"5\" src="/images/progress.gif" width="'+$("string", xml).text() + "%"+'" id="imgResult" />';
         if($("string", xml).text()==100)
         {
              document.getElementById("btnSDC").disabled=false;
              window.clearInterval(oInterval);
              alert('Dispatch has completed successful!');
         }
     });
}

var Interval;
function SendLetterSubscribers(text)
{
    if(!confirm('Are you sure send letter!'))
        return false;
    _subs='';
    $("input.[@type=checkbox]").each(function(){
      if ( $(this).is(":checked") )
          _subs+=$(this).val() + ',';
    });
    //alert(_subs);
     $.post("/ajax.asmx/SendLetterSubscribers",{ body:text, subscribers:_subs},
            function(xml)
            {
                //window.clearInterval(Interval);
                //document.getElementById("progress").innerHTML = '<img height=\"5\" src="/images/progress.gif" width="100%" id="imgResult" />';

               //alert($("string", xml).text());
            }
       );
       Interval = window.setInterval("GetProgress1()", 2000); 
       
}

function GetProgress1()
{
     $.get("/ajax.asmx/GetProgress1", function(xml){
         document.getElementById("percent").innerHTML = '<img height=\"5\" src="/images/progress.gif" width="'+$("string", xml).text() + "%"+'" id="imgResult" />';
         if($("string", xml).text()==100)
         {
              document.getElementById("btnSDC").disabled=false;
              window.clearInterval(Interval);
              alert('Dispatch has completed successful!');
         }
     });
}

var iInterval;
function SendDealsCoupons()
{
    document.getElementById("btnSDC").disabled=true;
     $.get("/ajax.asmx/SendDealsCoupons",
            function(xml)
            {
                //document.getElementById("percent").innerHTML = '<img height=\"5\" src="/images/progress.gif" width="100%" id="imgResult" />';
               //alert($("string", xml).text());
            }
       );
       iInterval = window.setInterval("GetProgress2()", 1000); 
}

function GetProgress2()
{
     $.get("/ajax.asmx/GetProgress2", function(xml){
          document.getElementById("percent").innerHTML = '<img height=\"5\" src="/images/progress.gif" width="'+$("string", xml).text() + "%"+'"/>';
         if($("string", xml).text()==100)
         {
             //alert($("string", xml).text());
              //document.getElementById("btnSDC").disabled=true;
              window.clearInterval(iInterval);
              alert('Dispatch has completed successful!');
         }
     });
}




