function popupFullResults(name, url) {
	width = 800;
	height = 600;
	nwin = window.open(url, name, 'dependent=1, status=0, resizable=1, height='
		+ height + ',width=' + width + ',scrollbars=1, toolbar=1, location=0,'
		+ 'menubar=1,directories=0');
	nwin.moveTo((screen.availWidth - width) / 2, (screen.availHeight - height) / 2);
	if(window.focus) {
		nwin.focus();
	}
	return false;
}

function popupManageFiles(url) {
	width = 760;
	height = 330;
	nwin = window.open(url, 'manageFiles', 'dependent=1, status=0, resizable=1, height='
		+ height + ',width=' + width + ',scrollbars=1, toolbar=0, location=0,'
		+ 'menubar=0,directories=0');
	nwin.moveTo((screen.availWidth - width) / 2, (screen.availHeight - height) / 2);
	if(window.focus) {
		nwin.focus();
	}
	return false;
}

function popupManageImages(url) {
	width = 760;
	height = 330;
	nwin = window.open(url, 'images', 'dependent=1, status=0, resizable=1, height='
		+ height + ',width=' + width + ',scrollbars=1, toolbar=0, location=0,'
		+ 'menubar=0,directories=0');
	nwin.moveTo((screen.availWidth - width) / 2, (screen.availHeight - height) / 2);
	if(window.focus) {
		nwin.focus();
	}
	return false;
}

function confirmDelete() {
	var agree = confirm("Are you sure you want to delete?");
	if(agree)
		return true;
	else
		return false;
}

function confirmDeleteLease() {
	var agree = confirm("Deleting a lease will also remove all transactions for the lease.\nAre you sure you want to delete?");
	if (agree)
		return true;
	else
		return false;
}

function confirmDeleteUnit() {
	var agree = confirm("Deleting a unit will also remove all information related to the unit.\nAre you sure you want to delete?");
	if (agree)
		return true;
	else
		return false;
}

function confirmDeleteTx() {
	var agree = confirm("Deleting a transaction will also remove all payment associations.\nAre you sure you want to delete?");
	if (agree)
		return true;
	else
		return false;
}

function featureNotActive() {
	alert("This feature is not yet active.")
	return false;
}

function confirmSave() {
	var agree = confirm("Are you sure you want to save this report?");
	if(agree)
		return true;
	else
		return false;
}

function confirmDeprecated() {
	var agree = confirm("This feature has been deprecated and will be removed in the future.\nDo you want to continue?");
	if (agree)
		return true;
	else
		return false;
}

function unauthorized() {
	alert("You do not have sufficient rights to perform this operation.");
	return false;
}

function setCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if(nDays==null || nDays==0) nDays=1;
		expire.setTime(today.getTime() + 3600000*24*nDays);
		document.cookie = cookieName+"="+escape(cookieValue)
				+ ";expires="+expire.toGMTString();
}

function getCookie(name) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if(( !start) && (name != document.cookie.substring( 0, name.length))){
		return null;
	}
	if(start == -1)
		return null;
	var end = document.cookie.indexOf( ";", len );
	if (end == -1)
		end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function toggleHistory(anchor) {
	var div = document.getElementById('history');
	if(anchor.on != null) {
		if(anchor.on == true) {
			anchor.on = false;
			anchor.removeChild(anchor.firstChild);
			var txt = document.createTextNode('[+] History');
			anchor.appendChild(txt);
			div.style.display = "none";
			setCookie("history", "off", 5);
		}
		else {
			anchor.on = true;
			anchor.removeChild(anchor.firstChild);
			var txt = document.createTextNode('[-] History');
			anchor.appendChild(txt);
			div.style.display = "block";
			setCookie("history", "on", 5);
		}
	}
	else {
		if(getCookie("history") == "on") {
			anchor.on = true;
			anchor.removeChild(anchor.firstChild);
			var txt = document.createTextNode('[-] History');
			anchor.appendChild(txt);
			div.style.display = "block";
		}
		else {
			anchor.on = false;
			anchor.removeChild(anchor.firstChild);
			var txt = document.createTextNode('[+] History');
			anchor.appendChild(txt);
			div.style.display = "none";
		}
	}
}

function refreshSelects() {
    var selects = document.getElementsByTagName("select");
    var multipleSelects = new Array();
    for(var i = 0; i < selects.length; i++) {
        if(selects[i].multiple)
            multipleSelects.push(selects[i]);
    }
    for(var i = 0; i < multipleSelects.length; i++) {
        //Find the first selected="selected" and set it to selected again
        //this is to fix IE issues
        for(var j = 0; j < multipleSelects[i].options.length; j++) {
            if(multipleSelects[i].options[j].selected) {
                multipleSelects[i].options[j].selected = true;
                break;
            }
        }
    }
}

function setCheckboxes(form, check) {
	for (var i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == 'checkbox') {
			form.elements[i].checked = check;
		}
	}	
}