
function login_event(result) 
{
	try
	{
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");
		cursor_clear();
		eval("var loginarray = " + result);
		if (loginarray['error_code'] != "0") 
		{
			_DBG(2,"error number = "+loginarray['error_code']);
			if (loginarray['error_code'] == "3") 
			{
				_DBG(2,"invalid username");
				document.getElementById("message_text").innerText = "Invalid Username/Password";
			}
			
			if (loginarray['error_code'] == "999") 
			{
				_DBG(2,"invalid user type");
				alert("This User is not compatible - please contact Support with your User details");
				//document.getElementById("message_text").innerText = "Invalid Password";
			}
		} 
		else  
		{
			session_id = loginarray['session_id'];
			_DBG(2,"Login successful [session = " +session_id+"]" );
			popUpWindow(g_app_path+'popup.php?session_id='+session_id+(g_debug?"&debug":""), 100, 100, 780, 520);
		}
	}
    catch(ex)
    {			
	    _DBG(0,ex.message, document.URL,document.lastModified, document.referrer);
    }
}


function email_password_event(result) 
{
	try
	{
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");
		eval("var loginemailarray = " + result);
		if (loginemailarray['error_no']) 
		{
			if (loginemailarray['error_no'] == "251") 
			{
				document.getElementById("forgot_message_text").innerText = "Invalid email format";
			} 
			else if (loginemailarray['error_no'] == "255") 
			{
				document.getElementById("forgot_message_text").innerText = "Email address does not exist";
			}
			else if (loginemailarray['error_no'] == "0") 
			{
				document.getElementById("forgot_message_text").innerText = "Email has been sent to your email address. Press Login to return";			
			}	 
		} 
		else 
		{
			document.getElementById("forgot_message_text").innerText = "Email has been sent to your email address. Press Login to return";
		}
	}
    catch(ex)
    {	
		_DBG(0,ex.message, document.URL,document.lastModified, document.referrer);
    }
}


