<html>
<head>
<script language="JavaScript1.2">
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
} else {
begin += 2;
}
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
return unescape(dc.substring(begin + prefix.length, end));
}
function setCookie(NameOfCookie, value, expiredays) {
var ExpireDate = new Date ();
ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
document.cookie = NameOfCookie + "=" + escape(value) +
((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}
function delCookie (NameOfCookie) {
if (getCookie(NameOfCookie)) {
document.cookie = NameOfCookie + "=" +
"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}
function validate()
{
var chk = getCookie('login');
//alert(chk);
var cook;
var nick;
if (chk == null)
{
var checknick = /^\w{1,}$/
nick = prompt("Enter your nickname","");
if (nick.search(checknick) == -1){
alert("Invalid nickname!");return false;}
cook = getCookie(nick);
}
else{
cook = getCookie(chk);nick = chk;}
if (cook != null)
{
alert("Hello " + nick);
var n = "", e = "", w = "", v = "";
var i = 0;
while(cook[i] != " ")
{
if (cook[i] != undefined)
n += cook[i]; // get nickname
i++;
}
i++;
while(cook[i] != " ")
{
e += cook[i]; //get email
i++;
}
i++;
while(cook[i] != " ")
{
w += cook[i]; //web site
i++;
}
i++;
while(i < cook.length && cook[i] != " ")
{
v += cook[i]; //get number of visited visited
i++;
}
v = parseInt(v);
v++;
var newcook = n + " " + e + " " + w + " " + v;
//alert(newcook);
setCookie(nick,newcook);
setCookie('login',n);
return true;
}
var email = prompt("Enter your email","");
var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
if (!reg1.test(email) && reg2.test(email)) ;
else {
alert("\"" + email + "\" is an invalid e-mail!"); return false;}
var website = prompt("Enter your website.(optional)","");
var checksite = /^(http\:\/\/)?(www\.)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3})$/
if (website != null && website != "" && website.search(checksite) == -1 )
{alert("Invalid website!");return false;}
if (website == null || website == "") website = "none";
var cookvalue = nick + " " + email + " " + website + " " + "1";
//alert(cookvalue);
setCookie(nick,cookvalue);
setCookie('login',nick);
alert("Hello " + nick);
}
var a = document.onLoad = validate();
function chkadmin()
{
var user = prompt("Enter username:","");
var pass = prompt("Enter password:","");
if (user == "administrator" && pass == "administrator")
{
var ca = unescape(document.cookie.split(';'));
//document.write(ca);
//document.write(ca);
var j;
document.write("<table cellpadding='10'>");
document.write("<tr>");
document.write("<th>Nickname</td>");
document.write("<th>Email</td>");
document.write("<th>Website</td>");
document.write("<th>Visited this site</td>");
document.write("</tr>");
var temp = "";
j = 0;
var k;
var ntd = 1;
document.write("<tr>");
while(j < ca.length)
{
if (ca[j] == "=")
{
j++;
for(k = 0;k <= temp.length;k++)
{
j++;
}
if (temp == "login")
{
while(j < ca.length && ca[j] != ",")
j++;
j++;
}
else
{
document.write("<td>"+temp+"</td>");
temp = "";
ntd++;
}
}
else if (ca[j] == " ")
{
j++;
document.write("<td>"+temp+"</td>");
temp = "";
ntd++;
}
else if(ca[j] == ",")
{
j++;
j++;
document.write("<td>"+temp+"</td></tr><tr>");
temp="";
}
else
{
temp += ca[j];
j++;
}
}
if (temp != "login")document.write("<td>"+temp+"</td>")
document.write("</tr>");
}
else
alert("Invalid username or password!");
}
function erase()
{
delCookie('login');
}
</script>
</head>
<body>
<p align="center">
Nickname
<input type="text" name="nick" id="nick">
</p>
<p align="center">
<label> e-mail
<input type="text" name="email" id="email">
</label>
</p>
<p align="center">
<label>
webpage
<input type="text" name="webpage" id="webpage">
</label>
</p>
<p align="center">
<label>
<input type="submit" name="Submit" id="Submit" value="Submit">
</label>
<input type="button" onClick="chkadmin()" value="Admin">
<input type="button" onClick="erase()" value="Log out">
<br /> <br />
</p>
</body>
</html>