$(document).ready(function() {
		
		// PROVERA BROWSERA I POSTAVLJANJE BACK-A I SHOW MESSAGE-A
		
		var browserName=navigator.appName;
		

		var browserName=navigator.appName;
		
		$("#back").css("background", "black");
		$("#back").css("opacity", "0.6");
		$("#back").css("width", "100%");
		$("#back").css("min-height", "100%");
		$("#back").css("top", "0");
		$("#back").css("left", "0");
		$("#back").css("z-index", "9");
		$("#back").hide();
		
		$("html").css("overflow-x", "hidden");

		var browserVer=parseInt(navigator.appVersion);
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		}


		
		if (browserName=="Microsoft Internet Explorer" && ieversion<7)	{
			//alert(browserVer);
			$("#back").css("width", "102%");
			$("#back").css("height", "150%");
			$("#back").css("position", "absolute");
			$("#showMessage").css("position", "absolute");
		} else {
			$("#back").css("position", "fixed");
			$("#showMessage").css("position", "fixed");
		}	

		$("#showMessage").css("top", "50%");
		$("#showMessage").css("left", "50%");	
		$("#showMessage").css("margin", "-150px 0 0 -200px");
		$("#showMessage").css("width", "400px");
		$("#showMessage").css("height", "300px");
		$("#showMessage").css("background", "white url(js/ajax-loader.gif) no-repeat center");
		$("#showMessage").css("z-index", "10");
		$("#showMessage").css("border", "1px solid black");
		
		$("#showMessage").hide();


		
		// FUNKCIJE
		
		function showMessageWindow() { 
			$("#showMessage").empty(); $("#showMessage").css("background", "white url(js/ajax-loader.gif) no-repeat center"); $("#showMessage").css("padding", "0"); $("#showMessage").fadeIn("slow"); $("html").css("overflow-y", "hidden"); $("#back").toggle();
		}
		
		function hideMessageWindow() {
			$("#showMessage").hide(); $("#back").hide(); $("html").css("overflow-y", "auto");
		}
		
		function ieScroll() {
			if (browserName=="Microsoft Internet Explorer" && ieversion<7)	{ window.scrollTo(0,0); }
		}
		
		// HANDLER PRIJAVE NA MAILING LISTU
		
		$("#mailingSubmit").click(function() {
			ieScroll();
			showMessageWindow();
			$.post("js/php/insertMail.php", {'mail': $('input[@name="mailing"]').val()}, function(data) {
				if(data==true) {
					$("#showMessage").load("js/php/addMailSuccessMessage.php", function() {
						$("#showMessage").css("background", "white");
						$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
					});
				} else {
					$("#showMessage").load("js/php/addMailFailureMessage.php", function() {
						$("#showMessage").css("background", "white");
						$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
					});
				}
			});			
		});
		
		// HANDLER REGISTRACIJA FORMA
		
		$("#registrationSubmit").click(function() {
			ieScroll();			
			showMessageWindow();
			
			$.post("js/php/checkEmail.php", {'email': $('input[@name="email"]').val()}, function(data) {
				if (data==true) {
					
					//alert("URADIO SAM");

					$.post("js/php/insertClient.php", {'firma': $('input[@name="firma"]').val(), 'prezime': $('input[@name="prezime"]').val(), 'ime': $('input[@name="ime"]').val(), 'grad': $('input[@name="grad"]').val(), 'ulicaibroj': $('input[@name="ulicaibroj"]').val(), 'pbroj': $('input[@name="pbroj"]').val(), 'drzava': $('input[@name="drzava"]').val(), 'pib': $('input[@name="pib"]').val(), 'firma': $('input[@name="firma"]').val(), 'firma': $('input[@name="firma"]').val(), 'firma': $('input[@name="firma"]').val(), 'firma': $('input[@name="firma"]').val(), 'firma': $('input[@name="firma"]').val(), 'firma': $('input[@name="firma"]').val(), 'firma': $('input[@name="firma"]').val(),
														 'racun': $('input[@name="racun"]').val(), 'maticnibroj': $('input[@name="maticnibroj"]').val(), 'potvrda': $('input[@name="potvrda"]').val(), 'telefon1': $('input[@name="telefon1"]').val(), 'telefon2': $('input[@name="telefon2"]').val(), 'fax': $('input[@name="fax"]').val(), 'email': $('input[@name="email"]').val(), 'website': $('input[@name="website"]').val(), 'password': $('input[@name="password"]').val()}, function(data) {
						if(data==true) {
							$("#showMessage").load("js/php/addClientSuccessMessage.php", function() {
								$("#showMessage").css("background", "white");
								$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
							});
						} else {
							$("#showMessage").load("js/php/addClientFailureMessage.php", function() {
								$("#showMessage").css("background", "white");
								$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
							});
						}
					});
				} else {
					$("#showMessage").load("js/php/addClientFormFailureMessage.php", {'poruka': data}, function() {
						$("#showMessage").css("background", "white");
						$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
					});
				}
			});
		});
		
		// PROVERA REGISTRACIJE HANDLER
		
		$("#registrationEndSubmit").click(function() {
			ieScroll();
			showMessageWindow();
			$.post("js/php/registrationClientCheck.php", {'username': $('input[@name="username"]').val(), 'tPass': $('input[@name="tPassword"]').val(), 'nPass': $('input[@name="nPassword"]').val(), 'nPassAgain': $('input[@name="nPasswordAgain"]').val()},
				function(data)	{
					if (data) {
						//alert(data);
						$("#showMessage").load("js/php/successfulRegistrationMessage.php", function() {
							$("#showMessage").css("background", "white");
							$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
						});
					} else {
						alert(data);
					}
					// MOGUCNOSTI : 1) neispravno korisnicko ime/privremena lozinka 2) razlikuju se nova lozinka i potvrda nove lozinke				
				});
		});
		
		// LOGIN HANDLER
		
		$("#loginSubmit").click(function() {
			ieScroll();
			showMessageWindow();
			var user = $('input[@name="loginUsername"]').val();
			var pass = $('input[@name="loginPassword"]').val();

			if ((user != "") && (pass != "")) {
				//$("#login").empty();
				$.post("js/php/loginCheck.php", {'username': user, 'password': pass}, function(data) {
					if (data=="OK") {
						// sucessful login
						
						$("#login").load("js/php/loginSuccess.php",{'username': user, 'password': pass}, function() {
							hideMessageWindow();
							$("#logoutSubmit").click(function() {
								//$("#login").empty();
								showMessageWindow();
								$("#login").load("js/php/logout.php", function() {hideMessageWindow();});
							});
						});
						
					} else if (data=="NOT OK") {
						// login failed
						$("#showMessage").load("js/php/loginFailed.php", function() {
							$("#showMessage").css("background", "white");
							$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
						});
					} else {
						// already logged
					}
				});
			} else {hideMessageWindow();}
		});
		
		// HANLDER CENOVNIKA
		
		$("#cenovnik").click(function () 
		{
			/*
			 * ne nudimo vise download cenovnika
			 * 
			$.post("js/php/checkCenovnik.php", function(data) {
				if(data=="OK") {
					ieScroll();
					showMessageWindow();
					$("#showMessage").load("js/php/cenovnikSuccess.php", function() {
						$("#showMessage").css("background", "white");
						$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
					});
				} else {
					*/
					ieScroll();
					showMessageWindow();
					$("#showMessage").load("js/php/cenovnikFailed.php", function() {
						$("#showMessage").css("background", "white");
						$(".closeAjaxWindow").click(function() { hideMessageWindow(); });
					/*
					});
				}
					*/
			});
		});
});


