// JavaScript Document
var undefined; 

var whitespace = " \t\n\r";
var digits = "0123456789";
var USStateCodeDelimiter = "|";
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP";


function isEmpty(s)
{   
	return ((s == null) || (s.length == 0));
}

function isWhitespace(s)
{   
	var i;
	
    if (isEmpty(s))
    {
		return true;
	}
	
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1)
        {
			return false;
		}
    }
    return true;
}

function isDigit(c)
{   
	return ((c >= "0") && (c <= "9"));
}

function isInteger(s)
{   
	var i;
	
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c))
        { 
			return false;
		}
    }
    return true;
}

function isNumber(s)
{   
	var i;
	
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if ((!isDigit(c)) && (c != "."))
        { 
			return false;
		}
    }
    return true;
}

function isZIPCode(s)
{  
	return (isInteger(s) && (s.length == 5));
}

function isEmail(email)
{
	var spacePos = email.indexOf(" ");
	var atPos = email.indexOf("@");
	var dotPos = email.indexOf(".", atPos);
	var endPos = email.length;
			
	if (spacePos >= 0) 
	{
		return false;
	}

	if (atPos < 2) 
	{
		return false;
	}
						
	if (dotPos < (atPos + 2))	
	{
		return false;
	}

	if (endPos <= dotPos) 
	{
		return false;
	}
		
	return true;
}

function warnInvalid(theField, s)
{   
	var sType = theField.type;
	
	if (!isEmpty(sType))
	{ 
		theField.focus();
    }
    
    if (sType == "text")
    {
		theField.select();
    }
    alert(s);
    return false;
}	

// e contains the Javascript Event (the key that was pressed), allowable characters is the list of characters that are allowed.
function limitEntry(e, allow)
{
	var key;
	var keychar;

	// The following accounts for differences in browser implementation
	// IE 4.0 isn't quite slick enough to recognize the event, so we have to assign it
	if (window.event)
	{
		key = window.event.keyCode;
	}
	else if (e)
	{
		key = e.which;
	}
	else
	{
		return true;
	}
		
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();

	// control keys - Unicode characters (13 = RETURN;) 
	if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27))
	{
		return true;
	}
	// allowable characters
	else if (((allow).indexOf(keychar) > -1))
	{
		return true;
	}
	else
	{
		return false;
	}
}

//check the selected items in a dropdown with the maximum allowed
function checkMax(sInput, nMax, sVerbose)
{
	var nCount = 0;
	for (var i = 0; i < sInput.length; i++)
	{
		if (sInput[i].selected == true)
		{
			//if you don't have a 'No Preference' value, remove this if..else
			//statement, but leave the code that increments the count
			if (i != 0)		//this is the default selection
			{
				nCount++;
			}
			else
			{
				sInput[i].selected = false;
			}
		}
	}
	if (nCount > nMax)
	{
		return warnInvalid("", "You have selected " + nCount + sVerbose + ".  The maximum is " + nMax + ".  Please select again."); 
	}
	
	return true;
}

function Name_onClick()
{
	resetForm(document.frmMain);
	//if (!document.frmMain.address1.value == "") 
		//document.frmMain.address1.value = "";
		
	if (!document.frmMain.city.value == "") 
		document.frmMain.city.value = "";
	
	if (!document.frmMain.state.value == "") 
		document.frmMain.state.value = "";
	
	//if (!document.frmMain.zip.value == "") 
		//document.frmMain.zip.value = "";
	
	//if (!document.frmMain.countyID.value == "") 
		//document.frmMain.countyID.value = "";	
}

//Clears values on a Location Search
function Location_onClick()
{
	resetForm(document.frmMain);
	//if (!document.frmMain.countyID.value == "") 
		//document.frmMain.countyID.value = "";
		
	if (!document.frmMain.lookuplastname.value == "") 
		document.frmMain.lookuplastname.value = "";
	
	//if (!document.frmMain.lookupfirstname.value == "") 
		//document.frmMain.lookupfirstname.value = "";	
		
}

//Clears values on a Location Search
function Location_onChange()
{
	resetForm(document.frmMain);
	//if (!document.frmMain.countyID.value == "") 
		//document.frmMain.countyID.value = "";
		
	if (!document.frmMain.lookuplastname.value == "") 
		document.frmMain.lookuplastname.value = "";
	
	//if (!document.frmMain.lookupfirstname.value == "") 
		//document.frmMain.lookupfirstname.value = "";	
		
}
//switch tabs
function changeTab(sForm, sAction, sSrchType)
{
	resetForm(sForm);
	sForm.srchtype.value = sSrchType;
	return submitForm(sForm, sAction);
}

