var chain = new Array();
var obj = new Object();
var TheForm = new Object();
var throttle = false;
var do_ps = 0;
IAUpdatePreview = function(type,id,TheForm){
	switch(type){
		case 'chain':
			for(i=0;i<TheForm.elements.length;i++){
				if(TheForm.elements[i].type == 'radio'){
					TheForm.elements[i].disabled = true;
					}
				}
			if(chains_loaded){
				if(chain_array[id]){
					chain = chain_array[id];
					chain['id'] = id;
				}else{
					return false;
					}
				d = document;
				obj = d.getElementById('preview_chain');
				new Effect.BlindUp('preview_chain',{
							duration:.5,
							queue:{scope:'myscope', position:'end', limit: 1},
							afterFinish:function(){
									IACloseEnd(obj,chain,TheForm);
									new Effect.BlindDown('preview_chain',{
											duration:1,
											queue:{scope:'myscope', position:'end', limit: 1},
											afterFinish:function(){
												//alert('?');
												IAOpenEnd(obj,chain,TheForm);
												}
											}
										)
									}
							}
						);
				}
		break;
		case 'color':
		
		break;
		case 'text':
		
		break;
		}
	
	
	}
	
IACloseEnd = function(obj,chain,TheForm){
	obj.innerHTML = '';
	img = d.createElement('img');
	img.src = '/images/build/'+chain['img'];
	img.alt = chain['title'];
	img.style.marginTop = 0;
	
	h3 = d.createElement('h3');
	h3.innerHTML = chain['title'];
	
	obj.appendChild(img);
	return true;
	}
	
IAOpenEnd = function(obj,chain,TheForm){
	for(i=0;i<TheForm.elements.length;i++){
		if(TheForm.elements[i].type == 'radio'){
			TheForm.elements[i].disabled = false;
			}
		}
	//alert('hello!');
	return true;
	}
	
periodic_submit = function(){
	if(throttle == true){
		clearTimeout(do_ps);
		do_ps = setTimeout('periodic_submit()',1000);
		return false;
		}	
	setTimeout('throttle = false;',10000);
	throttle = true;
	a = document.getElementById('preview_main_image');
	z = document.getElementById('loading_gif');
	z.style.height = parseInt(a.height)+'px';
	a.style.display = 'none';
	z.style.display = 'block';
	obj = document.getElementById('mainForm');
	param = new Array();
	for(i=0;i<obj.elements.length;i++){
		el = obj.elements[i];
		var n = el.name;
		if(n.match(/^text/)){
			param[i] = el.name+'='+encodeURI(el.value);
			}
		}
	params = param.join("&");
	
	new Ajax.Request('/build.id?customize/ajax', {
			parameters:params,
			method:'post',
			onSuccess:periodic_submit_complete,
			onFailure:periodic_submit_failure
			});
	return false;
	}
	
count_chars = function(E){
	if(!E){ E = window.event; }
	/* DEBOOG 
	text = "GO:<br />\n";
	for(x in E){
		text += x+':'+E[x]+"<br />\n";
		}
	document.write(text);
	return;
	*/
	if(E){
		if(E.type.toLowerCase() == 'keyup'){
			document.title=E.keyCode;
			if(E.keyCode){
				if(E.keyCode == 68){
					return;
					}
				}
			}
		}
	if(E.target){
		x = E.target;
	}else{
		x = E.srcElement;
		}
	
	/* DEBOOG!
	text = '';
	for(i in x){
		text += i+':'+x[i]+"\n<hr />";
		}
	document.write(text);
	return;
	*/
	
	max = x.maxLength-1;
	var new_value = x.value;
	new_value = new_value.toUpperCase();
	new_value = new_value.replace(/[^A-Z0-9?%*\[\]()&:+\/"\.,#\'\s-]/gi,'');
	if(x.value != new_value){
		x.value = new_value;
		}
	y = x.value;
	if (y.length>max){
		x.value = y.substring(0,max);
		var next=x.tabIndex;
		if (next<x.form.length){
			x.form.elements[next].focus()
			}
		}	
	counter = document.getElementById('r_'+x.name);
	counter.innerHTML = x.value.length;
	}
	
var periodic_submit_complete = function(t) {
	setTimeout('show_update()',800);
	}
	
var show_update = function(){
	obj = document.getElementById('preview_main_image');
	var a = ''+obj.src;
	end = a.substring(a.length-4,a.length);
	if(end != 'XXXX'){
		b = a.substring(0,a.length-4)+Math.round(Math.random()*1000 + 1000);
		obj.src = b;
		}
	obj.style.display = 'block';
	z = document.getElementById('loading_gif');
	z.style.height = parseInt(obj.height)+'px';
	z.style.display = 'none';
	}

var periodic_submit_failure = function(t) {
	setTimeout('show_update()',250);
	}