// JavaScript Document
var dhtml = (document.getElementById || document.all || document.layers);
var http_request = false;
var http_address = "http://stripky.tom/visin.php";

function getObj(name) {
  if (document.getElementById){
  this.obj = document.getElementById(name);
	this.style = document.getElementById(name).style;
  }
  else if (document.all) {
	this.obj = document.all[name];
	this.style = document.all[name].style;
  }
  else if (document.layers) {
   	this.obj = document.layers[name];
   	this.style = document.layers[name];
  }
}

function invi(name) {
	if (!dhtml) return;
  // Proměnné
  var x = new getObj(name);
	// Objekt x
	if (x.style.display == 'none'){x.style.display = 'block';}
  else {x.style.display = 'none';}
  // Adresa
  address = http_address + '?modul=' + name + '&d=' + x.style.display;
  // Ajax
  makeRequest(address);
}

function invi2(name1, name2, name3, v1, v2, v3) {
	if (!dhtml) return;
  // Proměnné
  var x = new getObj(name1);
	var y = new getObj(name2);
	var z = new getObj(name3);
	// Objekt x
	x.style.display = v1;
	y.style.display = v2;
	z.style.display = v3;
  // Adresa
  address = http_address + '?modul=' + name1 + '&d=' + v1;
  // Ajax
  makeRequest(address);
}

function invi3(name,s) {
	if (!dhtml) return;
	var x = new getObj(name);
  x.style.display = s;
}

function makeRequest(adresa) {
  http_request = false;
 
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    http_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) { // IE
    try {
      http_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }

  if (!http_request) {
    //alert('Lituji, ale váš prohlížeč nepodporuje objekt XmlHttpRequest :-(');
    return false;
  }
  //http_request.onreadystatechange = alertContents;
  http_request.open('GET', adresa, true);
  http_request.send(null);
}

function alertContents() {
  if (http_request.readyState == 4) {
    if (http_request.status == 200) {
      alert(http_request.responseText);
    }
    else {
      alert('Požadavek se nepodařilo zpracovat.');
    }
  }
}
/********************************************
Kontrola PSČ
********************************************/
function checkPSC (strPSC)  {
  var re = /\d\d\d\d\d/
  
  if (strPSC.length != 5) {
    alert("Pole PSČ musí být ve tvaru XXXXX")
    return false
  }
  if (strPSC.match(re) == null) {
    alert("Pole PSČ může obsahovat pouze číslice 0-9.")
    return false
  }
    return true
}
/********************************************
Kontrola mailové adresy
********************************************/
function checkMail(emailStr) {
  var emailPat=/^(.+)@(.+)$/
  var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
  var validChars="\[^\\s" + specialChars + "\]"
  var quotedUser="(\"[^\"]*\")"
  var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
  var atom=validChars + '+'
  var word="(" + atom + "|" + quotedUser + ")"
  var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
  var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
  
  var matchArray=emailStr.match(emailPat)
  if (matchArray==null) {
  	alert("Mailová adresa není spravně.\nZkontrolujte zda adresa obsahuje znaky @ a .")
  	return false
  }
  var user=matchArray[1]
  var domain=matchArray[2]
   
  if (user.match(userPat)==null) {
      alert("Jméno v mailové adrese není korektní.")
      return false
  }
  
  var IPArray=domain.match(ipDomainPat)
  if (IPArray!=null) {
  	  for (var i=1;i<=4;i++) {
  	    if (IPArray[i]>255) {
  	        alert("Cílová IP adresa je chybná!")
  		      return false
  	    }
      }
      return true
  }
  
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
  	alert("V mailové adrese není správný název domény .")
      return false
  }
  
  var atomPat=new RegExp(atom,"g")
  var domArr=domain.match(atomPat)
  var len=domArr.length
  if (domArr[domArr.length-1].length<2 || 
      domArr[domArr.length-1].length>3) {
     alert("Mailová adresa musí obsahovat tři znaky pro určení domény, nebo dva znaky pro určení země.")
     return false
  }
  
  if (len<2) {
     var errStr="V mailové adrese chybý hostname!"
     alert(errStr)
     return false
  }
  return true
}
/********************************************
Kontrola telefonního čísla
********************************************/
function checkTel (strTel)  {
  var re = /\d\d\d\d\d\d\d\d\d/
  
  if (strTel.length != 9) {
    alert("Pole Telefon musí být ve tvaru XXXXXXXXX");
    return false
  }
  if (strTel.match(re) == null) {
    alert("Pole Telefon může obsahovat pouze číslice 0-9.")
    return false
  }
    return true
}
