
//-------------------------------------------------------------------------------------------------------------
// these 2 function "skip" and "toggleSelect" make it so the state can't be selected when another country but US is selected

function skip () { 
	this.blur(); 
}

function toggleSelect (select) {
  if (!select.disabled) {
    select.disabled = true;
    if (!document.all && !document.getElementById) {
      select.oldOnFocus = 
        select.onfocus ? select.onfocus : null;
      select.onfocus = skip;
    }
  } 
  else {
    select.disabled = false;
    if (!document.all && !document.getElementById) {
      select.onfocus = select.oldOnFocus;
    }
  }
}

function checkCSC () {
  var country = document.forms['regform2'].elements['country'];
  var state   = document.forms['regform2'].elements['state'];
  var county  = document.forms['regform2'].elements['county'];

  if (country.value == 203) {      // United States
    if (state.disabled)
      toggleSelect(state);
    document.getElementById('regstateusa').disabled = '';
    document.getElementById('regstateusa').style.display = 'list-item';
    document.getElementById('regstatecanada').disabled = 'disabled';
    document.getElementById('regstatecanada').style.display = 'none';
    // check counties
    if (county) {
      if (county.disabled)
        toggleSelect(county);
      if (state.value == 43) {        // N.Y.
        document.getElementById('regcounty43').disabled = false;
        document.getElementById('regcounty46').disabled = true;
        document.getElementById('regcounty54').disabled = true;
        document.getElementById('regcounty43').style.display = 'list-item';
        document.getElementById('regcounty46').style.display = 'none';
        document.getElementById('regcounty54').style.display = 'none';
      } else if (state.value == 46) { // Ohio
        document.getElementById('regcounty43').disabled = 'disabled';
        document.getElementById('regcounty46').disabled = '';
        document.getElementById('regcounty54').disabled = 'disabled';
        document.getElementById('regcounty43').style.display = 'none';
        document.getElementById('regcounty46').style.display = 'list-item';
        document.getElementById('regcounty54').style.display = 'none';
      } else if (state.value == 54) { // Texas
        document.getElementById('regcounty43').disabled = 'disabled';
        document.getElementById('regcounty46').disabled = 'disabled';
        document.getElementById('regcounty54').disabled = '';
        document.getElementById('regcounty43').style.display = 'none';
        document.getElementById('regcounty46').style.display = 'none';
        document.getElementById('regcounty54').style.display = 'list-item';
      } else {
        document.getElementById('regcounty43').disabled = 'disabled';
        document.getElementById('regcounty46').disabled = 'disabled';
        document.getElementById('regcounty54').disabled = 'disabled';
        document.getElementById('regcounty43').style.display = 'none';
        document.getElementById('regcounty46').style.display = 'none';
        document.getElementById('regcounty54').style.display = 'none';
        toggleSelect(county);
        county.selectedIndex = 0;
      }
    }

  } else if (country.value == 33) { // Canada
    if (county) {
      county.selectedIndex = 0;
    }
    if (state.disabled)
      toggleSelect(state);
    if (county) {
      if (! county.disabled)
        toggleSelect(county);
    }
    document.getElementById('regstatecanada').disabled = '';
    document.getElementById('regstatecanada').style.display = 'list-item';
    document.getElementById('regstateusa').disabled = 'disabled';
    document.getElementById('regstateusa').style.display = 'none';
  } else {                         // Other countries
    state.selectedIndex = 0;
    if (! state.disabled)
      toggleSelect(state);
    if (county) {
      county.selectedIndex = 0;
      if (! county.disabled)
        toggleSelect(county);
    }
  }
}

//--------------------------------------------------------------------------------------------------------------------------------------------
// this function makes sure the correct state goes with the corrrect country

function checkStateAndCountry(state, country)   {

    if(country.value == 203)      {       //this is the US
        if(state)       {
            if(document.reg.state.disabled) {
                    toggleSelect(document.reg.state);
                    document.forms['reg'].elements['state'][document.forms['reg'].elements['state'].selectedIndex].text = "N/A";
            }
        }
  } else if(country.value == 33)        {  //this is canada
        if(state)   {
            if(document.reg.state.disabled) {
                    toggleSelect(document.reg.state);
                    document.forms['reg'].elements['state'][document.forms['reg'].elements['state'].selectedIndex].text = "N/A";
            }
        }

  } else    {       // any country except US or Canada
                document.forms['reg'].elements['state'][0].selected = true;

                var system = /linux/i;

                if(navigator.userAgent.match(system) == null) {
                    document.forms['reg'].elements['state'][document.forms['reg'].elements['state'].selectedIndex].text = 'N/A (Disabled)';
                }
                if(!document.reg.state.disabled)
                    toggleSelect(document.reg.state);
   }
}

