	function clearGapAlert(alertID)
	{		
			
			$.ajax({
		   type: "POST",
		   url: "clearGapAlert.php",
		   data: "alertID="+alertID,
		   success: function(msg){
				if(msg){
					$('#'+alertID).fadeOut('slow');
					
					//if(msg == 'no more alerts')
					//$('#alertImage').fadeOut('slow');
					
					
				}else{
					alert('Unable to clear alert');
				}
			
		   }
		 });
	}
	
	
	function clearAllGapAlerts(userID)
	{		
			
			$.ajax({
		   type: "POST",
		   url: "clearGapAlert.php",
		   data: "accountID="+userID,
		   success: function(msg){
				if(msg){
					
					//alert(msg);
					//$('#'+alertID).fadeOut('slow');
					
					//if(msg == 'no more alerts')
					//$('#newAlerts').fadeOut('slow');
					$('#gap_alerts').fadeOut('slow');
					
				}else{
					alert('Unable to clear alerts');
				}
			
		   }
		 });
	}	
	
	


	//This function takes a cnum and loads the county object
	function loadCounty(cnum)
	{

			$.ajax({
		   type: "POST",
		   url: "loadCounty.php",
		   data: "cnum="+cnum,
		   success: function(msg){
				if(msg){
					return true;
				}else{
					return false;
				}
			
		   }
		 });	
	
	}


	function getCounties()
	{		
		if(!$('#state').val())
		{
		   	var options='';
			   options +='<option value=""> - Select County - </option>';
			   $('#county').html(options);	
			   return;
		}


		$.ajax({
		   type: "POST",
		   url: "getCounties.php",
		   data: "state="+$('#state').val(),
		   success: function(msg){
				if(msg)
				{
					var counties =  eval("(" + msg + ")");
					var options='';
				   for (var i = 0; i < counties.length; i++)
				   {
					 options += '<option value="' + counties[i]['CountyNum'] + '">' + counties[i]['CountyName'] + '</option>';
				   }
				  $('#county').html(options);							
					
				}
			
		   }
		 });
	}



	function removeAlertEmail(alertEmailID,mon_id)
	{
		$.ajax({
		   type: "POST",
		   url: "alertEmails.php",
		  data: "alertEmailID="+alertEmailID+"&action=remove",
		   success: function(msg){
		  	 if(msg){
				
				getAlertEmails(mon_id);
		   	}
		   }
		 });		
	}


	function addAlertEmail(mon_id)
	{
		emailAddress = $('#alert_email_'+mon_id).val();
		if(emailAddress.length < 6){
			alert('Email address must be at least 8 characters long and include an @');
			return;
		}
		
		$.ajax({
		   type: "POST",
		   url: "alertEmails.php",
		  data: "emailAddress="+emailAddress+"&monitorID="+mon_id+"&action=add",
		   success: function(msg){
		  	 if(msg){
				
				getAlertEmails(mon_id);
		   	}
		   }
		 });		
	}


	function updateAlertEmail(alertEmailID,mon_id)
	{
		emailAddress = $('#'+alertEmailID).val();
		if(emailAddress.length < 6){
			alert('Email address must be at least 8 characters long and include an @');
			return;
		}
		
	
		$.ajax({
		   type: "POST",
		   url: "alertEmails.php",
		  data: "emailAddress="+emailAddress+"&alertEmailID="+alertEmailID+"&monitorID="+mon_id+"&action=update",
		   success: function(msg){
		  	 if(msg){
				alert('Email Updated');
				getAlertEmails(mon_id);
		   	}
		   }
		 });	
	}


	function getAlertEmails(monitor_id)
	{
		$.ajax({
		   type: "POST",
		   url: "alertEmails.php",
		  data: "monitor_id="+monitor_id+"&action=get",
		   success: function(msg){
		  	 if(msg){
				 //msg = emails
				$('#alertEmails').fadeIn('slow');
				$('#alertEmailsTable').html(msg);
		   	}
		   }
		 });		
	}

	
	function hideAlertEmails()
	{
		$('#alertEmails').fadeOut('slow');	
	}


	function hideQuickAdd()
	{
		$('#gapQuickAdd').fadeOut('slow');	
	}


