/*---
input fields:
	mail_job_list = select
	mail_nick = text
	mail_email = text
	mail_subject = text
	mail_count = text disabled
	mail_message = textarea
---*/
var b_running = false;
var i_iframe;

/*---
 after page is loaded
---*/
$(document).ready(function(){
	/*---
	 activate triggers
	---*/
	// bind select box with subject
	$('#mail_job_list').change(function(){
		$('#' + $(this).attr('bind_to')).val($(this).val());
	});
	// 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('');
}
/*---
 mail form is submitted
---*/
function mail_submit(){
  if (b_running) { // is busy?
		w_log('Mail server is busy. Please try again later!', 'E');
		return false;
	}
	b_running = true; // set busy
	c_log(); // clear log
	if ($('#mail_file_upload').val().length == 0){ // check for file input value
		mail_valid(); // send data to db
  }else{
	  w_log('Uploading file: ' + $('#mail_file_upload').val() + '.', 'I');
		$.ajaxFileUpload({
			upload_type: 'mail', // library, mail?
			url:'php/file_upload_handling.php', // ajax site
			fileElementId:'mail_file_upload', // id of file input tag
			dataType: 'json', // return data type
			success: function (data, status){ // on success of ajax execution
				if(data.state != 'S'){ // on error of file upload handle
				  w_log(data.error, 'E'); // display upload error
					b_running = false;
					return false;
				}else{
					mail_valid(data.file_id); // send data to db
				}
			},
			error: function (data, status, e){ // on error of ajax execution
				w_log(e, 'E'); // display ajax error
			}
		})
  }
}
/*---
 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
		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; 	
}