function get_user_event(result) 
{
	try
	{		
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");	
		alert(result); 
		eval("var get_user_result = " + result);

		if(get_user_result['sequence_id'] < g_sequence_id)
		{
			_DBG(1,"Redundant response.. ignoring");
			return;
		}

		if (get_user_result["error_code"] != "0") 
		{
			alert(translate_token("error_fetching_user_information"));
		} 
		else 
		{
			g_user				  = get_user_result['user'];
		}


		//init_timeframes();
		//init_language();


	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}


function get_report_event(result)
{
	try
	{			
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");	
		eval("var get_report_result = " + result);
		_DBG(2,"parse complete");

		

		if((get_report_result['sequence_id'] < g_sequence_id) && 
		   ((get_report_result['target_div'] == 'report_content_div') ||
		    (get_report_result['target_div'] == 'journeyDetails')))
		{
			_DBG(1,"Redundant response.. ignoring");
			show_loading_bar(false);			
			return;
		}
		


		var el = null;
		if(document.getElementById(get_report_result['target_div']))
		{
			_DBG(1,"target div is on main window");
			el = document.getElementById(get_report_result['target_div']);
		}
		else if(printWindow.document.getElementById(get_report_result['target_div']))
		{
			_DBG(1,"target div is on print window");
			el = printWindow.document.getElementById(get_report_result['target_div']);
		}
		else
		{
			_DBG(1,"target div for report doesnt exist:"+get_report_result['target_div']);
			return;	
		}		
		

		if(!is_visible(el))
		{
			_DBG(1,"Report parent div ["+el.id+"] is no longer viewable ["+el.style.display+"] - clearing content");			
			el.innerHTML = "";
			return;			
		}

		if(get_report_result['error_code'] != "0")
		{
			el.innerHTML = get_report_error_content();
			return;
		}

		_DBG(2,"offset="+get_report_result['offset']);		
		// if this is the first chunk then we need to reset the target div content
		if(get_report_result['offset'] == 0)
		{
			_DBG(2,"this is the first chunk...");			
			el.innerHTML	  = get_report_result['report'];
		}
		else
		{
			var d = document.createElement("div");
			d.innerHTML = get_report_result['report'];
			el.appendChild(d);
		}	
				
		// if we still have more chunks to retrieve then make the next request
		if(get_report_result['offset'] < (get_report_result['total'] - 1))
		{		
			g_report_offset = get_report_result['offset'] + 1;
			run_report(false);
		}
		else
		{
			// we are done - reset the offset counter and update the display	
			show_loading_bar(false);
			if((get_report_result['target_div'] != 'report_content_div') && (get_report_result['target_div'] != 'journeyDetails'))
			{
				new Effect.BlindDown(el,{duration:0.20});				
			}			
			g_report_offset  = 0;
		}
	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}

function show_print_report_event(result)
{
	try
	{			
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");	
		eval("var print_report_result = " + result);


		if((print_report_result['sequence_id'] < g_sequence_id))
		{
			_DBG(1,"Redundant response.. ignoring");
			if(printWindow)printWindow.document.getElementById("print_progress_div").innerHTML = "";
			return;
		}
		
		var el = null;
		
		if(printWindow)
		{
			var el = printWindow.document.getElementById("print_report_div");
		}
		else
		{
			_DBG(1,"No print window available");
			return;
		}
		
		if(print_report_result['error_code'] != "0")
		{
			el.innerHTML = get_report_error_content();
			printWindow.document.getElementById("print_progress_div").innerHTML = "";
			return;
		}
		

		if(!is_visible(el))
		{
			_DBG(1,"Report parent div ["+el.id+"] is no longer viewable ["+el.style.display+"]- clearing content");
			el.innerHTML = "";
			printWindow.document.getElementById("print_progress_div").innerHTML = "";
			return;			
		}

		if(print_report_result['error_code'] != "0")
		{
			printWindow.document.getElementById("print_progress_div").innerHTML = get_report_error_content();
			return;
		}
		
		// if this is the first chunk then we need to reset the target div content
		if(print_report_result['offset'] == 0)
		{
			_DBG(2,"this is the first chunk...");			
			el.innerHTML = "";
		}

		el.innerHTML	  += print_report_result['report'];
				
		// if we still have more chunks to retrieve then make the next request
		if(print_report_result['offset'] < (print_report_result['total'] - 1))
		{		
			g_report_offset = print_report_result['offset'] + 1;
			var parameter_list_xml = build_parameter_xml();
			show_print_report(g_report_id,parameter_list_xml);
		}
		else
		{
			// we are done - reset the offset counter and update the display	
			printWindow.document.getElementById("print_progress_div").innerHTML = "";
			if((print_report_result['target_div'] != 'print_report_div')) new Effect.BlindDown(el,{duration:0.20});				
			
			g_report_offset  = 0;
		}

				
	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}




function email_report_event(result)
{
	try
	{			
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");
		// not much we can do here...

	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}

function get_poigroup_array_event(result)
{
	try
	{
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");	
		eval("var poigroup_array = " + result);

		if(poigroup_array['sequence_id'] < g_sequence_id)
		{
			_DBG(1,"Redundant response.. ignoring");
			return;
		}
	
		
		// populate the dropdown of poi groups
		var dropdown = document.getElementById("poi_group_filter_select");
		dropdown.options.length = 0;

		// add the all pois item
		g_poigroup_array	= poigroup_array['group_array'][i];
		var el				= document.createElement("option");
		el.value			= "0";
		el.innerHTML		= translate_token("all_pois");
		dropdown.appendChild(el);
		
		for (var i = 0; i < poigroup_array['group_array'].length; i++)
		{
			g_poigroup_array = poigroup_array['group_array'][i];
			var el = document.createElement("option");
			el.value		= poigroup_array['group_array'][i]["id"];
			el.innerHTML	= poigroup_array['group_array'][i]["name"];
			
			if(g_poi_group_id_filter == poigroup_array['group_array'][i]["id"])
			{
				el.selected = true;
			}
			
			dropdown.appendChild(el);
		}
		
		document.getElementById("run_report_button").disabled = false;
	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}



function get_driver_array_event(result)
{
	try
	{
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");	
		eval("var driver_array = " + result);

		if(driver_array['sequence_id'] < g_sequence_id)
		{
			_DBG(1,"Redundant response.. ignoring");
			return;
		}
	
		
		// populate the dropdown of poi groups
		var dropdown = document.getElementById("driver_select");
		dropdown.options.length = 0;

		// add the all pois item		
		g_driver_array		= driver_array['item_array'];



		if(driver_array['item_array'].length > 0)	
		{
			var el				= document.createElement("option");
			el.value			= "0";
			el.innerHTML		= translate_token("all_drivers");
			if(g_driver_id == 0) g_driver_name = el.innerHTML;
			dropdown.appendChild(el);
			
			for (var i = 0; i < driver_array['item_array'].length; i++)
			{
				if(driver_array['item_array'][i]['type'] != "4") continue;
				var el = document.createElement("option");
				el.value		= driver_array['item_array'][i]["id"];
				el.innerHTML	= driver_array['item_array'][i]["name"];
				
				if(g_driver_id == driver_array['item_array'][i]["id"])
				{
					el.selected = true;
				}
				dropdown.appendChild(el);
			}		

			document.getElementById("run_report_button").disabled = false;
		}
		else
		{
			var el				= document.createElement("option");
			el.value			= "0";
			el.innerHTML		= translate_token("no_drivers");
			dropdown.appendChild(el);
			document.getElementById("run_report_button").disabled = true;
		}		
	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}


function get_asset_array_event(result)
{
	try
	{
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");	
		eval("var asset_array = " + result);

		if(asset_array['sequence_id'] < g_sequence_id)
		{
			_DBG(1,"Redundant response.. ignoring");
			return;
		}

		// populate the dropdown of poi groups
		var dropdown = document.getElementById("asset_select");
		dropdown.options.length = 0;

		//TODO: change to be assets...
		g_asset_array		= asset_array['driver_array'];

		if(asset_array['driver_array'].length > 0)	
		{
			var el				= document.createElement("option");
			el.value			= "0";
			el.innerHTML		= translate_token("all_assets");
			if(g_driver_id == 0) g_asset_name = el.innerHTML;
			dropdown.appendChild(el);
			
			for (var i = 0; i < asset_array['driver_array'].length; i++)
			{
				var el = document.createElement("option");
				el.value		= asset_array['driver_array'][i]["driver_id"];
				el.innerHTML	= asset_array['driver_array'][i]["name"];
				
				if(g_asset_id == asset_array['driver_array'][i]["driver_id"])
				{
					el.selected = true;
				}
				dropdown.appendChild(el);
			}		

			document.getElementById("run_report_button").disabled = false;
		}
		else
		{
			var el				= document.createElement("option");
			el.value			= "0";
			el.innerHTML		= translate_token("no_assets");
			dropdown.appendChild(el);
			document.getElementById("run_report_button").disabled = true;
		}		
	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}


function get_languagetoken_array_event(result)
{
	try
	{
		_DBG(2,_FUNC(arguments.callee)+":"+"entered");	
		eval("var temp_array = " + result);

//		if(temp_array['sequence_id'] < g_sequence_id)
//		{
//			_DBG(1,"Redundant response.. ignoring");
//			return;
//		}



		for (var i = 0; i < temp_array['token_item_array'].length; i++)
		{
			g_language[temp_array['token_item_array'][i]['token']] = temp_array['token_item_array'][i]['language_string'];
		}

		init_reportselector();
	}
	catch(ex)
	{
		_DBG(0,ex.message);
	}
}

