var callback = "";

function ToggleAreas(areas){
	
	var TempAreas = areas.split(',');
	
	var max = TempAreas.length;
	
	if(max > 0){
		for(var i = 0; i < max; i++){
			
			var id = TempAreas[i];
			
			$('#area'+id).show();
		}
	}else{
		$('#area'+areas).show();
	}
}

function LoadPackaging(pageid,identifier){
	var a = window.open("/page"+pageid+".aspx?callback="+identifier, "Packaging", "width=500,height=380,status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1");
}

function AddVariationBlockOdd(){
	
	$(".group-block .variation-block:even").css("background-color", "#f9f8f8");
	
}

function ToggleGroup(group){
	
	$("#group"+group+" .group-block").toggle();
}

function PackagingDone(category){
	
	var packid = 0;
	
	$("input[name=onlyone]").each(function(){
		
		if($(this).is(":checked")){
			packid = $(this).val();
		}
	});
	
	window.opener.SavePackageId(packid, category, callback);
	
	window.close();
	
}

function SavePackageId(id, category, in_callback){
	
	var strPackage = category + " - " + id;
	
	$("#"+in_callback).val(strPackage);
	
	$("#"+in_callback +"_chosen").html(strPackage);
	
}

function AdjustActionUrl(prod, pageid){
	
	var action = $("#listform"+pageid).attr("action");

	action = action + "&prod="+prod;
	
	$("#listform"+pageid).attr("action", action);
	
}

function SubmitAndGo(step, pageid){

	var action = $("#listform"+pageid).attr("action");
	
	action = action + "&step="+step;
	
	$("#listform"+pageid).attr("action", action);
	
	$("#listform"+pageid).submit();
	
}

function rumSubmitter(pageid){
	CollectChecks();
	$('#listform' + pageid).submit();
	$('#listform' + pageid).disabled = true;
}

function CollectChecks(){
	
	var collected = '';
	var current = '';
	$('.choicegroup input[type=checkbox]').each(function(index, value){
		
		if(this.checked){
			if(current == this.name){
				var curid = this.id; 
				
				collected = collected + ', ' + $('label[for=' + curid + ']').html();;
				
				$('#collector').val(collected);
			}
			else
			{
				current = this.name;
				var curid = this.id;
				var label = $('label[for=' + this.name + ']').html();
				collected = collected + '\n' + label + $('label[for=' + curid + ']').html();
				
				$('#collector').val(collected);
			}
		}
		
	});	
}

function rumSelect2Checkbox(lang){
	
	var start = lang;
	var langfound = false;
	
	$('select').each(function(key, value){
		
		var currentid = this.id;
		
		if(currentid != ''){
			
			$(this).hide();
			
			var html = '<table cellpadding="0" cellspacing="0" border="0" class="dummy-checkboxes" width="480">';
			html += '<tbody>';
			var counter = 0;
			$('#'+currentid+' > option').each(function(okey, ovalue){
				
				var optionId = $(this).val();
				var optionValue = $(this).html();
				var selected = $(this).is(':selected');
				
				if(optionValue == start){
					langfound = true;
					
				}else if(langfound){
					
					if(optionValue.indexOf('&nbsp') >= 0){
						
						var thevalue = optionValue.replace(/(&nbsp;)+/g, '');
						
						var checked = '';
						if(selected){
							checked = 'checked="checked"';
						}
						
						var index = counter+1;
						if(counter%2 == 0){
							html += '<tr>';
						}
						html += '<td class="choicegroup" width="50%">';
						html += '<input type="checkbox" value="'+optionId+'" name="'+currentid+'_check" id="'+currentid+'_check'+index+'" '+checked+' class="dummy-checkbox"/>';
						html += '<label class="choicelabel" for="'+currentid+'_check'+index+'">'+thevalue+'</label>';
						html += '</td>';
						if((counter+1)%2 == 0){
							html += '</tr>';
						}
						
						counter++;
					}			
				}
			});
			
			html += '</tbody>';
			html += '</table>';
			$(this).after(html);
		
		}
		
	});
}

function rumToggleSelected(theobj){
	
	var value = $(theobj).val();
	
	var name = $(theobj).attr('name');
	name = name.substr(0, name.length-6);
	var parent = name;

	if($(theobj).is(':checked')){

		$('#'+parent+' > option').each(function(key, ovalue){
			
			if(value == $(this).val()){
				this.selected = true;
			}
		});
		
	}else{
		$('#'+parent+' > option').each(function(key, ovalue){
			
			if(value == $(this).val()){
				this.selected = false;
			}	
		});
	}
	
}