function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function setRootCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        "; path=/" +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

// The return value is a encoded string containing Vista cookies for 
// the given integration.
function getSCFromIC(integration) {
    return getCookie(integration);
}

// Caches vista integration cookies (encoded string).  
function addSCToIC(integration, scookies) {

    //if ( !isIntegrationCached(integration)) {
        setRootCookie(integration, scookies, false, false, false, false);   
    //}
}

function isIntegrationCached(integration) {
    value = getCookie(integration);
    if (null == value || value.length < 1) {
        return false;
    }
    return true;
}

function setActiveIntegration(integration) {    
    setRootCookie("active_integration", integration, false, false, false, false);   
}

function isActiveIntegration(integration) {
	check = getCookie("active_integration")
	if (null == check || check.length < 1 || check != integration) {
	    return false;
	}
	return true;
}

// deserializes the encoded cookie string and calls setIntegrationCookie
function setSCookies(scookies) {
    if (scookies != null) {
        splitcookies = scookies.split(",");
        for (var i=0;i<splitcookies.length;i++) {
            var scookie = splitcookies[i];
            splitcookie = scookie.split(";");
            var name = splitcookie[0];
            var val = splitcookie[1];
            var domain = splitcookie[2];
            var path = splitcookie[3];
            //  alert("name: " +name +"  value: " +val +"  domain: " +domain +"  path: " +path);
            setIntegrationCookie(name, val, false, domain, path, false);
        }
    } 

}

// Sets a cookie required by the integration application so a user
// can access integration in an integrated Community System frameset.
// The value of the cookie should not be escaped.
function setIntegrationCookie(name, value, expires, domain, path, secure)
{
    document.cookie= name + "=" + value +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        "; path=" + path +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");    
}

// Returns a cookie required by the integration application so a user
// can access integration in an integrated Community System frameset.
// The value of the cookie should not be escaped.
function getIntegrationCookie(name, value)
{
    var dc = document.cookie;
    var prefix = name + "=" + value;
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return dc.substring(begin + prefix.length, end);
}

// Display a popup window
function openBox(url,title,width,height,retval)  {

    var features="'"+'toolbar=no, scrollbars=yes, status=yes, resizable=yes';
    if (width != undefined && width != '' && height != undefined && height != '') {
        features += ', height='+ height+', width='+ width;
    }
    features += "'";

    newWindow=window.open(url,title,features);
    if(newWindow != null){
	    newWindow.focus();
	    if (retval != undefined && retval == "ret")
	        return newWindow;
     }
}
