﻿function menu(id) {
	var menu=document.getElementById(id);
	if(menu.style.display=="none") menu.style.display="";
	else menu.style.display="none";
}

function pic(pic) {
	document.getElementById('laa').src=document.getElementById(pic).src;
}

function addinput(num) {
	var form=document.getElementById('group');
	var adder=document.getElementById('adder');
	var input=document.createElement('input');
	input.setAttribute('id','dyn'+num);
	input.setAttribute('name','dyn'+num);
	var space=document.createTextNode(' ');
	var pee=document.getElementById('dyninputs');
	var br=document.createElement('br');
	var a=document.createElement('a');
	a.innerHTML="[Poista]";
	a.setAttribute("href","javascript:reminput('dyn"+num+"')");
	pee.appendChild(input);
	pee.appendChild(space);
	pee.appendChild(a);
	pee.appendChild(br);
	num=parseInt(num)+1;
	adder.href="javascript:addinput('"+num+"')";
}

function reminput(rem) {
	var input=document.getElementById(rem);
	var pee=document.getElementById('dyninputs');
	var space=input.nextSibling;
	var a=space.nextSibling;
	var br=a.nextSibling;
	pee.removeChild(input);
	pee.removeChild(space);
	pee.removeChild(a);
	pee.removeChild(br);
}

function part(id, todo) {
	if(todo=="open") document.getElementById(id).style.display="block";
	else if(todo=="close") document.getElementById(id).style.display="none";
}

function addCellsToTable(num) {
	var cellnum=document.getElementById('newcell');
	var plaa=cellnum.value;
	var tbl=document.getElementById('prodlist');
	var tr=document.createElement('tr');
	tr.setAttribute('id','row'+plaa);
	for(var i=0;i<num;i++) {
		var td=document.createElement('td');
		var inp=document.createElement('input');
		inp.setAttribute('class','tbl');
		//inp.setAttribute('value','dyn'+plaa+'_'+i);
		inp.setAttribute('name','dyn'+plaa+'_'+i);
		td.appendChild(inp);
		td.setAttribute('class','top');
		tr.appendChild(td);
	}
	var rem=document.createElement('td');
	var a=document.createElement('a');
	a.setAttribute('href','javascript:remRow(\'row'+plaa+'\')');
	a.innerHTML='[Poista]';
	td.appendChild(a);
	tr.appendChild(td);
	tbl.appendChild(tr);
	plaa++;
	cellnum.value=plaa;
}

function remRow(row) {
	document.getElementById('prodlist').removeChild(document.getElementById(row));
	var cellnum=document.getElementById('newcell');
	var plaa=cellnum.value;
	plaa--;
	cellnum.value=plaa;
}

function addInputs() {
	var div=document.getElementById('dyninputs');
	var inp=document.createElement('input');
	var num=document.getElementById('num').innerHTML;
	inp.setAttribute('type','file');
	inp.setAttribute('name','dynfile'+num);
	inp.setAttribute('id','dynfile'+num);
	div.appendChild(inp);
	var space=document.createTextNode(' dynfile'+num+' ');
	div.appendChild(space);
	var a=document.createElement('a');
	a.innerHTML="[Poista]";
	a.setAttribute("href","javascript:reminput('dynfile"+num+"')");
	div.appendChild(a);
	div.appendChild(document.createElement('br'));
	num++;
	document.getElementById('num').innerHTML=num;
}

function addPart() {
	var arr=new Array('Nimi','Mitat','Sopivuus','Hinta');
	var brands=new Array('eduard','anssems');
	var cats=new Array('wheel','light','brake','axle','pullend','other');
	var catnames=new Array('Renkaat & vanteet','Valot','Jarruosat','Akselit','Vetopää','Muut');
	var adder=document.getElementById('adder').innerHTML;
	var container=document.getElementById('dynparts');
	var div=document.createElement('div');
	div.setAttribute('id','new'+adder)
	var a=document.createElement('a');
	var space=document.createTextNode(' ');
	//sa.setAttribute("href","javascript:removePart('new"+adder+"')");
	//a.innerHTML='[Poista]';
	//div.appendChild(a);
	div.appendChild(space);
	for(i=0;i<arr.length;i++) {
		var txt=document.createTextNode(arr[i]+': ');
		var input=document.createElement('input');
		input.setAttribute('name','new'+adder+'_'+i);
		input.setAttribute('value','new'+adder+'_'+i);
		div.appendChild(txt);
		div.appendChild(input);
		var space=document.createTextNode(' ');
		div.appendChild(space);
	}
	var sel=document.createElement('select');
	sel.setAttribute('name','brand'+adder);
	for(i=0;i<brands.length;i++) {
		var opt=document.createElement('option');
		opt.setAttribute('value',brands[i]);
		opt.innerHTML=brands[i];
		sel.appendChild(opt);
	}
	div.appendChild(sel);
	var sel=document.createElement('select');
	sel.setAttribute('name','category'+adder);
	for(i=0;i<cats.length;i++) {
		var opt=document.createElement('option');
		opt.setAttribute('value',cats[i]);
		opt.innerHTML=catnames[i];
		sel.appendChild(opt);
	}
	div.appendChild(sel);
	var file=document.createElement('input');
	file.setAttribute('type','file');
	file.setAttribute('name','file'+adder);
	div.appendChild(file);
	div.style.marginBottom="25px";
	container.appendChild(div);
	adder++;
	document.getElementById('adder').innerHTML=adder;
}

