function showTagRecord(title, id) {
	Modalbox.show('/patient/medical-records/tag-record.html?recordId=' + id, {title: title, width:450});
	return false;
}

function showDeleteRecord(title, id) {
	Modalbox.show('/patient/medical-records/delete-record.html?recordId=' + id, {title: title, width:450});
	return false;
}

function showEditUserRecord(title, id) {
	Modalbox.show('/patient/medical-records/user-record-details.html?recordId=' + id, {title: title, width:450});
	return false;
}

function showFacilityRecordDetails(title, id) {
	Modalbox.show('/patient/medical-records/facility-record-details.html?recordId=' + id, { title: title, width: 550 });
	return false;
}


function showRecordDetails(title) {
	var selectedRadio = Form.getInputs('facilityForm','radio','recordId').find(function(radio) { return radio.checked; });
	if (selectedRadio != null) {
		var recordId = selectedRadio.value;
		Modalbox.show('/patient/facility-record-details.html?recordId=' + recordId, { title: title, width: 650 });
	}
	else {
		alert("Please choose a Record.");
	}
	
	return false;
}

function tagRecord() {
	var tags = "";
	var tagBoxes = Form.getInputs("editRecord", "checkbox").findAll(function(check) { return check.checked == true });
	if (tagBoxes.length == 0) {
		alert("Please select at least one folder to save the record to.");
		return;
	}
	
	for (var i = 0; i < tagBoxes.length; i++) {
		if (tags.length > 0) tags += "|";
		tags += tagBoxes[i].value;
	}

	new Ajax.Request('/patient/medical-records/tag-record.html', {
		method: 'post',
		parameters: {
			recordId: $('recordId').value,
			recordType: $('recordType').value,
			tags: tags
		},
		onSuccess: function(response) {
			Modalbox.hide();
			location.href = location.pathname;
		},
		onFailure: function(response) {
			alert("Save Error: " + response.status);
		}
	});
}

function swapFolderBox() {
	var tb = new Element("input", {"type": "text", "name": "tag", "id": "tag"});
	$("tagHolder").update("").appendChild(tb);
	$('comment').focus(); // must divert focus somewhere else in IE or else tag will not focus.
	$('tag').focus();
}

function addTag() {
	if ($("newTag").value != "") {
		var tagList = $("tagList");
		var li = new Element("li");
		var check = new Element("input", {type: "checkbox", value: $("newTag").value, checked: true});
		li.insert(check);
		li.insert(" " + $("newTag").value);
		tagList.insert(li);
		Modalbox.resizeToContent();
		$("newTag").value = "";
	} else {
		alert("Please enter a folder name.");
		$("newTag").focus();
	}
}



function deleteRecord() {
	new Ajax.Request('/patient/medical-records/delete-record.html', {
		method: 'post',
		parameters: {
			recordId: $('recordId').value
		},
		onSuccess: function(response) {
			location.href = location.pathname;
		},
		onFailure: function(response) {
			alert("Delete Error: " + response.status);
		}
	});
}



function saveFacilityRecord(facilityId, recordId) {
	new Ajax.Request('/patient/medical-records/facility-record-details.html', {
		method: 'post',
		parameters: {
			recordId: recordId,
			notes: $('facililtyRecordNotes').value
		},
		onSuccess: function(response) {
			location.href = location.pathname+'?facilityId='+facilityId;
		},
		onFailure: function(response){
			alert("Facility Record Error: " + response.status);
		}
	});
}

function saveUserRecord() {
	new Ajax.Request('/patient/medical-records/user-record-details.html', {
		method: 'post',
		parameters: {
			recordId:  $('recordId').value,
			examDate: $('examDate').value,
			imageType: $('imageType').value,
			comment: $('comment').value
		},
		onSuccess: function(response) {
			var data = JSON.parse(response.responseText);
			if (data.success) location.href = location.pathname;
			else if (data.dateMissing) $('dateRequired').style.visibility = "visible";
		},
		onFailure: function(response){
			alert("User Record Error: " + response.status);
		}
	});
}

function sendRecord(title) {
	var contact  = $("contact").options[$("contact").selectedIndex].value;
	var recordId = $("recordId").options[$("recordId").selectedIndex].value;
	if (contact == -1 ) {
		alert("Please choose a contact.");
		return;
	}
	if (recordId == -1) {
		alert("Please choose a record.");
		return;
	}

	new Ajax.Request('/patient/communications/send-records.html', {
		method: 'post',
		parameters: {
			"contact": contact,
			"recordId": recordId,
			"note": $("note").value
		},
		onSuccess: function(response) {
			var data = JSON.parse(response.responseText);
			if (data.success)
				Modalbox.show('/patient/communications/send-records-complete.html', {title: title, width: 350});
			else {
				alert("There was a problem sending your records.  Please try again later.");
				Modalbox.hide();
			}
		},
		onFailure: function(response){
			alert("Send Record Error: " + response.status);
		}
	});
}


function requestRecord(name, email, date, records, sendTo, sendToAddress)
{
	$$("span.error").each(
		function(el) {
			el.className = "hidden";
		}
	);
	
	Modalbox.deactivate();
    new Ajax.Request(
        "/patient/medical-records/request-records.html",
        {
            method: "post",
            parameters: {
                name: name,
                email: email,
                examDate: date,
                records: records,
                sendTo: sendTo,
                sendToAddress: sendToAddress
            },
            onSuccess: function(response) {
            	Modalbox.activate();
            	var data = JSON.parse(response.responseText);
            	if (data.success == true) {
                	Modalbox.show("/patient/medical-records/request-records-complete.html", {title: "Request Records", width: 350});
                }
                else {
                	if (data.nameError == true) $("nameError").className = "error";
                	if (data.examDateError == true) $("examDateError").className = "error";
                	if (data.sendToError == true) $("sendToError").className = "error";
                }
            },
            onFailure: function() {
            	alert("Save Error:" + response.status);
                Modalbox.hide();
            }
        }
    );
}