﻿var myid = "0";
var lasttarget = "0";
var readyforlong = false;
var firstclick = true;
var country = 0;
var updatetimer = null;
var aftertimer = null;
var longwaittimer = null;
var timeouttimer = null;
var ajaxtimer = null;
var lastcalltype = 1;
var CacheMAX = 20;
var timeouts = 0;

var cachecounter = 0;
var querycache = new Array();
var countrycache = new Array();
var resultscache = new Array();


// called after a certain wait period
function StartUpdate() {

    var tb = document.getElementById("TextBox1");
    var pattern = trim(tb.value);

    // first check the cache
    for (i = 0; i < CacheMAX; i++) {
        if (querycache[i] == pattern && countrycache[i] == country && resultscache[i] != "") {
            var div = document.getElementById("FoundUsers");
            div.innerHTML = resultscache[i];

            SetMessageDivMore(1);
            
            HideLongWait();
            HideTimeout();

            return;
        }
    }

    // kick off a search timer
    if (updatetimer != null) clearTimeout(updatetimer);
    updatetimer = setTimeout("StartSearch()", 750);

    StartLongWait();
    StartTimeout();

    // if not in cache then make a call
    if (pattern.length > 0 && pattern != "firstname lastname") {

        cachecounter++;
        if (cachecounter == CacheMAX) cachecounter = 0;
        querycache[cachecounter] = pattern;
        countrycache[cachecounter] = country;
        resultscache[cachecounter] = "";

        ret = NameFetch.FetchData(pattern, country, 1, cachecounter, OnComplete, IncrementTimeout, IncrementTimeout);
    }
    else {
        var div = document.getElementById("FoundUsers");
        div.innerHTML = "";

        HideLongWait();
        HideTimeout();

        HideMessageDiv();
    }
    lastcalltype = 1;

}



// the three outcomes from the call
function OnComplete(results) {
    var divs = document.getElementById("SideFeature");
    divs.className = "Visible";
    
    var callnum = parseInt(results.substring(0, 2));
    var clean = results.substring(2, results.length);

    // always overwrite the cache
    resultscache[callnum] = clean;
    timeouts = 0;

    // check that it's the most recent query
    if (cachecounter == callnum) {
        HideLongWait();
        HideTimeout();

        var div = document.getElementById("FoundUsers");
        div.innerHTML = clean;

        // show the long list button
        SetMessageDivMore(1);

        // delay the retrieval of long list by a few seconds
        if (aftertimer != null) clearTimeout(aftertimer);
        aftertimer = setTimeout("TimerLongReady()", 10000);
        readyforlong = false;
    }
    else {


    }
}

function OnLongComplete(results) {

    var callnum = parseInt(results.substring(0, 2));
    var clean = results.substring(2, results.length);

    // always overwrite the cache
    resultscache[callnum] = clean;
    timeouts = 0;

    // check that it's the most recent query
    if (cachecounter == callnum) {
        HideLongWait();
        HideTimeout();

        var div = document.getElementById("FoundUsers");
        div.innerHTML = clean;

        // show the long list button
        SetMessageDivMore(2);

        // delay the retrieval of long list by a few seconds
        if (aftertimer != null) clearTimeout(aftertimer);
        aftertimer = setTimeout("TimerLongReady()", 8000);
        readyforlong = false;
    }
}



//////////////

function TimerLongReady() {
    if (readyforlong == true) StartLongList();
    else readyforlong = true;
}
function UserLongReady() {
    if (readyforlong == true) StartLongList();
    else readyforlong = true;

    var href5 = document.getElementById("LongListLink");
    href5.innerHTML = "";

    SetMessage("Fetching more names, this might take a few seconds...");

    StartSearch2();

    return false;
}

function StartLongList() {
    
    readyforlong = false;
    var tb = document.getElementById("TextBox1");
    var pattern = trim(tb.value);

    StartLongWait();
    StartTimeout();


    if (pattern.length > 0 && pattern != "firstname lastname") {
        ret = NameFetch.FetchData(pattern, country, 2, cachecounter, OnLongComplete, IncrementTimeout, IncrementTimeout);
    }
    lastcalltype = 2;
}




