// Controls for jump menu 


function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

//
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//hide item by id
function hideRow(id) {
var myRow = document.getElementById(id);
myRow.style.display = "none";
}

/*  //alternative version of hideRow(id)
function hideRow(id){ 
  // Toggle visibility between none and inline 
  var myRow = document.getElementById(id);
  if ((myRow.style.display == 'none')) 
  { 
	myRow.style.display == ''; 
  } else { 
	myRow.style.display == 'none'; 
  } 
} 
*/

//show item by id
function showRow(id) {
var myRow = document.getElementById(id);
myRow.style.display = "";
}

/*  //alternative version of showRow(id)

function showRow(id){ 
  // Toggle visibility between none and inline 
  var myRow = document.getElementById(id);
  if ((myRow.style.display == '')) 
  { 
	myRow.style.display == 'none'; 
  } else { 
	myRow.style.display == ''; 
  } 
} 
*/

//show / hide item by id
function toggle_row(id) {
	var tr = document.getElementById(id);   
	if (tr==null) { return; }  
	var myToggleRow = tr.style.display == '';
	tr.style.display = (myToggleRow ? 'none' : '');
}

/* //alternative version of toggle_row(id)

//show / hide item by id
function toggle_row(id) {
	var myRow = document.getElementById(id);
	if(myRow.style.display == 'none')
	myRow.style.display = 'block';
	else
	myRow.style.display = 'none';
}

*/

function toggleText(id,text1,text2) 
{
	var DomObject = document.getElementById(id);
	DomObject.innerHTML = (DomObject.innerHTML == text1) ? text2 : text1;
}


/* This function controls itemType drop-down menu on the reserve_step2.php page */ 
//This code is for the libraryForm.html page only
function setFormChange(){
	
	var article = document.getElementById("myArticle");
	var book = document.getElementById("myBook");
 	
	var myChangeIndex  = document.libraryForm.itemType.selectedIndex;
	var SelChangeValue = document.libraryForm.itemType.options[myChangeIndex].id;
	
	formswitch = myChangeIndex;
	//formswitch = SelChangeValue;

	switch (formswitch)
	{											
											
		case 1:
		//case "Article":									
			//alert("This is article; myChangeIndex: " + myChangeIndex + "; SelChangeValue: " + SelChangeValue);
			article.style.display = "";
			book.style.display = "none";   			
			break;
											
		case 2:
		//case "Book Chapter":									
			//alert("This is book chapter; myChangeIndex: " + myChangeIndex + "; SelChangeValue: " + SelChangeValue);
			article.style.display = "none";
			book.style.display = "";                
			break;

		default:
			//alert("This is none of the above; myChangeIndex: " + myChangeIndex + "; SelChangeValue: " + SelChangeValue);												
			article.style.display = "none";
			book.style.display = "none";                
		   break;
	} 
	window.onload=setFormChange;
}

// JavaScript Document

function echeck(str) {
	
     // Check for correct email address length
	 myValidEMAIL = new RegExp(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/);

	if (!myValidEMAIL.test(str)) 
	{
		alert("Invalid E-mail Address. Please try again.");
		return false;
	}
	return true;
}

	
/*  alternative, but not as effective, email checking code

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1)
		{
		   alert("Invalid E-mail Address");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		{
		   alert("Invalid E-mail Address");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		{
		    alert("Invalid E-mail Address");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1)
		 {
		    alert("Invalid E-mail Address");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		 {
		    alert("Invalid E-mail Address");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1)
		 {
		    alert("Invalid E-mail Address");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1)
		 {
		    alert("Invalid E-mail Address");
		    return false;
		 }
	}
 		 return true;	
*/		 


function isNumberValid(str) 
{
     // Check for correct phone number
	 myValidNumber = new RegExp(/^\d{3}\-\d{3}\-\d{4}$/);
     // alt 2: myValidNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
 
     if (!myValidNumber.test(str)) 
	 {
		alert("The number must be entered in the following format: 555-555-5555");
		// alt 2: alert("The number must be entered in the following format:: (555) 555-5555");
		return false;
     }
 
	return true;
}


