/*---
 after page is loaded
---*/
$(document).ready(function(){
	/*---
	 activate triggers
	---*/
	// activate form
		var options = { 
			data: ({input_id:'input_file'}), // file input id
			beforeSubmit:beforeForm, // pre-submit callback 
			success:successForm, // post-submit callback 
			error:errorForm,
			url:"../mail/mail_e.php", // override for form's 'action' attribute 
			type:'post', // 'get' or 'post', override for form's 'method' attribute 
			dataType:'json' //'json' // 'xml', 'script', or 'json' (expected server response type) 
		}; 
		$('#mail_submit').click(function() { 
			$('#mail_form').ajaxSubmit(options);
			return false;
		});
		  
	// count number of characters in message body
	$('#mail_message').keyup(function(){
		n_max_char = $(this).attr('max_char');
    if ($(this).val().length > n_max_char) {	
			$(this).val( $(this).val().substring(0, n_max_char) );
			alert('Message can have maximum ' + n_max_char + ' characters.');
		}
		$('#mail_count').val(n_max_char - $(this).val().length);
		return false;
	});	
});
/*---
 evaluate null value
---*/
 function nvl(s_str, s_nvl) {
	if (s_str == 'NULL' || s_str === null || s_str == '' || s_str == undefined) {
		return s_nvl;
	} else {
		return s_str;
	}
}
/*---
 write log
---*/
function w_log(s_log, s_log_type) {
  s_class = (s_log_type=='E') ? 'mail_log_error' : 'mail_log_success';
  $("#mail_log").attr('class', s_class);
	s_log = '<span class="' + s_class + '">' + s_log + '</span><br />';
	$("#mail_log").html($("#mail_log").html() + s_log);
}
/*---
 clear log
---*/
function c_log() {
  s_class = 'mail_log_success';
  $("#mail_log").attr('class', s_class);
	$("#mail_log").html('');
}
var b_running = false;
/*---
 before form submit
 ---*/
function beforeForm(formData, jqueryForm, options){
  if (b_running) { // is busy?
		w_log('Mail server is busy. Please try again later!', 'E');
		return false;
	}
	c_log(); // clear log
	b_running = true; // set busy
	w_log('Sending message...', 'I');
	return true;
}
/*---
 success returned from form
 ---*/
function successForm(data, textStatus, jqXHR){
	
	if (data.state == "S") { // successfully executed?
		w_log(data.message, 'I');
	} else {
		c_log();
		w_log(data.message, 'E'); // if something wrong display it
	}
	b_running = false;
}
/*---
 error returned from form
 ---*/
function errorForm(jqXHR, textStatus, errorThrown){
	w_log('Unexpected error occured! (' + textStatus + ': ' + errorThrown + ')');
	b_running = false;
}

/*---
 validate data and send via ajax
 ---*/
function mail_valid(n_file_id){
	$.ajax({
	type: "POST",
	url: "../mail/mail_e.php",
	dataType: "json", // return object type
	data: { 
		nick: $('#mail_nick').val(),
		subject: $('#mail_subject').val(),
		email: $('#mail_email').val(),
		possition: $("#mail_job_list").val(),		
		message: $("#mail_message").val(),
		file_id: nvl(n_file_id, 0)
	},
	beforeSend: function() {
		w_log('Data are being sended to mail server.', 'I');
	},
	timeout: 10000, // ms
	success: function(data){ // ajax success
		if (data.state == "S") { // successfully executed?
			w_log('Thanks for your mail.', 'I');
		} else {
			w_log(data.error, 'E'); // if something wrong display it
		}
		b_running = false;
	},
	error: function(request,error) { // ajax error
	alert(typeof request + ' ' + (request == null));
		if (error == "timeout") { // timed-out
			w_log('The request timed-out, please resubmit!', 'E');
		} else {

			w_log('ERROR: ' + error, 'E'); // other ajax error
		}
		b_running = false;
	},
	});	
	return false; 	
}
