function isNumberKey(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}

function isNumberKeyPlus(evt)
{
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=52 && charCode!=52)
        return false;

    return true;
}

function validareIBAN() {
    // Dupa regula nr. 1 mutarea primelor 4 caractare la coada sirului
    var iban=document.getElementById("iban").value;
    var a,b,c,d, iban_tmp="";
    a=iban.charAt(0);
    b=iban.charAt(1);
    c=iban.charAt(2);
    d=iban.charAt(3);
    for (x=4; x<iban.length;++x)
    {
        iban_tmp+=iban.charAt(x);
    }
    iban=iban_tmp+a+b+c+d;
    //verificarea lungimii codului iban
    if (iban.length!=24) return "<li>IBAN invalid!</li>";
    var iban_sir="";
    // Conform regulii nr. 2 de calcul fiecarui litere i se da o anumita cifra..
    // si se aplica modulo conform regulii numarul 3
    var ponderi = '0123456789abcdefghijklmnopqrstuvwxyz';
    for (x=0;x<iban.length;x++)
    {
        var pozitie = ponderi.search(RegExp(iban.charAt(x),'i'));
        if(pozitie==-1)
            return "<li>IBAN invalid!</li>";
        else
            iban_sir = (parseInt(iban_sir+pozitie.toString())%97).toString();
    }
    //La sfarsit rezultatul trebuie sa fie 1
    if (parseInt(iban_sir)-1)
    {
        return "<li>IBAN invalid!</li>";
    }
    return "";
}

function validareCNP() {

    var constanta = new String("279146358279");
    var cnp="";
    if (document.getElementById("client").options.selectedIndex==0)
        cnp=document.getElementById("cnp_factura").value;
    else return "";
    if (cnp.length != 13) return "<li>CNP nu este valid!</li>";

    var suma = 0;

    for (i = 0; i < constanta.length; i++) {

        suma = suma + cnp.charAt(i) * constanta.charAt(i);

    }

    var rest = suma % 11;

    if (!((rest < 10 && rest == cnp.charAt(12)) || (rest == 10 && cnp.charAt(12) == 1)))
    {

        return "<li>CNP nu este valid!</li>";
    }
    return "";
}
function validareCNP1() {

    var constanta = new String("279146358279");
    var cnp=document.getElementById("cnp").value;
    if (cnp.length != 13) return "<li>CNP nu este valid!</li>";

    var suma = 0;

    for (i = 0; i < constanta.length; i++) {

        suma = suma + cnp.charAt(i) * constanta.charAt(i);

    }

    var rest = suma % 11;

    if (!((rest < 10 && rest == cnp.charAt(12)) || (rest == 10 && cnp.charAt(12) == 1)))
    {

        return "<li>CNP nu este valid!</li>";
    }
    return "";
}

function validareCUI(){
    var cui = document.getElementById("cnp_factura").value;
    /*var nr = cui.length, total = 0;
    if(nr<2 || nr>10)
    {
        return "<li>CUI nu este valid!</li>";
    }
    var pondere = "7532175321".substr(10-nr,nr);
    for(x=0;x<nr-1;++x)
    {
        total+=cui.charAt(x)*pondere.charAt(x);
    }
    if (cui.substr(nr-1,1)!=((total*10)%11)%10)
    {*/
    if(cui.length==0)
        return "<li>CUI nu este valid!</li>";
//    }
    return "";
}

function sum()
{
    if(document.getElementById("nrBucati").value>0 || document.getElementById("nrBucati2").value>0)
    {  
       var original=(Math.round(parseFloat(document.getElementById("nrBucati").value)*100)/100)
               *(Math.round(parseFloat(document.getElementById("pret").value)*100)/100);
	   var result=Math.round(original*100)/100;
	   var original2=(Math.round(parseFloat(document.getElementById("nrBucati2").value)*100)/100)
               *(Math.round(parseFloat(document.getElementById("pret2").value)*100)/100);
        var result2=Math.round(original2*100)/100;

       var totalPlata=Math.round(parseFloat(result+result2+Math.round(parseFloat(document.getElementById("transport").value)*100)/100)*100)/100;

       document.getElementById("total").value=result.toFixed(2);
	   document.getElementById("total2").value=result2.toFixed(2);
       document.getElementById("totalLabel").innerHTML=result.toFixed(2);
	   document.getElementById("totalLabel2").innerHTML=result2.toFixed(2);
       document.getElementById("totalPlata").value=totalPlata.toFixed(2);
       document.getElementById("totalPlataLabel").innerHTML=totalPlata.toFixed(2);

    }
}
function trimite(form){//si proceseaza cu form
    document.forms[0].action=form;
    document.myform.submit();
}

