// This function is called when the user tries to do a name search. It checks to make sure that the 
// user enter either a first or last name and selected a state.
function validatefrmNameSearch(){
	var bFlag = 0;
	var sSearchLastName;
	var sSearchState;

	sSearchLastName = document.frmNameSearch.sSearchLastName.value;
	sSearchState = document.frmNameSearch.sSearchState.options[document.frmNameSearch.sSearchState.selectedIndex].value;
	
	if ((sSearchLastName == "") || (sSearchLastName == " ") || (sSearchLastName == null))
	{
		alert("ERROR: Please enter a last name to search for.");
		document.frmNameSearch.sSearchLastName.focus();
		bFlag = 1;
	}	
	
	else if ((sSearchState == " ") || (sSearchState == "") || (sSearchState == null)|| (sSearchState == "Choose"))
	    
	{
		alert("ERROR: Please Select A State");
		document.frmNameSearch.sSearchState.focus();
		bFlag = 1;
	}	
			
	if (bFlag == 0){
		document.frmNameSearch.submit();
	}
}

function validatefrmAddressSearch(){
	var bFlag = 0;
	var sSearchAddress = document.frmAddressSearch.sSearchAddress.value;
	var sSearchCity = document.frmAddressSearch.sSearchCity.value;
	var sSearchState = document.frmAddressSearch.sSearchState.options[document.frmAddressSearch.sSearchState.selectedIndex].value;
	var sSearchZip = document.frmAddressSearch.sSearchZip.value;
	var sSearchRadius = document.frmAddressSearch.sSearchRadius.value;
	
	// Validate that a state was selected
	if ((sSearchState == " ") || (sSearchState == "")  || (sSearchState == null) || (sSearchState == "Choose"))
	{
		alert("ERROR: Please Select A State");
		document.frmAddressSearch.sSearchState.focus();
		bFlag = 1;
	}	
	
	// Validate that Zip Code is a valid zip code
	//else
	if(isValid(sSearchZip) == false)
	{
		alert("ERROR: Zip Code can not contain letters.");
		document.frmAddressSearch.sSearchZip.value = "";
		document.frmAddressSearch.sSearchZip.focus();
		bFlag = 1;
	}
	
	// Validate that Search Radius is numeric and not null
	else if (isNaN(sSearchRadius)) 
	{
		alert("ERROR: The Search Radius must be a number.");
		document.frmAddressSearch.sSearchRadius.focus();
		bFlag = 1;
	}
	
	else if (sSearchRadius == null || sSearchRadius == "") 
	{
		alert("ERROR: The Search Radius can not be blank.");
		document.frmAddressSearch.sSearchRadius.focus();
		bFlag = 1;
	}	
	
	sSearchRadius = parseInt(sSearchRadius);
	if (sSearchRadius > 100 && bFlag == 0)
	{
		alert("ERROR: The Search Radius must be less than 100 miles.");
		document.frmAddressSearch.sSearchRadius.focus();
		bFlag = 1;
	}
	
	if (bFlag == 0){
		document.frmAddressSearch.submit();
	}
	
}

function validatefrmAddressEntry(sReferer) {
	var bFlag = 0;
	var sAddress = document.frmAddressEntry.sSearchAddress.value;
	var sCity = document.frmAddressEntry.sSearchCity.value;	
	if (sReferer == "NEWADDRESS") {
		var sState = document.frmAddressEntry.sSearchState.options[document.frmAddressEntry.sSearchState.selectedIndex].value;
	} else {
		var sState = document.frmAddressEntry.sSearchState.value;
	}

	var sZip = document.frmAddressEntry.sSearchZip.value;

	if ((sAddress == "") || (sAddress == null) || (sAddress == " ")) {
		alert("ERROR: You must enter an address");
		document.frmAddressEntry.sSearchAddress.focus();
		bFlag = 1;
	}	
	else if ((sCity == "") || (sCity == null) || (sCity == " ")) {
		alert("ERROR: You must enter a city");
		document.frmAddressEntry.sSearchCity.focus();	
		bFlag = 1;
	}
	else if ((sState == "") || (sState == null) || (sState == " ")) {
		alert("ERROR: You must enter a state");
		document.frmAddressEntry.sSearchState.focus();
		bFlag = 1;
	}
	else if ((sZip == "") || (sZip == null) || (sZip == " ")) {
		alert("ERROR: You must enter a zip code");
		document.frmAddressEntry.sSearchZip.focus();
		bFlag = 1;
	}
	else if (isValid(sZip) == false) {
		alert("ERROR: Zip Code can not contain letters.");
		document.frmAddressEntry.sSearchZip.focus();
		bFlag = 1;
	}
	
	if (bFlag == 0)
	{
		document.frmAddressEntry.submit();
	}
	
}

function isValid(inputVal){
	var inputStr = inputVal.toString();
	for (var i=0; i<inputStr.length; i++) {
		var inputChar = inputStr.charAt(i);
		if(inputChar == "-" || (inputChar >= "0" && inputChar <= "9")){
			continue;
		} else {
			return false;
		}
	}
	return true;
}

function MM_swapImgRestore() { //v2.0
  if (document.MM_swapImgData != null)
    for (var i=0; i<(document.MM_swapImgData.length-1); i+=2)
      document.MM_swapImgData[i].src = document.MM_swapImgData[i+1];
}