var monitorTotal ='';
	function getMonitoringTotal()
	{ /*I prorbably should have made one catch all page for ajax calls instead of a page per each event...
		Will try to retroactively move the other ajax code to this page, time permitting*/

		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		   async: false,
		  data: "action=getMonitoringTotal",
		   success: function(msg){
		  	 if(msg){
				monitorTotal=msg;	
				//callback();
		   	}
		   }
		 });
		
	}


var monitorMax='';
	function getMonitoringMax()
	{
		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		   async: false,
		  data: "action=getMonitoringMax",
		   success: function(msg){
		  	 if(msg){
				monitorMax = msg;
				//callback();
		   	}
		   }
		 });		
	}






	function updateGapPlan(planID)
	{
		
		name = $('#'+planID+'_name').val();
		limit = $('#'+planID+'_monLimit').val();
		cost = $('#'+planID+'_cost').val();


		if(isNaN(parseFloat(cost)) || isNaN(parseFloat(limit)))
		{
		  alert('Cost and Limit Must Be Numeric');
		  return;
		}

		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		   async: false,
		  data: "action=updateGapPlan&planID="+planID+"&name="+name+"&limit="+limit+"&cost="+cost,
		   success: function(msg){
		  	 if(msg){
				alert(msg);
		   	}
		   }
		 });		
	}

	function removeGapPlan(planID)
	{
		
		var a = confirm("Removing this plan will reset the gap plan for any customer with it, are you sure?");
		if(a)
		{	
			$.ajax({
			   type: "POST",
			   url: "gapControlAction.php",
			   async: false,
			  data: "action=removeGapPlan&planID="+planID,
			   success: function(msg){
				 if(msg){
					alert(msg);
					window.location="gapControlPricing.php";
				}
			   }
			 });
		}
	}

	function addGapPlan()
	{
		
		name = $('#gapName').val();
		limit = $('#gapMonLimit').val();
		cost = $('#gapCost').val();		
		
		if(isNaN(parseFloat(cost)) || isNaN(parseFloat(limit)))
		{
		  alert('Cost and Limit Must Be Numeric');
		  return;
		}
		
		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		   async: false,
		  data: "action=addGapPlan&name="+name+"&limit="+limit+"&cost="+cost,
		   success: function(msg){
		  	 if(msg){
				alert(msg);
				window.location="gapControlPricing.php";
				
		   	}
		   }
		 });		
	}





	function addNewMonitor(monitor,monitor2,cnum,county,state,type)
	{
		getMonitoringTotal();
		getMonitoringMax();
		
		
		var answer = confirm('Currently using '+monitorTotal+' of '+monitorMax+' monitoring slots.\r\nWould you like to monitor\r\n\r\nMonitor: '+monitor+' '+monitor2+'\r\nCounty: '+county+'\r\nState: '+state+'\r\n\r\n');
		if(!answer)
		return;
		
		
		if(type=='bookpage')
		perams = "action=addMonitor&book="+escape(monitor)+"&page="+escape(monitor2)+"&cnum="+escape(cnum)+"&type="+type;
		else if(type=='instnum')
		perams = "action=addMonitor&instnum="+escape(monitor)+"&cnum="+escape(cnum)+"&type="+type;
		else
		perams = "action=addMonitor&lname="+escape(monitor)+"&cnum="+escape(cnum)+"&type="+type;
		
		
		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		  data: perams,
		   success: function(msg){
		  	 if(msg){
				
				var str = msg.split('::');				
				
				alert(str[0]);
		   	}
		   }
		 });		
	}


	/*
		Check that at least 3 characters are typed and that they aren't already monitoring this name
	*/
	var monitorType ='name'; //name is default when page loads
	
	function processMonitorRequest()
	{
		
		cnum 	= $('#county').val();
		fname 	= $('#monitor_first_name').val();
		lname 	= $('#monitor_last_name').val();
		instnum_val = $('#instnum').val();
		book_val 	= $('#book').val();
		page_val 	= $('#page').val();
				
		
		if(!cnum){
			alert('State and County must be selected');
			return;
		}
		
		if(monitorType == 'name'){ //name validation here
			if(lname.length < 3)
			{
				alert('Last Name/Company Name must be at least 3 characters');
				return;
			}
		}
		

		if(monitorType == 'instnum')
		{ //name validation here
			if(instnum_val.length < 8 || instnum_val.length > 10)
			{
				alert('Must provide an instrument number between 8-12 charecters in length');
				return;
			}
		}		
		
		if(monitorType == 'bookpage')
		{
			if(!book_val || !page_val)
			{
				alert('Must key in both a Book and Page');
				return;
			}
		}
		
		
		//$('#gapLoadingGif').fadeIn('fast');

		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		  data: "action=addMonitor&lname="+escape(lname)+"&fname="+escape(fname)+"&cnum="+cnum+"&instnum="+instnum_val+"&book="+book_val+"&page="+page_val+"&type="+monitorType,
		   success: function(msg){
		  	 if(msg){
				
				var str = msg.split('::');				
				
				alert(str[0]);
				
				if(str[0] == 'New Monitor Added'){
					//window.location='gapControl.php';
					if(str[1])
					$('#monitor_records').html(str[1]);
					
	
					getMonitoringTotal();
					getMonitoringMax();
					
$('#monListInfo').html('You have configured to monitor the following. Use the search tool above to add more. ('+monitorTotal+' of '+monitorMax+'</b> in use <a href="gapControlSettings.php" class="gaplink">upgrade</a>)');
					
				}
		   	}
		   }
		 });		
	}


	function showDiv(type)
	{
		monitorType = type;
		$('#add_monitor_'+type).fadeIn('slow');
	}
	
	function hideDiv(type)
	{
		document.getElementById('add_monitor_'+type).style.display='none';
	}


	function removeMonitor(monName, monID)
	{		
		var choice = confirm('Are you sure you want to remove this monitor - '+monName+'?');
		
		if(choice == true){
		   $.ajax({
			   type: "POST",
			   url: "gapControlAction.php",
			  data: "action=removeMonitor&monitorID="+monID,
			   success: function(msg){
				 if(msg){
					$('#monitor_records').html(msg);				 
				 	//alert(msg);
					//window.location='gapControl.php';
					
					getMonitoringTotal();
					getMonitoringMax();
					
$('#monListInfo').html('You have configured to monitor the following. Use the search tool above to add more. ('+monitorTotal+' of '+monitorMax+'</b> in use <a href="gapControlSettings.php" class="gaplink">upgrade</a>)');					
					
				}
			   }
			 });
		}

	}


	function gapAlertCheck()
	{
 		
		//return;
		
		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		   async: false,
		  data: "action=getNewAlerts",
		   success: function(msg){
		  	 if(msg){
				if(msg == 'Array'){
					$('#alertImage').fadeIn('slow');
				}
				//callback();
		   	}
		   }
		 });		
		
		
		setTimeout('gapAlertCheck()',60000);		
	}
	
	




function showSearchHistory(){
	$('#searchHistory').fadeIn('slow');
}

function hideSearchHistory(){
	$('#searchHistory').fadeOut('slow');
}

function searchHistory(){
	sname = $('#search_name').val();
	info = sname.split('::');
	p1 = info[0];
	cnum = info[1];
	search_date_val =  $('#search_date').val();
	loadCounty(cnum);
	window.open('nameSearch.php?nameType=2&searchType=PA&indexType=BOTH&p1='+p1+'&p2=&expandAll=on&startDate='+search_date_val+'&endDate=&itype=0&executeSearch=Execute+Search');
}




function changeMonitorStatus(status,monitorID,value){
	var x = confirm("Change monitor status to "+status);
	if(x)
	{
		$.ajax({
		   type: "POST",
		   url: "gapControlAction.php",
		   async: false,
		   data: "action=updateMonitor&monitorID="+monitorID+"&value="+value,
		   success: function(msg){
			     if(msg == 'limit reached'){
				 alert('You have reached your active limit. Please upgrade your account, or change the status of an existing monitor, to activate this one.');
				 }else{
				$('#monitor_records').html(msg);		
				 }
		   	}
		   
		 });	
	}	
}


