﻿//*************************************************************************
//    This Foundation Classes version 1.0 is written by Stanley Tso (bug report: cleverstan@gmail.com)
//    Foundation.js contains the foundation functions and classes:
//    Classes:
//      - Stack
//      - Queue (support iteration)
//      - Page (support iteration)
//    
//*************************************************************************

function Container(Type)
{
    if (Type == null)
    {
        this.FIFO = true;
    }
    else
    {
        this.FIFO = Type;
    }
    this.Data = new Array();
    this.Iterator = 0;
}

Container.prototype.Push = function(value)    /* Define Method */
{
    this.Data[this.Data.length] = value;
}

Container.prototype.Next = function()    /* Define Method */
{
    if (this.Iterator == this.Data.length)
        this.Iterator = 0;
    var val = this.Data[this.Iterator];
    this.Iterator++;
    return val
}

Container.prototype.Pop = function()    /* Define Method */
{
    var value = null;
    if (this.FIFO)
    {/* queue */
        if (this.Data.length > 0)
        {
            value = this.Data[0];
            this.Data[0] = null;
            for(var i = 0; i < (this.Data.length-1); i++)
            {
                this.Data[i] = this.Data[i+1];
                this.Data[i+1] = null;
            }
        }
    }
    else
    {/* stack */
        if (this.Data.length > 0)
        {
            value = this.Data[this.Data.length-1];
            this.Data[this.Data.length-1] = null;
        }
    }
    
    if (this.Data.length > 0)
        this.Data.length = this.Data.length - 1;

    return value;
}

Container.prototype.Length = function()
{
    return this.Data.length;
}

function Stack()
{
    return new Container(false);
}

function Queue()
{
    return new Container();
}

//Page object for page event managements
/************************************************
    //This class will automatically bound to DOM's body.onload & body.unload
    Class Page
    {
        //Methods:
        void AddOnResize(string script);
        bool HasOnResize();
        string GetOnResize();
        void AddOnload(string script);
        bool HasOnload();
        string GetOnload();
        void AddUnload(string script);
        bool HasUnload();
        string GetUnload();
        int Width();    //current open browser width
        int Height();
        bool Status();      //set windows status bar message
        void SwitchLanguage(language)
    }
*************************************************/
function Page()
{
    this.onloadList = new Queue;
    this.unloadList = new Queue;
    this.onResizeList = new Queue;
}

Page.prototype.AddOnResize = function(Script)
{
    return this.onResizeList.Push(Script);
}

Page.prototype.HasOnResize = function()
{
    var value = this.onResizeList.Length() != this.onResizeList.Iterator;
    if (!value)
        this.onResizeList.Iterator = 0;
    return value;
}

Page.prototype.GetOnResize = function()
{
    return this.onResizeList.Next();
}

Page.prototype.AddOnload = function(Script)
{
    return this.onloadList.Push(Script);
}

Page.prototype.HasOnload = function()
{
    return this.onloadList.Length() > 0;
}

Page.prototype.GetOnload = function()
{
    return this.onloadList.Pop();
}

Page.prototype.AddUnload = function(Script)
{
    return this.unloadList.Push(Script);
}

Page.prototype.HasUnload = function()
{
    return this.unloadList.Length() > 0;
}

Page.prototype.GetUnload = function()
{
    return this.unloadList.Pop();
}

Page.prototype.Width = function() {
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) 
    {
        myWidth = window.innerWidth;
    }
    else if( document.documentElement && document.documentElement.clientWidth ) 
    {
        myWidth = document.documentElement.clientWidth;
    }
    else if( document.body && document.body.clientWidth) 
    {
        myWidth = document.body.clientWidth;
    }
    return myWidth;
}

Page.prototype.Height = function() {
    var myHeight = 0;
    if( typeof( window.innerHeight ) == 'number' )
    {
        myHeight = window.innerHeight;
    }
    else if( document.documentElement && document.documentElement.clientHeight )
    {
        myHeight = document.documentElement.clientHeight;
    }
    else if( document.body && document.body.clientHeight )
    {
        myHeight = document.body.clientHeight;
    }
    return myHeight;
}

Page.prototype.Status = function (text)
{
    try
    {
        if (text == null)
        {
            text = '';
        }
        window.status = text;
    }
    catch (e)
    {
        return false;
    }
    return true;
}

Page.prototype.SwitchLanguage = function (lang)
{
    var url = window.location + '';
    var re1 = new RegExp("&?lang=[a-z][a-z]", "g");
    var re2 = new RegExp("&?lang=[a-z][a-z]-[A-Z][A-Z]", "g");
    url = url.replace(re2, "");
    url = url.replace(re1, "");
    window.location = (url.indexOf('?')>0)?url+((url.indexOf('?')==url.length-1)?'':'&')+'lang='+lang:url+'?lang='+lang;
}

var page = new Page;

function PageInit()
{
    while (page.HasOnload())
    {
        try
        {
            eval(page.GetOnload());
        } catch(e)
        {
            //alert(e); //silence problematic scripts
        }
    }
}

function PageDispose()
{
    while (page.HasUnload())
    {
        try
        {
            eval(page.GetUnload());
        } catch(e)
        {
            //alert(e); //silence problematic scripts
        }
    }
}

function PageResize()
{
    while (page.HasOnResize())
    {
        try
        {
            eval(page.GetOnResize());
        } catch(e)
        {
            //alert(e); //silence problematic scripts
        }
    }
}

function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}

window.onload = PageInit;
window.unload = PageDispose;
window.onresize = PageResize;

Common = {
    fixMinHeight : function () {
        if($('ContentWrap').getHeight() < 400) { $('ContentWrap').setStyle({'height' : '400px'}); }
    }
}

Event.observe(window, 'load', Common.fixMinHeight );