/*functions for general form validation*/

//check that input is an int
function isInt(strng, textName)
{
	var error = "";
	var ValidChars = "-0123456789";
	var isInt=true;
	var Char;
	
	
	for (i = 0; i < strng.length && isInt == true; i++) 
	{ 
		Char = strng.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			isInt = false;
		}
	}
	if (!(isInt))
	{
		error = "Please make sure that " + textName + " is an int.\n";
	}
	return error;
}

// check that email was entered and is in the correct format
function checkEmail (strng) 
{
	var error="";
	if (strng == "") 
	{
		error = "Please enter your email address.\n";
	} else 
	{
		var emailFilter=/^.+@.+\..{2,3}$/;
		if (!(emailFilter.test(strng))) 
		{ 
			error = "Please enter a valid email address.\n";
		} else 
		{
			//test email for illegal characters
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
			if (strng.match(illegalChars)) 
			{
			   error = "The email address contains illegal characters.\n";
			}
	    }
	}
	return error;    
}

// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng)
{
	var error = "";
	if (strng == "") {
	   error = "Please enter your home phone number.\n";
	} else {
		var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	    if (isNaN(parseInt(stripped))) {
	       error = "The home phone number contains illegal characters.";
	    }
	    if (!(stripped.length == 10)) {
		error = "The home phone number is the wrong length. Please make sure you included an area code.\n";
	    } 
	}
	return error;
}

// non-empty form elements
function isEmpty(strng, textName) 
{
	var error = "";
	if (strng.length == 0) 
	{
		error = "Please enter " + textName + ".\n";
	}
	return error;	  
}

// check that length of form field input does not exceed specified limit
function checkInputLength(strng, maxLength, fieldName)
{
	var error = "";
	if (strng.length > maxLength)
	{
		error = "The text you've entered for '"+fieldName+"' is too long.\n";
	}
	return error;
}

// check that date is in a format SQL can use
function checkSQLDate(strng, fieldName)
{
	var error = "";
	if (strng.search(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}/) == -1)
	{
		error = "Incorrect date format on '"+fieldName+"'\n";
	}
	return error;
}

// check that a radio button was selected
function checkRadioSelected(radioCount, form, fieldName, fieldText) 
{
	var error = "";
	var selected = false;
	for (var i=0; i < radioCount; i++) 
	{
		evalField = "document."+form+"."+fieldName+"["+i+"].checked";
		if (eval(evalField))
		{
			selected = true;
		}
	}
	if (!selected)
	{
		error = "Please make a selection in "+fieldText+".\n";
	}
	return error;
}

//check that a checkbox was selected
function checkCheckBoxSelected(fieldArray, form, fieldText)
{
	var error = "";
	var atLeastOne = false;
	for (index in fieldArray)
	{
		fieldName = "document."+form+"."+fieldArray[index]+".checked";
		if (eval(fieldName))
		{
			atLeastOne = true;
		}
	}
	if (!(atLeastOne))
	{
		error = "Please make a selection in "+fieldText+".\n";
	}
	return error;
}

//check that urls for database insertion conform to data integrity standards for local links (no leading slash, etc.) or to general standards for offsite links
function wellFormedLocation(strng, onsiteBln)
{
	var error = "";
	if (onsiteBln)//check to see if the location was marked as offsite
	{
		if (strng.charAt(0) == '/')
		{
			error = "Please remove the slash from the start of 'Location'.\n";
		}
		return error;
	} else 
	{
		if ((strng.search(/http:\/\//) == -1) && (strng.search(/https:\/\//) == -1))
		{
			error = "Please include 'http://' or 'https://' in the location for offsite locations.\n";
		}
		return error;
	}
}
