function GetId(id){ return document.getElementById(id); }
function GetValue(id){ return document.getElementById(id).value; }
function GetStyle(id){ return document.getElementById(id).style; }
function GetDisplay(id){ return document.getElementById(id).style.display; }

function link_it(page)
{
	document.location.href= page;
}


function upperthat(secure)
{
	if(secure.value)
	{
		var str	= secure.value;
		secure.value = str.toUpperCase();
		for(x=0;x<=9;x++)
		{
			if(secure.value.match(x))
			{
				secure.value = "";
			}
		}
	}
}




// Nutribar Original

function HighLightOn(section,id, color)
{
	document.getElementById('Arrow'+section+'_'+id).style.visibility = 'visible';
	document.getElementById('N_O_'+section+'_'+id).style.backgroundColor = '#'+color;
}
function HighLightOff(section,id,color)
{
	document.getElementById('Arrow'+section+'_'+id).style.visibility = 'hidden';
	document.getElementById('N_O_'+section+'_'+id).style.backgroundColor = '#'+color;	
}





var closing = false; //assume that we are opening as first action

function DisplayMain(id,total,section){ //id means mealbar powder or snacks as 1 2 or 3
	
	var	z = 0; //menu element 0
	var amount_items = 0;
	
	if(GetId(section+'MealBarTitle')){
		var event1 = GetId(section + 'MealBarTitle').onclick; //keep track of the function in order to lock/unlock while animating
	}
	if(GetId(section+'PowderTitle')){	
		var event2 = GetId(section + 'PowderTitle').onclick;
	}
	if(GetId(section+'SnackBarsTitle')){
		var event3 = GetId(section + 'SnackBarsTitle').onclick;
	}
	
	var StateElementClicked = GetDisplay("N_O_"+id+"_0"); //did we just click an open or closed element
	
	if(StateElementClicked == 'none'){ //if was closed
	
		if(GetId(section+'MealBarTitle')){
			amount_items++;
			document.getElementById(section + 'MealBarTitle').onclick = ''; //lock 3 panels
		}
		if(GetId(section+'PowderTitle')){
			amount_items++;
			document.getElementById(section + 'PowderTitle').onclick = '';
		}
		if(GetId(section+'SnackBarsTitle')){
			amount_items++;
			document.getElementById(section + 'SnackBarsTitle').onclick = '';		
		}
		
		
		for(x=1;x<=amount_items;x++){ //loop to see if we need to close a panel first
			if(x!=id){
				if(GetDisplay("N_O_"+x+"_0") != 'none'){
					var closing = true;
					close_item(x,document.getElementById('Total_'+x).value,section); //close the open panel
				}
			}
		}
		if(closing) //if we had to close a panel, wait a little bit to give it the time to be done closing
		{			
			setTimeout(function(){
				open_item(id,total,0,event1,event2,event3,section); //open new panel
			}, 800);			
		}else{
			open_item(id,total,0,event1,event2,event3,section); //open new panel
		}
	}else{
		var closing = true;
		close_item(id,document.getElementById('Total_'+id).value,section); //we want to close the clicked panel
	}
}
function open_item(id_to_open,total_to_open,z,event1,event2,event3,section){
	document.getElementById('N_O_' + id_to_open + '_' + z).style.display = '';
	setTimeout(function(){
		if(z!=total_to_open){
			document.getElementById('N_O_' + id_to_open + '_up').style.display = '';						
			z++;
			open_item(id_to_open,total_to_open,z,event1,event2,event3,section);
		}else{
			document.getElementById('N_O_' + id_to_open + '_up').style.display = '';						
			document.getElementById('N_O_' + id_to_open + '_dw').style.display = '';			
			clearAll('open',event1,event2,event3,section);	
		}
	}, 60 );
}
function close_item(id_to_close,total_to_close,section){
	document.getElementById('N_O_' + id_to_close + '_' + total_to_close).style.display = 'none';
	setTimeout(function(){
		if(total_to_close!=0){
			document.getElementById('N_O_' + id_to_close + '_dw').style.display = 'none';						
			total_to_close--;
			close_item(id_to_close,total_to_close,section);
		}else{
			document.getElementById('N_O_' + id_to_close + '_dw').style.display = 'none';						
			document.getElementById('N_O_' + id_to_close + '_up').style.display = 'none';			
			clearAll('close',section);	
		}
	}, 60 );	
}
function clearAll(action,event1,event2,event3,section){
	var z = 0;
	var closing = false;
	if(action=='open'){
		if(GetId(section+'MealBarTitle')){ document.getElementById(section + 'MealBarTitle').onclick = event1;}
		if(GetId(section+'PowderTitle')){ document.getElementById(section + 'PowderTitle').onclick = event2;}
		if(GetId(section+'SnackBarsTitle')){ document.getElementById(section + 'SnackBarsTitle').onclick = event3;}
	}
}
