// JavaScript Document
$(document).ready(function() {
 
	// email field focus
	//$('#name').focus();
	
   
	// anchor trigger submit
   	$(function() {
  		$("#ahrefsubmit").click(function() {
    		$("#form1").submit();
			return false;
		});
	
		// use this to reset several forms at once
		$("#ahrefreset").click(function() {
			$("#form1").each(function() {
				this.reset();
			});
		})
	}); 

	// prepare the form when the DOM is ready 
	var options = { 
		beforeSubmit:   validate, 
		success:       showResponse  
	}; 
 
    // bind to the form's submit event 
    $("#form1").submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
		
        // always return false to prevent standard browser submit and page navigation 	
        return false; 
    }); 	
	
	$("#form1").ajaxSuccess(function(evt, request, settings){	
	});
	
});

//pre post validate form values
function validate(formData, jqForm, options) { 
    // jqForm is a jQuery object which wraps the form DOM element 
	 var form = jqForm[0]; 
	 var msgErrorText = "";
	 var mail = "";
	/*
	if validation is NOT OK
	*/	
    if ((!form.name.value) || (/^[ \t]+$/.test(form.name.value))) { 
		msgErrorText = "Unesite ime i prezime <br />";
    } 
	//email
	 if (!form.emailform.value) { 
	 	msgErrorText += "Unesite vaš email <br />";
		}
		else {
			mail = form.emailform.value;
			if (!(/^[A-Za-z0-9._%+-]+@(?:[A-Za-z0-9-]+\.)+[A-Za-z]{2,4}$/.test(mail))){
				msgErrorText += "Email addresa koju ste uneli nije ispravna. <br />";
			}
		}
	//description
	 if ((!form.description.value) || (/^[ \t]+$/.test(form.description.value))) {
	 	msgErrorText += "Unesite sadržaj poruke <br />";
    } 
	//uploadfile is not *
	
	//print erroe msg
	if(msgErrorText.length!=0){ 
		//FBug debuger:: 
		//console.log(msgErrorText)
		alert_lightbox("<b>Molimo Vas unesite sledeće podatke:</b><br />" + msgErrorText);
		return false;
	}
	/*
	if validation is OK
	-----------------------------------------------------
	 if visior choosed to upload file for mail attachment 
	 1 - we need to upload it first on server
	 2 - & then send mail with file attachment
	 
	*/
/*	 if (form.uploadfile.value) { 
	 	//FBug debuger:: 
		console.log(form.uploadfile.value)
	 } */
	
	//alert("validation is OK..");
	return true; 
}
 
// post-submit callback 
function showResponse(responseText, statusText)  { 
	 //alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
       // '\n\nThe output div should have already been updated with the responseText.'); 
	 $('#form1').resetForm();
	 alert_lightbox("Hvala na Vašem e-mail-u. Odgovorićemo ubrzo. ");
}

//prompt Fn
function alert_lightbox(msg){
	$.prompt(msg,
	{
		buttons:{'OK':true}, 
		show:'fadeIn',
		opacity: 0.4
	});
}


        