// Example c++ versions of these functions (plus a few other things) are in http://shadow2531.com/cpp/mailto_funcs1.zip
// Example python versions of these functions are in http://shadow2531.com/py/mailto_funcs.py
// Example Java versions of these functions are in http://shadow2531.com/java/MailtoFuncs.java
// Example Ruby versions of these functions are in http://shadow2531.com/ruby/mailto_funcs.rb

// Create a modified value that only contains a &-separated list of hname=hvalue pairs (so it can be tokenized)
function prepURI(uri) {
    if (uri.indexOf("mailto:?") == 0) {
        return uri.substr(8);
    }
    if (uri.indexOf("mailto:") == 0) {
        var prep = uri;
        var q = prep.indexOf('?');
        if (q != -1) {
            prep = prep.replace(/\?/,"&");
        }
        return "to=" + prep.substr(7);
    }
    return "";
}

// skip_method
// 0 = concat (with delim) all hvalues for the hname (even if they're empty)
// 1 = concat (with delim) all non-empty hvalues for the hname (to, cc and bcc rule)
// 2 = concat (with delim) first non-empty hvalue for the hname and all (even if empty) hvalues after that. (body rule)
// 3 = only use the last hvalue for the hname (even if the hvalue is empty) (subject rule)
function searchMailtoURI(uri, hname, delim, skip_method) {
    var puri = prepURI(uri);
    var lkey = hname.toLowerCase();
    var hlist = puri.split('&');
    var value = "";
    var zskip = true;
    for (var i = 0; i < hlist.length; ++i) {
        var eq = hlist[i].indexOf('=');
        if (eq == -1 || hlist[i] == "" || hlist[i].substring(0, eq).toLowerCase() != lkey) {
            continue;
        }
        if (skip_method != 3) {
            if (value) {
                if (skip_method == 1 && hlist[i].substr(eq + 1) == "") {
                    continue;
                }
                value += delim;
            } else if (skip_method == 0) {
                if (!zskip) {
                    value += delim;
                }
                zskip = false;
            }
            value += hlist[i].substr(eq + 1);
        } else {
            value = hlist[i].substr(eq + 1);
        }
    }
    return value;
}

function getEncodedTOValue(uri) {
    return searchMailtoURI(uri, "to", "%2C%20", 1);
}
function getDecodedTOValue(uri) {
    return decodeURIComponent(getEncodedTOValue(uri));
}

function getEncodedSubjectValue(uri) {
    return searchMailtoURI(uri, "subject", "", 3);
}
function getDecodedSubjectValue(uri) {
    return decodeURIComponent(getEncodedSubjectValue(uri));
}

function getEncodedBodyValue(uri) {
    return searchMailtoURI(uri, "body", "%0D%0A", 2);
}
function getDecodedBodyValue(uri) {
    return decodeURIComponent(getEncodedBodyValue(uri));
}

function getEncodedCCValue(uri) {
    return searchMailtoURI(uri, "cc", "%2C%20", 1);
}
function getDecodedCCValue(uri) {
    return decodeURIComponent(getEncodedCCValue(uri));
}

function getEncodedBCCValue(uri) {
    return searchMailtoURI(uri, "bcc", "%2C%20", 1);
}
function getDecodedBCCValue(uri) {
    return decodeURIComponent(getEncodedBCCValue(uri));
}

function getNormalizedMailtoURI(uri) {
    var temp = "mailto:";
    temp += getEncodedTOValue(uri);
    temp += "?subject=";
    temp += getEncodedSubjectValue(uri);
    temp += "&body=";
    temp += getEncodedBodyValue(uri);
    temp += "&cc=";
    temp += getEncodedCCValue(uri);
    temp += "&bcc=";
    temp += getEncodedBCCValue(uri);
    return temp;
}

