// Utils.js - general utilities used throughout the Winship site

// ==========================================
// maskXXXX - utilities for formatting a field:
// maskPhone, maskDate, maskSSN, maskTime
//
// Usage:
//   onkeypress="maskPhone(this)"
//
// ==========================================

<!--
//creates appropriate pop-up window.  Derived from cancer.gov site to facilitate dictionary popups.
function popWindow(type, urlargs){
 if (type == "privacy") {
  window.open('http://www.cancer.gov/common/popUps/popPrivacy.aspx','','scrollbars=no,resizable=yes,width=300,height=300');
 } else if (type == "livehelp") {
  window.open('http://www.cancer.gov/common/popUps/popLiveHelp.aspx','LiveHelp','scrollbars=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes,width=425,height=500');
 } else if (type == "definition") {
  urlargs = urlargs.replace(/\s/g, '+');
  window.open('http://www.cancer.gov/common/popUps/popDefinition.aspx?term=' + urlargs,'','scrollbars=yes,resizable=yes,width=350,height=450');
 } else if (type == "defbyid") {
  window.open('http://www.cancer.gov/common/popUps/popDefinition.aspx?id=' + urlargs,'','scrollbars=yes,resizable=yes,width=350,height=450');
 } else if (type == "file") {
  window.open(urlargs, '', 'scrollbars=yes,resizable=yes,width=550,height=550');
 } else if (type == "fullbrowser") {
  window.open(urlargs, '', 'menubar=yes,location=yes,status=yes,toolbar=yes,titlebar=yes,scrollbars=yes,resizable=yes,width=675,height=510');
    } else if (type == "small") {
  window.open(urlargs, '', 'scrollbars=no,resizable=no,menubar=no,status=no,toolbar=no,titlebar=no,width=200,height=100,left=400,screenX=400,top=300,screenY=300');
 }
}

function dynPopWindow(url, name, windowAttributes)
{
 options = '';
 optWidth = 'width=500';
 optHeight = 'height=500';
 optScrollbar = 'scrollbars=yes';
 optResizable = 'resizable=yes';
 optMenubar = 'menubar=yes';
 optLocation = 'location=yes';
 optStatus = 'status=yes';
 optToolbar = 'toolbar=yes';

 windowOptions = windowAttributes.split(',');

 for(i = 0; i < windowOptions.length; i++)
 {
  attribute = windowOptions[i].substring(0, windowOptions[i].indexOf('=')).toLowerCase();

  if(attribute == 'width'){
   optWidth = windowOptions[i];
  } else if(attribute == 'height'){
   optHeight = windowOptions[i];
  } else if(attribute == 'scrollbars'){
   optScrollbar = windowOptions[i];
  } else if(attribute == 'resizable'){
   optResizable = windowOptions[i];
  } else if(attribute == 'menubar'){
   optMenubar = windowOptions[i];
  } else if(attribute == 'location'){
   optLocation = windowOptions[i];
  } else if(attribute == 'status'){
   optStatus = windowOptions[i];
  } else if(attribute == 'toolbar'){
   optToolbar = windowOptions[i];
  }
 }

 options = optWidth + ',' + optHeight + ',' + optScrollbar + ',' + optResizable + ',' + optMenubar + ',' + optLocation + ',' + optStatus + ',' + optToolbar;

 window.open(url, name, options);

}
// end of popup scripts from cancer.gov
//-->


var origColor = '';
var rollOverColor = 'yellow';

function TableRowOver(o) {
	origColor = o.style.backgroundColor;
	o.style.backgroundColor = 'lightyellow';
	o.style.cursor = 'hand';
}

function TableRowOut(o) {
	o.style.backgroundColor = origColor;
}

function chkNAN(char2chk)
{
   var validNum = "0123456789";  
   if (validNum.indexOf(char2chk) == "-1") {
		alert("Error - Please enter only numbers.\n");
		return false;
		}
	return true;
}

function hideField(cb, fielda, fieldb) {
    var a = document.getElementById(fielda);
    var b = document.getElementById(fieldb);
    
    if (cb.checked) {
        a.style.visibility = b.style.visibility = 'hidden';
    } else {
        a.style.visibility = b.style.visibility = 'visible';
    }
}

