///////////////////////////////////////////////////
// javascript for tile-design workshop for LittleWolf
// CopyRight by Polly Jennings 1998-2001
///////////////////////////////////////////////////

// this function runs when page starts
function init() 
{
	modgrid(document.appletform);
	readgrid(document.appletform);
}

// this function changes grid-size from the size-selection list
function modgrid(form) {
	var rows = form.grows.value;
	var cols = form.gcols.value;

	switch (form.gridsize.options.selectedIndex) {
	case 0: // first item on size-selection list
		if (rows != 32 || cols != 32) { // if new size is different
		    // next line tells applet to change its grid-size
			document.TileWorkshop.NewGrid(32, 32);
			form.gcols.value = 32; // update custom numbers	
			form.grows.value = 32;
		}
		break;

	// Susan's special sizes
	case 1: // 3 x 4
		if (rows != 4 || cols != 3) {
			document.TileWorkshop.NewGrid(3, 4);
			form.gcols.value = 3;		
			form.grows.value = 4;	
		}
		break;

	case 2: // 3 x 12
		if (rows != 12 || cols != 3) {
			document.TileWorkshop.NewGrid(3, 12);
			form.gcols.value = 3;		
			form.grows.value = 12;	
		}
		break;
	case 3: // 6 x 12
		if (rows != 12 || cols != 6) {
			document.TileWorkshop.NewGrid(6, 12);
			form.gcols.value = 6;		
			form.grows.value = 12;	
		}
		break;
	case 4: // 6 x 18
		if (rows != 18 || cols != 6) {
			document.TileWorkshop.NewGrid(6, 18);
			form.gcols.value = 6;		
			form.grows.value = 18;	
		}
		break;

	case 5: // 8 x 10
		if (rows != 10 || cols != 8) {
			document.TileWorkshop.NewGrid(8, 10);
			form.gcols.value = 8;		
			form.grows.value = 10;	
		}
		break;
	case 6: // 10 x 20
		if (rows != 20 || cols != 10) {
			document.TileWorkshop.NewGrid(10, 20);
			form.gcols.value = 10;		
			form.grows.value = 20;	
		}
		break;

	default: // last item on list (custom)
		customgrid(form); // call function to get custom numbers
		break;
	}
	adjustsize(form);
}

// this function changes grid-size from the custom size numbers
function customgrid(form)
{
	form.gridsize.options.selectedIndex = 4;

	var y = form.grows.value; // get the user numbers
	var x = form.gcols.value;

	// NaN (Not a Number)
	if (isNaN(x)) x = 1; // not a number, change to 1
	if (isNaN(y)) y = 1;
	
	// grid-dimensions out of range
	if (x > 32) x = 32; // too big, change to max
	if (y > 32) y = 32;
	if (x < 1) x = 1; // too small, change to 1
	if (y < 1) y = 1;

    // write numbers back on page in case we changed them above
	form.gcols.value = x; 
	form.grows.value = y;
	document.TileWorkshop.NewGrid(x, y); // tell applet about new grid-size
	adjustsize(form); // change applet size on page if possible
}

// this functions changes applet size based on grid size
function adjustsize(form)
{
    // start with minimum size
	var w = 640; // width for 32 columns
	var h = 284; // height for 16 rows
	var y = form.grows.value; // get current numbers
	var x = form.gcols.value;

	if (y > 16) // if more than 16 rows, enlarge applet height
		h = h + 14 * (y - 16);

	if (x > 32) // if more than 32 cols, enlarge applet width
		w = w + 14 * (x - 32);

	if (navigator.appName != "Netscape") { // Netscape won't do it
		document.TileWorkshop.width = w; // change applet width on page
		document.TileWorkshop.height = h; // change applet height on page
	}
}

// this function saves the current design in a user cookie
function savegrid(form)
{
    // ask the applet to make a text version of the current design
	var grid = document.TileWorkshop.SaveDesign();
	deleteCookie("tgrid");
	setCookie("tgrid", grid); // send the text to the cookie function
	form.Read.disabled = false; // enable the get button
}

// this function reads a saved design from a user cookie
function readgrid(form)
{
	var val = getCookie("tgrid");
	if (val == null) { // no design found
	    form.Read.disabled = true; // disable the get button
		return;
	}

    // tell the applet to change the text-design into a real one
	var result = document.TileWorkshop.LoadDesign(val, val.length);
	if (result > 0) { // applet had to change grid-size, fix page UI to match
	    form.grows.value = document.TileWorkshop.GetRows();
	    form.gcols.value = document.TileWorkshop.GetCols();
	    adjustsize(form);
	    form.gridsize.options.selectedIndex = 4;
    }
}

// this function deletes the cookie
function deleteCookie(name)
{
	var expdate = new Date();
	expdate.setTime(expdate.getTime() - 1);
	document.cookie = name + "=;expires="
	  + expdate.toGMTString();
}                                                     

// this function puts the text in the cookie
function setCookie(name, value)
{
	var expdate = new Date(); // new expiration date 400 days from now
	expdate.setTime(expdate.getTime() + (1000 * 60 * 60 * 24 * 400));
	document.cookie = name + "=" + value
	  + ";expires=" + expdate.toGMTString();
}                                                     

// this function gets the appropriate text from the cookie
function getCookie(name) 
{
	var dcookie = document.cookie; 
	var cname = name + "=";
	var clen = dcookie.length;
	var cbegin = 0;

	while (cbegin < clen) {
		var vbegin = cbegin + cname.length;
		if (dcookie.substring(cbegin, vbegin) == cname) { 
			var vend = dcookie.indexOf(";", vbegin);
			if (vend < 0) 
				vend = clen;
			return dcookie.substring(vbegin, vend); // grid text
		}
		cbegin = dcookie.indexOf(" ", cbegin) + 1;
		if (cbegin == 0) 
			break;
	}
	return null;
}
