var app, output_area_1, output_area_2, dao_type_edit, JSTemplate_original, HTMLtemplate_original;


function page_init()
{
	
	// Lookup HTML elements
	output_area_1 = document.getElementById("output_area_1");
	output_area_2 = document.getElementById('output_area_2');
	dao_type_edit = document.getElementById('dao_type_edit');
		
	// Create an application
	app = new application("Web transcoder"); 
	app.IEOKcolor = "#EEEEFF"; // F#?"ing IE

	JSTemplate_original = MakeRequest("/include/application/JSDC_generator_simple_JS_template.txt");
	HTMLtemplate_original = MakeRequest("/include/application/JSDC_generator_simple_HTML_template.txt");

}



function generate_code()
{
	var JSTemplate = JSTemplate_original;
	// Get application settings
	var application_title_edit = document.getElementById('application_title_edit');
	if (application_title_edit.value=='') 
	{ 
		alert ("You must provide an application title"); 
		focusandblink(application_title_edit);
		return; 
	}
	var errorcolor_edit = document.getElementById('errorcolor_edit');
	if (errorcolor_edit.value=='') 
	{ 
		alert ("You must set a valid errorcolor."); 
		focusandblink(errorcolor_edit);
		return; 
	}
	var IEOKcolor_edit = document.getElementById('IEOKcolor_edit');
	if (IEOKcolor_edit.value=='') 
	{ 
		alert ("You must set a valid IEOKcolor.");
		focusandblink(IEOKcolor_edit); 
		return; 
	}

	// Data path settings
	var data_path_edit = document.getElementById('data_path_edit');
	if (data_path_edit.value=='') 
	{
		alert ("You must supply a valid data path."); 
		focusandblink(data_path_edit);
		return; 
	}
	
	// Parser settings
	var colsep_edit = document.getElementById('colsep_edit');
	var rowsep_edit = document.getElementById('rowsep_edit');
	if (rowsep_edit.value=='') 
	{ 
		alert ("You must set a valid row separator."); 
		focusandblink(rowsep_edit);
		return; 
	}
	var header_edit = document.getElementById('header_edit');

	// Dataview code settings
	var automatic_fields_edit = document.getElementById('automatic_fields_edit');
	
	// Code generation settings
	var comments_edit = document.getElementById('comments_edit');
	var singlefile_edit = document.getElementById('singlefile_edit');

	var JS_path_edit = document.getElementById('JS_path_edit');
	
	if (!singlefile_edit.checked & JS_path_edit.value=='')
	{ 
		alert("You must set a valid javascript path if you want to have the javascript in a separate file.\n"); 
		focusandblink(JS_path_edit);
		return; 
	}

	var suffix_edit = document.getElementById('suffix_edit');
	if (suffix_edit.value=='') 
	{ 
		alert ("You must set a valid suffix."); 
		focusandblink(suffix_edit);
		return;
	}
	var JSDCpath_edit = document.getElementById('JSDCpath_edit');
	if (JSDCpath_edit.value=='') 
	{ 
		alert ("You must set a valid JSDC path."); 
		focusandblink(JSDCpath_edit);
		return;
	}
	// Generate JS code
	
	// Fix variable names
	JSTemplate = JSTemplate.replace(/#suffix/gi, suffix_edit.value);
	// Fix application title
	JSTemplate = JSTemplate.replace(/#application_title/gi, application_title_edit.value);
	// Fix row separator
	JSTemplate = JSTemplate.replace(/#rowsep/gi, rowsep_edit.value);
	// Fix column separator
	JSTemplate = JSTemplate.replace(/#colsep/gi, colsep_edit.value);
	// Fix JSDC path
	JSTemplate = JSTemplate.replace(/#JSDCpath/gi,JSDCpath_edit.value);	
	// Fix JS path
	JSTemplate = JSTemplate.replace(/#JSpath/gi,JS_path_edit.value);	
	// Fix IEOKColor
	JSTemplate = JSTemplate.replace(/#IEOKColor/gi,IEOKcolor_edit.value);	
	// Fix JS path
	JSTemplate = JSTemplate.replace(/#errorcolor/gi,errorcolor_edit.value);	
	
	JSTemplate = JSTemplate.replace(/#data_path/gi,data_path_edit.value);	
	// Fix header handling
	if (header_edit.checked)
	{
		JSTemplate = JSTemplate.replace(/#noheader/gi,'');
	}
	else
	{
		JSTemplate = JSTemplate.replace(/#noheader/gi,', true');
	}
	
	// Dao settings
	
	switch (dao_type_edit.value) 
	{
		case '0': 
			JSTemplate = JSTemplate.replace(/#dao_options/gi,'"table", 0 , 0');
			JSTemplate = JSTemplate.replace(/#dao_style/gi,'table');
		break;
		case '1': 
			JSTemplate = JSTemplate.replace(/#dao_options/gi,'"select", 0 , 0');
			JSTemplate = JSTemplate.replace(/#dao_style/gi,'select');
		break;
	}
	
	// Generate HTML
	
	var HTMLTemplate = HTMLtemplate_original;
	HTMLTemplate = HTMLTemplate.replace(/#application_title/gi,application_title_edit.value);	
	
	switch (dao_type_edit.value) 
	{
		case '0': 
			HTMLTemplate = HTMLTemplate.replace(/#HTML_gui_node/gi,'<table id ="'+suffix_edit.value+'_gui"><tr><td></td></tr></table>')
		break;
		case '1': 
			HTMLTemplate = HTMLTemplate.replace(/#HTML_gui_node/gi,'<select id ="'+suffix_edit.value+'_gui">');
		break;
	}	
	
	HTMLTemplate = HTMLTemplate.replace(/#JSDCdir/gi,JSDCpath_edit.value);	
	
	if (!singlefile_edit.checked)
	{
	
		var JS_inc = '<script type="text/javascript" src="'+JS_path_edit.value+'"></script>';
		HTMLTemplate = HTMLTemplate.replace(/#generated_javascript/gi,JS_inc);
		output_area_1.value = HTMLTemplate;
		output_area_2.value = JSTemplate;
	}
	else
	{

		HTMLTemplate = HTMLTemplate.replace(/#generated_javascript/gi,'<script type="text/javascript"><!--\n\n'+JSTemplate+'\n//--></script>');
		output_area_1.value = HTMLTemplate;
		output_area_2.value = '';
	}	
	
}

