/* javascript helper function, for looking up color pallette entries
   and getting the right index for them. we will always use the first
   pallette, text_color1, and assume that it is the same as all the other
   instantiations of that pallette. return the index of the color in the
   pallete, or a negative number if it is not found.
*/


function preset_lookupalignment(align)
{var alignlength = document.text_form.align.length;
var i = 0;

    if (align != "")
        while (i < alignlength)
	    if (document.text_form.align[i].value != align) {
	        i++;
		if (i >= alignlength) return -1;
	    }
	    else return i;
	else return -1;
}

function preset_lookupgradient(grad)
{var gradientlength = document.text_form.grad_text_direction.length;
var i = 0;

    if (grad != "")
        while (i < gradientlength)
	    if (document.text_form.grad_text_direction[i].value != grad) {
	        i++;
		if (i >= gradientlength) return -1;
	    }
	    else return i;
	else return -1;
}

function preset_lookupfont(font)
{var fontlength = document.text_form.font.length;
var i = 0;

    if (font == "") 
	return -1;
        else while (i < fontlength)
	    if (document.text_form.font[i].value != font) {
	        i++;
		if (i >= fontlength) return -1;
	    }
	    else return i;
}

function preset_lookuppallette(text_color1)
{var pallettelength = document.text_form.text_color1.length;

    if (text_color1 != "") {
	var i = 0;
	while (i < pallettelength)
	    if(text_color1 !=document.text_form.text_color1.options[i].value){
		i++;
		if (i >= pallettelength) return -1;
	    }
	    else return i;
    }
    return -1;
}


/* helper function, to look up effects index, or to produce an error.
*/
function preset_lookupeffect(effect)
{var effectlength = document.text_form.effect.length;
var i = 0;

    if (effect != "")
        while (i < effectlength) 
	    if (document.text_form.effect[i].value != effect) {
	        i++;
		if (i >= effectlength) return -1;
	    }
	    else return i;
	else return -1;
}




