var submitcount = 0;

function popupFilter(form, screen2) {

    if(screen2 != "SECURITY2"){
  if (form.selection_filter.checked == true)
  {
      form.selection_filter.checked = false;
      var popupWin = window.open("setfilter.asp?screen=" + screen2, "", "width=1175, height=675, resizable=yes, scrollbars=yes");
  }
  else
  {
    form.filter_name.value = "";
    form.filter_id.value = "";
    var popupWin = window.open("unsetfilter.asp?screen=" + screen2, "", "width=1, height=1, resizable=yes, scrollbars=yes");
  }
   }
    else {
  if (form.read_only_selection_filter.checked == true) {
    form.read_only_selection_filter.checked = false;
    var popupWin = window.open("setfilter.asp?screen=" + screen2, "", "width=1175, height=675, resizable=yes, scrollbars=yes");
  }
  else
  {
    form.filter_name2.value = "";
    form.filter_id2.value = "";
    var popupWin = window.open("unsetfilter.asp?screen=" + screen2, "", "width=1, height=1, resizable=yes, scrollbars=yes");
  }

    }

}



function printableReportEFT(da_URL)
{

  var reportWin = window.open(da_URL, "", "width=800, height=500, resizable=yes, scrollbars=yes, menubar=yes");
  return false;
}


function popupFilter3(form, screen)
{
    if (form.gift_selection_filter.checked == true)
    {
        form.gift_selection_filter.checked = false;
        var popupWin = window.open("setfilter.asp?screen=" + screen, "", "width=1175, height=675, resizable=yes, scrollbars=yes");
    }
    else
    {
      form.filter_name3.value = "";
        form.filter_id3.value = "";
        var popupWin = window.open("unsetfilter.asp?screen=" + screen, "", "width=1, height=1, resizable=yes, scrollbars=yes");
    }
}

function popupFilter4(form, screen)
{
    if (form.contact_selection_filter.checked == true)
    {
        form.contact_selection_filter.checked = false;
        var popupWin = window.open("setfilter.asp?screen=" + screen, "", "width=1175, height=675, resizable=yes, scrollbars=yes");
    }
    else
    {
      form.filter_name4.value = "";
        form.filter_id4.value = "";
        var popupWin = window.open("unsetfilter.asp?screen=" + screen, "", "width=1, height=1, resizable=yes, scrollbars=yes");
    }
}


function printableReport_fin(da_URL)
{

  var reportWin = window.open(da_URL, "", "width=750, height=500, resizable=yes, scrollbars=yes, menubar=yes, toolbar=yes");
  return false;
}

function printableReport(da_URL)
{
  var reportWin = window.open(da_URL, "", "width=750, height=500, resizable=yes, scrollbars=yes, menubar=yes");
  return false;
}

function printableReportMulti(da_URL)
{
  var reportWin = window.open(da_URL, "", "width=850, height=500, resizable=yes, scrollbars=yes, menubar=yes");
  return false;
}
function printableReportSmall(da_URL)
{
    var reportWin = window.open(da_URL, "", "width=600, height=350, resizable=yes, scrollbars=yes, menubar=yes");
  return false;
}

function printableReportSmall2(da_URL)
{
     window.open(da_URL, "", "width=600, height=350, resizable=yes, scrollbars=yes, menubar=yes");

}

function printableReportSmall3(da_URL)
{
     window.open(da_URL, "", "width=600, height=400, resizable=yes, scrollbars=yes, menubar=yes");

}


function poptastic(url)
{
    var newWindow;
    var props = 'scrollBars=yes,resizable=yes,toolbar=no,menubar=no,location=no,directories=no,width=940,height=600';
    newWindow = window.open(url, "Add_from_Src_to_Dest", props);
}

function memoPad(da_URL)
{
  var reportWin = window.open(da_URL, "", "width=680, height=470, resizable=yes, scrollbars=auto, menubar=no");
  return false;
}

function wopen(url, name, w, h) {
    // Fudge factors for window decoration space.
    // In my tests these work well on all platforms & browsers.
    w += 32;
    h += 96;
    wleft = (screen.width - w) / 2;
    wtop = (screen.height - h) / 2;
    // IE5 and other old browsers might allow a window that is
    // partially offscreen or wider than the screen. Fix that.
    // (Newer browsers fix this for us, but let's be thorough.)
    if (wleft < 0) {
        w = screen.width;
        wleft = 0;
    }
    if (wtop < 0) {
        h = screen.height;
        wtop = 0;
    }
    $(document).ready(function() {

        var win = window.open(url, name,
    'width=' + w + ', height=' + h + ', ' +
    'left=' + wleft + ', top=' + wtop + ', ' +
    'location=yes, menubar=no, directories=no,' +
    'status=no, toolbar=no, scrollbars=yes, resizable=no');
        // Just in case width and height are ignored
        win.resizeTo(w, h);
        // Just in case left and top are ignored
        win.moveTo(wleft, wtop);
        win.focus();
    });
}

function todaysDate() {

  var Today = new Date();
  var yr = Today.getFullYear();
  var vmonth = Today.getMonth() + 1;
  var vday = Today.getDate();
  Today = vmonth + "/" + vday + "/" + yr;
  return Today;
}

function todaysDateEurope() {

  var Today = new Date();
  var yr = Today.getFullYear();
  var vmonth = Today.getMonth() + 1;
  var vday = Today.getDate();
  Today = vday + "/" + vmonth + "/" + yr;
  return Today;
}

function insertTodaysDate(field) {

  var Today = todaysDate();

  if (field.value.length == 0) {
    field.value = Today + " - ";}
  else {
    field.value = Today + " - \n" + field.value;}
	setCaretPosition(field, Today.length + 3);
	field.focus();
}

function insertTodaysDateEurope(field) {

  var Today = todaysDateEurope();

  if (field.value.length == 0) {
    field.value = Today + " - ";}
  else {
    field.value = Today + " - \n" + field.value;}
	setCaretPosition(field, Today.length + 3);
	field.focus();
}

