// set_id_and_debugging.js
// -------------------------------------------------------------------------------------------------
// Used to set override dealer ID and debugging options for TST
// These are stored in cookies
//
// Project    > brand1_TST_aug05:S01_C24
// Copyright  > (C) Digital Mail Limited (2005-2008)
// Author     > RAK for DML
// -------------------------------------------------------------------------------------------------


var temp = dmb1_get_cookie('dmx_debug_settings');
if (temp)
{
	eval('dmx_debug_settings =' + temp)
}
else
{
	dmx_debug_settings = new Object;
}

switch(document.title.toLowerCase())
{
	case 'set id and tst diagnostic options':
		onload = read_cookies_and_set_form_fields_in_set_id;
		break;

	case 'set tst diagnostic options':
		onload = read_cookies_and_set_form_fields_in_tst_indexpage;
		break;
}

// -------------------------------------------------------------------------------------------------

function save_id_and_diagnostic_options()
{
	// Save the dealers ID and diagnostic options by writing three cookies
	
	// First read the data from the form
	var dealer_id   = document.forms[0].elements[0].value;
	var debug_tst	= document.forms[0].elements[1].checked ? 1 : 0;
	
	dmx_debug_settings.tst = debug_tst;
	// var json_debug = JSON.encode(dmx_debug_settings);
	var json_debug = dmb1_object_to_string(dmx_debug_settings);

	alert('Setting dealer ID to ' + dealer_id + '\n and saving TST debugging options');
	
	var exp = dmb1_get_exp_date(1,0,0); // Set a cookie expiry date 24 hours from now
	
	dmb1_set_cookie('dmx_debug_settings', json_debug, exp);
	dmb1_set_cookie('dmb1_dealer_id', dealer_id, exp);


}

// -------------------------------------------------------------------------------------------------

function save_diagnostic_options()
{
	// Save  diagnostic options by writing two cookies
	
	// First read the data from the form
	var debug_tst	= document.forms[0].elements[0].checked ? 1 : 0;
	
	dmx_debug_settings.tst = debug_tst;
	// var json_debug = JSON.encode(dmx_debug_settings);
	var json_debug = dmb1_object_to_string(dmx_debug_settings);

	alert('Saving TST debugging options');
	
	var exp = dmb1_get_exp_date(1,0,0); // Set a cookie expiry date 24 hours from now
	
	dmb1_set_cookie('dmx_debug_settings', json_debug, exp);
}

// -------------------------------------------------------------------------------------------------

function read_cookies_and_set_form_fields_in_set_id()
{
	// Make sure current values are always displayed in the form fields
	// in set_id_and_debug.html
	
	// Standard DHTML initialisation
	libinit();

	// First try to retrieve the cookies
	var dealer_id   = dmb1_get_cookie('dmb1_dealer_id');
	var diagnostics = dmb1_get_cookie('dmb1_diagnostics');
	diagnostics = diagnostics == 'true' ? true : false; // Convert ruturned string back to true Boolean dataytpe;
	
	// If they are defined, set the value in the corresponding form field
	document.forms[0].elements[0].value = dealer_id ? dealer_id : '';
	document.forms[0].elements[1].checked = diagnostics;
}

// -------------------------------------------------------------------------------------------------

function read_cookies_and_set_form_fields_in_tst_indexpage()
{
	// Make sure current values are always displayed in the form fields
	// in /tst/index.html
	
	// Standard DHTML initialisation
	libinit();

	// First try to retrieve the cookies
	var diagnostics = dmb1_get_cookie('dmb1_diagnostics');
	diagnostics = diagnostics == 'true' ? true : false; // Convert ruturned string back to true Boolean dataytpe;
	
	// If they are defined, set the value in the corresponding form field
	document.forms[0].elements[0].checked = diagnostics;
}

