/*******************************************************************************
* File Name : jslibrary.asp                                                    *
*                                                                              *
* Copyright : DynaCal, llc., www.dynaCal.com                                   *
* Created   : 11/18/2003                                                       *
* Author    : Shawn Ramirez, shawnr@dynacal.com                                *
* Purpose   : General JavaScript Functions						               *
*******************************************************************************/ 

//Global setting for whether or not a value is required.  This can be
//overwritten in most functions by submitting the emptyOK parameter.
  var defaultEmptyOK = false

// Whitespace Characters
  var whitespace = " \t\n\r";
  

//Close current window after refreshing parent window.

  function closerefresh()   //can't be in a library file
	{
	window.opener.location.reload();
	window.close();
	}

  function warnInvalid (theField, fieldLabel) 
  {
    theField.focus()
    alert('Invalid Field: ' + fieldLabel)
    return false  
  }

  function checkStringInRange (theField, fieldLabel, minValue, maxValue, emptyOK) {
      if (checkStringInRange.arguments.length == 4) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    if (!isStringInRange(theField.value, minValue, maxValue))
      warnInvalid (theField, fieldLabel)
    else return true;
  }
  

  

//Returns true if string s is empty
  function isEmpty(s) {   
    return ((s == null) || (s.length == 0))
  }

// Returns true if string s is empty or whitespace characters only.
  function isWhitespace (s) {   
    var i;
    if (isEmpty(s)) return true;
    for (i=0; i<s.length; i++) {   
      var c = s.charAt(i);
      if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
  }
  
//Returns true if character c is a digit (0 .. 9)
  function isDigit (c) {   
    return ((c >= "0") && (c <= "9"))
  }

//Returns true if all characters in string s are digits
  function isInteger (s) {   
    var i;
    if (isEmpty(s)) 
      if (isInteger.arguments.length == 1) return defaultEmptyOK;
      else return (isInteger.arguments[1] == true);
    for (i=0; i<s.length; i++) {
      var c = s.charAt(i);
      if (!isDigit(c)) return false;
    }
    return true;  
  }  
  
  function isIntegerMsg(pValue, pName)
  {
  if (isInteger(pValue) == false)
	{return pName + ' must be an integer \n';}
  }
  
  
//Returns true if string s has a length in the range of integer arguments a and b
  function isStringInRange (s, a, b) {   
    if (isEmpty(s)) 
      if (isStringInRange.arguments.length == 3) return defaultEmptyOK;
      else return (isStringInRange.arguments[3] == true);
	var len = s.length;
    return ((len >= a) && (len <= b));
  }  
  
//Returns true if string s is in valid email format
  function isEmail (s) {   
    if (isEmpty(s)) 
      if (isEmail.arguments.length == 1) return defaultEmptyOK;
      else return (isEmail.arguments[1] == true);
    if (isWhitespace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@")) { 
      i++ 
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;
    while ((i < sLength) && (s.charAt(i) != ".")) { 
      i++ 
    }
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
  }
  
function isDate(dateStr) {

		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;

		var matchArray = dateStr.match(datePat); // is the format ok?

		if (matchArray == null) {
		    return false;
		}

		month = matchArray[1]; // parse date into variables
		day = matchArray[3];
		year = matchArray[5];

		if (month < 1 || month > 12) { // check month range
		    return false;
		}

		if (day < 1 || day > 31) {
		    return false;
		}

		if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		    return false;
		}

		if (month == 2) { // check for february 29th
		    var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		    if (day > 29 || (day==29 && !isleap)) {
		        return false;
		    }
		}
		return true;                           // date is valid
}
	  
  
function CheckforNumber(object, friendlydesc, low, high) {
			
	number = parseInt(object.value)
	if ( isNaN(number) ) {
		return ("Please enter a number for " + friendlydesc + " \n")
	}else {
		if (number < low | number > high) {
			return ("The number entered must be between " + low + " and " + high + " \n")
		}
		return ("")
	}
			
}    
    
 
 function checkEmail (theField, fieldLabel, emptyOK) {
      if (checkEmail.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return '';
     if (!isEmail(theField.value)) {
      //warnInvalid (theField, fieldLabel)
      return fieldLabel + ' ' + ' is not valid. \n'  ;}
    else return '';
  }    
  
  function checkStringInRange (theField, fieldLabel, minValue, maxValue, emptyOK) {
    if (checkStringInRange.arguments.length == 4) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return '';
    if (!isStringInRange(theField.value, minValue, maxValue))
      return fieldLabel + ' must not be empty. \n';
      //warnInvalid (theField, fieldLabel)
    else return '';
  }  
  
    function checkPhone(pField, pLabel)
  {
    if (pField.length == 0)
      {return pLabel + ' must be in form 555-555-5555. \n';}
    else
      {
		var pattern3 = /\d{3}\-\d{3}\-\d{4}/; 
		if(pattern3.test(pField.value)) 
			{return '';}
		else
			{return pLabel + ' must be in form 555-555-5555. \n';}
      }
  }
//Opens a new browser window
  function openWin(url,name,toolbar,width,height,status,scrollbars,resize,menubar) {
  //window.open("ENAddress1.asp", "EmailNotification");
    window.open(url,name,"toolbar=" + toolbar + ",width=" + width + ",height=" + height + ",status=" + status + ",scrollbars=" + scrollbars + ",resize=" + resize + ",menubar=" + menubar);  
  }  
  
  
//Resize a window after it has been opened.
//Ex: <body onload="javascript:reSizeToImage();">
	function reSizeToImage(pWidth, pHeight){
		var isNN,isIE;
		if (parseInt(navigator.appVersion.charAt(0))>=4){
			isNN=(navigator.appName=="Netscape")?1:0;
			isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
		if (isIE){
			window.resizeTo(100,100);
			width=pWidth;
			height=pHeight;
			window.resizeTo(width,height);}
		if (isNN){
			window.innerWidth=pWidth;
			window.innerHeight=pHeight;}
			}  

	function reSizeWindow(pWidth, pHeight){
		var isNN,isIE;
		if (parseInt(navigator.appVersion.charAt(0))>=4){
			isNN=(navigator.appName=="Netscape")?1:0;
			isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
		if (isIE){
			window.resizeTo(100,100);
			width=pWidth;
			height=pHeight;
			window.resizeTo(width,height);}
		if (isNN){
			window.innerWidth=pWidth;
			window.innerHeight=pHeight;}
			}  
			
		
			
//check to see if a character (non numeric is in the vString)
function HasCharacter(vString) 
{
  var re_alpha = /[a-zA-Z]/;
 if (!re_alpha.exec(vString.value))
	return false;
 else
   return true;
 }

function HasCharacterOnly(vString)
{
var re_alphanum = /[0-9]/;
 if (re_alphanum.exec(vString.value))
   return false;
 else
   return true;

}
 
function checkHasAlpha(pValue, pTitle)
{
  if (HasCharacter(pValue) == true)
    {return '';}
  else
    return pTitle + ' must contain a character. \n';
}

function checkHasAlphaOnly(pValue, pTitle)
{
  if (HasCharacterOnly(pValue) == true)
    {return '';}
  else
    return pTitle + ' must contain only characters. \n';
}

function isInteger1(sInteger) {
    var isInt = true;
    inputStr = sInteger.toString(); // in case not a string already
    for (var i = 0; i < inputStr.length; i++) {
        var oneChar = inputStr.charAt(i);
        if (oneChar < "0" || oneChar > "9") {
            isInt = false;
            i = inputStr.length; // break out of loop when bad char found
        }
    } return isInt;
}