// search events
function StartSearch() {
    var tb = document.getElementById("TextBox1");
    if (tb.value.length > 0 && tb.value != "firstname lastname") ret = NameFetch.StartSearch(tb.value, lastcalltype, cachecounter, OnSearchComplete, OnSearchNothing, OnSearchNothing);
}
function StartSearch2() {
    var tb = document.getElementById("TextBox1");
    if (tb.value.length > 0 && tb.value != "firstname lastname") ret = NameFetch.StartSearch(tb.value, 2, cachecounter, OnSearchComplete, OnSearchNothing, OnSearchNothing);
}

function OnSearchComplete(results) {

    var callnum = parseInt(results.substring(0, 2));
    var clean = results.substring(2, results.length);
    
    // no searching, so can do long list straight away
    if (results.length <= 2 && callnum == cachecounter) {
        //alert(results);
        if (aftertimer != null) clearTimeout(aftertimer);
        if (readyforlong == true) StartLongList();
        else readyforlong = true;
        return;
    }

    // invalidate the cache & set follow up timer
    for (i = 0; i < CacheMAX; i++) {
        if (querycache[i] == clean) {
            querycache[i] = "zPzIzXaIaEa";
            resultscache[i] = "";
        }
    }

}
function OnSearchNothing() {

}




// feedback messages to the user

function StartLongWait() {
    if (longwaittimer != null) clearTimeout(longwaittimer);
    longwaittimer = setTimeout("ShowLongWait()", 1000);
}

function ShowLongWait() {
    var wait = document.getElementById("PleaseWait");
    wait.className = "Visible";

    var timeout = document.getElementById("TimeoutMsg");
    timeout.className = "Hidden";
}

function HideLongWait() {
    if (longwaittimer != null) clearTimeout(longwaittimer);
    var wait = document.getElementById("PleaseWait");
    wait.className = "Hidden";
}

function IncrementTimeout() {
    timeouts++;
    if (timeouts > 5) ShowTimeout();
}

function StartTimeout() {
    if (timeouttimer != null) clearTimeout(timeouttimer);
    timeouttimer = setTimeout("ShowTimeout()", 15000);

}

function ShowTimeout() {
    timeouts == 0;
    var wait = document.getElementById("PleaseWait");
    wait.className = "Hidden";

    var timeout = document.getElementById("TimeoutMsg");
    timeout.className = "Visible";

    HideMessageDiv();
}

function HideTimeout() {

    if (timeouttimer != null) clearTimeout(timeouttimer);
    var timeout = document.getElementById("TimeoutMsg");
    timeout.className = "Hidden";
}

function HideMessageDiv() {
    var msg = document.getElementById("MessageDiv");
    msg.className = "Hidden";

    if (ajaxtimer != null) clearTimeout(ajaxtimer);
}

function SetMessageDivMore(type) {
    var href5 = document.getElementById("MessageDiv");
    href5.className = "Visible";


    var href5 = document.getElementById("LongListLink");
    href5.innerHTML = "Click here to refresh this list...";

    if (type == 1) {
        SetMessage("Can't find the person you're looking for? Well, we're searching behind the scenes for them right now...");

        var img = document.getElementById("AjaxImg");
        img.className = "VisibleEl";

        if (ajaxtimer != null) clearTimeout(ajaxtimer);
        ajaxtimer = setTimeout("HideAjax()", 20000);
    }
    else {
        SetMessage("If the person you're looking for is not appearing above then they may not be on twitter, they may be spelling their name differently, or we may just not have found them yet. We also limit our results to 50 of the best matches.");

        HideAjax();
    }
}

function HideAjax() {
    var img = document.getElementById("AjaxImg");
    img.className = "HiddenEl";

    if (ajaxtimer != null) clearTimeout(ajaxtimer);
}

function SetMessage(msg) {
    var span4 = document.getElementById("MessageLabel");
    span4.innerHTML = msg;
}

///
function CheckId(id) {
    ret = NameFetch.CheckId(id, OnIdComplete, CheckIdTimeout, CheckIdTimeout);

    return true;
}
function OnIdComplete(result) {
    //var div = document.getElementById("FoundUsers");
    //div.innerHTML = result + div.innerHTML;
}
function CheckIdTimeout() {

}

