function subLevel(val,id) {
	if(val) {
		$("."+id).show();
	}
	else {
		$("."+id).hide();
	}
	return true;
}

function showSubForm(id1,id2) {
	$("#"+id2).toggle();
	//alert ($("#"+id1).css("backgroundImage").indexOf);
	if($("#"+id1).css("backgroundImage").indexOf("kalendar_down.gif") > 0) {$("#"+id1).css({background:"url(/images/kalendar_right.gif) no-repeat top right"});}
	else {$("#"+id1).css({background:"url(/images/kalendar_down.gif) no-repeat top right"});}
}

function selectedValue (dropdown) {
	var i;
	for (i = 0; i < dropdown.length; i++) {
		if (dropdown[i].selected) {
			return dropdown[i].value;
		}
	}
}

function isChecked (checkboxes) {
	var i;
	for (i = 0; i < checkboxes.length; i++) {
		if (checkboxes[i].checked) {
			return true;
		}
	}
	return false;
}

function disableEnableElement (sender, disableValue, target) {
	if (sender.value === disableValue) {
		target.disabled = true;
	}
	else {
		target.disabled = false;
	}
}

function disableEnableElements (sender, disableValue, target1, target2) {
	if (sender.value === disableValue) {
		target1.disabled = true;
		target2.disabled = true;
	}
	else {
		target1.disabled = false;
		target2.disabled = false;
	}
}


//Validacija forme

var step2Started = false;
var step3Started = false;

/*$('input#st1, p#btn2').click (function () {
	step2Started = true;
});

$('input#st2, p#btn3').click (function () {
	step3Started = true;
});*/

