// JavaScript Document
var i = 2;

var className_przedOLD = "sel_off";
var className_przed = "sel_on";

var lastCreatedDiv_id = "DIV[1]";

var text="";



var Tab= new Array();
var Tab2= new Array();

function DivMove(iddiv) {
	
	//alert(className_przed);
	tmp = className_przedOLD;
	className_przedOLD = className_przed;
	className_przed = tmp;
	document.getElementById(iddiv).className = className_przed;
}

function DivOut(id,iddiv) {
	//alert(className_przedOLD);
	//alert(className_przedOLD);
	//if (className_przedOLD == 'sel_off') {
		//document.getElementById(iddiv).className = 'sel_off';
	//}//else {
	/*if (className_najechaniemOLD == 'sel_on') {
		document.getElementById(iddiv).className = 'sel_on'
	}*/
}

function DivClick(class1, id,iddiv) {
	//alert(iddiv);
	//className_przedOLD = className_przed;
	
	//checkboxName = (id+'['+iddiv+'_input'+']');
	
		if (id+'_D1' == iddiv) {
			checkboxName = id+'[D1]';
		}
		if (id+'_D2' == iddiv) {
			checkboxName = id+'[D2]';
		}
		if (id+'_K1' == iddiv) {
			checkboxName = id+'[K1]';
		}
		if (id+'_K2' == iddiv) {
			checkboxName = id+'[K2]';
		}
	
	//alert(String(checkboxName));
	
	if (class1 == 'selAll') {
		///alert(1);
		//className_przed = 'sel_on';
		document.getElementById(iddiv).className = 'sel_on';
		//document.ZAMFORM.checkboxName.checked = "true";
		document.getElementById(checkboxName).checked = true;
		document.getElementById(checkboxName).checked = true;
		document.getElementById(id+'d1').className = 'selAll2';
	}//else {
	else if (class1 == 'selAll2') {
		//alert(2);
		//className_przed = 'sel_off';
	document.getElementById(iddiv).className = 'sel_off';
		//document.ZAMFORM.checkboxName.checked = "false";
		document.getElementById(checkboxName).checked = false;
		document.getElementById(id+'d1').className = 'selAll';
	}
	else if (class1 == 'sel_off') {
		//alert(2);
		//className_przed = 'sel_off';
	document.getElementById(iddiv).className = 'sel_on';
		//document.ZAMFORM.checkboxName.checked = "false";
		document.getElementById(checkboxName).checked = true;
		//document.getElementById('d1').className = 'selAll';
	}
	else if (class1 == 'sel_on') {
		//alert(2);
		//className_przed = 'sel_off';
	document.getElementById(iddiv).className = 'sel_off';
		//document.ZAMFORM.checkboxName.checked = "false";
		document.getElementById(checkboxName).checked = false;
		//document.getElementById('d1').className = 'selAll';
	}
}


