var modifyCounter = 0;
function incModCount() {
    modifyCounter++;
}
function flipValue(box) {
    if (!box.checked) {
        box.value=0;
        // alert("t -> f");
    } else {
        box.value=1;
        // alert("f -> t");
    }
   incModCount();
}
function notifyImminentUnload() {
    if (modifyCounter>0) {
        if (confirm("You have unsaved changes on your form.\n"+
                    "Click 'OK' to return to your form to save your changes.\n"+
                    "Click 'Cancel' to cancel your changes and continue.\n"+
                    "YOU WILL LOSE ALL UNSAVED CHANGES IF YOU CLICK 'Cancel'.")) {
            return true;
        }
    }
    return false;
}
function popMeUp(aURL,aName,width,height) {
  if (width==null) {
    width = 600;
  }
  if (height==null) {
    height = 400;
  }

  var param = "width="+width+",height="+height+",toolbar=0,directories=0,menubar=0,status=1,resizable=yes,location=0,scrollbars=yes";
  var myName = window.open(aURL,aName,param);
  myName.focus();
}
function SaveChanges(svc) {
  if ((typeof form_dep_onsave== "undefined") || form_dep_onsave()) {
      if (svc==null)
        document.form1.service.value=2; // 2 => FormsValidateServlet.ACTION_UPDATE_THE_FORM
      else
        document.form1.service.value=svc;
      if (ecValidateForm) {
        if ( ecValidateForm(document.form1)) {
          document.form1.submit();
        }
      } else {
          document.form1.submit();
      }
      modifyCounter = 0;
      self.focus();
  }
}
function goPropPage(id,iFormDirId) { location.href='/servlet/ec_properties?page_type=formdata&id='+id+'&iFormDirId='+iFormDirId; }
function deleteInst(subCount,action,id,pkgId,saveAct) {
  var confirmString;
  if (subCount==0) {
    confirmString = "Permanently delete this form?";
  } else {
    confirmString = "This form has related data.\n Permanently delete this form and all related forms?";
  }
  if (confirm(confirmString)) {
    changeCount = 0;
    document.form1.removed.value='true';
    if (pkgId>0) {
        location.href='/servlet/forms?action='+action+'&formDataId='+id+'&saveAction='+saveAct+'&pkg='+pkgId;
    } else {
        location.href='/servlet/forms?action='+action+'&formDataId='+id+'&saveAction='+saveAct;
    }
  }
}
function newInst(action,id,pkgId,saveAct) {
  if (document.form1.removed.value!='true'){
    if (pkgId>0) {
        location.href='/servlet/forms?action='+action+'&formDataId='+id+'&saveAction='+saveAct+'&pkg='+pkgId;
    } else {
        location.href='/servlet/forms?action='+action+'&formDataId='+id+'&saveAction='+saveAct;
    }
  } else {
    location.href='/form_views/DataExpired.jsp';
  }
}
function formatElement(name, value) {
	return "<LI>" + "<BOLD>" + name + ":</BOLD> " + value + "\n";
}
function blocking(nr)
{
 if (document.getElementById) // Netscape 5 and Explorer 5
 {
  current = (document.getElementById(nr).style.display == 'block') ? 'none' : 'block';
  document.getElementById(nr).style.display = current;
 }
 else if (document.all) // Explorer 4
 {
  current = (document.all[nr].style.display == 'block') ? 'none' : 'block'
  document.all[nr].style.display = current;
 }
 else alert ('This link does not work in your browser.');
}
function doPopupRefresh() {
  changeCount = 0;
  window.opener.document.forms[0].submit();
  self.focus();
}
function doIFrameRefresh(id) {
  changeCount = 0;
  if (id==0) {
    parent.document.forms[0].submit();
  } else {
    parent.setSelectedIForm(id);
  }
  self.focus();
}
function refreshFields() {
  changeCount = 0;
  /* Because we need to keep some form elements, we don't want to just do a relocation. */
  document.form1.service.value=1; // 1 => FormsValidateServlet.ACTION_RELOAD
  document.form1.submit();
}
function popMeUpMax(aURL,aName,style) {
  var myName;
  if (style=='full')
    myName = window.open(aURL,aName);
  else
    myName = window.open(aURL,aName,"toolbar=0,directories=0,menubar=0,status=1,resizable=yes,location=0,scrollbars=yes");
  myName.focus();
}
function now(field) {
    var mydate=new Date()
    var year=mydate.getYear()
    if (year < 1000)
        year+=1900
    var day=mydate.getDay()
    var month=mydate.getMonth()+1
    if (month<10)
        month="0"+month
    var daym=mydate.getDate()
    if (daym<10)
        daym="0"+daym
    field.value =(month+"/"+daym+"/"+year)
}
function isNumeric(passedVal) {
  if (passedVal == "") {
    return true;
  }
  for (i = 0; i < passedVal.length; i++) {
    var pointCnt = 0;
    if (passedVal.charAt(i) < "0") {
      if (i==0 && passedVal.charAt(i) != "-" && passedVal.charAt(i) != "+" && passedVal.charAt(i) != ".") {
        return false;
      } else if (passedVal.charAt(i) == ".") {
        pointCnt ++;
        if (pointCnt > 1) {
          return false;
        }
      }
    }
    if (passedVal.charAt(i) > "9") {
      return false;
    }
  }
  return true;
}
/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}
/*
 * hdn_name is used for SengIFormInclude.jsp
 */
