
function showAddRecordByComputer(title) {
	Modalbox.show('/patient/medical-records/add-records-by-computer.html', {title: title, width:450});
	return false;
}

function showAddRecordByFax(title) {
	Modalbox.show('/patient/medical-records/add-records-by-fax.html', {title: title, width:450});
	return false;
}

function showRequestRecords(title) {
	Modalbox.show('/patient/medical-records/request-records.html', {title: title, width:450});
	return false;
}

function showSendRecord(title, selectedContactId, selectedRecordId) {
	Modalbox.show('/patient/communications/send-records.html?selectedContactId=' + selectedContactId + '&selectedRecordId=' + selectedRecordId, {title: title, width:450});
	return false;
}

function showAddFacility(title, zip) {
    if (zip == undefined) {
        zip = null;
    }    
	Modalbox.show('/facility-locator.html?zip=' + zip, {title: title, width: 740, height: viewPortHeight()});
	return false;
}

function showEditFacility(title, facilityId) { 
 	Modalbox.show('/patient/facility-details.html?facilityId=' + facilityId, { title: title, width: 650, height: auto });
	return false;
}

function showChangePassword(title) {
	Modalbox.show('/patient/account-setup/change-password.html', {title: title, width: 450});
	return false;
}

function showChangePasswordComplete(title, pass, newpass1, newpass2) {
	Modalbox.show('/patient/account-setup/change-password-complete.html?pass=' + pass + '&newpass1=' + newpass1 + '&newpass2=' + newpass2, {title: title, width: 450});
	return false;
}

function showMessageDetail(title, id) {
	Modalbox.show('/patient/communications/message-detail.html?messageId=' + id, {title: title, width:650});
	return false;
}

function showModifyContact(title, id) {
	Modalbox.show('/patient/communications/modify-contact.html?id=' + id, {title: title, width:650});
	return false;
}

function showForgotPassword(title, step, address, securityAnswer) {
	Modalbox.show('/forgot-password-step' + step + ".html?emailAddress=" + address + "&securityAnswer=" + securityAnswer, {title: title, width: 450});
	return false;
}

function showRiskAssessment(title) {
	Modalbox.show('/patient/health-tools/breast-health/risk-assessment.html', {title: title, width: 650, height: viewPortHeight()});
	return false;
}

function showRiskAssessmentDetail(title, riskAssessmentId) { 
 	Modalbox.show('/patient/health-tools/breast-health/risk-assessment-detail.html?riskAssessmentId=' + riskAssessmentId, {title: title, width: 650, height: viewPortHeight()}); 
	return false;
}

function showBreastExam(title, examdate) {
	Modalbox.show('/patient/health-tools/breast-health/self-exam.html?examdate=' + examdate, {title: title, width: 650, height: viewPortHeight()});
	return false;
}

function showRegistrationComplete(title) {
	Modalbox.show('/patient-register-complete.html', {title: title, width: 450, overlayClose: false, closeString: '', closeValue: ''});
	return false;
}

function showGlucoseReading(title, id) {
	Modalbox.show('/patient/health-tools/diabetes-management/new-reading.html?id=' + id, {title: title, width:350});
	return false;
}

function showCreateReport(title) {
	Modalbox.show('/patient/create-report.html', {title: title, width: 550});
	return false;
}

function showCreateReportComplete(title, folderName, reportId) {
	Modalbox.show('/patient/create-report-complete.html?folderName=' + folderName + '&reportId=' + reportId, {title: title, width: 350});
	return false;
}

function showConfirmDeleteContact(title, id) {
	Modalbox.show('/patient/communications/delete-contact.html?contactId=' + id, {title: title, width:350});
	return false;
}

function showConfirmDeleteAlert(title, id) {
	Modalbox.show('/patient/communications/delete-alert.html?alertId=' + id, {title: title, width:350});
	return false;
}

function showConfirmDeleteSentItem(title, id) {
	Modalbox.show('/patient/communications/delete-sent-item.html?itemId=' + id, {title: title, width:350});
	return false;
}

function showPrivacyPolicy(title) {
	Modalbox.show('/privacy-policy.html', {title: title, width: 800});
	return false;
}

function showSecurityPolicy(title) {
	Modalbox.show('/security-policy.html', {title: title, width: 800});
	return false;
}

function showTermsOfUse(title) {
	Modalbox.show('/terms-of-use.html', {title: title, width: 800, height: viewPortHeight()});
	return false;
}

function showHelp(link, title)
{    
	Modalbox.show(link, {title: title, width: 800});
	return false;
}  

function addDaysForDropdown(daysDropdown, selectedDay) {
	for (i=1; i<=31; i++)
		daysDropdown.options.add(new Option(i, i, false, (i==selectedDay)));
}

function addYearsForDropdown(yearsDropdown, selectedYear) {
	var currentYear = new Date().getFullYear();
	for (i=currentYear - 18; i >= 1930; i--)
		yearsDropdown.options.add(new Option(i, i, false, (i==selectedYear)));
}