function MM_preloadImages() { //v2.0
  if (document.images) {
    var imgFiles = MM_preloadImages.arguments;
    if (document.preloadArray==null) document.preloadArray = new Array();
    var i = document.preloadArray.length;
    with (document) for (var j=0; j<imgFiles.length; j++) if (imgFiles[j].charAt(0)!="#"){
      preloadArray[i] = new Image;
      preloadArray[i++].src = imgFiles[j];
  } }
}

function MM_swapImage() { //v2.0
  var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.MM_swapImgData;
  for (i=0; i < (MM_swapImage.arguments.length-2); i+=3) {
    objStr = MM_swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];
    if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
        (objStr.indexOf('document.all[')   ==0 && document.all   ==null))
      objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);
    obj = eval(objStr);
    if (obj != null) {
      swapArray[j++] = obj;
      swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];
      obj.src = MM_swapImage.arguments[i+2];
  } }
  document.MM_swapImgData = swapArray; //used for restore
}


// This function is used in the ANP entry form page. Whenever the user selects a customer supplied
// address to search by, this function is called and passed in the address information of the desired
// search address. The fields of the default form are then populated with these values and the form
// is submitted to the address search results page.
function fillFrmCustAddress(sAddress, sCity, sState, sZip) {
	document.frmCustAddresses.sSearchAddress.value = sAddress;
	document.frmCustAddresses.sSearchCity.value = sCity;
	document.frmCustAddresses.sSearchState.value = sState;
	document.frmCustAddresses.sSearchZip.value = sZip;
	document.frmCustAddresses.submit();
}

// This function is used by the results page to set the values in the Driving Directions form.
function fillFrmDrivingDirections(iAddressRecord, iAgentId, sAgentLat, sAgentLong, sBusinessName, sStreet, sCity, sState, sZip) {
	document.frmDrivingDirections.iAddressId.value = iAddressRecord;
	document.frmDrivingDirections.iAgentId.value = iAgentId;
	document.frmDrivingDirections.sAgentLat.value = sAgentLat;
	document.frmDrivingDirections.sAgentLong.value = sAgentLong;
	document.frmDrivingDirections.sBusinessName.value = sBusinessName;
	document.frmDrivingDirections.sStreet.value = sStreet;
	document.frmDrivingDirections.sCity.value = sCity;
	document.frmDrivingDirections.sState.value = sState;
	document.frmDrivingDirections.sZip.value = sZip;
	document.frmDrivingDirections.submit();
}
// This function is used by the results page to set the values in the Map It form.
function fillFrmMapIt(iAddressRecord, iAgentId) {
	document.frmMapIt.iAddressId.value = iAddressRecord;
	document.frmMapIt.iAgentId.value = iAgentId;
	document.frmMapIt.submit();
}
// This function is used by the results page to set the values in the Select Agent form.
function fillFrmSelectAgent(iAddressRecord, iAgentId) {
	document.frmSelectAgent.iAddressId.value = iAddressRecord;
	document.frmSelectAgent.iAgentId.value = iAgentId;
	document.frmSelectAgent.submit();
}
// This function is used by the map location page to set the values in the Navigate form.
function fillFrmNavigate(sCommand) {
	document.frmNavigate.sCommand.value = sCommand;
	document.frmNavigate.submit();
}

// This function is used by the map location page to set the values in the Navigate form for Zoom.
function fillFrmNavigateZoom(sZoom) {
	document.frmNavigate.sZoom.value = sZoom;
	document.frmNavigate.submit();
}

// Create a new window. URL path has been sent from the HREF tag. 
function newWindow(width, height, site, windowName, scrollBars) { 
	newWind = window.open(site, windowName, "directories=no,location=no,menubar=yes,resizable=yes,scrollbars=" + scrollBars + ",status=no,toolbar=no,width=" + width + ",height=" + height + ""); 

     // Take care of Navigator 2's special needs.
        if (newWind.opener == null) {
            newWind.opener = window;
        } 
}

// This function tests whether a child window is open or closed. 
// If it's already open, close it. If it hasn't been spawned yet, 
// create a new window. Code includes an ugly workaround for IE3. 
// URL path is sent from HREF tag.  
function popupWin(path, name, width, height, scroll) { 
        
	// Initialize global vars for new window object and the url path
	// so they can be accessed by all functions on the page.
	var newWind; 
	var path; 
	var site; 
	var windowName; 
	var newWidth; 
	var newHeight; 
	var scrollBars; 
	var launchWin; 

	// Set flag to help out with closing window for IE3.
 	var isIE3 = (navigator.appVersion.indexOf("MSIE 3") != -1) ? true : false;

	site = path; 
        name = windowName; 
        scrollBars = scroll; 
	newWidth = width; 
	newHeight = height; 

        if (isIE3) { 
             // For IE3, check if child window is already open. If so, 
             // nothing appears to happen. If the child window is 
             // closed, the subwindow flashes momentarily (yech!). 
             if (newWind.opener == null) {
                 newWind.opener = window; 
             } 
        }
        if (newWind && !newWind.closed) { 
             // If the child window already exists and if it hasn't
             // been closed yet, close it. Once closed, automatically 
             // spawn a new window with a different URL. 
                newWind.close() 
                launchWin = setTimeout("newWindow(newWidth, newHeight, site, windowName, scrollBars)",200); 
        }  
        else { 
             // Otherwise, no child window has been spawned yet, so 
             // execute the function that will create one. 
                newWindow(newWidth, newHeight, site, windowName, scrollBars); 
         } 
   }
