function closeWindow($Message) {
	if ( document.getElementById('Note') )  {
	  if ( document.getElementById('Note').value != "") {
	   if (confirm($Message + "Still Close Window?"))
       window.close();
     else
     	 return;   
    } 	 
  }
  window.close(); 	      
}

function htmlDecode(s) {
  var str = new String(s);    

  str = str.replace(/&quot;/g, '"');
  str = str.replace(/&gt;/g,   '>');
  str = str.replace(/&lt;/g,   '<');
  str = str.replace(/&amp;/g,  '&');

  return str;
}

function trim(s) {
  return s.replace(/^\s+|\s+$/g, '') ; 
}
function htmlEncode(s) {
  var str = new String(s);    

  str = str.replace(/&/g, "&amp;");
  str = str.replace(/</g, "&amp;lt;");
  str = str.replace(/>/g, "&amp;gt;");
  str = str.replace(/"/g, "&amp;quot;");
  str = str.replace(/#/g, "No. ");
  return str;
}

function htmlASXMLEncode(s) {
  var str = new String(s);    

  str = str.replace(/&/g, "ASXMLAMP");
  str = str.replace(/</g, "ASXMLLT");
  str = str.replace(/>/g, "ASXMLGT");
  str = str.replace(/"/g, "ASXMLQUOT");
  str = str.replace(/'/g, "ASXMLAPOS");
  str = str.replace(/#/g, "ASXMLPOUND");
  str = str.replace(eval("/" + String.fromCharCode(8217) + "/g"), "ASXMLAPOS")
  return str;
}


function htmlASXMLDecode(s) {
  var str = new String(s);    

  str = str.replace(/ASXMLAMP/g, "&");
  str = str.replace(/ASXMLLT/g, "<");
  str = str.replace(/ASXMLGT/g, ">");
  str = str.replace(/ASXMLQUOT/g, '"');
  str = str.replace(/ASXMLAPOS/g, "'");
  str = str.replace(/ASXMLPOUND/g, "#");
  //str = str.replace(/ASXMLEOL/g, "\n");
  //str = str.replace(/ASXMLREOL/g, "\r");
  return str;
}

function onEnter( evt) {
  var keyCode = null;
  
  if( evt.which ) {
    keyCode = evt.which;
  } else if( evt.keyCode ) {
    keyCode = evt.keyCode;
  }
  if( 13 == keyCode ) {
    return true;
  }
  return false;
}

function openNote(picTable, picIndex, picIndexVal, pilReloadParent) {
  //noteWindow = window.open('','','width=400,height=200,resizable=yes')
	noteWindow=window.open('./opennote.php?Index=' + picIndex + '&IndexVal=' + picIndexVal + '&Table=' + picTable + "&reloadParent=" + pilReloadParent,'addnote','width=400,height=200,resizable=yes,scrollbars=yes');
	noteWindow.focus();
}

function isDecimal(number){// positive or negative decimal
  if(!number) return false;
  decimalRegExp = /^-?(0|[1-9]{1}\d{0,})(\.(\d{1}\d{0,}))?$/
  return decimalRegExp.test(number);
}
function isInteger(n){
  var num = Number(n);
  if(isNaN(num)){
    return false;
  }
  return true
}
function displaySpans(lChecked, picSpan) {
  elements = document.getElementsByTagName('span') 
  
  for (i=0;i<elements.length;i++) {	
  	if ( elements[i].className==picSpan){
  	  if (lChecked) {
  	  	elements[i].style.display = 'inline';
  	  }
  	  else {
  	  	elements[i].style.display = 'none';
  	  }
  	}  
  } 	
}  


  

  function inArray(cNeedle, aHaystack) {
    
    for (var j=0;j<aHaystack.length; j++) {
      if (cNeedle.toString() == aHaystack[j].toString()) {
        return true
      }
    }
    return false     
  }
  function setCB(cChecked, oCB){ 
    //alert(oCB.name)
    lFound = false
    if (oCB) {
      aValues = cChecked.split("^")
      //alert("cChecked = " + cChecked + " " + oCB.length)
      if (oCB.length >= 2) {
        //showProp(oCB[0])
        for (var i=0;i<oCB.length;i++) {
          //alert(oCB[0].value)
          if (inArray(oCB[i].value, aValues)) {
            oCB[i].checked = true
            lFound = true
          } else
            oCB[i].checked = false
        }
      } else {
        if (inArray(oCB.value, aValues)) {
          oCB.checked = true
          lFound = true
        } else
          oCB.checked = false
        
      }
    }
    return lFound
  }
function setSBByText(oSB, cValue) {
  //if (oSB.name == "APDescription")
  //  alert("here")
  lFound = false
  if (oSB.options) {
    //if (oSB.name == "APDate")
    //  alert(oSB.options[0].text.length + " " + cValue.length)
    for (var i=0;i<oSB.options.length;i++) {
      if (oSB.options[i].text == cValue) {
        //if (oSB.name == "APUniformSize")
        //  alert(oSB.options[i].value)
        oSB.selectedIndex = i 
        oSB.defaultSelected = i
        lFound = true
      }
    }
  } 
  return lFound
}  
function setSB(oSB, cValue) {
  lFound = false
  cValue = cValue.toString()
  if (cValue && cValue.substring(cValue.length - 1, cValue.length) == "^") {
    //if (oSB.name == "Type")
    //  alert(cValue.substring(cValue.length -1, cValue.length))  

    cValue = cValue.substring(0, cValue.length - 1)
  }
  aValues = cValue.split("^")
  if (oSB.options) {
    for (var i=0;i<oSB.options.length;i++)
      oSB.options[i].selected = false 
    for (var i=0;i<oSB.options.length;i++) {
      //if (oSB.name == "Type")
      //  alert(oSB.options[i].value + " " + aValues.length + cValue)

      for (var j=0; j<aValues.length; j++) {
        if (oSB.options[i].value == aValues[j]) {
          if (oSB.type == "select-one") {
            oSB.selectedIndex = i 
            oSB.defaultSelected = i
            lFound = true
          }
          else {
            oSB.options[i].selected = true
            lFound = true
          }
            
        }
          
      }
    }
  } 
  return lFound
}
function checkDate(cString){
var checkstr = "0123456789";
//var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "-";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = cString;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      
      //DateField.value = year + seperator + month + seperator + day
      return true
   }
   else {
     return false
   }
}

function isIE(){
var browser=navigator.appName
if (browser=="Microsoft Internet Explorer")
  return true
else
  return false
}


function validate(pioValue, picType, picString) {

  if (picType == "decimal") {
    if (isDecimal(pioValue.value)) {
      pioValue.style.background = "white" 
      //if (picString != "")
      //  updateSpan(picString, 2)
      return true;
    }
    else {
      pioValue.style.background = "yellow"
      alert(pioValue.value + " not a decimal")
      return true;      
    }
  }
  else if (picType == "integer") {
    if (isInteger(pioValue.value)) {
      pioValue.style.background = "white"
      //if (picString != "")
      //  updateSpan(picString, 0)      
      return true;
    }
    else {
      pioValue.style.background = "yellow"
      alert(pioValue.value + " not a integer")
      return true;      
    }
  }  
}

function resizeIframe(cWindowName, piiHeight, piiWidth) {
  i = document.getElementById(cWindowName)

  //alert(i)
  if (isIE()) {
    iHeight = document.documentElement.clientHeight - piiHeight
    iWidth  = document.body.offsetWidth - piiWidth
  }
  else {
    iHeight = window.innerHeight - piiHeight - 5
    iWidth  = document.body.clientWidth - piiWidth
  
  }
  
  i.style.height = iHeight + "px"
  i.style.width = iWidth + "px"      

}

var regExp = /<\/?[^>]+>/gi;
function ReplaceTags(xStr){
  xStr = xStr.replace(regExp,"");
  return xStr;
}

function getSpan(pioDoc, picSpan) {
  oSpan = pioDoc.getElementById(picSpan)
  
  if (oSpan)
   return  oSpan.innerHTML
  else
    return ""
}

function getAttrById(picSpan, picAttr) {
  cValue = ""
  oSpan = document.getElementById(picSpan)
  if (oSpan) {
  	if (oSpan.getAttribute(picAttr) != null)
    	cValue=oSpan.getAttribute(picAttr);
  }
  return cValue
}
function setAttrById(picSpan, picAttr, picValue) {
  //alert(picSpan)
  var oSpan = document.getElementById(picSpan)
  //if (picAttr == "class")
  //  alert(picSpan + " " + picValue)
  if (oSpan) {

   	oSpan.setAttribute(picAttr, picValue)
    //if (picAttr == "class")
    // 	alert(oSpan.getAttribute("class"))
  }
  else
    alert("cant find " + picSpan + " not setting " +picAttr + " to " + picValue)
}
function setSpan(picSpan, picString) {
  var oSpan = document.getElementById(picSpan)
  //alert(picSpan)
  if (oSpan) {
    oSpan.innerHTML = picString 
    //alert(picString)
  }
  else
    alert("no span " + picSpan)

}
function spanVisible(pioDoc, picString, pilShow) {
  var oSpan = pioDoc.getElementById(picString)
  if (oSpan) {
    oSpan.style.visibility = pilShow?"visible":"hidden"
  }
    

} 

function setClass(picID, picClass) {
  var oElem = document.getElementById(picID)
  
  if (oElem) {
    oElem.className = picClass 
  }       
  else
    alert(picID + " not found")
}
      
function showSpan(pioDoc, picString, pilShow) {
  oSpan = pioDoc.getElementById(picString)
  if (oSpan) {
    //alert(picString + " "  + pilShow)
    oSpan.style.display = pilShow?"":"none"
  }
  else
    alert("cannot find " + picString)
    

} 

function pad(number,length) {  
    var str = '' + number;     
    while (str.length < length)
        str = '0' + str;       
    return str;                
}        

function getRequired(pioDoc, picForm) {
  
  aNotComplete = Array()
  
  if (!pioDoc)
    pioDoc = document
  //alert(picForm)
  if (pioDoc.forms[picForm]) {
    aElem = pioDoc.forms[picForm].elements;
    for (var i=0;i<aElem.length;i++) {
      lRequired = (aElem[i].attributes['required'])?aElem[i].attributes['required'].value:false
      if (lRequired && aElem[i].value=="")
        aNotComplete[aNotComplete.length] = aElem[i].attributes["Descr"]
    }
  
  } 
  return aNotComplete
  
}      

function radioValue(pioForm, picName)
{
  var cRadioValue = ""
  for (var i=0; i < pioForm[picName].length; i++) {
     if (pioForm[picName][i].checked) {
       cRadioValue = pioForm[picName][i].value;
      }
  }
  return cRadioValue   
}     
  function trim(s) {
    return s.replace(/^\s+|\s+$/g, '') ; 
  }
  
  function zeroPad(n, digits) {
  	n = n.toString();
  	while (n.length < digits) {
  		n = '0' + n;
  	}
  	return n;
  } 
  function removeElement(cID, cToDelete) {    
    //alert("deleting from " + cID + " = " + cToDelete)             
    var oElement = document.getElementById(cID);               
    var oRemove = document.getElementById(cToDelete);
    oElement.removeChild(oRemove);                       
  }       
  function URLDecode(psEncodeString)                                                                         
  {                                                                 
    // Create a regular expression to search all +s in the string   
    var lsRegExp = /\+/g;                                           
    // Return the decoded string                                    
    return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
  }    

  function displayElem(picElem, pilDisplay) {
    oElem = document.getElementById(picElem)
    if (oElem) {
    	if (pilDisplay)
    	  oElem.style.display = ""
    	else
    		oElem.style.display = "none"
    	 
    }
    else
      alert("cannot find " + picElem)
  }    
  function validDate(picMonth, picDay, picYear) {
    /*
    Purpose: return true if the date is valid, false otherwise
    
    Arguments: day integer representing day of month
    month integer representing month of year
    year integer representing year
    
    Variables: dteDate - date object
    
    */
    //alert(picMonth + " " + picDay + " " + picYear);
    var dteDate;
    
    //set up a Date object based on the day, month and year arguments
    //javascript months start at 0 (0-11 instead of 1-12)
    dteDate=new Date(picYear,picMonth - 1,picDay);
    
    /*
    Javascript Dates are a little too forgiving and will change the date to a reasonable guess if it's invalid. We'll use this to our advantage by creating the date object and then comparing it to the details we put it. If the Date object is different, then it must have been an invalid date to start with...
    */
    //alert(picDay+ " " + dteDate.getDate()+ " " + picMonth+ " " + dteDate.getMonth()+ " " + picYear+ " " + dteDate.getFullYear())
    return ((picDay==dteDate.getDate()) && ((picMonth - 1)==dteDate.getMonth()) && (picYear==dteDate.getFullYear()));
  }
  function formComplete(picForm) {
  	var lComplete = true
  	var lMatch = true
  	//alert("form = " + picForm)
    //var oForm = document.getElementById(picForm) // changed 8-7-09
    var oForm = document.forms[picForm]
    if (oForm) {    
      //alert("1") 
      if (oForm.getAttribute("matchFields") != null) {
        cMatch = oForm.getAttribute("matchFields")
        aMatch = cMatch.split(",")
        if (oForm[aMatch[0]].value != oForm[aMatch[1]].value) {
          alert("fields must match ") 
          lComplete = false
          $("#" + aMatch[0]).addClass("notComplete")
          $("#" + aMatch[1]).addClass("notComplete")
        } 
        else {
          $("#" + aMatch[0]).removeClass("notComplete")
          $("#" + aMatch[1]).removeClass("notComplete")
        }
      }
      //alert("2") 

      if (lComplete) {

        if (oForm.getAttribute("sizeLimit") != null) {
          cSizeLimit = oForm.getAttribute("sizelimit")
          aSizeLimit = cSizeLimit.split("-")
          if (oForm[aSizeLimit[0]].value.length < parseInt(aSizeLimit[1])) {
            lComplete = false
            alert("value not enough characters")
            $("#" + aSizeLimit[0]).addClass("notComplete")
          } 
          else {
            $("#" + aSizeLimit[0]).removeClass("notComplete")
          }
        }
      }
      //alert("3") 
      if (lComplete) {
        if (oForm.getAttribute("validDate") != null) {
          var cParams = oForm.getAttribute("validDate") 

          var aParams = cParams.split(",")
          lValidDate = validDate(oForm[aParams[0]].value, oForm[aParams[1]].value, oForm[aParams[2]].value)
          if (!lValidDate) {
            lComplete = false
            alert("not a valid date")
            $("#" + aParams[0]).addClass("notComplete")
            $("#" + aParams[1]).addClass("notComplete")
            $("#" + aParams[2]).addClass("notComplete")
          } 
          else {
            $("#" + aParams[0]).removeClass("notComplete")
            $("#" + aParams[1]).removeClass("notComplete")
            $("#" + aParams[2]).removeClass("notComplete")
          }
        } 
      }
      //alert("4" + lComplete) 

      if (lComplete) {
        for ( var x=0; x<oForm.length; x++ ) {
        	var oElem = oForm.elements[x]
          
        	//alert(oElem.type + " " + oElem.name + " " + oElem.name.length)
        	//alert(oElem.getAttribute("required"))
        	if (oElem.type != "checkbox" && oElem.name.length > 0) {
        	  //alert("x = " + x + " " + oElem.id + " " + oElem.type + " " + oElem.name)
            if ( oElem.getAttribute("required") && oElem.getAttribute("required") == "true"  && oElem.value == "") {       
              //alert(oElem.id)
            	$("#" + oElem.id).addClass("notComplete")
              lComplete = false
            }
            else if (oElem.getAttribute("required") && oElem.getAttribute("required") == "dependsOn"  ) {
              var cDependsOn = oElem.getAttribute("dependsOn")  
              var aInfo = cDependsOn.split("=") //action=activate
              //alert(aInfo[0])
          
              var oTemp = oForm.elements[aInfo[0]]
              //showProp(oTemp)
              if (oTemp.type && oTemp.type =="text")
                var cValue = oTemp.value
              else  // probably a radio button
                var cValue = getCheckedValue(oTemp)
              //alert(cValue + " " + aInfo[1] + " " + oElem.value)
              if (cValue == aInfo[1] && oElem.value == "") {
              	$("#" + oElem.id).addClass("notComplete")
                lComplete = false
              }
              else {
                $("#" + oElem.name).removeClass("notComplete") 
              }
            }
            else if (oElem.getAttribute("required") && oElem.getAttribute("required") == "depends"  ) {
              cDepends = oElem.getAttribute("depends")  
              if (oForm[cDepends].value == oElem.name && oElem.value == "") {
              	$("#" + oElem.id).addClass("notComplete")
                lComplete = false
              }
              else {
                $("#" + oElem.name).removeClass("notComplete") 
              }
            }
            else    {
            	$("#" + oElem.name).removeClass("notComplete")    
            }     
          }
        }
        //alert("5" + lComplete)
        
        if (!lComplete)
          alert("please complete all required fields")
      }
    }	
         		
    //alert("lcomplete = " + lComplete)
    return lComplete
  }  
       function num2Day(piiDay){
        var aDays = ["Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat"]
        return aDays[piiDay]
      }
      
      function num2Month(piiMonth){
        var aDays = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
        return aDays[piiMonth]
      }
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function getSelectedValue(pioSelect) {
  return pioSelect.options[pioSelect.selectedIndex].value
}       
function submitenter(myfield,e)
{
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
else return true;

if (keycode == 13)
   {
   myfield.form.submit();
   return false;
   }
else
   return true;
}
function alertXML(pioXML) {
  var string = (new XMLSerializer()).serializeToString(pioXML);
  alert(string);
  
}
                                                            