/*	grid.js
	(c)2007 Nuvola Ltd - www.nuvola.co.uk
	This is the client side handling for the selection grid
	Requires ajaxcontent.js by Dynamic Drive
	This code may be freely used provided this notice remains intact
*/

function grid_edit(rownum)
{
	var selrow = document.getElementById("grid_editrow").value;
	var i, colcount, optlist, curlen, opt, obj, x, y, before, issel;
	
	if(selrow == rownum)		//do nothing if current row
		return true;
	
	if(selrow != 0)				//save the current row if different
		grid_save();
		
	//now, highlight the current row.
	//this means changing all classes to be selgrid_edit
	//and also populating the option lists if they haven't already been filled
	
	document.getElementById("grid_edit" + rownum).style.display = "none";
	document.getElementById("grid_save" + rownum).style.display = "inline";
	document.getElementById("grid_canc" + rownum).style.display = "inline";
	document.getElementById("grid_editrow").value = rownum;
	
	document.getElementById("grid_ajaxreturn").innerHTML = "<br>";		//clear output div
	colcount = document.getElementById("grid_colcount").value;
	for(i = 0; i < colcount; i ++)
	{
		obj = document.getElementById("row" + rownum + "col" + i);
		
		try
		{
			obj.className = "selgrid_edit";
			//alert(obj.className + i);
		}
		catch(ex)
		{
			//alert("error row" + rownum + "col" + i);
			//do nothing if there's an error
		}
		
		//now, try to populate the select list - this will fail if the control is not a select
		try
		{
			issel = true;
			curlen = obj.options.length;
			if(curlen <= 1)		//we only need to do this if it hasn't already been done
			{
				optlist = document.getElementById("grid_sellist" + i).innerHTML;
				
				optlist = optlist.split(":::");
				x = obj.options[obj.selectedIndex];
				before = true;
				
				for(k = 0; k < optlist.length; k ++)
				{
					opt = optlist[k].split("|||");
					
					if(x.value != opt[0])
					{
						y = new Option(opt[1], opt[0])
						
						if(before) 		//add option before current
						{
							try
							{
								obj.add(y, x);
							}
							catch(e)
							{
								obj.add(y, obj.selectedIndex);		//IE, not standards compliant as ever
								//if (k==0) alert(opt[0] + " " + opt[1]);
							}
						}
						else 		//add option after
						{
							try
							{
								obj.add(y, null);
							}
							catch(e)
							{
								obj.add(y);							//IE, not standards compliant as ever
							}
						}
					}
					else		//change flag as current option = selected
					{
						before = false;
					}
					
				}
			}
		}
		catch(ex)
		{
			issel = false;
			//do nothing if there's an error
		}
		
		//store values in temporary controls
		switch(obj.type)
		{
			case "select-one":
				document.getElementById("grid_temp" + i).value = obj.selectedIndex;
				break;
			case "checkbox":
				document.getElementById("grid_temp" + i).value = obj.checked;
				break;
			default:
				document.getElementById("grid_temp" + i).value = obj.value;
		}
		
	}
	return true;
}

function grid_focus(obj, rownum)
/*	This function is executed every time a grid form element is clicked. It has the following responses:
		currently selected row -> do nothing
		unslected row -> fire grid_edit() for this row
	this is a separate function from grid_edit() to give us the flexibility to add different behaviour if a 
	control is clicked, compared to an edit button
*/
{
	var selrow = document.getElementById("grid_editrow").value;
	var x;
	
	if(selrow == rownum)		//do nothing if current row
		return true;
		
	if(selrow != 0)				//currently these do the same, this could be changed though
		grid_edit(rownum);
	else
		grid_edit(rownum);
	
	if(obj.name.substr(0,4) == "cal_")
	{
		x = obj.name;
		obj = document.getElementById(x.substr(4));
		cal.select(obj, x, "dd-MM-yyyy");
	}
}

function grid_canc()
/* cancel update operation - write values back from temp
*/
{
	//location.reload(true);
	var selrow = document.getElementById("grid_editrow").value;
	var colcount = document.getElementById("grid_colcount").value;
	
	var isselect, opts;
	document.getElementById("grid_ajaxreturn").innerHTML = "<br>";		//clear output div
	
	for(i = 0; i < colcount; i ++)
	{
		try
		{
			isselect = document.getElementById("grid_sellist" + i).innerHTML;		//will fail here if this isn't a select
			document.getElementById("row" + selrow + "col" + i).selectedIndex = document.getElementById("grid_temp" + i).value;
		}
		catch(ee)
		{
			document.getElementById("row" + selrow + "col" + i).value = document.getElementById("grid_temp" + i).value;
		}	
		document.getElementById("row" + selrow + "col" + i).className = "selgrid";
		document.getElementById("grid_temp" + i).value = 0;
	}
	//unhighlight row - switch edit controls
	document.getElementById("grid_edit" + selrow).style.display = "inline";
	document.getElementById("grid_save" + selrow).style.display = "none";
	document.getElementById("grid_canc" + selrow).style.display = "none";
	document.getElementById("grid_editrow").value = 0;
	
	
}

function grid_save()
/*	save the selected row by firing an ajax page request for action gridsave. 
	The URL string contains all values in the format gridfield1=abc&gridfield2=def etc
*/
{
	var selrow = document.getElementById("grid_editrow").value;
	var colcount = document.getElementById("grid_colcount").value;
	var i, saveval, isselect, isupdated = false;
	var urlstring = "?action=gridsave&ajax=1&id=" + document.getElementById("grid_id" + selrow).value;
	
	if(!selrow) return false;		//exit if no row highlighted
	
	for(i = 0; i < colcount; i ++)
	{
		saveval = "";
		//value comes from a different place for diff control types
		
		switch(document.getElementById("row" + selrow + "col" + i).type)
		{
			case "select-one":
				if(document.getElementById("grid_temp" + i).value != document.getElementById("row" + selrow + "col" + i).selectedIndex)
				{
					saveval = document.getElementById("row" + selrow + "col" + i).options[document.getElementById("row" + selrow + "col" + i).selectedIndex].value;	
					isupdated = true;
				}
				break;
			case "checkbox":
				if(document.getElementById("grid_temp" + i).value != document.getElementById("row" + selrow + "col" + i).checked)
				{
					
					saveval = (document.getElementById("row" + selrow + "col" + i).checked ? 1 : 0);
					isupdated = true;
				}
				break;
				
			default:
				if(document.getElementById("grid_temp" + i).value != document.getElementById("row" + selrow + "col" + i).value)
				{
					saveval = document.getElementById("row" + selrow + "col" + i).value;
					isupdated = true;
				}
		}
		
		if(saveval !== "") urlstring = urlstring + "&gridfield" + i + "=" + escape(saveval);
		
		document.getElementById("row" + selrow + "col" + i).className = "selgrid";		//unhighlight control
		document.getElementById("grid_temp" + i).value = 0;
	}
	//unhighlight row - switch edit controls
	document.getElementById("grid_edit" + selrow).style.display = "inline";
	document.getElementById("grid_save" + selrow).style.display = "none";
	document.getElementById("grid_canc" + selrow).style.display = "none";
	document.getElementById("grid_editrow").value = 0;
	
	if(isupdated) ajaxpage(urlstring, "grid_ajaxreturn");
}
	
