// Automatically attach a listener to the window onload
addEvent(window,"load",clientSideInclude);


// Utility function to add an event listener
function addEvent(o,e,f){
	if (o.addEventListener){ o.addEventListener(e,f,true); return true; }
	else if (o.attachEvent){ return o.attachEvent("on"+e,f); }
	else { return false; }
}

function clientSideInclude() {
  // chkDiv will hide the plain form	
  chkDiv('plainForm','none');
  chkDiv('javaOnShow','block');
  var id = "includeone";
  var url = "includeForm.htm";
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
  var element = document.getElementById(id);
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
  if (quryStrng('dept')) {
	funcDepartment('lstDepartments');
  }
}

function validateForm(form) {
  var tempCharc = "";
  var tempPhone = "";
  var tempZip = "";
  
  if (form.txtName.value == "") {
	alert("Please enter your name.");
	form.txtName.focus();
	return false;
  }
  if (form.txtEmail.value == "") {
	alert("Please enter your email address.");
	form.txtEmail.focus();
    return false;
  }
  if (form.lstDepartments.value == "") {
	alert("Please select a choice from the department list.");
    return false;
  } else {
    switch (form.lstDepartments.value){
  	  case "archdesk":
		if ((form.lstStateAgencyArch.value == "block") && (form.txtAgencyNameArch.value == "")) {
			alert("If you are from a state agency please supply the name of that agency.");
			form.txtAgencyNameArch.focus();
			return false;
		}
		if (form.arcAddress.value == "") {
			alert("Please enter your street address.");
			form.arcAddress.focus();
			return false;
		}
		if (form.arcCity.value == "") {
			alert("Please enter your city.");
			form.arcCity.focus();
			return false;
		}
		for (i=0; i<form.arcZip.value.length; i++) {
    		tempCharc = form.arcZip.value.substr(i,1);
			if (tempCharc >= 0 || tempCharc <= 9) {
				tempZip = tempZip + tempCharc;
			}
		}
		for (i=0; i<form.arcZip.value.length; i++) {
			tempZip = tempZip.replace(" ","");
		}
		if (tempZip == "") {
			alert("Please enter your zip code.");
			form.arcZip.focus();
			return false;
		}
		else if (tempZip.length < 5) {
			alert("Please check your zip code. It does not contain enough numbers.");
			form.arcZip.focus();
			return false;
		}
		else if (tempZip.length > 9) {
			alert("Please check your zip code. It contains to many numbers.");
			form.arcZip.focus();
			return false;
		}
		for (i=0; i<form.arcPhone.value.length; i++) {
			tempCharc = form.arcPhone.value.substr(i,1);
			if (tempCharc >= 0 || tempCharc <= 9) {
			  tempPhone = tempPhone + tempCharc;
			}
	    }
		for (i=0; i<form.arcPhone.value.length; i++) {
			tempPhone = tempPhone.replace(" ","");
  		}
		if (tempPhone.value == "") {
			alert("Please enter your telephone number.");
			form.arcPhone.focus();
			return false;
	  	}
	  	else if (tempPhone.length < 7) {
			alert("Please check your Phone Number. It does not contain enough numbers.");
			form.arcPhone.focus();
    		return false;
  		}
  		else if (tempPhone.length > 10) {
			alert("Please check your Phone Number. It contains to many numbers.");
			form.arcPhone.focus();
    		return false;
  		}
		if (form.arcRequestSubject.value == "") {
			alert("Please supply a subject");
			form.arcRequestSubject.focus();
			return false;
		}
		if (form.arcRequestInfo.value == "") {
			alert("Please tell us how we can help you");
			form.arcRequestSubject.focus();
			return false;
		}
		break;
	  case "refdesk":
		if ((form.lstStateAgency.value == "block") && (form.txtAgencyName.value == "")) {
			alert("If you are from a state agency please supply the name of that agency.");
			form.txtAgencyName.focus();
			return false;
		}
		for (i=0; i<form.refPhone.value.length; i++) {
			tempCharc = form.refPhone.value.substr(i,1);
			if (tempCharc >= 0 || tempCharc <= 9) {
			  tempPhone = tempPhone + tempCharc;
			}
	    }
		for (i=0; i<form.refPhone.value.length; i++) {
			tempPhone = tempPhone.replace(" ","");
  		}
		if (tempPhone.value == "") {
			alert("Please enter your telephone number.");
			form.refPhone.focus();
			return false;
	  	}
	  	else if (tempPhone.length < 7) {
			alert("Please check your Phone Number. It does not contain enough numbers.");
			form.refPhone.focus();
    		return false;
  		}
  		else if (tempPhone.length > 10) {
			alert("Please check your Phone Number. It contains to many numbers.");
			form.refPhone.focus();
    		return false;
  		}
		break;
	  case "suggest":		
		if (form.sugTitle.value == "") {
			alert("Please supply the title of the item you are suggesting.");
			form.sugTitle.focus();
    		return false;
  		}
		break;
	  case "tours":		
		for (i=0; i<form.turPhone.value.length; i++) {
			tempCharc = form.turPhone.value.substr(i,1);
			if (tempCharc >= 0 || tempCharc <= 9) {
			  tempPhone = tempPhone + tempCharc;
			}
	    }
		for (i=0; i<form.turPhone.value.length; i++) {
			tempPhone = tempPhone.replace(" ","");
  		}
		if (tempPhone.value == "") {
			alert("Please enter your telephone number.");
			form.turPhone.focus();
			return false;
	  	}
	  	else if (tempPhone.length < 7) {
			alert("Please check your Phone Number. It does not contain enough numbers.");
			form.turPhone.focus();
    		return false;
  		}
  		else if (tempPhone.length > 10) {
			alert("Please check your Phone Number. It contains to many numbers.");
			form.turPhone.focus();
    		return false;
  		}
		break;
	  case "toursPoe":		
		for (i=0; i<form.turPhone.value.length; i++) {
			tempCharc = form.turPhone.value.substr(i,1);
			if (tempCharc >= 0 || tempCharc <= 9) {
			  tempPhone = tempPhone + tempCharc;
			}
	    }
		for (i=0; i<form.turPhone.value.length; i++) {
			tempPhone = tempPhone.replace(" ","");
  		}
		if (tempPhone.value == "") {
			alert("Please enter your telephone number.");
			form.turPhone.focus();
			return false;
	  	}
	  	else if (tempPhone.length < 7) {
			alert("Please check your Phone Number. It does not contain enough numbers.");
			form.turPhone.focus();
    		return false;
  		}
  		else if (tempPhone.length > 10) {
			alert("Please check your Phone Number. It contains to many numbers.");
			form.turPhone.focus();
    		return false;
  		}
		break;
	  default : 
		if (form.defPhone.value == "") {
		  alert("Please enter you telephone number");
		  form.defPhone.focus();
		  return false;
		}
		if (form.defRequestInfo.value == "") { 
		  alert("Please enter your request");
		  form.defRequestInfo.focus();
		  return false;
		}
  	} //end lstDepartments switch
  } //end if/else lstDepartments == ""
 
  //all tests passed return true
  return true;
} //end function validate

