/*
 * CampusSHELF.com 
 * ©2005 Logan Marc
 */

// SORTING FUNCTIONS
// ***************** 
function sortTable(column){
    switch (column) {
        case "Author" :
            jsArray.sort(compareAuthor);
            break;
        case "Condition" :
            jsArray.sort(compareCondition);
            break;
        case "CourseName" :
            jsArray.sort(compareCourseName);
            break;
        case "CourseNum" :
            jsArray.sort(compareCourseNum);
            break;
        case "Date" :
            jsArray.sort(compareDate);
            break;
        case "Hits" :
            jsArray.sort(compareHits);
            break;
        case "ISBN" :
            jsArray.sort(compareISBN);
            break;                   
        case "Price" :
            jsArray.sort(comparePrice);
            break;
        case "School" :
            jsArray.sort(compareSchool);
            break;
        case "Seller" :
            jsArray.sort(compareSeller);
            break;
        case "Title" :
            jsArray.sort(compareTitle);
            break;    
        default:
        	break;
	}
}

//The following "compare" functions are comparators
//for Ascending, they should return +1 if a>b, -1 if a<b, 0 if a=b
//for Descending, they should return -1 if a>b, +1 if a<b, 0 if a=b
function compareAuthor(a, b) {
	x = a.Author.toLowerCase();
	y = b.Author.toLowerCase();
	
	return ((x > y) ? 1 : (x < y) ? -1 : compareHits(a, b));
}

function compareCondition(a, b) {
	aCond = a.Condition.toLowerCase();
	if (aCond == "fair")
		x = 4;
	else if (aCond == "good")
		x = 3;
	else if (aCond == "like new")
		x = 2;
	else if (aCond == "brand new")
		x = 1;
	else
		x = 5;
	
	bCond = b.Condition.toLowerCase();
	if (bCond == "fair")
		y = 4;
	else if (bCond == "good")
		y = 3;
	else if (bCond == "like new")
		y = 2;
	else if (bCond == "brand new")
		y = 1;
	else
		y = 5;
	
	return ((x > y) ? 1 : (x < y) ? -1 : compareHits(a, b));
}

function compareCourseName(a, b) {
	x = a.CourseName.toLowerCase();
	y = b.CourseName.toLowerCase();
	
	return ((x > y) ? 1 : (x < y) ? -1 : compareHits(a, b));
}

function compareCourseNum(a, b) {
	x = a.CourseNum.toLowerCase();
	y = b.CourseNum.toLowerCase();
	
	return ((x > y) ? 1 : (x < y) ? -1 : compareHits(a, b));
}

function compareDate(a, b) {
	var shownDateA, shownDateB;
	shownDateA += a.DateCreated.substring(0,8); //YYYYMMDD	
	shownDateB += b.DateCreated.substring(0,8); //YYYYMMDD		
	
	if (shownDateA > shownDateB)
		return -1;
	else if (shownDateA < shownDateB)
		return 1;
	else
		return compareTitle(a, b);	
}

function compareHits(a, b) {
	x = parseInt(a.Hits);
	y = parseInt(b.Hits);

	return ((x > y) ? -1 : (x < y) ? 1 : compareTitle(a, b));
}

function compareISBN(a, b) {
	x = a.ISBN.toLowerCase();
	y = b.ISBN.toLowerCase();
	
	return ((x > y) ? 1 : (x < y) ? -1 : 0);
}


function comparePrice(a, b) {
	x = parseInt(a.Price);
	y = parseInt(b.Price);

	return ((x > y) ? 1 : (x < y) ? -1 : compareHits(a, b));
}

function compareSchool(a, b) {
	x = a.School.toLowerCase();
	y = b.School.toLowerCase();
	
	return ((x > y) ? 1 : (x < y) ? -1 : compareHits(a, b));
}

function compareSeller(a, b) {
	x = a.Seller.toLowerCase();
	y = b.Seller.toLowerCase();
	
	return ((x > y) ? 1 : (x < y) ? -1 : 0);
}

function compareTitle(a, b) {
	x = a.Title.toLowerCase();
	y = b.Title.toLowerCase();
	
	return ((x > y) ? 1 : (x < y) ? -1 : 0);
}
// *****************

