//////////////////////////////////////////////////////
// script for minitile design workshop - Copyright Polly Jennings 2001
/////////////////////////////////////////////////////////////////////

var oURL = "http://www.users.qwest.net/~lajoie/minitiles/order.htm";
//var oURL = "file://D:\\lajoie\\minitiles\\order.htm";

var owin;

function init()
{
	// The following lines would load the last-saved design from the cookie
	// modgrid(document.appletform);
	//readgrid(document.appletform);
	
	// The next line makes a 12x12 random pattern
	document.TileWorkshop.StartWithRandom();
}

function exitpage()
{
	if (owin != null && !owin.closed)
		owin.close();

	if (!document.TileWorkshop.isModified())
		return;

	var str1 = "Replace saved design before leaving page?";
	if (getCookie("tgrid") == null) {
		savegrid(document.appletform);
	}

	else if (window.confirm(str1)) {
		savegrid(document.appletform);
	}
}

function modgrid(form) 
{
	var rows = form.grows.value;
	var cols = form.gcols.value;

	switch (form.gridsize.options.selectedIndex) {
	case 0:
		if (rows != 12 || cols != 12) {
			document.TileWorkshop.NewGrid(12, 12);
			form.gcols.value = 12;		
			form.grows.value = 12;
		}
		break;

	case 1:
		if (rows != 12 || cols != 24) {
			document.TileWorkshop.NewGrid(24, 12);
			form.gcols.value = 24;		
			form.grows.value = 12;	
		}
		break;

	case 2:
		if (rows != 24 || cols != 24) {
			document.TileWorkshop.NewGrid(24, 24);
			form.gcols.value = 24;		
			form.grows.value = 24;	
		}
		break;

	case 3:
		if (rows != 24 || cols != 12) {
			document.TileWorkshop.NewGrid(12, 24);
			form.gcols.value = 12;		
			form.grows.value = 24;	
		}
		break;

	case 4:
		customgrid(form);
		break;
	}
}

function customgrid(form)
{
	form.gridsize.options.selectedIndex = 4;

    var y = 0;
    var x = 0;
	y = form.grows.value;
	x = form.gcols.value;

	// NaN or oor
	if (isNaN(x)) x = 1;
	if (isNaN(y)) y = 1;
	if (x > 38) x = 38;
	if (y > 38) y = 38;
	if (x < 1) x = 1;
	if (y < 1) y = 1;

	form.gcols.value = x;
	form.grows.value = y;
	document.TileWorkshop.NewGrid(x, y);
}

function savegrid(form)
{
	deleteCookie("tgrid");
	var grid = document.TileWorkshop.SaveDesign();
	setCookie("tgrid", grid);
	if (getCookie("tgrid"))
		form.Read.disabled = 0;
}

function readgrid(form)
{
	var val = getCookie("tgrid");
	if (val == null) {
		form.Read.disabled = 1;
		return;
	}

	var result = document.TileWorkshop.LoadDesign(val, val.length);
	if (result > 0) {
	    form.grows.value = document.TileWorkshop.GetRows();
	    form.gcols.value = document.TileWorkshop.GetCols();
	    form.gridsize.options.selectedIndex = 4;
    }
}

function setCookie(name, value)
{
	var expdate = new Date();
	expdate.setTime(expdate.getTime() + (1000 * 60 * 60 * 24 * 400));
	document.cookie = name + "=" + value
	  + ";expires=" + expdate.toGMTString();
}

function deleteCookie(name)
{
	var expdate = new Date();
	expdate.setTime(expdate.getTime() - 1);
	document.cookie = name + "=;expires="
	  + expdate.toGMTString();
}                                                     

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);
		}
		cbegin = dcookie.indexOf(" ", cbegin) + 1;
		if (cbegin == 0) 
			break;
	}
	return null;
}

function stripspace(astr)
{
	var nstr = "";
	for (var i = 0; i < astr.length; i++) {
		if (astr.charAt(i) != " ")
			nstr += astr.charAt(i);
	}
	return nstr;
}

function savetotals(form)
{
    var twin = window.open("", "", "resizable=1,scrollbars=1,width=400,height=200");
    twin.document.write("<body>");
    
    // document.TileWorkshop.SaveTotals() returns the comma separated order list
    twin.document.write(document.TileWorkshop.SaveTotals());
    
	twin.document.write("<input type=button name='close' value='Close'");
	twin.document.write("onClick='window.close()'>");
	twin.document.close();
}

function showtextgrid(form)
{
    var twin = window.open("", "", "resizable=1,scrollbars=1,width=440,height=340");
    twin.document.write("<body>");
    
	var grid = document.TileWorkshop.ColorCodeGrid();
    twin.document.write(grid);
    
	twin.document.write("<input type=button name='close' value='Close'");
	twin.document.write("onClick='window.close()'>");
	twin.document.close();
}


