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=" + encode(titol) +
				     "&style=" + $F("cmbStyle") + "-" + $F("cmbFontSize") +
				     "&width=" + $F("txtWidth") + encodeURIComponent($F("cmbWidthUnit")) +
				     "&height=" + $F("txtHeight") +
				     "&countries=" + countries +
				     "&hides=" + columns +
				     "&view=" + $F("cmbView");;
				    params = params + "&culture=ru-RU";
	
	if (tpl == "") tpl = $("contentCode").innerHTML;
	var 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, ">");
	$("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, 
						{
						    method: 'get',
							parameters: params.toQueryParams(),							
							evalScripts: 'true'
						});
	}
}

function encode(title)
{
    var newTitle;
    
    newTitle = encodeURI(title);       
    
    return newTitle;
}

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));
							