// JavaScript Document

function get_clock_bbs_new() { 
	stance = 0;
	weekday=new Array("Sun","Mon","Tue","Wed","Thr","Fri","Sat") //現在の時刻の取得 
	get_time = new Date(); 
	get_time.setTime(get_time.getTime() + stance); 
	get_month = get_time.getMonth() + 1;
	switch(get_month) {
		case 1 : set_month = 'Jan'; break;
		case 2 : set_month = 'Feb'; break;
		case 3 : set_month = 'Mar'; break;
		case 4 : set_month = 'Apr'; break;
		case 5 : set_month = 'May'; break;
		case 6 : set_month = 'Jun'; break;
		case 7 : set_month = 'Jul'; break;
		case 8 : set_month = 'Aug'; break;
		case 9 : set_month = 'Sep'; break;
		case 10 : set_month = 'Oct'; break;
		case 11 : set_month = 'Nov'; break;
		case 12 : set_month = 'Dec'; break;
		default : 'Error';
	}
	get_milliseconds = get_time.getMilliseconds(); 
	get_milliseconds = "000" + get_milliseconds.toString(); 
	get_milliseconds = get_milliseconds.slice(-3); 
	time_text = "";
	ampm = 'am';
	set_hours = get_time.getHours();
	if(set_hours > 12) {
		ampm = 'pm'
		set_hours = set_hours - 12;
	}
	if(set_hours < 10) {
		set_hours = '0' + set_hours;	
	}
	time_text = time_text + set_hours + ":";
	set_minutes = get_time.getMinutes();
	if(set_minutes < 10) {
		set_minutes = '0' + set_minutes;
	}
	time_text = time_text + set_minutes;
	time_text = time_text + ampm + ' ';
	time_text = time_text + set_month + "/"; 
	time_text = time_text + get_time.getDate() + "/"; 
	get_year = get_time.getFullYear();
	if(get_year > 2000) {
		set_year = get_year - 2000;	
	} else {
		set_year = get_year - 1900;	
	}
	time_text = time_text + set_year; 
	$('#set_bbs_date').text(time_text);
	setTimeout("get_clock_bbs_new()", 1000); // 10/1000秒に指定
}
function count_words(strSrc){
	len = 0;
	strSrc = escape(strSrc);
	for(i = 0; i < strSrc.length; i++, len++){
		if(strSrc.charAt(i) == "%"){
			if(strSrc.charAt(++i) == "u"){
				i += 3;
				len++;
			}
			i++;
		}
	}
	return len;
}
function check_form() {
		error_texts = '';
		error = 'false';
		//alert($('[name=bbs_use_condition]').attr('checked'));
		if($('[name=bbs_use_condition]').attr('checked') == false) {
			error_texts += '投稿には利用規約の同意が必要です' + "\n";
			error = 'true';
		}
		if($('[name=bbs_name]').attr('value').length <= 0) {
			//alert('名前を入力してください');
			error_texts += '名前を入力してください' + "\n";
			error = 'true';
		} 
		if(count_words($('[name=bbs_name]').attr('value')) > 60){
			error_texts += '名前が長すぎます' + "\n";
			error = 'true';
		}
		if($('[name=bbs_type]').attr('value').length <= 0 || $('[name=bbs_type]').attr('value') == 'all') {
			error_texts += '投稿タイプを選択してください' + "\n";
			error = 'true';
		}
		if(count_words($('[name=bbs_type]').attr('value')) > 10){
			error_texts += '投稿タイプに不正アクセスです' + "\n";
			error = 'true';
		}
		if($('[name=bbs_title]').attr('value').length <= 0) {
			error_texts += 'タイトルを記入してください' + "\n";
			error = 'true';
		}
		if(count_words($('[name=bbs_title]').attr('value')) > 60){
			error_texts += 'タイトルが長すぎます' + "\n";
			error = 'true';
		}
		if($('[name=bbs_contents]').attr('value').length <= 0) {
			error_texts += '内容を記入してください' + "\n";
			error = 'true';
		}
		if(count_words($('[name=bbs_contents]').attr('value')) > 1200){
			error_texts += '内容が多すぎます' + "\n";
			error = 'true';
		}
		if($('[name=bbs_email]').attr('value').length <= 0) {
			error_texts += 'メールアドレスを記入してください' + "\n";
			error = 'true';
		}
		if(count_words($('[name=bbs_email]').attr('value')) > 60){
			error_texts += 'メールアドレスが長すぎます' + "\n";
			error = 'true';
		}
		if(count_words($('[name=bbs_url]').attr('value')) > 300){
			error_texts += 'ウェブアドレスが長すぎます' + "\n";
			error = 'true';
		}
		if($('[name=bbs_security]').attr('value').length <= 0) {
			error_texts += 'セキュリティーコードを入力してください' + "\n";
			error = 'true';
		}
		if(error == 'true') {
			alert(error_texts);
			return false;
		} else {
			confirmation = confirm('入力した内容を掲載しますか？');
			if(confirmation) {
				return true;
			} else {
				return false;	
			}			
		}
	}
	function encode_html_style(texts) {
		if(texts.match(/</g)) {
			//alert('here');
			texts = texts.replace(/</g, '&lt;');
		}
		if(texts.match(/>/g)) {
			texts = texts.replace(/>/g, '&gt;');
		}
		if(texts.match(/\n/g)) {
			texts = texts.replace(/\n/g, '<br />');
		}
		if(texts.match(/<br \/>/g) || texts.match(/&lt;br \/>/g) || texts.match(/<br \/&gt;/g)) {
			texts = texts.replace(/.br \/./, '<br />');
		}
		if(texts.match(/&lt;br \/&gt;/g)) {
			texts.replace(/&lt;br \/&gt;/g, '<br />');	
		}//else {
			
		//}
		
		return texts;
	}