//return an appropriate action
function getAction(sPage)
{
	if(sPage != "")
	{
		return sPage;
	}
	else
	{
		return "begin.asp";
	}	
}

//change the target of a form
function setTarget(sForm, sTarget)
{
	if(sTarget != "")
	{
		sForm.target = sTarget;
	}
	return true;
}

//submit a form
function submitForm(sForm, sAction)
{
	sForm.action = getAction(sAction);
	sForm.submit();
	return false;
}

//printer friendly page
function printPage(sForm, sPage)
{
	resetForm(sForm);
	sForm.print.value = "TRUE";
	setTarget(sForm, "_blank");
	return submitForm(sForm, sPage);
}

//go to the map processing page
function submitMap(sForm, sProvID)
{
	resetForm(sForm);
	if(sProvID != "")
	{
		sForm.provid.value = sProvID;
	}
	return submitForm(sForm, "MapAddressEntry.asp");
}

//go to the site help page
function submitHelp(sForm, sAnchor)
{
	resetForm(sForm);
	return submitForm(sForm, "SiteHelp.asp#" + sAnchor);
}

//reset key form variables that may have been changed after doing a printer friendly version of the page
function resetForm(sForm)
{
	setTarget(sForm, "_self");
	sForm.srchtype.value = "SEARCH";
	sForm.print.value = "FALSE";
	return false;
}

function element_setClass(sElement, sClassName)
{
	document.getElementById(sElement).className = sClassName;
}

//_______________________________________________________________________________//

function product_onChange()
{
		
    if (document.frmMain.prodid.selectedIndex == 0)
    {
        var nLength = document.frmMain.rolecode.length;

        for (var i = 0; i < nLength; i++)
        {
            document.frmMain.rolecode.options[0] = null;
        }

        document.frmMain.rolecode.length = 2;
        document.frmMain.rolecode.options[0].value = "";
        document.frmMain.rolecode.options[0].text =  "All Providers";
        document.frmMain.rolecode.options[1].value = "";
        document.frmMain.rolecode.options[1].text = "";

        /* This is a hack for NS 7.0 - since there will always be 2 items in the select
           list we set the selected index to 1 to clear the dots in NS 7.0 and then set the
           selected index to 0 to select the correct item and then remove the item at
           index 1 */
        document.frmMain.rolecode.selectedIndex = 1;
        document.frmMain.rolecode.selectedIndex = 0;
        document.frmMain.rolecode.options[1] = null;
    }

    if (document.frmMain.prodid.selectedIndex == 1)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 2)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 3)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 4)
    {
        document.frmMain.rolecode.length = 6;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "3";
        document.frmMain.rolecode.options[4].text = "Primary Care Physician";
        document.frmMain.rolecode.options[5].value = "2";
        document.frmMain.rolecode.options[5].text = "Referral Institution";
    }

    if (document.frmMain.prodid.selectedIndex == 5)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 6)
    {
        document.frmMain.rolecode.length = 8;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "8";
        document.frmMain.rolecode.options[3].text = "Network Specialist";
        document.frmMain.rolecode.options[4].value = "6";
        document.frmMain.rolecode.options[4].text = "Obstetrics";
        document.frmMain.rolecode.options[5].value = "4";
        document.frmMain.rolecode.options[5].text = "Pharmacies";
        document.frmMain.rolecode.options[6].value = "3";
        document.frmMain.rolecode.options[6].text = "Primary Care Physician";
        document.frmMain.rolecode.options[7].value = "11";
        document.frmMain.rolecode.options[7].text = "Well Woman";
    }

    if (document.frmMain.prodid.selectedIndex == 7)
    {
        document.frmMain.rolecode.length = 10;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "8";
        document.frmMain.rolecode.options[3].text = "Network Specialist";
        document.frmMain.rolecode.options[4].value = "6";
        document.frmMain.rolecode.options[4].text = "Obstetrics";
        document.frmMain.rolecode.options[5].value = "4";
        document.frmMain.rolecode.options[5].text = "Pharmacies";
        document.frmMain.rolecode.options[6].value = "3";
        document.frmMain.rolecode.options[6].text = "Primary Care Physician";
        document.frmMain.rolecode.options[7].value = "2";
        document.frmMain.rolecode.options[7].text = "Referral Institution";
        document.frmMain.rolecode.options[8].value = "9";
        document.frmMain.rolecode.options[8].text = "Referral Specialist";
        document.frmMain.rolecode.options[9].value = "11";
        document.frmMain.rolecode.options[9].text = "Well Woman";
    }

    if (document.frmMain.prodid.selectedIndex == 8)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 9)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 10)
    {
        document.frmMain.rolecode.length = 3;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "10";
        document.frmMain.rolecode.options[1].text = "Institution";
        document.frmMain.rolecode.options[2].value = "7";
        document.frmMain.rolecode.options[2].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 11)
    {
        document.frmMain.rolecode.length = 6;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "10";
        document.frmMain.rolecode.options[1].text = "Institution";
        document.frmMain.rolecode.options[2].value = "8";
        document.frmMain.rolecode.options[2].text = "Network Specialist";
        document.frmMain.rolecode.options[3].value = "6";
        document.frmMain.rolecode.options[3].text = "Obstetrics";
        document.frmMain.rolecode.options[4].value = "3";
        document.frmMain.rolecode.options[4].text = "Primary Care Physician";
        document.frmMain.rolecode.options[5].value = "11";
        document.frmMain.rolecode.options[5].text = "Well Woman";
    }

    if (document.frmMain.prodid.selectedIndex == 12)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 13)
    {
        document.frmMain.rolecode.length = 3;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "10";
        document.frmMain.rolecode.options[1].text = "Institution";
        document.frmMain.rolecode.options[2].value = "7";
        document.frmMain.rolecode.options[2].text = "Professional Provider";
    }

    if (document.frmMain.prodid.selectedIndex == 14)
    {
        document.frmMain.rolecode.length = 5;
        document.frmMain.rolecode.selectedIndex = 0
        document.frmMain.rolecode.options[0].value = "0000"; 
        document.frmMain.rolecode.options[0].text = "Choose a Provider Type"; 
        document.frmMain.rolecode.options[1].value = "1";
        document.frmMain.rolecode.options[1].text = "Dentists";
        document.frmMain.rolecode.options[2].value = "10";
        document.frmMain.rolecode.options[2].text = "Institution";
        document.frmMain.rolecode.options[3].value = "4";
        document.frmMain.rolecode.options[3].text = "Pharmacies";
        document.frmMain.rolecode.options[4].value = "7";
        document.frmMain.rolecode.options[4].text = "Professional Provider";
    }


}