function removePart(part) {
	document.getElementById('dynparts').removeChild(document.getElementById(part));
}

function editPart(num) {
	var tr=document.getElementById(num);
	for(i=0;i<tr.childNodes.length-1;i++) {
		var inp=document.createElement('input');
		inp.setAttribute('name','edit'+num+'_'+i);
		if(i==3) inp.setAttribute('type','file');
		else {
			inp.setAttribute('value',tr.childNodes[i].innerHTML);
			//tr.childNodes[i].innerHTML="";
		}
		tr.childNodes[i].appendChild(inp);
	}
	tr.childNodes[5].innerHTML="";
	var a=document.createElement('a');
	a.setAttribute("href","javascript:cancelEditing('"+num+"')");
	a.innerHTML="[Peruuta]";
	tr.childNodes[5].appendChild(a);
}

function cancelEditing(num) {
	var tr=document.getElementById(num);
	for(i=0;i<tr.childNodes.length-1;i++) {
		for(j=0;j<tr.childNodes[i].childNodes.length;j++) {
			var input=tr.childNodes[i].childNodes[j];
			if(input.tagName=="INPUT") {
				tr.childNodes[i].removeChild(input);
			}
		}
	}
	tr.childNodes[5].innerHTML="";
	var a=document.createElement('a');
	a.setAttribute("href","javascript:editPart('"+num+"')");
	a.innerHTML="[Muokkaa]";
	tr.childNodes[5].appendChild(a);
	var a=document.createElement('a');
	a.setAttribute("href","?p=perakarryt&removepart="+tr.id.substr(4));
	a.innerHTML="[Poista]";
	tr.childNodes[5].appendChild(a);
}

function addRetailer() {
	var adder=document.getElementById('adder').innerHTML;
	var fields=new Array('name','town','info','www');
	var tbl=document.getElementById('retailers');
	var tr=document.createElement('tr');
	tr.setAttribute('id','newtr_'+adder);
	for(i=0;i<4;i++) {
		var td=document.createElement('td');
		td.setAttribute('class','top');
		var inp=document.createElement('input');
		inp.setAttribute('name','new_'+fields[i]+'_'+adder);
		inp.style.width="90px";
		td.appendChild(inp);
		tr.appendChild(td);
	}
	var td=document.createElement('td');
	td.setAttribute('class','top');
	var a=document.createElement('a');
	a.setAttribute("href","javascript:removeNewRetailer('newtr_"+adder+"')");
	a.innerHTML="[Poista]";
	td.appendChild(a);
	tr.appendChild(td);
	tbl.appendChild(tr);
	adder++;
	document.getElementById('adder').innerHTML=adder;
}

function removeNewRetailer(elem) {
	document.getElementById('retailers').removeChild(document.getElementById(elem));
}

function editRetailer(num) {
	var tr=document.getElementById(num);
	var fields=new Array('name','town','info','www');
	for(i=0;i<4;i++) {
		var inp=document.createElement('input');
		inp.setAttribute('name','edit_'+fields[i]+'_'+num);
		inp.setAttribute('id','edit_'+fields[i]+'_'+num);
		inp.value=tr.childNodes[i].innerHTML;
		inp.style.width="90px";
		tr.childNodes[i].appendChild(inp);
	}
	tr.childNodes[4].firstChild.innerHTML="[Peruuta]";
	tr.childNodes[4].firstChild.href="javascript:cancel('"+num+"')";
}

function cancel(num) {
	var tr=document.getElementById(num);
	var fields=new Array('name','town','info','www');
	for(i=0;i<4;i++) {
		tr.childNodes[i].removeChild(document.getElementById('edit_'+fields[i]+'_'+num));
	}
	tr.childNodes[4].firstChild.innerHTML="[muokkaa]";
	tr.childNodes[4].firstChild.href="javascript:editRetailer('"+num+"')";
}
