var CONTACTME_MUSTMARK = '<span class="COMMENTME_MUSTMARK">*</span>';
var CONTACTME_SETTINGS = null;

function contactmeCreateXMLHttpObject(){
	try {
		return new XMLHttpRequest();
	} catch (e1) {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e2) {
			try {
				return new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e3) {
				return null;
			}
		}
	}
}

function contactmeCheckForm(){
	var CONTACTME_ERROR = false;
	for (i in CONTACTME_SETTINGS){
		if (CONTACTME_SETTINGS[i].must){
			if (document.getElementById(CONTACTME_SETTINGS[i].name).value == null || document.getElementById(CONTACTME_SETTINGS[i].name).value == ""){
				CONTACTME_ERROR = true;
			}
		}
	}
	
	if (!CONTACTME_ERROR){
		document.getElementById("CONTACTME_SUBMITBUTTON").disabled = false;
	} else {
		document.getElementById("CONTACTME_SUBMITBUTTON").disabled = true;
	}
}

function contactmeResetForm(){
	for (i in CONTACTME_SETTINGS){
		document.getElementById(CONTACTME_SETTINGS[i].name).value = '';
	}
	contactmeCheckForm();
}

function contactmeSendMessage(){
	if (!confirm('送信します。よろしいですか？'))return false;

	document.getElementById("CONTACTME_SUBMITBUTTON").disabled = true;

	var contactmehttpobjSend = contactmeCreateXMLHttpObject();

	contactmehttpobjSend.open('POST' ,'./contactme/contactme.cgi' ,true );
	contactmehttpobjSend.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	contactmehttpobjSend.onreadystatechange = function(){
		if (contactmehttpobjSend.readyState!=4){
			return;
		}
		if (contactmehttpobjSend.responseText.indexOf('OK') == 0){
			document.getElementById('CONTACTME-MESSAGEAREA').innerHTML = '送信が完了しました。<br>お問合せいただきありがとうございました。';
			contactmeResetForm();
			window.scrollTo(0,0);
		} else {
			document.getElementById('CONTACTME-MESSAGEAREA').innerHTML = 'エラーが発生しました。<br>しばらくしてから再度送信してください。';
			document.getElementById('CONTACTME-MESSAGEAREA').innerHTML += contactmehttpobjSend.responseText;
		}
	};

	contactmepostdata = '';
	for (i in CONTACTME_SETTINGS){
		if (document.getElementById(CONTACTME_SETTINGS[i].name).value != null && document.getElementById(CONTACTME_SETTINGS[i].name).value != ""){
			if (contactmepostdata != '') contactmepostdata += '&'
			contactmepostdata += CONTACTME_SETTINGS[i].name + '=' + document.getElementById(CONTACTME_SETTINGS[i].name).value;
		}
	}

	contactmehttpobjSend.send(contactmepostdata);

	return false;
}


var contactmereqtime = new Date().getTime();
var contactmehttpobj = contactmeCreateXMLHttpObject();

contactmehttpobj.open('GET' ,'./contactme/contactme.json?reqtime=' + contactmereqtime ,true );
contactmehttpobj.onreadystatechange = function(){
	if (contactmehttpobj.readyState!=4){
		return;
	}
	
	res = contactmehttpobj.responseText;
	CONTACTME_SETTINGS = eval(res);
	
	var contactme_form = '<form name="CONTACTME_FORM" id="CONTACTME_FORM">';
	contactme_form = '<div id="CONTACTME-MESSAGEAREA"></div>';
	contactme_form += '<span class="CONTACTME_ATTENTION">' + CONTACTME_MUSTMARK + 'は入力必須です。</span>';
	contactme_form += '<table  border="0" cellspacing="0" cellpadding="0" class="CONTACTME-FORMTABLE">';
	
	for (i in CONTACTME_SETTINGS){
		contactme_form += '<tr>';
		contactme_form += '<th>';
		contactme_form += CONTACTME_SETTINGS[i].label
		if (CONTACTME_SETTINGS[i].must){
			contactme_form += CONTACTME_MUSTMARK;
		}
		contactme_form += '</th>';
		contactme_form += '<td>';
		
		if (CONTACTME_SETTINGS[i].type == "Text"){
			contactme_form += '<textarea id="' + CONTACTME_SETTINGS[i].name + '" cols="' + CONTACTME_SETTINGS[i].size[0] + '" rows="' + CONTACTME_SETTINGS[i].size[1] + '" onchange="contactmeCheckForm();"></textarea>';
		} else if(CONTACTME_SETTINGS[i].type == "Select"){
			contactme_form += '<select id="' + CONTACTME_SETTINGS[i].name + '" onchange="contactmeCheckForm();">';
			contactme_form += '<option>'
			for (j in CONTACTME_SETTINGS[i].value){
				contactme_form += '<option value="' + j + '">' + CONTACTME_SETTINGS[i].value[j];
			}
			contactme_form += '</select>'
		} else {
			contactme_form += '<input type="text" id="' + CONTACTME_SETTINGS[i].name + '" size="' + CONTACTME_SETTINGS[i].size + '" onchange="contactmeCheckForm();">'
		}
		contactme_form += '</td>'
		contactme_form    += '</tr>';
	}
	
	contactme_form += '<tr><th colspan="2"><input type="button" id="CONTACTME_SUBMITBUTTON" onclick="contactmeSendMessage();" value="送信" disabled></th></tr>';
	contactme_form += '</table>';
	contactme_form += '</table>';
	contactme_form += '</form>';
	
	document.all.CONTACTME_FORM.innerHTML = contactme_form;
}

document.open();
document.write('<div id="CONTACTME_FORM" class="CONTACTME-FORM">入力フォーム構成中．．．</div>');
document.close();

contactmehttpobj.send('');