// JavaScript Document
// Write function.
// m is the test, b is boolean, if true, adds newline
function w(m,b) { 
	m = "" + m + ""; 				//  Make sure that the m variable is a string.
	if ("undefined" != m) { 		// Test for empty write or other undefined item.
   		document.write(m);
	}
	if (b) document.write("<br>");
}
function dbw(m,b) { 
	if (!DEBUG) return;
	m = "" + m + ""; 				//  Make sure that the m variable is a string.
	if ("undefined" != m) { 		// Test for empty write or other undefined item.
   		document.write(m);
	}
	if (b) document.write("<br>");
}
// Convert a color name to hex value
function convert(color) {
	var c, r
	r = "";
	c = color.toLowerCase()
	switch (c) {
		case "aqua":
		r="00FFFF";
		break;
		case "black":
		r="000000";
		break;
		case "blue":
		r="0000FF";
		break;
		case "fuchsia":
		r="FF00FF";
		break;
		case "grey":
		r="808080";
		break;
		case "green":
		r="008000";
		break
		case "lime":
		r="00FF00";
		break;
		case "maroon":
		r="800000";
		break;
		case "navy":
		r="000080";
		break;
		case "olive":
		r="808000";
		break;
		case "purple":
		r="800080";
		break;
		case "red":
		r="FF0000"
		break;
		case "silver":
		r="C0C0C0";
		break;
		case "teal":
		r="008080";
		break;
		case "white":
		r="FFFFFF";
		break;
		case "yellow":
		r="FFFF00";
		break;
		default:
		r = "900099";
		break;
	}
	return r;
}
function inverse(theString) {
	// If theString starts with a #, assume it's a 6 digit hex value
	// and remove the #
	// If not, assume it's a color name: convert to hex
	if (theString.charAt(0) == "#") {
		theString = theString.substr(1, theString.length - 1)
	} else {
		theString = convert(theString)
	}
	if(theString.length<6||theString.length>6){
		alert('Illegal colour code ' + theString)
		return false;
	}
//	alert("Converting " + theString)
	a=theString.slice(0,2);
	b=theString.slice(2,4);
	c=theString.slice(4,6);
	a1=16*giveHex(a.slice(0,1));
	a2=giveHex(a.slice(1,2));
	a=a1+a2;
	b1=16*giveHex(b.slice(0,1));
	b2=giveHex(b.slice(1,2));
	b=b1+b2;
	c1=16*giveHex(c.slice(0,1));
	c2=giveHex(c.slice(1,2));
	c=c1+c2;
	newColor=DecToHex(255-a)+""+DecToHex(255-b)+""+DecToHex(255-c)
	newColor = "#"+newColor
	return newColor;	
}
var hexbase="0123456789ABCDEF";
function DecToHex(number) {
	return hexbase.charAt((number>> 4)& 0xf)+ hexbase.charAt(number& 0xf);
}
function giveHex(s){
	s=s.toUpperCase();
	return parseInt(s,16);
}



	