// JavaScript Document
var popUpWin=0;

function popUpWindow(URLStr, width, height)

{

  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  
  if (URLStr == "") {
	  URLStr = "about:blank";
  }
  
  if(width == "") width='400';
  if(height == "") height='200';

  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,scrollbars=yes,status=no,menubar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left=100, top=100,screenX=100,screenY=100');
  popUpWin.document.close;
  popUpWin.focus();
}


function validate_contact_form()
{
	var msg = "";
	var f = document.form;
	
	if (f.name.value == "" || f.name.value.length < 2) 
	{
		msg += "Please enter your name!\n";
	}
	
	if (f.email.value != "")
	{
		if (!check_email(f.email.value)) {
			msg += "Email address not in the correct format!\n";
		}
	}
	
	if (f.phone.value == "") 
	{
		msg += "Please enter your phone number!\n";
	}
	
	if (f.phone.value != "")
	{
		var checkOK = "0123456789-";
  		var checkStr = f.phone.value;
		var allValid = true;
		var length = 0;
		
		for (i = 0;  i < checkStr.length;  i++)
		{
   			ch = checkStr.charAt(i);
			
			if (!isNaN(ch)) length++;
			
   			for (j = 0; j < checkOK.length; j++)
   			{
				if (checkOK.indexOf(ch) == -1)
				{
   					allValid = false;
   					break;
				}
	  		}
			if (!allValid) break;
		}
	
		if (!allValid)
		{
   			msg += "Enter only digit characters in the phone number field\n";
		} else {
			if (length < 10)
			{
				msg += "Phone number must be at least 10 digits!\n";
			}
			if (length > 10)
			{
				msg += "Phone number has too many digits!\n";
			}
		}
	}
	
	if (f.message.value == "") 
	{
		msg += "Blank messages are not allowed!\n";
	}

	if (msg != "")
	{
		alert("The following errors need to be fixed:\n\n" + msg);
		return;
	}
	
	f.phone.value = trim(f.phone.value);	
   	f.name.value = trim(f.name.value);	
	f.message.value = trim(f.message.value);	
	f.email.value = trim(f.email.value);

	popUpWindow('','400','150');
	f.action = "sendinfo.php";
	f.target = "popUpWin";
	f.submit();
	f.reset();
	
}

function trim(str)
{
   return str.replace(/^\s+|\s+$/g,'');
}

function check_email(email)
{
		var valid_chars = "abcdefghijklmnopqrstuvwxyz0123456789@_-.";
		var len = email.length;
		
		if (email.indexOf("@") == -1 || email.indexOf(".") == -1)
		{
			return false;
		}

		for (var i=0; i < len; i++)
		{
			var letter = email.charAt(i).toLowerCase();
			if (valid_chars.indexOf(letter) != -1) 	continue;
			return false;
		}
		
		return true;
}
