function requestItem(type, id) {
	var url = root + "/ajax/borrow.php?confirmation=yes&type=" + type + "&"+type+"ID=" + id;
	new Ajax.Request(url, {method: 'get', onComplete:function(res){eval(res.responseText);}});
}
function returnItem(type, id) {
	var url = "ajax/return.php?ReturnedType=" + type + "&id=" + id;
	new Ajax.Request(url, {method: 'get', onComplete:function(res){
		if (res.responseText == "1") 
			Modalbox.newshow("<div style='font-weight:bold;color:red;margin-top:8px;font-size:16px;'>The item has been returned.</div>", {title: 'Item Returned', width: 700, height: 120, afterHide: function(){window.location.reload();}});
		else
			Modalbox.newshow("<div style='font-weight:bold;color:red;margin-top:8px;font-size:16px;'>You have already indicated that you returned this item, please contact the lender to confirm that they have received it, or <a href='mailto:info@neighborrow.com' style='font-size:16px;'>CONTACT US</a>.</div>", {title: 'Item Returned', width: 700, height: 120, afterHide: function(){}});
	}});
}
function showErrorMessage(messages, successHolderId, errorHolderId) {	
	if ($(successHolderId) && $(errorHolderId)) {
		$(errorHolderId).innerHTML = messages;
		$(successHolderId).innerHTML = "";
	}

	if ($('errorMessage')) {
		$('errorMessage').innerHTML = messages;
	}
	else alert(messages);
	if ($('successMessage')) {
		$('successMessage').innerHTML = "";
	}
	
}
function gotoMessage(title, link, afterHide) {
	Modalbox.show(title, link, {width: 640, height:140, afterHide:afterHide});
}

function update(message, tobeHidden) {
	if ($(tobeHidden)) {
		$(tobeHidden).innerHTML = "";
	}
	if ($('successMessage')) {
		$('successMessage').innerHTML = message;
	} else alert(message);
}


function showSuccessMessage(messages, successHolderId, errorHolderId) {
	if ($(successHolderId) && $(errorHolderId)) {
		$(errorHolderId).innerHTML = "";
		$(successHolderId).innerHTML = messages;
	}
	if ($('errorMessage'))  $('errorMessage').innerHTML = "";
	if ($('successMessage')) {
		$('successMessage').innerHTML = messages;
	} else alert(messages);
}
function updateContainer(containerId, data) {
	if ($(containerId)) {
		$(containerId).innerHTML = data;
		data.extractScripts().map(function(script) { 
			return eval(script.replace("<!--", "").replace("// -->", ""));
		});
	}
}
function borrowItem(type, id, nid)
{
	//Modalbox.show('What do you want to do with this item?', root + "/quickborrowitem.php?type=" + type + "&" + type + "ID=" + id + "&neighborrowhoodID=" + nid, {width: 720, height:200});
	Modalbox.show('What do you want to do with this item?', "quickborrowitem.php?type=" + type + "&" + type + "ID=" + id + "&neighborrowhoodID=" + nid, {width: 720, height:200});
}
function showBorrowSuccessMessage() {
	Modalbox.show('Borrow Item', "ajax/messages/borrowSuccess.html", {width: 640, height:140});
}
function sendingJoinRequest(neighborrowhoodId) {
	var url = "ajax/joinRequest.php?task=add&neighborrowhoodID=" + neighborrowhoodId;
	new Ajax.Request(url, {method: 'get', onComplete:function(res){eval(res.responseText);}});
}
function sendingAcceptRequest(requestId) {
	var url = "ajax/joinRequest.php?task=accept&requestID=" + requestId;
	new Ajax.Request(url, {method: 'get', onComplete:function(res){updateContainer("divJoinRequest", res.responseText);updateTotalMessage();}});
}
function sendingRefuseRequest(requestId) {
	var url = "ajax/joinRequest.php?task=refuse&requestID=" + requestId;
	new Ajax.Request(url, {method: 'get', onComplete:function(res){updateContainer("divJoinRequest", res.responseText);updateTotalMessage();}});
}
function sendingDeleteRequest(requestId) {
	var url = "ajax/joinRequest.php?task=delete&requestID=" + requestId;
	new Ajax.Request(url, {method: 'get', onComplete:function(res){updateContainer("divJoinResponse", res.responseText);updateTotalMessage();}});
}

function lendItem(type, id) {
	Modalbox.show('Lend This Item', 'ajax/lendThis.php?type='+type+'&itemID=' + id, {width: 540, height:190});	
}

function submitForm(url, formId) {
	var valid = new Validation(formId, {onSubmit:false});
    var result = valid.validate();
	if (result) {
		new Ajax.Request(url, {method: 'post', postBody: $(formId).serialize(), onComplete:function(res){
			var response = new String(res.responseText);
			response.extractScripts().map(function(script) { 
				return eval(script.replace("<!--", "").replace("// -->", ""));
			});				
		}});
	}
	else {
		
	}
}

function assignItem(type, id) {
	Modalbox.show('Assign This Item', 'ajax/assignThis.php?type='+type+'&itemID=' + id, {width: 540, height:320});	
}
function replyWishlist(type, id) {
	Modalbox.show('Reply Wishlist', 'ajax/replyWishList.php?type='+type+'&itemID=' + id, {width: 540, height:180});
}

function renewItem(type, id) {
	Modalbox.newshow("<p align=\"center\" style=\"magin:0; font-size:114%;font-weight:bold;color:red;\">Do you want to renew this item?</p><p align=\"center\"><a href=\"javascript:void(0);\" onclick=\"sendRenewRequest('"+type+"', '"+id+"');\">Yes</a><a href=\"javascript:void(0);\" onclick=\"Modalbox.close();\" style=\"margin-left: 86px;\">No</a></p>", {title: "Renew Item", width: 540, height: 160});
}
function sendRenewRequest(type, id) {
	Modalbox.show('Renew This Item', 'ajax/renewItem.php?type='+type+'&itemID=' + id, {width: 540, height:160});	
}