function MailForm(script)
{
	this._script = script;
	this._form = null;
	this._params = null;
	this._session = '';
}

MailForm.prototype = {
	send: function (form, subject, template) {
		this._form = document[form];
		this._successed = false;
		
		if (!this._script) {
			alert('送信スクリプトが指定されていません！');
			return;
			
		} else if (!this._form) {
			alert('送信フォームが指定されていません！');
			return;
			
		} else if (!template) {
			template = 'default';
		}
		
		this._params = 'subject=' + encodeURI(subject);
		this._params += '&template=' + encodeURI(template);
		this._submit('mode=1', '_send');
	},
	
	_send: function (data) {
		data = eval("(" + data + ")");
		
		if (data.result < 1) {
			alert(data.message);
			return;
		}
		
		var names = this._validate($('input, textarea', this._form));
		var formValue;
		var addedCheckboxes = new Array();
		var _self = this;
		
		for (var i = 0; names.length > i; i++) {
			switch ($(this._form[names[i]]).attr('type')) {
				case 'radio':
					formValue = $("input[@name=" + names[i] + "]:checked").val();
					
					if (formValue != undefined) {
						this._params += '&' + encodeURI(names[i]) + '=' + encodeURI(formValue);
					} else {
						this._params += '&' + encodeURI(names[i]) + '=';
					}
					
					break;
				case 'checkbox':
					if (!addedCheckboxes[names[i]]) {
						formValue = $('input[@name^="' + names[i] + '"]:checked');
						
						if (formValue.size() > 0) {
							formValue.each(function() {
								_self._params += '&' + encodeURI(names[i]) + '=' + encodeURI($(this).val());
							});
						}
					}
					addedCheckboxes[names[i]] = 1;
					break;
				default:
					this._params += '&' + encodeURI(names[i]) + '=' + encodeURI(this._form[names[i]].value);
			}
		}
		
		this._submit(this._params + '&mode=2&session_key=' + data.session_key, '_confirm');
	},
	
	_validate: function (elements) {
		var names = new Array();
		
		for (var i = 0; elements.length > i; i++) {
			if (elements[i].name.search(/^s[a-z]?_(.*)$/i) != -1) {
				names.push(elements[i].name);
			}
		}
		
		return names;
	},
	
	_confirm: function (data) {
		data = eval("(" + data + ")");
		
		if (data.result < 1) {
			alert(data.message);
			return;
		}
		
		if (confirm(data.confirm)) {
			this._submit(this._params + '&mode=3&session_key=' + data.session_key, '_success');
		}
	},
	
	_success: function (data) {
		data = eval("(" + data + ")");
		
		if (data.result < 1) {
			alert(data.message);
			return;
		}
		
		alert('メール送信が完了しました。');
		
		for (var i = 0; i < this._form.length; i++) {
			if (this._form[i].name.search(/^s[a-z]?_(.*)$/i) != -1) {
				switch ($(this._form[i]).attr('type')) {
					case 'radio':
					case 'checkbox':
						$("input[@name=" + this._form[i].name + "]").val([ 'undefined9999' ]);
						break;
					default:
						this._form[i].value = '';
				}
			}
		}
	},
	
	_error: function (data) {
		alert('サーバーとの通信に失敗しました！\nお手数ですが、管理者に連絡して下さい。');
	},
	
	_submit: function (params, callback) {
		var self = this;
		
		$.ajax({
			type: "POST",
			url: this._script,
			data: params,
			timeout: 3000,
			success: function (data, status) {self[callback](data, status);},
			error: function (data, status) {self['_error'](data, status);}
		});
	}
}

function sendmail(script, form, subject, template) {
	var mailObj = new MailForm(script);
	mailObj.send(form, subject, template);
}