var t_err="";
var t_button_id='send_form';
var t_errors=new Array();
var t_frm=null;
var t_fltr=false;
var t_prev=false;

function openclose(id){
  var oc=document.getElementById(id).style;
  if (oc.display=='' || oc.display=='none') oc.display='block';
  else oc.display='none';
  return false;
}

function reFresh(o){
  if (o.id=='codepic') o.src='http://www.skyguard.hu/tform/codegen.php?r='+Math.floor(Math.random()*10000);
  else o.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader( src='http://www.skyguard.hu/tform/codegen.php?"+Math.floor(Math.random()*10000)+"', sizingMethod='image');";
}

function showframe(t){
  var t_o=document.getElementById('naptar_'+t.id);
  t_o.style.display='block';
  if (t_prev && t_prev!=t_o) t_prev.style.display='none';
  t_o.style.top=t.offsetTop-t_o.offsetHeight+18+"px";;
  t_o.style.left=t.offsetLeft-t_o.offsetWidth-1+"px";
  t.focus();
}

function drawsign(id,type,error){
  if (t_fltr==true){
    nc=document.createElement('div');
    nc.style.position="absolute";
    nc.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://www.skyguard.hu/tform/"+type+".png', sizingMethod='scale');";
  }
  else {
    nc=document.createElement('img');
    var tmpsrc="http://www.skyguard.hu/tform/"+type+".png";
    nc.src=tmpsrc;
  }
  nc.id=id+"_m";
  nc.style.width="20px";
  nc.style.height="20px";
  nc.style.position="absolute";
  nc.style.left="0px";
  nc.style.top="0px";
  nc.style.borderWidth="0";

   if (type=='pirosfelk') {
    nc.style.cursor="help";
    nc.onmouseover=new Function("document.getElementById('formerror').style.display='block';");
    nc.onmouseout=new Function("document.getElementById('formerror').style.display='none';");
    nc.onmousemove=function (e){
      obj=document.getElementById('formerror');
      obj.lastChild.previousSibling.innerHTML=error;
      if (e==undefined) e=event;
      scrlT=document.body.scrollTop || document.documentElement.scrollTop;
      scrlL=document.body.scrollLeft || document.documentElement.scrollLeft;
      obj.style.top=e.clientY+scrlT+13+'px';
      obj.style.left=e.clientX+scrlL+13+'px';
    }
  }
  dob=document.getElementById(id).parentNode;
  if (dob.lastChild.id==id+"_m") dob.replaceChild(nc,dob.lastChild);
  else dob.appendChild(nc);
  //frst=dob.firstChild;
  //if (frst.id==id+"_m") dob.replaceChild(nc,frst);
  //else dob.insertBefore(nc,frst);
}

function tagsToArray(cols) {
	a=new Array();
	for (var i=0; i<cols.length; i++){
	  col=t_frm.getElementsByTagName(cols[i]);
  	for (var j=0; j<col.length; j++)
  		a[a.length]=col[j];
  }	
	return a;
}

function initcheck(){
  t_frm=document.t_form;
  if (document.getElementById(t_button_id)){
    ediv=document.createElement('div');
    ediv.id='formerror';
    ediv.innerHTML='<span>Error message</span><div>&nbsp;</div>';
    document.body.appendChild(ediv);
    ediv.style.display='none';
    var arVersion = navigator.appVersion.split("MSIE");
    var ver = parseFloat(arVersion[1]);
    if ((ver >= 5.5) && (document.body.filters)) t_fltr=true;
    else t_fltr=false;
    
    l=tagsToArray(['input','textarea','select']);

    for (var i=0; i<l.length; i++){
      l[i].style.filter="alpha(opacity:'70')";
      l[i].style.opacity='0.7';
      /*l[i].onfocus=function(e) {
        this.style.filter="alpha(opacity:'100')";
        this.style.opacity='1';
        if (t_prev) t_prev.style.display='none';
      }
      l[i].onblur=function(e) {
        this.style.filter="alpha(opacity:'70')";
        this.style.opacity='0.7';
      }*/
      if (l[i].id){

        t=l[i].id.split("_");
        if (t[0].toLowerCase()=='choose'){
          s=String(l[i].onchange);
          if (s.length>10){
            tmp=s.split('{');
            tt=tmp[1].split('}');
            core=tt[0];
            //core+=" error_check(this);";
            l[i].onchange=function(){ error_check(this); eval(core);};
          }
          else l[i].onchange=function(){ error_check(this);};
        }

        if (t[0].toLowerCase()=='date'){
          anyu=l[i].parentNode;
          l[i].readOnly='readonly';
          
          var t_mif=document.createElement('iframe');
          t_mif.id="naptar_"+l[i].id;
          t_mif.frameBorder="0";
          t_mif.src="../../tform/naptar.php?inp="+l[i].id+"&num="+i;
          t_mif.style.position='absolute';
          t_mif.style.display='none';
          t_mif.style.borderRight='1px solid #999';
          t_mif.style.borderBottom='1px solid #888';
          t_mif.style.borderLeft='1px solid #ccc';
          t_mif.style.borderTop='1px solid #ddd';
          t_mif.style.width='156px';
          t_mif.style.height='179px';
          t_mif.style.background='white';
          t_mif.style.zIndex='100';
          anyu.insertBefore(t_mif,l[i]);
//          anyu.appendChild(t_mif);
          l[i].onfocus=function(){
		showframe(this);
		this.style.filter="alpha(opacity:'100')";
		this.style.opacity='1';
		}
          l[i].onblur=function(e) {
            this.style.filter="alpha(opacity:'70')";
            this.style.opacity='0.7';
            t_prev=this.previousSibling;
          }
        }
        else {
          l[i].onfocus=new Function("if (t_prev) t_prev.style.display='none'; this.style.filter=\"alpha(opacity:'100')\"; this.style.opacity='1';");
          l[i].onblur=function() {
            this.style.filter="alpha(opacity:'70')";
            this.style.opacity='0.7';
          }
        }

        l[i].onkeyup=function() { error_check(this); }
        error_check(l[i]);
      }
    }
  }
}

