$(document).ready
(
	function()
	{
		showSHExpo();
		if ($.browser.msie && parseInt($.browser.version) < 7)
		{
			$('#header .menu li').eq(15).hover
			(
				function()
				{
					$('#div_substation').show();
				},
				function()
				{
					$('#div_substation').hide();
				}
			);
		}
		$('#ipt_comment_scode').focus
		(
			function()
			{
				if ( $('#img_scode').attr('src') == 'about:blank' )
				{
					$('#img_scode').attr('src', $('#img_scode').attr('rev'));
					$('#img_scode').show();
				}
			}
		);
		$('#img_scode').click
		(
			function()
			{
				var src = $(this).attr('src').replace(/&ts=.*/ig, '');
				$(this).attr('src', src +'&ts='+ Math.random());
			}
		);
		$('form[name=comment_form]').submit
		(
			function()
			{
				var str_notice_header = str_notice_body = str_notice_footer = '';
				if ($.trim($('textarea[name=commentval]', this).val()) == '') str_notice_body += '<li>评论内容不能为空</li>';
				if ($('input[name=scode]', this).length > 0 && $.trim($('input[name=scode]', this).val()) == '') str_notice_body += '<li>验证码不能为空</li>';
				if (str_notice_body != '')
				{
					str_notice_header = '<div class="user_notice">' +
									'<ul id="notice_info">';
					str_notice_footer =	'<\/ul><\/div>';
					if ($(this).prev('div.user_notice').length == 0)
					{
						$(this).before(str_notice_header + str_notice_body + str_notice_footer);
					}
					else
					{
						$('ul#notice_info').html(str_notice_body);
					}
					return false;
				}
				if ($(this).attr('target') == '_blank')
				{
					//$('input[name=comment_submit_btn]', this).attr({disabled: true});return false;
					$.ajax
					({
						type: 'POST',
						dataType: 'json',
						cache: false,
						url: $(this).attr('action') +'&is_ajax=1',
						data: 'commentval='+ $.trim($('textarea[name=commentval]', this).val()) +'&scode='+ $.trim($('input[name=scode]', this).val()),
						//beforeSend:function(){$('form[name=comment_form] input[name=comment_submit_btn]').attr({disabled: true});},
						success: function(sjson)
							{
								if (sjson.error == 0)
								{
									window.alert('成功保存您的评论，感谢您的参与');
									if ($('#comment_count').length > 0)
									{
										$('#comment_count').text(parseInt($('#comment_count').text()) + 1);
									}
									$('form[name=comment_form]')[0].reset();
								}
								else
								{
									window.alert('对不起，系统不能保存您的评论'+ (sjson.message == '' ? '' : '，错误信息如下：\n'+ sjson.message));
								}
								//$('form[name=comment_form] input[name=comment_submit_btn]').attr({disabled: false});
							}
					});
					return false;
				}
			}
		);
		$('#cform').submit
		(
			function()
			{
				var str_error = '';
				if ($.trim($('input[name=uname]', this).val()) == '') str_error += '姓名不能为空' + "\n";
				if ($.trim($('input[name=utel]', this).val()) == '') str_error += '联系电话不能为空' + "\n";
				if ($.trim($('input[name=utel]', this).val()) != '')
				{
					if (!check_tel($.trim($('input[name=utel]', this).val())) && !check_mobile($.trim($('input[name=utel]', this).val())))
					{
						str_error += '电话格式不正确' + "\n";
					}
				}
				if ($.trim($('input[name=ucard]', this).val()) == '') str_error += '身份证号不能为空' + "\n";
				if ($.trim($('input[name=umail]', this).val()) == '') str_error += 'Email 不能为空' + "\n";
				if ($.trim($('input[name=umail]', this).val()) != '')
				{
					if (!check_mail($.trim($('input[name=umail]', this).val())))
					{
						str_error += 'Email 格式不正确' + "\n";
					}
				}
				if ($.trim($('input[name=uaddr]', this).val()) == '') str_error += '通讯地址不能为空' + "\n";
				if ($.trim($('input[name=scode]', this).val()) == '') str_error += '验证码不能为空';
				if (str_error != '')
				{
					window.alert(str_error);return false;
				}
			}
		);
		
	}
);
//add to favorites
function addFavorite(url, title)
{
	if(window.ActiveXObject)
	{
		window.external.AddFavorite(url, title);
	}
	else
	{
		window.sidebar.addPanel(title, url, '');
	}
}
//set homepage
function setHomePage(url)
{
	if (window.ActiveXObject)
	{
		document.links[0].style.behavior = 'url(#default#homepage)';
		document.links[0].setHomePage(url);
	}
	else
	{
		window.alert('请使用 工具->选项 添加当前页为您的首页，谢谢！');
	}
}
//set font size
function setFontSize(element, fsize)
{
	$('#'+ element).css({fontSize: fsize +'px'});
}
//load article comment count
function load_comment_count(articleid)
{
	$.get('/index.php', {id: articleid, 'act': 'getcount', 'app': 'comment'}, function(icount){$('#comment_count').text(icount);});
	//$('#comment_count').text('0');
}
function check_mail(str_mail)
{
	return /^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|com|gov|mil|org|edu|int)$/.test(str_mail);
}
function check_tel(str_tel, is_fax)
{
	return (is_fax ? /^(\d{3,4}-)?\d{7,8}$/ : /^(\d{3,4}-)?\d{7,8}(-\d+)?$/).test(str_tel);
}
function check_mobile(str_mobile)
{
	return /^(0)?1[35]\d{9}$/.test(str_mobile);
}
function check_url(str_url)
{
	return /^(http:\/\/)?[a-zA-Z0-9-]+(\.[a-zA-z0-9-]+)+\/?$/.test(str_url);
}
function setCookie(name, value, time)
{
	var strcookie = name + '=' + encodeURIComponent(value) + ';path=\/;domain=.sjxueche.com';
	if (time)
	{
		var d = new Date();
		d.setHours(8);
		d.setTime(d.getTime() + time * 1000);
		strcookie += ';expires=' + d.toGMTString();
	}
	document.cookie = strcookie;
}
function getCookie(name)
{
	var arr = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'));
	if (arr != null) return decodeURIComponent(arr[2]); return null;
}
function delCookie(name)
{
	var _cookie = getCookie(name);
	if (_cookie != null)
	{
		setCookie(name, '' , -86400);
	}
}
function showSHExpo()
{
	var now = new Date();
	var expoDate = new Date(2010, 4, 1);
	$('#sh-shibo').html('距离上海世博会还有 <font style="color:#ff5a00;font-size:14px;font-family:Georgia;">'+ Math.floor((expoDate - now) / 86400000 + 1) +'</font> 天 ');
}