function NoFrame() 

// ====================================================================
// if current page in frame then break out of it
// ====================================================================

{
  if (top != window)
    top.location.href = window.location.href;
}

function ConfirmAndLocate(strConfirmMessage, strLocation) 

// ====================================================================
// display confirmation message
// if confirmted then redirect to page location specified
// ====================================================================

{
  if (confirm(strConfirmMessage))
    location = strLocation;
}

function isblank(s)

// ====================================================================
// Function  : checks whether parameter string is blank or not
// Parameters: string
// Returns   : true/false  
// Author    : Jason N. Yip
// ====================================================================

{
  if ((s == null) || (s=="")) 
  {
    return true;
  }
  else 
  {
    for (var i = 0; i < s.length; i++) 
    {
      var c = s.charAt(i);
      if ((c != ' ') && (c != '\n') && (c != '\t') && (c != '\r')) return false;
    }
  }
    return true;
}

function showDate (YMDDate) 

// ====================================================================
// Function  : display YMD date
// Parameters: date in format yyyymmdd
// Returns   : - nothing -
// Author    : Jason N. Yip
// ====================================================================

{
  year = parseInt(YMDDate.substr(0,4));
  month = parseInt(YMDDate.substr(4,2));
  day = parseInt(YMDDate.substr(6,2));
  
  alert ( "Year: " + year + "\n" 
        + "Month: " + month + "\n" 
        + "Day: " + day + "\n" );
}

function validDate (inpDate)

// ====================================================================
// Validate specified DMY date 
// ====================================================================

{
  var errMsg = "";

  if ((inpDate=="") || (inpDate==null))
  {
    errMsg = "Date not entered";
  }
  
  else if (inpDate.indexOf("/")== -1) 
  {
    errMsg = "Date separators not found.";
  }
  
  else
  {
    var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");

    var date_array = inpDate.split("/");
    var datePart = 0;

    var day = parseInt(date_array[0]);
    var month = parseInt(date_array[1]);
    var year = parseInt(date_array[2]);
    
    var realDate = new Date(year, month-1, day);

    // @@@
    // alert ("Year: " + year + "\nMonth: " + month + "\nDay: " + day);

    // if Not a Number OR NOT valid date then
    if ( isNaN(realDate) || !validYMDDate ( year, month, day ) )
    {
      errMsg = "Invalid date format.";
    }
    
    else
    {
      // alert ( realDate );
    }
  }
  
  if (!errMsg == "")
  {
    // alert (errMsg);
    return false;    
  }
  
  else
  {
    return true;
  }
  
}

function validDateYMD (YMDDate) 

// ====================================================================
// Function  : validate YMD date
// Parameters: date in format yyyymmdd
// Returns   : true/false
// Author    : Jason N. Yip
// ====================================================================

{
  
  if ( YMDDate.length != 8) 
  { 
    return false;
  }
  else 
  {    
    year = parseInt(YMDDate.substr(0,4));
    month = parseInt(YMDDate.substr(4,2));
    day = parseInt(YMDDate.substr(6,2));
  
    if (validYMDDate (year, month, day)) 
    { 
      return true;
    }
    
    else 
    {
      return false;
    }
  }
}

function validYMDDate ( year, month, day )

// ====================================================================
// Function: validate date
//
// Parameters:
// day:   day of month to check
// month: month of year to check
// year:  year (4 digits)
//
// Return values: true/false
//
// Author: Jason N. Yip
// ====================================================================

{
  // assume valid until proven otherwise
	var blnReturn = true;

	// error if day is less than 1 or greater than 31
	if (day < 1 || day > 31) 
  {
    blnReturn = false;
  }  

	// error if month less than 1 or greater than 12
	if (month < 1 || month > 12) 
  {
    blnReturn = false;
  }

	// error if 31 days specified but month is April, June, September or November
	if (day==31 && (month==4 || month==6 || month==9 || month==11)) 
  {
    blnReturn=false;
  }  
	
	// If February
	if (month==2) 
  {

    // If leap year
    
    // NOTE
    //  A leap year is one that is
    //  evenly divisible by 400
    //  or evenly divisible by 4 but not evenly divisble by 100
    
    //  That is, every 400 years is always a leap year
    //  Otherwise every 4 years is a leap year except for every 100
    
    //  400, 800, 1200, 1600 and 2000 are leap years
    //  1800, 1900 and 2100 are not leap years

		if (year%400==0 || ((year%4==0) && (year%100!=0))) 
    {
      // error if more than 29 days
			if (day > 29) blnReturn=false;
		} 
    
    // else (not leap year) error if more than 28 days  
    else 
    {
			if (day > 28) blnReturn=false;
		}
	}
  
  if (!blnReturn)
  {
    // @@@
    // alert ("validYMDDate detected an error with following:\n" + "Year: " + year + "\nMonth: " + month + "\nDay: " + day);
  }
  
	return blnReturn;

}

function validform (f) 

// ====================================================================
// Function  : validate an HTML form
// Parameters: form object
// Returns   : true/false
// Author    : Jason N. Yip
// ====================================================================