function setCampuriPJ()
{
    document.getElementById("prenume_factura").readOnly=true;
    document.getElementById("prenume_factura").value="";
    document.getElementById("prenume_factura").className="readonly";
    document.getElementById("denumire").readOnly=false;
    document.getElementById("denumire").className="text";
    document.getElementById("J_factura").readOnly=false;
    document.getElementById("J_factura").className="text";
    document.getElementById("RC").style.display="inline";
    document.getElementById("ib").style.display="inline";
    document.getElementById("bnk").style.display="inline";
    document.getElementById("den").style.display="inline";
    document.getElementById("copiaza").onclick=copyPJ;
}
function setCampuriPF()
{
    document.getElementById("prenume_factura").readOnly=false;
    document.getElementById("prenume_factura").className="text";
    document.getElementById("denumire").readOnly=true;
    document.getElementById("denumire").className="readonly";
    document.getElementById("denumire").value="";
    document.getElementById("J_factura").readOnly=true;
    document.getElementById("J_factura").value="";
    document.getElementById("J_factura").className="readonly";
    document.getElementById("RC").style.display="none";
    document.getElementById("ib").style.display="none";
    document.getElementById("bnk").style.display="none";
    document.getElementById("den").style.display="none";
    document.getElementById("copiaza").onclick=copyPF;
}
function setCampuriIP()
{
    document.getElementById("prenume_factura").readOnly=true;
    document.getElementById("prenume_factura").value="";
    document.getElementById("prenume_factura").className="readonly";
    document.getElementById("denumire").readOnly=false;
    document.getElementById("denumire").className="text";
    document.getElementById("J_factura").className="readonly";
    document.getElementById("J_factura").readOnly=true;
    document.getElementById("J_factura").value="";
    document.getElementById("RC").style.display="none";
    document.getElementById("ib").style.display="inline";
    document.getElementById("bnk").style.display="inline";
    document.getElementById("den").style.display="inline";
    document.getElementById("copiaza").onclick=copyIP;
}

function setFormType()
{   
    if(document.getElementById("client").options.selectedIndex=="0")
    {
        setCampuriPF();
        document.getElementById("cnp_factura").onkeypress="return isNumberKey(event)";
        document.getElementById("cnp").onkeypress="return isNumberKey(event)";
    }
    if(document.getElementById("client").options.selectedIndex=="1")
    {
        setCampuriPJ();
        document.getElementById("cnp_factura").onkeypress="";
        document.getElementById("cnp").onkeypress="";
    }
    if(document.getElementById("client").options.selectedIndex=="2")
    {
        setCampuriIP();
        document.getElementById("cnp_factura").onkeypress="";
        document.getElementById("cnp").onkeypress="";
    }
//    if (document.myform.elements["client"].options[1].selected)

//    else {
//        if (document.myform.elements["client"].options[2].selected) {
//            document.getElementById("clientDesc").value = document.myform.elements["client"].options[2].value;
//        } else
//            document.getElementById("clientDesc").value = document.myform.elements["client"].options[3].text;
//        }
}

function copyPF()
{
    document.getElementById("nume").value=document.getElementById("nume_factura").value;
    document.getElementById("prenume").value=document.getElementById("prenume_factura").value;
    document.getElementById("cnp").value=document.getElementById("cnp_factura").value;
    document.getElementById("strada").value=document.getElementById("strada_factura").value;
    document.getElementById("nrStrada").value=document.getElementById("nrStrada_factura").value;
    document.getElementById("localitate").value=document.getElementById("localitate_factura").value;
    document.getElementById("codpostal").value=document.getElementById("codpostal_factura").value;
    document.getElementById("apartament").value=document.getElementById("apartament_factura").value;
    document.getElementById("scara").value=document.getElementById("scara_factura").value;
    document.getElementById("bloc").value=document.getElementById("bloc_factura").value;
    document.myform.elements["judet"].options.selectedIndex=document.myform.elements["judet_factura"].options.selectedIndex;
    copyIndex(2);
}
function copyPJ()
{
    document.getElementById("denumire").value=document.getElementById("nume_factura").value;
    document.getElementById("strada").value=document.getElementById("strada_factura").value;
    document.getElementById("nrStrada").value=document.getElementById("nrStrada_factura").value;
    document.getElementById("localitate").value=document.getElementById("localitate_factura").value;
    document.getElementById("codpostal").value=document.getElementById("codpostal_factura").value;
    document.getElementById("apartament").value=document.getElementById("apartament_factura").value;
    document.getElementById("scara").value=document.getElementById("scara_factura").value;
    document.getElementById("bloc").value=document.getElementById("bloc_factura").value;
    document.myform.elements["judet"].options.selectedIndex=document.myform.elements["judet_factura"].options.selectedIndex;
    copyIndex(2);
}
function copyIP()
{
    document.getElementById("denumire").value=document.getElementById("nume_factura").value;
    document.getElementById("strada").value=document.getElementById("strada_factura").value;
    document.getElementById("nrStrada").value=document.getElementById("nrStrada_factura").value;
    document.getElementById("localitate").value=document.getElementById("localitate_factura").value;
    document.getElementById("codpostal").value=document.getElementById("codpostal_factura").value;
    document.getElementById("apartament").value=document.getElementById("apartament_factura").value;
    document.getElementById("scara").value=document.getElementById("scara_factura").value;
    document.getElementById("bloc").value=document.getElementById("bloc_factura").value;
    document.myform.elements["judet"].options.selectedIndex=document.myform.elements["judet_factura"].options.selectedIndex;
    copyIndex(2);
}