function check(obj){
  t=obj.id.split("_");
  t_err="";
  if (obj.value=="" && t[t.length-1]=="MAND") {t_err="Kötelezően kitöltendő mező!<br/>";}
  else if (obj.value=="" && t[t.length-1]!="MAND") t_err="";
  else{
    switch (t[0].toLowerCase()){
      case "char":{
        if (obj.value.length>t[1]) t_err="Maximum "+t[1]+" karakter!";
      }
      break;
      case "num":{
        if (isNaN(obj.value)) t_err="Csak szám lehetséges!<br/>(E.g: 12)";
        else if (obj.value==0) t_err="0 nem megfelelő!";
        else if (obj.value.length>t[1]) t_err="Maximum "+t[1]+" számjegy!<br/>(E.g: 12)";
      }
      break;
      case "numnull":{
        if (isNaN(obj.value)) t_err="Csak szám lehetséges!<br/>(E.g: 12)";
        else if (obj.value.length>t[1]) t_err="Maximum "+t[1]+" számjegy!<br/>(E.g: 12)";
      }
      break;

      case "time":{
        reg=/^(\d{1,2}):(\d{2})$/;
        if (!reg.test(obj.value)) t_err="Nem megfelelő idő formátum!<br /> (E.g: 21:35)";
      }
      break;
      case "tel":{
        reg=/^[0-9\/+ -]*$/;
        if (!reg.test(obj.value) || obj.value.length>18 || obj.value.length<7) t_err="Nem megfelelő telefonszám forma!<br/> (E.g: +36 20 444-5334)";
      }
      break;
      case "email":{
        reg=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
        if (!reg.test(obj.value)) t_err="Helytelen e-mail forma!<br /> (E.g: name@mailserver.com)";
      }
      break;
      case "fname":{
        if (!isNaN(obj.value)) t_err="Számot nem tartalmazhat!<br /> (E.g: John)";
        else if (obj.value.length<2) t_err="Minimum 5 karakter!<br /> (E.g: Smith)";
      }
      break;
      case "lname":{
        if (!isNaN(obj.value)) t_err="Számot nem tartalmazhat!<br/> (E.g: Smith)";
        else if (obj.value.length<2) t_err="Minimum 2 karakter!<br /> (E.g: Smith)";
      }
      break;
      case "name":{
        if (!isNaN(obj.value)) t_err="Számot nem tartalmazhat! <br/> (E.g: John Smith)";
        else if (obj.value.length<6) t_err="Minimum 6 karakter!<br/> (E.g: John Smith)";
      }
      break;
      case "date":{
        reg=/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
        if (!reg.test(obj.value)) t_err="Válasszon dátumot!";
      }
      break;
      case "choose":{
          if (obj.value=="" && obj.value=="0" ) {t_err="Kérem válasszon!";}
      }
      break;
    }
  }
  if (t_err!="") return false;
  else return true;
}

function error_check(obj){
  if (!check(obj)){
    t=obj.id.split("_");
    drawsign(obj.id,"pirosfelk",t_err);
    hit=false;
    for (i=0; i<t_errors.length; i++){
      if (t_errors[i]==obj.id) hit=true;
    }
    if (!hit) t_errors[t_errors.length]=obj.id;
  }
  else{
    drawsign(obj.id,"zoldpipa",t_err);
    for (i=0; i<t_errors.length; i++){
      if (t_errors[i]==obj.id) t_errors.splice(i,1);
    }
  }
  if (t_errors.length==0) {
    t_frm.action="../tform/proc.php";
    document.getElementById(t_button_id).style.visibility="visible";
  }
  else document.getElementById(t_button_id).style.visibility="hidden";
}