function SaveRoadMap(loggedIn)
{
	var answer = false;
	
	if(loggedIn)
	{
		answer = confirm("Saving this Road Map will replace any existing saved Road Maps. \n\nWould you like to continue?")
		if (answer)
			document.roadmapForm.submit();
	}
	else
	{
		alert("You must log in before saving road map.");
		location.href='/common/login.cfm?NextPage=' + encodeURIComponent('/RoadMaps/roadmap.cfm?roadmapid=' + roadMapId);
	}						
}					
		
function ShowAmazonBook(obj, productId)
{
	//$("#productDialog").dialog("close");  // this is not needed if dialog is modal
	$("#productDialog").dialog("option", "title", "LEI Book" );
	ShowDialog(obj, "/RoadMaps/amazonbookInfo.cfm?test=nb&productId=" + productId);
}
	
function ShowBook(obj, productId)
{
	//$("#productDialog").dialog("close");  // this is not needed if dialog is modal
	$("#productDialog").dialog("option", "title", "LEI Book" );
	
	var url = "/RoadMaps/bookInfo.cfm?productId=" + productId + GetBookQtyQueryString();
	
	if(roadMapId != 0)
		url = url + "&roadmapid=" + roadMapId;
		
	ShowDialog(obj, url);
}
		
function ShowWorkshop(obj, productId, productTypeId, isCompleted, eventId)
{
	//$("#productDialog").dialog("close");  // this is not needed if dialog is modal
	$("#productDialog").dialog("option", "title", "LEI Workshop" );
	
	var url = "/RoadMaps/workshopInfo.cfm?productId=" + productId + "&productTypeId=" + productTypeId;
			
	if(isCompleted == 1)
		url = "/RoadMaps/completedInfo.cfm?productId=" + productId + "&productTypeId=" + productTypeId;
				
	if(eventId != "")
		url = url + "&eventid=" + eventId;				
	
	if(roadMapId != 0)
		url = url + "&roadmapid=" + roadMapId;
		
	ShowDialog(obj,url);
}
		
function ShowInactive(obj, dTitle, productType)
{
	//$("#productDialog").dialog("close");  // this is not needed if dialog is modal
	$("#productDialog").dialog("option", "title", dTitle );

	var url = "/RoadMaps/inactiveInfo.cfm?productType=" + productType;

	ShowDialog(obj, url);
}
	
function ShowDialog(obj,urlToLoad)
{								
	var randomnumber = Math.floor(Math.random()*1000);
	var loadurl = urlToLoad + "&cb=" + randomnumber;

	$("#productDialog").load(loadurl, "", function (responseText, textStatus, XMLHttpRequest) {
		if (textStatus == "error") $("#productDialog").text("Error loading details");
		$("#productDialog").dialog("option", "position", ["center","center"] );
		$("#productDialog").dialog("open");
		});
}
		
function GetBookQtyQueryString()
{
	var i;
	var j;
	var qs = "";
	
	if(document.bookForm)
	{
		for(i=0; i<document.bookForm.elements.length; i++)
		{
			if(document.bookForm.elements[i].type=="text")
			{
				qs = qs + "&" + document.bookForm.elements[i].name + "=" + document.bookForm.elements[i].value;
			}
		}	
	}
	return qs;	
}
		
function ShowEmailForm(obj)
{
	$("#emailFormBody").html($("#emailFormContent").html());
	$("#emailDialog").dialog("open");
}		
		
function EmailRoadMap()
{			
	var toElem, toEmails, haveEmail, i;

	toElem = document.getElementById("emailDialog").getElementsByTagName('form')[0].getElementsByTagName('input')[0];
	if (toElem.value == "") {
		haveEmail = false;
	} else {
		toEmails = toElem.value.split(",");
		haveEmail = true;
		for (i = 0; i < toEmails.length; i++) {
			if (!IsValidEmail(toEmails[i])) haveEmail = false;
		}
	}

	if (!haveEmail) {
		alert("Please enter a valid 'To' email address.");
		return false;
	}
	
	//document.emailForm.submit();
	//return;
	
	var randomnumber = Math.floor(Math.random()*1000);	
	var url = "/roadmaps/emailroadmap.cfm?cb=" + randomnumber;
	
	if(roadMapId != 0) url = url + "&roadmapid=" + roadMapId;

	url = url + "&titlename=" + encodeURIComponent(titleName);

	$("#emailFormBody").load(url, { to: '' + toElem.value + ''},
			function (responseText, textStatus, XMLHttpRequest) {
					if (textStatus != "success") {
						msg = "<br /><br />There was an error sending your email<br /><br />";
						$("#emailFormBody#").html(msg);
					}
			});

	return;
}
		