function validate()
{
    var erori="";
    var erori1="";
    var eroare="<table align='center' style='margin-left:22px'>" +
               "<tr><td style='font-size:10pt;color:black;width:296px'>Adresa de facturare</td><td style='font-size:10pt;color:black;width:280px'>Adresa de livrare</td></tr>";

    var valid=true;

    if (document.getElementById("nume_factura").value.length==0)
    {
        erori+="<li>Numele este obligatoriu!</li>";
    }
    if (document.getElementById("nume").value.length==0)
    {
        erori1+="<li>Numele este obligatoriu!</li>";
    }
    if (document.getElementById("prenume_factura").value.length==0 && document.getElementById("client").options.selectedIndex=="0")
    {
        erori+="<li>Prenumele este obligatoriu!</li>";
    }
    if (document.getElementById("prenume").value.length==0)
    {
        erori1+="<li>Prenumele este obligatoriu!</li>";
    }
    if (document.getElementById("strada_factura").value.length==0)
    {
        erori+="<li>Strada este obligatoriu!</li>";
    }
    if (document.getElementById("strada").value.length==0)
    {
        erori1+="<li>Strada este obligatoriu!</li>";
    }
    if (document.getElementById("nrStrada_factura").value.length==0)
    {
        erori+="<li>Nr strada este obligatoriu!</li>";
    }
    if (document.getElementById("nrStrada").value.length==0)
    {
        erori1+="<li>Nr strada este obligatoriu!</li>";
    }
    if (document.getElementById("localitate_factura").value.length==0)
    {
        erori+="<li>Localitate este obligatoriu!</li>";
    }
    if (document.getElementById("localitate").value.length==0)
    {
        erori1+="<li>Localitate este obligatoriu!</li>";
    }

//    if(document.getElementById("client").options.selectedIndex=="0")
//    {
        erori+=validareCNP();
        erori1+=validareCNP1();
//    }
    if(document.getElementById("client").options.selectedIndex=="1")
    {
        erori+=validareCUI();
        if(document.getElementById("J_factura").value.length==0)
        {
            erori+="<li>Nr. RC este obligatoriu!</li>";
        }
        erori+=validareIBAN();
        if (document.getElementById("banca").value.length==0)
        {
            erori+="<li>Banca este obligatoriu!</li>";
        }
        if (document.getElementById("denumire").value.length==0)
        {
            erori1+="<li>Denumire este obligatoriu!</li>";
        }
    }
    if(document.getElementById("client").options.selectedIndex=="2")
    {
        erori+=validareCUI();
        erori+=validareIBAN();
        if (document.getElementById("banca").value.length==0)
        {
            erori+="<li>Banca este obligatoriu!</li>";
        }
        if (document.getElementById("denumire").value.length==0)
        {
            erori+="<li>Denumire este obligatoriu!</li>";
        }
    }
    if (document.getElementById("telefon").value.length==0)
    {
        erori+="<li>Telefon este obligatoriu!</li>";
    }
    if(document.myform.elements["judet_factura"].options.selectedIndex>0)
    {
        copyIndex();
    } else {
        erori+="<li>Judet este obligatoriu!</li>";
    }
    if(document.myform.elements["judet"].options.selectedIndex>0)
    {
        copyIndex();
    } else {
        erori1+="<li>Judet este obligatoriu!</li>";
    }

    erori+=validateEmail();
    document.getElementById("clientDesc").value = document.myform.elements.client.options[document.myform.client.options.selectedIndex].text;
    if(erori.length>0 || erori1.length>0)
    {
        document.getElementById("erori").style.display="block";
        eroare+="<tr><td style='font-size:10pt;color:red;vertical-align:top;'>"+erori+"</td><td style='font-size:10pt;color:red;vertical-align:top;'>"+erori1+"</td></tr></table>"
        document.getElementById("erori").innerHTML=eroare;
        window.location.href = document.getElementById('gotop').href;
        return false;
    } else {
        document.getElementById("erori").innerHTML="";
        document.getElementById("erori").style.display="none";
    }
    document.forms["myform"].submit();
}

