/* file: defs.js
** all client-side Javascript code shared (or potentially shared) 
** by more than one HTML pages goes here.
*/

// GLOBAL VARIABLES

// VIS Config Dictionary Cookie parameters
var VisConfigCookieNameD = "VIS" 	// dictionary cookie name
var VisMbrIdCookieKeyD = "mbrid"	// VIS Member ID key
var VisMbrIdCookieName = "vismbrid"	// obsolete non-dictionary
									// VIS member ID cookie name

var VisMbrIdCookieExpDays = 365

// Password (VIS member ID) lookup app window parameters
var PLWinOpt = 'toolbar=no,scrollbars=yes,width=450,height=350'
var PLWinUrl = 'plookup.aspx'
var PLWinName = 'PLookup'

// VRC help page window parameters
var VRCWinOpt = 'toolbar=no,scrollbars=yes,width=450,height=350'
var VRCWinUrl = 'vrchelp.htm'
var VRCWinName = 'VRCHelp'

// VRC groups window parameters
var VRCWinOpt = 'toolbar=yes,menubar=yes,scrollbars=yes,alwaysRaised=yes,resizable=yes'
var VRCWinName = 'Group'



// FUNCTION DEFINITIONS

function addCookie(sName, sValue, dtDaysExpires)		//adds Cookie named sName with value = sValue
{
	var dtExpires = new Date();
	var dtExpiryDate = "";
	dtExpires.setTime(dtExpires.getTime() + dtDaysExpires * 24 * 60 *60*1000);
	dtExpiryDate = dtExpires.toUTCString();
	var pathName ="/";
	document.cookie = sName + "=" + sValue + "; expires=" + dtExpiryDate + "; path=" + pathName;
}

function addCookieDictionary(sName, sKey, sValue, dtDaysExpires)		//adds a Dictionary Cookie named sName with value = sValue and key = sKey
{
	var dtExpires = new Date();
	var dtExpiryDate = "";
	dtExpires.setTime(dtExpires.getTime() + dtDaysExpires * 24 * 60 *60*1000);
	dtExpiryDate = dtExpires.toUTCString();
	var pathName ="/";
	document.cookie = sName + "=" + sKey + "=" + sValue + "; expires=" + dtExpiryDate + "; path=" + pathName;
}


//--------------------------------------------------------------------------------------
//check if the client browser allows Cookies
function BrowserAllowsCookies()						
//---------------------------------------------------------------------------------------
{
	addCookie("TestCookies", "OK" , 10);
	var sTestCookies = funFindCookie("TestCookies");
	if (sTestCookies == "")
		return false;
	else
		// invalidate the test cookie
		addCookie("TestCookies", "OK" , -10);
		return true;
}


function emailCheck (emailStr) {
/* 1.1.2: Fixed a bug where trailing . in e-mail address was passing
            (the bug is actually in the weak regexp engine of the browser; I
            simplified the regexps to make it work).
   1.1.1: Removed restriction that countries must be preceded by a domain,
            so abc@host.uk is now legal.  However, there's still the 
            restriction that an address must end in a two or three letter
            word.
     1.1: Rewrote most of the function to conform more closely to RFC 822.
     1.0: Original  */
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}

function openWin (url, propName, windowName, winOpt) {
/*	function to open a named popup window, or if the window
    already exists, to give the window "focus"
	11/11/01 - H.Hubik - Rev 1.0
*/
  if (!window[propName] || window[propName].closed)
    window[propName] = open (url, windowName,winOpt);
  else
    window[propName].location.href = url;
  window[propName].focus();

}


function funFindCookie(sName)  
//finds and returns a Cookie named sName 
{	var i = 0;
	var nStartPosition = 0;
	var nEndPosition = 0;
	var sCookieString = document.cookie;
	while( i <= sCookieString.length)
	{	nStartPosition = i;
		nEndPosition = nStartPosition + sName.length;
		var substr = sCookieString.substring(nStartPosition, nEndPosition)
		if(sCookieString.substring(nStartPosition, nEndPosition) == sName)
		{
			nStartPosition = nEndPosition + 1;
			nEndPosition = document.cookie.indexOf(";", nStartPosition);
			if(nEndPosition < nStartPosition)
				nEndPosition = document.cookie.length;
			
			return document.cookie.substring(nStartPosition, nEndPosition);
			break;
		}
		i++;	
	}
return "";
}


//--------------------------------------------------------------------------------------
function MailOrderFormValidator(theForm)
{
//check the First Name field	
  if (theForm.txtFirstName.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.txtFirstName.focus();
    return (false);
  }

  if (theForm.txtFirstName.value.length < 2)
  {
    alert("Please enter at least 2 characters in the \"First Name\" field.");
    theForm.txtFirstName.focus();
    return (false);
  }

  if (theForm.txtFirstName.value.length > 25)
  {
    alert("Please enter at most 25 characters in the \"First Name\" field.");
    theForm.txtFirstName.focus();
    return (false);
  }

  var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\x83\x8A\x8C\x8E\x9A\x9C\x9E\x9F\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF\x27\x2D";
  var checkStr = theForm.txtFirstName.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter characters in the \"First Name\" field.");
    theForm.txtFirstName.focus();
    return (false);
  }


//check the Last Name field	
  if (theForm.txtLastName.value == "")
  {
    alert("Please enter a value for the \"Last Name\" field.");
    theForm.txtLastName.focus();
    return (false);
  }

  if (theForm.txtLastName.value.length < 3)
  {
    alert("Please enter at least 3 characters in the \"Last Name\" field.");
    theForm.txtLastName.focus();
    return (false);
  }

  if (theForm.txtLastName.value.length > 25)
  {
    alert("Please enter at most 25 characters in the \"Last Name\" field.");
    theForm.txtLastName.focus();
    return (false);
  }

  var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\x83\x8A\x8C\x8E\x9A\x9C\x9E\x9F\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF\x27\x2D";
  var checkStr = theForm.txtLastName.value;
  var allValid = true;
  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("Please enter only letter characters in the \"Last Name\" field.");
    theForm.txtLastName.focus();
    return (false);
  }


// check the email field

  if (!emailCheck(theForm.txtEmail1.value))
  {
    theForm.txtEmail1.focus();
    return (false);
  }

  if (!emailCheck(theForm.txtEmail2.value))
  {
    theForm.txtEmail2.focus();
    return (false);
  }


//check if the both email fields are equal
  if (!(theForm.txtEmail1.value == theForm.txtEmail2.value))
  {
    alert("Please reenter your email");
    theForm.txtEmail1.value = "";
    theForm.txtEmail2.value = "";
    
    theForm.txtEmail1.focus();
    return (false);
  }


// if all is well so far, return True
  return (true);
}