function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return unescape(pair[1]);
		}
	} 
}

function setSelectedValue(selectbox, value) {
	for (var i = 0; i < selectbox.options.length; i++) {
		if (selectbox.options[i].value == value){
			selectbox.selectedIndex = i;
			return;
		}
	}
}

function createReportRequest() {
	var dateRangeType = Form.getInputs('create-report-form','radio','dateRangeType').find(function(radio) { return radio.checked; }).value;
	
	new Ajax.Request("/patient/create-report-data.html", {
		method: 'post',
		parameters: {
			recordsSummary: $("recordsSummary").checked,
			sentItemsSummary: $("sentItemsSummary").checked,
			contactsSummary: $("contactsSummary").checked,
			diabetesSummary: $("diabetesSummary").checked,
			diabetesTimeframe: $("diabetesTimeframe").options[$("diabetesTimeframe").selectedIndex].value,
			breastSummary: $("breastSummary").checked,
			breastReportType: $("breastReportType").options[$("breastReportType").selectedIndex].value,
			dateRangeType: dateRangeType,
			fromDate: $("fromDate").value,
			toDate: $("toDate").value,
			saveToFolder: $("saveToFolder").options[$("saveToFolder").selectedIndex].value
		},
		onSuccess: function(response) {
			var data = JSON.parse(response.responseText);
			if (data.success != true) {
				if (data.missingFromDate == true) alert("Please enter a valid 'from' date.");
				else if (data.missingToDate == true) alert("Please enter a valid 'to' date.");
				else alert("There was a problem creating the report");
			}
			else {
				showCreateReportComplete("Report Complete", data.folderName, data.reportId);
			}
		},
		onFailure: function (response) {
			alert("Report Data Error: " + response.status);
		}
	});
}

function setHomePageNews(type) {
	new Ajax.Request(
		"/news-data.html",
		{
			method: "post",
			parameters: {
				type: type
			},
			onSuccess: function(response) {
				var data = JSON.parse(response.responseText);
				
				$("news").update("");
				for (var i = 0; i < data.news.length; i++) {
					var li = new Element("li");
					if (i == data.news.length-1) li.className = "end";
					
					var a  = new Element("a", { href: data.news[i].url, target: "_blank" }).update(data.news[i].title);
					li.update(a);
					$("news").appendChild(li);
					
				}
			},
			onFailure: function(response) {
				alert("News Error:" + response.status);
			}
		}
	);
}

function sort(column, selectedName, page) {
	var id = getQueryVariable(selectedName);
	var lastSort = getQueryVariable("sort");
	var ascending = getQueryVariable("asc");
	
	if (lastSort == column && ascending == "true") ascending = "false";
	else if (lastSort == column && ascending == "false") ascending = "true";
	else ascending = "true";
	
	var url = page + '?' + '&sort=' + column + '&asc=' + ascending;
	if (id != null) url += '&' + selectedName + '=' + id; 
	
	//this section applies to diabetes readings only
	var days = $('numberOfDays');
	if (days) {
		days = $F(days);
		url += '&numberOfDays=' + days;
	}
	//
	
	location.href = url;
}

function toggleRole(role) {
	var e = $('homeFlashImage');
	e.className = role;
	var a = $$('#content #homeFlash div#roleSelect ul li a.tab');
	for (var i = 0; i < a.length; i++) {
		if (a[i].parentNode.id==role) {
			a[i].className = 'tab active';
			a[i].parentNode.className = 'active';
		} else {
			a[i].className = 'tab inactive';
			a[i].parentNode.className = 'inactive';
		}
	}
	
	if (role=='homeFlashPatients'){  
            setHomePageNews("Patient");
    } else if (role=='homeFlashProfessionals'||role=='homeFlashPayers'){ 
            setHomePageNews("Professional"); 
    } 
}

function setText(control,title) 
{    
   control.value =  control.value.length<=0 ? title : control.value ; 
} 

function viewPortHeight() {
	var viewportwidth;
	var viewportheight;
	if (typeof window.innerWidth != 'undefined') {
	     viewportwidth = window.innerWidth,
	     viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined'
	    && typeof document.documentElement.clientWidth !=
	    'undefined' && document.documentElement.clientWidth != 0) {
	      viewportwidth = document.documentElement.clientWidth,
	      viewportheight = document.documentElement.clientHeight
	} else {
	      viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
	      viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return viewportheight;
}

function assignCalendar(itemId, iconId) {
	
	var dpck = new DatePicker(
		{ 
			relative: itemId, 
			language: 'en', 
			externalControl: iconId, 
			keepFieldEmpty: true, 
			zindex: 50000, 
			enableShowEffect: false, 
			enableCloseEffect: false,
			enableCloseOnBlur: false
		}
	);
	dpck.setDateFormat([ "mm", "dd", "yyyy" ], "/");
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}


