﻿function Create( )
{
    new Ajax.Request("/Content.aspx",
   {
      asynchronous: true,
      method: 'post',
      //parameters: {getCode: "body"},
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error.html";
            return;
         }
         var div = $("body");
         div.innerHTML = myResponse;
         
         PositionContent( );
         GetNav( );
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error.html";
      }
   });
}
function GetRelationships( )
{
    new Ajax.Request("/Relationships.aspx",
   {
      asynchronous: true,
      method: 'post',
      //parameters: {getCode: "body"},
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error.html";
            return;
         }
         var div = $("relationships");
         div.innerHTML = myResponse;
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error.html";
      }
   });
}
function GetNav( )
{
    var strUrl = location.href;
    new Ajax.Request("/Nav.aspx",
   {
      asynchronous: true,
      method: 'post',
      parameters: {url: strUrl},
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error.html";
            return;
         }
         var div = $("rightcontent");
         div.innerHTML = myResponse;
         PositionNav( );
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error.html";
      }
   });
}
function GetNews( )
{
    var strUrl = location.href;
    new Ajax.Request("/News.aspx",
   {
      asynchronous: true,
      method: 'post',
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error.html";
            return;
         }
         var div = $("news");
         div.innerHTML = myResponse;
         PositionNav( );
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error.html";
      }
   });
}
function GetTopArticles(iNum,area)
{
    new Ajax.Request("/Articles.aspx",
   {
      asynchronous: true,
      method: 'post',
      parameters: {intNum: iNum},
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error.html";
            return;
         }
         var div = $(area);
         div.innerHTML = myResponse;
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error.html";
      }
   });
}
function GoToCategoryArticles(iNum,link)
{
    var now = new Date( );
    fixDate(now);
    now.setTime(now.getTime( ) + 1000 * 60 * 10); //10 minute time-out
        
    var img = link.getElementsByTagName("img")[0];    
    var text = img.alt;
        
    setCookie("currentCategory",iNum,now,"/");
    setCookie("catText",text,now,"/");
    
    document.location = "http://www.next-plateau.com/articles/index.html";
}
function GetCategoryArticles(iNum,link)
{
    var str = "";
    if(link.innerHTML == null || link.innerHTML == "")
    {
        str = link;
    }
    else
    {
        var raw = link.innerHTML;
        str = escape(raw);
        str = str.replace("%3Cbr%3E","").replace("%3Cbr%20%3E","");
    }
    
    new Ajax.Request("/Articles.aspx",
   {
      asynchronous: true,
      method: 'post',
      parameters: {categoryID: iNum, intNum: 6,strCat: str},
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error1.html";
            return;
         }
         var div = $("articles");
         if(div == null)
         {
            div = $("leftcontent");
         }
         div.innerHTML = myResponse;
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error2.html";
      }
   });
}
function GoToArticle(iNum)
{
    //if you're not on the articles page, set the cookie then go there
    if(document.location.toString( ).indexOf("articles") < 0)
    {
        var now = new Date( );
        fixDate(now);
        now.setTime(now.getTime( ) + 1000 * 60 * 10); //10 minute time-out
        
        setCookie("currentArticle",iNum,now,"/");
        
        document.location = "http://www.next-plateau.com/articles/index.html";
    }
    if(CheckCookieJar( ))
    {
       new Ajax.Request("/Articles.aspx",
       {
          asynchronous: true,
          method: 'post',
          parameters: {articleID: iNum},
          onSuccess: function(ajaxObject)
          {
             myResponse = ajaxObject.responseText;
             if(myResponse == null)
             {
                //alert("Fail!");
                document.location = "http://www.next-plateau.com/error.html";
                return;
             }
             var div = $("articles");
             div.innerHTML = myResponse;
          },
          onFailure: function()
          {
             //alert("This is also failure");
             document.location = "http://www.next-plateau.com/error.html";
          }
       });
    }
    else
    {
        GetLoginForm(iNum);
    }
}
function GetRegisterForm(iNum)
{
    new Ajax.Request("/Register.aspx",
   {
      asynchronous: true,
      method: 'post',
      parameters: {articleID: iNum, page: "htmlRegister"},
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error.html";
            return;
         }
         var div = $("leftcontent");
         div.innerHTML = myResponse;
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error.html";
      }
   });
}
function GetLoginForm(iNum)
{
    new Ajax.Request("/Register.aspx",
   {
      asynchronous: true,
      method: 'post',
      parameters: {articleID: iNum, page: "htmlLogin"},
      onSuccess: function(ajaxObject)
      {
         myResponse = ajaxObject.responseText;
         if(myResponse == null)
         {
            //alert("Fail!");
            document.location = "http://www.next-plateau.com/error.html";
            return;
         }
         var div = $("leftcontent");
         div.innerHTML = myResponse;
      },
      onFailure: function()
      {
         //alert("This is also failure");
         document.location = "http://www.next-plateau.com/error.html";
      }
   });
}
function Validate( )
{
    GiveArticleCookies( )
}
function GiveArticleCookies( )
{
    var now = new Date( );
    fixDate(now);
    now.setTime(now.getTime( ) + 1000 * 60 * 10); //10 minute time-out
    
    var input = document.getElementById("articleNum");
    var iNum = parseInt(input.value);
    
    setCookie("currentArticle",iNum,now,"/");
}
function CheckArticleCookie( )
{    
    var article = getCookie("currentArticle");
    var category = getCookie("currentCategory");
    if(article != null)
    {
        var iNum = parseInt(article);
        deleteCookie("currentArticle", "/", "");
        GoToArticle(iNum);
    }
    else if(category != null)
    {
        var iNum = parseInt(category);
        var sCat = getCookie("catText");
        
        deleteCookie("currentCategory", "/", "");
        deleteCookie("catText", "/", "");
        GetCategoryArticles(iNum,sCat)
    }
    else
    {
        GetTopArticles(6,'articles');
    }
}
function IsSubmitted( )
{
    var submit = getCookie("submitted");
    if(submit != null)
    {
        deleteCookie("submitted", "/", "");
        var message = document.getElementById("message");
        if(message != null)
        {
            message.style.display = "block";
        }
    }
}
function CheckCookieJar( )
{
    var login = getCookie("userID");
    if(login == null)
    {
        return false;
    }
    
    var now = new Date( );
    fixDate(now);
    now.setTime(now.getTime( ) + 1000 * 60 * 60 * 24 * 90); //90 days - approximately 3 months
    
    setCookie("userID",login,now,"/");
    
    return true;
}
function PositionNav( )
{
    var wrapper = document.getElementById("navwrapper");
    var nav = document.getElementById("mainnav");
    nav.parentNode.removeChild(nav);
    wrapper.appendChild(nav);
}
function PositionContent( )
{
    var leftcontent = document.getElementById("leftcontent");
    var leftwrapper = document.getElementById("leftcontentwrapper");
    leftcontent.parentNode.removeChild(leftcontent);
    leftwrapper.appendChild(leftcontent);
}

function Hover(element)
{
    var item = element;
    if(element.nodeName.toLowerCase() == "a")
    {
        item = element.getElementsByTagName("img")[0];
    }
    var src = item.src;
    if(src.indexOf("/hover/") > 0)
    {
        item.src = src.replace("/hover/","/");
    }
    else if(item.className != "selected")
    {
        item.src = src.replace("images/","images/hover/");
    }
}
