// functions used in contact.php

// check email address for ?@?.?? format
function ckEmail(y)
{
	var atLoc = y.indexOf("@",1);
	var dotLoc = y.indexOf(".",atLoc+1);
	var len = y.length;
	if (atLoc>0 && dotLoc>0 && len>dotLoc+2)
	{
    	return false; // eddress passed test
	}
	else
	{
	    return true;  // invalid form
	}
}

// check user inputs
function validate(x)
{
	var sEml = x.sEmail.value.toLowerCase();
	var sEml2 = x.sEmail2.value.toLowerCase();
	var targetStr1 = "<";
	var targetStr2 = "&lt;";
	var errorMsg = "";
  
	if (!x.sName.value)
	{
    	errorMsg += "\n - - your name";
	}

	if (!sEml)
	{
		errorMsg += "\n - - your email address";
	}
	else if (ckEmail(sEml))
	{
		errorMsg += "\n - - invalid format for your email address";
	}
	
	if (!sEml2)
	{
		errorMsg += "\n - - second email address";
	}
	else if (sEml && sEml != sEml2)
	{
		errorMsg += "\n - - email addresses do not agree";
	}

//  if (x.bestTime.value && !x.sPhone.value)
//   errorMsg += "\n - - your phone number";
   
	if (!x.msg.value)
	{
		errorMsg += "\n - - your message";
	}
	else
	{
	    var strFound1 = x.msg.value.indexOf(targetStr1);
    	var strFound2 = x.msg.value.indexOf(targetStr2);

	    if (strFound1 >= 0 || strFound2 >= 0)
    	{
	    	errorMsg += "\n - - message can not contain HTML";
	    }
	}

	if (!errorMsg == "")
	{
    	errorMsg = "Please complete or correct the following fields:\n"+errorMsg;
		window.alert(errorMsg);
		return false;
	}
	else
	{
		return true;
	}
}

// textarea length warning
function checkLength(lenLimit,len,countedTextarea,reporterInputId)
{
  var reporter = countedTextarea.form[reporterInputId];

	if (len <= (.9 * lenLimit)) /* up to 90% of limit, hide */
	{
		reporter.style.display = "none";
		reporter.style.background = "#002f55";
	}
	else if ((len > (.9 * lenLimit)) && (len <= lenLimit)) /* from 90% to 100% show counter */
	{
		reporter.style.display = "block";
		reporter.value = "Remaining characters: " + (lenLimit - len);
		reporter.style.background = "#fff";
		reporter.style.color = "#2053D2";

	}
	else /* over limit, show warning */
	{
		reporter.style.display = "block";
		reporter.value = "Over Limit by: " + (len - lenLimit) + " characters. Excess will be cropped.";
		reporter.style.background = "#fff";
		reporter.style.color = "#c30";
	}
}