$(function() {
	get_clock_bbs_new();
	
	text_contents = '';
	function close_upload_file_popup(event) {
		event.preventDefault();
		
		$.ajax({
			type:"GET",
			url:"type_file.php",
			data:"filename=uploaded",
			cache:'false',
				complete: function(XHR){
				//alert(XHR.responseText);
				var filename = XHR.responseText.split(' ');
				$('#bbs_contents dd:eq(2) img').remove();
				var count_file = filename.length - 1;
				//alert(count_file);
				set_image = '';
				for(var i = 0; i < count_file; i++) {
					set_image = set_image + '<img src="http://www.peoploop.com/images/bbs/s/' + filename[i] + '.jpg" />';
				}
				//alert(set_image);
				$('#bbs_contents dd:eq(1)').html(set_image);
				$('#upload_file_popup').hide();
				}
		});
	}
	function show_file_uploader(event) {
		event.preventDefault();
		// Begin Get Size of Browser
		var browser_width = $('body').width();
		var browser_height = $(document).height();
		var browser_scrollTop = $(window).scrollTop();
		var display_width = $(document).height();
		var display_height = $(document).height();
		// End Get Size of Browser
		
		// Begin Setting Position to Open Iframe
		//alert(browser_height);
		position_top = (browser_height - 600) * 0.5 + browser_scrollTop - 23;
		position_left = (browser_width - 400) * 0.5;
		
		$('#upload_file_bg').width(browser_width).height(browser_height).css('opacity', '0.5');
		//alert(position_top);
		$('#upload_file').css({'height':display_height + 'px'});
		//$('#button_close').css({'top':position_top + 'px', 'left':position_left + 356 + 'px'});
		$('#upload_file_contents').css({'top':position_top + 'px', 'left':position_left + 'px'});
		//$('#upload_file').hide();
		// End Setting Position to Open Iframe
	
		$('#upload_file_popup').show();
	}
	$('body')
	.append($('<div>').attr('id','upload_file_popup')
		.append($('<div id="upload_file">')
			.append($('<div id="upload_file_contents">')
				.append($('<div id="upload_file_contents_close"><a href="?action=close_popup">X</a></div>'))
				.append($('<iframe scrolling="no" frameborder="0" allowtransparency="true">').attr('src','http://www.peoploop.com/type_file.php'))
			)
			.append($('<div id="upload_file_contents_bg">'))			
		)
		.append($('<div id="upload_file_bg">'))
	);
	$('#upload_file_popup').hide();
	$('#open_bbs_file_type').bind('click', show_file_uploader);
	$('#upload_file_contents_close').bind('click', close_upload_file_popup);
	//alert(location.href);
	if(location.href.match(/bbs_new.php/)) {
		$('.cate_link a').click(function(event) { event.preventDefault(); });	
		
	}
	$(':text').width('100%').css({'border':'solid 1px #057c05', 'background':'#eee'});
	$(':input[name=bbs_contents]').width('100%').css({'border':'solid 1px #057c05', 'background':'#eee'});
	$('[name=bbs_name]')
	.bind('keyup', function() {
		//alert(texts);
		texts = $(this).attr('value');
		var countWords = count_words(texts);
		//alert(countWords);
		if(countWords <= 60) {
			$(this).parent().prev().children('em').html('<span class="error_lv1">' + countWords + '</span> / 60');
			$('#set_bbs_name').text(texts);
		} else {
			$(this).parent().prev().children('em').html('<span class="error_lv3">' + countWords + '</span> / 60');
		}
	});
	$('[name=bbs_title]')
	.bind('keyup', function() {
		//alert(texts);
		texts = $(this).attr('value');
		var countWords = count_words(texts);
		if(countWords <= 60) {
			$(this).parent().prev().children('em').html('<span class="error_lv1">' + countWords + '</span> / 60');
			$('#bbs_contents h2').text(texts);
		} else {
			$(this).parent().prev().children('em').html('<span class="error_lv3">' + countWords + '</span> / 60');
		}
	});
	$('[name=bbs_type]')
	.bind('change', function() {
		texts = $(this).attr('value');
		$('.cate_link ul li a').attr('id','');
		$('.cate_link ul li').each(function() {
			if(texts.length > 0) {
				if($(this).children('a').attr('href').match(texts)) {
					//alert($(this).children('a').attr('href'));
					$(this).children('a').attr('id', 'bbs_type_set');
				}	
			}			
		});	 
	});
	$('[name=bbs_contents]')
	.bind('keyup', function() {
		//alert(texts);
		texts = encode_html_style($(this).attr('value'));
		//alert(texts);
		var countWords = count_words(texts);
		if(countWords <= 1200) {
			$(this).parent().prev().children('em').html('<span class="error_lv1">' + countWords + '</span> / 1200');
			$('#bbs_contents dd:eq(2)').html(texts);
			
		} else {
			$(this).parent().prev().children('em').html('<span class="error_lv3">' + countWords + '</span> / 1200');
		}
	});
	email_address = '';
	$('[name=bbs_email]')
	.bind('keyup', function() {
		texts = $(this).attr('value');
		var countWords = count_words(texts);
		if(countWords <= 60) {
			$(this).parent().prev().children('em').html('<span class="error_lv1">' + countWords + '</span> / 60');
			if($('[name=bbs_email_status]').attr('checked') == true) {
				if(texts != '') {
					email_address = 'Email: <a href="mailto:' + texts + '">' + texts + '</a>';
					$('#bbs_contents dd:eq(3)').html(email_address);	
				} else {
					$('#bbs_contents dd:eq(3)').html('');
				}
			} else {
				$('#bbs_contents dd:eq(3)').html('');
			}
		} else {
			$(this).parent().prev().children('em').html('<span class="error_lv3">' + countWords + '</span> / 60');
		}		
	});
	$('[name=bbs_email_status]').bind('click', function() {
		if($(this).attr('checked') == false) {
			$('#bbs_contents dd:eq(3)').html('');
		} else {
			$('#bbs_contents dd:eq(3)').html(email_address);
		}
		//alert($(this).attr('checked'));
	});
	$('[name=bbs_url]')
	.bind('keyup', function() {
		//alert(texts);
		texts = $(this).attr('value');
		var countWords = count_words(texts);
		text_contents = '';
		if(countWords <= 300) {
			$(this).parent().prev().children('em').html('<span class="error_lv1">' + countWords + '</span> / 300');
			if(texts != 'http://'&& texts != '') {
				text_contents	= 'Website: <a href="' + texts + '" target="_blank">' + texts + '</a>'
			}
			$('#bbs_contents dd:eq(4)').html(text_contents);
		} else {
			$(this).parent().prev().children('em').html('<span class="error_lv3">' + countWords + '</span> / 300');
		}		
	});
	
	$('#form_reset').click(function() {
		confirmation = confirm('リセットしますか？');
		if(confirmation) {
			$('#bbs_contents h2').text('');
			$('#bbs_contents dd:eq(0) span:eq(0)').text('');
			$('#bbs_contents dd:eq(1)').text('');
			$('#bbs_contents dd:eq(2)').text('');
			$('#bbs_contents dd:eq(3)').text('');
			$('#bbs_contents dd:eq(4)').text('');
			$.ajax({
				type:"GET",
				url:"type_file.php",
				data:"filename=delete",
				cache:'false',
				complete: function(XHR){
					location.href="bbs_new.php";
				}
			});	
		}		
	});	
});
