// File: funcs.js
// Paul Henderson, February, 2005
// Revised March, 2005 to add sverify and supporting functions


//*********** start of right click disable code ****************//
var message="";
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")

//*********** end of right click disable code ****************//


function sverify()
{
	var optval = document.search.optionval.value;

	switch(optval)
	{
		case "0":
		{
			alert("You must select one of the required options, for example the option 'Search for a Specific Name:'");
			return false;
		}
		break;	

		case "1":
		{
			var str = document.search.name.value
			var strlen = str.length;

			// check first to see if anything was entered, not including only blanks
			if ( strlen == 0 || blank_check(str, strlen) == false )
			{
				alert("No name was entered.");
				document.search.name.focus();
				return false;
			}
			else
			{
				//capture only the first word in the string - after any preceding blanks and ending with the first next blank
				//return false if more than one word

				var words = str.split(/\s+/);
				
				if (words.length > 1)
				{
					alert("The name to be searched for must consist of only 1 word.");
					document.search.name.focus();
					return false;				
				}
				else
				{
					strlen = words[0].length;
					if (strlen < 3 )
					{
						alert("The name to be searched for must consist be atleast 3 characters long.");
						document.search.name.focus();
						return false;
					}
					else
					{
						if ( invalidchar_check(str) == false )
							return false;
						else
							return true;
					}
				}
			}
		}
		break;	
	
		case "2":
		{
			if ( document.search.area.value == "#" )
			{
				alert("Please select one of the Areas of Interest from the drop down menu.");
				return false;
			}
			else
				return true;		
		}
		break;

		case "3":
		{
			if ( document.search.donor.value == "#" )
			{
				alert("Please select one of the Donors from the drop down menu.");
				return false;
			}
			else
				return true;		
		}
		break;

		case "4":
		{
			var str = document.search.fromyear.value
			var fromyear = str;
			var strlen = str.length;

			if ( yearcheck(str,strlen) )
			{
				var str = document.search.toyear.value
				var toyear = str;
				var strlen = str.length;
				
				if ( yearcheck(str,strlen) )
				{
					if ( fromyear > toyear )
					{
						alert("The 'from' year must be earlier than or the same as the 'through' year.");
						return false;
					}
					else
						return true;
				}
				else
					return false;
			}
			else
				return false;
		}
		break;
	}
}

//******** these functions are used by other functions *********//

function yearcheck(str,strlen)
{
	if (strlen == 0)
	{
		alert("Both the 'from' year and the 'through' year must be entered.");
		return false;
	}

	if (blank_check(str, strlen) == false)
	{
		alert("Both the 'from' year and the 'through' year must be entered.");
		return false;
	}

	if (  isNaN(str) || str <= 0 || (str % 1) != 0 )
	{
		alert("Both the 'from' year and the 'through' year must be integer values greater than zero.");
		return false;
	}

	if (strlen != 4)
	{
		alert("Both the 'from' year and the 'through' year must be in the format 'yyyy'; example: 1888.");
		return false;
	}

	return true;
}

function blank_check(str, strlen)
{  
	var i=0, flag=0;
   	var str, strlen;
   	var posvalue=" ";

   	for (i = 0; i < strlen; i++)
   	{  
		posvalue = str.substr(i,1);
      		if (posvalue != ' ') flag=1;	
   	}

   	if (flag == 1)
      		return true;
   	else
      		return false;
}

function invalidchar_check(str)
{ 
   	if ( str.match(/[\?\*\%\#\_[]/) != null )
   	{
		alert("One or more invalid characters ( ? * % # _ [ ) are being used in the name.\nPlease reenter a valid name.");
		return false;
   	}
	else
		return true;
}

function apos_check(strg)
{  var apos = "'", newstring = " ", posapos = 0, underscore = "_";
   posapos = strg.indexOf(apos);
   if (posapos > 0)				// apostrophe found
   {  newstring = strg.slice(0, posapos);
      newstring = newstring + underscore;
      newstring = newstring + strg.slice(posapos+1);
      return newstring;
   }
   else
      return strg;
}

