//1.93 [non-ajax support]
//1.94 [better error handling for mailserver erors]
//b_validator version:1.95 [error handling bugfix on mailserver errors]

var b_debug_mode=0;

vf_testcamp=new Array();

function vf_mesaj(formular, cod)
{
if (cod.indexOf("email_invalid")>-1) cod="email_invalid";
if (document.getElementById(formular+"_succes")) document.getElementById(formular+"_succes").style.display="none";
if (document.getElementById(formular+"_obligatorii")) document.getElementById(formular+"_obligatorii").style.display="none";
if (document.getElementById(formular+"_email_invalid")) document.getElementById(formular+"_email_invalid").style.display="none";
if (document.getElementById(formular+"_eroare")) document.getElementById(formular+"_eroare").style.display="none";
if (document.getElementById(formular+"_"+cod)) document.getElementById(formular+"_"+cod).style.display="block";
}

function camp_optional(formular, camp, label)
{
vf_testcamp.push(new vf_camp(formular, camp, label));
}

function camp_obligatoriu(formular, camp, label, valoare_interzisa)
{
vf_testcamp.push(new vf_camp(formular, camp, label, valoare_interzisa, 1));
}

function vf_camp(_formular, _camp, _label, _valoare_interzisa, _obligatoriu)
{
this.formular=_formular;
this.camp=_camp;
this.label=_label;
this.interzis=_valoare_interzisa;
this.obligatoriu = _obligatoriu;
}

function verifica(serverpage, formular, errcolor, normcolor)
{
valid=1;

for (i=0; i<vf_testcamp.length; i++)
{
document.getElementById(vf_testcamp[i].label).style.color=normcolor;
if (b_debug_mode) alert(vf_testcamp[i].camp);
if (vf_testcamp[i].obligatoriu && formular==vf_testcamp[i].formular)
  {
  if ((typeof vf_testcamp[i].camp) != "string") // radio buttons
    {
    radio_valid=0;
    for (j=0; j<vf_testcamp[i].camp.length; j++)
      {
      if (document.getElementById(vf_testcamp[i].camp[j]).checked) radio_valid=1;
      }
    if (!radio_valid)
      {
      valid=0;
      document.getElementById(vf_testcamp[i].label).style.color=errcolor;
      }
    }
  else //textfield / select
    {
    if (document.getElementById(vf_testcamp[i].camp).type!="checkbox")
      {
      if (document.getElementById(vf_testcamp[i].camp).value == vf_testcamp[i].interzis)
        {
        document.getElementById(vf_testcamp[i].label).style.color=errcolor;
        valid=0;
        }
      }
    else //checkbox
      {
      if (vf_testcamp[i].interzis == document.getElementById(vf_testcamp[i].camp).checked)
        {
        valid=0;
        document.getElementById(vf_testcamp[i].label).style.color=errcolor;
        }
      }
    }
  }
}
if (valid)
  {
  if (serverpage!="NOAJAX")
    MakePost(serverpage, vf_creeaza_parametrii(formular), formular)
  else
    return true;
  }
else
  {
  vf_mesaj(formular, "obligatorii");
  if (serverpage=="NOAJAX") return false;
  }
}

function MakePost(url, parameters, formular)
{
http_request = false;
if (window.XMLHttpRequest)
  {
  http_request = new XMLHttpRequest();
  if (http_request.overrideMimeType)
    {
    http_request.overrideMimeType('text/html');
    }
  }
else
  if (window.ActiveXObject)
    {
    try
      {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
      try
        {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			  }
      catch (e) {}
		  }
	  }
if (!http_request)
  {
  return false;
  }

	http_request.onreadystatechange = function()
    {
    if (http_request.readyState == 4)
      {
      if (http_request.status == 200)
        {
        if(b_debug_mode) alert(http_request.responseText)
        vf_mesaj(formular, http_request.responseText);
        }
      else
        {
        vf_mesaj(formular, "eroare");
        }
      }
    }
	http_request.open('POST', url, true);
	http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http_request.setRequestHeader("Content-length", parameters.length);
	http_request.setRequestHeader("Connection", "close");
	http_request.send(parameters);
}

function URLEncode(plaintext)
{
var SAFECHARS = "0123456789" +					// Numeric
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
"abcdefghijklmnopqrstuvwxyz" +
"-_.!~*'()";					// RFC2396 Mark characters
var HEX = "0123456789ABCDEF";

var encoded = "";
for (var i = 0; i < plaintext.length; i++ )
  {
  var ch = plaintext.charAt(i);
  if (ch == " ")
    {
    encoded += "+";				// x-www-urlencoded, rather than %20
    }
  else
    if (SAFECHARS.indexOf(ch) != -1)
      {
      encoded += ch;
      }
    else
      {
      var charCode = ch.charCodeAt(0);
      encoded += "%";
      encoded += HEX.charAt((charCode >> 4) & 0xF);
      encoded += HEX.charAt(charCode & 0xF);
      }
  }
return encoded;
}


function vf_creeaza_parametrii(formular)
{
vf_numar_campuri=0;
poststr="";
for (j=0; j<vf_testcamp.length; j++)
  {
  if (vf_testcamp[j].formular==formular)
    {
    if (document.getElementById(vf_testcamp[j].label).innerText) poststr+="&vf_label"+vf_numar_campuri+"="+URLEncode(document.getElementById(vf_testcamp[j].label).innerText);
    else if(document.getElementById(vf_testcamp[j].label).textContent) poststr+="&vf_label"+vf_numar_campuri+"="+URLEncode(document.getElementById(vf_testcamp[j].label).textContent);
    else poststr+="&vf_label"+vf_numar_campuri+"=";
    
    if (typeof vf_testcamp[j].camp!='string')
      {
      k=0;
      while(k<vf_testcamp[j].camp.length && !(document.getElementById(vf_testcamp[j].camp[k]).checked)) k++;
      if (k<vf_testcamp[j].camp.length) poststr+="&vf_content"+vf_numar_campuri+"="+URLEncode(document.getElementById(vf_testcamp[j].camp[k]).value);
      else poststr+="&vf_content"+vf_numar_campuri+"=Undefined%20(Nedefinit)";
      }
    else
      {
      if (document.getElementById(vf_testcamp[j].camp).type=="checkbox") cf_creeaza_parametri_temp_valoare=(document.getElementById(vf_testcamp[j].camp).checked)?"Yes%20(Da)":"No%20(Nu)";
      else cf_creeaza_parametri_temp_valoare=document.getElementById(vf_testcamp[j].camp).value
      poststr+="&vf_content"+vf_numar_campuri+"="+URLEncode(cf_creeaza_parametri_temp_valoare)
      }
    vf_numar_campuri++;
    }
  }
poststr+="&vf_campuri_numar="+vf_numar_campuri;
return poststr;
}