//window.onload = alert("Am intrat");

function Formatez(id) {

var suma='suma'+id;
eval("var sumapl=document.plata."+suma+".value;");

var suman=sumapl.replace(/,/, ".");

eval("document.plata."+suma+".value='"+suman+"';");
return;
}

function UpdateEchivalent(id) {
var suma='suma'+id;
eval("var sumapl=document.plata."+suma+".value;");

if ((sumapl<=0 || isNaN(sumapl)) && sumapl.length>0) {
eval("document.getElementById('sm"+id+"').innerHTML=\"<input type='text' name='suma"+id+"' value='"+sumapl+"' style='width: 70px; border: 1px solid #ff0000;' onKeyup='Formatez("+id+")' onBlur='UpdateEchivalent("+id+")'>\";");
return;
}


else if (sumapl>0) {
eval("document.getElementById('sm"+id+"').innerHTML=\"<input type='text' name='suma"+id+"' value='"+sumapl+"' style='width: 70px; border: 1px solid #99cc00; background: #f2ffcb' onKeyup='Formatez("+id+")' onBlur='UpdateEchivalent("+id+")'>\";");
}

else  {
eval("document.getElementById('sm"+id+"').innerHTML=\"<input type='text' name='suma"+id+"' value='"+sumapl+"' style='width: 70px; border: 1px solid #333; ' onKeyup='Formatez("+id+")' onBlur='UpdateEchivalent("+id+")'>\";");
}

var moneda='moneda'+id;
eval("var monedapl=document.plata."+moneda+".value;");

var echivalent='echivalent'+id;
total=sumapl*monedapl;
total=total.toFixed(2);
if (sumapl>0) {
        eval("document.getElementById('"+echivalent+"').innerHTML=total+' RON';");
}
return;
}

function ValidezFrm() {
	var eroare='';
if (document.plata.nume.value.length<2) { 
	eroare=eroare+'1';  
	document.getElementById('dnume').innerHTML="<input type='text' name='nume' value='"+document.plata.nume.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\"nume\")'>";
}
if (document.plata.prenume.value.length<2) {
	eroare=eroare+'2'; 
	document.getElementById('dprenume').innerHTML="<input type='text' name='prenume' value='"+document.plata.prenume.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\"prenume\")'>";
}

if (document.plata.adresa.value.length<5) {
	eroare=eroare+'3';  
	document.getElementById('dadresa').innerHTML="<input type='text' name='adresa' value='"+document.plata.adresa.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\"adresa\")'>";
}

if (document.plata.oras.value.length<2) {
	eroare=eroare+'4';   
	document.getElementById('doras').innerHTML="<input type='text' name='oras' value='"+document.plata.oras.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\"oras\")'>";
}

if (document.plata.telefon.value.length<2) {
	eroare=eroare+'5'; 
	document.getElementById('dtelefon').innerHTML="<input type='text' name='telefon' value='"+document.plata.telefon.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\"telefon\")'>";
}

var temail=validate_email(document.plata.email.value) 

if (temail== false ) {
	eroare=eroare+'6'; 
	document.getElementById('demail').innerHTML="<input type='text' name='email' value='"+document.plata.email.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\"email\")'>";
}

if (eroare.length>0) { alert ('Nu ai completat corect toate campurile!'); return false; }

else { return true; }
}

function validate_email(email)
{

  apos=email.indexOf("@");
  dotpos=email.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {return false;}
  else {return true;}
  
}

