//
// javascript/misc.js
//
function Confirm(url, msg)
{
	if(window.confirm(msg) == true)
	{
		location.href = url;
	}
}

function confirmDelete(item1)
{
	agree = window.confirm("Are you sure you wish to delete this "+item1+"?");
	if(agree) return true;
	return false;
}

function updateView()
{
	var viewId = window.document.getElementById('viewId').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "viewId", viewId);
}

function updateCampus()
{
	var campus = window.document.getElementById('campus').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "campus", campus);
}

function updateSectionHeading()
{
	var sectionHeading = window.document.getElementById('sectionHeading').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "sectionHeading", sectionHeading);
}

function updatePreacher()
{
	var preacher = window.document.getElementById('preacher').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "preacher", preacher);
}

function updateYear()
{
	var year = window.document.getElementById('year').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "year", year);
}

function updateGroup()
{
	var groupId = window.document.getElementById('groupId').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "groupId", groupId);
}

function updateApproval()
{
	var approval = window.document.getElementById('approval').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "approval", approval);
}

function updateGroup()
{
	var groupId = window.document.getElementById('groupId').value;
	var fullHref = location.href;

	location.href = updateArgs(fullHref, "groupId", groupId);
}

function updateMonthYear()
{
	var monthYear = window.document.getElementById('monthYear').value;
	var monthYear1 = monthYear.split("-");
	var month = monthYear1[0];
	var year = monthYear1[1];
	var fullHref = location.href;

	fullHref = updateArgs(fullHref, "month", month);
	location.href = updateArgs(fullHref, "year", year);
}

function updateArgs(fullHref, arg, val)
{
	var i;
	var href1 = fullHref.split("?");
	var href2;
	var args = new Array();
	var found = 0;

	fullHref = fullHref.replace('#', '');
	// retrieve existing arguments (if any)
	if(href1.length > 1) args = href1[1].split("&");
	// replace view argument with new
	// value if already existing
	for(i=0; i<args.length; i++) {
		end = arg.length+1;
		if(args[i].substr(0, end) == arg+"=")
		{
			args[i] = arg+"="+val;
			found = 1;
		}
	}
	// add view argument if not in args list
	if(!found) args = args.concat(arg+"="+val);
	// reconstruct href
	href2 = href1[0]+"?"+args.join("&");
	// return new href
	return(href2);
}

