function resetFields() {
	document.getElementById('contact_submit_button').className = 'contact_submit_button'; //Show submit button again
	if (document.getElementById('contact_progressbar')) {
		document.getElementById('contact_progressbar').parentNode.removeChild(document.getElementById('contact_progressbar'));
	};
	if (document.getElementById('contact_thanksmessage')) {
		document.getElementById('contact_thanksmessage').parentNode.removeChild(document.getElementById('contact_thanksmessage'));
	};
	document.getElementById('holler_address').value = ''; //Add some initialization here!
	document.getElementById('holler_message').value = '';
	document.getElementById('holler_address').disabled = false;
	document.getElementById('holler_message').disabled = false;
	if (!is_iphone()) {
		$('#holler_address').fadeTo("fast", 1); //This needs JQuery
		$('#holler_message').fadeTo("fast", 1); //This needs JQuery
	}
}

function delayResetFields(){
	if(is_iphone()){
		window.scrollTo(0, 0); //Dont use the jQuery scroll plugin.
	}
	setTimeout('resetFields()', 1000); //1秒後にクリア
}


function resetThanks() {
	if(!is_iphone()){ // Normal mode
		headerDisclosure(document.getElementById('contact'), 'slidingcontact', delayResetFields()); //CLOSE SLIDING CONTACT
	}else{ // iPhone mode
		headerDisclosure(document.getElementById('i-contact'), 'i-slidingcontact', delayResetFields()); //CLOSE SLIDING CONTACT
	}

}

function showThanks() {

	pageTracker._trackPageview('/header_contact/thanks_message'); //Google Analytics Trigger

	if (document.getElementById('contact_progressbar')) {
		document.getElementById('contact_progressbar').parentNode.removeChild(document.getElementById('contact_progressbar'));
	};

	if (!is_iphone()) {
		$('#holler_address').fadeTo("slow", 0.5); //This needs JQuery
		$('#holler_message').fadeTo("slow", 0.5); //This needs JQuery
	}

	var thanksMessage = document.createElement('span');
	thanksMessage.id='contact_thanksmessage';
	thanksMessage.innerHTML='ありがとうございました。';
	document.getElementById('submitblock').appendChild(thanksMessage);

}

function showProgress() {
	//document.getElementById('holler_address').disabled = true;
	document.getElementById('holler_message').disabled = true;
	document.getElementById('contact_submit_button').className = 'contact_submit_button_hidden'; //Hide submit button
	var progressBar = document.createElement('span');
	progressBar.id='contact_progressbar';
	progressBar.innerHTML='<img src="/assets/images/progressbar-normal.gif" />';
	document.getElementById('submitblock').appendChild(progressBar);

}


function holler_out() {

	showProgress();

	var script_uri = "/soswp/wp-content/themes/sociomedia/holler.php";

	var holler_response = document.getElementById('holler_response');
	var holler_form = document.getElementById('holler');
	var to = holler_form.holler_to.value;
	var name = holler_form.holler_name.value;
	var address = holler_form.holler_address.value;
	var subject = holler_form.holler_subject.value;
	var message = holler_form.holler_message.value;
	var cc = holler_form.holler_cc.value;

	var holler = new sack(script_uri);
	if (holler.failed) {
		holler_response.innerHTML = "XMLHTTP Error: Contact Form Disabled";
		//holler_form.style.display = "none";
		resetThanks();
		return false;
	}

	holler.myResponseElement = holler_response;

	holler.method = 'POST';
	holler.onCompletion = function() {
	//window.alert(holler.response.substring(1)); //DEBUGGING
		holler.myResponseElement.innerHTML = holler.response.substring(1);
		if (holler.response.charAt(0) == '+') {
			showThanks();
			setTimeout("resetThanks()", 2000);
		} else {
			window.alert('送信できませんでした。');
			resetThanks();
		}
	}

	holler.runAJAX('holler=true&holler_to=' + to + '&holler_name=' + name + '&holler_subject=' + subject + '&holler_address=' + address + '&holler_message=' + message + '&holler_cc=' + cc);

}