{
  var msg = "";
  var empty_fields = "";
  var errors = "";
  
  for (var i = 0; i < f.length; i++) 
  {
    var e = f.elements[i];
    
    if (( (e.type=="text") || (e.type=="textarea") || (e.type=="password") || (e.type=="select-one") || (e.type=="select-multiple") ||(e.type=="file") ) && !e.optional) 
    {
      if ((e.value==null) || (e.value=="") || isblank(e.value)) 
      {
        empty_fields += "\n  -  " + ((e.title) ? e.title: e.name);
        continue;
      }
      if (e.numeric || (e.min != null) || (e.max != null)) 
      {
        var v = parseFloat(e.value);
        if (isNaN(v) ||
          ((e.min != null) && (v < e.min)) ||
          ((e.max != null) && (v > e.max))) 
        {
          errors += "  -  " + ((e.title) ? e.title: e.name) + " must be a number";
          if (e.min != null)
            errors += " greater than " + e.min;
          if (e.max != null && e.min !=null)
            errors += " and less than " + e.max;
          else if (e.max !=null)
            errors += " that is less than " + e.max;
          errors += ".\n";
        }
      }
    }
    if (e.minfieldlen != null) 
    {
      if ((!e.optional && (e.value.length < e.minfieldlen)) ||
         (e.optional && (!isblank(e.value)) && (e.value.length < e.minfieldlen))) 
      {
        errors += e.title + " must be a minimum of " + e.minfieldlen + " characters.\n"
      }
    }    
    if ((e.datefield) && (!isblank(e.value))) 
    {
      if (!validDate(e.value)) 
      {
        errors += e.title + " contains an invalid date.\n"
      }
    }
    
  }
  if (errors || empty_fields) 
  {
    msg += "Errors were found in the information provided.\n";
    msg += "Please do the following before you continue.\n\n";
    
    if (empty_fields) 
    {
      msg += "Complete the following:\n" + empty_fields + "\n\n";
    }
 
    if (errors) 
    {
      msg += "Correct the following:\n\n";
      msg += errors;
    }
 
    alert(msg);
    return false;
  }
  else 
  {
    return true;
  }
}


function validImages (imgName)

// ====================================================================
// validate image filenames
// ====================================================================

{

  var strLen;
  var strExt;
  
  strLen = imgName.value.length;
  
  if (strLen > 3)
  {
    strExt = imgName.value.substring((strLen - 3), strLen);
    strExt = strExt.toLowerCase();
    
    if ((strExt == 'gif') || (strExt == 'jpg'))
      return true;
    else
      alert ('Invalid image type.\n\nImage must be *.gif or *.jpg');
      return false;
  }
  
      alert ('Invalid image type.\n\nImage must be *.gif or *.jpg');
  return false;
}

function validchgpwd(f) 

// ====================================================================
// Validate change of password
// ====================================================================

{

  var errors = "";
  var msg = "";
  
  if (!validform(f)) {
    return false;
  }
  else {
    if (f.NewPWD.value.length < 5) {
      errors += "\n  -  New Password is less than 5 characters."
    }
    
    if (f.NewPWD.value != document.frmchgpwd.ConfPWD.value) {
      errors += "\n  -  New Password and Confirm Password are different."
    }
    
    if (f.OldPWD.value == document.frmchgpwd.NewPWD.value) {
      errors += "\n  -  Present Password and New Password are the same."
    }
    
    if (errors) {
      msg = "This form was not sent because of errors.\n";
      msg += "Please do the following before sending again.\n";
      msg += errors;
      alert (msg);
      return false;
    }
    else {
      return true;
    }
  }
}

function NewWindow (strUrl)

// ====================================================================
// Open a new browser window with specified width and height
// ====================================================================

{
  window.open(strUrl,'viewpag',toolbar='no');
}

function openWindow(url, winName, winWidth, winHeight) 
{
  var options = "width=" + winWidth + ", height=" + winHeight + ", toolbar=no, menubar=no, scrollbars=no, resizable=yes, location=no, directories=no, status=no";
  var winOpen = window.open(url, winName, options);
}

function ConfirmDelete(RecType, strDesc, strUrl, ID, strQueryString)
// ====================================================================
// Confirm deletion of record
// ====================================================================

{
  if (confirm("Are you sure you want to permanently delete the following " + RecType +"?\n\n  -  " + strDesc))
    location = strUrl + "?" + "ID=" + ID + "&" + strQueryString
}

function validpassword (f) 

// ====================================================================
// Validate change of password
// ====================================================================

{

  var errors = "";
  var msg = "";
  
  if (!validform(f)) 
  {
    return false;
  }
  else 
  {
    if (f.txtPassword.value.length < 5) 
    {
      errors += "\n  -  Enter a password at least 5 characters long."
    }
    
    if (f.txtPassword.value != f.txtConfirmPassword.value) 
    {
      errors += "\n  -  Make sure that Password and Confirm Password are the same."
    }
    
    if (errors) 
    {
      msg = "This form was not sent because of errors.\n";
      msg += "Please do the following before sending again.\n";
      msg += errors;
      alert (msg);
      return false;
    }
    else 
    {
      return true;
    }
  }
}

function MaximizeWindow ()
// ====================================================================
// Maximize current window
// ====================================================================
{
  if (document.all || document.layers)
    { 
      self.moveTo ( 0,0 );
      self.resizeTo ( screen.availWidth, screen.availHeight);
    } 
}
