//-------------------------------------------------------------------------------------
// some useful functions for JavaScript
//-------------------------------------------------------------------------------------


function print_all_states(selected_abr)
{
  var allstates_abr = new Array("", "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY");
  var allstates_text = new Array("Please Select", "Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "District of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");

  for(i=0;i<allstates_abr.length;++i)
    if ( selected_abr == allstates_abr[i] )
      document.write("<option value='"+allstates_abr[i]+"' selected>"+allstates_text[i]+"</option>");
    else
      document.write("<option value='"+allstates_abr[i]+"'>"+allstates_text[i]+"</option>");
}

function validated(string) {
  
	for (var i=0, output='', valid="1234567890."; i<string.length; i++)
       if (valid.indexOf(string.charAt(i)) != -1)
          output += string.charAt(i)
    return output;
} 

function GetRadioIndex(thisform)
{
    var i;

    for(i=0;i<thisform.length;i++)
        if ( thisform[i].checked ) return(i);

    return(-1);
}

function CalculateChecked(thisform,checkname)
{
    var i, num = 0;

    for(i=0;i<thisform.elements.length;i++)
        if ( thisform.elements[i].name == checkname && thisform.elements[i].checked ) num++;

    return(num);
}

function SetAllCheck(thisform,checkname,value)
{
    var i;
    for(i=0;i<thisform.elements.length;i++)
        if ( thisform.elements[i].name == checkname ) thisform.elements[i].checked = value;
}

function SetOneRadio(thisform,value)
{
   
	var i;
	
    for(i=0;i<thisform.length;i++)
    {
		
        if ( thisform[i].value == value ) thisform[i].checked = true;
    }
}

function SetCheckbox(thisform,value)
{
    if ( thisform.value == value ) thisform.checked = true;
}

function ChangeSelectMenuToValue(selectform,value)
{
    var i;
    for(i=0;i<selectform.options.length;i++)
    {
        if ( selectform.options[i].value == value || selectform.options[i].text == value )
        {
            selectform.selectedIndex = i;
            break;
        }
    }
}

function FindByName(thisform,name)
{
    var i;
    for(i=0;i<thisform.elements.length;i++)
        if ( thisform.elements[i].name == name ) return thisform.elements[i];

    return null;
}

function EmailCheck(email)
{
    var emailReg = "^[\\w\\d-_\\.]+\@[\\w\\d-_\\.]+\\.[a-zA-Z]{2,}$";
    var regex = new RegExp(emailReg);
    if ( regex.test(email) ) return true;
    else                     return false;
}

function RegExCheck(subject, pattern)
{
    var regex = new RegExp(pattern);
    if ( regex.test(subject) ) return true;
    else                       return false;
}

function Trim(str)
{
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

function SetCookie(name, value)
{
   var argv = SetCookie.arguments;
   var argc = SetCookie.arguments.length;
   var expires = (argc > 2) ? argv[2] : null;
   var path = (argc > 3) ? argv[3] : null;
   var domain = (argc > 4) ? argv[4] : null;
   var secure = (argc > 5) ? argv[5] : false;
   document.cookie = name + "=" + escape (value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}

function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}


  


var popWin = null    // use this when referring to pop-up window
var winCount = 0
var winName = "popWin"

function openPopWin(winURL, winWidth, winHeight, winFeatures, winLeft, winTop){
  var d_winLeft = 20  // default, pixels from screen left to window left
  var d_winTop = 20   // default, pixels from screen top to window top
  winName = "popWin" + winCount++ //unique name for each pop-up window
  closePopWin()           // close any previously opened pop-up window
  if (openPopWin.arguments.length >= 4)  // any additional features? 
    winFeatures = "," + winFeatures
  else 
    winFeatures = "" 
  if (openPopWin.arguments.length == 6)  // location specified
    winFeatures += getLocation(winWidth, winHeight, winLeft, winTop)
  else
    winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop)
  popWin = window.open(winURL, winName, "width=" + winWidth 
           + ",height=" + winHeight + winFeatures)
  }

function closePopWin(){    // close pop-up window if it is open 
  if (navigator.appName != "Microsoft Internet Explorer" 
      || parseInt(navigator.appVersion) >=4) //do not close if early IE
    if(popWin != null) if(!popWin.closed) popWin.close() 
  }

function getLocation(winWidth, winHeight, winLeft, winTop){
  return ""
  }


function getLocation(winWidth, winHeight, winLeft, winTop){
  var winLocation = ""
  if (winLeft < 0)
    winLeft = screen.width - winWidth + winLeft
  if (winTop < 0)
    winTop = screen.height - winHeight + winTop
  if (winTop == "cen")
    winTop = (screen.height - winHeight)/2 - 20
  if (winLeft == "cen")
    winLeft = (screen.width - winWidth)/2
  if (winLeft>0 & winTop>0)
    winLocation =  ",screenX=" + winLeft + ",left=" + winLeft   
                + ",screenY=" + winTop + ",top=" + winTop
  else
    winLocation = ""
  return winLocation
  }






