﻿// IO
function DataIO()
{
    var request = false;
    try
    {
        request = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (err2) 
    {
        try 
        {
            request = new ActiveXObject('Microsoft.XMLHTTP');
        }
        catch (err3)
        {
            try
            {
                request = new XMLHttpRequest();
            }
            catch (err1) 
            {
                request = false;
            }
        }
    }
    return request;
}

function setCookie(name,value,expires) {
	expires = expires * 60*60*24*1000;
    var cookieString = name + "=" +encodeURIComponent(value) + ((expires) ? ";expires=" + new Date(new Date().getTime() + (expires)).toGMTString() : "");
    document.cookie = cookieString+"; path=/";
}

function getCookie(name)
{ 
    var dc=document.cookie;
    var start = dc.indexOf(name+"="); 
    var len = start+name.length+1; 
    if ((!start) && (name != dc.substring(0,name.length))) return null; 
    if (start == -1) return null; 
    var end = dc.indexOf(";",len); 
    if (end == -1) end = dc.length; 
    return decodeURIComponent(dc.substring(len,end).replace(/\+/g,  " ")); 
}

// Elements
function posX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

function posY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
}

function autoHeight()
{
    return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
}

// Prototypes for Strings
String.prototype.as = function(format,n)
{
    switch(format.toString().toLowerCase())
    {
        case "double":
            var val=this.toString().replace(",",".");
            if(val[0]=='.') val="0"+val;
            if(val.indexOf(".")==-1)
                return val+".00";
            var arg=((n==null)?2:n);
            //if(parseFloat(val)- parseInt(val,10)>0)
            {
                val=val.substr(0,val.indexOf("."))+"."+val.substr(val.indexOf(".")+1).padRight(arg,'0');
            }
            //else val=val+".".padRight(arg+1,'0');
            return val;
        break;
        case "date":
            
        break;
    }
}

String.prototype.padLeft = function(n, pad)
{
    t='';
    if(n>this.toString().length)
        for(pl=0;pl<n-this.toString().length;pl++)
            t+=pad;
    return t+this.toString();
}

String.prototype.padRight = function(n, pad)
{
    t=this.toString();
    if(n>this.toString().length)
        for(pr=0;pr<n-this.toString().length;pr++)
            t+=pad;
    return t;
}

String.prototype.Trim = function()
{
    return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
}

String.prototype.endsWith = function(str)
{
    return (this.match(str+"$")==str);
}


// Regex
function noSpecialChars(e)
{
    var charCode = String.fromCharCode(e.keyCode? e.keyCode : e.charCode);
    return !(/[|!\'^+%&/()=?_\"*£#$½{\[\]}\-.<>]/.test(charCode));
}

function onlyNumbers(e)
{
    if(((e.keyCode)? e.keyCode : e.charCode)!=8)
    {
        var charCode = String.fromCharCode(e.keyCode? e.keyCode : e.charCode);
        return (/[0-9]/.test(charCode));
    }
}
