function addUser() {
    params = "username="+escape(document.getElementById("username").value);
    params += "&name1="+escape(document.getElementById("name1").value);
    params += "&getsdate="+document.getElementById("getsdate").checked;
    params += "&name2="+escape(document.getElementById("name2").value);
    params += "&password="+escape(document.getElementById("password").value);
    params += "&email="+escape(document.getElementById("email").value);
    params += "&addr1="+escape(document.getElementById("address1").value);
    params += "&addr2="+escape(document.getElementById("address2").value);
    params += "&city="+escape(document.getElementById("city").value);
    params += "&state="+escape(document.getElementById("state").value);
    params += "&zip="+escape(document.getElementById("zip").value);
    params += "&admin="+document.getElementById("admin").checked;
    ajax("/wedding/admin/functions.php?f=createUser&uniq="+Math.random(), params, addUserCB);
}

function addUserCB(response) {
    if (response != "true") {
        document.getElementById("userError").style.display = "block";
        document.getElementById("userError").innerHTML = response;
    } else {
        document.getElementById("userError").style.display = "none";
        document.getElementById("username").value = "";
        document.getElementById("name1").value = "";
        document.getElementById("getsdate").checked = true;
        document.getElementById("name2").value = "";
        document.getElementById("name2").disabled = false;
        document.getElementById("password").value = "";
        document.getElementById("email").value = "";
        document.getElementById("address1").value = "";
        document.getElementById("address2").value = "";
        document.getElementById("city").value = "";
        document.getElementById("state").value = "";
        document.getElementById("zip").value = "";
        document.getElementById("admin").checked = false;
        getUserTable();
    }
}

function editUser() {
    document.getElementById("editUserError").style.display = "none";

    var params = "username="+escape(document.getElementById("username").value);
    params += "&name1="+escape(document.getElementById("name1").value);
    params += "&getsdate="+document.getElementById("getsdate").checked;
    if (document.getElementById("name2")) {
        params += "&name2="+escape(document.getElementById("name2").value);
    } else {
        params += "&name2=";
    }
    params += "&email="+escape(document.getElementById("email").value);
    params += "&addr1="+escape(document.getElementById("address1").value);
    params += "&addr2="+escape(document.getElementById("address2").value);
    params += "&city="+escape(document.getElementById("city").value);
    params += "&state="+escape(document.getElementById("state").value);
    params += "&zip="+escape(document.getElementById("zip").value);
    params += "&sendUpdate="+document.getElementById("sendUpdate").checked;
    ajax("/wedding/admin/functions.php?f=editUser&uniq="+Math.random(), params, editUserCB);
}

function editUserCB(response) {
    document.getElementById("editUserError").style.display = "block";
    if (response != "true") {
        document.getElementById("editUserError").className = "error";
        document.getElementById("editUserError").innerHTML = response;
    } else {
        document.getElementById("editUserError").className = "success";
        document.getElementById("editUserError").innerHTML = "Successfully edited user information";
    }
}

function addRoleToUser(roleid) {
    var params = "username=" + document.getElementById("username").value;
    params += "&roleid=" + roleid;
    ajax("/wedding/admin/functions.php?f=addRoleToUser&uniq="+Math.random(), params, addRoleToUserCB);
}

function addRoleToUserCB(response) {
    document.getElementById("roleError").style.display = "block";
    if (response != "true") {
        document.getElementById("roleError").className = "error";
        document.getElementById("roleError").innerHTML = response;
    } else {
        document.getElementById("roleError").className = "success";
        document.getElementById("roleError").innerHTML = "Successfully added role.";
    }
}

function getUserTable(filter) {
    document.getElementById("userList").innerHTML = "<img src='/img/ajaxload.gif' alt='Loading...' />";
    ajax("/wedding/admin/functions.php?f=getUserTable&filter="+filter+"&uniq="+Math.random(),"",userTableCB);
    ajax("/wedding/admin/functions.php?f=getNumUsers&uniq="+Math.random(),"",numUsersCB);
    ajax("/wedding/admin/functions.php?f=getNumGuests&uniq="+Math.random(),"",numGuestsCB);
    ajax("/wedding/admin/functions.php?f=getNumInvitees&uniq="+Math.random(),"",numInviteesCB);
} 

