//This file was originally created by TBWA\Play
//Modified to acess a auto-generated list of themes

function createstep(step){
	//Handles changing between steps and uses handlestep to hide or show a given step
	if (step==1) {
		//tema
		handlestep(1,"block");
		handlestep(3,"none");
		handlestep(2,"none");
		handlestep(4,"none");
		
		//Show/hide buttons 
		$("#lavlod_btnnext").css("display","inline");
		$("#lavlod_btnudskriv").css("display","none");
		$("#lavlod_btnprev").css("display","none");
		$("#lavlod_btnclear").css("display","none");
		lodstep++;
	}
	if (step==2) {
		//tekst	
		handlestep(2,"block");
		handlestep(1,"none");
		handlestep(3,"none");
		
		//Now the back button is relevant, show it
		$("#lavlod_btnprev").css("display","inline");
		lodstep++;
	}
	if (step==3) {
		//udskriv
		getLod();
		handlestep(3,"block");
		handlestep(1,"none");
		handlestep(2,"none");
		
		//hide next button, we're at last step		
		$("#lavlod_btnnext").css("display","none");
		
		$("#lavlod_btnprev").css("display","none");
		
		//show buttons for this stop
		$("#printbuttons").css("display","inline");
//		$("#lavlod_btnudskriv").css("display","inline");
//		$("#lavlod_btnclear").css("display","inline");
		
		lodstep++;
	}
	if (step==4) {
		//make sure the user wants to clear
		 if (confirm("Ønsker du at slette indholdet af den nuværende lodseddel?")){
			//reset and restart at step 1
			lodstep=1;
			//hide step 3
			handlestep(3,"none");
			//clear the fields
			clearlod();
			//return to step1
			createstep(lodstep);
		 }	
	}
}

function handletextarea(controlID){
			//get the textfield contents	
			//var fritekst=$('#fritekstfelt').attr('value')+"";
			var fritekst=$(controlID).attr('value')+"";
			
			if (fritekst.indexOf("undefined")>-1) $('#preview_valgfri').html(" ");
			//If its not empty or undefined
			if (fritekst.length>0 & !fritekst.indexOf("undefined")>-1){
				//replace newlines with html breaks to let user force a line shift
				$('#preview_valgfri').html(fritekst.replace(/\n/g,'<br>'));
			}	
}

function handlestep(step,displaytype){
	//Sets a given step's [display: X] to "displaytype"
	if (step==1){
		//Tema
		$("#lavlod_tematab").css("display",displaytype);
		$("#lavlod_tema").css("display",displaytype);
	}
	if (step==2){
		//Tekst
		$("#lavlod_teksttab").css("display",displaytype);
		$("#lavlod_tekst").css("display",displaytype);
	}
	if (step==3){
		//Udskriv
		$("#lavlod_udskrivtab").css("display",displaytype);
		$("#lavlod_udskriv").css("display",displaytype);
	}
}

function openprintlod(address){
	//Opens the print window (popup window) to make the print-size larger
	//var address="lavlod_printwindow.html";

	//Get parameters - o1 o2 lodno theme valgfri fritekstfelt
	var o1=$("#overskrift1").attr("value");
	var o2=$("#overskrift2").attr("value");
	var lodno=$("#preview_lodnummer").html();
	var lodno2=$("#preview_lodnummer2").html();
	var valgfri=$("#fritekstfelt").attr("value")+"";
	
	if (valgfri.length>0 & !valgfri.indexOf("undefined")>-1){
		//replace newlines with html breaks to let user force a line shift
		valgfri=valgfri.replace(/\n/g,'<br>');
	}	
	
	//Pass the info to the printing window
	var query="?o1="+escape(o1)+"&o2="+escape(o2)+"&valgfri="+escape(valgfri)+"&lodno="+escape(lodno)+"&lodno2="+escape(lodno2)+"&theme="+current_theme;
	window.open(address+query,'printwindow','width=990,height=708')
	return false;
}

