/*
	* This function will get the specific report.
	*/
	function getOndemandReport()
	{
		var adminReportTypeId = document.getElementById('adminReportTypeId').value;
		var adminReportService = document.getElementById('adminReportService').value;
		if(adminReportTypeId == '0' || adminReportTypeId == '')
		{
			alert(select_report_type);
			return false;
		}
		else if(adminReportService == '0' || adminReportService == '')
		{
			alert(select_report);
			return false;
		}
		document.ReportsForm.method = "post";
		document.ReportsForm.action = adminReportService;
		document.ReportsForm.submit();
	}

	/*
	* This function will get the cities by passing the report type id for admin side(On demand report).
	*/
	function selectAdminReportType(reportTypeId)
	{
		var adminReportTypeId = reportTypeId.value;
		try
		{
			document.getElementById('adminReportTypeId').value = adminReportTypeId;
		}
		catch(err)
		{
		}
		if(adminReportTypeId == 0)
		{
			return false;
		}
		
		var xmlHttp = getXMLHTTP();
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState == 4)
				{
					document.getElementById('selectAdminCityDiv').innerHTML = xmlHttp.responseText;
					document.getElementById('selectAdminReportsDiv').innerHTML = "<select id='adminReports' style='WIDTH: 377px' name='adminReports' onchange='javascript:selectAdminReport(this);'><option value='0' >--SELECT REPORT--</option></select>"
					var cityId = 0;
					document.getElementById('adminCityId').value = cityId;
					selectAdminReportCity(cityId);

				}
			}
			xmlHttp.open("GET","/report/getCitiesByReportTypeId?mode=Admin&reportTypeId="+adminReportTypeId,true);
			xmlHttp.send(null);
		
	}

	/*
	* This function will get the cities by passing the report_type_id on schedulled report.
	*/
	function selectScheduledReportType(reportTypeId)
	{
		var scheduledReportTypeId = reportTypeId.value;
		try
		{
			document.getElementById('scheduledReportTypeId').value = scheduledReportTypeId;
		}
		catch(err)
		{
		}
		if(scheduledReportTypeId == 0)
		{
			return false;
		}
		
		var xmlHttp = getXMLHTTP();
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState == 4)
				{
					document.getElementById('selectScheduledCityDiv').innerHTML = xmlHttp.responseText;
					document.getElementById('selectScheduledReportsDiv').innerHTML = "<select id='scheduledReports' style='WIDTH: 235px' name='scheduledReports' onchange='javascript:selectScheduledReport(this);'><option value='0' >--SELECT REPORT--</option></select>";
					var cityId = 0;
					document.getElementById('scheduledCityId').value = cityId;
					selectScheduledReportCity(cityId);
				}
			}
			xmlHttp.open("GET","/report/getCitiesByReportTypeId?mode=Schedule&reportTypeId="+scheduledReportTypeId,true);
			xmlHttp.send(null);
	}

	/*
	* This function will get the reports by passing the report_type_id and city id for selecting the admin report.
	*/
	function selectAdminReportCity(city)
	{
		var cityId = city.value;
		try
		{
			document.getElementById('adminCityId').value = cityId;
			var adminReportTypeId = document.getElementById('adminReportTypeId').value;
		}
		catch(err)
		{
		}
		var xmlHttp = getXMLHTTP();
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState == 4)
				{
					document.getElementById('selectAdminReportsDiv').innerHTML = xmlHttp.responseText;
				}
			}
			xmlHttp.open("GET","/report/getReportsByReportTypeIdAndCity?mode=Admin&reportTypeId="+adminReportTypeId+"&cityId="+cityId,true);
			xmlHttp.send(null);
	}

	/*
	* This function will get the reports by passing the report_type_id and city id for selecting the scheduled
	* report.
	*/
	function selectScheduledReportCity(city)
	{
		var cityId = city.value;
		try
		{
			document.getElementById('scheduledCityId').value = cityId;
			var scheduledReportTypeId = document.getElementById('scheduledReportTypeId').value;
		}
		catch(err)
		{
		}
		var xmlHttp = getXMLHTTP();
			xmlHttp.onreadystatechange=function()
			{
				if(xmlHttp.readyState == 4)
				{
					document.getElementById('selectScheduledReportsDiv').innerHTML = xmlHttp.responseText;
				}
			}
			xmlHttp.open("GET","/report/getReportsByReportTypeIdAndCity?mode=Schedule&reportTypeId="+scheduledReportTypeId+"&cityId="+cityId,true);
			xmlHttp.send(null);
	}


	/*
	* This function will get the report service name and set into a hidden variable for on demand report.
	*/
	function selectAdminReport(report)
	{
		var reportName = report.value;
		try
		{
			document.getElementById('adminReportService').value = reportName;
		}
		catch(err)
		{
		}
	}

	/*
	* This function will get the report service name and set into a hidden variable for scheduled report.
	*/
	function selectScheduledReport(report)
	{
		var reportName = report.value;
		try
		{
			document.getElementById('scheduledReportService').value = reportName;
		}
		catch(err)
		{
		}
	}

	/*
	* This function will get the report service name and set into a hidden variable.
	*/
	function selectFrequencyValue(frequency)
	{
		var frequencyValue = frequency.value;
		document.getElementById('frequency').value = frequencyValue;
	}
	
	/*
	* This function will get the add scheduled report popup.
	*/
	function addScheduleReportPopup()
	{
		var x,y;
		if (self.innerHeight) // all except Explorer
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		
		var xmlHttp = getXMLHTTP();
		var url = "/report/addScheduleReportForm";
		if(xmlHttp) 
		{
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState == 4) 
				{
					if (xmlHttp.status == 200)
					{	
						var selectTag=document.getElementsByTagName("select");
						for(var i=0;i<selectTag.length;i++)
						{
							selectTag[i].style.display='none';
						}
						document.getElementById('AddScheduleWaterMark').style.width = x+"px" ;
						document.getElementById('AddScheduleWaterMark').style.height = (130+(y))+"px" ;
						document.getElementById('AddScheduleWaterMark').style.visibility="visible";
						document.getElementById('AddScheduleWaterMark').style.display="block";
						document.getElementById('AddScheduleWaterMark').innerHTML = xmlHttp.responseText;
					}
					else 
					{
						alert(addScheduleReport_page);
					}
				}
			}
			xmlHttp.open("GET", url , true);
			xmlHttp.send(null);
		}
	}
	
	/*
	* This function will validate the schedule report form and submit values.
	*/
	function validateScheduledReport(mode)
	{
		if(trim(document.getElementById('scheduledReportTypeId').value) == '' || trim(document.getElementById('scheduledReportTypeId').value) == '0')
		{
			alert(select_report_type);
			document.getElementById('reportType').focus();
			return false;
		}
		else if(trim(document.getElementById('scheduledReportService').value) == '' || trim(document.getElementById('scheduledReportService').value) == '0')
		{
			alert(select_report);
			document.getElementById('scheduledReports').focus();
			return false;
		}
		else if(trim(document.getElementById('reportSendTo').value) == '')
		{
			alert(select_atleast_one_email);
			document.getElementById('reportSendTo').focus();
			return false;
		}
		else if(!checkReportSendToEmail('reportSendTo'))
		{
			return false;
		}
		else
		{
			
			if(mode == 'Add')
			{
				document.AddScheduleReportForm.method = "post";
				document.AddScheduleReportForm.action = "/report/addScheduleReport";
				document.AddScheduleReportForm.submit();
			}
			else
			{
				document.UpdateScheduleReportForm.method = "post";
				document.UpdateScheduleReportForm.action = "/report/updateScheduleReport";
				document.UpdateScheduleReportForm.submit();
			}
			
		}
	}

	/*
	* This function will close or cancel the add schedule report page.
	*/
	function cancelScheduledReportPage(mode)
	{
		if(mode == 'Add')
		{
			document.getElementById('AddScheduleWaterMark').style.visibility = "hidden";
			document.getElementById('AddScheduleWaterMark').style.display = "none";
		}
		else
		{
			document.getElementById('EditScheduleWaterMark').style.visibility = "hidden";
			document.getElementById('EditScheduleWaterMark').style.display = "none";
		}
		var selectTag = document.getElementsByTagName("select");
		for(var i=0; i < selectTag.length; i++)
		{
			selectTag[i].style.display='block';
		}
	}

	/*
	* This function will display edit schedule report page by passing the schedule id.
	*/
	function editScheduleReport(scheduleId)
	{
		var x,y;
		if (self.innerHeight) // all except Explorer
		{
			x = self.innerWidth;
			y = self.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight) // Explorer 6 Strict Mode
		{
			x = document.documentElement.clientWidth;
			y = document.documentElement.clientHeight;
		}
		else if (document.body) // other Explorers
		{
			x = document.body.clientWidth;
			y = document.body.clientHeight;
		}
		
		var xmlHttp = getXMLHTTP();
		if(xmlHttp) 
		{
			xmlHttp.onreadystatechange = function()
			{
				if (xmlHttp.readyState == 4) 
				{
					if (xmlHttp.status == 200)
					{	
						var selectTag=document.getElementsByTagName("select");
						for(var i=0;i<selectTag.length;i++)
						{
							selectTag[i].style.display='none';
						}
						document.getElementById('EditScheduleWaterMark').style.width = x+"px" ;
						document.getElementById('EditScheduleWaterMark').style.height = (130+(y))+"px" ;
						document.getElementById('EditScheduleWaterMark').style.visibility="visible";
						document.getElementById('EditScheduleWaterMark').style.display="block";
						document.getElementById('EditScheduleWaterMark').innerHTML = xmlHttp.responseText;
					}
					else 
					{
						alert(editScheduleReport_page);
					}
				}
			}
			xmlHttp.open("GET","/report/editScheduleReportForm?scheduleId="+scheduleId,true);
			xmlHttp.send(null);
		}
	}

	/*
	* This function will display edit schedule report page by passing the schedule id.
	*/
	function deleteScheduleReport(scheduleId)
	{
		if(scheduleId != '')
		{
			var report = document.getElementById('schedule_'+scheduleId).value;
			var response = confirm('Are you sure to delete the '+report+' report');
			if(response)
			{
				document.ReportsForm.method = "post";
				document.ReportsForm.action = "/report/deleteScheduleReport?scheduleId="+scheduleId;
				document.ReportsForm.submit();
			}
			else
			{
				return false;
			}
		}
	}

	/*
	* This function will validate the multiple email addresses.
	*/
	function checkReportSendToEmail(fieldId)
	{
		var field = document.getElementById(fieldId);
		var reportSendToStr = field.value;
		var reportSendToArr = reportSendToStr.split(",");
		for(var i = 0; i < reportSendToArr.length; i++)
		{
			var emailStr = trim(reportSendToArr[i]);
			if (emailStr.search(/^\w+((-\w+)|(\.\w+))*\@[0-9A-Za-z]+((\.|-)[A-Za-z]+)*\.[A-Za-z]+$/) != -1)
			{
				if(i == reportSendToArr.length-1)
				{
					return true;
				}
			}
			else
			{
				alert(email_notvalid);
				field.value = "";
				field.focus();
				return false;
			}
		}
	}
