//This array returns the opposite of the current hidden value.
var hiddenarray = new Array("1","0");
var yesNo = new Array("No","Yes");

function clearOld(){
	//Need to delete the current list, and then relist.
	var oldlist = document.getElementById("cattable");
	//oldlist.value = "";
	while (oldlist.hasChildNodes()){
		oldlist.removeChild(oldlist.firstChild);
	}
	oldlist = document.getElementById("commentstable");
	while (oldlist.hasChildNodes()){
		oldlist.removeChild(oldlist.firstChild);
	}
	oldlist = document.getElementById("buttonsarea");
	while (oldlist.hasChildNodes()){
		oldlist.removeChild(oldlist.firstChild);
	}
	document.getElementById("admincats").style.display = "none";
	document.getElementById("commentlist").style.display = "none";
	document.getElementById("buttonarea").style.display = "none";
}

/**
	This function shows the div that lists the categories.
	Then queries the DB and gets them.
*/
function listCats(){
	clearOld();
	var parse = function(returnedXML){
		var table = document.getElementById("cat");
		var tbody = document.getElementById("cattable");
		
		//This inserts the header row for the list
		var header = document.createElement("tr");
		header.setAttribute('id', 'header1');
		header.setAttribute('class', 'bioheader');
		//For IE
		header.className = "bioheader";
		
		//Insert the headers for the table
		var headers = new Array("id","Category Name","Hidden?","Public?");
		for (x = 0; x < headers.length; x++){
			var aheader  = document.createElement("th");
			aheader.setAttribute('class', 'header');
			//For IE
			aheader.className = "header";
			
			aheader.innerHTML = headers[x];
			header.appendChild(aheader);
		}
		
		tbody.appendChild(header);
				
		y = returnedXML.getElementsByTagName("cats")[0].childNodes.length;
		for (x = 0; x < y; x++){
			var id = returnedXML.getElementsByTagName("id")[x].childNodes[0].nodeValue;
			var category = returnedXML.getElementsByTagName("category")[x].childNodes[0].nodeValue;
			var public = returnedXML.getElementsByTagName("public")[x].childNodes[0].nodeValue;
			var hidden = returnedXML.getElementsByTagName("hidden")[x].childNodes[0].nodeValue;

			var newbio = document.createElement("tr");
			newbio.setAttribute('id', 'b' + id);
			
			var thedata = new Array(id,category,hidden,public);
			//Add the friends info to the table based on thedata array.
			for (j = 0; j < thedata.length; j++){
				
				var anitem = document.createElement("td");
				anitem.setAttribute('class', 'biocontainer');
				//For IE
				anitem.className = "biocontainer";
				
				anitem.innerHTML = thedata[j];
				newbio.appendChild(anitem);
			}

			//This adds the buttons (Edit and Delete)
			var anitem = document.createElement("td");
			anitem.setAttribute('class', 'buttons');
			//For IE
			anitem.className = "buttons";
			
			anitem.innerHTML = '<input type="button" onclick="startEditCat(' + id + ');" value="Edit" /><input type="button" onclick="deleteCat(' + id + ');" value="Delete" disabled="true" />';
			newbio.appendChild(anitem);

			//Add the new tag to the page
			tbody.appendChild(newbio);
			//alert(x + " " + tbody.innerHTML);
		}
		table.appendChild(tbody);
		document.getElementById("admincats").style.display = "block";
	}
	AJAX("GET","../libs/managecontact.php","?action=listcats","true",parse);
}

/**
	This function clears out the text boxes and begins the process of adding a 
	new category to the database.
*/
function startCatAdd(){
	document.getElementById("alterCat").value = "Add Category";
	document.getElementById("category").value = "";
	document.getElementById("hidden").value = 0;
	document.getElementById("public").value = 0;
}

