﻿// JScript File
var SavedFunction = "";
var oldcount = "0";
var chatloading = false;


function startchattimer() {
//    var manager = Sys.WebForms.PageRequestManager.getInstance();
//    manager.add_endRequest(endRequest);
    getCounts();
}
function endRequest(sender, args) {
    var Error = args.get_error();
    //document.getElementById("divError").innerHTML = Error.message;
    args.set_errorHandled(true);
}  

function hide(o)   {
   o.style.display="none";
   o.style.visibility="hidden"; 
}
function RenderContent (o)    {
GEID("bodycontent").innerHTML=o;
if (RequestedPage=="login")
{
    GEID("txtLoginUsername").value=readCookie("un");
}
}
function doresetpassword()    {
    var em=GEID("txtForgottenEmail").value;
   GEID("btnReset").disabled=true; 
   Zedheads.ResetPassword(em,PWReset); 
}
function PWReset(o) {
    GEID("btnReset").disabled=false;
    alert(o);
}
function gosearchheader()   {
    var s=GEID("txtSearchHeader").value;
    if (s=="")
        alert("Please enter a search term");
    else
        document.location.href="search.aspx?term="+s;
}
function newshout() {
    showhide("ShoutControl");
}
function _hide(o)    {
    o.style.visibility="hidden";
    o.style.display="none";
    
}
function _show(o)    {
    o.style.visibility="visible";
    o.style.display="block";
    
}
function gonewshout() {
    var s=GEID("txtShout").value;
    if (s!="" && s!=null)
    {
        GEID("btnShout").disabled=true;
        Zedheads.ShoutOut(s,Shouted);
    }
}
function Shouted(o) {
    GEID("btnShout").disabled=false;
    if (o!="OK")
        alert(o);
    else    {
        getCounts();
        GEID("txtShout").value="";
    }
}
function addfavourite(LinkID)   {
    Zedheads.AddFavourite(LinkID,FavAdded);
}
function FavAdded(o)   {
    alert(o);
}
function GEID(id) {
    return document.getElementById(id);
}
function sendtoafriend(LinkID)    {
   if (!LoggedIn()) return;
   var addr=prompt("Enter your friends email address and we'll send them a link.\nWe don't record any email addresses you enter.","");
   if (addr!=null)   {
    Zedheads.SendLink(addr,LinkID,LinkSent);
   } 
}
function LinkSent(o) {
    alert(o);
}
function LoggedIn()  {
    if (_LoggedIn)
       return true;
   else
   { 
        go('login');  
   } 
}

function doregister()
{
   var un=GEID("txtRegisterUsername").value;
   var em= GEID("txtRegisterEmail").value;
   createCookie("un",un,30);
   GEID("btnRegister").disabled=true;
   Zedheads.CreateAccount(un,em,CreateResult);
}

function dologin()
{
   var un=GEID("txtLoginUsername").value;
   var pwd= GEID("txtLoginPassword").value;
   if (GEID("chkRememberLogin").checked) {
       createCookie("un",un,30);
       createCookie("pwd",pwd,30); 
   }
   else
   {
    eraseCookie("un");
    eraseCookie("pwd"); 
   }
   GEID("btnLogin").disabled=true;
   Zedheads.Login(un,pwd,LoginResult);
}
function CreateResult(o)   {
   GEID("btnRegister").disabled=false;
   if (o[0]!="OK")
    alert(o[0]);
   else
   {
       go('login'); 
       alert(o[1]);
   } 
}
function LoginResult(o) {
    GEID("btnLogin").disabled=false;
    if (o[0]!="OK")
        alert(o[0]); 
   else
   {
        _LoggedIn=true;
       GEID("WelcomeText").innerHTML=o[1]; 
       if (o[2]=="false")
        reload(); 
       else
       {
        go('editprofile');
        alert("Welcome to Zedheads!\nPlease take some time to create a profile.");     
       } 
   }  
}
function getCounts() {
    try {
        Zedheads.getCounts(ShowChatCount, callMethod_Failure);
    }
    catch(e) {}
}

