
// Given form name, updates "query" field based on "searchBy"
// Currently should be only be used for textbook form
function updateQuery(formName){
  eval("form = document."+formName);
  
    switch(form.searchBy.value){ 
      case "courseName": 
      	defaultQuery = "PSYCH 101";
      	document.textbookForm.action = "http://www.uwtools.com/books/books.cgi";
      	break;
      case "bookTitle": 
      	defaultQuery = "Calculus For Dummies"; 
      	document.textbookForm.action = "http://www.uwtools.com/books/index.cgi";
      	break;
      case "bookIsbn": 
      	defaultQuery = "0393310906"; 
      	document.textbookForm.action = "http://www.uwtools.com/books/index.cgi";
      	break;
      default: defaultQuery = "";
    }
    form.Keywords.value = defaultQuery;
    form.defaultQuery.value = defaultQuery;
    form.Keywords.style.color = "#808080";
}

// Given form name, clears default query
function clearKeywords(formName){
  eval("form = document."+formName);
  if(formName == "restaurantSearchForm"){
  		if(form.q.value == form.defaultQuery.value){
    		form.q.value = "";
    	}
    	form.q.style.color = "#000000";
  } 
  else {
  	if(form.Keywords.value == form.defaultQuery.value){
   	form.Keywords.value = "";
  	}
  	form.Keywords.style.color = "#000000";
  }
  
}

// Given form name, resets query to default 
function resetKeywords(formName){
  eval("form = document."+formName);
  if(formName == "restaurantSearchForm"){
  		if(form.q.value == ""){
    		form.q.style.color = "#808080";
    		form.q.value = form.defaultQuery.value;
    	}
  } else if(form.Keywords.value == ""){
    form.Keywords.style.color = "#808080";
    form.Keywords.value = form.defaultQuery.value;
  }
}

function fillFormDefaults(){
  updateQuery('textbookForm');
  resetKeywords('wikiForm');
  resetKeywords('profReviewForm');
  //resetKeywords('trackerForm');
  //resetKeywords('restaurantSearchForm');
}

// body onload...
function init(){
	ReadUrlTool();
   fillFormDefaults();
   loadBoardPostings();
}


// <div class="bboardMessageTitle"><a href="" style="color:#63319C">ADULT PARTICIPANTS NEEDED for hearing research</a></div>
function loadBoardPostings() {
	// BBoardPostings
	
		httpRequest('/content/content.cgi?cmd=display&tag=bboard/main'
				+'&output=<tr id="bBoardRow_[i]" style="display:none"><td id="bBoard_[i]"><div class="bboardMessageTitle"><a href="javascript: ShowPostingDetail([i])">[0][1]</a></div></td></tr>'
				+'&date='+Date(), // don't cache this result

		function(respText) { 
				
			document.getElementById('BBoardPostings').innerHTML = 
				'<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center" id="bBoardTable">'
				+respText
				+'</table>';
				
			displayBoardPostings(15); // # of posts to display

		});
	
}

// display last n postings
// use n = -1 to display all
function displayBoardPostings(n) {
	var theTB = (document.getElementById('bBoardTable')).tBodies.item(0);
	
	if( n < 0 ) {
		n = theTB.rows.length;
		document.getElementById('bBoardShowAll').style.display="none";
	}
	
	for( i = theTB.rows.length - n; i < theTB.rows.length; i++ ){
		theTB.rows.item(i).style.display="block";
	}
}

var origHTML = new Array(); // store orig. posting detail in ShowPostingDetail

function ShowPostingDetail(i){
	
	origHTML[i] = document.getElementById("bBoard_"+i).innerHTML;
	
	httpRequest('/content/content.cgi?cmd=display&tag=bboard/main'
			+'&output=<div class="bboardMessageSelected"><b>[0][1]</b><br><div class="bboardMessageDetail">[2]'
			+'<div class="bboardColorBar"><img src="spacer.gif" width="1" height="1"></div>'
			+'<b><a href="javascript: HidePostingDetail('+i+')" class="bBoardTitleLink">[collapse]</a> <a href="javascript: RemovePosting('+i+')" class="bBoardTitleLink">[delete]</a></b>'
			+'</div></div>'
			+'&row='+i
			+'&date='+Date(), // don't cache this result

	function(respText) { 
		
		document.getElementById("bBoard_"+i).innerHTML = respText;

	});
	
}

function HidePostingDetail(i){
	document.getElementById("bBoard_"+i).innerHTML = origHTML[i];
}

function RemovePosting(i){

	confirm1 = confirm("PERMANENTLY REMOVE this posting from the page?");
	if (confirm1) {
		
		confirm2 = prompt("Are you SURE this posting should be removed? If so, type \"delete\" below.", "");
		if(confirm2.toLowerCase() == "delete"){
			document.location='http://www.uwtools.com/content/content.cgi?cmd=delete&tag=bboard/main'
												+'&row='+i+'&redirect=http://www.uwtools.com/index.html';
		}
		
	}

}


// handle the "Create Posting" onclick
function showCreatePostingForm(){
	document.getElementById("BBoardCreatePosting").innerHTML = 
		'<table width="100%" cellpadding="0" cellspacing="0" border="0" align="center">'
		+'<tr><td colspan="2"><div class="boldedLargeTextBox">Create Posting</div></td></tr>'
		+'<tr><td bgcolor="#808080" colspan="2"><img src="spacer.gif" width="1" height="1"></td></tr>'
		+'<tr><td class="boldedNormalTextBox"><nobr>Title:</nobr></td><td width="100%" class="normalTextBox">'
				+'<table cellpadding=0 cellspacing=0 border=0><tr><td><select name="A0">'
				+'<option value="SEEKING: ">SEEKING:</option>'
				+'<option value="FOR SALE: ">FOR SALE:</option>'
				+'<option value="EVENT: ">EVENT:</option>'
				+'<option value="FREE: ">FREE:</option>'
				+'<option value="LOST: ">LOST:</option>'
				+'<option value="FOUND: ">FOUND:</option>'
				+'<option value="">(blank)</option>'
				+'</select></td><td width="100%"><input name="A1" type="text" maxlength="45" style="width: 100%"></td></tr></table></td></tr>'
		+'<tr><td class="boldedNormalTextBox" valign="top"><nobr>Details:</nobr></td><td width="100%" class="normalTextBox"><textarea name="A2" rows="8" style="width: 100%"></textarea></td></tr>'
		+'<tr><td></td><td class="boldedNormalTextBox"><input type="submit" class="button" value="Create Posting"></td></tr>'
		+'</table>';
}

function ReadUrlTool(){
	var urlParts = location.href.split("index.html?");
	if(urlParts.length > 1){
		if(urlParts[1] == "RequestsForm"){
			RequestsForm();
		}
		else{
			Tool(urlParts[1]);
		}
	}
}

function Tool(toolid){
	if(document.getElementById(toolid)){
		document.getElementById('AllTools').insertBefore(document.getElementById(toolid),document.getElementById('AllTools').firstChild);
	}
}

function RequestsForm(){
	
	document.getElementById('AllTools').insertBefore(document.getElementById('RequestsForm'),document.getElementById('AllTools').firstChild);
	document.getElementById('RequestsForm').style.display="block";
	
}