function startEditCat(id){
	document.getElementById("alterCat").value = "Edit Category";
	var parse = function(returnedXML){
		document.getElementById("id").value = returnedXML.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		document.getElementById("category").value = returnedXML.getElementsByTagName("category")[0].childNodes[0].nodeValue;
		document.getElementById("public").value = returnedXML.getElementsByTagName("public")[0].childNodes[0].nodeValue;
		document.getElementById("hidden").value = returnedXML.getElementsByTagName("hidden")[0].childNodes[0].nodeValue;
	}
	AJAX("GET","../libs/managecontact.php","?action=getcat&id=" + id,"true",parse);
}

function alterCategory(){
	var parse = function(returnedXML){
		outcome = returnedXML.getElementsByTagName("outcome")[0].childNodes[0].nodeValue;
		message = returnedXML.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		//if(outcome != "001"){
			alert(outcome + "\n" + message);
		//}
	}
	category = encodeItem("category",document.getElementById("category").value);
	hidden = encodeItem("&hidden",document.getElementById("hidden").value);
	public = encodeItem("&public",document.getElementById("public").value);
	datatopost = category + hidden + public;
	if(document.getElementById("alterCat").value == "Add Category"){
		AJAX("POST","../libs/managecontact.php?action=addcategory",datatopost,"true",parse);	
	}
	else if(document.getElementById("alterCat").value == "Edit Category"){
		id = document.getElementById("id").value;
		AJAX("POST","../libs/managecontact.php?action=editcategory&id=" + id,datatopost,"true",parse);	
	}
}





/**
	This function shows the div that lists the messages.
	Then queries the DB and gets them.
*/
function listMessages(user){
	clearOld();
	var parse = function(returnedXML){
		var table = document.getElementById("comments");
		var tbody = document.getElementById("commentstable");
		
		//This inserts the header row for the list
		var header = document.createElement("tr");
		header.setAttribute('id', 'header1');
		header.setAttribute('class', 'bioheader');
		//For IE
		header.className = "bioheader";
		
		//Insert the headers for the table
		var headers = new Array("id","From","Subject","Date","Category");
		for (x = 0; x < headers.length; x++){
			var aheader  = document.createElement("th");
			aheader.setAttribute('class', 'header');
			//For IE
			aheader.className = "header";
			
			aheader.innerHTML = headers[x];
			header.appendChild(aheader);
		}
		
		tbody.appendChild(header);
				
		y = returnedXML.getElementsByTagName("comments")[0].childNodes.length;
		for (x = 0; x < y; x++){
			//var id;
			//var from;
			//var subject;
			//var dateadded;
			//alert(x);
			var read;
			//var category;
			var thedata = "";
			thedata = new Array();
			m_contactattributes = new Array('id','name','subject','dateadded','topic','read');
			var id = returnedXML.getElementsByTagName("id")[x].childNodes[0].nodeValue;
			for(g = 0; g < m_contactattributes.length; g++){
				if(returnedXML.getElementsByTagName(m_contactattributes[g])[x].hasChildNodes()){
					if(m_contactattributes[g] == "read"){
						read = returnedXML.getElementsByTagName(m_contactattributes[g])[x].childNodes[0].nodeValue;
					}
					else{
						thedata.push(returnedXML.getElementsByTagName(m_contactattributes[g])[x].childNodes[0].nodeValue);
					}
				}
				else{
					thedata.push("");
				}
			}
			//alert(thedata);
			//
			//var from = returnedXML.getElementsByTagName("name")[x].childNodes[0].nodeValue;
			//var subject = returnedXML.getElementsByTagName("subject")[x].childNodes[0].nodeValue;
			//var dateadded = returnedXML.getElementsByTagName("dateadded")[x].childNodes[0].nodeValue;
			//var read = returnedXML.getElementsByTagName("read")[x].childNodes[0].nodeValue;
			//var category = returnedXML.getElementsByTagName("topic")[x].childNodes[0].nodeValue;


			var newrow = document.createElement("tr");
			newrow.setAttribute('id', 'b' + id);
			
			//var thedata = new Array(id,from,subject,dateadded,category);
			//Add the friends info to the table based on thedata array.
			for (j = 0; j < thedata.length; j++){
				
				var anitem = document.createElement("td");
				if (read=="0"){
					anitem.setAttribute('class', 'unreadmessage');
					//For IE
					anitem.className = "unreadmessage";
				}
				else{
					anitem.setAttribute('class', 'readmessage');
					//For IE
					anitem.className = "readmessage";		
				}
				
				anitem.innerHTML = '<span class="linktomessage" onclick="openMessage(' + id + ');">' + thedata[j] + "</span>";
				newrow.appendChild(anitem);
			}

			//Add the new tag to the page
			tbody.appendChild(newrow);
			//alert(x + " " + tbody.innerHTML);
		}
		table.appendChild(tbody);
		document.getElementById("commentlist").style.display = "block";
	}
	AJAX("GET","../libs/managecontact.php","?action=listmessages&user=" + user,"true",parse);
}