// http://www.netlobo.com/div_hiding.html
function toggleLayer(whichLayer, defaultVal)
{
	if(document.getElementById)
	{
		// this is the way the standards work
		var style2 = document.getElementById(whichLayer).style;
		flipStyle(style2, defaultVal);
	}
	else if(document.all)
	{
		// this is the way old msie versions work
		var style2 = document.all[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
	else if(document.layers)
	{
		// this is the way nn4 works
		var style2 = document.layers[whichLayer].style;
		style2.display = style2.display? "":"block";
	}
}

function flipStyle(style, defaultVal)
{
	if(style.display == "none")
	{
		style.display = "block";
	}
	else if(style.display == "block")
	{
		style.display = "none";
	}
	else
	{
		style.display = defaultVal;
	}
}

function hide(div)
{
	var divStyle = document.getElementById(div).style;
	divStyle.display = "none";
}

function show(div)
{
	var divStyle = document.getElementById(div).style;
	divStyle.display = "block";
}

function checkAll(checkName) {
	var i;
	var len;
	var e;
	var checkVal;
	var rowId;
	var curTR;

	checkVal = document.getElementById("all"+tCaseWord(checkName)).checked;
	len = checkName.length;

	for(i=0; i<document.forms[0].elements.length; i++)
	{
		e = document.forms[0].elements[i];
		if(e.name.substr(0, len) == checkName && e.type=='checkbox')
		{
			e.checked = checkVal;
			rowId = e.id.substr(2);
			curTR = document.getElementById("r"+rowId);
			if(checkVal)
			{
				curTR.className = "highlighted"+(rowId%2);
			}
			else
			{
				curTR.className = "c"+(rowId%2);
			}
		}
	}
}

function tCaseWord(word)
{
	var len = word.length;
	if(len < 1) return("");

	var start = word.substring(0, 1).toUpperCase();
	if(len == 1) return(start);

	var end = word.substring(1, len).toLowerCase();
	return(start+end);
}

function updateCharCount()
{
	var msg = document.getElementById('message').value;
	
//	document.getElementById('charCount').innerHTML = msg.length+"/300";
	document.getElementById('charCount').innerHTML = msg.length+"/160";
}

// don't flip check back again if checkbox fires checkRow event
var allowRowCheck = 1;
function checkRow(rowId)
{
	var curTR = document.getElementById("r"+rowId);
	var style = curTR.style;
	var curCheck = document.getElementById("ch"+rowId);

// 	var isBorrow = (location.href.indexOf("borrows.php") > 0);
// 	var curTable = curTR.parentNode.parentNode;
// 	var tableStyle = curTable.style;

	if(allowRowCheck) curCheck.checked = !curCheck.checked;
	
	if(curTR.className == "highlighted"+(rowId%2))
	{
		curTR.className = "c"+(rowId%2);
	}
	else
	{
		curTR.className = "highlighted"+(rowId%2);
	}
}

function useRow(rowId)
{
	var curTR = document.getElementById("r"+rowId);
	var curCheck = document.getElementById("ch"+rowId);

	var idField = document.getElementById("assetId");
	var descriptionField = document.getElementById("description");
	var groupField = document.getElementById("group");
	var locationField = document.getElementById("location");
	var statusField = document.getElementById("status");
	var makeField = document.getElementById("make");
	var modelField = document.getElementById("model");
	var serialNumField = document.getElementById("serialNum");
	var acquiredField = document.getElementById("acquired");
	var priceField = document.getElementById("price");

	var idNew = curTR.childNodes[1];
	var descriptionNew = curTR.childNodes[3];
	var groupNew = curTR.childNodes[5];
	var locationNew = curTR.childNodes[7];
	var statusNew = curTR.childNodes[9];
	var makeNew = curTR.childNodes[11];
	var modelNew = curTR.childNodes[13];
	var serialNumNew = curTR.childNodes[15];
	var acquiredNew = curTR.childNodes[17];
	var priceNew = curTR.childNodes[19];
	
	idField.value = idNew.innerHTML;
	descriptionField.value = descriptionNew.innerHTML;
	groupField.value = groupNew.innerHTML;
	locationField.value = locationNew.innerHTML;
	statusField.value = statusNew.innerHTML;
	makeField.value = makeNew.innerHTML;
	modelField.value = modelNew.innerHTML;
	serialNumField.value = serialNumNew.innerHTML;
	acquiredField.value = acquiredNew.innerHTML;
	priceField.value = priceNew.innerHTML;

	checkRow(rowId);
	location.href = "#footer";
}

function disable(rowId)
{
	allowRowCheck = 0;
}

function enable(rowId)
{
	allowRowCheck = 1;
}

function addUser(comboNum)
{
	if(window.opener && !window.opener.closed)
	{
		var options = document.getElementById('addUserId').value.split(":");
		var userId = options[0];
		var userName = options[1];
		var option = window.opener.document.createElement("OPTION");
		option.text = userName;
		option.value = userId;
		var combo = window.opener.document.getElementById('userCombo'+comboNum);
		addToCombo(combo, option);
// 		combo.options.add(option);
	}
	window.close();
}

function addToCombo(combo, option)
{
	var i;
	var j;
	var len = combo.options.length;
	var optionName = option.text;
	var optionValue = option.value;
	var extraOpts = new Array();
	var foundLoc;
	
	// first item is null, just add to end straight away
	if(len == 0 || len == 1)
	{
		combo.options.add(option)
		// select new user
		selectComboOption(combo, len);
		return;
	}
	j = 0;
	foundLoc = -1;
	for(i=1; i<len; i++)
	{
		// don't add if already in list
		if(combo.options[i].text == optionName && combo.options[i].value == optionValue)
		{
			return;
		}
		// store in array if alphabetically after new option
		else if(combo.options[i].text > optionName)
		{
			if(foundLoc < 0) foundLoc = i;
			extraOpts[j] = combo.options[i];
			j++;
		}
	}

	// alphabetically last, so add to end
	if(foundLoc < 0)
	{
		combo.options.add(option);
		// select new user
		selectComboOption(combo, len);
	}
	else
	{
		// remove options alphabetically after new option
		combo.options.length = foundLoc;
		// add new option
		combo.options[foundLoc] = option;
		// add other options back
		for(i=0; i<j; i++)
		{
			combo.options[foundLoc+1+i] = extraOpts[i];
			combo.options[foundLoc+1+i] = extraOpts[i];
		}
		// select new user
		selectComboOption(combo, foundLoc);
	}
}

function selectComboOption(combo, optionNum)
{
	var i;
	for(i=0; i<combo.options.length; i++)
	{
		combo.options[i].selected = false;
	}
	combo.options[optionNum].selected = true;
}

function openUserCombo(taskId, comboNum)
{
	var url = "/userCombo.php?taskId="+taskId+"&comboNum="+comboNum;
	openPopup(url, "Select_from_all_users", 170, 700);
}

function openUploadProgress(uploadId)
{
	var url = "/progress.php?uploadId="+uploadId;
	openPopup(url, "Upload_Progress", 170, 1000);
}

// title cannot contain a space to satisfy IE
function openPopup(href, title, height, width)
{
	var options = "status=0, toolbar=0, menubar=0, directories=0, resizable=0, scrollbars=0, height="+height+", width="+width;
	window.open(href, title, options);
}

