// JavaScript Document
// Body Mass Index Calculator
// copyright 28th April 2006, by Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (including these
// comments) is used without any alteration



function valButton(btn) {
	var cnt = -1;
	for (var i=btn.length-1; i > -1; i--) {
		if (btn[i].checked) {
			cnt = i;
			i = -1;
			}
	}
	if (cnt > -1) return btn[cnt].value;
	else return null;
}

function stripBlanks(fld) {
	var result = "";
	var c = 0;
	for (i=0; i < fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") c = result.length;
		}
	}
	return result.substr(0,c);
}

function calc(thisform) {
	var s = valButton(thisform.s);
	if (s == null) {
		alert("Vyberte si pohlavie Žena alebo Muž");
		return false;
	}
	var meno = stripBlanks(thisform.meno.value);
	if (meno == '') {
		alert("Nezadali ste Meno.");
		thisform.meno.focus();
		return false;
	}    

	var email = stripBlanks(thisform.email.value);
	if (email=='') {
			alert("Nezadali ste E-mail adresu");
			thisform.email.focus();
			return false;
	}
	else {
         var filter=/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;		
		 if (filter.test(email)){
	   		
		 }
		else{
			alert("E-mail adresa nie je ok");
			thisform.email.focus();
			return false;
		
		 }
		
	}
    var h = stripBlanks(thisform.vyska.value);
	if (h == '') {
		alert("Nezadali ste výšku.");
		thisform.vyska.focus();
		return false;
	}
	if (h != Number(h) || (h = Number(h/100)) < 1 || h > 2.5) {
		alert("Nesprávne zadaná výška");
		thisform.vyska.focus();
		return false;
	}
	var w = stripBlanks(thisform.vaha.value);
	if (w == '') {
		alert("Nezadali ste váhu.");
		thisform.vaha.focus();
		return false;
	}
	if (w != Number(w) || (w = Number(w)) < 25 || w > 250) {
		alert("Nesprávne zadaná váha.");
		thisform.vaha.focus();
		return false;
	}
	
	
	
	
	
	var bmi=0;
	var v='';

	bmi=Math.round(w / (h*h)*100)/100;
	
	//document.getElementById("f").innerHTML = bmi;

	switch (1*s)
	{
	case 1: if (bmi > 0  && bmi <=20.0) {v='nedostatočná hmotnosť'; }
			if (bmi > 20.0  && bmi <=25.0) {v='normálna hmotnosť'; }			
			if (bmi > 25.0  && bmi <=30.0) {v='nadváha'; }			
			if (bmi > 30.0  && bmi <=40.0) {v='obezita'; }						
			if (bmi > 40.0 ) {v='ťažká obezita'; }									
	 	break;
	case 0:	if (bmi > 0  && bmi <=19.0) {v='nedostatočná hmotnosť'; }
			if (bmi > 19.0  && bmi <=24.0) {v='normálna hmotnosť'; }			
			if (bmi > 24.0  && bmi <=29.0) {v='nadváha'; }			
			if (bmi > 29.0  && bmi <=39.0) {v='obezita'; }						
			if (bmi > 39.0 ) {v='ťažká obezita'; }									
			break;
	}
	eraseCookie('bmi');
	createCookie('bmi',bmi,7);
	/*if (document.getElementById("bmiv")) {
	    document.getElementById("bmiv").innerHTML=bmi
	}
	*/
	if (document.getElementById("bmi")) {
	    document.getElementById("bmi").value=bmi
	}

	//document.getElementById("v").innerHTML=v;
	//document.getElementById("bmilink").innerHTML='<a href="bmi-index.html?bmi='+bmi+'">Podrobné vysvetlenie</a>';
	
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}