function updateMybooksTable(){
	for (row = 0; row < jsArray.length; row++){
		var formatedDate = '';
		formatedDate += jsArray[row]['DateCreated'].substring(5,7) + '/'; //Month
		formatedDate += jsArray[row]['DateCreated'].substring(8,10) + '/'; //Day
		formatedDate += jsArray[row]['DateCreated'].substring(0,4); //Year
		
		var formatedTitle = jsArray[row]['Title'];
		if (formatedTitle.length > 39)
			formatedTitle = jsArray[row]['Title'].substring(0,39) + ' ...';
		
		document.getElementById('Date' + row).innerHTML = formatedDate;	
		document.getElementById('ISBN' + row).innerHTML = jsArray[row]['ISBN'];
		document.getElementById('Title' + row).innerHTML = formatedTitle;
	}
}

function updateDiscussionsListTable(){
	for (row = 0; row < jsArray.length; row++){
		var formatedDate = '';
		formatedDate += jsArray[row]['DateCreated'].substring(5,7) + '/'; //Month
		formatedDate += jsArray[row]['DateCreated'].substring(8,10) + '/'; //Day
		formatedDate += jsArray[row]['DateCreated'].substring(0,4); //Year
		
		var formatedTitle = jsArray[row]['Title'];
		if (formatedTitle.length > 30)
			formatedTitle = jsArray[row]['Title'].substring(0,30) + ' ...';
		
		// Update visual fields
		document.getElementById('Date' + row).innerHTML = formatedDate;	
		document.getElementById('Seller' + row).innerHTML = jsArray[row]['Seller'];
		document.getElementById('Title' + row).innerHTML = formatedTitle;
		
		// Update hidden form fields
		document.getElementById('IDHidden' + row).value = jsArray[row]['ID'];
		document.getElementById('SellerHidden' + row).value = jsArray[row]['Seller'];
		document.getElementById('TitleHidden' + row).value = jsArray[row]['Title'];
		
		// highlight when discussions when necessary
		var newMessage = (jsArray[row]['Speaker'].toLowerCase() == jsArray[row]['Seller'].toLowerCase());
		if (parseInt(jsArray[row]['ClosedFlag']))
			document.getElementById('Title' + row).style.color = "red";
		else if (document.getElementById('NumItems') && newMessage)
			document.getElementById('Title' + row).style.color = "blue";
		else
			document.getElementById('Title' + row).style.color = "black";
	}
}

function updateWatchListTable(){
	for (row = 0; row < jsArray.length; row++){
		var formatedDate = '';
		formatedDate += jsArray[row]['DateCreated'].substring(5,7) + '/'; //Month
		formatedDate += jsArray[row]['DateCreated'].substring(8,10) + '/'; //Day
		formatedDate += jsArray[row]['DateCreated'].substring(0,4); //Year

		// The following was from before Vdeck 3.0 changed things
		//formatedDate += jsArray[row]['DateCreated'].substring(4,6) + '/'; //Month
		//formatedDate += jsArray[row]['DateCreated'].substring(6,8) + '/'; //Day
		//formatedDate += jsArray[row]['DateCreated'].substring(0,4); //Year
		
		var formatedTitle = jsArray[row]['Title'];
		if (formatedTitle.length > 30)
			formatedTitle = jsArray[row]['Title'].substring(0,30) + ' ...';
		
		document.getElementById('Date' + row).innerHTML = formatedDate;	
		document.getElementById('ISBN' + row).innerHTML = jsArray[row]['ISBN'];
		document.getElementById('Title' + row).innerHTML = formatedTitle;
		document.getElementById('ISBNHidden' + row).value = jsArray[row]['ISBN'];
	}
}

function checkSubmit(){
	for (row = 0; row < jsArray.length; row++){
		//if any checkbox is check return true, then return true.
		if (document.getElementById('select' + row).checked){
			return confirm("Are you sure you want to do this?");
		}
	}
	
	return false;
}

function flushCheckBoxes(){
	//sets all checkBoxes to false.
	for (row = 0; row < jsArray.length; row++){
		document.getElementById('select' + row).checked = false;
	}
}