function ShowChatCount(o) {
    try {
        if (oldcount != o[0] && !chatloading) {
            try { GetLatestChat(oldcount); } catch (e) { }
            oldcount = o[0];
        }
        if (GEID('commentsboard') != null && oldcommentcount.toString() != o[2]) {
            try { GetLatestComments(); } catch (e) { }
            oldcommentcount = o[2];
        }
        GEID("chatcounter").innerHTML = o[0];
        GEID("shouts").innerHTML = o[1];
        window.setTimeout("getCounts()", 3000);
    }
    catch (e) { }
}
function callMethod_Failure(errors, userContext, methodName) {
   // alert(errors.get_Message());
}
function GetLatestComments() {
    Zedheads.GetLatestComments(RenderLatestComments);
}
function RenderLatestComments(o) {
    oldcommentcount = parseInt(o[0]);
    if (GEID('commentsboard') != null) {
        GEID('commentsboard').innerHTML = o[1];
    }
}
var RequestedPage="";
function gocat(id)    {
    document.location.href="showlinks.aspx?LinkType=7&catid="+id;
}
function gonotice(noticeid)    {
    Zedheads.GoNotice(noticeid,GotNoticeURL);
}
function GotNoticeURL(o)  {
    if (o.toLowerCase().substring(0,4)=="http")
        document.location.href=o;
    else
        eval(o); 
}
function addtofavourites() 
{ 
if (document.all)
    window.external.AddFavorite(location.href, document.title);
else if (window.sidebar)
    window.sidebar.addPanel(document.title, location.href, "")
}  
function go(page,query){
    if (query==null) {query="";} else { query="?"+query;}
    switch(page)   { 
       case "editprofile":
            if (!LoggedIn()) return;
            document.location.href=page+".aspx"+query;  
       break;  
       case "register":
       case "login":
       case "categories":
       case "forgottenpassword":
       case "karma":
       case "addnotice":
        document.location.href=page+".aspx";  
       break;  
       case "logout":
        Zedheads.Logout(LoggedOut);
       break;
       case "editprofile":
       case "chatboard":
       case "messages":
       case "message":
       case "suggest":
            if (!LoggedIn()) return;
            document.location.href=page+".aspx"+query;  
       break; 
       case "showlinks":
           if (query=="?LinkType=1" || query=="?LinkType=3" || query=="?LinkType=4")
           {
             if (!LoggedIn()) return;
           }  
           document.location.href=page+".aspx"+query;  
       break;
       default:
            document.location.href=page+".aspx"+query; 
       break; 
       } 
}
function LoggedOut(o)  {
    _LoggedIn=false;
    eraseCookie("pwd");
    document.location.href="default.aspx?r="+Math.random();
}
function dohide(event,o) {
    var newEl=event.relatedTarget||event.toElement;
   var ischild=null; 
    try { 
        ischild=newEl.getAttribute("ischild");
    }
   catch (e) {}
    if (ischild!=null) return true;
    hide(o);
}
function reload() {
    document.forms[0].submit();
}
function showhide(id,t)   {
   var o=GEID(id);
   if (o.style.visibility=="visible" || o.style.visibility=="" ||o.style.visibility==null) {
    o.style.display="none";
    o.style.visibility="hidden";
    if (t!=null) 
        t.style.backgroundPosition="-20px";
    createCookie(id,0,30);
   }
   else
   {
    o.style.display="block";
    o.style.visibility="visible";
    if (t!=null) 
        t.style.backgroundPosition="0px";
    createCookie(id,1,30); 
   }
}
function createCookie(name,value,days)
{
    if (days)
    {
	    var date = new Date();
	    date.setTime(date.getTime()+(days*24*60*60*1000));
	    var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function eraseCookie(name)
{
    createCookie(name,"",-1);
}

if (typeof(Sys) !== 'undefined') {
    Sys.Application.add_load(startchattimer);
    Sys.Application.notifyScriptLoaded();
} 