var tpl = "";

function Select(name, checked)
{
	$$('input[name="' + name + '"]').each
	(
		function(checkBox)
		{
			checkBox.checked = checked || checkBox.disabled;
		}
	)
}

function getCheckBoxes(name, checked)
{
	var q = "";
	var i = 0;
	$$('input[name="' + name + '"]').each
	(
		function(checkBox)
		{
			if (checkBox.checked == checked) q += "," + checkBox.value;
		}
	)
	//if(q == ",AU,CA,JP,EMU,DE,NZ,SW,UK,US" && name == "chkCountry")q = "_All";
	return q.substring(1);
}

function generateQuery(preview)
{
	if (!save()) 
	{
		$("errores").show();
		return;
	}
	$("errores").hide();
	var titol = $F("txtCaption").replace("&", "and");
	var url = "fxstreet_proxy.aspx?url=http://xml.fxstreet.com/EconomicCalendar/preview.aspx";
	var countries = getCheckBoxes("chkCountry", true);
	var columns = getCheckBoxes("chkColumns", false);
	var params = "id=" + $F('txtId') + 
				 "&caption=" + titol +
				 "&style=" + $F("cmbStyle") + "-" + $F("cmbFontSize") +
				 "&width=" + $F("txtWidth") + encodeURIComponent($F("cmbWidthUnit")) +
				 "&height=" + $F("txtHeight") +
				 "&countries=" + countries +
				 "&hides=" + columns +
				 "&view=" + $F("cmbView") +
				 "&culture=es-ES";

	if (tpl == "") tpl = $("contentCode").innerHTML;
	var iframe = "<iframe src=\"http://xml.fxstreet.com/EconomicCalendar/iframe.aspx?id=FXstreetCalendar&caption=##TITOL##&style=##STYLE##&width=##WIDTH##&height=##HEIGHT##&countries=##COUNTRIES##&hides=##HIDES##&view=##VIEW##&culture=##CULTURE##&mode=0\" width=\"##W##\" height=\"##H##\" frameborder=\"0\"></iframe>";
	var code;
	if (document.getElementById('selectMethod').options[document.getElementById('selectMethod').selectedIndex].value == '0') {
	    code = tpl.replace("#width#", $F("txtWidth") + $F("cmbWidthUnit"));
	    code = code.replace("#style#", $F("cmbStyle"));
	    code = code.replace("#fontsize#", $F("cmbFontSize"));
	    code = code.replace("#params#", params);
	    code = code.replace("#id#", $F('txtId'));
	    code = code.replace(/&lt;/g, "<");
	    code = code.replace(/&gt;/g, ">");
	}
	else {
	    code = iframe.replace("##WIDTH##", $F("txtWidth") + $F("cmbWidthUnit"));
	    code = code.replace("##W##", $F("txtWidth") + $F("cmbWidthUnit"));
	    code = code.replace("##TITOL##", titol);
	    code = code.replace("##STYLE##", $F("cmbStyle") + "-" + $F("cmbFontSize"));
	    code = code.replace("##HEIGHT##", $F("txtHeight"));
	    code = code.replace("##H##", $F("txtHeight"));
	    code = code.replace("##COUNTRIES##", countries);
	    code = code.replace("##HIDES##", columns);
	    code = code.replace("##VIEW##", $F("cmbView"));
	    code = code.replace("##CULTURE##", "es-ES");
	}
	$("contentCode").value = code;
	$("contentCode").style.display = "";
	var cssHref = $F("cssTemplate").replace("#style#", $F("cmbStyle"));
	cssHref = cssHref.replace("#fontsize#", $F("cmbFontSize"));
	$("previewcss").href = cssHref
					
	if (preview)
	{
		params += "&type=aspx";
		new Ajax.Updater('preview', 
						url, 
						{
							parameters: params.toQueryParams(),
							method: 'get',
							evalScripts: 'true'
						});
	}
}


FXStreet.require('validaciones.js');
var errores = Array(new Array(0, 'txtCaption', 'Empty', true),
					new Array(0, 'txtWidth', 'Int', true),
					new Array(0, 'txtHeight', 'Int', true),
					new Array(0, 'chkCountry', 'Option', true));
							