/* the mother of all presets. the parameters to the routine are in the
   same order and the name as the varibles in the preset variable
   writing code. Get them from there, and just fill them in here.
*/
function setcontrols(
    text,
    font,
    s,
    text_color1,
    plasma_text,
    grad_text,
    grad_text_direction,
    text_color2,
    bg_color1,
    plasma_bg,
    grad_bg,
    grad_bg_direction,
    bg_color2,
    bg_image_check,
    bg_image,
    border,
    border_size,
    border_color,

    effect,
    outline_color2,
    fuzzy_color,
    s_shadow_color,
    outline_color1,
    button_color,
    shadow_color,
    layers,
    layer_color,
    align,
    strokewidth)
{
    /* variables to support processing
    */
    var r;
    var errors = "";

    /* now start setting values in forms of presets. If a color has been
       set, then we have to do a lookup in the table to set the color.
    */
    document.text_form.s.value = s;

    r = preset_lookupfont(font);
    if (r < 0) {
        r = 0;
	errors = errors + "preset " + font + " for font was " +
	     "not found in font list.<br>";
    }
    document.text_form.font.selectedIndex = r;

    if (text_color1 != "") {
	r = preset_lookuppallette(text_color1);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + text_color1 + 
	        " for text color was not found in pallette.<br>";
	}
	document.text_form.text_color1.selectedIndex = r;
    }

    document.text_form.plasma_text.checked = plasma_text;
    document.text_form.grad_text.checked = grad_text;

    if (grad_text_direction != "") {
	r = preset_lookupgradient(grad_text_direction);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + grad_text_direction + 
	        " for text gradient was not found.<br>";
	}
	document.text_form.grad_text_direction.selectedIndex = r;
    }

    if (text_color2 != "") {
	r = preset_lookuppallette(text_color2);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + text_color2 + 
	        " for gradient text color was not found in pallette.<br>";
	}
	document.text_form.text_color2.selectedIndex = r;
    }

    if (bg_color1 != "") {
	r = preset_lookuppallette(bg_color1);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + bg_color1 + 
	        " for gradient text color was not found in pallette.<br>";
	}
	document.text_form.bg_color1.selectedIndex = r;
    }

    document.text_form.grad_bg.checked = grad_bg;

    if (grad_bg_direction != "") {
	r = preset_lookupgradient(grad_bg_direction);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + grad_bg_direction + 
	        " for background gradient direction was not found.<br>";
	}
	document.text_form.grad_bg_direction.selectedIndex = r;
    }

    document.text_form.plasma_bg.checked = plasma_bg;
    
    if (align != "") {
        r = preset_lookupalignment(align);
	if (r < 0) {
	    r = 1;
	    errors = errors + "preset " + align + 
	        " for alignment was not found.<br>";
	}
	document.text_form.align.selectedIndex = r;
    }

    if (bg_color2 != "") {
	r = preset_lookuppallette(bg_color2);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + bg_color2 + 
	        " for gradient background color was not found in pallette.<br>";
	}
	document.text_form.bg_color2.selectedIndex = r;
    }

    document.text_form.bg_image_check.checked = bg_image_check;
    document.text_form.bg_image.value = bg_image;

    document.text_form.border.checked = border;

    if (border_size != "") {
	if (border_size - 1 <= document.text_form.border_size.length)
	    document.text_form.border_size.selectedIndex = border_size - 1;
	else {
	    document.text_form.border_size.selectedIndex = 
		document.text_form.border_size.length - 1;
	    errors = errors + "preset " + border_size + 
		" for border size was not found in pallette.<br>";
	}
    }
    			
    if (border_color != "") {
	r = preset_lookuppallette(border_color);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + border_color + 
	        " for border was not found in pallette.<br>";
	}
	document.text_form.border_color.selectedIndex = r;
    }


    if (outline_color2 != "") {
	r = preset_lookuppallette(outline_color2);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + outline_color2 + 
	        " for psy effect was not found in pallette.<br>";
	}
	document.text_form.outline_color2.selectedIndex = r;
    }

    if (fuzzy_color != "") {
	r = preset_lookuppallette(fuzzy_color);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + fuzzy_color + 
	        " for fuzzy effect was not found in pallette.<br>";
	}
	document.text_form.fuzzy_color.selectedIndex = r;
    }

    if (s_shadow_color != "") {
	r = preset_lookuppallette(s_shadow_color);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + s_shadow_color + 
	        " for sheared shadow effect was not found in pallette.<br>";
	}
	document.text_form.s_shadow_color.selectedIndex = r;
    }

    if (outline_color1 != "") {
	r = preset_lookuppallette(outline_color1);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + outline_color1 + 
	        " for outline effect was not found in pallette.<br>";
	}
	document.text_form.outline_color1.selectedIndex = r;
    }

    if (strokewidth != "") {
	if (strokewidth - 1 <= document.text_form.strokewidth.length)
	    document.text_form.strokewidth.selectedIndex = strokewidth - 1;
	else {
	    document.text_form.strokewidth.selectedIndex = 
		document.text_form.strokewidth.length - 1;
	    errors = errors + "preset " + strokewidth + 
		" for stroke width was not found.<br>";
	}
    }

    if (button_color != "") {
	r = preset_lookuppallette(button_color);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + button_color +
	        " for gel effect was not found in pallette.<br>";
	}
	document.text_form.button_color.selectedIndex = r;
    }

    if (shadow_color != "") {
	r = preset_lookuppallette(shadow_color);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + shadow_color + 
	        " for gel effect was not found in pallette.<br>";
	}
	document.text_form.shadow_color.selectedIndex = r;
    }

    if (layer_color != "") {
	r = preset_lookuppallette(layer_color);
	if (r < 0) {
	    r = 0;
	    errors = errors + "preset " + layer_color + 
	        " for layer effect was not found in pallette.<br>";
	}
	document.text_form.layer_color.selectedIndex = r;
    }

    r = preset_lookupeffect(effect);
    if (r < 0) {
        r = 0;
	errors = errors + "preset " + effect + " for effect was " +
	    "not found in effect list.<br>";
    }
    document.text_form.effect[r].checked = true;
    disable_fx(false,effect);

    /* if there were any errors, pass them out
    */
    return(errors);
}