/**
	Opens the comment in a new window.
*/
function openMessage(id){
	var oldlist = document.getElementById("buttonsarea");
	while (oldlist.hasChildNodes()){
		oldlist.removeChild(oldlist.firstChild);
	}
	var parse = function(returnedXML){
		var id = returnedXML.getElementsByTagName("id")[0].childNodes[0].nodeValue;
		var from ="";
		if(returnedXML.getElementsByTagName("name")[0].hasChildNodes()){
			var from = returnedXML.getElementsByTagName("name")[0].childNodes[0].nodeValue;
		}
		var subject ="";
		if(returnedXML.getElementsByTagName("subject")[0].hasChildNodes()){
			var subject = returnedXML.getElementsByTagName("subject")[0].childNodes[0].nodeValue;
		}
		var dateadded ="";
		if(returnedXML.getElementsByTagName("dateadded")[0].hasChildNodes()){
			var dateadded = returnedXML.getElementsByTagName("dateadded")[0].childNodes[0].nodeValue;
		}
		var read = returnedXML.getElementsByTagName("read")[0].childNodes[0].nodeValue;
		var category ="";
		if(returnedXML.getElementsByTagName("topic")[0].hasChildNodes()){
			var category = returnedXML.getElementsByTagName("topic")[0].childNodes[0].nodeValue;
		}
		var emailaddress = "";
		if(returnedXML.getElementsByTagName("emailaddress")[0].hasChildNodes()){
			emailaddress = returnedXML.getElementsByTagName("emailaddress")[0].childNodes[0].nodeValue;
		}
		var to = "";
		if(returnedXML.getElementsByTagName("to")[0].hasChildNodes()){
			to = returnedXML.getElementsByTagName("to")[0].childNodes[0].nodeValue;
		}
		var read = returnedXML.getElementsByTagName("read")[0].childNodes[0].nodeValue;
		if(returnedXML.getElementsByTagName("commenttext")[0].hasChildNodes()){
			var comment = returnedXML.getElementsByTagName("commenttext")[0].childNodes[0].nodeValue;
		}
		document.getElementById("divfrom").innerHTML = 'From: ' + from + ' - <a href="mailto:' + emailaddress + '">' + emailaddress + '</a>';
		document.getElementById("divdateadded").innerHTML = 'Date Sent: ' + dateadded;
		document.getElementById("divto").innerHTML = 'To: ' + to;
		document.getElementById("divtopic").innerHTML = 'Category: ' + category;
		document.getElementById("divsubject").innerHTML = 'Message Subject: ' +subject;
		document.getElementById("displaycomment").innerHTML = "" + comment;
		document.getElementById("displaycomment").value = "" + comment;
		var buttons = document.getElementById("buttonsarea");
		var deletebutton = document.createElement("input");
		deletebutton.setAttribute("type","button");
		deletebutton.setAttribute("value","Delete Comment");
		//deletebutton.setAttribute("onclick","deleteComment('" + id + "');");
		//deletebutton.setAttribute("onclick","alert('test')");
		deletebutton.type = "button";
		deletebutton.value = "Delete Comment";
		deletebutton.onclick = new Function("deleteComment('" + id + "');");
		//deletebutton.onclick = "alert('test')";
		buttons.appendChild(deletebutton);
		
		var transferbutton = document.createElement("input");
		transferbutton.setAttribute("type","button");
		transferbutton.setAttribute("value","Transfer To: ");
		//transferbutton.setAttribute("onclick","transfer('" + id + "');");
		
		transferbutton.type = "button";
		transferbutton.value = "Transfer To: ";
		transferbutton.onclick = new Function("transfer('" + id + "');");
		
		buttons.appendChild(transferbutton);
		document.getElementById("buttonarea").style.display = "inline";
		
		markRead(id);
	}
	AJAX("GET","../libs/managecontact.php","?action=getmessage&id=" + id,"true",parse);
}

