// JavaScript Document
var gynMecca = true;
function trimString (str) {  str = this != window? this : str;  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');}
function funcfocus() {
	document.lrsignup.fname.focus();
}
function verifyRequired(pfrm) {
	var vtxEmailField = "email";  //The email field is 'email' for the loudreach signup form
	var vtxEmailField = "email2"; //The email field is 'email2' for the mecca form
	vtxEmailField = gynMecca ? "email2": "email";
	
	if (trimString(document.lrsignup["fname"].value) == "") {
		alert("The First Name field is required.");
		document.lrsignup["fname"].value="";
		document.lrsignup["fname"].focus();
		return false;
	} else {
		document.lrsignup["fname"].value=trimString(document.lrsignup["fname"].value);  
	}
	if (trimString(document.lrsignup["lname"].value) == "") {
		alert("The Last Name field is required.");
		document.lrsignup["lname"].value="";
		document.lrsignup["lname"].focus();
		return false;  
	} else {
		document.lrsignup["lname"].value=trimString(document.lrsignup["lname"].value);
	}
	if (trimString(document.lrsignup[vtxEmailField].value) == "") { 
		alert("The Email field is required.");document.lrsignup[vtxEmailField].value="";
		document.lrsignup[vtxEmailField].focus(); 
		return false;
	}

 	pfrm[vtxEmailField].value=trimString(pfrm[vtxEmailField].value);  
	if (!frmIsValidEmailAddr(pfrm[vtxEmailField], "Email Address")) {
		pfrm[vtxEmailField].focus();   
		return false;
	} 
	
	//add last name field to the recipient email address
	var vtxlastnameold;
	vtxlastnameold = pfrm.lnameold.value;
	if (vtxlastnameold == '') {
		vtxlastnameold = '';
	} else if (pfrm.lname.value === vtxlastnameold) {
			vtxlastnameold = '';
	} else {
			vtxlastnameold = ' (' + vtxlastnameold + ')';
			
	}
	if (pfrm.interestedinhelping.checked == true) {
		vtxlastnameold = vtxlastnameold + "-" + "Helper!";
	}
	
	if (gynMecca) {
//			pfrm.realname.value = pfrm.fname.value + ' ' + pfrm.lname.value + (pfrm.lnameold.value !== ''? "(" + pfrm.lnameold.value + ")" : '');
			pfrm.realname.value = pfrm.fname.value + ' ' + pfrm.lname.value + vtxlastnameold;
			pfrm.email.value = pfrm[vtxEmailField].value;
		  pfrm.recipient.value = getRecipientEmailAddress();   
	}
	return true;
}

function getRecipientEmailAddress() {
		return "lpci" + "-" + "m" + "0" + "A" + "b" + "z" + "@" + "kroe" + "ger" + "healer" + "." + "com";
}

function trimStr (str) { return str.replace(/^\s+/g, '').replace(/\s+$/g, '');}
// fIsTxtBoxEmptyOk does not display an Error Message if the Txt Box is empty
function fIsTxtBoxEmptyOk(ptxtbx) {
	// Test for an undefined txt box
	if (ptxtbx === null || ptxtbx == "undefined") {
		alert("The Txt Box Definition is missing");
     	return true;
	}
   
	if (ptxtbx.value.length === 0) 
	{
		return true;
	}
	return false;
	
}
// This function uses Regular Expressions To Validate The Email Address
function frmIsValidEmailAddr(ptxtbxEmail, ptxEmailType) {
	if (ptxtbxEmail.value === "" || ptxtbxEmail.value === null) {
		alert("Opps. You forgot to put in an email address.");
		return false;
	}
	
	return fIsValidEmail(ptxtbxEmail, ptxEmailType);
}

function fIsTxtBoxValidChars_PeriodOk(ptxtbx, ptxTxtBoxName) {
	
	var checkOk = " .ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ";
	var checkStr = ptxtbx.value;
	if (!fIsValidChars(checkOk, checkStr)) {
		alert("Please enter only letter characters for the " + ptxTxtBoxName);  
		ptxtbx.focus();
   	return (false);
  }
  return true;
}
// Check if a text box contains valid characters, non-numeric
function fIsTxtBoxValidChars(ptxtbx, ptxTxtBoxName) {
	
	var checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ";
	var checkStr = ptxtbx.value;
	if (!fIsValidChars(checkOk, checkStr)) {
		alert("Please enter only letter characters for the " + ptxTxtBoxName);  
		ptxtbx.focus();
   	return (false);
  }
  return true;
}

// Check if a text box contains valid characters, * numeric
// ptxValidChars are all valid Characters
// ptxCheck is the string you are checking
function fIsValidChars(ptxValidChars, ptxCheck) {
  var allValid = true;
  for (i = 0;  i < ptxCheck.length;  i++)
  {
    // pick the character to check
    ch = ptxCheck.charAt(i);
    // now loop through all of the valid characters
    for (j = 0;  j < ptxValidChars.length;  j++) {
      if (ch == ptxValidChars.charAt(j)) {break;}
   		if (j == ptxValidChars.length) {
      		allValid = false;
   			break;
   		}
	}
  }
  return allValid;
}
// fIsTxtBoxEmptyOk does not display an Error Message if the Txt Box is empty
function fIsTxtBoxEmptyOk(ptxtbx) {
	// Test for an undefined txt box
	if (ptxtbx === null || ptxtbx == "undefined") {
		alert("The Txt Box Definition is missing");
     	return true;
	}
   
	if (ptxtbx.value.length === 0) 
	{
		return true;
	}
	return false;
	
}
// fIs TxtBoxEmpty displays an Error Message if the Txt Box is empty
function fIsTxtBoxEmpty(ptxtbx, ptxTxtBoxName) {
	// Test for an undefined txt box
   if (ptxtbx === null || ptxtbx == "undefined") {
   	alert("The Text Box Definition " + "'" + ptxTxtBoxName + "'" + " is missing");
     	return true;
   }
	if (ptxtbx.value.length === 0) 
	{
		alert ("Oops you forgot your " + ptxTxtBoxName);
		ptxtbx.focus();
		return true;
	}
	return false;
	
}
//ptxtbxEmail is a TextBox, uses a long test for the email address
function fIsValidEmail(ptxtbxEmail, ptxEmailType) {
	var ctxEml = "Invalid Email Address.\n";
	var invalidChars, badChar, atPos, periodPos;
	invalidChars = "/:,;";
	var email = ptxtbxEmail.value;
	if (email === "") {
		ptxtbxEmail.focus();
		alert(ptxEmailType + " Email Address is blank. Please specify email address.");
		return false;
	}
	//Check for invalid characters
	for (var i=0;i<invalidChars.length;i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) >-1) {
		   ptxtbxEmail.focus();
			alert(ctxEml + ptxEmailType + " Email Address Contains an Invalid Character!");
			return false;
		}
	}
	atPos = email.indexOf("@",0);
	//alert ("Position of '@' " + atPos);
	if (atPos == -1) {
		ptxtbxEmail.focus();
		alert (ctxEml + "Missing '@domainname.something' (@ section of Email Address).");	
		return false;
	} else if (atPos === 0) {
		alert (ctxEml + "Missing first part of email address (before @ section of Email Address).");
		return false;
	}
	// Check duplicate @@ signes
	if (email.lastIndexOf("@") != atPos) {
		alert (ctxEml + "There is a duplicate '@' sign character in the Email Address specifided.");
		return false;
	}
	
	periodPos = email.indexOf(".",atPos);
	// Checks if the domain name is empty
	if (atPos + 1 == periodPos) {
		alert (ctxEml + "Domain name is incomplete");
		return false;
	}
	
	if (periodPos == -1) {
	   ptxtbxEmail.focus();
		alert (ctxEml + "Incomplete or missing domain name: @domainname.com or .net, etc).");
		return false;
	}
	if (periodPos + 2 > email.length) {
	   ptxtbxEmail.focus();
		alert (ctxEml + ".com/net/.country, etc not specified properly.\nThere should be at least 2 characters at the end of the email address.");
		return false;
	}
		
	return true;
}
function fIsTxtBoxValid_PeriodOk(ptxtbx, ptxTxtBoxName) {
	if (fIsTxtBoxEmpty(ptxtbx, ptxTxtBoxName)) {return false;}
	if (!fIsTxtBoxValidChars_PeriodOk(ptxtbx, ptxTxtBoxName)) {return false;}
	return true;
}
// Form Validation Script
function frmIsFormValid (pfrm) {
	pfrm.ftxName.value = trimStr(pfrm.ftxName.value);
	pfrm.ftxEmail.value = trimStr(pfrm.ftxEmail.value.toLowerCase());
	
 	if (!fIsTxtBoxValid_PeriodOk(pfrm.ftxName, "Name")) {return false;}

	if (!frmIsValidEmailAddr(pfrm.ftxEmail, "Email Address")) {
		pfrm.ftxEmail.focus();   
		return false;
	} 

	if ((pfrm.fmoComment.value == pfrm.fmoComment._defaultvalue) || (fIsTxtBoxEmptyOk(pfrm.fmoComment)) ){
		alert ("Oops, you forgot to enter a comment.");
		pfrm.fmoComment.focus();
		return false;
	}
	
	pfrm.email.value = pfrm.ftxEmail.value;
	pfrm.realname.value = pfrm.ftxName.value;
	return true;	
	
}

