function externalLinks(){
	for (var i=0; (a=document.getElementsByTagName('a')[i]);i++) {
          if (a.getAttribute("rel") == "external")
               a.target = "_blank";
	}
}

function setTime() {
	if (document.getElementById('seconds').className == 'passed'){
		document.getElementById('seconds').innerHTML -= (-1);
	} else {
		document.getElementById('seconds').innerHTML -= 1;
	}
}

function setTimer(){
	if (document.getElementById('seconds')){
		setTime();
		setTimeout("setTimer()",1000);
	}
}

function checkClock(){
	var nx = document.getElementById('x_name').value;
	var hx = document.getElementById('x_height').value;
	var wx = document.getElementById('x_weight').value;
	var dd = document.getElementById('x_day').value;
	var mm = document.getElementById('x_month').value;
	
	if (nx == ''){
		alert('Proszę podać swoje imię i nazwisko.');
		return false;
	}
	
	if (document.getElementById('x_man').checked == false && document.getElementById('x_woman').checked == false){
		alert('Proszę zaznaczyć swoją płeć.');
		return false;
	}

	if (hx < 30 || hx > 300 || hx == '' || isNaN(hx) == true ){
		alert('Proszę podać poprawny wzrost.');
		return false;
	}
	
	if (wx < 5 || wx > 300 || wx == '' || isNaN(wx) == true){
		alert('Proszę podać poprawną wagę.');
		return false;
	}
	
	if((mm == '02' && dd > '29') || (dd > '30' && (mm == '04' || mm == '06' || mm == '09' || mm == '11'))){
		alert('Proszę wybrać poprawną datę.');
		return false;
	}
	
	return true;
}

function checkContact(){
	var x_fields = new Array('x_name','x_email','x_subject','x_message');
	var x_email = document.getElementById('x_email');
	var x = 0;
	for (var i=0; i<x_fields.length; i++){
		if (document.getElementById(x_fields[i]).value == ''){
			x++;
		}
	}
	if (x > 0){
		alert('Proszę wypełnić wszystkie pola formularza.');
		return false;
	}
	if ((x_email.value.indexOf(".") < 1) || (x_email.value.indexOf("@") < 1)){
		alert('Proszę wpisać poprawny adres e-mail');
		return false;
	}
	return true;
}

function removeSendText(x,y){
	if(document.getElementById(x)){
		document.getElementById(x).onfocus = function(){
			if(this.value == y){
				this.value = '';
			}
		}
	}
}

function checkSend(){
	var x_email = document.getElementById('x_email');
	if ((x_email.value.indexOf(".") < 3) || (x_email.value.indexOf("@") < 1)){
		alert('Proszę wpisać poprawny adres e-mail');
		return false;
	}	
}

function checkForm(x){
	if (document.getElementById(x)){
		document.getElementById(x).onsubmit = function(){
			if (x == 'x_form'){
				return checkClock();
			} else if(x == 'x_contact'){
				return checkContact();
			} else if(x == 'x_send'){
				return checkSend();
			}
		}
	}
}

function init(){
	if(arguments.callee.done)return;
	arguments.callee.done=true;
	externalLinks();
	setTimer();
	checkForm('x_form');
	checkForm('x_contact');
	checkForm('x_send');
	removeSendText('x_email','podaj adres email...');
};


if(document.addEventListener){document.addEventListener("DOMContentLoaded", init, null);}
/*@cc_on @*/
/*@if (@_win32)
  document.write('<script defer="defer" src="./js/javascript_ieo.js"><'+'/script>');
/*@end @*/
window.onload=init;