/////////////////

function clearDefault(el) {
    if (el.defaultValue == el.value) {
        el.value = ""
        el.className = "TextBoxStyle";
    }
}
function setDefault(el) {
    if (el.value == "") {
        el.value = el.defaultValue
        el.className = "TextBoxStyleWatermark";
    }
}

function DoCountry() {
    var div = document.getElementById("ChooseCountry");
    div.className = "Visible";

    var div2 = document.getElementById("CountrySelect");
    div2.value = country;

    return false;
}

function DoNoThanks() {
    var div = document.getElementById("ChooseCountry");
    div.className = "Hidden";

    return false;
}
function OnCountrySelect() {
    var div = document.getElementById("ChooseCountry");
    div.className = "Hidden";

    var div2 = document.getElementById("CountrySelect");
    country = div2.value;

    var span3 = document.getElementById("CountryLabel");

    if (country == 1) span3.innerHTML = "Global";
    if (country == 2) span3.innerHTML = "United States";
    if (country == 3) span3.innerHTML = "United Kingdom";

    if (country == 4) span3.innerHTML = "India";
    if (country == 5) span3.innerHTML = "Canada";
    if (country == 6) span3.innerHTML = "Germany";
    if (country == 7) span3.innerHTML = "France";
    if (country == 8) span3.innerHTML = "Spain";
    if (country == 9) span3.innerHTML = "Netherlands";
    if (country == 10) span3.innerHTML = "Japan";
    if (country == 11) span3.innerHTML = "Brazil";
    if (country == 12) span3.innerHTML = "Australia";
    if (country == 13) span3.innerHTML = "Indonesia";
    if (country == 14) span3.innerHTML = "Italy";
    if (country == 15) span3.innerHTML = "South Africa";
    if (country == 16) span3.innerHTML = "Pakistan";
    if (country == 17) span3.innerHTML = "Turkey";
    if (country == 18) span3.innerHTML = "Russia";
    if (country == 19) span3.innerHTML = "Mexico";
    if (country == 20) span3.innerHTML = "China";
    if (country == 21) span3.innerHTML = "Austria";
    if (country == 22) span3.innerHTML = "Sweden";
    if (country == 23) span3.innerHTML = "Philippines";
    if (country == 24) span3.innerHTML = "South Korea";
    if (country == 25) span3.innerHTML = "Thailand";
    if (country == 26) span3.innerHTML = "New Zealand";
    if (country == 27) span3.innerHTML = "Switzerland";
    if (country == 28) span3.innerHTML = "UAE";
    if (country == 29) span3.innerHTML = "Ireland";
    if (country == 30) span3.innerHTML = "Wales";
    if (country == 31) span3.innerHTML = "Scotland";
    if (country == 32) span3.innerHTML = "Andorra";
    if (country == 33) span3.innerHTML = "Angola";
    if (country == 36) span3.innerHTML = "Antigua and Barbuda";
    if (country == 37) span3.innerHTML = "Argentina";
    if (country == 38) span3.innerHTML = "Armenia";
    if (country == 40) span3.innerHTML = "Azerbaijan";
    if (country == 41) span3.innerHTML = "Bahamas";
    if (country == 42) span3.innerHTML = "Bahrain";
    if (country == 43) span3.innerHTML = "Bangladesh";
    if (country == 44) span3.innerHTML = "Barbados";
    if (country == 45) span3.innerHTML = "Belarus";
    if (country == 46) span3.innerHTML = "Belgium";
    if (country == 47) span3.innerHTML = "Belize";
    if (country == 48) span3.innerHTML = "Benin ";
    if (country == 50) span3.innerHTML = "Bhutan";
    if (country == 51) span3.innerHTML = "Bolivia";
    if (country == 52) span3.innerHTML = "Bosnia and Herzegovina";
    if (country == 53) span3.innerHTML = "Botswana";
    if (country == 56) span3.innerHTML = "Brunei Darussalam";
    if (country == 57) span3.innerHTML = "Bulgaria ";
    if (country == 58) span3.innerHTML = "Burkina Faso";
    if (country == 59) span3.innerHTML = "Burundi";
    if (country == 60) span3.innerHTML = "Cambodia";
    if (country == 61) span3.innerHTML = "Cameroon";
    if (country == 62) span3.innerHTML = "Cape Verde";
    if (country == 64) span3.innerHTML = "Central African Republic";
    if (country == 65) span3.innerHTML = "Chad";
    if (country == 66) span3.innerHTML = "Chile";
    if (country == 69) span3.innerHTML = "Colombia";
    if (country == 70) span3.innerHTML = "Comoros";
    if (country == 71) span3.innerHTML = "Congo";
    if (country == 72) span3.innerHTML = "East Timor";
    if (country == 73) span3.innerHTML = "Costa Rica";
    if (country == 74) span3.innerHTML = "Cote D'ivoire";
    if (country == 75) span3.innerHTML = "Croatia";
    if (country == 76) span3.innerHTML = "Cuba";
    if (country == 77) span3.innerHTML = "Cyprus";
    if (country == 78) span3.innerHTML = "Czech Republic";
    if (country == 79) span3.innerHTML = "Denmark";
    if (country == 80) span3.innerHTML = "Djibouti";
    if (country == 81) span3.innerHTML = "Dominica";
    if (country == 82) span3.innerHTML = "Dominican Republic";
    if (country == 83) span3.innerHTML = "Ecuador";
    if (country == 84) span3.innerHTML = "Egypt";
    if (country == 85) span3.innerHTML = "El Salvador";
    if (country == 86) span3.innerHTML = "Equatorial Guinea";
    if (country == 87) span3.innerHTML = "Eritrea";
    if (country == 88) span3.innerHTML = "Estonia";
    if (country == 89) span3.innerHTML = "Ethiopia";
    if (country == 92) span3.innerHTML = "Fiji";
    if (country == 93) span3.innerHTML = "Finland";
    if (country == 97) span3.innerHTML = "Gabon";
    if (country == 98) span3.innerHTML = "Gambia";
    if (country == 99) span3.innerHTML = "Georgia";
    if (country == 100) span3.innerHTML = "Ghana ";
    if (country == 102) span3.innerHTML = "Greece ";
    if (country == 104) span3.innerHTML = "Grenada ";
    if (country == 107) span3.innerHTML = "Guatemala ";
    if (country == 108) span3.innerHTML = "Guinea ";
    if (country == 109) span3.innerHTML = "Guinea-bissau ";
    if (country == 110) span3.innerHTML = "Guyana ";
    if (country == 111) span3.innerHTML = "Haiti ";
    if (country == 114) span3.innerHTML = "Honduras ";
    if (country == 115) span3.innerHTML = "Kosovo";
    if (country == 116) span3.innerHTML = "Hungary ";
    if (country == 117) span3.innerHTML = "Iceland ";
    if (country == 118) span3.innerHTML = "Iran";
    if (country == 119) span3.innerHTML = "Iraq ";
    if (country == 120) span3.innerHTML = "Israel ";
    if (country == 121) span3.innerHTML = "Jamaica ";
    if (country == 122) span3.innerHTML = "Jordan ";
    if (country == 123) span3.innerHTML = "Kazakhstan ";
    if (country == 124) span3.innerHTML = "Kenya ";
    if (country == 125) span3.innerHTML = "Kiribati ";
    if (country == 126) span3.innerHTML = "Kuwait ";
    if (country == 127) span3.innerHTML = "Kyrgyzstan ";
    if (country == 128) span3.innerHTML = "Laos";
    if (country == 129) span3.innerHTML = "Latvia ";
    if (country == 130) span3.innerHTML = "Lebanon ";
    if (country == 131) span3.innerHTML = "Lesotho ";
    if (country == 132) span3.innerHTML = "Liberia ";
    if (country == 133) span3.innerHTML = "Libya ";
    if (country == 134) span3.innerHTML = "Liechtenstein ";
    if (country == 135) span3.innerHTML = "Lithuania ";
    if (country == 136) span3.innerHTML = "Luxembourg ";
    if (country == 138) span3.innerHTML = "Macedonia ";
    if (country == 139) span3.innerHTML = "Madagascar ";
    if (country == 140) span3.innerHTML = "Malawi ";
    if (country == 141) span3.innerHTML = "Malaysia ";
    if (country == 142) span3.innerHTML = "Maldives ";
    if (country == 143) span3.innerHTML = "Mali ";
    if (country == 144) span3.innerHTML = "Malta ";
    if (country == 145) span3.innerHTML = "Marshall Islands ";
    if (country == 147) span3.innerHTML = "Mauritania ";
    if (country == 148) span3.innerHTML = "Mauritius ";
    if (country == 150) span3.innerHTML = "Micronesia";
    if (country == 151) span3.innerHTML = "Moldova ";
    if (country == 152) span3.innerHTML = "Monaco ";
    if (country == 153) span3.innerHTML = "Mongolia ";
    if (country == 154) span3.innerHTML = "Montenegro ";
    if (country == 155) span3.innerHTML = "Morocco ";
    if (country == 156) span3.innerHTML = "Mozambique ";
    if (country == 157) span3.innerHTML = "Burma (Myanmar) ";
    if (country == 158) span3.innerHTML = "Namibia ";
    if (country == 159) span3.innerHTML = "Nauru ";
    if (country == 160) span3.innerHTML = "Nepal ";
    if (country == 163) span3.innerHTML = "Nicaragua ";
    if (country == 164) span3.innerHTML = "Niger ";
    if (country == 165) span3.innerHTML = "Nigeria ";
    if (country == 169) span3.innerHTML = "Norway ";
    if (country == 170) span3.innerHTML = "Oman ";
    if (country == 171) span3.innerHTML = "Palau ";
    if (country == 172) span3.innerHTML = "Palestinian State";
    if (country == 173) span3.innerHTML = "Panama ";
    if (country == 174) span3.innerHTML = "Papua New Guinea ";
    if (country == 175) span3.innerHTML = "Paraguay ";
    if (country == 176) span3.innerHTML = "Peru ";
    if (country == 178) span3.innerHTML = "Poland ";
    if (country == 179) span3.innerHTML = "Portugal ";
    if (country == 181) span3.innerHTML = "Qatar ";
    if (country == 183) span3.innerHTML = "Romania ";
    if (country == 184) span3.innerHTML = "Rwanda ";
    if (country == 186) span3.innerHTML = "Saint Kitts and Nevis ";
    if (country == 187) span3.innerHTML = "Saint Lucia ";
    if (country == 189) span3.innerHTML = "Saint Vincent and The Grenadines ";
    if (country == 190) span3.innerHTML = "Samoa ";
    if (country == 191) span3.innerHTML = "San Marino ";
    if (country == 192) span3.innerHTML = "Sao Tome and Principe ";
    if (country == 193) span3.innerHTML = "Saudi Arabia ";
    if (country == 194) span3.innerHTML = "Senegal ";
    if (country == 195) span3.innerHTML = "Serbia";
    if (country == 196) span3.innerHTML = "Seychelles ";
    if (country == 197) span3.innerHTML = "Sierra Leone ";
    if (country == 198) span3.innerHTML = "Singapore ";
    if (country == 199) span3.innerHTML = "Slovakia ";
    if (country == 200) span3.innerHTML = "South Korea";
    if (country == 34) span3.innerHTML = "Slovenia ";
    if (country == 35) span3.innerHTML = "Solomon Islands ";
    if (country == 39) span3.innerHTML = "Somalia ";
    if (country == 49) span3.innerHTML = "Sri Lanka ";
    if (country == 54) span3.innerHTML = "Sudan ";
    if (country == 55) span3.innerHTML = "Suriname ";
    if (country == 63) span3.innerHTML = "Swaziland ";
    if (country == 67) span3.innerHTML = "Syria ";
    if (country == 68) span3.innerHTML = "Taiwan ";
    if (country == 90) span3.innerHTML = "Tajikistan ";
    if (country == 91) span3.innerHTML = "Tanzania ";
    if (country == 94) span3.innerHTML = "Togo ";
    if (country == 95) span3.innerHTML = "Tonga ";
    if (country == 96) span3.innerHTML = "Trinidad and Tobago ";
    if (country == 101) span3.innerHTML = "Tunisia ";
    if (country == 103) span3.innerHTML = "Turkmenistan ";
    if (country == 105) span3.innerHTML = "Tuvalu ";
    if (country == 106) span3.innerHTML = "Uganda ";
    if (country == 112) span3.innerHTML = "Ukraine ";
    if (country == 113) span3.innerHTML = "Uruguay ";
    if (country == 137) span3.innerHTML = "Uzbekistan ";
    if (country == 146) span3.innerHTML = "Vanuatu ";
    if (country == 149) span3.innerHTML = "Venezuela ";
    if (country == 161) span3.innerHTML = "Vietnam ";
    if (country == 162) span3.innerHTML = "Western Sahara ";
    if (country == 166) span3.innerHTML = "Yemen ";
    if (country == 167) span3.innerHTML = "Zambia ";
    if (country == 168) span3.innerHTML = "Zimbabwe";
    if (country == 182) span3.innerHTML = "Afghanistan";
    if (country == 185) span3.innerHTML = "Albania";
    if (country == 188) span3.innerHTML = "Algeria";
    if (country == 177) span3.innerHTML = "Vatican City";
    if (country == 180) span3.innerHTML = "Zaire";
    
    var href4 = document.getElementById("CountryLink");
    href4.innerHTML = "(change)";

    if (country == 0) {
        span3.innerHTML = "";
        href4.innerHTML = "Country preference?";
    }

    if (lastcalltype == 1) StartUpdate();
    else StartLongList();

    return true;
}



