// by Roy S. Rubinstein (roy@acm.org) October 2000
// All Rights Reserved.

function checkEmail(em)
{
	var atIndex, afterAt, dotIndex, OK = true

	if (isAllAllowed(em.toLowerCase()) && (atIndex = em.indexOf("@")) > 0) {
		afterAt = em.substring(atIndex+1, em.length)
		dotIndex = afterAt.indexOf(".")
		if (dotIndex <= 0  || dotIndex > afterAt.length-3 ||
		    afterAt.indexOf("@") >= 0) {
			OK = false
		}
	}
	else {
		OK = false
	}
	//alert(em + "'s validity is " + OK)
	return OK
}

function allowedChar(c)
{
	return (c >= "a" && c <= "z") || (c >= "0" && c <= "9") ||
		(c == "@") || (c == ".") || (c == "_") || (c == "-") || (c == "$")
}


function isAllAllowed(s) {
	var i = 0, OK = true

	while (i < s.length && OK) {
		OK = allowedChar(s.charAt(i++))
	}
	return OK
}


function checkAndContinue(email)
{
	var em, OK = false

	em = email.value
	if (em == "") {
		alert("Please enter a valid email address.")
		email.focus()
	}
	else {
		//alert(em + "'s validity is " + checkEmail(em))
		if (checkEmail(em)) {
			setCookieValue("email", em)
			if (getCookieValue("email") != em) {
				alert("You must accept this (nonpersistent) cookie to continue.")
			}
			else {
				OK = true
			}
		}
		else {
			alert(em + " is not a valid email address.\nPlease try again.")
			email.focus()
		}
	}
	//alert("returning " + OK)
	return OK
}

function getCookieValue(s)
{
	var cook, pos1 = 0, pos2, val = ""

	cook = document.cookie
	if (cook.substring(0, s.length + 1) == (s + "=")) {
		pos1 = s.length + 1
	}
	else {
		if ((pos1 = cook.indexOf(" "+s+"=")) >= 0) {
			pos1 += s.length + 1
		}
	}
	if (pos1 > 0) {
		pos2 = cook.indexOf("; ", pos1)
		val = cook.substring(pos1, (pos2 < 0) ? cook.length : pos2)
	}
	return unescape(val)
}

function setCookieValue(k, v)
{
	//var exp = new Date(), x

	if (getCookieValue(k) != v) {
		//x = exp.getTime() + 600000
		//exp.setTime(x)
		//document.cookie = k + "=" + escape(v) + "; expires=" + exp.toGMTString()
		document.cookie = k + "=" + escape(v) + "; path=/"
	}
}

function loadOfferPage()
{
	var em

	if ((em = getCookieValue("email")) != "") {
		document.FrontPage_Form1.email.value = em
	}
}
