resolvePetitionPath = function(){
    // this function serves as a redirecter for petitions
    // if it finds a referring URL such as 'takeaction/123456789'
    // it will redirect to 'takeaction/123/456/789/'
    // if that location is not found, it will redirect to '/create.html'
    var docLocHref = document.location.href;
    if(docLocHref.indexOf("takeaction/")!=-1){
        var petitionPath = docLocHref.slice((docLocHref.indexOf("takeaction/"))+11);
        var slashCount=0,pathArray = new Array(),pathChunk=0,newPath='';
        for(var s=0;petitionPath.charAt(s);s++){ if(petitionPath.charAt(s)=='/') { slashCount++; } }
        if(slashCount>1) { location.href = 'http://'+stage+'www.care2.com/petitions/create-online-petition'; }
        else{
            var petitionPath_noSlash = petitionPath.replace('/','');
            if(isNum(petitionPath_noSlash)==true){
                for(var g=0;petitionPath_noSlash.charAt(g);g+=3){
                    pathArray[pathChunk]=petitionPath_noSlash.substr(g,3); pathChunk++;
                    if(petitionPath_noSlash.charAt(g)) { pathArray[pathChunk]='/'; pathChunk++; }
                }
                for(var n=0;pathArray[n];n++){ newPath+=pathArray[n]; }
            }
            location.href = '/takeaction/'+newPath;
        }
    }
}
function isNum(sText) {
    var ValidChars="0123456789",IsNumber=true,Char;
    for (i = 0; i < sText.length && IsNumber == true; i++) { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1) { IsNumber = false; }
    }
    return IsNumber;
}
window.onload = resolvePetitionPath;