//-----------------------------------------------------------------------------------------------------------------

function test_domainstring(str,msg)  {
  var pattern = /^[A-Za-z]+[A-Za-z0-9.-]+[A-Za-z0-9]+$/;
	
	if (!pattern.test(str))  {
		return msg;
	}

  return('');
}

//-----------------------------------------------------------------------------------------------------------------
// trim leading and trailing white spaces 

function trim(str){

	while (str.length > 0 && str.charAt(str.length - 1) == ' '){  // trim trailing white spaces 
		str = str.slice(0, str.length - 1);
	}
	
	while (str.length > 0 && str.charAt(0) == ' ')	{	// trim leading white spaces 
		str = str.slice(1, str.length);
	}

  return str;

}

//-----------------------------------------------------------------------------------------------------------------
function isNumber(data)  {
  var numstr="0123456789";
  var thisChar;
  var counter = 0;
	
	for (var i=0; i<data.length; i++)	{
		thisChar = data.substring(i,i+1);

		if (numstr.indexOf(thisChar)!= -1)
			counter++;
	}

	if (counter == data.length )
		return 1;
	else 
		return 0;
}

//-----------------------------------------------------------------------------------------------------------------
function verifyIP (IPvalue) {

	if(IPvalue == "")  {
		return "";
	}

	errorString = "";
	theName = "";

	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern); 

	if (IPvalue == "0.0.0.0")
		errorString = errorString + theName +IPvalue+' is a special IP address and cannot be used here.\n';
	else if (IPvalue == "255.255.255.255")
		errorString = errorString + theName +IPvalue+' is a special IP address and cannot be used here.\n';
	if (ipArray == null)
		errorString = errorString + theName +IPvalue+' is not a valid IP address.\n';
	else {
		for (i = 0; i < 4; i++) {
			thisSegment = ipArray[i];
			
			if (thisSegment > 255) {
				errorString = errorString + theName +IPvalue+' is not a valid IP address.\n';

				i = 4;
			}
			
			if ((i == 0) && (thisSegment > 255)) {
				errorString = errorString + theName +IPvalue+' is a special IP address and cannot be used here.\n';
			
				i = 4;
      		}
   		}
	}

	extensionLength = 3;

  return errorString;
}

//-----------------------------------------------------------------------------------------------------------------
function validateLogin(login,msg)  {

	if (login.length < 3)    {
        return(msg = msg + "Your login must be at least three characters.\r\n");
   	}  else	{
		var loginpattern = /[^-a-zA-Z0-9_@.]/;
		
		if (loginpattern.test(login))	{
			return(msg = msg + "Your login contains invalid characters.\r\n");
		}
	} 
	
  return('');
}


//-------------------------------------------------------------------------------------------------------
// this fucnction verifies that all the characters in "word" are valid ASCII characters

function verifyASCIIChars(word)   {

  var asciiValue;
  var i;
  var splitWord = new Array();
  var badChars = "";
  
    if (word != "")    {
        splitWord = word.split('');
     
        for(i = 0; i < splitWord.length; i++)   {
            asciiValue = splitWord[i].charCodeAt(0) - 65;
            
            if(asciiValue < 1 || asciiValue > 127)
                badChars += splitWord[i];
        }
    
    }
    
    return badChars;
}    

//-----------------------------------------------------------------------------------------------------------------
// testing for nonregular chars

function test_nonreg(str,msg)  {

  var ourstr = String(str);
  var f;

  var numStr= "\\:`\"?|<>;/*$^~";

  var thisChar;	
  var counter=0;
  var regular='';
  var regularname='';

	for (var i= 0; i < ourstr.length; i++) 	{
		thisChar = ourstr.substring(i, i+1);
		
		if(numStr.indexOf(thisChar) != -1)  {
		    regular += thisChar;
			counter++;
		} 
            // this checks for valid ascii characters
        if(thisChar.charCodeAt(0) < 1 || thisChar.charCodeAt(0) > 127) {
            regular += thisChar;
            counter++;
        }
	}

	if (counter > 0)  {
		if (counter ==1)
			regularname=" Symbol ";
		else
			regularname=" Symbols ";

		return(msg+' '+regularname+'\''+regular+'\' not allowed.\r\n');
	}

	return('');
}