﻿var initialText = "Please enter your doctor’s last name";
var manualText = "Please enter your doctor’s name manually";

// Setup Page functionality
$j(function() {
	// Constants
	var threshold = 1;
	var maxResults = 20;


	// Input elements
	var doctor = $j("input[type=text][id*=Doctor_txt]:first");
	var city = $j("input[type=text][id*=City_txt]:first");
	var zip = $j("input[type=text][id*=Zip_txt]:first");
	var state = $j("select[id*=State_ddl]:first");

	// Set initial text
	// note - may move to getting this from the element and have the server get the text from sitecore
	// this is disabled.
	//doctor.attr("value", initialText);

	// Autocomplete
	var invokeCounter = 0;
	doctor.keyup(function() {
		if (invokeCounter >= threshold) {
			// if there is more then one word in the doctor field, search using only the last word
			// incase the user begins by entering "dr" or the doctor's first name.
			var searchText = doctor.attr("value");
			searchText = searchText.substring(searchText.lastIndexOf(' '), searchText.length);

			Genzyme.layouts.DoctorInformationService.SearchForDoctors(
				searchText, city.attr("value"), zip.attr("value"), state.attr("value"), maxResults, onSearchComplete, onSearchError
			);
			invokeCounter = 0;
		}
		++invokeCounter;
		$j("input[type=hidden][id*=DoctorId]:first").attr("value", "");
		return false;
	}).keypress(function(e) {
		// hide drop down on esc key
		if (e.keyCode == 27) { $j(".autocomplete").hide("fast"); }
	});

	// clear initial text
	doctor.focus(function() {
		ClearDoctorInstuctions(doctor);
		return false;
	});

	// enable not listed doctor link
	$j("#not-listed").click(function() {
		$j('.autocomplete').hide("fast");
		doctor.unbind("keyup");
		//doctor.attr("value", manualText);
		$j("input[type=hidden][id*=DoctorId]:first").attr("value", "");
		return false;
	});


	// set inline style to prevent default bright red
	$j("span.required").css("color", "");

	// When the submit button is clicked, clear text from doctor field for validation
	$j("button:first").mousedown(function(e) {
		ClearDoctorInstuctions(doctor);
		return false;
	});

});

function onSearchComplete(result) {
	if (result.length > 0) {
		var body = $j("#search-results");

		var html = "";
		var dr = undefined;
		for (var i = 0; i < result.length; ++i) {
			dr = result[i];
			html += "<tr class='doctor-row' onclick='SelectDoctor(\"" + dr.Name + "\", \"" + dr.Id + "\")'>";
			html += "<td>" + dr.Name + "</td>";
			html += "<td>" + dr.City + ", " + dr.State + "</td>";
			html += "</tr>";
		}

		body.html(html);

		//ie6 row hover
		$j("#search-results tr").hover(
			function() { $j(this).addClass("hover"); },
			function() { $j(this).removeClass("hover"); }
		);

		$j(".autocomplete").show("fast")
	} else {
		$j(".autocomplete").hide("fast");
	}
}

function onSearchError(result) {
	$j(".autocomplete").hide("fast");
	$j("input[type=text][id*=Doctor_txt]:first").unbind("keyup");
	$j("input[type=hidden][id*=DoctorId]:first").attr("value", "");
	//alert(result._exceptionType + ":\n" + result._message + "\n StackTrace:" + result._stackTrace);	
}

function SelectDoctor(name, id) {
	$j("input[type=text][id*=Doctor_txt]:first").attr("value", name);
	$j("input[type=hidden][id*=DoctorId]:first").attr("value", id);
	$j(".autocomplete").hide("fast");
}


function ValidateEmail(source, arguments) {
	var selected = $j("input[type=checkbox][id*=ReceiveEmailReminders_chk]:first").attr("checked");

	if (selected) {
		var text = $j("input[type=text][id*=EmailAddress_txt]:first");
		arguments.IsValid = ("" != text.attr("value"));
	}
}



function ValidateDueDate(source, arguments) {
	var minDueDate = new Date();
	minDueDate.setTime(0);
	var maxDueDate = new Date();
	// don't forget then month setFullYear is 0 indexed, unlike day or year
	maxDueDate.setFullYear(9999, 11, 31);
	maxDueDate.setHours(0, 0, 0, 0);
	var dueDate = Date.parse($j("input[type=text][id*=ExpectedDueDate_txt]:first").attr("value"));
	arguments.IsValid = (dueDate >= minDueDate && dueDate <= maxDueDate);
}



function ClearDoctorInstuctions(doctor) {
	if (doctor.attr("value") == initialText || doctor.attr("value") == manualText) {
		doctor.attr("value", "");
	}
}