function ContactFormCheck() {
	var ispravno = true;
	
	if(CheckEmptyField(document.getElementById("message")) == false){
    	ispravno = false;
	}
    if(CheckEmptyField(document.getElementById("subject")) == false){
    	ispravno = false;
	}
	if(CheckEmail(document.getElementById("email")) == false){
        ispravno = false;
    }
    if(CheckCapital(document.getElementById("firstname")) == false){
        ispravno = false;
    }
	
    if(ispravno == false){
        return false;
    }
    else {
    	return true;
	}
}

function ContactSendMail(basepath) {
	if(ContactFormCheck() == false) {
		alert("Svi elementi elementi formulara nisu pravilno ispunjeni!");
	}
	else {
		if($("#captcha_code").val().length == 0) {
			alert('Niste unijeli antispam (captcha) kod sa slike!');
		}
		else{
			$.ajax({
				type: "GET",
				url: basepath + "check_captcha.php",
				data: "captcha=" + $("#captcha_code").val(),
				success: function(msg){
					if (msg == "1") {
					    
					    var ime = $("#firstname").val();
						var email = $("#email").val();
						var naslov = $("#subject").val();
						var poruka = $("#message").val();
						
						$.ajax({
							type: "GET",
							url: basepath + "contact.sendmail.php",
							data: "action=kontakt_mail&ime=" + ime + "&email=" + email + "&naslov=" + naslov + "&poruka=" + poruka,
							success: function(msg){
								if(msg == "true") {
									alert("E-mail je uspješno poslan!\nHvala");
            						
            						$("#firstname").val("");
            						$("#email").val("");
            						$("#subject").val("");
            						$("#message").val("");
            						
            						window.location = "/";
								}
								else {
								    alert("Došlo je do greške kod slanja e-Maila.\nMolimo, pokušajte ponovo!\nHvala");
								}
							}
						});
					    
					    
					}
					if(msg == "0") {
						alert("Unijeli ste pogrešan antispam (captcha) kod sa slike!");
						document.getElementById('image').src = '/securimage/securimage_show.php?sid=' + Math.random();
					}
				}
			});
		}
	}
}

function CheckEmptyField(obj) {
	if(obj.value.length == 0) {
		obj.className = 'text errorfield';
		obj.focus();
        
        document.getElementById('status_' + obj.name).className = 'error';
        
        return false;
	}
	else
	{
		obj.className = 'text';
		
        document.getElementById('status_' + obj.name).className = 'error checked';
        
        return true;
	}
}

function CheckEmail(obj) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	//var filter = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/;
	if (!filter.test(obj.value)) {
		obj.className = 'text errorfield';
		obj.value = obj.value.toLowerCase();
		obj.focus();
		
		document.getElementById('status_' + obj.name).className = 'error';
		
		return false;
	}
	else {
		obj.className = 'text';
		obj.value = obj.value.toLowerCase();
		
		document.getElementById('status_' + obj.name).className = 'error checked';
	    
	    return true;
	}
}

function CheckCapital(obj) {
	if(obj.value.length == 0) {
		obj.className = 'text errorfield';
		obj.focus();
        
        return false;
	}
	else
	{
        obj.className = 'text';
        
        var prvoSlovo = obj.value.substr(0,1);
		var velikoSlovo = prvoSlovo.toUpperCase();
		
		document.getElementById(obj.id).value = velikoSlovo + obj.value.substr(1, obj.value.length-1);
        
        return true;
	}
}

function CheckCaptcha(obj) {
	if($("#captcha_code").val().length == 0) {
		obj.className = 'text errorfield';
		obj.focus();
	    
	    document.getElementById('status_' + obj.name).className = 'error';
	    
	    return false;
	}
	else{
		$.ajax({
			type: "GET",
			url: "/check_captcha.php",
			data: "captcha=" + $("#captcha_code").val(),
			success: function(msg){
				if (msg == "1") {
					obj.className = 'text';
				    document.getElementById('status_' + obj.name).className = 'error checked';
				    return true;
				}
				if (msg == "0") {
					//alert("Unijeli ste pogrešan antispam (captcha) kod sa slike!");
					
					obj.className = 'text errorfield';
				    document.getElementById('status_' + obj.name).className = 'error';
				    document.getElementById('image').src = '/securimage/securimage_show.php?sid=' + Math.random();
				    return false;
				}
			}
		});
	}
}

function CheckCaptchaEmptyField(obj) {
	if(obj.value.length == 0) {
		obj.className = 'text errorfield';
		obj.focus();
        
        document.getElementById('status_' + obj.name).className = 'error';
        
        return false;
	}
	else
	{
		obj.className = 'text';
		
        document.getElementById('status_' + obj.name).className = 'correct';
        
        return true;
	}
}
