var obj, obj2;

function onStart(){
	

	// JSon stuff
	
	document.getElementById('cmbColors').onchange = updateCombo;	
	document.getElementById('cmbSizes').onchange = update3Combo;
	
	json=document.getElementById("json_text").value;
	json2=document.getElementById("json_text2").value;
	 
	
	obj=eval('('+json+')');	
	obj2=eval('('+json2+')');
	
	
	document.createElement("ul");
	for(val in obj){	    
	    load_combo ("cmbColors", obj[val]);		
	}		
	
	// Forse first update
	updateCombo();
	
	// Automatic Gallery stuff

	for (i=0; i<numImages; i++)
	{
	  dimages[i]=new Image();    
	  dimages[i].src=document.getElementById('image'+i).value;
	  
		
	}
	
	swapPicture();
	setTimeout("swapPicture()", 5000);
	
}
// Sacado de = http://java-programming.suite101.com/article.cfm/creating_dynamic_comboboxes_with_javascript
function add_option (select_id, text) {
	var select = document.getElementById(select_id);
	select.options[select.options.length] = new Option(text);
}

function clear_combo (select_id) {
	var select = document.getElementById(select_id);
	select.options.length = 0;
}

function load_combo_condition (select_id, option_array, condition) {

	
	for (var i = 0; i < option_array.length; i++) {		
		
		if (trim(option_array[i])==condition){				
				add_option (select_id, option_array[i+1]);				
			}			
	}
}


function load_3combo_condition (select_id, option_array, condition1, condition2) {
	
	for (var i = 0; i < option_array.length; i++) {		
		if (trim(option_array[i])==condition1){
			if (trim(option_array[i+1])==condition2){				
				addNumbers(select_id, option_array, i);				
			}							
		}			
	}
}

function addNumbers(select_id, option_array, index)
{
	for(var i=1; i<=option_array[index+2]; i++){
		add_option (select_id, i);
		document.getElementById('skuID').value = option_array[index+3];
	}
}

function load_combo (select_id, option_array) {				
			
	for (var i = 0; i < option_array.length; i++) {
		if (typeof(option_array[i]) != "undefined")			
			add_option (select_id, option_array[i]);
	}
}

function updateCombo () {
	 
	clear_combo ("cmbSizes");
	
	for(val2 in obj2){	   

		var combo = document.getElementById("cmbColors");	
		var indice = combo.selectedIndex;
	    var valor = combo.options[combo.selectedIndex].text;			
		load_combo_condition("cmbSizes", obj2[val2],valor);				
	}		

	// Update the last combo as well
	update3Combo();		
}

function update3Combo () {	 
	
	clear_combo ("cmbQuantity");	
	
	for(val2 in obj2){	   

		var combo = document.getElementById("cmbColors");	
		var indice = combo.selectedIndex;
	    var valor = combo.options[combo.selectedIndex].text;

	    var combo2 = document.getElementById("cmbSizes");	
		var indice2 = combo2.selectedIndex;
	    var valor2 = combo2.options[combo2.selectedIndex].text;		
	    		
		load_3combo_condition("cmbQuantity", obj2[val2],valor,valor2 );				
	}			
}



function trim(str)
{
    if(!str || typeof str != 'string')
        return null;

    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
}


function roll(img_name, img_src){
   document[img_name].src = img_src;
}

var hide= true;

var isHidden=new Array();
isHidden[0] = false;
isHidden[1] = false;
isHidden[2] = false;
isHidden[3] = false;

function toggleRows(start, end, tag, who){

	who= 0;
	tableId= "myTable";
	tbl = document.getElementById(tableId);

	var len = tbl.rows.length;
	var vStyle = (isHidden[who])? "none":"";

	for(i=start ; i<end; i++){
		tbl.rows[i].style.display = vStyle;
	}


	if(hide){		
		isHidden[who]= !isHidden[who];
	}else{
		
		isHidden[who]= !isHidden[who];
		
	}

}


function load(){
	toggleRows(5,10, this);

}

function toggleMenu(){
	toggleRows(1,21, this);
}


