//<!--
//
//
// Global variable defaultEmptyOK defines default return value 
// for many functions when they are passed the empty string. 
// By default, they will return defaultEmptyOK.
//
// defaultEmptyOK is false, which means that by default, 
// these functions will do "strict" validation.  Function
// isInteger, for example, will only return true if it is
// passed a string containing an integer; if it is passed
// the empty string, it will return false.
//
// You can change this default behavior globally (for all 
// functions which use defaultEmptyOK) by changing the value
// of defaultEmptyOK.
//
// Most of these functions have an optional argument emptyOK
// which allows you to override the default behavior for 
// the duration of a function call.
//
// This functionality is useful because it is possible to
// say "if the user puts anything in this field, it must
// be an integer (or a phone number, or a string, etc.), 
// but it's OK to leave the field empty too."
// This is the case for fields which are optional but which
// must have a certain kind of content if filled in.

var defaultEmptyOK = false

// whitespace characters
var whitespace = " \t\n\r";

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function sayHello(form)
{
	if (!isEmail(document.signupform.email.value)) {
		alert("Please enter a valid email address.");
    	document.signupform.email.focus();
    	return false;
    } else {
    	popup("http://services.theglobalist.com/fastSignup.asp?email="+document.signupform.email.value+"&R1="+(document.signupform.R1[0].checked?"chkSub":"chkUnsub"));
		document.signupform.email.value = "My e-mail address";
		document.signupform.R1[0].click();
    	return false;
    }
}

function sayHello2(form)
{
	if (!isEmail(document.signupform.email.value)) {
		alert("Please enter a valid email address.");
    	document.signupform.email.focus();
    	return false;
    } else {
    	popup("http://services.theglobalist.com/fastSignup.asp?email="+document.signupform.email.value+"&R1=" + "chkSub");
		document.signupform.email.value = "email address";
    	return false;
    }
}

function popup (form)
{
	openWindow=this.window.open(form,"Signup","resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=no,status=no,menubar=no,width=478,height=220,top=5,left=5");
}

function popup2 (form)
{
	openWindow=this.window.open(form,"Signup","resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no,width=1,height=1,top=1,left=1");
}

function checkComment (form)
{
    if (!isEmail(document.commentForm.email.value)) {
		alert("Please enter a valid email address.");
    		document.commentForm.email.focus();
    } else if (isEmpty(document.commentForm.comment.value)) {
		alert("Please enter a comment.");
    		document.commentForm.email.focus();
    } else {
  		popup("http://services.theglobalist.com/saveCmt2.asp?em="+document.commentForm.email.value+"&stLnk="+document.URL+"&sh="+(document.commentForm.chkShare.checked?"y":"n")+"&cmt="+escape(document.commentForm.comment.value));
			document.commentForm.email.value = "My e-mail address";
			document.commentForm.comment.value = "Got a comment, question or story idea for The Globalist?";
			document.commentForm.chkShare.value = "ON";
			document.commentForm.chkShare.click();
			window.close();
    }
    return false;
}


function checkComment2 (form)
{
    if (!isEmail(document.commentForm.email.value)) {
		alert("Please enter a valid email address.");
    		document.commentForm.email.focus();
    } else if (isEmpty(document.commentForm.comment.value)) {
		alert("Please enter a comment.");
    		document.commentForm.email.focus();
    } else {
    		popup2("http://services.theglobalist.com/saveCmt.asp?em="+document.commentForm.email.value+"&stLnk="+document.URL+"&sh="+(document.commentForm.chkShare.checked?"y":"n")+"&cmt="+escape(document.commentForm.comment.value));
		window.close();
    }
    return false;
}


function checkComments (form)
{
    if (!isEmail(document.commentForm.email.value)) {
		alert("Please enter a valid email address.");
    		document.commentForm.email.focus();
    } else if (isEmpty(document.commentForm.comment.value)) {
		alert("Please enter a comment.");
    		document.commentForm.email.focus();
    } else {
    //		popup("http://services.theglobalist.com/saveCmt2.asp?em="+document.commentForm.email.value+"&stLnk="+document.URL+"&sh="+(document.commentForm.chkShare.checked?"y":"n")+"&cmt="+escape(document.commentForm.comment.value));
//		window.close();
    }
    return false;
}

function clear()
{
 document.signupform.email.value = "";
}
function clearcommentForm(key)
{
 	if ((key == 1) && (	document.commentForm.email.value == "My e-mail address")){
  		document.commentForm.email.value = "";
  	}
	if ((key == 2) && (	document.commentForm.comment.value == "Got a comment, question or story idea for The Globalist?")){
  		document.commentForm.comment.value = "";
  	}
}

//-->