function CreateDiv() {
	
//<!--var text = '<div class="zamDiv" id="DIV['+i+']"><div id="DIV['+i+']_D1" class="sel_off" onmousemove="javascript:DivMove(this.className,this.parentNode.id,this.id);"onclick="javascript:DivClick(this.parentNode.id,this.id);">D</div><div id="DIV['+i+']_D2" class="sel_off" onmousemove="javascript:DivMove(this.className,this.parentNode.id,this.id);" onclick="javascript:DivClick(this.parentNode.id,this.id);">D</div><div id="DIV['+i+']_K1" class="sel_off" onmousemove="javascript:DivMove(this.className,this.parentNode.id,this.id);" onclick="javascript:DivClick(this.parentNode.id,this.id);">K</div><div id="DIV['+i+']_K2" class="sel_off" onmousemove="javascript:DivMove(this.className,this.parentNode.id,this.id);" onclick="javascript:DivClick(this.parentNode.id,this.id);">K</div>     <input name="DIV['+i+'][D1]" type="checkbox" class="" id="DIV['+i+'][D1]" style="display:none"  /><input name="DIV['+i+'][D2]" type="checkbox" class="" id="DIV['+i+'][D2]" style="display:none"  /><input name="DIV['+i+'][K1]" type="checkbox" class="" id="DIV['+i+'][K1]" style="display:none"  />                <input name="DIV['+i+'][K2]" type="checkbox" class="" id="DIV['+i+'][K2]" style="display:none"  />     <div class="zamInput"><input name="DIV['+i+'][wys]" type="text" class="" id="DIV['+i+'][wys]"  /></div>                <div class="zamInput"><input name="DIV['+i+'][szer]" type="text" class="" id="DIV['+i+'][szer]"  /></div>                <div class="zamInput"><input name="DIV['+i+'][szt]" type="text" class="" id="DIV['+i+'][szt]" onkeydown="showEv(this.id);"   /></div>         <div class="zamInput"><input name="DIV['+i+'][sloje]" type="checkbox" class="checkbox_zamTab" id="DIV['+i+'][sloje]"  /></div><div id="1" class="divIterator" style="display: none;"></div>                <div class="delDiv" onmouseover="this.className=\'delDiv_on\'" onmouseout="this.className=\'delDiv\'" onclick="removeDiv(this.parentNode.id);">X Usuń</div></div>';-->
	//alert(i);  \\\		`
var text= '<div class="zamDiv" id="DIV['+i+']"><div class="selAll" id="DIV['+i+']d1" onclick="SellAll(this.className, this.parentNode.id);">»</div>   <div class="zamInput"><input type="text" class="" id="DIV['+i+'][clear]" style="width:0px;"  /></div>     <div id="DIV['+i+']_D1" class="sel_off" onclick="DivClick(this.className, this.parentNode.id,this.id);">D</div><div id="DIV['+i+']_D2" class="sel_off" onclick="DivClick(this.className, this.parentNode.id,this.id);">D</div><div id="DIV['+i+']_K1" class="sel_off" onclick="DivClick(this.className, this.parentNode.id,this.id);">K</div><div id="DIV['+i+']_K2" class="sel_off" onclick="DivClick(this.className, this.parentNode.id,this.id);">K</div><input name="data[DIV]['+i+'][D1]" type="checkbox" class="" id="DIV['+i+'][D1]" style="display:none"  /><input name="data[DIV]['+i+'][D2]" type="checkbox" class="" id="DIV['+i+'][D2]" style="display:none"  /><input name="data[DIV]['+i+'][K1]" type="checkbox" class="" id="DIV['+i+'][K1]" style="display:none"  /><input name="data[DIV]['+i+'][K2]" type="checkbox" class="" id="DIV['+i+'][K2]" style="display:none"  />   <div class="zamInput"><input name="data[DIV]['+i+'][wys]" type="text" class="" id="DIV['+i+'][wys]" style="width:40px;"   onblur="checkSign(this.id);" maxlength="4"  /></div><div class="zamInput"><input name="data[DIV]['+i+'][szer]" type="text" class="" id="DIV['+i+'][szer]"  onblur="checkSign(this.id);"  style="width:40px;"  maxlength="4" /></div><div class="zamInput"><input name="data[DIV]['+i+'][szt]"  onblur="checkSign(this.id);"  type="text" class="" id="DIV['+i+'][szt]" style="width:40px;"  onblur="checkSign(this.id);"  onkeydown="showEv(this.parentNode.parentNode.id,this.id);"  maxlength="4" /></div><div class="zamInput"><input name="data[DIV]['+i+'][sloje]" type="checkbox" class="checkbox_zamTab" id="DIV['+i+'][sloje]"  /></div><div style="width:1px; float:left">&nbsp;</div><div class="zamInput"><input name="data[DIV]['+i+'][uwaga]" type="text" class="" id="DIV['+i+'][uwaga]" style="width:200px;"  /></div>  <div id="'+i+'" class="divIterator" style="display: block; background-color:#CC0000;"></div>   <div class="delDiv" onmouseover="this.className=\'delDiv_on\'" onmouseout="this.className=\'delDiv\'" onclick="removeDiv(this.parentNode.id);">X Usuń</div>';


		$('#elementy').append(text);
		
		//document.getElementById('elementy').innerHTML += text;
		
		lastCreatedDiv_id = 'DIV['+i+']';
		
		//Tab[i-2]=lastCreatedDiv_id;
		
		Tab.push(lastCreatedDiv_id);
		
		//alert(Tab);
		//console.log('tablica glowna: '+Tab);
	
	i++;
	
	
	
}

