var callback = "";

function ToggleAreas(areas){
	
	var TempAreas = areas.split(',');
	
	var max = TempAreas.length;
	
	if(max > 0){
		for(var x = 0; x < max; x++){
			
			var id = TempAreas[x];
			
			$('#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(selectid, lang, type, rows){
	
	var start = lang;
	var langfound = false;
		
	$("#"+selectid).hide();
	
	var width = Math.floor(100/rows);
	
	var html = '<table cellpadding="0" cellspacing="0" border="0" class="dummy-checkboxes" width="480">';
	html += '<tbody>';
	var counter = 0;
	$('#'+selectid+' > 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%rows == 0){
					html += '<tr>';
				}
				html += '<td class="choicegroup" width="'+width+'">';
				html += '<input type="'+type+'" value="'+optionId+'" name="'+selectid+'_check" id="'+selectid+'_check'+index+'" '+checked+' class="dummy-checkbox"/>';
				html += '<label class="choicelabel" for="'+selectid+'_check'+index+'">'+thevalue+'</label>';
				html += '</td>';
				if((counter+1)%rows == 0){
					html += '</tr>';
				}
				
				counter++;
			}else{
				langfound = false;
			}			
		}
	});
	
	if(!rumIsEven(counter)){
		html += '</tr>';
	}
	
	html += '</tbody>';
	html += '</table>';
	
	$("#"+selectid).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;
			}	
		});
	}
}

function rumIsEven(number){
	return (number%2 == 0) ? true : false;
}
