/************************
HINTS:
nie stosowaæ htmlentities przy requestach ajaxowych po stronie php
************************/


var submitTimer; //do czyszczenia zegara
window.onload = function() {
  	var inputs = getElementsByClassName('notok');
	var i;

	
	inputs[0].focus();
	$('pass').value=''; //wyczyszczenie has³a
	
	$('login').onkeyup = function() {
		clearTimeout(submitTimer); // czysci wszelkie wczesniejsze wywolania setTimeout
		//opoznienie po wpisanu logina
		submitTimer = setTimeout(loginCheck,1000);
	}
	$('pass').onkeyup = passCheck;
	$('pass2').onkeyup = passCheck;
	$('e-mail').onkeyup = emailCheck;
	$('imie').onkeyup = letterCheck;
	$('nazwisko').onkeyup = letterCheck;
	$('firma').onkeyup = firmaCheck;
	$('nip').onkeyup = nipCheck;
	$('ulica').onkeyup = ulicaCheck;	
	$('nr_bud').onkeyup = nr_budCheck;
	$('poczta').onkeyup = pocztaCheck;
	$('miasto').onkeyup = letterCheck;
	$('telefon').onkeyup = phoneNumberCheck;
	$('fax').onkeyup = phoneNumberCheck;
}

function loginCheck() {
	var re = /^[a-zA-Z0-9±æê³ñó¶¿¼]+$/;
	if ($('login').value.search(re) != -1) {
		$('login').parentNode.nextSibling.innerHTML = 'OK';
		$('login').className = 'ok'
		
		var url = 'register_check.php'; //skrypt php tworzy dokument xml
		var strParams = 'login=' + $('login').value;

		
		var xhr = createXMLHttpRequest();
		xhr.onreadystatechange = function() {
			
			$('login').parentNode.nextSibling.innerHTML = '<img src="images/indicator.gif" alt="">Sprawdzam login...';
			if (xhr.readyState == 4) {
				if (xhr.status == 200) {
					var xmlDoc; 
					xmlDoc = xhr.responseXML;
					var xmlForm = xmlDoc.firstChild;
	 				var xmlPola = xmlForm.childNodes;
	 				
	 				$('login').parentNode.nextSibling.innerHTML = xmlPola[0].firstChild.nodeValue;
	 				//je¶li ju¿ taki login istnieje 
	 				if ($('login').parentNode.nextSibling.innerHTML != 'OK') {
	 					$('login').className = 'notok';
	 					regCheck()
	 				}
	 			}
				else {	
					alert("Wyst±pi³ b³±d, spróbuj ponownie");
				}
			}
		}	
		
		xhr.open("POST", url , true);
	
		xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xhr.setRequestHeader("Content-length", strParams.length);
		xhr.setRequestHeader("Connection", "close");
		xhr.send(strParams);		
	} 
	else {
		$('login').parentNode.nextSibling.innerHTML = 'Tylko litery i cyfry';
		$('login').className = 'notok';
	}
	regCheck()
}

function passCheck() {
	var re = /^[a-zA-Z0-9±æê³ñó¶¿¼]+$/;

	if (this.value.length > 4) {
		if (this.value.search(re) != -1) {
			this.parentNode.nextSibling.innerHTML = 'OK';	
			this.className ='ok';
			
			if ( ($('pass2').value == $('pass').value) && ($('pass2').value.length > 4) ) {
				$('pass').className = 'ok';
   			$('pass2').className ='ok';
				$('pass2').parentNode.nextSibling.innerHTML = 'OK';
			}
   		else {
   			$('pass2').className = 'notok';
   			$('pass2').parentNode.nextSibling.innerHTML = 'Has³a s± ró¿ne';
   		}
		}
		else {
			this.parentNode.nextSibling.innerHTML = 'Tylko litery i cyfry';
			$('pass').className = 'notok';
			$('pass2').className = 'notok';
		}	
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Za ma³o znaków';
		this.className = 'notok';
	}
	regCheck()
}

function emailCheck() {
	var re = /^[a-zA-Z0-9±æê³ñó¶¿¼_\-\.]+@[a-zA-Z0-9±æê³ñó¶¿¼_\-\.]+\.[a-zA-Z0-9±æê³ñó¶¿¼_\-\.]+$/;
	if (this.value.search(re) != -1) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'To nie jest poprawny adres e-mail';	
		this.className ='notok';
	}
	regCheck()
}

//imie, nazwisko, miasto
function letterCheck() {
	var re = /^[a-zA-Z±æê³ñó¶¿¼]+$/;
	if (this.value.search(re) != -1) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Tylko litery';	
		this.className ='notok';
	}
	regCheck()
}

function firmaCheck() {
	if (this.value.length > 0) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Dowolne znaki';	
		this.className ='notok';
	}
	regCheck()
}

function nipCheck() {
	var re = /^[0-9]{3}-[0-9]{3}-[0-9]{2}-[0-9]{2}$/;
	if (this.value.search(re) != -1) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Tylko format xxx-xxx-xx-xx';	
		this.className ='notok';
	}
	regCheck()
}

function ulicaCheck() {
	var re = /^[a-zA-Z0-9±æê³ñó¶¿¼_\-\/ ]+$/;	
	if (this.value.search(re) != -1) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Niedozwolony znak';	
		this.className ='notok';
	}
	regCheck()
}

function nr_budCheck() {
	var re = /^[0-9\/]+$/;
	if (this.value.search(re) != -1) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Tylko cyfry i \'/\'';	
		this.className ='notok';
	}
	regCheck()
}

function pocztaCheck() {
	var re = /^[0-9]{2}-[0-9]{3}$/;
	if (this.value.search(re) != -1) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Tylko format xx-xxx';	
		this.className ='notok';
	}
	regCheck()
}

function phoneNumberCheck() {
	var re = /^[0-9\+ ]+$/;
	if (this.value.search(re) != -1) {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else if (this.value == '') {
		this.parentNode.nextSibling.innerHTML = 'OK';	
		this.className ='ok';
	}
	else {
		this.parentNode.nextSibling.innerHTML = 'Niedozwolony znak';	
		this.className ='notok';
	}
	regCheck()
}

function regCheck() {
	var inputs = getElementsByClassName('notok');
	if (inputs.length>0) {
		$('submit').value = 'Uzupe³nij dane';
		$('submit').disabled = true;
	}
	else {
		$('submit').value = 'Rejestruj';
		$('submit').disabled = false;
	}

}

//stworzenie obiektu XML
function createXMLHttpRequest() {
	try {return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {}
	try {return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
	try {return new XMLHttpRequest(); } catch(e) {}
	
	return null;
}

//alias na getElementById
function $() {

	var elements = new Array();
	
	for (var i = 0; i < arguments.length; i++) {

		var element = arguments[i];

		if (typeof element == 'string') {
			if (document.getElementById) {
				element = document.getElementById(element);
			} 
			else if (document.all) {
				element = document.all[element];
			}
		}

		elements.push(element);

	}

	if (arguments.length == 1 && elements.length > 0) {
			return elements[0];
	}
	else {
		return elements;
	}
}

function getElementsByClassName(cn){
  var arr = new Array(); 
  var els = document.getElementsByTagName("*");
  var exp= new RegExp("^(.* )?"+cn+"( .*)?$", "g");
  for (var i = 0; i < els.length; i++ ){
    if (exp.test(els[i].className)){
      arr.push(els[i]);
    }
  }
  return arr;
}
