//-------------------------------------------------------------------
var xmlhttp = null;

//-------------------------------------------------------------------
function Ajaxx (uri, request, callback) {
  if (window.XMLHttpRequest) {
    xmlhttp=new XMLHttpRequest();
  }  else if (window.ActiveXObject) {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  
  if (xmlhttp) {
    xmlhttp.onreadystatechange = callback;
    xmlhttp.open('POST', uri, true);
    
    if (typeof xmlhttp.setRequestHeader != 'undefined') {
      xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    }
    xmlhttp.send(request);
  } else {
	alert(xmlhttp);
  }
}

function select_form(select, value) {
	if (select !="" && value!="") {
		Ajaxx('ajax.php', 'op='+select+'&id='+value, re_select_form);
	}
}

function re_select_form() {
	if (xmlhttp.readyState == 4) {
		// ok
		if (xmlhttp.status == 200) {
		
			// aici facem ce facem cu datele primite
			var data = xmlhttp.responseText;
			if (data!=""){
				//var array = data.split('~~~~');
				var array = data.split('~~~~');
				var d = document.getElementById(array[0]);
				while (d.hasChildNodes()) {       
					d.removeChild(d.firstChild);     
				}
				
				var op0 = document.createElement('option');
				op0.value = '';
				var s0 = document.createTextNode('Selectati');
				op0.appendChild(s0);
				d.appendChild(op0);
				for (var i=1;i<array.length;i++) {
					if (array[i].length == 0) continue;
					var x = array[i].split(':');
					var op = document.createElement('option');
					op.value = x[0];
					var s = document.createTextNode(x[1]);
					op.appendChild(s);
					d.appendChild(op);
				}

				if (array[0]=='form_juridic') {
					var select = document.getElementById('stare');
					var mydiv1 = document.getElementById('stare_pj');
					var mydiv2 = document.getElementById('stare_pf');
					var mydivpj = document.getElementById('angajator_pj');
					var mydivpf = document.getElementById('angajator_pf');
					if (select.value==2) {
						mydiv1.style.display="none";
						mydiv2.style.display="block";
						mydivpj.style.display="none";
						mydivpf.style.display="block";
					}
					else {
						mydiv1.style.display="block";
						mydiv2.style.display="none";	
						mydivpf.style.display="none";
						mydivpj.style.display="block";
					}
				}
				if (array[0]=='form_org') {
					var mydiv = document.getElementById('denumire');
					var select = document.getElementById('form_juridic');
					var prefix = document.getElementById('prefix');
					var caen = document.getElementById("div_caen");
					if (select.value==1) {
						mydiv.firstChild.nodeValue = "Denumire Angajator : S.C. ";
						prefix.value = 'S.C.';
					}
					else if (select.value==2) {
						mydiv.firstChild.nodeValue = "Denumire Angajator : R.A. ";
						prefix.value = 'R.A.';
					}
					else if (select.value==3) {
						mydiv.firstChild.nodeValue = "Denumire Angajator : C.N. ";
						prefix.value = 'C.N.';
						caen.style.display='block';
					}
					else if (select.value==4) {
						mydiv.firstChild.nodeValue = "Denumire Angajator : S.N. ";
						prefix.value = 'S.N.';
						caen.style.display='block';
					}
					else if (select.value==7) {
						mydiv.firstChild.nodeValue = "Denumire Angajator : S. COOP. ";
						prefix.value = 'S. COOP.';
						caen.style.display='block';
					}
					else if (select.value==13) {
						mydiv.firstChild.nodeValue = "Denumire Angajator : ORG. ";
						prefix.value = 'ORG.';
						caen.style.display='block';
					}
					else if (select.value==10) {
						mydiv.firstChild.nodeValue = "Nume si Prenume : P.F. ";
						prefix.value = 'P.F.';
						caen.style.display='none';
					}
					else if (select.value==14) {
						mydiv.firstChild.nodeValue = "Nume si Prenume Reprezentant : P.F.A. ";
						prefix.value = 'P.F.A.';
						caen.style.display='block';
					}
					else if (select.value==15) {
						mydiv.firstChild.nodeValue = "Nume si Prenume Reprezentant : A.F. ";
						prefix.value = 'A.F.';
						caen.style.display='block';
					}
					else if (select.value==16) {
						mydiv.firstChild.nodeValue = "Nume si Prenume / Denumire  : - ";
						prefix.value = '';
						caen.style.display='none';
					}
					else {
						mydiv.firstChild.nodeValue = "Denumire Angajator : - ";
						prefix.value = '';
						caen.style.display='block';
					}
				}
				if (array[0]=='localitate') {
					var jud = document.getElementById('judet');
					var sector = document.getElementById('sector');
					if (jud.value=='B') {
						//judet == BUCURESTI
						sector.style.display="block";
					}
					else {
						sector.style.display="none";
					}
				}
				if (array[0]=='tara') {
					var divpf = document.getElementById('angajator_pf');
					var cet = document.getElementById('cetatenie');
					if (cet.value==2) {
						divpf.firstChild.nodeValue = "Nr. Pasaport/ Act identitate : ";
					}
					else if (cet.value==3) {
						divpf.firstChild.nodeValue = "Nr. Pasaport : ";
					}
					else {
						divpf.firstChild.nodeValue = "CNP : ";
					}
				}
				if (array[0]=='localitate') {
					var jd = document.getElementById('judet');
					var itm = document.getElementById('itm_rap');
					var index     = jd.selectedIndex;//alert(index);
					itm.options[index].selected='selected';
				}
			}
		} else {
			alert('Error: ' + xmlhttp.status + ' ' + xmlhttp.error);
		}
	}
}

function sufix_denumire(org) {
	var mydiv = document.getElementById('denumire');
	var sufix = document.getElementById('sufix');
	var caen = document.getElementById("div_caen");
	if (org==1) {
		mydiv.lastChild.nodeValue = " S.N.C. ";
		sufix.value = 'S.N.C.';
	}
	else if (org==2) {
		mydiv.lastChild.nodeValue = " S.C.S. ";
		sufix.value = 'S.C.S.';
	}
	else if (org==3) {
		mydiv.lastChild.nodeValue = " S.A. ";
		sufix.value = 'S.A.';
	}
	else if (org==4) {
		mydiv.lastChild.nodeValue = " S.C.A. ";
		sufix.value = 'S.C.A.';
	}
	else if (org==5) {
		mydiv.lastChild.nodeValue = " S.R.L. ";
		sufix.value = 'S.R.L.';
	}
	else {
		mydiv.lastChild.nodeValue = " - ";
		sufix.value = '';
		
	}
	
	if (org==67) {
		caen.style.display='none';
	}
	else {
		caen.style.display='block';
	}
}

function show_identificare(nivel) {
	//alert(nivel);
	var den = document.getElementById('denumire');
	var ang = document.getElementById('angajator_pj');
	if (nivel==2) {
		var input1 = document.getElementById('den_parinte');
		var input2 = document.getElementById('cui_parinte');
		if (input1) {
			den.removeChild(input1);
			den.removeChild(den.lastChild);     
		}
		if (input2) {
			ang.removeChild(input2);
			ang.removeChild(ang.lastChild);     
		}
		den.firstChild.nodeValue = "Denumire Filiala : ";
		var cuif = document.createTextNode('CUI Filiala : ');
		ang.appendChild(cuif);
		var inp1 = document.createElement('input');
		inp1.type="text";
		inp1.name="cui_parinte";
		inp1.id = "cui_parinte";
		ang.appendChild(inp1);
	}
	else if (nivel==3) {
		var input3 = document.getElementById('cui_parinte');
		if (input3) {
			ang.removeChild(input3);
			ang.removeChild(ang.lastChild);     
		}
		var sucurs = document.createTextNode('Denumire Sucursala : ');
		var inp2 = document.createElement('input');
		inp2.type="text";
		inp2.name="den_parinte";
		inp2.id = "den_parinte";
		
		var cuif = document.createTextNode('CIF Sucursala : ');
		var inp3 = document.createElement('input');
		inp3.type="text";
		inp3.name="cui_parinte";
		inp3.id = "cui_parinte";
		den.appendChild(sucurs);
		den.appendChild(inp2);
		ang.appendChild(cuif);
		ang.appendChild(inp3);

	}
	else {
		den.firstChild.nodeValue = "Denumire Angajator : ";
		var input1 = document.getElementById('den_parinte');
		var input2 = document.getElementById('cui_parinte');
		var input3 = document.getElementById('cui_parinte');
		if (input1) {
			den.removeChild(input1);
			den.removeChild(den.lastChild);     
		}
		if (input2) {
			ang.removeChild(input2);
			ang.removeChild(ang.lastChild);     
		}
		if (input3) {
			ang.removeChild(input3);
			ang.removeChild(ang.lastChild);     
		}
		
	}
}
/*function act_ang(cetatean) {
	var divpf = document.getElementById('angajator_pf');
	if (cetatean==2) {
		divpf.firstChild.nodeValue = "Nr. Pasaport/ Act identitate : ";
	}
	else if (cetatean==3) {
		divpf.firstChild.nodeValue = "Nr. Pasaport : ";
	}
	else {
		divpf.firstChild.nodeValue = "CNP : ";
	}
}*/