function userTableCB(response) {
    document.getElementById("userList").innerHTML = response;
}

function numUsersCB(response) {
    document.getElementById("numUsers").innerHTML = response;
}

function numGuestsCB(response) {
    document.getElementById("numGuests").innerHTML = response;
}

function numInviteesCB(response) {
    document.getElementById("numInvitees").innerHTML = response;
}

function loginUser() {
    document.getElementById("auth_error").style.display = "none";
    
    params = "username="+document.getElementById("auth_username").value;
    params += "&password="+document.getElementById("auth_password").value;
    ajax("/wedding/login.php?uniq="+Math.random(), params, loginUserCB);

    return false;
}

function loginUserCB(response) {
    if (response == "unverified") {
        window.location.href = "/wedding/verify.php";
    } else if (response != "true") {
        document.getElementById("auth_error").style.display = "block";
        document.getElementById("auth_error").innerHTML = response;
    } else {
        window.location.reload();
    }
}

function logoutUser() {
    var date = new Date();
    date.setTime(date.getTime()-(24*60*60*1000));
    document.cookie = "hash=; path=/wedding/; domain=.jakattack.net; expires="+date.toGMTString();
    window.location.reload();
}

function addMessage() {
    params = "message="+escape(document.getElementById("message").value);
    ajax("/wedding/functions.php?f=addMessage&uniq="+Math.random(),params,addMessageCB);
}

function addMessageCB(response) {
    if (response != "true") {
        document.getElementById("message_error").style.display = "block";
        document.getElementById("message_error").innerHTML = response;
    } else {
        document.getElementById("message_error").style.display = "none";
        getAllMessages();
    }
}

function getAllMessages() {
    ajax("/wedding/functions.php?f=getMessagesDiv&uniq="+Math.random(),"",getAllMessagesCB);
}

function getAllMessagesCB(response) {
    document.getElementById("messages").innerHTML = response;
}

function clearMessage() {
    if (document.getElementById("message").value == "Leave your message here...") {
        document.getElementById("message").value = "";
    }
}

function verifyUser() {
    params = "name1="+escape(document.getElementById("name1").value);
    if (document.getElementById("date")) {
        params += "&date="+escape(document.getElementById("date").value);
    } else {
        params += "&date=";
    }
    params += "&email="+escape(document.getElementById("email").value);
    params += "&addr1="+escape(document.getElementById("addr1").value);
    params += "&addr2="+escape(document.getElementById("addr2").value);
    params += "&city="+escape(document.getElementById("city").value);
    params += "&state="+escape(document.getElementById("state").value);
    params += "&zip="+escape(document.getElementById("zip").value);
    params += "&sendUpdate="+document.getElementById("sendUpdate").checked;
    ajax("/wedding/functions.php?f=verifyUser&uniq="+Math.random(),params,verifyUserCB);
}

function verifyUserCB(response) {
    if (response != "true") {
        document.getElementById("verify_error").style.display = "block";
        document.getElementById("verify_error").innerHTML = response;
    } else {
        window.location.href = "/wedding/index.php";
    }
}

function rsvpUser() {
    params = "rsvp="+document.getElementById("rsvp")[document.getElementById("rsvp").selectedIndex].value;
    if (arguments.length == 1) {
        params += "&user="+arguments[0];
    }
    document.getElementById("rsvp_result").style.display = "none";
    ajax("/wedding/functions.php?f=rsvpUser&uniq="+Math.random(),params,rsvpUserCB);
}

