// JavaScript Document
var gStrToParse;
var gIntParsePos;
var gLastNumber;

function ParseAZ()
{
  var count=0;
  var ch=gStrToParse.charAt(gIntParsePos);
  while ('a'<= ch && ch<='z' || 'A'<=ch && ch<='Z'|| '0'<=ch && ch<='9' || ch=='_' || ch=='-')
  {
    if (count == 0 && ch=='-')
	break;
    if (count == gStrToParse.length-1 && ch=='-')
	break;
    count++;
    gIntParsePos++;
    ch=gStrToParse.charAt(gIntParsePos);
  }
  return (count>0);
}

function ParseName()
{
  var count=0;
  var ch=gStrToParse.charAt(gIntParsePos);
  while ('a'<= ch && ch<='z' || 'A'<=ch && ch<='Z' || ch==' ' || ch=='-' || ch==',' || ch=='.')
  {
    if (count == 0 && ch=='-')
    	break;
    if (count == gStrToParse.length-1 && ch=='-')
    	break;
    count++;
    gIntParsePos++;
    ch=gStrToParse.charAt(gIntParsePos);
  }
  return (count>0);
}

function ParseChar(ch)
{
  if (gStrToParse.charAt(gIntParsePos)==ch)
  {
    gIntParsePos++;
    return true;
  }
  else
  {
    return false;  
  }
}

function ParseEmail(m)
{
//  alert("ParseEmail");
  gStrToParse = m;
  gIntParsePos = 0;
  if (!ParseAZ()) return false;
  while (ParseChar('.'))
  {
    if (!ParseAZ()) return false;
  }
  if (!ParseChar('@')) return false
  if (!ParseAZ()) return false;
  if (!ParseChar('.')) return false;
  if (!ParseAZ()) return false;
  while (ParseChar('.'))
  {
    if (!ParseAZ()) return false;
  }
  return true;
}

function ParseTelefon(m)
{
  var count=0;
  gStrToParse = m;
//  alert(m);
  gIntParsePos = 0;
  gLastNumber="";
  var ch=gStrToParse.charAt(gIntParsePos);
  while (("0"<=ch && ch<="9" || ch == " " || ch == ",")&& count<gStrToParse.length)
  {
    count++;
    gLastNumber = gLastNumber+ch;
    gIntParsePos++;
//    alert(ch);
    ch=gStrToParse.charAt(gIntParsePos);    
  }    
  return (count==gStrToParse.length);
}


function ParseNumber(m)
{
  var count=0;
  gStrToParse = m;
//  alert(m);
  gIntParsePos = 0;
  gLastNumber="";
  var ch=gStrToParse.charAt(gIntParsePos);
  while ("0"<=ch && ch<="9" && count<gStrToParse.length)
  {
    count++;
    gLastNumber = gLastNumber+ch;
    gIntParsePos++;
//    alert(ch);
    ch=gStrToParse.charAt(gIntParsePos);    
  }
  return (count==gStrToParse.length);
}

function bejelentkezes_submit()
{
  var frm = document.getElementById("bejelentkezes_form");
  for (i=0;i<frm.elements.length;i++)
  {
    var elm = frm.elements[i];
    if (elm.name == "email")
    { 
      if (elm.value == "")
      {
        alert("az email mezo ures");
        elm.focus();
        return false;
      }
      if (!ParseEmail(elm.value))
      {
        alert("az email mezo nem emailcimet tartalmaz:"+elm.value);
        elm.focus();
        return false;               
      }
    }

    if (elm.name == "jelszo")
    {
      if(elm.value == "")
      {
        alert("jelszo mezo ures");
        elm.focus();
        return false;  
      }
    }
  }
  return true;
}

function addform_submit()
{
  var frm = document.getElementById("add_hirdetes_form");
  var uresmail		= false;
  var urestelefon	= false;
  var elm = null;
  for ( i = 0; i < frm.elements.length; i++ )
  {
    elm = frm.elements[i];

    if (elm.name == "email")
    { 
//      alert("email");
      if (elm.value == "")
      {
        uresmail = true;
      }
      else if (!ParseEmail(elm.value))
      {
        alert("az email mezo nem tartalmaz ervenyes emailcimet :"+elm.value);
        elm.focus();
        return false;               
      }
    }

    if (elm.name == "telefon")
    {
//      alert("telefon");
      if(elm.value == "")
      {
        urestelefon = true;
      }
      else if (!ParseTelefon(elm.value))
      {        
       alert("a telefon mezo kitoltesenel kerjuk csak szamokat hasznaljon:"+elm.value);
        elm.focus();
        return false;               
      }
    }

    if (elm.name == "szoveg")
    {
    //  alert("szoveg");
      if(elm.value == "")
      {
        alert("a szoveg mezojet nem toltotte ki");
        elm.focus();
        return false;
      }
      if(elm.value.length > 200)
      {
        alert("a szoveg tul hosszu");
        elm.focus();
        return false;
      }
    }
 
    if (elm.name == "captcha_str")
    {
     // alert("captcha");
      if(elm.value == "")
      {
        alert("az ellenorzo kod mezojet nem toltotte ki,kerjuk masolja at a bal oldali keprol a kodot");
        elm.focus();
        return false;
      }
    }

  }
  
//  alert("vege a ciklusnak");
  
  if (urestelefon && uresmail)
  {
    alert("legalabb az e-mailt vagy a telefonszamot meg kell adni");
    elm = document.getElementById("textfield3");
    elm.focus();
    return false;  
  }
  
  return true;
}
