<!--
//  Title:       Region Summary
//  Developer:   Wade McDonough
//  Verion:      1.0
//  Date:        3 Nov 2006
//  Description: 
// 
/////////////////////////////

function transport_summary_onclick(x_button)
{
	if (x_button == 'apply')
	{
		var url = 'modules/transport/transport_summary.php';

		var param = 'filter_id='			+ document.getElementById('filter_id').value;
		param += '&filter_string_transport='	+ encodeURIComponent(document.getElementById('filter_string_transport').value);
		param += '&filter_string_agent='	+ encodeURIComponent(document.getElementById('filter_string_agent').value);
		param += '&filter_string_freight='	+ encodeURIComponent(document.getElementById('filter_string_freight').value);
		param += '&filter_client=' 			+ encodeURIComponent(document.getElementById('filter_client').value);
		param += '&filter_client_id=' 		+ document.getElementById('filter_client_id').value;
		param += '&filter_agent=' 			+ encodeURIComponent(document.getElementById('filter_agent').value);
		param += '&filter_agent_id=' 		+ document.getElementById('filter_agent_id').value;
		
		if (document.getElementById('filter_scanned').checked == true)
			param += '&filter_scanned=' 		+ 'checked';
		else
			param += '&filter_scanned=' 		+ '';

		if (document.getElementById('filter_pending').checked == true)
			param += '&filter_pending=' 		+ 'checked';
		else
			param += '&filter_pending=' 		+ '';
		
		if (document.getElementById('filter_accepted').checked == true)
			param += '&filter_accepted=' 		+ 'checked';
		else
			param += '&filter_accepted=' 		+ '';
		
		if (document.getElementById('filter_delivery').checked == true)
			param += '&filter_delivery=' 		+ 'checked';
		else
			param += '&filter_delivery=' 		+ '';
		
		if (document.getElementById('filter_pod').checked == true)
			param += '&filter_pod=' 		+ 'checked';
		else
			param += '&filter_pod=' 		+ '';
		
		if (document.getElementById('filter_complete').checked == true)
			param += '&filter_complete=' 		+ 'checked';
		else
			param += '&filter_complete=' 		+ '';
		
		if (document.getElementById('filter_cancelled').checked == true)
			param += '&filter_cancelled=' 		+ 'checked';
		else
			param += '&filter_cancelled=' 		+ '';
		
		// Change to MYSQL format
		var temp_date = document.getElementById('filter_from').value;
		if (temp_date != '')
		{
			temp_date = temp_date.substring(6) + '-' + temp_date.substring(3,5) + '-' + temp_date.substring(0,2);
		}		
		param += '&filter_from='	+ temp_date;
		
		var temp_date = document.getElementById('filter_to').value;
		if (temp_date != '')
		{
			temp_date = temp_date.substring(6) + '-' + temp_date.substring(3,5) + '-' + temp_date.substring(0,2);
		}		
		param += '&filter_to='	+ temp_date;
		// Refresh
		if (document.getElementById('filter_refresh').checked == true)
			param += '&filter_refresh=' 		+ 'checked';
		else
			param += '&filter_refresh=' 		+ '';

		var div = 'div_body';
		var callback = "transport_summary_onload();";
		ajax_processRequestPost(url,param,div,callback)
	}
	if (x_button == 'reset')
	{
		var url = 'modules/transport/transport_summary.php';

		var param = 'filter_id=' 				+ '';
		param += '&filter_string_transport=' 	+ '';
		param += '&filter_string_agent=' 		+ '';
		param += '&filter_string_freight=' 		+ '';
		param += '&filter_client=' 				+ '';
		param += '&filter_client_id=' 			+ '0';
		param += '&filter_agent=' 				+ '';
		param += '&filter_agent_id=' 			+ '0';
		param += '&filter_scanned=' 			+ 'checked';
		param += '&filter_pending=' 			+ 'checked';
		param += '&filter_accepted=' 			+ 'checked';
		param += '&filter_delivery=' 			+ 'checked';
		param += '&filter_pod=' 				+ 'checked';
		param += '&filter_complete=' 			+ 'checked';
		param += '&filter_cancelled='			+ '';
		param += '&filter_refresh='				+ '';

		// From
/*		var d = new Date();
		var day = d.getDate();
		var month = d.getMonth() + 1;
		var year = d.getFullYear();
		if (month == 1)
		{
			month = 12;
			year--;
		}
		else
		{
			month--;
		}
		if (month < 10)
		{
			month = '0' + month;	
		}
		if (day < 10)
		{
			day = '0' + day;	
		}
		param += "&filter_from="		+  year + "-" + month + "-" + day;
*/
		param += "&filter_from="		+  "";

		// To
/*		var d = new Date();
		var day = d.getDate();
		var month = d.getMonth() + 1;
		var year = d.getFullYear();
		if (month == 12)
		{
			month = 1;
			year++;
		}
		else
		{
			month++;
		}
		if (month < 10)
		{
			month = '0' + month;	
		}
		if (day < 10)
		{
			day = '0' + day;	
		}
		param += "&filter_to="		+ year + "-" + month + "-" + day;
*/
		param += "&filter_to="		+ "";

		var div = 'div_body';
		var callback = "transport_summary_onload();";
		ajax_processRequestPost(url,param,div,callback)
	}
	else if (x_button == 'select_apply')
	{
		if (!isSelected('select_action', 'Please select an item from \'With Selected\''))
			return false;
		
		// Test for at least one checked checkbox
		var checkbox_count = 0;
		var checkbox_string = '';
		for (var i=1; i<document.forms[0].elements.length; i++) 
		{
			// Look for check box matches
			if ((document.forms[0].elements[i].type == "checkbox") && (document.forms[0].elements[i].name == "checkbox_id[]")) 
			{
				if (document.forms[0].elements[i].checked == true)
				{
					checkbox_count++;
					checkbox_string += document.forms[0].elements[i].value + ',';
				}
			}
		}
		checkbox_string = checkbox_string.slice(0, -1)

		if (checkbox_count == 0)
		{
			alert('No items selected!\n\nPlease select at least one checkbox');
			return;
		}

		var with_selected = document.getElementById('select_action').value

		// Assign Client and OCR
		if (with_selected == 1)
		{
			if (!isSelected('select_template', 'Please select a \'Template\''))
				return false;
		
			var url = 'modules/transport/transport_assign_detail.php';

			var param = 'mode=assign';
			param += '&id=' 			+ checkbox_string;
			param += '&crop_id=' 		+ document.getElementById('select_template').value;

			var div = 'div_popup_1';
			var callback = "transport_assign_detail_onload('assign');";
			ajax_processRequestPost(url,param,div,callback)

			return false;
		}
		// Delete Jobs
		if (with_selected == 2)
		{
			var answer = confirm("This action will PERMANENTLY DELETE all select jobs\n\n Click OK to continue or CANCEL to quit.")
			if (answer)
			{
				ajax_showdiv('div_result');
		
				var url = 'modules/transport/transport_summary.php';
				var param = 'mode=delete';
				param += '&transport_id=' 				+ checkbox_string;
	
				var div = 'div_body';
				var callback = "transport_summary_onload();";
				ajax_processRequestPost(url,param,div,callback)
				return false;
			}
		}
		// Print
		if (with_selected == 3)
		{
			x_window  = "modules/transport/transport_summary_print.php";
			x_window += "?mode=print";
			x_window += "&checkbox_id=" + checkbox_string;
			var new_window = window.open(x_window,'print','width=950,height=800,resizable=yes,scrollbars=yes');
			new_window.focus();
		}
		return false;
	}
	return false;
}

