//*******************************************************************************
// This file contains javascript functions developped for
// Hawker BECS application
// These functions control the process of the user interface.
// 
// Modification History:
//
// 24/04/03 : Creation by T.DUBUIS
//
//
//*******************************************************************************


//******************************************************************
// Called when frame ctrl_board is loaded.
//******************************************************************
function init_frame_ctrlboard()
{


	// Init globlal variables to default values
	init_gbl_vars();
  parent.focus();
  alert("INITIALISATION Successfull");
 
  load_frame("frame_modules.asp");

}

//******************************************************************
// Called by reset/init button.
//******************************************************************
function reset_init()
{
	if ( confirm("Going to reset all and restart Pro/E ?") )
	{
		  init_frame_ctrlboard();
	}
}

//**********************************************************
// Called to display an error messge and update the frame status
//**********************************************************
function alert_frame_status (msg, alert_ok)
{
	if (alert_ok) alert(msg);
	set_frame_status(msg);
}

//******************************************************************
// 
//******************************************************************
function set_frame_status(msg)
{
	parent.wizard_frame.document.forms[0].frame_status.value = msg;
}

//******************************************************************
// 
//******************************************************************
function load_frame(url)
{
	parent.wizard_frame.location.href = url;
}


//******************************************************************
// Return a memorised value from another form
//******************************************************************
function get_mem_value(form_name, elt_name)
{
	var forms_values = parent.ctrl_board.forms_values;
	if ( forms_values[form_name] != null )
	{
		if ( forms_values[form_name][elt_name] != null )
		{
				return( forms_values[form_name][elt_name]);
		}
		else
		{
			alert("ERROR in get_mem_val() forms_values["+form_name+"]["+elt_name+"] == null");
		}
	}
	else
	{
			alert("ERROR in get_mem_val() forms_values["+form_name+"] == null");
	}

	return ("");
}

//******************************************************************
// Read all elements in the wizard form, and store them in the global array "forms_values"
//******************************************************************
function memorise_form_values()
{
	var form_obj = parent.wizard_frame.document.forms[0];
	parent.ctrl_board.forms_values[form_obj.name] = new Array();
	for ( var i = 0; i < form_obj.elements.length ; i++)
	{
			var elt_obj=form_obj.elements[i];
			parent.ctrl_board.forms_values[form_obj.name][elt_obj.name] = getHtmlEltValue( form_obj.name, elt_obj.name , "wizard_frame" );
	}
}

//******************************************************************
// Replace all elements values in the wizard form, with global array "forms_values" 
// if any !!
//******************************************************************
function restore_memorised_values()
{
	var form_obj = parent.wizard_frame.document.forms[0];
	var forms_values = parent.ctrl_board.forms_values;

	if ( forms_values[form_obj.name] != null )
	{
		for ( var i = 0; i < form_obj.elements.length ; i++)
		{
				var elt_obj=form_obj.elements[i];
				if ( forms_values[form_obj.name][elt_obj.name] != null )
				{
					setHtmlEltValue( form_obj.name, elt_obj.name, forms_values[form_obj.name][elt_obj.name], "wizard_frame" );
				}
		}		
	}
}

//******************************************************************
// Called to switch to next frame
// 1) Check current frame is completed
// 2) Backup all form values in an array
// 3) Next frame URL is defined in current frame
// 4) Load frame
//******************************************************************
function next_frame()
{

	// If user click many time on "next" button, wizard page may not be loaded yet ...
   if ( parent.wizard_frame.document == null ) return;
   if ( parent.wizard_frame.document.forms == null ) return;
   if ( parent.wizard_frame.document.forms[0] == null ) return;

	var next_frame = parent.wizard_frame.document.forms[0].next_frame.value;
	

	var frame_status = parent.wizard_frame.document.forms[0].frame_status.value;
	if ( frame_status != "OK" )
	{
		alert("ERROR: Current Step is not completed. \n status: ["+frame_status+"]");		
	}
	else
	{
		memorise_form_values();

		if ( next_frame != "none" ) 
			load_frame(next_frame);
		else
			window.status = "Button has no action within current context";
	}
}



//******************************************************************
// Called to switch to previous frame
// 1) Current frame may not be completed
// 2) If frame completed : Backup all form values in an array
// 3) previous frame URL is defined in current frame
// 4) Load frame
//******************************************************************
function previous_frame()
{

	// If user click many time on "previous" button, wizard page may not be loaded yet ...
   if ( parent.wizard_frame.document.forms == null ) return;

	var previous_frame = parent.wizard_frame.document.forms[0].previous_frame.value;
	
	var frame_status = parent.wizard_frame.document.forms[0].frame_status.value;
	
   memorise_form_values();		


	if ( previous_frame != "none" ) 
			load_frame(previous_frame);
		else
			window.status = "Button has no action within current context";

}