function checkSelect(){
	var checked = '';
	var notchecked = '';
	for (row = 0; row < jsArray.length; row++){
		if (document.getElementById('select' + row).checked)
			checked = 'yes';
		else
			notchecked = 'yes';
		
		if (checked == 'yes' && notchecked == 'yes'){
			checked = 'change';
			break;
		}
	}
	
	if	(checked == 'change'){
		for (row = 0; row < jsArray.length; row++){
			if(!document.getElementById('select' + row).checked)
				document.getElementById('select' + row).checked = true;
		}
	}
	else{	
		for (row = 0; row < jsArray.length; row++){
			document.getElementById('select' + row).checked = !document.getElementById('select' + row).checked;
		}
	}
}

function updateSearchForm(string, school){
	if (document.quicksearch){
		document.quicksearch.string.value = string;
		document.quicksearch.school.value = school;
	}
}

function updateSearchResultsTable(){
	for (row = 0; row < jsArray.length; row++){
		// ROW 1
		if (jsArray[row]['Title'].length > 70)
			var formatedTitle = jsArray[row]['Title'].substring(0,70) + ' ...';
		else
			var formatedTitle = jsArray[row]['Title'];	
		document.getElementById('Title' + row).innerHTML = formatedTitle;

		var formatedISBN = '';
		formatedISBN += jsArray[row]['ISBN'].substring(0,1) + '-';
		formatedISBN += jsArray[row]['ISBN'].substring(1,4) + '-';
		formatedISBN += jsArray[row]['ISBN'].substring(4,9) + '-';
		formatedISBN += jsArray[row]['ISBN'].substring(9,10);
		document.getElementById('ISBN' + row).innerHTML = formatedISBN;

		document.getElementById('Condition' + row).innerHTML = jsArray[row]['Condition'];		
		document.getElementById('Price' + row).innerHTML = "$" + jsArray[row]['Price'];
		
		// ROW 2
		if (jsArray[row]['Author'] == "")
			document.getElementById('Author' + row).innerHTML = "[No Author]";
		else{
			var formatedAuthor = 'By: ' + jsArray[row]['Author'];
			document.getElementById('Author' + row).innerHTML = formatedAuthor;
		}
		
		document.getElementById('SchoolName' + row).innerHTML = jsArray[row]['SchoolName'];
				
		if (jsArray[row]['CourseNum'] == "")
			document.getElementById('CourseNum' + row).innerHTML = "[No Course #]";
		else				
			document.getElementById('CourseNum' + row).innerHTML = jsArray[row]['CourseNum'];
		
		if (jsArray[row]['CourseName'] == "")
			document.getElementById('CourseName' + row).innerHTML = "[No Course Name]";
		else{
			if (jsArray[row]['CourseName'].length > 40)
				var formatedCourseName = jsArray[row]['CourseName'].substring(0,40) + ' ...';
			else
				var formatedCourseName = jsArray[row]['CourseName'];	
			document.getElementById('CourseName' + row).innerHTML = formatedCourseName;
		}				
	}
}


function updateBookTable(){
	for (i = 0; i < jsArray.length; i++){
		// Restore data
		document.getElementById(jsArray[i].field).value = jsArray[i].message;
		
		// Change the color of fields with bad data
		if (!jsArray[i].success){
			document.getElementById(jsArray[i].field + '_row').style.color = "red";
			document.getElementById('Errors_row').innerHTML += jsArray[i]['error'] + '<br>';
		}

		// If in 'book' view and not in edit mode (aka submit button says Unlist)
		if (document.getElementById('SubmitButton') != null){	
			if (document.getElementById('SubmitButton').value == "Unlist")
				document.getElementById(jsArray[i].field).disabled = true;
			else
				document.getElementById(jsArray[i].field).disabled = false;
		}
	}
}

function updateTable(){
	for (i = 0; i < jsArray.length; i++){
		// Restore data
		document.getElementById(jsArray[i].field).value = jsArray[i].message;
		
		// Change the color of fields with bad data and display error message
		if (!jsArray[i].success){
			document.getElementById(jsArray[i].field + '_row').style.color = "red";
			if (jsArray[i].field == 'ActivateEmail')
				document.getElementById('Errors_row2').innerHTML += jsArray[i]['error'] + '<br>';	
			else if (jsArray[i].field != 'NewPassword2' && jsArray[i].field != 'PrimaryEmail2')
				document.getElementById('Errors_row').innerHTML += jsArray[i]['error'] + '<br>';	
		}
	}
}

