function change_category(cat, character, dest) {

// alert(cat.value + '...' + dest);



if (cat.value) {
	character.length = characters[cat.value].length;
	var i = 0;

	while(i < characters[cat.value].length) {
		character.options[i].text = characters[cat.value][i];
		character.options[i].value = characters[cat.value][i];
		i++;	
	}
}

character.selectedIndex = 0;
change_char(character.options[0], dest, cat);

}



function change_char(source, dest, cat) {
	dest.src = "http://comicstrip.ca/images/"+cat.value+"/"+source.value+".gif";
}

function change_dialog_tail(source, dest) {
	dest.src = "http://comicstrip.ca/images/"+source+".gif";
}


function populate_list(cat) {

var i = 0;

cat.length = categories.length;

while (i < categories.length) 
{
	cat.options[i].text = categories[i];
	cat.options[i].value = categories[i];
	i++;
}
cat.selectedIndex=0;
}

function change_dialog(source, dest, balloon) {

	dest = document.getElementById(dest);
	balloon = document.getElementById(balloon);

	if(source.value.length == 1) {
		dest.innerHTML = '&nbsp;&nbsp;' + source.value + '&nbsp;&nbsp;';
	}
	else if(source.value.length == 2) {
		dest.innerHTML = '&nbsp;' + source.value + '&nbsp;';
	}
		else {
				dest.innerHTML = source.value;
	}
	
	if(!dest.innerHTML) {
		balloon.style.visibility = "hidden";
	}
	else {
		balloon.style.visibility = "visible";
	}
	
}

function change_back(source, dest) {

	dest = document.getElementById(dest);
	
	if(source.value) {
		dest.style.background = 'url(http://comicstrip.ca/images/backgrounds/'+source.value+'.jpg)';
	}
}

function populate_back(bg) {
	bg.length = backgrounds.length;
	
	var i = 0;
	
	while(i < backgrounds.length) {
		bg.options[i].value = backgrounds[i];
		bg.options[i].text = backgrounds[i];

		i++;	
	}
	
}

function change_narration(source, dest, td, border) {

	dest = document.getElementById(dest);
	border = document.getElementById(border);
	td = document.getElementById(td);

	dest.innerHTML = source.value;
	
	if(!dest.innerHTML) {
		td.style.visibility = "hidden";
		border.style.visibility = "hidden";
	}
	
	if(dest.innerHTML) {
		td.style.visibility = "visible";
		border.style.visibility = "visible";
	}

}

function switch_panels(panels, comic)
{
	
 if(comic == 0) {
 	comic = null;	
 	editcomic ="";
 }
 else {
	editcomic = "&ID=" + comic;
 }
	
 var where_to= confirm("Switching to a " + panels + " panel layout will erase your current work. Is this okay?");
 if (where_to== true)
 {
   window.location="http://comicstrip.ca/make.php?panels=" + panels + editcomic;
 }
 else
 {
  }
}

// this is an awful hack
function clone_panel(panel, panels) {

if(panel == 1) {

	if(panels > 1) {
		document.selectform.cats3.selectedIndex = document.selectform.cats1.selectedIndex;
		document.selectform.cats4.selectedIndex = document.selectform.cats2.selectedIndex;
		change_category(document.selectform.cats3, document.selectform.c3, 'char3');
		change_category(document.selectform.cats4, document.selectform.c4, 'char4');
		document.selectform.c3.selectedIndex = document.selectform.c1.selectedIndex;
		document.selectform.c4.selectedIndex = document.selectform.c2.selectedIndex;
		change_char(document.selectform.c3, document.char3, document.selectform.cats3);
		change_char(document.selectform.c4, document.char4, document.selectform.cats4);
		document.selectform.bg2.selectedIndex = document.selectform.bg1.selectedIndex;
		change_back(document.selectform.bg2, 'panel2');
		document.selectform.n2.value = document.selectform.n1.value;
		change_narration(document.selectform.n2, 'nar2', 'nar2td', 'nar2border');
		document.selectform.c3dialog.value = document.selectform.c1dialog.value;
		document.selectform.c4dialog.value = document.selectform.c2dialog.value;
		change_dialog(document.selectform.c3dialog, 'dialog3', 'dialogballoon3');
		change_dialog(document.selectform.c4dialog, 'dialog4', 'dialogballoon4');
		document.selectform.type3.selectedIndex = document.selectform.type1.selectedIndex;
		document.selectform.type4.selectedIndex = document.selectform.type2.selectedIndex;
		document.dtail3.src = document.dtail1.src;
		document.dtail4.src = document.dtail2.src;
	}


	if(panels > 2) {
	// CATEGORIES

	document.selectform.cats5.selectedIndex = document.selectform.cats1.selectedIndex;
	document.selectform.cats6.selectedIndex = document.selectform.cats2.selectedIndex;
	change_category(document.selectform.cats5, document.selectform.c5, 'char5');
	change_category(document.selectform.cats6, document.selectform.c6, 'char6');
	document.selectform.c5.selectedIndex = document.selectform.c1.selectedIndex;
	document.selectform.c6.selectedIndex = document.selectform.c2.selectedIndex;
	change_char(document.selectform.c5, document.char5, document.selectform.cats5);
	change_char(document.selectform.c6, document.char6, document.selectform.cats6);
	document.selectform.bg3.selectedIndex = document.selectform.bg1.selectedIndex;
	change_back(document.selectform.bg3, 'panel3');
	document.selectform.n3.value = document.selectform.n1.value;
	change_narration(document.selectform.n3, 'nar3', 'nar3td', 'nar3border');
	document.selectform.c5dialog.value = document.selectform.c1dialog.value;
	document.selectform.c6dialog.value = document.selectform.c2dialog.value;
	change_dialog(document.selectform.c5dialog, 'dialog5', 'dialogballoon5');
	change_dialog(document.selectform.c6dialog, 'dialog6', 'dialogballoon6');
	document.selectform.type5.selectedIndex = document.selectform.type3.selectedIndex;
	document.selectform.type6.selectedIndex = document.selectform.type4.selectedIndex;
	document.dtail5.src = document.dtail1.src;
	document.dtail6.src = document.dtail2.src;
	}
}

}

// disable enter
function kH(e) {
var pK = document.all? window.event.keyCode:e.which;
return pK != 13;
}

document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