function rsvpUserCB(response) {
    document.getElementById("rsvp_result").style.display = "block";
    document.getElementById("rsvp_result").className = "success";
    document.getElementById("rsvp_result").innerHTML = "Your RSVP status has been successfully updated.";
    
    if (response == "0") {
        document.getElementById("rsvp_result").innerHTML += " We're sorry you won't be able to make it."
        document.getElementById("rsvp_status").innerHTML = "NOT ATTENDING";
    } else if (response == "1") {
        document.getElementById("rsvp_result").innerHTML += " We're glad you're coming! Check out the rest of our site via the navigation bar on the right: <ul><li>The Maps and Accomodations page gives yout directions to the wedding, hotel, and rehearsal dinner, as well as things to do around the area.</li><li>The Reception Info page to see the reception schedule, menu, and help us pick our music!</li><li>The Wedding Party page, if you're standing up at the wedding, will give you your personal instructions</li><li>The Photos page will have links to our wedding pictures after the ceremony.</li></ul>";
        document.getElementById("rsvp_status").innerHTML = "ONE PERSON ATTENDING";
    } else if (response == "2") {
        document.getElementById("rsvp_result").innerHTML += " We're glad you're coming! Check out the rest of our site via the navigation bar on the right: <ul><li>The Maps and Accomodations page gives yout directions to the wedding, hotel, and rehearsal dinner, as well as things to do around the area.</li><li>The Reception Info page to see the reception schedule, menu, and help us pick our music!</li><li>The Wedding Party page, if you're standing up at the wedding, will give you your personal instructions</li><<li>The Photos page will have links to our wedding pictures after the ceremony.</li>/ul>";
        document.getElementById("rsvp_status").innerHTML = "TWO PEOPLE ATTENDING";
    } else {
        document.getElementById("rsvp_result").className = "error";
        document.getElementById("rsvp_result").innerHTML = response;
    }
}

function addSong() {
    params = "artist="+escape(document.getElementById("artist").value);
    params += "&track="+escape(document.getElementById("track").value);
    document.getElementById("addSongError").style.display = "none";
    ajax("/wedding/functions.php?f=addSong&uniq="+Math.random(),params,addSongCB);
}

function addSongCB(response) {
    if (response == "false") {
        document.getElementById("addSongError").style.display = "block";
        document.getElementById("addSongError").innerHTML = "Sorry, there has been an error. Try again.";
    } else {
        document.getElementById("songList").innerHTML = response + document.getElementById("songList").innerHTML;
    }
}

function upVoteSong(id) {
    params = "id="+id;
    if (document.getElementById("plus"+id).style.color != "red") {
        ajax("/wedding/functions.php?f=upVoteSong&uniq="+Math.random(),params,upVoteSongCB);
    } else {
        ajax("/wedding/functions.php?f=removeVoteSong&uniq="+Math.random(),params,removeVoteSongCB);
    }
}

function upVoteSongCB(response) {
    var id = response.split(",")[0];
    var votes = response.split(",")[1];
    document.getElementById("plus"+id).style.color = "red";
    document.getElementById("minus"+id).style.color = "black";
    document.getElementById("numVotes"+id).innerHTML = votes;
}

function downVoteSong(id) {
    var params = "id="+id;
    if (document.getElementById("minus"+id).style.color != "red") {
        ajax("/wedding/functions.php?f=downVoteSong&uniq="+Math.random(),params,downVoteSongCB);
    } else {
        ajax("/wedding/functions.php?f=removeVoteSong&uniq="+Math.random(),params,removeVoteSongCB);
    }
}

function downVoteSongCB(response) {
    var id = response.split(",")[0];
    var votes = response.split(",")[1];
    document.getElementById("minus"+id).style.color = "red";
    document.getElementById("plus"+id).style.color = "black";
    document.getElementById("numVotes"+id).innerHTML = votes;
}

function removeVoteSongCB(response) {
    var id = response.split(",")[0];
    var votes = response.split(",")[1];
    document.getElementById("minus"+id).style.color = "black";
    document.getElementById("plus"+id).style.color = "black";
    document.getElementById("numVotes"+id).innerHTML = votes; 
}

function sendUpdate() {
    var params = "title=" + escape(document.getElementById("title").value);
    params += "&desc=" + escape(document.getElementById("description").value);
    params += "&role=" + escape(document.getElementById("role")[document.getElementById("role").selectedIndex].value);
    ajax("/wedding/admin/functions.php?f=sendUpdate&uniq="+Math.random(),params,sendUpdateCB);
}

function sendUpdateCB(response) {
    alert(response);
}
