vf_testcamp=new Array();

function vf_mesaj(formular, cod)
{
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")) document.getElementById(formular+"_email").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++)
{
if (vf_testcamp[i].obligatoriu)
  {
  if (formular==vf_testcamp[i].formular && document.getElementById(vf_testcamp[i].camp).value == vf_testcamp[i].interzis)
    {
    document.getElementById(vf_testcamp[i].label).style.color=errcolor;
    valid=0;
    }
  else document.getElementById(vf_testcamp[i].label).style.color=normcolor;
  }
}
if (valid)
  {
  MakePost(serverpage, vf_creeaza_parametrii(formular), formular)
  }
  
else
  {
  vf_mesaj(formular, "obligatorii");
  }
}

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)
  {
  alert('Browser doesn\'t support AJAX.');
  return false;
  }

	http_request.onreadystatechange = function()
    {
    if (http_request.readyState == 4)
      {
      if (http_request.status == 200)
        {
        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)
{
poststr="vf_campuri_numar="+vf_testcamp.length;
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"+j+"="+URLEncode(document.getElementById(vf_testcamp[j].label).innerText);
    else poststr+="&vf_label"+j+"="+URLEncode(document.getElementById(vf_testcamp[j].label).textContent);
    poststr+="&vf_content"+j+"="+URLEncode(document.getElementById(vf_testcamp[j].camp).value)
    }
  }
return poststr;
}
