//===============================================================================
// SWEDISH
//===============================================================================
// COPYRIGHT ENVIRON STUDIOS 2007. ALLA RÄTTIGHETER RESERVERADE =
// ATT ANVÄNDA KODEN UTAN GODKÄNNANDE ÄR ETT BROTT MOT UPPHOVSRÄTTSLAGEN

//===============================================================================
// ENGLISH
//===============================================================================
// COPYRIGHT ENVIRON STUDIOS 2007. ALL RIGHTS RESERVED.
// USAGE OF THIS CODE WITHOUT PERMISSION IS A VIOLATION OF COPYRIGHT LAWS.
//===============================================================================


function test (){
	test2 = window.innerHeight;
	alert(test2);
}

var totanswers = 0;
var totquestions = 0;
var scheck = 0; 

getqa = new Array(3);


var counter = 0;





function AddAnswer(id,count,name) {

if(document.getElementById(count).count != 5) {
totanswers ++;
document.getElementById(count).count += 1;


//NAME = ANSWER NUMBER
// COUNT = QUESTION THAT ANSWER BELONGS TO


var AddTxt = document.createTextNode("Svar " + name +": ");



var answer = document.createElement('input');
answer.name  = "q"+count + "-an" + name;
answer.id  = "q"+count + "-an" + name;


var AddTxt2 = document.createTextNode(" Poäng: ");

var points = document.createElement('input');
points.type="text";
points.name= "q" + count + "-an" + name + "-p";
points.id= "q" + count + "-an" + name + "-p";


var br = document.createElement('br');

document.getElementById("track"+count).appendChild(AddTxt);
document.getElementById("track"+count).appendChild(answer);
document.getElementById("track"+count).appendChild(br);
document.getElementById("track"+count).appendChild(AddTxt2);
document.getElementById("track"+count).appendChild(points);
document.getElementById("track"+count).appendChild(br);


}
}

// the delete question function

function DeleteQuestion(q,a) {

split = q.split("q"); // split it so that we can check it easier

SBid = split[1];


for(i=SBid; i < 21; i++) {
	
	

		
		
	if(i == SBid) {
				
				// Remove it.
				
				var div = document.getElementById('questions');
				var q = document.getElementById('q'+SBid);
				var t = document.getElementById('track'+SBid);
				var b = document.getElementById('br'+SBid);
				var b2 = document.getElementById('br2'+SBid);
				var btn = document.getElementById(SBid);
				var xbtn = document.getElementById('x' + SBid);
				
		
				div.removeChild(q);
				div.removeChild(t);
				div.removeChild(b);
				div.removeChild(b2);
				div.removeChild(btn);
				div.removeChild(xbtn);
				
				// decrease
				counter --;
				totquestions --;				
				
	
				//alert('removed clicked');
		
	} else {
		
		// Compensate for the loss.

		
		

		
				

		document.getElementById('track'+i).id = "track" +(i-1);
		document.getElementById('br'+i).id = "br" +(i-1);
		document.getElementById('br2'+i).id = "br2" +(i-1);
		
		
		
		
		document.getElementById(i).name =(i-1);
		document.getElementById(i).id =(i-1);
		

		document.getElementById('x'+i).id = "x" +(i-1);
		//document.getElementById('q'+i).value = "q" +(i-1);
		document.getElementById('q'+i).name = "q" +(i-1);
		document.getElementById('q'+i).id = "q" +(i-1);
		
		
		
		
		//==============================================
		// also compensate for the loss in the children
		//==============================================
		
		
		var element = (document.getElementById((i-1)).count);
		if(element != 1) {
			
			for(x = 1; x < element; x++)  { // loop through the children.
			
			
			
			
			document.getElementById("q" + i + "-an" + x).name = "q"+ (i-1) + "-an" + x;
			document.getElementById("q" + i + "-an" + x + "-p").name = "q" + (i-1) + "-an" + x + "-p";
			
			
			//document.getElementById("q" + i + "-an" + x).value = "q"+ (i-1) + "-an" + x;
			//document.getElementById("q" + i + "-an" + x + "-p").value = "q" + (i-1) + "-an" + x + "-p";
			
			document.getElementById("q" + i + "-an" + x).id = "q"+ (i-1) + "-an" + x;
			document.getElementById("q" + i + "-an" + x + "-p").id = "q" + (i-1) + "-an" + x + "-p";
			
			//alert("q" + (i-1) + "-an" + x + "-p");
			
			//document.getElementById("q" + i + "-an" + x + "-p").name = "q" + (i-1) + "-an" + x + "-p";
			//document.getElementById("q" + i + "-an" + x).name = "q"+ (i-1) + "-an" + x;
			
			/*document.getElementById("q" + i + "-an" + x + "-p").id = "q" + (i-1) + "-an" + x + "-p";
			document.getElementById("q"+ i + "-an" + x).id = "q"+ (i-1) + "-an" + x;			*/
			}
		
		}
		
			
		//==============================================	
		// END
		//==============================================
		
		
		
	}
	
}
	
		
		

return true;
}
// ==================================