function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}

function DoFollow(targetid) {

    if (myid == "0") {
        window.location = "Authenticate.aspx";
        return false;
    }

    lasttarget = targetid;
    var div = document.getElementById(targetid.toString());
    div.className = "VisibleSM";

    div.innerHTML = "<img src='/images/ajax1.gif'/> One moment please...<br/><br/>";

    ret = Actions.Follow(myid, targetid.toString(), OnFollowed, OnFollowFail, OnFollowFail);


    return false;
}
function DoFollow2(targetid) {

    if (myid == "0") {
        window.location = "Authenticate.aspx";
        return false;
    }
    
    lasttarget = targetid;
    var div = document.getElementById(targetid.toString());
    div.className = "VisibleSM2";

    div.innerHTML = "<img src='/images/ajax1.gif'/> One moment please...<br/><br/>";

    ret = Actions.Follow(myid, targetid.toString(), OnFollowed, OnFollowFail, OnFollowFail);


    return false;
}

function OnFollowed(result) {
    var outcome = parseInt(result.substring(0, 1));
    var id = result.substring(1, result.length);

    var div = document.getElementById(id);

    if (outcome == 4) {
        div.innerHTML = "<img src='/images/lock.png'/> &nbsp; Sorry, but we need to authenticate you first...<br/><br/>";

        window.location = "Authenticate.aspx";
    }
    if (outcome == 1 || outcome == 2) {
        if (outcome == 1) div.innerHTML = " <img src='/images/tick.png'/> &nbsp; You are now following this person...<br/><br/>";
        if (outcome == 2) div.innerHTML = " <img src='/images/cross.png'/> &nbsp; Sorry, you couldn't follow this person. " +
                                                                " Are you following them already?    <br/><br/>";
   
    }
    else {

        div.innerHTML = "  <img src='/images/cross.png'/> &nbsp; An error occured, please try again......<br/><br/>";
    }

 }

function OnFollowFail() {

    var div = document.getElementById(lasttarget);

    div.innerHTML = "  An error occured, please try again...<br/><br/>";

}

function TweetLink() {

    var url = "Just came across this great site for finding people on twitter... http://diggyr.com";

    var safeurl = "http://twitter.com/home?status=" + URLEncode(url);
    
    window.open(safeurl);

}

function URLEncode(clearString) {
    var output = '';
    var x = 0;
    clearString = clearString.toString();
    var regex = /(^[a-zA-Z0-9_.]*)/;
    while (x < clearString.length) {
        var match = regex.exec(clearString.substr(x));
        if (match != null && match.length > 1 && match[1] != '') {
            output += match[1];
            x += match[1].length;
        } else {
            if (clearString[x] == ' ')
                output += '+';
            else {
                var charCode = clearString.charCodeAt(x);
                var hexVal = charCode.toString(16);
                output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
            }
            x++;
        }
    }
    return output;
}