function SubmitEmailOnEnter(e) {		
	var keynum;
	
	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}
	
	if(keynum == 13)
	{
		EmailRoadMap();
		return false;
	}
	else
	{
		return true;
	}
}
		
function IsValidEmail(emailAddress) 
{
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

    if(reg.test(emailAddress.replace(/^\s\s*/, '').replace(/\s\s*$/, '')) == false) {
		return false;
	}
	else
	{
		return true;
	}
}

function CreateTwoDigitValue(value)
{
	if(value < 10)
		return "0" + value;
	else
		return value;
}
			
function DisableOverlapingWorkshops()
{
	var chkBoxes = document.workshopRegForm.workshopOffering;
	var selectedDates = new Array();
	
	// Loop creates a list of dates that have been checked and/or dates that a checked workshop span
	for (var i=0; i < chkBoxes.length; i++)
	{
		if (chkBoxes[i].checked)
		{
			var selectedData = chkBoxes[i].value.split("|");
			selectedDates.push(selectedData[1]);
		
			if(selectedData[2] > 1)
			{						
				//Need to add 1 for every day over 1 to initial date and add to selectedDates array
				var dateData = selectedData[1].split("-");						
				var dt = new Date(dateData[0], dateData[1], dateData[2]);
				
				for(var y=1; y <= selectedData[2]-1; y++)
				{
					dt.setDate(dt.getDate() + y);
					var parsedDate = dt.getFullYear() + "-" + CreateTwoDigitValue(dt.getMonth()) + "-" + CreateTwoDigitValue(dt.getDate())
					ArrayPush(selectedDates, parsedDate);
				}
			}
		}
	}
			
	// Create an array of unique dates									
	var uniqueDates = FilterUnique(selectedDates);
	
	// Loop through all workshops and disable unchecked workshops that have same dates as selected dates
	if(uniqueDates.length > 0)
	{
		for (var x=0; x < chkBoxes.length; x++)
		{
			if (!chkBoxes[x].checked)
			{						
				for(var j=0; j < uniqueDates.length; j++)
				{
					var unselectedData = chkBoxes[x].value.split("|");
					
					if(unselectedData[2] > 1)
					{						
						//Need to add 1 for every day over 1 to initial date and add to selectedDates array
						var dateData = unselectedData[1].split("-");						
						var dt = new Date(dateData[0], dateData[1], dateData[2]);
						
						for(var z=0; z <= unselectedData[2]; z++)
						{
							dt.setDate(dt.getDate() + z);
							var parsedDate = dt.getFullYear() + "-" + CreateTwoDigitValue(dt.getMonth()) + "-" + CreateTwoDigitValue(dt.getDate())
							
							if(unselectedData[1] == parsedDate)
							{
								chkBoxes[x].disabled = true;
								break;
							}
							else
							{
								chkBoxes[x].disabled = false;
							}
						}
					}
					else
					{
						if(unselectedData[1] == uniqueDates[j])
						{
							chkBoxes[x].disabled = true;
							break;
						}
						else
						{
							chkBoxes[x].disabled = false;
						}
					}
					
					
				}
			}
		}								
	}
	else
	{
		// Nothing is checked so make sure all checked boxes are enabled
		for (var x=0; x < chkBoxes.length; x++)
		{
			chkBoxes[x].disabled = false;
		}
	}
	
}
		
function FilterUnique(arr){
	var a=[],i;arr.sort();
	for(i=0;i<arr.length;i++)
	{
		if(arr[i]!==arr[i+1])
		{
			a[a.length]=arr[i];
		}
	}
	return a;
}
							
function ArrayPush(arr, value){
	arr[arr.length+1]=value;
	
	return arr.length;
}

function CheckOrigination(formId) {
	fElem = document.getElementById(formId);
	if (location.search.search(/roadmap=yes/i) >= 0) {
		fElem.OrderOrigination.value = "roadmap";
	}
	fElem.action = "https://" + location.hostname + "/Events/Seminars/Registration.cfm";
	return;
}

function ValidateBookForm(productId) {
	var i, j;
	var isValid = false;
	var formElem = document.getElementById("bookForm"+productId);
	if (formElem) {
		with (formElem) {
			for(i = 0; i < elements.length; i++) {
				if (elements[i].type=="text") {
					if (elements[i].value > 0) {
						isValid = true;
						break;
					}
				}
			}
		}		
		if(!isValid) {
			alert("Please set a quantity for at least one book before clicking 'Purchase' button.");
			elements[0].focus();
		}
	}
	
	return isValid;	
}