function validateNumeric(fld,hdn_name) {
    if (fld.value != "") {
        var mydata = parseFloat(fld.value);
        if (isNaN(mydata)) {
            alert('Please input a number for this field.');
            fld.focus();
        } else {
            fld.value = mydata;
            incModCount();
            if (hdn_name!=null) {
                hdn_name.value=1;
            }
        }
    } else {
        incModCount();
        if (hdn_name!=null) {
            hdn_name.value=1;
        }
    }
}
/*
 * hdn_name is used for SengIFormInclude.jsp
 */
function validateDate(fld,hdn_name) {
    if (fld.value != "") {
        if (isDate(fld.value)==false){
            fld.focus();
        } else {
            incModCount();
            if (hdn_name!=null) {
                hdn_name.value=1;
            }
        }
    } else {
        incModCount();
        if (hdn_name!=null) {
            hdn_name.value=1;
        }
    }
}

function validateTextareaLength(fld, limit) {
    if (fld.value.length <= limit) {
        incModCount();
    } else {
        alert("Your data has exceeded the maximum capacity "+limit);
        fld.focus();
    }
}

function openImageViewerWindow(fileId)
    {
        if (fileId > 0) {
            var aURL = '/form_views/SimpleImageDisplay.jsp?fileId='+fileId;
            var myName = window.open(aURL);
            myName.focus();
        } else {
            var w = window.open();
            w.document.writeln("\x3Chtml>");
            w.document.writeln("\x3Chead>");
            w.document.writeln("\x3Ctitle>Image Viewer\x3C/title>");
            w.document.writeln("\x3C/head>");
            w.document.writeln("\x3Cbody");
            w.document.writeln("There\'s no image to display, because none has been uploaded.");
            w.document.writeln("\x3C/body>");
            w.document.writeln("\x3C/html>");
            w.document.close();
            w.focus();
        }
    }

function showReferencedIForm(grandformId, formId, metaId, iformDir) {
    if (document.form1.saveAction.value==2) {
//        window.opener.doMetaSearch(metaId);
        location.href='/servlet/forms?action=2&formDataId='+formId+'&saveAction=2'+'&iFormDirId='+iformDir;
    } else if (document.form1.saveAction.value==4) {
        if (grandformId != 0) {
            parent.doInstanceSearch(grandformId, formId);
        } else {
            parent.doMetaSearch(metaId, formId);
        }
    }
}

function openLink(field, name) {
    popMeUpMax(field.value,name,'full');
}

function printThis() {
    var iformDir = document.forms[0].IFormDirId.value;
    var iform = document.forms[0].formDataId.value;
    if (modifyCounter>0) {
        alert('You have unsaved changes on this form. Please either save or cancel your changes');
        return;
    }
    if (iform==0) {
        alert('You must save this form first');
        return;
    }
    popMeUp('/servlet/FormToPdf?IFormDirId='+iformDir+'&IFormId='+iform,'FormToPdf');
}

function do_quick_save(svc) {
    document.form1.service.value=svc;
      if (ecValidateForm) {
        if ( ecValidateForm(document.form1)) {
          document.form1.submit();
        }
      } else {
        document.form1.submit();
      }
      modifyCounter = 0;
      self.focus();
}

function doAdditionalForm() {
    if (!notifyImminentUnload()) {
        location.href='/servlet/forms?IFormDirId='+document.form1.IFormDirId.value+'&action=4&formDataId='+document.form1.formDataId.value+'&SaveAction=2';
    }
}

function doCloneForm() {
    if (!notifyImminentUnload()) {
        location.href='/servlet/forms?IFormDirId='+document.form1.IFormDirId.value+'&action=9&formDataId='+document.form1.formDataId.value+'&SaveAction=2';
    }
}

function setElementDisplay(fld, id, val, state) {
    var obj = document.getElementById(id);
    if (obj && fld.value == val) {
        obj.style.display=state;
    } else if (obj){
        if (state=='' || state=='inline') {
            obj.style.display='none';
        } else {
            obj.style.display='';
        }
    }
}