function doGetCaretPosition (ctrl) {
	var CaretPos = 0;
	// IE Support
	if (document.selection) {
		ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
			CaretPos = ctrl.selectionStart;
	return (CaretPos);
}

function setCaretPosition(ctrl, pos){
	if(ctrl.setSelectionRange)	{
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}	else if (ctrl.createTextRange) {
	var range = ctrl.createTextRange();
	range.collapse(true);
	range.moveEnd('character', pos);
	range.moveStart('character', pos);
	range.select();	}
}

function checkDate(field) {
     var err = 0;
     var yr;
     var vmonth;
     var vday;
     var Today;
     a = field.value;


     if (((a.length == 1) || (a.length == 2)) && (! isNaN(a))) {
  Today = new Date();
  yr = Today.getFullYear();
  vmonth = Today.getMonth() + 1;
  field.value = vmonth + "/" + a + "/" + yr;
  a = vmonth + "/" + a + "/" + yr;
     }

     if (a == "/") {
  Today = new Date();
  yr = Today.getFullYear();
  vmonth = Today.getMonth() + 1;
  vday = Today.getDate();
  field.value = vmonth + "/" + vday + "/" + yr;
     }

     else if (a.length != 0)
     {
    z = a.split("/",3);

    if (z.length == 2) {
      Today = new Date();
      yr = Today.getFullYear();
      a = a + "/" + yr;
      field.value = a;
      z = a.split("/",3);
    }

    if (z.length == 3) {

        b = z[0]; // month
        d = z[1]; // day
        f = z[2]; // year

        if (isNaN(b)) err = 1;
        if (isNaN(d)) err = 1;
        if (isNaN(f)) err = 1;
        if ((b < 1) || (b > 12)) err = 1;
        if (b.length == 0) err = 1;
        if ((d < 1) || (d > 31)) err = 1;
        if (d.length == 0) err = 1;
        if ((f.length != 4) && (f.length != 2)) err = 1;
        if (f.length == 2) {
            if (parseInt(f) >= 30) {
                field.value = b + "/" + d + "/" + "19" + f;
                f = "19" + f;
            }
            else {
                field.value = b + "/" + d + "/" + "20" + f;
                f = '20' + f;
            }
        }
      if ((f<1753) || (f>9999)) err = 1;
      if ((b==4) || (b==6) || (b==9) || (b==11)){
        if (d==31) err = 1;
        }
      if (b==2){
        var g=parseInt(f/4);
        if (isNaN(g)) {
          err=1;
          }
        if (d>29) err=1;
        if ((d==29) && ((f/4)!=parseInt(f/4))) err=1;
      }
    }
    else {
        err = 1;
    }

    if (err != 1) {
        Today = new Date();
        if ( f > Today.getFullYear() + 100 ) {
            alert("Date can not be more than 100 years in the future.");
            field.focus();
            field.select();
           
        }
    }
    
    if (err==1) {
      alert("You have entered an invalid date or date format.  \nPlease reenter using the format MM/DD/YYYY, M/D/YYYY, MM/DD/YY, M/D/YY, M/D or MM/DD.");
      field.focus();
      field.select();
    }
    }
}

function checkDateEurope(field) {



     var err = 0;
     var yr;
     var vmonth;
     var vday;
     var Today;
     a = field.value;


     if (((a.length == 1) || (a.length == 2)) && (! isNaN(a))) {
  Today = new Date();
  yr = Today.getFullYear();
  vmonth = Today.getMonth() + 1;
  field.value = a + "/" + vmonth + "/" + yr;
  a = a + "/" + vmonth + "/" + yr;
     }

     if (a == "/") {
     	
     	
  Today = new Date();
  yr = Today.getFullYear();
  vmonth = Today.getMonth() + 1;
  vday = Today.getDate();


  field.value = vday + "/" + vmonth + "/" + yr;
     }

     else if (a.length != 0)
     {
    z = a.split("/",3);

    if (z.length == 2) {
      Today = new Date();
      yr = Today.getFullYear();
      a = a + "/" + yr;
      field.value = a;
      z = a.split("/",3);
    }

    if (z.length == 3) {

      d = z[0]; // day
      b = z[1]; // month
      f = z[2]; // year

      if (isNaN(b)) err = 1;
      if (isNaN(d)) err = 1;
      if (isNaN(f)) err = 1;
      if ((b<1) || (b>12)) err = 1;
      if (b.length == 0) err = 1;
      if ((d<1) || (d>31)) err = 1;
      if (d.length == 0) err = 1;
      if ((f.length != 4) && (f.length != 2)) err = 1;
      if (f.length == 2) {
        if (parseInt(f) >= 20) {
          field.value = b + "/" + d + "/" + "19" + f;
          f = "19" + f;
          }
        else {
          field.value = b + "/" + d + "/" + "20" + f;
          f = '20' + f;
          }
        }
      if ((f<1753) || (f>9999)) err = 1;
      if ((b==4) || (b==6) || (b==9) || (b==11)){
        if (d==31) err = 1;
        }
      if (b==2){
        var g=parseInt(f/4);
        if (isNaN(g)) {
          err=1;
          }
        if (d>29) err=1;
        if ((d==29) && ((f/4)!=parseInt(f/4))) err=1;
      }
    }
    else {
        err = 1;
    }

    if (err != 1) {
        Today = new Date();
        if (f > Today.getFullYear() + 100) {
            alert("Date can not be more than 100 years in the future.");
            field.focus();
            field.select();

        }
    }
    if (err==1) {
      alert("You have entered an invalid date or date format.  \nPlease reenter using the format DD/MM/YYYY, D/M/YYYY, DD/MM/YY, D/M/YY, D/M or DD/MM.");
      field.focus();
      field.select();
    }
    }
}


function checkMonth(field)
   {
     var err = 0;     a = field.value;
     if (a.length != 0)
     {
  if ((a == "1") || (a == "2") || (a == "3") || (a == "4") || (a == "5") || (a == "6")
    || (a == "7") || (a == "8") || (a == "9")) field.value = "0" + a;

  a = field.value;

  if ((a != "01") && (a != "02") && (a != "03") && (a != "04") && (a != "05") && (a != "06")
    && (a != "07") && (a != "08") && (a != "09") && (a != "10") && (a != "11") && (a != "12")) err=1;
  if (err==1) {
    alert("You have entered an invalid month.  \nPlease reenter in the format MM");
    field.focus();
    field.select();
  }
     }
  }

function MonthDayMask (field) {

  var p;
  p = field.value;

  if (p.length == 2) { field.value = p + '/'; }
}

function checkMonthDay(field) {
     var err = 0;
     a = field.value;


     if (a.length != 0)
     {
    m = a.substr(0,2); // month
    d = a.substr(3,2); // day

    if (a.length == 5) {

      if (isNaN(m)) err = 1;
      if (isNaN(d)) err = 1;

      if ((m<1) || (m>12)) err = 1;
      if (m.length == 0) err = 1;

      if ((d<1) || (d>31)) err = 1;
      if ((m==4) || (m==6) || (m==9) || (m==11)){
        if (d==31) err = 1;
        }

      // Do not allow Feburary 29th since it doesn't occur every year
      if ((m==2) && (d>28)) err=1;
    }
    else
      err = 1;

    if (err==1) {
      alert("You have entered an invalid date or date format.  \nPlease reenter using the format MM/DD");
      field.focus();
      field.select();
    }
    }
}

function checkMonthDayEurope(field) {
     var err = 0;
     a = field.value;


    if (a.length != 0) {
    	d = a.substr(0,2); // day
    	m = a.substr(3,2); // month

    	if (a.length == 5) {

    	    if (isNaN(m)) err = 1;
            if (isNaN(d)) err = 1;

            if ((m<1) || (m>12)) err = 1;
            if (m.length == 0) err = 1;

            if ((d<1) || (d>31)) err = 1;
            if ((m==4) || (m==6) || (m==9) || (m==11)) {
				if (d==31) err = 1;
			}
			// Do not allow Feburary 29th since it doesn't occur every year
			if ((m==2) && (d>28)) err=1;
		}
		else {
			err = 1;
		}
	}
    if (err==1) {
      alert("You have entered an invalid date or date format.  \nPlease reenter using the format DD/MM");
      field.focus();
      field.select();
      return false;
    }
    else {
	  return true;
    }
}

function checkMonthYear(field) {
     var err = 0;
     a = field.value;

     if (a.length != 0)
     {
    z = a.split("/",2);

    if (z.length == 2) {

      b = z[0]; // month
      f = z[1]; // year

      if ((b<1) || (b>12)) err = 1;
      if (b.length == 0) err = 1;

      if ((f<0) || (f>9999)) err = 1;
      if ((f.length != 4) && (f.length != 2)) err = 1;
      if (b.length == 1) {
        b = "0" + b;
        field.value = b + "/" + f;
        }
      if (f.length == 2) {
        field.value = b + "/" + "20" + f;
        f = '20' + f;
        }
    }
    else
      err = 1;

    if (err==1) {
      alert("You have entered an invalid date or date format.  \nPlease reenter using the format MM/YYYY, M/YYYY, MM/YY or M/YY.");
      field.focus();
      field.select();
    }
    }
}


function confirm_delete() {
    var str;
    str = (arguments.length!=0) ? arguments[0] : "this record";
  if (confirm("Are you sure you want to delete "+str+"?")) {
    return true;
  }
  else {
    return false;
     }
  }


function confirm_donor_delete(inputStr) {
  if (confirm("Are you sure you want to delete donor ID " + inputStr + "?  This will delete all associated records (gifts, pledges, etc).")) {
    return true;
  }
  else {
    return false;
     }
  }

function formatCurrency(num,curr_symbol) {
  num = num.toString().replace(curr_symbol,'');
  num = num.toString().replace(/\$|\,/g,'');

  if(isNaN(num) || num == '')
    num = "0";

  sign = (num == (num = Math.abs(num)));
  num = Math.floor(num*100+0.50000000001);
  cents = num%100;
  num = Math.floor(num/100).toString();

  if(cents<10)
    cents = "0" + cents;

  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
    num = num.substring(0,num.length-(4*i+3)) + ',' + num.substring(num.length-(4*i+3));

  return (((sign)?'':'-') + curr_symbol + num + '.' + cents);
}


function changeCodeDisplay(form)
{
  document.getElementById('divGLAvailable').style.display = "none";
  document.getElementById('divSolAvailable').style.display = "none";
  document.getElementById('divSolicitation').style.display = "none";
  document.getElementById('divMCAT').style.display = "none";
  document.getElementById('divLink').style.display = "none";
  document.getElementById('divGL').style.display = "none";
  document.getElementById('divMembershipType').style.display = "none";
  document.getElementById('divLeewayDays').style.display = "none";
  document.getElementById('divMilestone').style.display = "none";
  //document.getElementById('div_region_fiscal_year').style.display = "none";

  if (form.field_name.value == "CAMPAIGN")
  {
    document.getElementById('divGLAvailable').style.display = "block";
    return;
  }
  if (form.field_name.value == "SUB_SOLICIT_CODE")
  {
    document.getElementById('divSolAvailable').style.display = "block";
    document.getElementById('divSolicitation').style.display = "block";
    return;
  }
  if (form.field_name.value == "SOLICIT_CODE")
  {
    document.getElementById('divSolicitation').style.display = "block";
    return;
  }
  if (form.field_name.value == "MCAT")
  {
    document.getElementById('divMCAT').style.display = "block";
    return;
  }
  if (form.field_name.value == "TY_LETTER_NO")
  {
    document.getElementById('divMilestone').style.display = "block";
    return;
  }
  if (form.field_name.value == "MEMBERSHIP_TYPE")
  {
    document.getElementById('divLeewayDays').style.display = "block";
    return;
  }
  if (form.field_name.value == "MEMBERSHIP_STATUS")
  {
    document.getElementById('divMCAT').style.display = "block";
    document.getElementById('divMembershipType').style.display = "block";
    return;
  }
  if (form.field_name.value == "LINK_CODE")
  {
    document.getElementById('divLink').style.display = "block";
    return;
  }
  if (form.field_name.value == "GL_CODE")
  {
    document.getElementById('divGL').style.display = "block";
    return;
  }
}


function changeUDFDisplay(form)
{
  if (form.display_type.value == "T")
    {
    document.getElementById('divTextbox').style.display = "block";
    }
  if ((form.display_type.value == "C") || (form.display_type.value == "D") || (form.display_type.value == "M") || (form.display_type.value == "L"))
    {
    document.getElementById('divTextbox').style.display = "none";
    }

  if (form.display_type.value == "C")
    {
    document.getElementById('divYesNo').style.display = "block";
    }
  else
    {
    document.getElementById('divYesNo').style.display = "none";
    }
}

function isFieldBlank(theField) {
  if(theField.value == "")
    return true;
  else
    return false;
}


function isFieldNumber(theField) {
        if(isNaN(theField.value)) {
            alert("That is not a valid number.  Please reenter.")
    theField.focus();
    theField.select();
  }
}

function setNumber(theField) {
        if ((isNaN(theField.value)) || (theField.value == "")){
            return 0;
  }
  else {
      return theField.value;
  }
}



function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 




function isValidEmail(field) {
     var str;
     var errorMsg="";

	field.value = trim(field.value);
	

     str = field.value;

   var whiteSpace = /\s/;
   var someEmail = /[^@ \/:;,]+@[^@ \/:;,]+\.[^@. \/:;,]+/;
   var sCurValue = str;
   var matchUp = str.match(someEmail);
   if(str.length > 0){
     if (str != matchUp){
        errorMsg = "Please enter a valid Email Address.";
     }else{
        if ((sCurValue.charAt(0) == " ") || (sCurValue.charAt(sCurValue.length-1) == " ")) errorMsg = "'"+friendlyName+"' may not contain leading or trailing spaces";
     }
   }
   if(errorMsg!=""){
      alert(errorMsg);
      field.focus();
      field.select();
   }
   /*
   if(str.length > 0) {
     if ((str.indexOf(".")<0) || (str.indexOf("@") < 0)){
            alert("You have entered an invalid email address!");
            field.focus();
            field.select();
     }
   }*/

}






function checkFieldName(field) {

     var err = 0;
     var a = field.value;
     var b = a.substring(0,1);
     var searchstring1;
     var searchstring2;

     if (a.length != 0)
     {
  searchstring1 = /[\?\&\%\+\=\-\)\(\^\$\#\@\!\~\`\'\"\ \:\*\;\<\>\,\.\/\\]/;
  if (a.search(searchstring1) != -1) err = 1;


if (field.name != "Batch_no"){
  searchstring2 = /\d/;
  if (b.search(searchstring2) != -1) err = 2;
}

  if (err==1) {
  
	  if (field.name == "Batch_no")
	  {
	    alert("You have entered an invalid Batch number.  \nYou may use alpha-numerics and underscores but not special characters such as spaces and apostrophe's.");
	  }
	  else
	  {
	    alert("You have entered an invalid field name.  \nYou may use alpha-numerics and underscores but not special characters such as spaces and apostrophe's.");
	  }  
  field.focus();
  field.select();
    
  }

    if (err==2) {
      alert("You have entered an invalid field name.  \nField names may not begin with a number.");
      field.focus();
      field.select();
  }
    }
}


function checkCode(field, allow_period) {

     var a = field.value;
     var searchstring1;

     if (a.length != 0)
     {
 if (allow_period) {  
  searchstring1 = /[\?\&\%\+\=\)\(\^\$\#\@\!\~\`\'\"\ \:\*\;\<\>\,\/\\]/;
  }
  else {
  searchstring1 = /[\?\&\%\+\=\)\(\^\$\#\@\!\~\`\'\"\ \:\*\;\<\>\,\.\/\\]/;
  }
  
  if (a.search(searchstring1) != -1) {
    alert("You have entered an invalid code.  \nYou may use alpha-numerics and underscores but not special characters such as spaces and apostrophe's.");
    field.focus();
    field.select();
  }
    }
}


function check_null(form, screen) {


  // First make sure the form has not already been submitted
  if (submitcount > 0)
  {
  alert("This form has already been submitted once.");
  return false;
  }
  else
  {

    // Make sure required fields are entered

    if (screen == "MAIN") {
      var databaseName;
      databaseName = form.db.value;

      //britzedek customization
      if ((databaseName.indexOf("brittzedekshalom") != -1) && (form.donor_id.value != "D")) {
          if (!isFieldBlank(form.email)) {

            if (document.getElementById("EMAIL_STATUS").selectedIndex == 0 || document.getElementById("EMAIL_STATUS").selectedIndex == -1) {
              alert("Please choose an email status");
              return false;
            }
          }

          if (document.getElementById("address_type").selectedIndex == 0 || document.getElementById("address_type").selectedIndex == -1) {
                alert("Please choose an address type");
                return false;
              }

          if (document.getElementById("donor_type").value == 'IN') {
            if (document.getElementById("HOUSEHOLD").selectedIndex == 0 || document.getElementById("HOUSEHOLD").selectedIndex == -1) {
                  alert("Please choose a household type");
                  return false;
                }

            if (document.getElementById("SOURCE_CODE").selectedIndex == 0 || document.getElementById("SOURCE_CODE").selectedIndex == -1) {
                  alert("Please choose a source code");
                  return false;
                }
          }

        }


      if ((isFieldBlank(form.last_name)) && (form.donor_id.value != "D" && (databaseName.indexOf("brittzedekshalom") == -1))) {
         alert("Please enter a last name or organization name.");
         return false;
      } else if ((form.flags.length < 1) && (databaseName.indexOf("brittzedekshalom") != -1) && (form.donor_id.value != "0") && (form.donor_id.value != "D")) {
        alert("Please select at least one flag for the person.");
        return false;
      } else {
        submitcount ++;
        return true;
      }
    }

    if (screen == "MAINGIFT") {
  if (isFieldBlank(form.last_name)) {
    alert("Please enter a last name or organization name.");
    return false;
  }
  else if (isFieldBlank(form.gift_date)) {
    alert("Please enter a gift date.");
    return false;
  }
  else {
    submitcount ++;
    return true;
  }
    }

    if (screen == "CONTACT") {
      var databaseName;
      databaseName = form.db.value;
      if (databaseName.indexOf("brittzedekshalom") != -1) {
        if (isFieldBlank(form.activity_code)) {
          alert("Please enter an activity code.");
          return false;
        } else {
          submitcount ++;
          return true;
        }
      } else if (databaseName.indexOf("citizens_services") != -1) {
      	if (document.getElementById("activity_code").selectedIndex == -1 || document.getElementById("activity_code").selectedIndex == 0)
	{
		alert("Please enter Activity Code.");
		return false;
	} else if (document.getElementById("OPS").selectedIndex == -1 || document.getElementById("OPS").selectedIndex == 0) {
		alert("Please enter Operation.");
		return false;
	} else if (document.getElementById("CAMP").selectedIndex == -1 || document.getElementById("CAMP").selectedIndex == 0) {
		alert("Please enter Campaign.");
		return false;
     	} else if (document.getElementById("due_date").value == "") {
     		alert("Please enter Due Date.");
		return false;
	} else {
		submitcount ++;
	        return true;
        }

      } else {
        submitcount ++;
        return true;
      }
    }


    if (screen == "GIFT")  {

  var allInputs = document.getElementsByTagName("input");
  var allCheckboxes = new Array();
  var ownershipCheckboxes = new Array();
  var entityCheckboxes = new Array();

  var databaseName;
  databaseName = form.db.value;

  if (allInputs.length > 0) {

  for (i = 0; i < allInputs.length; i++) {

    if (allInputs[i].type == "checkbox") {

      allCheckboxes.push(allInputs[i]);
    }
  }

  if ((allCheckboxes != null) && (allCheckboxes.length > 0)) {

    for (i = 0; i < allCheckboxes.length; i++) {

      if (allCheckboxes[i].checked == true) {

        if (allCheckboxes[i].name == "M_GIFT_OWNERSHIP") {

          ownershipCheckboxes.push(allCheckboxes[i]);
        }
        else if (allCheckboxes[i].name == "M_GIFT_ENTITY") {

          entityCheckboxes.push(allCheckboxes[i]);
        }
      }
    }
  }
  }

  if (databaseName.indexOf("brittzedekshalom") != -1 && form.gift_id.value != 'D') {
  if (document.getElementById("gl_code").selectedIndex == -1 || document.getElementById("gl_code").selectedIndex == 0)
      {
            alert("Please enter GL Code.");
            return false;
      }

  if (document.getElementById("ty_letter_no").selectedIndex == -1 || document.getElementById("ty_letter_no").selectedIndex == 0)
      {
            alert("Please enter Thank-you Code.");
            return false;
      }

  if (document.getElementById("gift_type").selectedIndex == -1 || document.getElementById("gift_type").selectedIndex == 0)
      {
            alert("Please enter Type of Gift.");
            return false;
      }


  if (document.getElementById("gl_code").value == "40110" || document.getElementById("gl_code").value == "40120") {
    if (isFieldBlank(form.NUM_OF_MEMB)) {
      alert("Please enter Number of Membership.");
      return false;
    }
   }

  }

  if (databaseName.indexOf("citizens_services") != -1 && form.gift_id.value != 'D') {
    if (document.getElementById("gl_code").selectedIndex == -1 || document.getElementById("gl_code").selectedIndex == 0)
        {
              alert("Please enter GL Code.");
              return false;
        }

    if (document.getElementById("amount").value == "$0.00")
        {
              alert("Gift Amount should be greater than 0");
              return false;
        }

    if (document.getElementById("campaign").selectedIndex == -1 || document.getElementById("campaign").selectedIndex == 0 )
          {
                alert("Please select campaign.");
                return false;
      }
    if (document.getElementById("DOES_REPORTING").selectedIndex == -1 || document.getElementById("DOES_REPORTING").selectedIndex == 0 )
              {
                    alert("Please select an operation.");
                    return false;
      }
  }



  if ((isFieldBlank(form.gift_date)) && (form.gift_id.value != "D")) {
    alert("Please enter a gift date.");
    return false;
  }
  else if (databaseName.indexOf("naral") != -1 && databaseName.indexOf("naral_co") < 0)
  {
     if (form.gift_id.value != "D") {
     	if ((ownershipCheckboxes.length < 1) || (entityCheckboxes.length < 1)) {
    		if ((ownershipCheckboxes.length < 1) && (entityCheckboxes.length < 1)) {
      			alert("Please Select an Ownership and Entity for this gift.");
      			return false;
    		} else if (entityCheckboxes.length < 1) {
      			alert("Please Select an Entity for this gift.");
      			return false;
    		} else {
		      alert("Please Select an Ownership for this gift.");
      		      return false;
    		}
        } else {
        	return true;
        }
     }     
  }
  else {
    submitcount ++;
    allInputs = null;
    allCheckboxes = null;
    ownershipCheckboxes = null;
    entityCheckboxes = null;
    return true;
  }
    }

    if (screen == "LINK")  {
  if ((form.link_id.value != "D") && ((isFieldBlank(form.link_code)) ||
  (isFieldBlank(form.txtLinkID)) )) {
    alert("Please enter both link type and linked name.");
    return false;
  }
  else {
    submitcount ++;
    return true;
  }
    }

    if (screen == "LINKEDGIFT")  {
  if ((form.record_type.value == "P") && (isFieldBlank(form.start_date)))
  {
    alert('Please enter a start date.');
    return false;
  }

  if (isFieldBlank(form.txtLinkID))
  {
    alert('Please enter a linked name.');
    return false;
  }
  else {
    submitcount ++;
    return true;
  }
    }

    if (screen.substring(0,6) == "PLEDGE")  {
    

  if ((form.frequency.value != "U") && (screen == "PLEDGESCHEDULE"))
  {
    alert('You can only set up a schedule if frequency is set to "U" (Unusual)');
    return false;
  }

var error = "";
  if (form.gift_id.value != "D" && isFieldBlank(form.gift_date)){
    error = " Please enter pledge date";
    $("#gift_date").focus();
  } 
  
  if  (isFieldBlank(form.start_date)) {
    error +=" Please enter start date";
    $("#start_date").focus();
 
  }
   if(isFieldBlank(form.frequency)){
     error +=" Please enter Frequency.";
    $("#frequency").focus();

}
if ($("#EFT").val() == "Y" && $("#vault_id").val() == "") {
    error += " EFT is enabled, please select a Payment Method.";
    $("#vault_id").focus();

}
   
   if (error != "") {
       alert(error);
      return false;
    }
   else {
 
    submitcount ++;
    return true;
  }
    }

    if (screen == "SCHEDULE")  {


  if ((isFieldBlank(form.payment_date)) || (form.payment_amount == "$0.00")) {
      alert("Please enter both payment date and payment amount.");
      return false;
    }


    else {
     var err = 0;
     a = form.payment_date.value;

     if (a.length != 0)
     {
    z = a.split("/",2);

    if (z.length == 2) {

      b = z[0]; // month
      f = z[1]; // year

      if ((b<1) || (b>12)) err = 1;
      if (b.length == 0) err = 1;

      if ((f<0) || (f>9999)) err = 1;
      if ((f.length != 4) && (f.length != 2)) err = 1;
      if (b.length == 1) {
        b = "0" + b;
        field.value = b + "/" + f;
        }
      if (f.length == 2) {
        field.value = b + "/" + "20" + f;
        f = '20' + f;
        }
    }
    else
      err = 1;

    if (err==1) {
      alert("You have entered an invalid date or date format.  \nPlease reenter using the format MM/YYYY, M/YYYY, MM/YY or M/YY.");

      return false;
    }
      }
  }

    submitcount ++;
    return true;

    }

    if (screen == "LINK")  {
  if ((form.link_id.value != "D") && ((isFieldBlank(form.link_code)) ||
  (isFieldBlank(form.txtLinkID)) )) {
    alert("Please enter both link type and linked name.");
    return false;
  }
  else {
    submitcount ++;
    return true;
  }
    }

    if (screen == "FINANCIAL")  {
  if ((form.report_name.value == "MTDYTD") && (isFieldBlank(form.end_date))) {
    alert("Please enter an end date.");
    return false;
  }
  else if (((form.report_name.value == "ST") || (form.report_name.value == "ML") ||
    (form.report_name.value == "SA")) &&
    ((isFieldBlank(form.from_date)) || (isFieldBlank(form.to_date)))) {
    alert("Please enter both dates.");
    return false;
  }
    }

    if (screen == "USERFIELD")  {
  if ((isFieldBlank(form.field_name)) || (isFieldBlank(form.prompt))) {
    alert("Please enter both field name and prompt.");
    return false;
  }
  else {
    submitcount ++;
    return true;
  }
    }

    if (screen == "CODE")  {
  if ((isFieldBlank(form.code)) || (isFieldBlank(form.description))) {
    alert("Please enter both code and description.");
    return false;
  }
  else {
    submitcount ++;
    return true;
  }
    }

    if (screen == "COMPOUND")  {
  if ((isFieldBlank(form.filter_name)) || (isFieldBlank(form.filter_id1)) ||
  (isFieldBlank(form.filter_id2))) {
    alert("Please enter all information.");
    return false;
  }
  else {
    submitcount ++;
    return true;
  }
    }
        if (screen == "OTHER")  {
    submitcount ++;
    return true;
   }

  }
}

function close_window() {
  window.close();
}

function remove_spaces(words) {

	  var spaces = words.length;
	  for(var x = 1; x<spaces; ++x){
	   words = words.replace(" ", "");
	 }
	 return words;
}


function clearSearchFields(form) {
  form.txtID.value = "";
  form.txtFirst.value = "";
  form.txtCity.value = "";

try {
  form.txtState.value = "";
  form.txtZip.value = "";
  form.txtOptLine.value = "";
  form.txtCountry.value = "";
}
catch(err) {}

  form.txtAddress.value = "";
  form.filter_id.value = "";
  form.filter_name.value = "";
  form.txtEmail.value = "";
  form.selection_filter.checked = false;
  form.txtID.focus();
}

function clearSearchFields2(form) {
  form.txtID.value = "";
  form.txtFirst.value = "";
  form.txtCity.value = "";

try {
  form.txtState.value = "";
  form.txtZip.value = "";
  form.txtOptLine.value = "";
  form.txtCountry.value = "";
}
catch(err) {}

  form.txtAddress.value = "";

  form.filter_id.value = "";
  form.filter_name.value = "";
  form.selection_filter.checked = false;
  form.txtID.focus();
}


function browser_check(form){
  if (navigator.appVersion.indexOf("MSIE 5.5") > -1) {
    form.browser_version.value = "5.5";
  }
}

function textCounter(field, maxlimit) {
  if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
}

function validate_solicit_code (da_solicit_code, form_number) {

  var left = (screen.width) / 2 - 50;
  var top = (screen.height) / 2 - 50;
  var winprops = "top="+top+",left="+left;
  var vformnum;

  if (form_number == 0) {
    if ((isFieldBlank(document.forms[form_number].gift_date)) && (document.forms[form_number].gift_id.value != "D")) {
      alert("Please enter a gift date.");
      return false;
    }

  }
  else {if (form_number == 1) {
    if ((document.forms[form_number].record_type.value == "P") && (isFieldBlank(document.forms[form_number].start_date)))
    {
      alert('Please enter a start date.');
      return false;
    }

    if (isFieldBlank(document.forms[form_number].txtLinkID))
    {
      alert('Please enter a linked name.');
      return false;
    }

  }}

  if (form_number == 2) {
    vformnum = 0;
  }
  else {
    vformnum = form_number;
  }

  if (da_solicit_code.length > 0) {
    var reportWin = window.open("validatesolicitcode.asp?solicit_code=" + da_solicit_code + "&form_number=" + vformnum, "", "width=1,height=1,resizable=no,scrollbars=no,menubar=no," + winprops);
  }
  else {
    document.forms[vformnum].submit();
  }
}

function validate_solicit_code2 (da_solicit_code, form_number) {
 
  var left = (screen.width) / 2 - 50;
  var top = (screen.height) / 2 - 50;
  var winprops = "top="+top+",left="+left;
  var vformnum;

  if (form_number == 0) {
    if ((isFieldBlank(document.forms[form_number].gift_date)) && (document.forms[form_number].gift_id.value != "D")) {
      alert("Please enter a gift date.");
      return false;
    }

  }
  else {if (form_number == 1) {
    if ((document.forms[form_number].record_type.value == "P") && (isFieldBlank(document.forms[form_number].start_date)))
    {
      alert('Please enter a start date.');
      return false;
    }

    if (isFieldBlank(document.forms[form_number].txtLinkID))
    {
      alert('Please enter a linked name.');
      return false;
    }

  }}

  if (form_number == 2) {
    vformnum = 0;
  }
  else {
    vformnum = form_number;
  }

  if (da_solicit_code.length > 0) {
    var reportWin = window.open("validatesolicitcode2.asp?solicit_code=" + da_solicit_code + "&form_number=" + vformnum, "", "width=1,height=1,resizable=no,scrollbars=no,menubar=no," + winprops);
  }
  else {
    document.forms[vformnum].submit();
  }
}

function validate_solicit_code_lg (da_solicit_code, form_number) {

  var left = (screen.width) / 2 - 50;
  var top = (screen.height) / 2 - 50;
  var winprops = "top="+top+",left="+left;
  var vformnum;

  if (form_number == 1) {
    if ((isFieldBlank(document.forms[form_number].gift_date)) && (document.forms[form_number].gift_id.value != "D")) {
      alert("Please enter a gift date.");
      return false;
    }

  }
  else {if (form_number == 0) {
    if ((document.forms[form_number].record_type.value == "P") && (isFieldBlank(document.forms[form_number].start_date)))
    {
      alert('Please enter a start date.');
      return false;
    }

    if (isFieldBlank(document.forms[form_number].txtLinkID))
    {
      alert('Please enter a linked name.');
      return false;
    }

  }}

  if (form_number == 2) {
    vformnum = 0;
  }
  else {
    vformnum = form_number;
  }

  if (da_solicit_code.length > 0) {
    var reportWin = window.open("validatesolicitcode.asp?solicit_code=" + da_solicit_code + "&form_number=" + vformnum, "", "width=1,height=1,resizable=no,scrollbars=no,menubar=no," + winprops);
  }
  else {
    document.forms[vformnum].submit();
  }
}


function calculateBirthdateOLD(form)
{
  if (form.BIRTHDATE.value != "") {
  var current;
  var birth;
  var curryear;
  var birthyear;
  var currmonth;
  var birthmonth;
  var currday;
  var birthday;
  var age;

  current = new Date();
  curryear = current.getFullYear();
  currmonth = current.getMonth() + 1;
  currday = current.getDate();
  birth = form.BIRTHDATE.value.split("/");
  birthyear = parseInt(birth[2]);
  birthmonth = parseInt(birth[0]);
  birthday = parseInt(birth[1]);

  if ((currmonth > birthmonth) || ((currmonth == birthmonth) && (currday >= birthday))) {
    age = curryear - birthyear;
    }
  else
    age = curryear - birthyear - 1;

  form.age.value = age;
  }
}

function calculateBirthdate(birthdatefield, agefield, dateformat)
{

  if (birthdatefield.value != "") {
  var current;
  var birth;
  var curryear;
  var birthyear;
  var currmonth;
  var birthmonth;
  var currday;
  var birthday;
  var age;

  current = new Date();
  curryear = current.getFullYear();
  currmonth = current.getMonth() + 1;
  currday = current.getDate();
  birth = birthdatefield.value.split("/");
  birthyear = parseInt(birth[2]);

  if (dateformat == "DD/MM/YYYY")
  {
  birthday = parseInt(birth[0]);
  birthmonth = parseInt(birth[1]);
  }
  else
  {
  birthmonth = parseInt(birth[0]);
  birthday = parseInt(birth[1]);
  }

  if ((currmonth > birthmonth) || ((currmonth == birthmonth) && (currday >= birthday))) {
    age = curryear - birthyear;
    }
  else
    age = curryear - birthyear - 1;

  agefield.value = age;
  }
}

function checkMonthYear(field) {
     var err = 0;
     a = field.value;

     if (a.length != 0)
     {
    z = a.split("/",2);

    if (z.length == 2) {

      b = z[0]; // month
      f = z[1]; // year

      if ((b<1) || (b>12)) err = 1;
      if (b.length == 0) err = 1;

      if ((f<0) || (f>9999)) err = 1;
      if ((f.length != 4) && (f.length != 2)) err = 1;
      if (b.length == 1) {
        b = "0" + b;
        field.value = b + "/" + f;
        }
      if (f.length == 2) {
        field.value = b + "/" + "20" + f;
        f = '20' + f;
        }
    }
    else
      err = 1;

    if (err==1) {
      alert("You have entered an invalid date or date format.  \nPlease reenter using the format MM/YYYY, M/YYYY, MM/YY or M/YY.");
      field.focus();
      field.select();
    }
    }
}

function calculate_payment(form, numpayments, curr_symbol) {

  var payment;
  var total;

  if (isNaN(numpayments)) {
    alert('That is not a valid number.');
      form.numpayments.focus();
      form.numpayments.select();
  }
  else {
    total = form.total.value;

    //remove currency symbol and comma from total
    total = total.toString().replace(curr_symbol,'');
    total = total.toString().replace(/\$|\,/g,'');

    // if the total and numpayments are valid numbers > 0 then calculate the payment amt.
    if (!(isNaN(total)) && (total != 0) && (parseInt(numpayments) > 0)) {
      payment = total/parseInt(numpayments).toString();
      form.bill.value = formatCurrency(payment,curr_symbol);
    }
  }
}


function checkorder(orderitem, orderelement) {

  if (isNaN(orderitem)) {
    alert('That is not a valid number.');
      orderelement.focus();
      orderelement.select();
  }

}


function isNotADate(field) {
// The isNotADate function is used as an additional date check
// in case the user submits a form using the Enter key, in which
// case the checkDate validation on the individual field will
// not prevent the form from submitting.

     var err = 0;
     var yr;
     var vmonth;
     var vday;
     var Today;
     a = field.value;

     if (a == "/") {
  Today = new Date();
  yr = Today.getFullYear();
  vmonth = Today.getMonth() + 1;
  vday = Today.getDate();
  field.value = vmonth + "/" + vday + "/" + yr;

  return false;
     }

     else if (a.length != 0)
     {
    z = a.split("/",3);

    if (z.length == 2) {
      Today = new Date();
      yr = Today.getFullYear();
      a = a + "/" + yr;
      field.value = a;
      z = a.split("/",3);
    }

    if (z.length == 3) {

      b = z[0]; // month
      d = z[1]; // day
      f = z[2]; // year

      if (isNaN(b)) err = 1;
      if (isNaN(d)) err = 1;
      if (isNaN(f)) err = 1;
      if ((b<1) || (b>12)) err = 1;
      if (b.length == 0) err = 1;
      if ((d<1) || (d>31)) err = 1;
      if (d.length == 0) err = 1;
      if ((f.length != 4) && (f.length != 2)) err = 1;
      if (f.length == 2) {
        field.value = b + "/" + d + "/" + "20" + f;
        f = '20' + f;
        }
      if ((f<1753) || (f>9999)) err = 1;
      if ((b==4) || (b==6) || (b==9) || (b==11)){
        if (d==31) err = 1;
        }
      if (b==2){
        var g=parseInt(f/4);
        if (isNaN(g)) {
          err=1;
          }
        if (d>29) err=1;
        if ((d==29) && ((f/4)!=parseInt(f/4))) err=1;
      }
    }
    else
      err = 1;

    if (err==1) {
      return true;
    }
    else {
      return false;
    }
    }
}

function nomail_warning(field,record_type)
   {
     if ((field.value.substr(0,1) == "N") && (record_type == "G"))
     {
    alert("You have selected an address that is set to receive no mail.  The address will be associated with the gift but will not receive a thank-you letter unless you change its Get Mail flag.");
     }
     else if ((field.value.substr(0,1) == "N") && (record_type == "P"))
     {
    alert("You have selected an address that is set to receive no mail.  The address will be associated with the pledge but will not receive a pledge reminder unless you change its Get Mail flag.");
     }
  }

function submit_only_once()
{
  if (submitcount > 0)
  {
  alert("This form has already been submitted once.");
  return false;
  }
  else
  {
  submitcount ++;
  return true;
  }
}

function validatePage(page, maxpage, filename)
{
  if (page > maxpage || page < 1)
  {
    alert("Please enter a page number between 1 and " + maxpage);
    return false;
  }
  else
    window.location= filename + '.asp?page=' + document.pages.page.value;
}

function trimAll (value)
{
  return value;
}


function replaceSubstring(inputString, fromString, toString) {


   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
}


function isNotADateEurope(field) {

     var err = 0;
     var yr;
     var vmonth;
     var vday;
     var Today;
     a = field.value;


     if (((a.length == 1) || (a.length == 2)) && (! isNaN(a))) {
  Today = new Date();
  yr = Today.getFullYear();
  vmonth = Today.getMonth() + 1;
  field.value = a + "/" + vmonth + "/" + yr;
  a = a + "/" + vmonth + "/" + yr;
     }

     if (a == "/") {
  Today = new Date();
  yr = Today.getFullYear();
  vmonth = Today.getMonth() + 1;
  vday = Today.getDate();
  field.value = vday + "/" + vmonth + "/" + yr;
     }

     else if (a.length != 0)
     {
    z = a.split("/",3);

    if (z.length == 2) {
      Today = new Date();
      yr = Today.getFullYear();
      a = a + "/" + yr;
      field.value = a;
      z = a.split("/",3);
    }

    if (z.length == 3) {

      d = z[0]; // day
      b = z[1]; // month
      f = z[2]; // year

      if (isNaN(b)) err = 1;
      if (isNaN(d)) err = 1;
      if (isNaN(f)) err = 1;
      if ((b<1) || (b>12)) err = 1;
      if (b.length == 0) err = 1;
      if ((d<1) || (d>31)) err = 1;
      if (d.length == 0) err = 1;
      if ((f.length != 4) && (f.length != 2)) err = 1;
      if (f.length == 2) {
        if (parseInt(f) >= 20) {
          field.value = b + "/" + d + "/" + "19" + f;
          f = "19" + f;
          }
        else {
          field.value = b + "/" + d + "/" + "20" + f;
          f = '20' + f;
          }
        }
      if ((f<1753) || (f>9999)) err = 1;
      if ((b==4) || (b==6) || (b==9) || (b==11)){
        if (d==31) err = 1;
        }
      if (b==2){
        var g=parseInt(f/4);
        if (isNaN(g)) {
          err=1;
          }
        if (d>29) err=1;
        if ((d==29) && ((f/4)!=parseInt(f/4))) err=1;
      }
    }
    else
      err = 1;

    if (err==1) {
	return true;
    }
    }
}