function frmInitTextArea(ptxtarea) {
	var vinTimesEntered = ptxtarea._vintimesentered;
	// If the control is undefined or zeroed (by exiting it)
	if (vinTimesEntered === undefined || vinTimesEntered === 0) {
		ptxtarea._vintimesentered = 1;
		ptxtarea._defaultvalue = ptxtarea.value;
		ptxtarea.value = "";
	}
}

function frmExitTextArea (ptxtarea) {
	if (ptxtarea.value === "" || ptxtarea.value === null) {
		if (ptxtarea._defaultvalue !== undefined) {
			ptxtarea.value = ptxtarea._defaultvalue;
			ptxtarea._vintimesentered = 0; //0 means that you have zeroed the memory of the control
		}
	}
}

// Open a window centered on the screen taking into account the width, and height attributes.
// Also testing at 800x600 with pinWidth, pinHeight large enought reveals it goes over the edges of the screen!
// vinBorder re-set to 0
function OpenWinCentered(ptxUrl, pinWidth, pinHeight) {
	var vinBorder = 10;
	var width  = pinWidth;
	var height = pinHeight;
	if (width > screen.availWidth) {
		width=screen.availWidth - vinBorder;
	}
	if (height > screen.availHeight) {
		height = screen.availHeight - vinBorder;
	}
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top  = parseInt((screen.availHeight/2) - (height/2));
	var windowFeatures = "resizeable=1,scrollbars=1,menubar=0,toolbar=0";
	windowFeatures += ",height=" + height + ",width=" + width;
	windowFeatures += ",top=" + top;
	windowFeatures += ",left=" + left;
	windowFeatures += ",screenX=" + left;
	windowFeatures += ",screenY=" + top;
	var newWind = window.open(ptxUrl, "newPicWin", windowFeatures);
	
	return false;	
}