/**
	This function is called from the public comment page and adds a comment
	to the database.  Also checks the form to make sure it is valid before 
	submitting.
*/
function submitComment(){
	//A series of checks on the form to make sure it is valid.
	if(document.getElementById("name").value == ""){
		alert("You must fill in your name before submitting the form.  It can be an internet alias and not your real name.");
		return;
	}
	if(document.getElementById("email").value == ""){
		alert("You must fill in your email address before submitting the form.  It will be used only to follow up regarding your contact.");
		return;
	}
	if(document.getElementById("subject").value == ""){
		alert("You must fill enter a subject for this comment.");
		return;
	}
	if(document.getElementById("comment").value == ""){
		alert("You must fill enter a comment.");
		return;
	}
	if(document.getElementById("test").value != "I AM A PERSON"){
		alert("You must complete the anti-spam test before submitting your comment.");
		return;
	}
	//All tests were passed, time to submit the comment.
	var parse = function(returnedXML){
		outcome = returnedXML.getElementsByTagName("outcome")[0].childNodes[0].nodeValue;
		message = returnedXML.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		alert(message);
		if(outcome == "001"){
			//It was successful, send back to the main page.
			window.location = 'http://www.happyhitsradio.com/';
		}
	}
	pname = encodeItem("name",document.getElementById("name").value);
	pemail = encodeItem("&email",document.getElementById("email").value);
	psubject = encodeItem("&subject",document.getElementById("subject").value);
	pcomment = encodeItem("&comment",document.getElementById("comment").value);
	ptopic = encodeItem("&topic",document.getElementById("topic").value);
	//pto = encodeItem("&to",'happycamper');
	datatopost = pname + pemail + psubject + pcomment + ptopic;// + pto;
	AJAX("POST","../libs/managecontact.php?action=submitcomment",datatopost,"true",parse);
}