function transport_summary_action(mode,id,x_param)
{
	if (mode == 'insert')
	{
		ajax_showdiv('div_action');
		
		var url 		= 'modules/transport/transport_detail.php';
		var div 		= 'div_popup_1';
		var param   	= 'mode=' + mode;
		param   		+= '&transport_id=' + id;
		var callback 	= "transport_detail_onload('" + mode + "');";
		callback 		+= "ajax_hidediv('div_action');";
		
		ajax_processRequestPost(url,param,div,callback)
	}
	if ((mode == 'edit') || (mode == 'view'))
	{
		ajax_showdiv('div_result');
		
		var url 		= 'modules/transport/transport_detail.php';
		var div 		= 'div_popup_1';
		var param   	= 'mode=' + mode;
		param   		+= '&transport_id=' + id;
		var callback 	= "transport_detail_onload('" + mode + "');";
		callback 		+= "ajax_hidediv('div_result');";
		
		ajax_processRequestPost(url,param,div,callback)
	}
	if (mode == 'delete')
	{
		ajax_showdiv('div_result');
		
		var answer = confirm("This item will be permanently deleted\n\n Click OK to continue or CANCEL to quit.")
		if (answer)
		{
			var url 		= 'modules/transport/transport_summary.php';
			var div 		= 'div_body';
			var param   	= 'mode=' + mode;
			param   		+= '&transport_id=' + id;
			var callback 	= "transport_summary_onload('" + mode + "');";
			
			ajax_processRequestPost(url,param,div,callback)
		}
		else
		{
			ajax_hidediv('div_result');	
	
		}
	}
	else if (mode == 'inline_edit')
	{
		ajax_showdiv('div_result');

		var url 		= 'modules/transport/transport_summary.php';
		var div 		= 'div_body';
		var param   	= x_param;
		var callback 	= "transport_summary_onload('inline_edit','" + id + "');";
		
		ajax_processRequestPost(url,param,div,callback)
	}
	else if (mode == 'inline_save')
	{
		ajax_showdiv('div_result');

		var url 		= 'modules/transport/transport_summary.php';
		var div 		= 'div_body';
		var param   	= id;
		var callback 	= "transport_summary_onload('');";
		
		ajax_processRequestPost(url,param,div,callback)
	}
}

function transport_summary_fill_client(
					obj_id,value_id,
					obj_company,value_company,
					div)
{
	$('#' + obj_id).val(value_id);
	$('#' + obj_company).val(value_company);

	setTimeout("$('#" + div + "').hide();", 200);
	
	return;
}

function transport_summary_print(id)
{
	x_window = "modules/transport/transport_summary_print.php?checkbox_id=" + id;
	var new_window = window.open(x_window,'print','width=900,height=800,resizable=yes,scrollbars=yes');
	new_window.focus();
}

function transport_summary_onload(mode,id)
{
	Calendar.setup({
		inputField     	:    "filter_from",
		ifFormat       	:    "%d-%m-%Y",
		button         	:    "filter_from_link",
		align          	:    "BR",
		singleClick    	:    true
	});
	Calendar.setup({
		inputField     	:    "filter_to",
		ifFormat       	:    "%d-%m-%Y",
		button         	:    "filter_to_link",
		align          	:    "BR",
		singleClick    	:    true
	});	
	
	if (mode == 'inline_edit')
	{
		document.getElementById(id).focus();	
	}
	if (document.getElementById('filter_refresh').checked == true)
	{
		var url = 'modules/transport/transport_summary.php';
		var param = '';
		var div = 'div_body';
		var callback = "transport_summary_onload();";
		globalTimer = setTimeout("ajax_showdiv('div_result');ajax_processRequestPost('"+url+"','"+param+"','"+div+"','"+callback+"');",60000);
	}
}
-->