function validateEmail(){
	var emailID=document.getElementById("email").value;


	if (!(emailID.indexOf(".") > 2) || !(emailID.indexOf("@") > 0)){
		return "<li>Adresa de email nu este valida!</li>";
	}
	return "";
 }

function checkdate(dtfld) {
    var dtform=document.myform;
    var err = 0
    var dtstr = dtfld;
    var valid = "0123456789/";
    var temp;
    for (var i=0; i< dtstr.length; i++) {
    temp = "" + dtstr.substring(i, i+1);
    if (valid.indexOf(temp) == "-1") err = 1;
    }
    if (dtstr.length != 10) err=1

    b = dtstr.substring(0, 2) // day
    c = dtstr.substring(2, 3)// '/'

    d = dtstr.substring(3, 5) // month
    e = dtstr.substring(5, 6)// '/'
    f = dtstr.substring(6, 10) // year
    if (b<1 || b>31) err = 1
    if (c != '/') err = 1
    if (d<1 || d>12) err = 1
    if (e != '/') err = 1

    if (f<1900 || f>2099) err = 1
    if (d==4 || d==6 || d==9 || d==11){
    if (b==31) err=1
    }
    if (d==2){
    var g=parseInt(f/4)
    if (isNaN(g)) {
    err=1
    }
    if (b>29) err=1
    if (b==29 && ((f/4)!=parseInt(f/4))) err=1
    }
    var iDate = new Date(f,d,b);

    return err != 1;
}

function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function validateContact(){
    if((document.getElementById("descriere").value.length==0)||(document.getElementById("observatii").value.length<10))
    {
        alert("Un mesaj valid trebuie sa aiba un Titlu de mesaj si un Continut >10 caractere!");
        return;
    }
    document.myform.submit();
}
function validateConfirm(){
    var erori="";
    if (document.getElementById("nume").value.length==0)
    {
        erori+="<li>Numele este obligatoriu!</li>";
    }
    if (document.getElementById("nr").value.length==0)
    {
        erori+="<li>Nr. factura proforma este obligatoriu!</li>";
    }
    if (document.getElementById("data").value.length==0 || !checkdate(document.getElementById("data").value))
    {
        erori+="<li>Data factura proforma este obligatorie!</li>";
    }
    if (document.getElementById("plata").value.length==0)
    {
        erori+="<li>Nr dovada plata este obligatoriu!</li>";
    }
    if (document.getElementById("data_plata").value.length==0 || !checkdate(document.getElementById("data_plata").value))
    {
        erori+="<li>Data platii este obligatorie!</li>";
    }
    if (document.getElementById("suma").value.length==0)
    {
        erori+="<li>Suma platita este obligatorie!</li>";
    }

    if(erori.length>0)
    {
        document.getElementById("erori").style.display="block";
        document.getElementById("erori").innerHTML=erori;
        document.documentElement.style= document.documentElement.scrollTop;
        return;
    }    
    document.myform.submit();
}

function calculeazaEnable()
{
    if ((document.getElementById("nrBucati").value.length>0 && document.getElementById("nrBucati").value>0) || (document.getElementById("nrBucati2").value.length>0 && document.getElementById("nrBucati2").value>0) )
    {
        sum();
        document.getElementById("cmd1").disabled=false;        
    }
}
function enableDisableCommand()
{
    document.getElementById("cmd1").disabled=true;
}

function disableSend(){
    document.getElementById("send").disabled = !document.getElementById("check").checked;
}
function back()
{
    if (document.getElementById("stage").value=="3")
    {   
        document.getElementById("stage").value="1";
    } else {
        document.getElementById("stage").value="";
    }
    document.myform.submit();
}

function copyIndex()
{

        document.getElementById("judetFIndex").value=document.getElementById("judet_factura").options.selectedIndex;
        document.getElementById("judetIndex").value=document.getElementById("judet").options.selectedIndex;
}
function hide()
{
    if(document.getElementById('mes')!=null){
        document.getElementById('mes').style.display='none';
    }
}