/**
	This function is called from the public comment page and adds a a DJ 
	application to the messaging system.
*/
function submitApplication(){
	//A series of checks on the form to make sure it is valid.
	if(document.getElementById("name").value == ""){
		alert("You must fill in your name before submitting the form.  It can be an internet alias and not your real name.");
		return;
	}
	if(document.getElementById("email").value == ""){
		alert("You must fill in your email address before submitting the form.  It will be used only to follow up regarding your contact.");
		return;
	}
	if(document.getElementById("testapp").value != "I AM A PERSON"){
		alert("You must complete the anti-spam test before submitting your comment.");
		return;
	}
	if(document.getElementById("timezone").value == ""){
		alert("Please enter your timezone.");
		return;
	}
	if(document.getElementById("songs").value == ""){
		alert("Please enter the size of your music library.");
		return;
	}
	if(document.getElementById("genre").value == ""){
		alert("Please enter the primary genre of your music library.");
		return;
	}
	if(document.getElementById("processor").value == ""){
		alert("Please enter the type of processor you have.");
		return;
	}
	if(document.getElementById("ram").value == ""){
		alert("Please enter the abount of RAM you have in your computer.");
		return;
	}
	if(document.getElementById("hdd").value == ""){
		alert("Please enter the total size of your hard drive(s).");
		return;
	}
	if(document.getElementById("os").value == ""){
		alert("Please enter your computers operating system.");
		return;
	}
	if(document.getElementById("upload").value == ""){
		alert("Please complete the speed test, and enter the reported upload speed.");
		return;
	}
	if(document.getElementById("download").value == ""){
		alert("Please complete the speed test, and enter the reported download speed.");
		return;
	}
	if(document.getElementById("whyus").value == ""){
		alert("Please lets us know why you would like to be a DJ with us.");
		return;
	}
	//All tests were passed, time to submit the comment.
	var parse = function(returnedXML){
		outcome = returnedXML.getElementsByTagName("outcome")[0].childNodes[0].nodeValue;
		message = returnedXML.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		alert(message);
		if(outcome == "001"){
			//It was successful, send back to the main page.
			window.location = 'http://www.happyhitsradio.com/';
		}
	}
	pname = encodeItem("name",document.getElementById("name").value);
	pemail = encodeItem("&email",document.getElementById("email").value);
	psubject = encodeItem("&subject","I want to DJ!");
	var application = "";
	application = application + "Name: " + document.getElementById("name").value + "\n";
	application = application + "Email: " + document.getElementById("email").value + "\n";
	if(document.getElementById("djyes").checked == true){
		application = application + "DJed: Yes     With: " + document.getElementById("ifdjyes").value + "\n";
	}
	if(document.getElementById("djno").checked == true){
		application = application + "DJed: No\n";
	}
	application = application + "DJ Name: " + document.getElementById("djname").value + "\n";
	application = application + "Yahoo ID: " + document.getElementById("yahoo").value + "\n";
	application = application + "MSN ID: " + document.getElementById("msn").value + "\n";
	application = application + "AIM ID: " + document.getElementById("aim").value + "\n";
	application = application + "Time Zone: " + document.getElementById("timezone").value + "\n";
	if(document.getElementById("conndialup").checked == true){
		application = application + "Connection: Dialup (Poor bastards)\n";
	}
	if(document.getElementById("conndsl").checked == true){
		application = application + "Connection: DSL\n";
	}
	if(document.getElementById("conncable").checked == true){
		application = application + "Connection: Cable\n";
	}
	application = application + "Birthday: " + document.getElementById("bday").value + "\n";
	if(document.getElementById("over18yes").checked == true){
		application = application + "Over 18: Yes\n";
	}
	if(document.getElementById("over18no").checked == true){
		application = application + "Over 18: No\n";
	}
	application = application + "Number of Songs: " + document.getElementById("songs").value + "\n";
	application = application + "Genre: " + document.getElementById("genre").value + "\n";
	application = application + "Processor: " + document.getElementById("processor").value + "\n";
	application = application + "RAM: " + document.getElementById("ram").value + "\n";
	application = application + "HDD Space: " + document.getElementById("hdd").value + "\n";
	application = application + "OS: " + document.getElementById("os").value + "\n";
	application = application + "Upload: " + document.getElementById("upload").value + "\n";
	application = application + "Download: " + document.getElementById("download").value + "\n";
	application = application + "Why do you want to DJ with HappyHits Radio? " + document.getElementById("whyus").value + "\n";
	application = application + "What other information would you like to tell us? " + document.getElementById("comment").value + "\n";
	
	pcomment = encodeItem("&comment",application);
	ptopic = encodeItem("&topic",document.getElementById("topic").value);
	datatopost = pname + pemail + psubject + pcomment + ptopic;
	AJAX("POST","../libs/managecontact.php?action=submitcomment",datatopost,"true",parse);
}

