// JavaScript Document.
// ******** UTILITY FUNCTIONS ***************************************
//-------------------------------------------------------------------
// isNull(value)
//   Returns true if value is null
//-------------------------------------------------------------------
function isNull(val){return(val==null);}

//-------------------------------------------------------------------
//   Application Functions
//-------------------------------------------------------------------

// Make an area visible as block
	function showAsBlock(id) {
		document.getElementById(id).style.display = 'block';
		document.getElementById(id).style.visibility = 'visible';
	}
// Make an area visible inline
	function showAsInline(id) {
		document.getElementById(id).style.display = 'inline';
		document.getElementById(id).style.visibility = 'visible';
	}
	// Hide area
	function displayNone(id) {
		document.getElementById(id).style.display = 'none';
		document.getElementById(id).value = '';
	}
	// Swap visibility of two areas.
	function swapBlock(shownArea, hiddenArea) {
		displayNone(shownArea);
		showAsBlock(hiddenArea);
	}

	// Swap visibility of two areas.
	function swapInline(shownArea, hiddenArea) {
		displayNone(shownArea);
		showAsInline(hiddenArea);
	}

    function showField(optionid, opttext, targetid) {
		// Show target element if option value does not match txtvalue param
		if (document.getElementById(optionid).options[document.getElementById(optionid).selectedIndex].text==opttext)
		{   
		    displayNone(targetid);
		} else {
			showBlock(targetid);
		}
	}

	function aConfirm(url,msg) {
//	Request confirmation for anchor action
    if (isNull(msg)) {
	  msg="Are you sure?";
	}
		if (confirm(msg))
		{   
	      location.href=url;
		} 
	}

    // Check if supplied field is empty ... 
	function ckRequired(pval,pname) {
      var isOk = true;
      if ((document.getElementById(pval).value)==null||(document.getElementById(pval).value)=="") {
        alert("You have not provided a value for " + pname + "!");
        isOk = false;
      }
      return isOk;
	}

	function addToUrl(url,parg,pvalsrc) {
//	add argument/value pair to url
      var pval = document.getElementById(pvalsrc);
      var returl = url + '\&' + parg + '=' + pval.value;
      return returl;
	}

	function addCBoxToUrl(url,parg,pvalsrc) {
//	add argument/value pair to url
      var pval = document.getElementById(pvalsrc);
	  if (pval.checked)
	  {   
        var returl = url + '\&' + parg + '=' + pval.value;
	  } else {
	    var returl = url;
	  }
      return returl;
	}

	function addSelOptToUrl(url,parg,pvalsrc) {
//	add argument/value pair to url
      var pval = document.getElementById(pvalsrc).options[document.getElementById(pvalsrc).selectedIndex].value;
      var returl = url + '\&' + parg + '=' + pval;
      return returl;
	}

    function chkHour(fieldId) {
      // Save field value in variable ...
      var timeField=document.getElementById(fieldId);
      var timeValue=timeField.value;  
      // Set return value true initially
      var hourOK=true;
      if (timeValue.length!=0)  
	  {	// Validate field content is numeric and between 0 and 23
        if ((isNaN(timeValue)) || (parseInt(timeValue,10)<0) || (parseInt(timeValue,10)>23)) 
        {
          hourOK=false;
          alert("Hours must be a number between 0 and 23!");
          timeField.value='';
          timeField.focus();
        }  
      }
      return hourOK;
    }

    function chkMinute(fieldId) {
      // Save field value in variable ...
      var timeField=document.getElementById(fieldId);
      var timeValue=timeField.value;  
      // Set return value true initially
      var minuteOK=true;
      if (timeValue.length!=0)  
	  {	// Validate field content is numeric and between 0 and 59
        if ((isNaN(timeValue)) || (parseInt(timeValue,10)<0) || (parseInt(timeValue,10)>59)) 
        {
          minuteOK=false;
          alert("Minutes must be a number between 0 and 59!");
          timeField.value='';
          timeField.focus();
        }  
      }
      return minuteOK;
    }

    function isTime(fieldId) { 
      var errorMsg = "";
      var timeOk = true; 
      var timeField = document.getElementById(fieldId);
      alert("checking:" + timeField.value);  
      // regular expression to match required time format 
      var rxpr = /^(\d{1,2}):(\d{2})(:00)?$/; 
      if (timeField.value != '') {  
        if (regs = timeField.value.match(rxpr)) { 
          if(regs[1] < 1 || regs[1] > 23) { 
            errorMsg = "Invalid value for hours: " + regs[1]; 
          }
          if (!errorMsg && regs[2] > 59) { 
            errorMsg = "Invalid value for minutes: " + regs[2]; 
          }
        } else { 
          errorMsg = "Invalid time: " + timeField.value; 
        }
      } 
      if (errorMsg != "") { 
        alert(errorMsg); 
        timeField.focus(); 
        timeOk = false; 
      } 
      alert(timeField.value);  
      return timeOk; 
    } 

    function validate(fieldId,dtype) {
        // Save field value in variable ...
        var valueStr=fieldId.value;
		if (valueStr.length==0) { return true; }
		// Validate field content according to dtype
		switch (dtype) {
		  case 'time':
            if (!/\d{1,2}\d{2}/.test(valueStr)) {
              alert('invalid time  format');
              fieldId.value='';
			  document.all.fieldId.focus();
			  return false;
            }  
		  break;
		  default:
			  // do something; 
		  break;
		}
    }

	function setContentFrameHeight(id)
	{
		var contentHeight = 0;  
		var headingHeight = document.getElementById('heading').clientHeight;
        if ( headingHeight==0 ) { headingHeight=153; }
		var menuHeight = document.getElementById('topmenu').clientHeight;
        if ( menuHeight==0 ) { menuHeight=25; }
		var footingHeight = document.getElementById('footing').clientHeight;
        if ( footingHeight==0 ) { footingHeight=17; }
	    var mainWindowHeight = document.documentElement.clientHeight;
		contentHeight = mainWindowHeight - headingHeight - menuHeight - footingHeight -17;  
		if ( document.getElementById ) {
			var theIframe = document.getElementById(id);
			if (theIframe) {
				theIframe.style.height = contentHeight + "px";
			}
		}
        // alert('W=' + mainWindowHeight + ' H=' + headingHeight  + ' M=' + menuHeight + ' F=' + footingHeight);
		return contentHeight;
	}

	function setImageSize(id)
	{
		if ( document.getElementById ) {
			var maxImageWidth = document.documentElement.clientWidth*.9;
			var theImage = document.getElementById(id);
			if (theImage) {
				imageWidth = theImage.style.width;
				if (imageWidth>maxImageWidth) {
				theImage.style.width = maxImageWidth + "px";
				imageHeight = theImage.style.height;
				theImage.style.height = ((imageHeight*imageWidth)/maxImageWidth) + "px";
				}
			}
		}
	}