function clearlod(){
	//clears the fields when the user wants to create a new one
	$('#preview_lodnummer').html("");
	$('#preview_lodnummer2').html("");
	$('#preview_overskrift1').html("");
	$('#preview_overskrift2').html("");
	$('#preview_valgfri').html("");
	
}
function testjs(text1,text2){	
//  alert(text1);
//  alert(text2);
}

function changetheme(name, themeNameControlID, state){	
	//used when sending info to the printwindow
//	alert(state);
	current_theme=name;
	

	if (themeNameControlID!=null){
	  $(themeNameControlID).attr('value',name);
	}
  
	//HACK: changes margins if name contains "gammel" to avoid text overlap
	//adjust text positions for "gammel" themes
	if (name.indexOf("gammel")==-1){
		//set margin back (or overwrite it)
		$('#preview_texts').css('margin-top','50px');
		$('#preview_texts').css('margin-left','82px');
	}else{
		//move margin
		$('#preview_texts').css('margin-top','35px');
		$('#preview_texts').css('margin-left','140px');
		
	}
	imagepath = getThemeImage(name,themePictureSize.Medium);
	
	var previewObj = document.getElementById("preview_theme");
	previewObj.style.backgroundImage="url(" + imagepath + ")";
	
	//border on selected
	$("#sel_"+name).attr("class", "selectedtheme");
	
	//remove from previous
	$("#sel_"+last_theme).attr("class", "");
	
	//update for next click
	last_theme=name;
}

function getLod(){
	//live update of selected "lod" - divides into multiple lines
	var selObj = document.getElementById(TicketListControlID);
	//Counts the amount of selected fields
	var count=0;
	
	//One string per ticket field
	var lod1string="";
	var lod2string="";
	
	//Iterate through the selectedlod-object and get the selected values
	//divide into 2 strings depending on count
	for (i=0; i<selObj.options.length; i++) {
		 if (selObj.options[i].selected) {
			if (count <= 2) lod1string += "<span style='padding-right: 10px'>" + selObj.options[i].value + "</span>";
			else lod2string += "<span style='padding-right: 10px'>" + selObj.options[i].value + "</span>";
			count++;
		 }
	}

	//print to the preview
	$("#preview_lodnummer").html(lod1string);
	$("#preview_lodnummer2").html(lod2string); 
	
}

//Printvindue
function getPrintParms(){
	
	//get the url parameters
	var overskrift1 = unescape($.getURLParam("o1"));
	var overskrift2 = unescape($.getURLParam("o2"));
	var lodnummer=unescape($.getURLParam("lodno"));
	var lodnummer2=unescape($.getURLParam("lodno2"));
	var theme = ($.getURLParam("theme"));
	var valgfri = unescape($.getURLParam("valgfri"))+"";
	
	//Insert them on the print preview
	if (validtext(overskrift1)) $('#print_overskrift1').html(overskrift1);
	if (validtext(overskrift2)) $('#print_overskrift2').html(overskrift2);
	if (validtext(lodnummer)) $('#print_lodnummer').html(lodnummer);
	if (validtext(lodnummer2)) $('#print_lodnummer2').html(lodnummer2);
	if (validtext(valgfri)) $('#print_valgfri').html(valgfri);
	imagepath = getThemeImage(theme,themePictureSize.Normal);
	//imagepath = "/images/lavlod/temaer/palmer_small.jpg";
	if (validtext(theme)) $("#print_stortbillede").attr("src",imagepath);
	else $("#print_stortbillede").attr("src",imagepath);

	//added check to move layout if the theme is of the "gammel lodseddel" variant
	if (theme.indexOf("gammel")==-1){
		//set margin back (or overwrite it)
		$('#print_texts').css('margin-left','190px');
		$('#print_texts').css('margin-top','130px');
	}else{
		//move margin
		$('#print_texts').css('margin-left','280px');
		$('#print_texts').css('margin-top','60px');
	}
	
}

function validtext(input){
	//simple variable check to help when parsing the url params
	return (!(input=='null'||input==null||input=='undefined'));			
}