function validateContact() {



	//Clear Existing Cautions
	if(document.getElementById('alertIcon0')) {
		document.getElementById('adderssblock').removeChild(document.getElementById('alertIcon0'));
		document.getElementById('holler_address').className = 'holler_address';
		document.getElementById('holler_address').style.backgroundColor = ""; //This is needed to animate the bg color again.
		//$("#holler_address").css({backgroundColor: ""}); //This is needed to animate the bg color again.
	}
	if(document.getElementById('alertIcon1')) {
		document.getElementById('messageblock').removeChild(document.getElementById('alertIcon1'));
		document.getElementById('holler_message').className = 'holler_message';
		document.getElementById('holler_message').style.backgroundColor = ""; //This is needed to animate the bg color again.
		//$("#holler_message").css({backgroundColor: ""}); //This is needed to animate the bg color again.
	}

	//Array of Input Fields
	var inputArray = new Array(
		document.getElementById('holler_address'), //Email address field
		document.getElementById('holler_message') //Message field
	);

	var okToSubmit = true; //Flag if OK to submit

	if(!inputArray[0].value.match(/^\S+@\S+\.\S+$/)) { //Check Email Address
	
		var alertIcon0 = document.createElement('span');
		alertIcon0.id = 'alertIcon0';
		alertIcon0.innerHTML = '<img src=\"/assets/images/icon_alert_16\" alt=\"注意マーク\" title=\"記入内容に不備があるようです。\" \/>';
		document.getElementById('adderssblock').appendChild(alertIcon0);

		document.getElementById('holler_address').className = 'holler_address_error';

		if (!is_iphone()) {
			$("#holler_address").animate({ 
				backgroundColor: "#ffffff"
				//Add other actions here with ',' separator.
			}, 1000 );
		} else {
			//document.getElementById('holler_address').className = 'holler_address_error2';
		}

		okToSubmit = false; //Flag down
	} else { 
		//Put some action here
	}

	if(inputArray[1].value == '') { //Check Message

		var alertIcon1 = document.createElement('span');
		alertIcon1.id = 'alertIcon1';
		alertIcon1.innerHTML = '<img src=\"/assets/images/icon_alert_16\" alt=\"注意マーク\" title=\"記入内容に不備があるようです。\" \/>';
		document.getElementById('messageblock').appendChild(alertIcon1);

		document.getElementById('holler_message').className = 'holler_message_error';

		if (!is_iphone()) {
			$("#holler_message").animate({ 
				backgroundColor: "#ffffff"
				//Add other actions here with ',' separator.
			}, 1000 );
		} else {
			//document.getElementById('holler_message').className = 'holler_message_error2';
		}

		okToSubmit = false; //Flag down
	} else {
		//Put some action here
	}

	if (okToSubmit == true) {
		holler_out(); //Main Submit Function
	} else {
		//Put some action here
	}

}


function generateContactForm() {
	var theLine = '';
	theLine += '<div id="slidingcontact-inner">';
	theLine += '<form id="holler" action="" onsubmit="return false; ">';
	theLine += '<h2 id="holler_title">問い合わせフォーム</h2>';
	theLine += '<p id="contactlead">ソシオメディアへの各種お問い合わせ、ご依頼、その他のご意見などを承ります。（Tel. 03-5206-6787｜Fax. 03-5206-6823｜<a href="/539">地図</a>）</p>'
	theLine += '<p><input id="holler_to" type="hidden" value="Sociomedia" /></p>';
	theLine += '<input type="hidden" id="holler_name" value="ユーザー" />';
	theLine += '<p id="adderssblock"><span><input class="holler_address" id="holler_address" value="Eメールアドレス" style="color: #cccccc;" onblur="if(this.value==\'\'){this.style.color=\'#cccccc\'; this.value=\'Eメールアドレス\';}" onfocus="if(this.value==\'Eメールアドレス\'){this.style.color=\'#000000\'; this.value = \'\';}" /></span><input type="hidden"  id="holler_subject" value="ソシオメディアへのコンタクト" /></p>';
	theLine += '<p id="messageblock"><span><textarea class="holler_message" id="holler_message"></textarea></span></p>';
	theLine += '<input id="holler_cc" type="hidden" value="false" />';
	if(!is_iphone()){ //For normal
		theLine += '<p id="submitblock"><input class="contact_submit_button" id="contact_submit_button" type="submit" onclick="validateContact();" value="送信" /></p>';
	}else{ //For iPhone
		theLine += '<p id="submitblock"><input class="contact_submit_button" id="contact_submit_button" type="submit" onclick="validateContact();" value="問い合わせ送信" /></p>';
	}
	theLine += '</form>';
	theLine += '<div id="holler_response"></div>';
	theLine += '</div><!-- end of div.slidingcontact-inner -->';
	theLine += '</div><!-- end of div.slidingcontact-inner -->';
	theLine += '<div class="dropshadow dropshadow-down">&nbsp;</div><div class="dropshadow dropshadow-up">&nbsp;</div>';

	var theSlidingContact = document.createElement('div');
	if(!is_iphone()){ // Normal mode
		theSlidingContact.id = 'slidingcontact';
		theSlidingContact.className='slidingcontact';
	}else{
		theSlidingContact.id = 'i-slidingcontact';
		theSlidingContact.className='i-slidingcontact_closed';
	}
	theSlidingContact.innerHTML = theLine;

	document.getElementById('pane_wrapper').insertBefore(theSlidingContact, document.getElementById('pane_contexttab'));

}


generateContactForm();