function role_onChange()
{
	resetForm(document.frmMain);
	if (document.frmMain.rolecode[document.frmMain.rolecode.selectedIndex].value == "") 
	{
		document.frmMain.rolecode.selectedIndex = 0;
	}
	
	var sInput = 'RoleDesc' + document.frmMain.rolecode[document.frmMain.rolecode.selectedIndex].value;	
	if (sInput!='RoleDesc0000')
	{
		document.getElementById("RoleDesc").className = '';
		document.getElementById("RoleDesc").innerHTML = document.getElementById(sInput).value;
	}
	else
	{
		document.getElementById("RoleDesc").className = 'RoleDescript';
		document.getElementById("RoleDesc").innerHTML = '';
	}	
}

function role_onChange2()
{	
	var sInput = 'RoleDesc' + document.frmMain.rolecode[document.frmMain.rolecode.selectedIndex].value;	
	if (sInput!='RoleDesc0000')
	{
		document.getElementById("RoleDesc").className = '';
		document.getElementById("RoleDesc").innerHTML = document.getElementById(sInput).value;
	}
	else
	{
		document.getElementById("RoleDesc").className = 'RoleDescript';
		document.getElementById("RoleDesc").innerHTML = '';
	}
}

function form_onSubmit(sForm, bGoToResults)
{
	var sCity = sForm.city.value;
	var sState = sForm.state.value;
	//var sZip = sForm.zip.value;
	//var sCounty = sForm.countyID.value;
	var sLookup = sForm.lookuplastname.value;
	

		
	resetForm(sForm);
	if (validateForm(sForm))
	{
		//if (!isWhitespace(sCounty))
		//{
		//	sForm.srchtype.value = "SEARCH";
		//}

		//strSrchType = strSearchVal by default;
		
		//populate logcriteria input and form action if user chose "Find Providers Now"
		if(bGoToResults == true)
		{
			sForm.logcriteria.value = "TRUE";
			return submitForm(sForm, "http://www.geoaccess.com/bcbsks/po/DisplayResults.asp");
		}	
		else
		{
			return submitForm(sForm, "http://www.geoaccess.com/bcbsks/po/RefineSearch.asp");
		}	
	}
	return false;
}

function validateForm(sForm)
{
	//check address entry
	var sCity = sForm.city.value;
	var sState = sForm.state.value;
	//var sZip = sForm.zip.value;
	//var sCounty = sForm.countyID.value;
	var sLookup = sForm.lookuplastname.value;
	
		
	//check for at least 2 characters if namelookup is chosen
	
				
		//check role selection	
	if (sLookup != "" && sCity == "") {
		alert("Please choose a city");
		return false;
	}
	return true;
}
