// JavaScript Document
$(document).ready(function() {
 
	// email field focus
	//$('#name').focus();
   
	// anchor trigger submit
   	$(function() {
  		$("#ahrefsubmit").click(function() {
    		$("#form1").submit();
    	    return false;
  	    });
		$("#ahrefnews").click(function() {
    		$("#form4").submit();
    	    return false;
  	    });
			$("#ahrefcheck").click(function() {
    		$("#form2").submit();
    	    return false;
  	    });
				$("#ahrefrac").click(function() {
    		$("#form3").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 = "";
	/*
	if validation is NOT OK
	*/	
    if (!form.from.value) { 
		msgErrorText += "Input From: field <br />";
    } 
	//email
	if (!form.to.value) { 
	 	msgErrorText += "Input To: field <br />";
    } 
	//description
	if (!form.depdate.value) { 
	 	msgErrorText += "Input Departure date: field<br />";
    } 
	if (!form.retdate.value) { 
		msgErrorText += "Input Return date: field <br />";
    } 
	//email
	if (!form.pasname.value) { 
	 	msgErrorText += "Input Passanger name: field <br />";
    } 
	//description
	if (!form.name.value) { 
	 	msgErrorText += "Input Your name: field <br />";
    }
	if (!form.phone.value) { 
		msgErrorText += "Input your Telephone number <br />";
    }
	else {
	  var filter = /^(\s*[\+]?\s*[0-9\(\)\-\.\/\s]+)\s*$/i;
	  if (!filter.test(form.phone.value)) {
	    msgErrorText += "Phone number you provided is not valid<br />";
	  }
	}
	//email
	if (!form.email.value) { 
	 	msgErrorText += "Input your E-Mail <br />";
    }
	else {
	  var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
      if (!filter.test(form.email.value)) {
	    msgErrorText += "E-Mail you provided is not valid <br />";
	  }
	  else if(form.email.value != form.email2.value) {
	    msgErrorText += "Two E-Mails do not match <br />";
	  }
	}
	
	//uploadfile is not *
	
	//print erroe msg
	if(msgErrorText.length!=0){ 
		//FBug debuger:: 
		//console.log(msgErrorText)
		alert_lightbox("<b>Please insert next form data:</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)  {
	document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random(); // reloads captcha image
	if(responseText == "Mail sent") {
	  $('#form1').resetForm();
	  alert_lightbox("Thank You. You should receive a response in the next 12 hours! ");
	}
	else {
	  alert_lightbox(responseText);
	}
}

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

        
