﻿function write_cookie(cookie_name,cookie_value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie=cookie_name + "=" + escape(cookie_value) + ((expiredays==null) ? "" : ";expires=" + exdate.toGMTString())
}
function get_cookie(cookie_name)
{
    // cookies are separated by semicolons
    var i=0;
    var a1=document.cookie.split("; ");
    for (i=0; i < a1.length; i++)
    {
        // a name/value pair (a crumb) is separated by an equal sign
        var a2=a1[i].split("=");
        if (cookie_name==a2[0]) 
            return unescape(a2[1]);
    }
    return null;
}
function delete_cookie(cookie_name)
{
    var cookie_date=new Date();
    cookie_date.setTime (cookie_date.getTime() - 1);
    document.cookie=cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function change_cookie(chk,cookie_name)
{
    var c1='';
    var c2='';
    var iExist=false;
    var val=chk.id;
    c2=get_cookie(cookie_name);
    if(c2==null || c2==cookie_name){
        c2='';
    }
    if (chk.checked)
    {
        if (c2!=null){
            var i=0;
            var a1s=c2.split('|');
            for(i=0; i<a1s.length; i++){
                if(a1s[i]==val)
                {
                    iExist=true;
                    break;
                }
            }
            if(!iExist )
                c1=c2 + '|' + val.toString();
            else
                c1=c2;
        }
        else
        {
            c1=val.toString();
        }
       chk.style.backgroundColor='red';
    }
    else
    {
        c1=c2.replace(val,'');
        if (c1 != '')
        {
            var s;
            c1=c1.replace('||','|');
            s=c1.charAt(0);
            if (s=='|')
                c1=c1.substring(1,c1.length);
            s=c1.charAt(c1.length-1);
            if (s=='|')
                c1=c1.substring(0,c1.length-1);
        }
        chk.style.backgroundColor='';
    }
    
    write_cookie(cookie_name,c1,365);
}
function clearAll(cookie_name)
{
    var c1='';
    c1=get_cookie(cookie_name);
    if(c1==cookie_name || c1=="undefined"){
        c1='';
    }
    if (c1!=null && c1!=''){
        var a1=c1.split('|');
        var i=0;
        for(i=0; i<a1.length; i++){
            var chk=document.getElementById(a1[i]);
            if (chk != null)
            {
                chk.checked=false;
                chk.style.backgroundColor='';
                c1=c1.replace(a1[i],'');
                if (c1 != '')
                {
                    var s;
                    c1=c1.replace('||','|');
                    s=c1.charAt(0);
                    if (s=='|')
                        c1=c1.substring(1,c1.length);
                    s=c1.charAt(c1.length-1);
                    if (s=='|')
                        c1=c1.substring(0,c1.length-1);
                }
            }
        }
    }
    write_cookie(cookie_name,c1,365);
}
function sleep(naptime)
{
    naptime=naptime * 1000;
    var sleeping=true;
    var now=new Date();
    var startingMSeconds=now.getTime();
    var alarm;

    while(sleeping){
        alarm=new Date();
        alarmMSeconds=alarm.getTime();
        if(alarmMSeconds - startingMSeconds > naptime)
        { 
            sleeping=false; 
        }
    } 
}

function addCommas(nStr)
{
	nStr += '';
	x=nStr.split('.');
	x1=x[0];
	x2=x.length > 1 ? '.' + x[1] : '';
	var rgx=/(\d+)(\d{3})/;
	while (rgx.test(x1))
	{
		x1=x1.replace(rgx,'$1' + ',' + '$2');					
	}
	return x1 + x2;
}

function CheckTime(h1,m1,h2,m2)
{
    var d1=new Date();
    d1.setHours(h1,m1);
    var d2=new Date();
    d2.setHours(h2,m2);
    var d=new Date();
    if ((d>=d1) && (d2>=d))
        return true;
    else
        return false;
}
function CheckChangeData(result)
{
    try
    {
        if ((typeof(result) == "undefined") || (result == null) || (result == "") || (result == "undefined"))
            return "";
        var LastValue = "";
        var divLastValue = document.getElementById("LastValue");
        if ((typeof(divLastValue) != "undefined") && (divLastValue != null) && (divLastValue != "undefined"))
        {
            LastValue = divLastValue.innerText;
            divLastValue.innerText = result;
        }
        if ((typeof(LastValue) == "undefined") || (LastValue == null) || (LastValue == "") || (LastValue == "undefined"))
            LastValue = result;
        else
        {
            var NoChange = "";
            var divNoChange = document.getElementById("NoChange");
            if ((typeof(divNoChange) != "undefined") && (divNoChange != null) && (divNoChange != "undefined"))
            {
                NoChange = divNoChange.innerText;
                if (LastValue == result)
                {
                    if (NoChange == "0")
                    {
                        divNoChange.innerText = "1";
                        return "";
                    }
                    else if (NoChange == "1")
                    {
                        divNoChange.innerText = "2";
                    }
                    else
                        return "";
                }
                else
                    divNoChange.innerText = "0";                
            }
        }
    }
    catch(e)
    { 
        return "";
    }
    return LastValue;
}
function Diff(x1,x2)
{
    try
    {
        if (x1==null || x1=="")
            return "";
        if (x2==null || x2=="")
            return (parseFloat(x1)).toFixed(1);
        return (parseFloat(x1)-parseFloat(x2)).toFixed(1);
    }
    catch(e)
    {}
    return "";
}
function FormatNum(x)
{
    try
    {
        if (x==null || x=="" || x=="undefined")
            return "";
        if (x=="ATO" || x=="ATC")
            return x;
        return (parseFloat(x)).toFixed(1);
    }
    catch(e)
    {}
    return "";
}

