// This script is locale-independent
var gridErrIconUrl;
var gridDivColIcon;
var gridDivRowIcon;

function gridwpUpdatePreview(themeList, colorList, descriptionId, previewId, imagePath)
{
	var descriptionDiv = document.getElementById(descriptionId);
	var imageDiv = document.getElementById(previewId);
	
	var colorPath = "";
	if (colorList != null) {
		var selectedItem = colorList.options[colorList.selectedIndex];
		if(selectedItem != null)
		{
			var val = selectedItem.value;
			if(val != null && val != "")
			{
				var ind = val.indexOf("|");
				var descr = "";
					
				if(ind > 0) // Found description marker
				{
					descr = val.substr(ind + 1);
					colorPath = val.substr(0, ind);
				}
				else
				{
					colorPath = val;
				}
			}
		}
	}
	
	if(themeList != null && imageDiv != null && descriptionDiv != null)
	{
			var selectedItem = themeList.options[themeList.selectedIndex];
			if(selectedItem != null)
			{
				var val = selectedItem.value;
				if(val != null && val != "")
				{
					var ind = val.indexOf("|");
					var descr = "";
					var templ;
					
					if(ind > 0) // Found description marker
					{
						descr = val.substr(ind + 1);
						templ = val.substr(0, ind);
					}
					else
					{
						templ = val;
					}
					
					imageDiv.innerHTML = "<img src=\"" + imagePath + colorPath + "\\" + templ + ".gif\"/>";
					descriptionDiv.innerText = descr;
				}
			}
	}
}

function initializeThemeSelector(listBoxId)
{
	var lb = document.getElementById(listBoxId);
	if(lb != null)
	{
		//lb.selectedIndex = 0;
		lb.fireEvent("onchange");
	}
}

///////////////////////////////////////////////////////
// GridWizard create dialog 

function doGridWZDialog(themeId, colorId, colorGuidId, colId, rowId)
{
	var p = new Object();
	var v = window.showModalDialog(GridDialogUrl, p, 
		"scroll:no;center:yes;help:no;status:yes;dialogHeight:420px;dialogWidth:620px");
	
	if(v && v.theme && v.color && v.numRows && v.numCols)
	{
		var elem = document.getElementById(themeId);
		elem.value = v.theme;
		elem = document.getElementById(colorId);
		elem.value = v.color;
		if (v.colorGuid) {
			elem = document.getElementById(colorGuidId);
			elem.value = v.colorGuid;
		}
		elem = document.getElementById(colId);
		elem.value = v.numCols;
		elem = document.getElementById(rowId);
		elem.value = v.numRows;			
            return true;
	}			
        else
        {
             return false;
        }
}

// Validates positive integer represented as string
function validatePosInteger(inputFieldId) 
{
	var i = document.getElementById(inputFieldId);
	i.value = i.value.replace(/[\t\n ]/, ''); // Trim spaces everywhere
	
	if(i.value.match(/^[0-9]+$/) == null || i.value.match(/^0+$/) != null) // Validation failed
	{
		var o = new Object();
		o.field = i;
		throw o;
	}
	return i.value;
}

function ValidateRange(InputFieldId, Range)
{
	var i = document.getElementById(InputFieldId);
	
	if(i.value > Range)
	{
		var o = new Object();
		o.field = i;
		throw o;
	}
}

function okDialog()
{
	var res = new Object();
	var errmsg = "";
	var errfield = null;

	var i = document.getElementById('_lbTheme');
	var str = i.options(i.selectedIndex).value;
	var end = str.indexOf('|');
	if (end != -1) 
	{
		res.theme = str.substring(0,end);
	} else {
		res.theme = str;
	}
	
	i = document.getElementById('_tbColor');
	str = i.options(i.selectedIndex).value;
	end = str.indexOf('|');
	if (end != -1) 
	{
		res.color = str.substring(0,end);
		res.colorGuid = str.substring(end+1);
	} else {
		res.color = str;
	}
		
	try 
	{
		res.numRows = validatePosInteger('_tbRows');
	}
	catch(o)
	{
		errmsg += "- "+ L_GridInvalidRow_ErrorMessage;
		errfield = o.field;
	}
	
	try 
	{
		res.numCols = validatePosInteger('_tbColumns');
	}
	catch(o)
	{
		if (errmsg != "") 
		{
			errmsg += '\n';
		} else {
			errfield = o.field;
		}
            errmsg +=  "- "+ L_GridInvalidCol_ErrorMessage;
	}

	try 
	{
		ValidateRange('_tbRows', 40);
		ValidateRange('_tbColumns', 20);	
	}
	catch(o)
	{
		if (errmsg != "") 
		{
			errmsg += '\n';
		} else {
			errfield = o.field;
		}
            errmsg +=  "- "+ L_GridOutOfRange_ErrorMessage;
	}
	
	if (errmsg == "") {
		returnValue = res;
		window.close();
	}
	else
	{
              alert(errmsg);
		errfield.focus();
	}
}

