﻿// JScript File

// Modified "object finder" code from Dreamweaver (XBrowser)
function findObj(n, d) { 
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document); return x;
}

// Pop-up window for error messages (XBrowser)
function modelesswin(url,mwidth,mheight){ 
  if (document.all&&window.print) 
    eval('window.showModelessDialog(url,"","help:0;resizable:0;status:0;dialogWidth:'+mwidth+'px;dialogHeight:'+mheight+'px")') 
  else 
    eval('window.open(url,"","width='+mwidth+'px,height='+mheight+'px,resizable=0,scrollbars=0")') 
}

// Pop-up window for utilities (XBrowser - allows reloading of pages within popup)
function pop_up(h,w,where){ 
 
var winX = (screen.width/2)-(w/2); 
var winY = (screen.height/2)-(h/2); 
 
size="scrollbars=yes, toolbar=no, hotkeys=no, height=" + h + ", width=" + w + ",top=" + winY + ", left=" + winX; 
win = window.open(where, "myPop", size); 
} 

// Input Function : Force numeric entry into a textbox.
function numbersonly(e){
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
if (unicode<48||unicode>57) //if not a number
return false //disable key press
}
}

// Input Function : Force alpha or numeric entry into a textbox.
function alphanumericonly(e){
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
if ((unicode > 47 && unicode < 59) || (unicode > 64 && unicode < 91) || (unicode > 96 && unicode < 123))
  {
  }
else
  {
  return false;
  }
}
}

// Validate Function : Use RegExp to detect if string entered is a valid eMail format.
function isEmail(str) {
  // are regular expressions supported?
  var supported = 0;
  if (window.RegExp) {
    var tempStr = "a";
    var tempReg = new RegExp(tempStr);
    if (tempReg.test(tempStr)) supported = 1;
  }
  if (!supported) 
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
  return (!r1.test(str) && r2.test(str));
}

// Validate Function : Ensure that form field has non-blank content.
function valField(objName, ErrMessage) {
  var obj = findObj(objName);
  if (obj.value == "") {
    obj.focus();
    modelesswin('errorPop.aspx?ErrCode=' + ErrMessage,280,150);
  return false;
}
  return true;
}

// Validate Function : Ensure that two form fields contain identical content.
function valEqual(objName, objName2, ErrMessage) {
  var obj = findObj(objName);
  var obj2 = findObj(objName2);
  if (obj.value != obj2.value) {
    obj2.focus();
    modelesswin('errorPop.aspx?ErrCode=' + ErrMessage,280,150);
  return false;
}
  return true;
}

// Validate Function : Ensure that form field meets min/max length criteria
function valMinMax(objName, vMin, vMax, ErrMessage) {
  var obj = findObj(objName);
  if ((obj.value.length < vMin) || (obj.value.length > vMax)) {
    obj.focus();
    modelesswin('errorPop.aspx?ErrCode=' + ErrMessage,280,150);
  return false;
}
  return true;
}

// Validate Function : Calling Function for isEmail Function.
function valEmail(objName, ErrMessage) {
  var obj = findObj(objName);
  if (!isEmail(obj.value)) {
    obj.focus();
    modelesswin('errorPop.aspx?ErrCode=' + ErrMessage,280,150);
  return false;
}
  return true;
}

// Validate Function : Ensure that a given age is between two dates.
function valDateBetween(obj1Name, obj2Name, obj3Name, ageMin, ageMax) {
  var obj1 = findObj(obj1Name);
  var obj2 = findObj(obj2Name);
  var obj3 = findObj(obj3Name);
  
  var inDate = new Date(obj3.value, obj2.value-1, obj1.value);
  var nowDate = new Date();
  with (minDate = new Date()) setMonth(getMonth()-(ageMin*12));
  with (maxDate = new Date()) setMonth(getMonth()-(ageMax*12));
  
  if (((nowDate - inDate) - (nowDate - minDate) < 0) || ((nowDate - inDate) - (nowDate - maxDate) > 0)){
    obj1.focus();
    modelesswin('errorPop.aspx?ErrCode=AgeMismatch',280,150);
  return false;
}
  return true;
}

// Validate Function : Ensure that a given age is less than a set limit.
function valDateLessThan(obj1Name, obj2Name, obj3Name, ageCheck) {
  var obj1 = findObj(obj1Name);
  var obj2 = findObj(obj2Name);
  var obj3 = findObj(obj3Name);
  
  var inDate = new Date(obj3.value, obj2.value-1, obj1.value);
  var nowDate = new Date();
  with (checkDate = new Date()) setMonth(getMonth()-(ageCheck*12));
  
  if ((nowDate - inDate) - (nowDate - checkDate) > 0) {
    obj1.focus();
    modelesswin('errorPop.aspx?ErrCode=AgeMismatch',280,150);  
  return false;
}
  return true;
}

// Validate Function : Ensure that a given age is more than a set limit.
function valDateMoreThan(obj1Name, obj2Name, obj3Name, ageCheck) {
  var obj1 = findObj(obj1Name);
  var obj2 = findObj(obj2Name);
  var obj3 = findObj(obj3Name);
  
  var inDate = new Date(obj3.value, obj2.value-1, obj1.value);
  var nowDate = new Date();
  with (checkDate = new Date()) setMonth(getMonth()-(ageCheck*12));
  
  if ((nowDate - inDate) - (nowDate - checkDate) < 0) {
    obj1.focus();
    modelesswin('errorPop.aspx?ErrCode=AgeMismatch',280,150);
  return false;
}
  return true;
}