function removeDiv(id) {
	//document.getElementById('DIV2').removeChild(this.parentNode.id);
	
	//document.getElementById('DIV'+i).parentNode.removeChild(document.getElementById('DIV'+i));
	//alert(id);

	
	/*Tab2 = new Array();
	var k=0;
	for (var j=0;j<=Tab.length;j++) {
		if(Tab[j] == id)
			Tab.splice(j,1);
		else if (Tab[j] != null) {
			Tab2[k] = Tab[j];
			k++;
		}
	}
	
	console.log('tablica: '+Tab2);
	console.log('ostatni: '+Tab2[Tab2.length-1]);
	*/
	
	///for (var j=0;j<=Tab.length;j++) {
		
		//Tab.splice(1,1);
	//}
	
	
	//var str = Tab.toString();
	//var rx = ',';
	//tab2 = str.split(rx);
	
	/*var ind = new String();*/
	
	if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}
	
	var Tab = new Array();
	index = Tab.indexOf(id);
	
	Tab.splice(index,1);
	
	var p = document.getElementById(id);
	p.parentNode.removeChild(p);
	
	lastCreatedDiv_id = Tab[Tab.length-1];
	
	//alert(index);
	
	if(lastCreatedDiv_id == null || lastCreatedDiv_id == 'undefined') { lastCreatedDiv_id = 'DIV[1]'; }
	
	/*console.log('tablica: '+Tab);
	console.log('index: '+index);
	console.log('ostatni: '+Tab[Tab.length-1]);*/
	
	
}

function ShowInput(id,val) {
	//if(document.getElementById('Oklejanie').checked == true ){
		//alert(val);

		if (val == 'inne') {
			document.getElementById(id).style.display='block';
		}else {
			document.getElementById('Oklejanie_text').value = null; //czysci pole
			document.getElementById(id).style.display='none';
		}
		//alert('pojawil');
	//} //else {
		//document.getElementById(id).style.display='none';
		//alert('zniknal');
	//}
	//alert(id+ "   "+val);
}


function SellAll(class1, id) {
	//document.getElementById('DIV2').removeChild(this.parentNode.id);
	
	//document.getElementById('DIV'+i).parentNode.removeChild(document.getElementById('DIV'+i));
	//alert(id);
//alert(id);
	//alert(class);
	DivClick(class1,id,id+'_D1');
	DivClick(class1,id,id+'_D2');
	DivClick(class1,id,id+'_K1');
	DivClick(class1,id,id+'_K2');
	//alert(1);
}

var id_showEv ='';

function showEv(parentid,id) {
	id_showEv = parentid;
	//alert(parentid);

	
		document.getElementById(parentid).onkeydown = przechwyc;
	
}

function przechwyc(e) {
		//alert(lastCreatedDiv_id + " | " + id_showEv);
        var kod = (window.event) ? window.event.keyCode : e.which;
        /*Zamień nastepną linijkę na swój kod :) */
		if (kod == 9) {
        	//alert('Nacisnales: ' + String.fromCharCode(kod) + ', kod znaku: ' + kod);\
			if(lastCreatedDiv_id == id_showEv) {
				CreateDiv();
				this.onkeydown = '';
				//alert('DIV['+(i-1)+'][wys]');
				document.getElementById('DIV['+ (i-1) +'][clear]').focus();
				
				
				//document.forms['ZAMFORM']['DIV[1][x]'].focus();
				
				//document.forms['ZAMFORM']['DIV['(i-1)+'][szer]'].focus();
				
				
			}
		}
}

function checkdata(inp) {
var pole=document.getElementById(inp).value;
	if (pole == '' || pole == null || pole == 'undefined'){
		document.getElementById(inp).style.backgroundColor = '#CC0000';
		document.getElementById(inp).style.color = '#FFFFFF';	
		alert("Musisz wypełnić to pole.");
	return false;			
	}else {
		document.getElementById(inp).style.backgroundColor = '#FFFFFF';
		document.getElementById(inp).style.color = '';
	}
}


function checkSign(inp) {
	
var str = ' ';

var numer=document.getElementById(inp).value;

	//alert(numer);
	if (numer != Number(numer) || numer.indexOf(str)>=0){
		document.getElementById(inp).style.backgroundColor = '#CC0000';
		document.getElementById(inp).style.color = '#FFFFFF';	
		alert("Nieprawidłowy znak. Nie wolno stosować liter i spacji");
	return false;
	} else {
		document.getElementById(inp).style.backgroundColor = '#FFFFFF';
		document.getElementById(inp).style.color = '';
	}
	
	
	
	if (numer == '' || numer == null || numer == 'undefined'){
		alert("Musisz podać przynajmniej 1 cyfrę");
		document.getElementById(inp).style.backgroundColor = '#CC0000';
		document.getElementById(inp).style.color = '#FFFFFF';
	}else {
		document.getElementById(inp).style.backgroundColor = '#FFFFFF';
		document.getElementById(inp).style.color = '';
	}

}