function AddForm() {

if(totquestions != 40) {
counter ++;
totquestions ++;






var AddFormQ = document.createElement('<input>'); // lägg till en input och sätt attributen
AddFormQ.type ='text';
AddFormQ.name ='q' + counter;
AddFormQ.id = 'q' + counter;
//AddFormQ.value = counter;


var AddBr   = document.createElement('br'); // och en <br> såklart
AddBr.id    = "br"+counter;

var AddBttn = document.createElement('<input>'); //knappen
AddBttn.type ="button";
AddBttn.value ="Lägg till svar";
AddBttn.name = counter;
AddBttn.id = counter;
AddBttn.count = 1;

var track = document.createElement('<div>');
track.id = 'track'+counter;
track.value ='track'+counter;

AddBttn.onclick = function () {AddAnswer(AddFormQ.id,AddBttn.name, AddBttn.count)};


var XButton = document.createElement('input'); //X knappen
XButton.type ="button";
XButton.value ="X";
XButton.id = "x" + AddBttn.id;


XButton.onclick = function () {DeleteQuestion(AddFormQ.id,AddBttn.id)};// send is the current id


var AddBr2   = document.createElement('br');
AddBr2.id = "br2"+counter;

div = document.getElementById("questions");

// lägg till allt tillslut
div.appendChild(AddBr2);
div.appendChild(AddFormQ);
div.appendChild(AddBttn);
div.appendChild(XButton);
div.appendChild(AddBr);
div.appendChild(track);




}

}
function SubmitData() {

var pointcheck = 1;

div = document.getElementById("questions");

var info2   = document.createElement('<input>');
info2.name  = "totans";
info2.value = totanswers;
info2.type = "hidden";
div.appendChild(info2);

var i = 1;
var a = 1;

test = "0";



if( (totquestions < 5) || (totanswers < 10) )
{
alert("Du måste ha minst fem frågor och tio svarsalternativ!")

} else {

quizname    = document.getElementById("quizname").value.length;
description = document.getElementById("description").value.length;
bgcolor = document.getElementById("bgcolor").value.length;
bgcolor2 = document.getElementById("bgcolor2").value.length;
qcolor = document.getElementById("qcolor").value.length;
acolor = document.getElementById("acolor").value.length;


// check the forms to see if they are filled in...
if( (quizname == "") || (description == 0) || (bgcolor == 0) || (bgcolor2 == 0) || (qcolor == 0) || (acolor == 0) ) 
{ 
alert("Du har inte fyllt in alla fält.");
} else {
//document.newquiz.submit();

for( i=0; i < totquestions+1; i++ ) {


for(a =0; a < 5; a ++) {
if(document.getElementById("q"+i+"-an" + a+"-p")) {
acheck = document.getElementById("q"+i+"-an" + a+"-p").value;

if (acheck != "") {
var reg = /^[0-9]+$/;
	if(acheck.match(reg)){
	} else { 
 
  test ++;
  }


} else {

test ++;
}
}

}
}

if (test == 0) {

if(scheck == "0") {
document.newquiz.submit();
scheck = 1;
} else {

alert('Tryck bara på knappen en gång.');
}
} else {
alert('Du har antingen tomma eller felaktigt ifyllda formulär. Poäng måste vara siffror. Frågor och svar får inte lämnas tomma.');

}


pointcheck = 1;

}
}
}




function ShowExample() {

	
	
	
	bgcolor  = document.getElementById('bgcolor').value;
	bgcolor2 = document.getElementById('bgcolor2').value;
	qcolor   = document.getElementById('qcolor').value;
	acolor   = document.getElementById('acolor').value;

	if(bgcolor != "") {
	
		document.getElementById('box1').style.background ='#'+bgcolor;

	}
	
	if(bgcolor2 != "") {
	
		document.getElementById('box2').style.background ='#'+bgcolor2;

	}	
	
	if(qcolor != "") {
	
		document.getElementById('box4').style.color ='#'+qcolor;

	}		
	
	if(acolor != "") {
	
		document.getElementById('box5').style.color ='#'+acolor;

	}			
}

function LoadColor(element) {
	
if(element == 1) {
	sBgColor =  document.getElementById('sel1').value;
	
	document.getElementById('bgcolor').value = sBgColor;
}

else if(element == 2) {
	sBgColor =  document.getElementById('sel2').value;
	
	document.getElementById('bgcolor2').value = sBgColor;
}
else if(element == 3) {
	sBgColor =  document.getElementById('sel3').value;
	
	document.getElementById('qcolor').value = sBgColor;
}	
else if(element == 4) {
	sBgColor =  document.getElementById('sel4').value;
	
	document.getElementById('acolor').value = sBgColor;
}
}