function ValidateForm(){

	var Name = document.getElementById("Name");
			
		if (Name.value.length < 3) 
		{	
			alert("Please enter a Name.");
			Name.focus();
			return false;
		}
	//check to see whether FirstName is less than three characters in length
	var FirstName = document.getElementById("FirstName");
			
		if (FirstName.value == null)
		{
			return true;
		}
		else
		(FirstName.value.length < 1) 
		{	
			alert("Please enter a First Name.");
			FirstName.focus();
			return false;
		}

	//check to see whether LastName is less than three characters in length
	var LastName = document.getElementById("LastName");
			
		if (LastName.value.length < 3) 
		{	
			alert("Please enter a Last Name.");
			LastName.focus();
			return false;
		}

	//check to see whether EMailAddress is of valid character lengths
	var EMailAddress = document.getElementById("E-Mail");
			
		if ((EMailAddress.value==null)||(EMailAddress.value==""))
		{
			alert("Please Enter your E-mail Address.")
			EMailAddress.focus();
			return false;
		}
		if (echeck(EMailAddress.value)==false)
		{
			EMailAddress.value="";
			EMailAddress.focus();
			return false;
		}	

	//check to see whether Phone is of valid formatting
	var Phone = document.getElementById("Phone");
			
		if ((Phone.value==null)||(Phone.value==""))
		{
			alert("Please Enter your Phone Number.");
			Phone.focus();
			return false;
		}
		if (isNumberValid(Phone.value)==false)
		{
			Phone.value="";
			Phone.focus();
			return false;
		}	


	//check to see whether Fax has length; if so, check for valid formatting
	var Fax = document.getElementById("Fax");
		
		//debug :: alert("Fax.value.length: " + Fax.value.length);
		if ((Fax.value.length > 0))
		{			
			if (isNumberValid(Fax.value)==false)
			{
				Fax.value="";
				Fax.focus();
				return false;
			}			
		}

	//check to see whether Username is less than three characters in length
	var Username = document.getElementById("Username");
			
		if (Username.value.length < 3) 
		{	
			alert("Please enter a Username.");
			Username.focus();
			return false;
		}

	//check for matching passwords
	var notValid = " "; // Spaces are not valid
	var Password1 = document.getElementById("Password1");
	var Password2 = document.getElementById("Password2");
	
	/*other variables to be added later
	var minLength = 6; // Minimum length of password
	*/
	
	// check for a value in both fields.
	if (Password1.value == '' || Password2.value == '') 
	{
		alert('Please enter your password.');
		Password1.value="";
		Password2.value="";
		Password1.focus();		
		return false;
	}
	
	// check for spaces in password fields
	if (Password1.value.indexOf(notValid) > -1 || Password2.value.indexOf(notValid) > -1) 
	{
		alert("Spaces are not allowed. Please try again.");
		Password1.value="";
		Password2.value="";
		Password1.focus();
		return false;
	}
	else 
	{
		if (Password1.value != Password2.value) 
		{
			alert ("Your password entries do not match. Please try again.");
			Password1.value="";
			Password2.value="";
			Password1.focus();			
			return false;
		}
	}
	/* length may be added later
	// check for minimum length
	if (document.myForm.password.value.length < minLength) {
	alert('Your password must be at least ' + minLength + ' characters long. Try again.');
	return false;
	}
	*/
	var Comments = document.getElementById("Comments");
			
		if (Comments.value.length < 3) 
		{	
			alert("Please enter a Comment.");
			Comments.focus();
			return false;
		}

	//check to see whether Mailing/Billing Address: is less than three characters in length
		var SAddress = document.getElementById("SAddress");
				
			if (SAddress.value.length < 3) 
			{	
				alert("Please enter a Mailing/Billing Address.");
				SAddress.focus();
				return false;
			}		

		//check to see whether Institution/Organization is less than three characters in length
		var SAddress2 = document.getElementById("SAddress2");
				
			if (SAddress2.value.length < 3) 
			{	
				alert("Please enter a Institution/Organization.");
				SAddress2.focus();
				return false;
			}

		//check to see whether City is less than three characters in length
		var SCity = document.getElementById("SCity");
				
			if (SCity.value.length < 3) 
			{	
				alert("Please enter a City.");
				SCity.focus();
				return false;
			}
				
		//check to see whether State is less than three characters in length
		var SState = document.getElementById("SState");
				
			if (SState.value.length < 2) 
			{	
				alert("Please enter a State.");
				SState.focus();
				return false;
			}

		//check to see whether Zip Code is less than three characters in length
		var SZip = document.getElementById("SZip");
				

			if (SZip.value.length < 3) 
			{	
				alert("Please enter a Zip Code.");
				SZip.focus();
				return false;
			}	
				
		if(true)
		{
			//prompt users that username will undergo a 2nd check of database
			alert('Valid data. Please click the \'Send My Comment\' button to complete this process.');
							
			//how submit button
			var SubmitButton=document.getElementById("SubmitButton");
			SubmitButton.style.display = "";
			
			//hide validation button
			var ValidateButton=document.getElementById("ValidateButton");
			ValidateButton.style.display = "none";				
		}
		return true;
 }