function checkForm (forma) {
	
	//Korak 1
	if (document.forma.email.value=== "" || document.forma.email.value.indexOf("@")=== -1 || document.forma.email.value.indexOf(".")=== -1) {
		alert ("Molimo upišite ispravnu email adresu!");
		return false;
	}
	
	if (document.forma.uid.value === "" || document.forma.uid.value === " " || document.forma.uid.value === "  ") {
		alert ("Molimo upišite korisničko ime!");
		return false;
	}
	
	if (document.forma.pass.value === "") {
		alert ("Molimo upišite lozinku!");
		return false;
	}
	
	if (document.forma.pass.value !== document.forma.pass_ponovo.value) {
		alert ("Lozinke ne odgovaraju!");
		return false;
	}
	
	podrucja = document.forma.elements["podrucjeinteresa[]"];
	if (isChecked (podrucja) === false && document.forma.podrucjetxt.value === "") {
		alert ("Molimo označite područje interesa!");
		return false;
	}
	
	if (document.forma.gdjelokalno.checked === false 
		&& document.forma.gdjehrvatska.checked === false
		&& document.forma.gdjemedjunarodnokratkorocno.checked === false
		&& document.forma.gdjemedjunarodnodugorocno.checked === false) {
			alert ("Molimo označite gdje želite volontirati!");
			return false;
	}
	
	if (selectedValue (document.forma.kojiprojekti) === "0") {
		alert ("Molimo označite koji projekti vas zanimaju!");
		return false;
	}
	
	if (selectedValue (document.forma.primatiglasilo) === "2") {
		alert ("Molimo označite želite li primati glasilo VCZ-a!");
		return false;
	}
	
	if (selectedValue (document.forma.primatiinfo) === "2") {
		alert ("Molimo označite želite li primati informacije o projektima!");
		return false;
	}
	
	//Korak 2
	if (step2Started || step3Started) { 
		if (document.forma.ime.value === "") {
			alert ("Molimo upišite ime!");
			return false;
		}
		
		if (document.forma.prezime.value === "") {
			alert ("Molimo upišite prezime!");
			return false;
		}
		
		if (document.forma.telefon.value === "") {
			alert ("Molimo upišite broj telefona!");
			return false;
		}
		
		if (document.forma.kolikodnevno.checked === false 
			&& document.forma.kolikotjedno.checked === false
			&& document.forma.kolikomjesecno.checked === false
			&& document.forma.kolikogodisnje.checked === false) {
				alert ("Molimo označite koliko češto želite volontirati!");
				return false;
		}
		
		vjestine = document.forma.elements["vjestine[]"];
		if (isChecked (vjestine) === false && document.forma.vjestinetxt.value === "") {
			alert ("Molimo označite barem jednu vještinu!");
			return false;
		}
		
		if (selectedValue (document.forma.specificnapotpora) == 1 && document.forma.specificnapotporatxt.value === "") {
			alert ("Molimo upišite kakvu specifičnu potporu nudite!");
			return false;
		}
		
		if (selectedValue (document.forma.zelitipomoci) === "2") {
			alert ("Molimo označite želite li pomoći u radu VCZ-a!");
			return false;
		}
		
		reference = document.forma.elements["reference[]"];
		if (isChecked (reference) === false && document.forma.referencatxt.value === "") {
			alert ("Molimo označite kako ste doznali za VCZ!");
			return false;
		}
	}
	
	//Korak3
	if (step3Started) { 
		if (document.forma.adresa.value === "" || document.forma.grad.value === "" || document.forma.postanskibroj.value === "") {
			alert ("Molimo upišite potpunu adresu!");
			return false;
		}
		
		if (document.forma.dtelefon.value === "") {
			alert ("Molimo upišite broj telefona!");
			return false;
		}
		
		if (document.forma.drzavljanstvo.value === "") {
			alert ("Molimo upišite državljanstvo!");
			return false;
		}
		
		if (selectedValue (document.forma.birth_day) == 0 || selectedValue(document.forma.birth_month) == 0 || selectedValue(document.forma.birth_year) == 0) {
			alert ("Molimo upišite ispravan datum rođenja!");
			return false;
		}
	
		
		if (selectedValue (document.forma.spol) == 0) {
			alert ("Molimo označite spol!");
			return false;
		}
		
		if (selectedValue (document.forma.trenutnistatus) == 0) {
			alert ("Molimo označite trenutni status!");
			return false;
		}
		
		if (selectedValue (document.forma.strucnasprema) == 0) {
			alert ("Molimo označite stručnu spremu!");
			return false;
		}
		
		if (document.forma.zanimanje.value === "") {
			alert ("Molimo upišite zanimanje!");
			return false;
		}
		
		if (document.forma.strucnasprematxt.value === "") {
			alert ("Molimo upišite obrazovanje!");
			return false;
		}
		

		
		if (document.forma.kontaktosoba.value === "" || document.forma.kontakttelefon.value === "") {
			//alert("radi" + selectedValue(document.forma.birth_year) + " " + selectedValue(document.forma.birth_month) + " " + selectedValue(document.forma.birth_day));
			var today = new Date();
			
			if (parseInt(selectedValue(document.forma.birth_year), 10) + 18 <= today.getYear() + 1900) {
				if (parseInt(selectedValue(document.forma.birth_year), 10) + 18 === today.getYear() + 1900) {
					if (parseInt(selectedValue (document.forma.birth_month), 10) <= today.getMonth() + 1 ) {
						if (parseInt(selectedValue (document.forma.birth_month), 10) === today.getMonth() + 1) {
							if (parseInt(selectedValue (document.forma.birth_day), 10) > today.getDay() ) {
								alert ("Podaci o kontakt osobi su obvezni za maloljetne osobe!");
								return false;
							}
						}
					}
					else {
						alert ("Podaci o kontakt osobi su obvezni za maloljetne osobe!");
						return false;
					}
				}
			}
			else {
				alert ("Podaci o kontakt osobi su obvezni za maloljetne osobe!");
				return false;
			}
		}
		
		/*if (selectedValue (document.forma.iskustvo) === 2) {
			alert ("Molimo označite imate li iskustva u volontiranju!");
			return false;
		}
		
		if (selectedValue (document.forma.iskustvo) === 1 && selectedValue (document.forma.iskustvokoliko) === 0) {
			alert ("Molimo označite koliko sati volonterskog iskustva imate!");
			return false;
		}
		
		if (selectedValue (document.forma.iskustvo) === 1 && document.forma.iskustvotxt.value === "") {
			alert ("Molimo opišite iskustvo u volontiranju!");
			return false;
		}
		
		if (selectedValue (document.forma.vczprojekti) === 2) {
			alert ("Molimo označite jeste li sudjelovali na VCZ projektima!");
			return false;
		}
		
		if (selectedValue (document.forma.vczprojekti) === 1 && document.forma.vczprojektitxt.value === "") {
			alert ("Molimo upišite na kakvim VCZ projektima ste već sudjelovali!");
			return false;
		}
		
		if (selectedValue (document.forma.vczedukacija) === 2) {
			alert ("Molimo označite jeste li sudjelovali na VCZ edukaciji za volontere!");
			return false;
		}
		
		if (selectedValue (document.forma.vczedukacija) === 1 && document.forma.vczedukacijakoja.value === 0) {
			alert ("Molimo označite koju ste VCZ edukaciju prošli!");
			return false;
		}
		
		if (selectedValue (document.forma.drugaedukacija) === 2) {
			alert ("Molimo označite jeste li sudjelovali na nekoj drugoj edukaciji!");
			return false;
		}
		
		if (selectedValue (document.forma.drugaedukacija) === 1 && document.forma.drugaedukacijatxt.value === "") {
			alert ("Molimo upišite kakvu ste drugu edukaciju prošli!");
			return false;
		}*/
	}
	
	return true;
}

$(document).ready(function(){ //Wait for the page to load.
	//Phone sync
	$('input[name=dtelefon]').bind('change', function () {
		if (document.forma.dtelefon.value !== "") {
			document.forma.telefon.value = document.forma.dtelefon.value;
		}
	});
	$('input[name=telefon]').bind('change', function () {
		if (step3Started) {
			document.forma.dtelefon.value = document.forma.telefon.value;
		}
	});
});
