﻿// JScript File

//Create Cookie

function saveCalc(section, costofwater, settings, total)
{
    //Set current section
        createCookie('currentSection',section,365);
	//Set cost of water
        createCookie('costOfWater',costofwater,365);
    //Set display total
        createCookie('displayTotal',total,365);
    //Save calculator settings
        createCookie(section+'_settings', settings,365);
    //Update Tab display
		showTotal('textLocation');
		
		//return section + " -=- " + settings + " -=- " + total;
		
		//alert(readCookie('currentSection'));
		//alert(section + " -=- " + costofwater + " -=- " + settings + " -=- " + total);
		//return section + " -=- " + settings + " -=- " + total;
		//return readCookie('currentSection');
}

function setSectionNum(sectionNum)
{
	//Set current section number + name (ie: 0Household)
       createCookie('currentSectionNum',sectionNum,365);
	   return sectionNum;
}

function getCurrentSectionNum()
{
    if (document.cookie)
    {
	    if (readCookie('currentSectionNum')==null)
	    {
	        return '0';
	    }
	    else
	    {
	        return readCookie('currentSectionNum');
	    }
	}
    else
    {
    	return '0';
    }

}

function getCurrentSettings(section)
{
    if (document.cookie)
    {
        if (readCookie(section+'_settings')==null)
        {
            return 'no cookie';   
        }
        else
        {
            return readCookie(section+'_settings');
        }
    }
    else
    {
        return '0';
    }

}

function getCurrentSection()
{
    if (document.cookie)
    {
	    if (readCookie('currentSection')==null)
	    {
	        return '0';
	    }
	    else
	    {
	        return readCookie('currentSection');
	    }
	}
    else
    {
    	return '0';
    }

}


function getCostOfWater()
{
    if (document.cookie)
    {
	    if (readCookie('costOfWater')=='6.06')
	    {
	        return '6.06';
	    }
	    else
	    {
	        return readCookie('costOfWater');
	    }
	}
    else
    {
    	return '6.06';
    }

}


function showTotal(textLocation)
    {
        if (document.getElementById(textLocation))
        {
            if (document.cookie)
            {
            
            if (readCookie('displayTotal')==null)
            {
            	document.getElementById(textLocation).innerHTML='$0';
            }
            else
            {
            	document.getElementById(textLocation).innerHTML='$'+readCookie('displayTotal');
            }
            }
            else
            {
                document.getElementById(textLocation).innerHTML='$0';
            }
         }
    }



function createCookie(name, value, days)
{
    if (days)
    {
        var date=new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires="; expires="+date.toLocaleString();
    }
    else
    {
        var expires="";
    }
    document.cookie=name+"="+value+expires+"; path=/";
	//alert(name + " -=- " + value + " -=- " + days);
	//alert(readCookie(name));
	//alert("Cookie " + document.cookie + " was made!");
}

//Read Cookie
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 readCookie(name)
{
    var ca=document.cookie.split(";");
    var nameEQ=name+"=";
    
    if (ca.length==0)
    {
        return null;    
    }
    else
    {
    
   		for (var i=0;i<ca.length;i++)
        {
            var c=ca[i];
            while (c.charAt(0)=='') c=c.substring(1,c.length);
            if (c.indexOf(nameEQ)==0) return c.substring(nameEQ.length, c.length);
        }
		//return null;       
	}
}
*/

//Erase Cookie

function eraseCookie(name)
    {
        createCookie(name,"",-1);
    }
