﻿function createXMLHttpRequest() {
   if (window.XMLHttpRequest) {//Mozilla
  xmlHttp = new XMLHttpRequest();
   }
  else if (window.ActiveXObject) {//IE
  try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");  
  } catch (e) {
  try {
  xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
  } catch (e) {}
  }
  }
}

function changeLink(){
          var Link=document.links;
          var len=Link.length;
          var hrefstr;
              for(var i=0;i<len;i++){
                     hrefstr= Link[i].href;
                  if(hrefstr.indexOf("favid=")!=-1){
                      Link[i].href="javascript:addfav("+hrefstr.split("favid=")[1]+")";
                  }
                  else if(hrefstr.indexOf("favsid=")!=-1){
                      Link[i].href="javascript:addfav_2("+hrefstr.split("favsid=")[1]+")";
                  }
              }$("a.thickbox").unbind("click");
    }
function changeLink_2(){
          var Link=document.links;
          var len=Link.length;
          var hrefstr;
              for(var i=0;i<len;i++){
                  hrefstr= Link[i].href;
                  if(hrefstr.indexOf("addfav(")!=-1){
                    Link[i].href="#TB_inline?height=290&width=340&inlineId=popup_login&favid="+hrefstr.replace(/\D/g,"");
                  }
                  else if(hrefstr.indexOf("addfav_2(")!=-1){
                    Link[i].href="#TB_inline?height=290&width=340&inlineId=popup_login&favsid="+hrefstr.replace(/\D/g,"");
                  }
              }
              TB_init();
    }
function serverLogin(email,password)
{
    var title;
	xmlHttp.open("post","ajax/Login.ashx?email="+email+"&password="+password+"&fresh="+Math.random(),true);
	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState==4)
		{
				    if(xmlHttp.status==200)
				    {
				        if(xmlHttp.responseText=="ok")
				        {
				           if(document.getElementById('funType').value==1){
				               addfav(document.getElementById('funId').value,true);
				               title = "voucher";
				           }
				           if(document.getElementById('funType').value==2){
				               addfav_2(document.getElementById('funId').value,true);
				               title = "shop";
				           }
				            $("#TB_ajaxContent").each(function(){
				               $("div.popups_tellfriends_main", this).html("<div>This discount "+title+",sale news,deal news has been added to my favourites</div><br><div class=\"popup_login_btn_2\" ><a href=\"favourate.aspx\"> Go to my favourites </a><a href=\"javascript:void(0);\" onclick=\"TB_remove();\" >Continue browsing</a></div><div class=\"clear\"></div>");})
				           
				           $("div.loginBar").html("<ul><li><a style='background:url();' > Welcome,"+email+"!</a></li><li><a href='javascript:logout();' ><span> Logout </span></a></li></ul>");
				           addFavourateNav();
				           changeLink();
				        }else{
				                var hrefStr="#TB_inline?height=290&width=340&inlineId=popup_login&"
				                if(document.getElementById('funType').value==1)
				                {
				                    hrefStr +="favid="+document.getElementById('funId').value;
				                }
				                else
				                {
				                    hrefStr +="favsid="+document.getElementById('funId').value;
				                }
				                $("#TB_ajaxContent").each(function(){ $("div.popups_tellfriends_main", this).html("<div style=\"color:#ffa009;\"> Your email address or password error! </div><br><div class=\"popup_login_btn_2\" ><a  class=\"thickbox\" onclick=\"TB_show(null,'"+hrefStr+"',false);\" style='cursor:pointer' > Back to Previous </a><a href=\"javascript:void(0);\" onclick=\"TB_remove();\" >Continue browsing</a></div><div class=\"clear\"></div>");})
				               }
				    }
				    else
				    {
				        alert('The request failed.');
				    }
		}
	}
	xmlHttp.send(null);	
}
function logout(){
	xmlHttp.open("post","/logout.aspx?fresh="+Math.random(),true);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
		    if(xmlHttp.status==200){
		        if(window.location.href.toLowerCase().indexOf("favourate.aspx")!=-1){return window.location.href="/";}
		    $("div.loginBar").html("<ul>"+xmlHttp.responseText+"</ul>");
  		    var nn=document.getElementById("NavButtons").lastChild;
            if(nn.nodeType!=1)nn=nn.previousSibling;
		    removeElement(nn);		    
		    changeLink_2();
		    }
            else{alert('The request failed.');}
		}
	}
	xmlHttp.send(null);	
}
function addfav(id,bool){
    var sendvalue=id;
    if(sendvalue==''){return;} 
	xmlHttp.open("post","addfavourate.aspx?nid="+ escape(sendvalue),true);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
		    if(xmlHttp.status==200){
		    if(!bool)alert(xmlHttp.responseText);
		    }
            else{alert('The request failed.');}
		}
	}
	xmlHttp.send(null);	
}
function addfav_2(id,bool){
   var sendvalue=id;
   if(sendvalue==''){return;}
   	xmlHttp.open("post","addfavourate.aspx?sid="+ escape(sendvalue),true);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
		    if(xmlHttp.status==200){
		        if(!bool)alert(xmlHttp.responseText);
		        }
            else{alert('The request failed.');}
		}
	}
	xmlHttp.send(null);
}
function addFavourateNav(){
   var li=document.createElement("li");
   li.id="ctl00_navlink_NavFavourate";
   li.className="nav2";
   li.innerHTML="<a href=\"favourate.aspx\">Favourate</a>";
   document.getElementById("NavButtons").appendChild(li);
}     
function  removeElement(el){
      var   p   =   el.parentNode;   
      p.removeChild(el);   
}     
function getParam(id){
    var queryString = $('#'+id).attr('data');
    var items = queryString.split("&");
    var text="";
    for(key in items){
        var d = items[key];
        if(text.length!=0){
	        text += ","+d.split('=')[0]+":"+"'"+d.split('=')[1]+"'";
        }else{
	        text = d.split('=')[0]+":"+"'"+d.split('=')[1]+"'";
        }
    }
    var objs=null;
    eval("objs={" + text + "};");
    return objs;
}
createXMLHttpRequest();