// find the value of the given key in the passed string with the passed
// delimiter string. Key/value pairs are assumed to be "<key>=<value><delim>" with
// no spaces between the key, the "=" and the value.

function getValue(k,source,delim)
{
        var key = k + "=";
        var keyLen = key.length;
        var sourceLen = source.length;

        var keyIndex = source.indexOf(key,0);

        if(keyIndex == -1)
                return null;  // the key was not found

        var beginIndex = keyIndex + keyLen; // step past the key

        // get the index of the end of the value.  It is denoted
        // by the pattern delim.  If it is not found, then we have
        // gone to the end of the string.
        var endIndex = source.indexOf(delim,beginIndex);

        if(endIndex == -1)
                endIndex = sourceLen;

        return unescape(source.substring(beginIndex,endIndex));
}



function getQuery(queryName)
{
        return getValue(queryName,location.search,",");
}



function getCookie(name)
{
        return getValue(name,document.cookie,";");
}



function setCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}



// put the zip into the URL and into the cookie.

function saveit(zip) {
        setCookie("weather",zip,"Thu, 23-Nov-15 00:00:01 GMT","/",null,null);
}