function maskPhone(field)
{
   var isNamedFone;
   var tmpStr = "(";
   var fldVal = field.value;
   keyCount = fldVal.length;
   keyEntered =fldVal.substring(keyCount-1,keyCount);

   if (keyCount <= 1)   isNamedFone = false;
   if (!isNamedFone)    isNamedFone = chkNAN(keyEntered);

   keyCount++;
   switch (keyCount)
   {
      case 2: 
         tmpStr +=  field.value;
         field.value = tmpStr;
         break;
      case 5:
         field.value += ") " ;
         break;
      case 10:
         field.value += "-" ;
         break;
   }     
}

function maskDate(field)
{
   var tmpStr = "-";
   var fldVal = field.value;
   var isNamedFone;
   keyCount = fldVal.length;
   keyEntered =fldVal.substring(keyCount-1,keyCount);

   if (keyCount <= 1)   isNamedFone = false;
   if (!isNamedFone)    isNamedFone = chkNAN(keyEntered);
 
   keyCount++;
   switch (keyCount)
   {
      case 3: 
         tmpStr =  field.value+tmpStr;
         field.value = tmpStr;
         break;
      case 6:
         field.value += "-" ;
         break;
   }     
}

function maskSSN(field)
{
   var tmpStr = "-";
   var fldVal = field.value;
   var isNamedFone;
   keyCount = fldVal.length;
   keyEntered =fldVal.substring(keyCount-1,keyCount);

   if (keyCount <= 1)   isNamedFone = false;
   if (!isNamedFone)    isNamedFone = chkNAN(keyEntered);
 
   keyCount++;
   switch (keyCount)
   {
      case 4: 
         tmpStr =  field.value+tmpStr;
         field.value = tmpStr;
         break;
      case 7:
         field.value += "-" ;
         break;
   }     
}

function maskTime(field)
{
   var tmpStr = ":";
   var fldVal = field.value;
   var isNamedFone;
   keyCount = fldVal.length;
   keyEntered =fldVal.substring(keyCount-1,keyCount);

   if (keyCount <= 1)   isNamedFone = false;
   //if (!isNamedFone)    isNamedFone = chkNAN(keyEntered);
 
   keyCount++;
   switch (keyCount)
   {
      case 3: 
         tmpStr =  field.value+tmpStr;
         field.value = tmpStr;
         break;
   }     
}

//Date Calculation Functions
function validate_dt(date_arr)
{
	if (date_arr.length != 3)
	{
		return "invalid";
	}
	
	var mm = date_arr[0];
	var dd = date_arr[1];
	var yy = date_arr[2];
	
	if ((mm < 1) || (mm > 12) || (dd < 1) || (dd > 31) || (yy < 1) ||(mm == "") || (dd == "") || (yy == ""))
	{
		return "invalid";
	}
	else
	{ 
		if (((mm == 4) || (mm == 6) || (mm == 9) || (mm == 11)) && (dd > 30))
		{
			return "invalid";
		}
		else
		{ 
			if (mm == 2) 
			{
				if (dd > 29)
					return "invalid";
				else if((dd > 28) && (!leapyear(yy)))
					return "invalid";
			}
			else
			{
				if((yy > 9999)||(yy < 0))
					return "invalid";
				else
				{
					return "valid";
				}
			}
		}
	}
}

function leapyear(a) 
{
	if(((a % 4 == 0) && (a % 100 != 0)) || (a % 400 == 0))
	{
		return true;
	}
	else 
	{
		return false;
	}
}

function trim(str) {
	return str.replace(/^\s*|\s*$/g,"");
}

function objectDump(obj, showEmptyProperties) {
	if (obj == null) {
		alert("Object is null");
		return;
	}	
	var oType = typeof obj;
	var s = "Object ID: " + obj.id + "\n";
	for (prop in obj) {
		var pType = typeof prop;
		var theVal = obj[prop];
		if (! ((prop == "innerText") || (prop == "innerHTML") || 
		       (prop == "outerText") || (prop == "outerHTML"))) {
			if (theVal || (showEmptyProperties && !theVal)) 
				s += "   " + prop + " (" + pType + ") = " + theVal + "\n";			
		}
	}
	alert(s);	
}

