// initialize
var fb_form = new fbForm();
function initialize(){
	is_safari = navigator.userAgent.indexOf("Safari") != -1 ? true : false;
	if( !is_safari || navigator.userAgent.split("/")[3].match(/^[3-9]\./) ){
		if( fb_form.confirm = document.getElementById('fb-confirm') ){
			fb_form.error   = document.getElementById('fb-fill-error');
			if( document.getElementById( 'fb-modified' ) ){
				for( var id in fb_form.items ){
					if( document.getElementById( 'fb-'+id ) ){
						var item = document.getElementById( 'fb-'+id );
					}
					else{
						var item = document.getElementById( 'fb-'+id+'-1' );
					}
					fb_form.check( item );
				}
			}
			else{
				fb_form.fillCheck( null, null );
			}
		}
	}
 }

// check
function fbForm(){
	// status
	this.items = {
1: false
,2 : false
	}
	// button
	this.confirm = null;
	// button error
	this.error   = null;
	// item check
	this.check = function(item){
		// q no
		var n = item.id.replace( /^fb-(\d+)(-\d+)?$/, '$1' );
		// eror
		var error = document.getElementById( 'fb-'+n+'-error' );
		// test
		if( item.value.substr(0,6) == '##test' ){
			var inputs  = document.getElementsByTagName('input');
			for( var i = 0; i < inputs.length; i++ ){
				if( inputs[i].id.match(/^fb-\d+$/) )   inputs[i].value   = '##test';
				if( inputs[i].id.match(/^fb-\d+-1$/) ) inputs[i].checked = 'checked';
			}
			var textareas  = document.getElementsByTagName('textarea');
			for( var i = 0; i < textareas.length; i++ ){
				if( textareas[i].id.match(/^fb-\d+$/) ) textareas[i].value   = '##test';
			}
			var selects = document.getElementsByTagName('select');
			for( var i = 0; i < selects.length; i++ ){
				selects[i].value = selects[i].options[1].value;
			}
			for( var id in this.items ){
				this.items[id] = this.items[id] == null ? null : true;
			}
			this.confirm.disabled = null;
			this.error.innerHTML = '';
			return true;
		}
		if( this.items[n] != null ){
			// textarea
			if( item.name == 'comment' || item.rows ){
				checkLength( item.value, error, 1, 1600 );
			}
			// textbox
			else if( item.type == 'text' ){
				checkLength( item.value, error, 1, 50 );
			}
			// checkbox
			else if( item.type == 'checkbox' ){
				checkChecked( n, error );
			}
			// radio
			else if( item.type == 'radio' ){
				checkChecked( n, error );
			}
			// select
			else if( item.options ){
				checkSelected( item.options.selectedIndex, error );
			}
		}
		else{
			if( item.name == 'comment' ){
				checkLength( item.value, error, 0, 10000 );
			}
			else if( item.type == 'text' ){
				checkLength( item.value, error, 0, 50 );
			}
		}
		// fill check
		this.fillCheck( item, error );
	}
	// fill check
	this.fillCheck = function( item, error ){
		// item check
		if( item ){
			// q no
			var splits = item.id.split('-');
			var n = splits[1];
			if( error.innerHTML != '' ){
				item.className = item.className.replace( /^(.*?)( error)?$/, '$1 error' );
				this.items[n] = this.items[n] == null ? null : false;
			}
			else{
				item.className = item.className.replace( 'error', '' );
				this.items[n] = this.items[n] == null ? null : true;
			}
		}
		// other box
		var inputs  = document.getElementsByTagName('input');
		for( var i = 0; i < inputs.length; i++ ){
			if( inputs[i].type == 'text' && inputs[i].id.match(/-sub_text$/) ){
				var pInput = document.getElementById( inputs[i].id.replace( /-sub_text$/, '' ) );
				if( ( pInput.type=='checkbox' || pInput.type=='radio') && pInput.checked ){
					inputs[i].disabled = false;
					inputs[i].style.backgroundColor="#FFFFFF";
				}
				else if( pInput.type.match(/^select/) && pInput.options[pInput.options.selectedIndex].value=='その他'){
					inputs[i].disabled = false;
					inputs[i].style.backgroundColor="#FFFFFF";
				}
				else{
					inputs[i].disabled = true;
					inputs[i].style.backgroundColor="#F8F8F0";
				}
			}
		}
		// error message
		var spans = document.getElementsByTagName('span');
		for( var i = 0; i < spans.length; i++ ){
			if( spans[i].className == 'error' && spans[i].id != 'fb-fill-error' && spans[i].innerHTML != '' ){
				this.confirm.disabled = 'disabled';
				this.error.innerHTML = '';
				return false;
			}
		}
		// all check
		for( var id in this.items ){
			if( this.items[id] == false ){ // is required but might be empty
				var empty_flag = true;
				if(x = document.getElementById( 'fb-'+id )){
					if(x.value && x.value != '') empty_flag = false;
				}
				else if(x = document.getElementById( 'fb-'+id+'-1' )){
					for(var i = 1; y = document.getElementById('fb-'+id+'-'+i); i++){
						if(y.checked && y.checked == true) empty_flag = false;
					}
				}
				if(empty_flag){
					this.confirm.disabled = 'disabled';
					this.error.innerHTML = '';
					return false;
				}
			}
		}
		this.confirm.disabled = null;
		this.error.innerHTML = '';
	}
}

//
// check parts
//

// length
function checkLength( text, error, min, max ){
	if( text.length < min ){
		error.innerHTML = 'ご記入ください。';
		return false;
	}
	else if( max < text.length ){
		error.innerHTML = '{LIMIT}文字以内でご記入ください。'.replace( '{LIMIT}', max );
		return false;
	}
	else{
		error.innerHTML = '';
		return true;
	}
}
// checkbox
function checkChecked( n, error ){
	for( var i = 1; input = document.getElementById( 'fb-'+n+'-'+i ); i++ ){
		if( input.checked == true ){
			error.innerHTML = '';
			return true;
		}
	}
	error.innerHTML = '&nbsp;';
	return false;
}
// select
function checkSelected( index, error ){
	if( index == 0 ){
		error.innerHTML = '&nbsp;';
		return false;
	}
	else{
		error.innerHTML = '';
		return true;
	}
}

// other
function checkSubText(input){
	if( sub_text = document.getElementById( input.id+'-sub_text' ) ){
		if( input.checked ){
			sub_text.disabled = false;
			sub_text.style.backgroundColor="#FFFFFF";
		}
		else if(input.options[input.options.selectedIndex].value=='その他'){
			sub_text.disabled = false;
			sub_text.style.backgroundColor="#FFFFFF";
		}
		else{
			sub_text.disabled = true;
			sub_text.style.backgroundColor="#F8F8F0";
		}
	}
}