/**
	This function is called from the public comment page and adds a
	guitar auction form to the comment system.
*/
function submitBid(){
	//A series of checks on the form to make sure it is valid.
	if(document.getElementById("name").value == ""){
		alert("You must fill in your name before submitting a bid.");
		return;
	}
	if(document.getElementById("email").value == ""){
		alert("You must fill in your email address before submitting  a bid.  It will be used only to follow up regarding your bid.");
		return;
	}
	if(document.getElementById("address").value == ""){
		alert("You must fill in your address before you can make a bid.");
		return;
	}
	if(document.getElementById("phone").value == ""){
		alert("You must fill in your phone number before submitting a bid.  It will be used only to follow up regarding your bid if email contact is impossible.");
		return;
	}
	if(document.getElementById("testbid").value != "I AGREE"){
		alert("You must agree to the statement regarding your bid before you can place a bid.");
		return;
	}
	//All tests were passed, time to submit the comment.
	var parse = function(returnedXML){
		outcome = returnedXML.getElementsByTagName("outcome")[0].childNodes[0].nodeValue;
		message = returnedXML.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		if(outcome == "001"){
			//It was successful, send back to the main page.
			alert('Thank you for your bid.  You will find out after the auction has ended.');
			window.location = 'http://www.happyhitsradio.com/';
		}
		else{
			alert(message);
		}
	}
	pname = encodeItem("name",document.getElementById("name").value);
	pemail = encodeItem("&email",document.getElementById("email").value);
	psubject = encodeItem("&subject","Guitar Bid");
	var application = "";
	application = application + "Name: " + document.getElementById("name").value + "\n";
	application = application + "Email: " + document.getElementById("email").value + "\n";
	application = application + "Mailing Address: " + document.getElementById("address").value + "\n";
	application = application + "Phone Number: " + document.getElementById("phone").value + "\n";
	application = application + "Bid: " + document.getElementById("bid").value + "\n";
	pcomment = encodeItem("&comment",application);
	ptopic = encodeItem("&topic",document.getElementById("topic").value);
	datatopost = pname + pemail + psubject + pcomment + ptopic;
	AJAX("POST","../libs/managecontact.php?action=submitcomment",datatopost,"true",parse);
}

function updateForm(value){
	if(value == "Want to DJ?"){
		document.getElementById("divcomment").style.display = "none";
		document.getElementById("divapplication").style.display = "block";	
	}
	else{
		document.getElementById("divcomment").style.display = "block";
		document.getElementById("divapplication").style.display = "none";
	}
}

function markRead(id){
	var readrow = document.getElementById("b" + id);
	if(readrow.firstChild.className == "unreadmessage"){
		var parse = function(returnedXML){
			readrow = readrow.firstChild;
		    while(readrow){
		        readrow.setAttribute('class', 'readmessage');
				//For IE
				readrow.className = "readmessage";
        		readrow = readrow.nextSibling;
		    }
		}
		AJAX("GET","../libs/managecontact.php","?action=markread&id=" + id,"true",parse);	
	}
}

function myComments(username){
	listMessages(username);
}

function allComments(){
	listMessages("null");
}

function transfer(id){
	var user = document.getElementById("users").value;
	if (user == ""){
		alert("You need to select a user to transfer the comment to.");
		return;
	}
	var parse = function(returnedXML){
		outcome = returnedXML.getElementsByTagName("outcome")[0].childNodes[0].nodeValue;
		message = returnedXML.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		if(outcome != "001"){
			alert(message);
		}
	}
	AJAX("GET","../libs/managecontact.php","?action=transfer&user=" + user + "&id=" + id,"true",parse);	
}

function deleteComment(id){
	var parse = function(returnedXML){
		outcome = returnedXML.getElementsByTagName("outcome")[0].childNodes[0].nodeValue;
		message = returnedXML.getElementsByTagName("message")[0].childNodes[0].nodeValue;
		if(outcome != "001"){
			alert(message);
		}
	}
	AJAX("GET","../libs/managecontact.php","?action=delete&id=" + id,"true",parse);	
}