﻿function validate(f)
{
	if(is_empty(f.title.value))
	{
		alert('請輸入標題')
		return false;
	}
	if(is_empty(f.url.value))
	{
		alert('請輸入網誌地址')
		return false;
	}
	if(is_empty(f.email.value))
	{
		alert('請輸入電郵地址')
		return false;
	}
	if(is_empty(f.captcha.value))
	{
		alert('請輸入驗證碼');
		return;
	}
	if(!is_email(f.email.value))
	{
		alert('電郵地址不正確')
		return false;
	}
	/*
	if(!is_url(f.url.value))
	{
		alert('網誌地址不正確')
		return false;
	}
	*/
	return true;
}
function input_avatar(url)
{
	if(url==1)
	{
		url = prompt('請輸入圖片網址：','');
		if(is_empty(url))
			return;
	}
	document.getElementById('avatar').innerHTML = '';
	document.getElementById('avatar').style.background = 'url(/img/loading_img.gif) center center no-repeat';
	document.getElementById('avatar_test').innerHTML =
		'<img src="'+url+'" onLoad="set_avatar(\''+url+'\')" onError="invalid_avatar()" onAbort="invalid_avatar()" />';
}
function set_avatar(url)
{
	document.getElementById('avatar').innerHTML = '';
	document.getElementById('avatar').style.background = 'url('+url+') center center no-repeat';
	document.info.avatar.value = url;
}
function invalid_avatar()
{
	document.getElementById('avatar').style.background = '';
	document.getElementById('avatar').innerHTML = '<br><br><br><br>圖片網址不正確';
}
function is_empty(str)
{
	if (str==null||str=='')
		return true;
	return false;
}
function paging(f,offset)
{
	var cat = '';
	if(f.cat.selectedIndex!=0)
		cat = f.cat.options[f.cat.selectedIndex].value;
	if(f.offset.options[f.offset.selectedIndex].value!='10/')
		document.write('<a href="/billboard/'+cat+(offset-10)+'/" style="font-size:10pt;text-decoration:none;color:#333;"><img src="/img/prev.gif" border="0" />&nbsp;上一頁</a>&nbsp;|&nbsp;');
	if(f.offset.selectedIndex!=f.offset.options.length-1)
		document.write('<a href="/billboard/'+cat+(offset+10)+'/" style="font-size:10pt;text-decoration:none;color:#333;">下一頁&nbsp;<img src="/img/next.gif" border="0" /></a>');
}
function cat_browse(f,s)
{
	f.cat.disabled = 1;
	f.offset.disabled = 1;
	if(s=='cat') f.offset.options[0].selected=1;
	top.location.href='/billboard/'+f.cat.options[f.cat.selectedIndex].value+f.offset.options[f.offset.selectedIndex].value;
}
function set_form(f,v)
{
	e = f.elements;
	for(i=0;i<e.length;i++)
	{
		e[i].disabled = v;
	}
}
function is_email(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
	   return false
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	   return false
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false
	if (str.indexOf(at,(lat+1))!=-1)
		return false
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false
	if (str.indexOf(dot,(lat+2))==-1)
		return false
	if (str.indexOf(" ")!=-1)
		return false
	return true					
}
function is_valid_id(str)
{
	var regex = /[^a-zA-Z0-9_-]+/;
	if(regex.test(str)||str.length<6||str.length>20)
		return false;
	return true;
}
function is_url(strUrl)
{
    var RegexUrl = /(http|https):\/\/[A-Za-z0-9\.-_]{3,}\.[A-Za-z]{3}/;
    return RegexUrl.test(strUrl);
}
function is_maxlength(obj)
{
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}
function get_selectedIndex(s)
{
	for (var i=0; i<s.options.length-1; i++)
		if(s.options[i].selected)
			return i;
}
function show(id) {document.getElementById(id).style.display='block';}
function hide(id) {document.getElementById(id).style.display='none';}