function getQueryString(str)
{
    try
    {
        if (str.length > 0)
        {
            var indexOfQuestionmark = str.indexOf('?');
            if (indexOfQuestionmark >= 0)
            {
                return str.substr(indexOfQuestionmark+1, str.length-1);
            }
        }
    }
    catch(e)
    {
        RealNetworks.Debug.fPrint('Error at getQueryString:\n'+e);
    }
    return '';
}

function getQueryStringDictionary(strQuery)
{
    var dQueryStringDictionary = {};
    var aQueryString = strQuery.split('&');
    for (var i = 0; i < aQueryString.length; i++)
    {
        var str = aQueryString[i];
        var iIndexOfEquals = str.indexOf('=');
        if (iIndexOfEquals > 0)
        {
            dQueryStringDictionary[str.substr(0,iIndexOfEquals)] = str.substr(iIndexOfEquals+1, str.length-1);
        }
        else if (str.length > 0)
        {
            dQueryStringDictionary[str] = '';
        }
    }
    return dQueryStringDictionary;
}

function getMatchPercentage(strUrl1, strUrl2)
{
    if (strUrl1 == strUrl2)
        return [100, true];
    
    var strQueryString1 = getQueryString(strUrl1);
    var strQueryString2 = getQueryString(strUrl2);
    var iPercentage = 0;
    if (strQueryString1 && strQueryString1.length > 0 && strQueryString2 && strQueryString2.length > 0)
    {
        try
        {
            var bURLInQueryString = false;
            var dDict1 = getQueryStringDictionary(strQueryString1);
            var dDict2 = getQueryStringDictionary(strQueryString2);

            var iTotalMatches = 0;
            var iDict1Length = 0;
            for (var property in dDict1)
            {
                iDict1Length++;
                if (dDict1[property] == dDict2[property])
                {
                    iTotalMatches++;
                }
                
                if (!bURLInQueryString && (unescape(dDict1[property]) == unescape(strUrl2)))
                {
                    bURLInQueryString = true;
                    break;
                }
            }

            var iDict2Length = 0;
            for (var property in dDict2)
            {
                iDict2Length++;
                
                if (!bURLInQueryString && (unescape(dDict2[property]) == unescape(strUrl1)))
                {
                    bURLInQueryString = true;
                    break;
                }
            }

            iPercentage = bURLInQueryString ? 100 : (iTotalMatches * 100) / Math.max(iDict1Length, iDict2Length);
        }
        catch (e)
        {
            RealNetworks.Debug.fPrint("getMatchPercentage error:\n"+e);
        }
    }
    return [iPercentage, false];
}


