// Javascript support for BBB Ring Forms Any page which used ring
// forms needs to include this file, usually this is done by putting
// the following in the head
// <script type="text/javascript" src="ringforms.js"></script>
//

function anodized_ring_submit(myform){
	try {
		
		var opt = new String() ;
		var mycolors=new Array();
		var colorinputs= new Array();
		var sizeinputs= new Array();

		// how many rings per group, encoded as hidden field
		var pergroup = parseInt(myform.pergroup.value) ;
		
		// Stop looking for colors when we will get an error asking
		// for a non-existing color select.  You can have 5 or less
		// color selectors.  Each one is worth 250 rings.  -- CLB
		try {
			colorinputs.push(myform.color1.value) ;
			colorinputs.push(myform.color2.value) ;
			colorinputs.push(myform.color3.value) ;
			colorinputs.push(myform.color4.value) ;
			colorinputs.push(myform.color5.value) ;
		} catch (error)  {}
		

		try {
			sizeinputs.push(myform.size1.value) ;
			sizeinputs.push(myform.size2.value) ;
			sizeinputs.push(myform.size3.value) ;
			sizeinputs.push(myform.size4.value) ;
			sizeinputs.push(myform.size5.value) ;
		} catch (error)  {}
	
		for ( var sel in colorinputs )
		{
			var color = colorinputs[sel] ;
			var size = sizeinputs[sel] ;
			if ( typeof color == "string" )
			{
				if ( typeof size == "string" )
				{
					color = size + "-" + color ;
				}
				if ( isNaN( mycolors[color] ) ) {
					mycolors[color] = pergroup;
				}
				else {
					mycolors[color] += pergroup ;
				}
			}
		}
		
		first=true;
		for ( var i in mycolors ) {
			if ( ! first) { opt= opt + ",  "; }
			opt = opt + mycolors[i] + "x" + i;
			first = false;
		}
		myform.on0.value = "Colors";
		myform.os0.value = opt;
//		alert("Opt: " + opt); return false;
		return true;
	} catch (error){
		alert("Error: " + error );
	}
}

function anodized_ring_init(){
    for ( var i = 0; i < document.forms.length; i++ )
	{ initialize_colors(document.forms.item(i)); }
    return true;
}
	
function initialize_colors(form) {
	try {
		update_colors(form.size1);
		update_colors(form.size2);
		update_colors(form.size3);
		update_colors(form.size4);
		update_colors(form.size5);
	} catch (error) { }
}

function update_colors(sizesel) {
        var form = sizesel.form ;
	var colorid = sizesel.name.replace(/size/,"color");
	var size = sizesel.value ;
	var colorsel = eval("form." + colorid) ;
	var colorspec = eval("form." + size + "_colors.value");
	var colors = colorspec.split(",");
	update_options(colorsel, colors);
}


function update_options(selector, options) {
	// remove options

	var sel = selector.value ;
	while (selector.length > 0)
        {
		selector.remove(selector.length-1);
        }
	// add new color options
	for ( var i in options ) 
	{	
		selector.options[selector.options.length] = new Option(options[i], options[i]);
		if ( sel == options[i] )
		{ selector.value = sel; }
	}	
}

function allthiskind(formid) {
	// first we snc the size
	var form = document.getElementById(formid) ;
	try {
		var size = form.size1.value ;
		form.size2.value = size ;
		update_colors(form.size2);
		form.size3.value = size ;
		update_colors(form.size3);
		form.size4.value = size ;
		update_colors(form.size4);
		form.size5.value = size ;
		update_colors(form.size5);
	} catch (error) {};

	// then we sync colors
	try {
		var color = form.color1.value ;
		form.color2.value = color ;
		form.color3.value = color ;
		form.color4.value = color ;
		form.color5.value = color ;
	} catch (error) {}
	return true;
}

