// Open Window
function openWindow(url) {
  popupWin = window.open(url,'new_page','width=400,height=450,scrollbars=yes')

}



// Function to jump to next field if data entry is complete
	function KeyPress(what,e,max,action) {
				
		if (document.all) {
		   if (what.value.length > (max-1))
				eval(action);
			}
	}

  function KeyDown(DnEvents) {
  k = window.event.keyCode;
   //if (k == 13) // enter key pressed
   //  window.event.keyCode = 9
     return true;
}

// Call the text editor
function Call_The_Text_Editor(cbox) {
//opener.document.all.ID_Field.value;
msgWindow=window.open   ("MyEdit/rte.asp","displayWindow","toolbar=no,scrollbars=yes,resizable=yes,width=800,height=600'");
msgWindow.document.bgColor='lightyellow';


}

// Call the ListBox
function Call_The_ListBox(WhatField,WhatReturn,Width,Height,WhatParameters) {
document.all.From_Field.value = WhatField;
document.all.Return_Field.value = WhatReturn;
TheName = "ListBox.asp?" + WhatParameters
centerWindow(TheName,"displayWindow",Width,Height,"yes","no")
}

// Call the Extra Sessions Page
function Call_Extra(cbox) {
//opener.document.all.ID_Field.value;
msgWindow=window.open   ("Request_Extra.asp","displayWindow","toolbar=no,scrollbars=yes,resizable=yes,width=425,height=600'");
msgWindow.document.bgColor='lightyellow';


}



// Call the Calendar
function Call_The_Calendar(WhatField,WhatReturn) {
document.all.From_Field.value = WhatField;
document.all.Return_Field.value = WhatReturn;
centerWindow   ("Calendar/CALENDAR.ASP","displayWindow","500","450","no","yes")
}

function centerWindow(url,name,WinW,WinH,scrollbars,resizeable) {
 
SWidth = screen.width;
SHeight = screen.height;
var CenW = (SWidth - WinW) /2; 
var CenH = (SHeight - WinH) /2; 

window.open(url,name,"width="+WinW+",height="+WinH+",top="+CenH+",left="+CenW+",toolbar=no,location=no,status=no,menubar=no,scrollbars="+scrollbars+",resizable="+resizeable);
}

// Change the checkbox if text next to box is clicked
function changeBox(cbox) {
box = eval(cbox);
box.checked = !box.checked;
}

function ValidateNumber(field) {
var valid = " 0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are accepted!");
field.focus();
field.select();
   }
}

function checkPriorAfterdate(Prior1,Prior2,Prior3,After1,After2,After3) {

if (Prior3>After3) return false;
if (Prior1>After1) return false;
if (Prior2>After2) return false;

 return true;
}


function GotFocus(WhatField) {
WhatField.style.backgroundColor='#FFFACD';
return true;
}

function LostFocus(WhatField) {
WhatField.style.backgroundColor='#FFFFFF';
return true;
}

// used for gray shaded backgrounds
function LostFocusShaded(WhatField) {
WhatField.style.backgroundColor='#eeeeee';
return true;
}

function checkdate(month,day,year){
//	window.onerror=null // for all other strange errors
	var err=0
	//a=document.frm.dat.value
	// window.alert(a)
	//if (a.length != 8) err=1
	//b = a.substring(0, 2)// month
	//c = a.substring(2, 3)// '/'
	//d = a.substring(3, 5)// day
	//e = a.substring(5, 6)// '/'
	//f = a.substring(6, 8)// year


    //basic error checking
	if (month<1 || month>12) err = 1
	//if (c != '/') err = 1
	if (day<1 || day>31) err = 1
	//if (e != '/') err = 1
	if (year<0 || year>2010) err = 1
	
	//advanced error checking

	// months with 30 days
	if (month==4 || month==6 || month==9 || month==11){
		if (day==31) err=1
	}

	// february, leap year
	if (month==2){
		// feb
		var g=parseInt(year/4)
		if (isNaN(g)) {
			err=1
		}

		if (day>29) err=1
		if (day==29 && ((year/4)!=parseInt(year/4))) err=1
	}

    //is blank?
    if (month < 1 && day < 1 && year < 1){
      err=0
    }


	if (err==1){
		//alert('Wrong input!');
		return false; 
	}
	else{
	//	alert('OK!');
		return true; 
	}

}

function IgnoreSpaces(string) {
  var temp = "";
  string = '' + string;
  splitstring = string.split(" ");
  for(x = 0; x < splitstring.length; x++)
  temp += splitstring[x];
  return temp;
}


function MustInput(WhatField){
	var name = eval('document.all.' + WhatField) 
	
	if (IgnoreSpaces(name.value) == ""){  
	    name.focus(); 
	   	  
	  // Specify which field cannot be blank 
	   switch(WhatField) 
       {
       case(n="emailentered"): 
          alert("\nEmail cannot be blank.\n\nPlease enter the information.");      
	      break
        case(n="firstname"): 
          alert("\nThe First Name cannot be blank.\n\nPlease enter the information.");      
	      break
        case(n="lastname"): 
       	  alert("\nThe Last Name cannot be blank.\n\nPlease enter the information.");      
	      break
       default:
       alert("This field cannot be blank.\n\nPlease enter the information");
       break
       }
		
   	name.focus();
	return false;      
	}   
	return true;	
}

function DataValidation(){
     	           
    //Uses Array from Data Input Page to Check for Blanks
     for (i=1;i<aWhatFields.length;i++){
     if (!(MustInput(aWhatFields[i]))) return false;
     }
	 
 // Validate Email Address
     for (i=1;i<aValidateEmail.length;i++){
     var name = eval('document.all.' + aValidateEmail[i] + '.value');
     var returnname = eval('document.all.' + aValidateEmail[i]);  
             
     if (emailCheck(name)) {
        
     }     
     else{
    
     alert("\nThis E-Mail Address is not valid.\n\Please enter a valid E-Mail Address.");
     returnname.focus();
     return false;
     }
     }



// If we've gotten this far, everything's valid!
return true;
}


   function emailCheck (emailStr) {
/* 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
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	// alert("The domain name doesn't seem to be valid.")
    return false
}

/* 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;
}

     
