// JavaScript Document
function PopupCenter(pageURL, title,w,h) {
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	title = "_1";
	var option_list = 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left;
	var targetWin = window.open (pageURL, title, option_list);
}
function isValidEmail(email, required) {
	if (required==undefined) {   // if not specified, assume it's required
		required=true;
	}
	if (email==null) {
		if (required) {
			return false;
		}
		return true;
	}
	if (email.length==0) {
		if (required) {
			return false;
		}
		return true;
	}
	if (! allValidChars(email)) {  // check to make sure all characters are valid
		return false;
	}
	if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
		return false;
	} else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
		return false;
	} else if (email.indexOf("@") == email.length) {  // @ must not be the last character
		return false;
	} else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
	} else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
	}else if (email.indexOf(".") != email.lastIndexOf(".") && false) return false;
	return true;
}
function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
	var letter = email.charAt(i).toLowerCase();
	if (validchars.indexOf(letter) != -1)
	  continue;
	parsed = false;
	break;
  }
  return parsed;
}
function mailFriend()
{
	var from = document.getElementById('email_from');
	var to = document.getElementById('email_to');
	var message = document.getElementById('message');
	var captcha = document.getElementById('captcha');
	if( captcha.value == '' || captcha.value == null)
	{
		alert("Please fill in the letters appearing in the letters appearing in the image");
		return false;
	}
	if(isValidEmail(from.value,true))
	{
		if(isValidEmail(to.value,true))
		{
			if(message.value!='' && message.value!= null)
			{
				var ajax = new sack();
				ajax.method='POST';
				ajax.requestFile = 'referTofriend.php';
				ajax.element = 'referTofriend';
				ajax.setVar('from' ,from.value);
				ajax.setVar('to' ,to.value);
				ajax.setVar('_message' ,message.value);
				ajax.setVar('captcha' ,captcha.value);
				ajax.onLoading = function(){
					document.getElementById( this.element ).innerHTML = '...processing';
				}
				ajax.onCompletion = function(){
					var from = document.getElementById('email_from');
					var to = document.getElementById('email_to');
					var message = document.getElementById('message');
					var captcha = document.getElementById('captcha');
					if( this.response.search( RegExp('Captcha ') ) == -1)
					{
						from.value  = to.value = message.value = captcha.value = '';
						setTimeout("document.getElementById('referTofriend').innerHTML = '';", 5000);
					}
				}
				ajax.runAJAX();
			}
			else
				alert("You forgot to fill in a message");
		}
		else
			alert("Your friend's email is incorrect");
	}
	else
		alert("Your email is incorrect");
}