function funcTopics(x)
{
  var w;
  var h;
  var p;
  p='quickHelp/' + document.getElementById(x).value + '.asp';
  // deliver canned response popUp when appropriate
  switch(document.getElementById(x).value) {
	case "directions":
	  w=730;
	  h=730;
	  window.open(p,'quickHelp','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width='+w+',height='+h);
	  break;    
	case "shop":
	  w=695;
	  h=530;
	  window.open(p,'quickHelp','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width='+w+',height='+h);
	  break;
	case "archivesResearch":
	  chkDiv('arcRes','block');
	  break;
	default:
	  // standard topics
  }
} //end function funcTopics

function funcDepartment(elemID) {
  switch(document.getElementById(elemID).value) {
    case "refdesk":
	  chkDiv('divRef','block');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','none');
	  break
	case "archdesk":
	  chkDiv('divRef','none');
	  chkDiv('divArc','block');
  	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','none');
	  break
	case "ill":
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','block');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','block');
	  break
	case "picture":
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','block');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','block');
	  break
	case "suggest":
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','block');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','none');
	  break
	case "tours":
	  if (quryStrng('dept') == 'poe') {
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','block');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','none');
	  break
	  }
	  else {
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','block');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','none');
	  break
	  }	
	case "toursPoe":
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','block');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','none');
	  break
	case "education":
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','block');	  
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','block');
	  break
	case "hr":
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','block');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','block');
	  break
	case "foundation":
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','block');
	  chkDiv('divDef','block');
	  break
	default:
	  chkDiv('divRef','none');
	  chkDiv('divArc','none');
  	  chkDiv('divTur','none');
	  chkDiv('divPoe','none');
	  chkDiv('divEdu','none');
	  chkDiv('divILL','none');
	  chkDiv('divSpe','none');
	  chkDiv('divSug','none');
	  chkDiv('divHum','none');
	  chkDiv('divFnd','none');
	  chkDiv('divDef','block');
	  break
  }
  //call to update the topics list, removed for ver.1
  //updateTopicsListMenu(deptSelected);
} //end function funcDepartment()

function chkDiv(id, view) {
  var div
  
  if(document.getElementById) {
    div = document.getElementById(id);
  }
  if(div) {
    div.style.display = view;
  }

} //end function chkDiv


function updateTopicsListMenu(deptSelected) {
  nullOptions('lstTopics');
  
  switch(document.getElementById(deptSelected).value) {
    case "refdesk@lva.virginia.gov":
	  document.getElementById("lstTopics").options[0] = new Option("Please Select a topic", ""); 
	  document.getElementById("lstTopics").options[1] = new Option("Test1", "Test Value"); 
	  document.getElementById("lstTopics").options[2] = new Option("Test2", "Test Value");
	  document.getElementById("lstTopics").options[3] = new Option("Not Listed", "other"); 
	  break
	case "archdesk@lva.virginia.gov":
	  document.getElementById("lstTopics").options[0] = new Option("Test1", "Test Value"); 
	  document.getElementById("lstTopics").options[1] = new Option("Test2", "Test Value");
	  break
    default:
	  document.getElementById("lstTopics").options[0] = new Option("Please Select a topic", ""); 
	  document.getElementById("lstTopics").options[1] = new Option("Directions", "directions");
	  document.getElementById("lstTopics").options[2] = new Option("Circulation", "circulation");
	  document.getElementById("lstTopics").options[3] = new Option("Interlibrary Loan", "ill");
	  document.getElementById("lstTopics").options[4] = new Option("The Virginia Shop", "shop");
	  document.getElementById("lstTopics").options[5] = new Option("LVA Foundation", "foundation");
	  document.getElementById("lstTopics").options[6] = new Option("Archives Research", "archivesResearch");
	  break
  }
} //end function updateTopicsListMenu()

function nullOptions(lstName) {
  var list;
  list = document.getElementById(lstName);
  var count=list.options.length;
  for (i=0;i<count;i++)
  {
	list.options[i]=null;
  }
  list.options.length=0;
} //end function nullOptions()

function quryStrng(uri) {
  uri = uri.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+uri+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
} //end quryStrng()