function highlightSearchBook(num, mouseover){
	if (mouseover){
		document.getElementById('searchRow1_table' + num).style.background = "#C0C0C0";
		document.getElementById('searchRow2_table' + num).style.background = "#C0C0C0";
		document.getElementById('searchRow3_table' + num).style.background = "#C0C0C0";
	}
	else{
		document.getElementById('searchRow1_table' + num).style.background = "#FFFFFF";
		document.getElementById('searchRow2_table' + num).style.background = "#FFFFFF";
		document.getElementById('searchRow3_table' + num).style.background = "#FFFFFF";
	}
		
}

function highlightBook(num, mouseover){
	if (mouseover)
		document.getElementById('Book' + num).style.background = "#C0C0C0";
	else
		document.getElementById('Book' + num).style.background = "#FFFFFF";
}

function goToBook(row){
	 window.location="index.php?view=book&id=" + jsArray[row]['ID'];
}

function allowEdit(){
	var badData = false;
	for (i = 0; i < jsArray.length; i++){
		document.getElementById(jsArray[i].field).disabled = !document.getElementById(jsArray[i].field).disabled;
		if (!jsArray[i].success)
			badData = true;
	}
		
	if (document.getElementById('EditButton').value == 'Edit'){ //enter Edit Mode
		document.getElementById('EditButton').value = 'Cancel';	
		document.getElementById('SubmitButton').value = 'Update';
	} 
	else{ //exit Edit mode
		document.getElementById('EditButton').value = 'Edit'; 
		document.getElementById('SubmitButton').value = 'Unlist'; 
		if (badData)
			window.location.reload(false);
		else
			updateBookTable();
	}
}

function discuss(){
	// use the appropriate table depending on whether the form is 
	// embedded in a table aka the discussion has been started
	if (document.getElementById('discuss_form_table'))
		var table = document.getElementById('discuss_form_table');
	else
		var table = document.getElementById('discuss_table');

	// retract form
	if (table.name != "retracted"){
		table.name = "retracted";

		document.getElementById('Discuss_Message_row').style.display = "none";
		document.getElementById('Discuss_Submit_row').style.display = "none";
	}
	else{ // expand form
		table.name = "expanded";

		document.getElementById('Discuss_Message_row').style.display = "";
		document.getElementById('Discuss_Submit_row').style.display = "";
	
		document.discuss_form.Message.focus();
	}
}

function openDiscussion(buyer){
	var table = document.getElementById('discussion_table');
	var requestedID = "discussion_table_" + buyer;
	var active = document.getElementById("buyerActive_" + buyer).value;

	// retract discussion
	if (table.style.display != "none" && buyer == table.name){ 
		table.style.display = "none";
	}
	// expand discussion OR change discussion
	else{
		table.style.display = "";
		table.name = buyer;
		//set the active buyer for discussion posts
		if (document.getElementById('Discuss_Buyer_row'))
			document.getElementById('Discuss_Buyer_row').value = buyer;
					
		var numRows = table.rows.length;
		for (i = 0; i < numRows; i++){
			var row = table.rows.item(i);

			if (row.id == requestedID || row.id == requestedID + "_head" || row.id == "discuss_form_row")
				row.style.display = "";
			//else if (row.id == "discuss_form_row" && parseInt(active))
			//	row.style.display = "";
			else
				row.style.display = "none";
		}
		
		//retract discussion form (useful for change discussion)
		if (document.getElementById('discuss_form_table')){
			var form = document.getElementById('discuss_form_table');
			form.name = "retracted";
			document.getElementById('Discuss_Message_row').style.display = "none";
			document.getElementById('Discuss_Submit_row').style.display = "none";
			document.getElementById('Message').value = "";
		}
	}
}

function highlightLink(link, mouseover){
	if (mouseover)
		link.style.color = "blue";
	else
		link.style.color = "#009300";
}

function highlightNavigateLink(link, mouseover){
	if (mouseover)
		link.style.color = "#000000";
	else
		link.style.color = "#FFFFFF";
}