function Validez(camp) {
eval("var campref=document.plata."+camp+".value;");	
var eroare=0;
if (camp=='nume') {
	if (campref.length<2 && campref.length>0) { 
		alert('Nu ai completat numele!');	
		document.getElementById('dnume').innerHTML="<input type='text' name='nume' value='"+document.plata.nume.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\""+camp+"\")'>";
		
	}
	else if (campref.length>0) {
		document.getElementById('dnume').innerHTML="<input type='text' name='nume' value='"+document.plata.nume.value+"' style='width: 150px; border: 1px solid #99cc00; background: #f2ffcb' onBlur='Validez(\""+camp+"\")'>";
		document.plata.prenume.focus();
	}
	else {
		document.getElementById('dnume').innerHTML="<input type='text' name='nume' value='"+document.plata.nume.value+"' style='width: 150px; border: 1px solid #333;' onBlur='Validez(\""+camp+"\")'>";

	}
	return;
}

if (camp=='prenume') {
	if (campref.length<2 && campref.length>0) { 
		alert('Nu ai completat prenumele!');	
		document.getElementById('dprenume').innerHTML="<input type='text' name='prenume' value='"+document.plata.nume.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\""+camp+"\")'>";
	}
	else if (campref.length>0) {
		document.getElementById('dprenume').innerHTML="<input type='text' name='prenume' value='"+document.plata.prenume.value+"' style='width: 150px; border: 1px solid #99cc00; background: #f2ffcb' onBlur='Validez(\""+camp+"\")'>";
	document.plata.adresa.focus();
	}
	else {
		document.getElementById('dprenume').innerHTML="<input type='text' name='prenume' value='"+document.plata.prenume.value+"' style='width: 150px; border: 1px solid #333;' onBlur='Validez(\""+camp+"\")'>";

	}
	return;
}

if (camp=='adresa') {
	if (campref.length<5 && campref.length>0) { 
		alert('Nu ai completat adresa!');	
		document.getElementById('dadresa').innerHTML="<input type='text' name='adresa' value='"+document.plata.adresa.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\""+camp+"\")'>";
	}
	else if (campref.length>0) {
		document.getElementById('dadresa').innerHTML="<input type='text' name='adresa' value='"+document.plata.adresa.value+"' style='width: 150px; border: 1px solid #99cc00; background: #f2ffcb' onBlur='Validez(\""+camp+"\")'>";
	document.plata.oras.focus();
	}
	else {
		document.getElementById('dadresa').innerHTML="<input type='text' name='adresa' value='"+document.plata.adresa.value+"' style='width: 150px; border: 1px solid #333;' onBlur='Validez(\""+camp+"\")'>";

	}
	return;
}

if (camp=='oras') {
	if (campref.length<2 && campref.length>0) { 
		alert('Nu ai completat orasul!');	
		document.getElementById('doras').innerHTML="<input type='text' name='oras' value='"+document.plata.oras.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\""+camp+"\")'>";
	}
	else if (campref.length>0) {
		document.getElementById('doras').innerHTML="<input type='text' name='oras' value='"+document.plata.oras.value+"' style='width: 150px; border: 1px solid #99cc00; background: #f2ffcb' onBlur='Validez(\""+camp+"\")'>";
	document.plata.judet.focus();
	}
	else {
		document.getElementById('doras').innerHTML="<input type='text' name='oras' value='"+document.plata.oras.value+"' style='width: 150px; border: 1px solid #333;' onBlur='Validez(\""+camp+"\")'>";

	}
	return;
}

if (camp=='telefon') {
	if (campref.length<6 && campref.length>0) { 
		alert('Nu ai completat telefonul!');	
		document.getElementById('dtelefon').innerHTML="<input type='text' name='telefon' value='"+document.plata.telefon.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\""+camp+"\")'>";
	}
	else if (campref.length>0) {
		document.getElementById('dtelefon').innerHTML="<input type='text' name='telefon' value='"+document.plata.telefon.value+"' style='width: 150px; border: 1px solid #99cc00; background: #f2ffcb' onBlur='Validez(\""+camp+"\")'>";
	document.plata.email.focus();
	}
	else {
		document.getElementById('dtelefon').innerHTML="<input type='text' name='telefon' value='"+document.plata.telefon.value+"' style='width: 150px; border: 1px solid #333;' onBlur='Validez(\""+camp+"\")'>";

	}
	return;
}

if (camp=='email') {
	
var temail=validate_email(document.plata.email.value) 


	
	if (temail== false && campref.length>0) { 
		alert('Nu ai completat adresa de email!');	
		document.getElementById('demail').innerHTML="<input type='text' name='email' value='"+document.plata.email.value+"' style='width: 150px; border: 1px solid #ff0000' onBlur='Validez(\""+camp+"\")'>";
	}
	else if (campref.length>0) {
		document.getElementById('demail').innerHTML="<input type='text' name='email' value='"+document.plata.email.value+"' style='width: 150px; border: 1px solid #99cc00; background: #f2ffcb' onBlur='Validez(\""+camp+"\")'>";
	document.plata.tr_plata.focus();
	}
	else {
		document.getElementById('demail').innerHTML="<input type='text' name='email' value='"+document.plata.email.value+"' style='width: 150px; border: 1px solid #333;' onBlur='Validez(\""+camp+"\")'>";

	}
}

return;	
}

function Verific(id) {
var numar='numar'+id;
eval("var numarpl=document.plata."+numar+".value;");
eroare=0;
if (numarpl.length != 8 && numarpl.length>0) { alert('Numarul de polita trebuie sa aiba 8 caractere'); eroare=1;  }
else if ((numarpl.charAt(0)!=2 && numarpl.charAt(0)!=3) && numarpl.length>0) { alert('Numarul de polita trebuie sa inceapa cu cifra 2 sau 3'); eroare=2;  }
else if (!IsNumeric(numarpl) && numarpl.length>0) { alert('Numarul de polita trebuie sa contina doar cifre'); eroare=3;  }

if (eroare>0 && numarpl.length>0) {
eval("document.getElementById('nr"+id+"').innerHTML=\"<input type='text' name='numar"+id+"' value='"+numarpl+"' style='width: 100px; border: 1px solid #ff0000' onBlur='Verific("+id+")'>\";");
     }
else if (numarpl.length>0) {
eval("document.getElementById('nr"+id+"').innerHTML=\"<input type='text' name='numar"+id+"' style='width: 100px; border: 1px solid #99cc00; background: #f2ffcb' value='"+numarpl+"' onBlur='Verific("+id+")'>\";");
eval("document.plata.sm"+id+".focus();");
}

else  {
eval("document.getElementById('nr"+id+"').innerHTML=\"<input type='text' name='numar"+id+"' style='width: 100px; border: 1px solid #333; value='"+numarpl+"' onBlur='Verific("+id+")'>\";");

}
return;
}



function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;

   }

function createRequest() {
  try {
    request = new XMLHttpRequest();
  } catch (tryMS) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (otherMS) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = null;
      }
    }
  }
  return request;
}

function getDetails(itemName) {
  request = createRequest();
  if (request == null) {
    alert("Unable to create request");
    return;
  }
  var url= "getDetails.php?ImageID=" + escape(itemName);
  request.open("GET", url, true);
  request.onreadystatechange = displayDetails;
  request.send(null);
}

function displayDetails() {
  if (request.readyState == 4) {
    if (request.status == 200) {
      detailDiv = document.getElementById("description");
      detailDiv.innerHTML = request.responseText;
    }
  }
}
