// Deezine I <3 Bike Lanes Positivism Project 2009
// Scott
// August 2009
// Provides AJAX functionality for "Add Rider"

var sent = false;
var ajaxRequest;

$(document).ready(function(){
    highlight();
    $("#button").click(function(){
        if ($("#buttonExpand").is(":hidden")){
            $('#button').css('backgroundImage','url("./images/button-tall-top.jpg")'); 
            $("#buttonExpand").slideDown("fast");
            success = document.getElementById("buttonFeedback").innerHTML = "(click here to count yourself in)";
        }
        else if (isValid()){
            doAdd();
        } 
    });
});

function highlight() {
    var title = document.getElementById("pageTitle").innerHTML;
    var link = "";
    if (title != "a massive thank-u-ride-a-thon.") {
    document.title = title + " - i heart bike lanes"; 
    switch (title) {
        case "about the event"                      : link = "about";           break; 
        case "map of the ride"                      : link = "map";             break;
        case "starting place"                       : link = "start";           break;
        case "free give-aways"                      : link = "giveaways";       break;
        case "buy a d-shirt"                        : link = "dshirts";         break;
        case "10% of profits to WAM"                : link = "wam";             break;
        case "70% bamboo + 30% cotton"              : link = "shirtmaterial";   break;
        case "official thank-u-ride-a-thon jersey"  : link = "jersey";          break;
        case "behind the idea"                      : link = "idea";            break;
        case "who's idea is this"                   : link = "who";             break;
        case "PDT - public display of thanks"       : link = "pdt";             break;
        case "get posters and stickers"             : link = "posters";         break;
        default                                     : link = "";                break;
    }
    if (link != "") {document.getElementById(link).style.color = "#ff3"; }
    }
}
function isValid() {
    var name  = document.getElementById("name").value;
    var email = document.getElementById("email").value;
    if (true == isEmailValid(email) && "Email" != email && true == !isBlank(name) && "Name" != name) 
    { return true; }
    else if (false == isEmailValid(email) || "Email" == email) { 
        document.getElementById('buttonFeedback').innerHTML = "What is your email?";
        return false; 
    }  
    else if (false == !isBlank(name) || "Name" == name) {
        document.getElementById('buttonFeedback').innerHTML = "What is your name?";
        return false;
    }
    else { 
        document.getElementById('buttonFeedback').innerHTML = "isValid error 1; email scott@deezine.ca";
        return false;
    }
}

function addToDB() {
    try { ajaxRequest = new XMLHttpRequest();
    } catch (e) { try { ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) { try { ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("addToDB Error.  Please email scott@deezine.ca with your operating system and browser version.  Thanks!"); 
                return false;
            } } }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function() {
        if (ajaxRequest.readyState == 4) { 
            if (0 < ajaxRequest.responseText) {
                document.getElementById('NumberOfRiders').innerHTML = "We're " + ajaxRequest.responseText + " Strong";
                document.getElementById('buttonFeedback').innerHTML = "(you're now an official rider!)";
                sendNewRiderEmail();
            } else {
                document.getElementById('buttonFeedback').innerHTML = "(you're already an official rider!)";                
    } } }
    var name  = document.getElementById("name").value;
    var email = document.getElementById("email").value; 
    var r = Math.floor(Math.random()*1000001)
    var url = "scripts/addToDB.php";  
    var params = "?email=" + email + "&name=" + name + "&r=" + r;
    ajaxRequest.open("GET", url + params, true);
    ajaxRequest.send(null);
}

function sendNewRiderEmail() {
    try { ajaxRequest = new XMLHttpRequest();
    } catch (e) { try { ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) { try { ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                alert("sendNewRiderEmail Error.  Please email scott@deezine.ca with your operating system and browser version.  Thanks!"); 
                return false;
            } } } 
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function() {
        if (ajaxRequest.readyState == 4) { 
            // do nothing 
        } }
    var name  = document.getElementById("name").value;
    var email = document.getElementById("email").value; 
    var r = Math.floor(Math.random()*1000001)
    var url = "scripts/newRiderEmail.php";  
    var params = "?email=" + email + "&name=" + name + "&r=" + r;
    ajaxRequest.open("GET", url + params, true);
    ajaxRequest.send(null);
}

function fieldclear(field)   {
  if (field.defaultValue == field.value) { field.value = ''; }
}
function restore(field) {
  if (field.value == "") { field.value = field.defaultValue; }
}
function isBlank(field)
{   
     if (field == "")
     {
         return true;
     }
     else
     {
         return false;
     }
}
function isEmailValid(email)
{   
     if (email.indexOf("@") < 0 || email.indexOf(".") < 0)
     {
         return false;
     }
     else
     {
         return true;
     }
}
function enter(event) {
    if (typeof event == "undefined") { event = window.event; } 
    var val = event.keyCode;
    if(val == 13) 
    { 
        doAdd();
    }
}

function doAdd () {
    if (isValid()) 
        {
            $("#buttonExpand").slideUp("fast", function() {
                $('#button').css('backgroundImage','url("./images/button.jpg")');} 
            );
            addToDB();
        }
}