// DO NOT CHANGE EXISTING FUNCTIONS WITHOUT CONSULTATION FIRST!

function MakeArray(n) {
  this.length = n
  return this
  }
  
monthNames = new MakeArray(12) 
monthNames[1] = "January"
monthNames[2] = "February"
monthNames[3] = "March"
monthNames[4] = "April"
monthNames[5] = "May"
monthNames[6] = "June"
monthNames[7] = "July"
monthNames[8] = "August"
monthNames[9] = "September"
monthNames[10] = "October"
monthNames[11] = "November"
monthNames[12] = "December"

// Returns the name of the current month
function customMonthString(oneDate) {
  var theMonth = monthNames[oneDate.getMonth() + 1 ]
  
  return theMonth
  }

// Returns the current year in IE and FireFox
function thisYear(oneDate) {
  return oneDate.getFullYear()
}

function formatNum(fld, milSep, decSep, maxLength, strCheck) 
{
  if (typeof strCheck == 'undefined' ) strCheck = '0123456789';    //CHECK FOR OPTIONAL ARGUMENT
  
   var sep = 0;
   var key = '';
   var i = j = 0;
   var len = len2 = 0;
   var aux = aux2 = '';

   len = fld.value.length;

   if(len > maxLength)
      i = len - maxLength;
   else
      i = 0
   for(i; i < len - 1; i++)
      if (!((fld.value.charAt(i) == '0') || (fld.value.charAt(i) == decSep))) 
         break;
   aux = '';
   for(; i < len; i++)
      if (strCheck.indexOf(fld.value.charAt(i))!=-1)
         aux += fld.value.charAt(i);
   len = aux.length;

   if (len > 0) 
   {
      aux2 = '';
      for (j = 0, i = len - 1; i >= 0; i--) 
      {
         if (j == 3) 
         {
            aux2 += milSep;
            j = 0;
         }
         aux2 += aux.charAt(i);
         j++;
      }
      if (aux2.length <= maxLength) 
      {
         fld.value = '';
         len2 = aux2.length;
         for (i = len2 - 1; i >= 0; i--)
            fld.value += aux2.charAt(i);
      } 
      else 
      { 
         fld.value = '';
         len2 = maxLength;
         for (i = maxLength - 1; i >= 0; i--)
            fld.value += aux2.charAt(i);
         return false; 
      }
   }
   else
   {
      fld.value = '';
   }
   return false;
}


function numbersOnly(fld, e, maxLength, strCheck) 
{
  if (typeof strCheck == 'undefined' ) strCheck = '0123456789';    //CHECK FOR OPTIONAL ARGUMENT
  
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var aux = aux2 = '';
	// cross-browser event switch
	var whichCode = (window.event) ? window.event.keyCode : e.which;
	if (whichCode == 13 || whichCode == 8 || whichCode == 0) 
		return true;  // Allow enter, backspace, and delete
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) 
		return false;  // Not a valid key

	//return valid keys
	if (fld.value.length >= maxLength) 
	{ 
		return false; 
	}  //too long

	return true;
}



//This function adds the char after the text box
function addPostNode(ques,c)
{
   if(document.getElementById(ques))
   {
      if(document.getElementById(ques).parentNode)
      {
         var percent = document.createTextNode(' ' + c);
         document.getElementById(ques).parentNode.appendChild(percent);
	 document.getElementById(ques).parentNode.noWrap = true;
      }
   }
}

//This function adds the char before the text box
function addPreNode(ques,c)
{
   if(document.getElementById(ques))
   {
      if(document.getElementById(ques).parentNode)
      {
         var dolla = document.createTextNode(c + ' ');
         document.getElementById(ques).parentNode.insertBefore(dolla,document.getElementById(ques));
         document.getElementById(ques).parentNode.noWrap = true;
      }
   }
}


//This variable is used for coloring grids in Firefox
var newColor = '#dddddd';
//This function is used to allow cell clicking in Firefox grids
function firefoxGridHandler(ques, pcode, scode)
{
   if(document.getElementById(ques))
   {
      //Check the radio button on grid cell click
      document.getElementById(ques).checked = true;

      var oldColor;
      if(document.getElementById(ques).parentNode)
      {
         if(document.getElementById(ques).parentNode.parentNode.childNodes[1].currentStyle)
         {
            //If IE, do nothing
         }
         else if(window.getComputedStyle(document.getElementById(ques).parentNode.parentNode.childNodes[1], null))
         {
            //Grab the background color of the rows answer label in Firefox
            if(window.getComputedStyle(document.getElementById(ques).parentNode.parentNode.childNodes[1], null))
            {
               oldColor = window.getComputedStyle(document.getElementById(ques).parentNode.parentNode.childNodes[1], null).getPropertyValue("backgroundColor");
            }
         
            //Loop through the row and reset each cell to the original row color
            for(var i = 0; i < document.getElementById(ques).parentNode.parentNode.childNodes.length; i++)
            {
               if(document.getElementById(ques).parentNode.parentNode.childNodes[i].style)
               {
   	          document.getElementById(ques).parentNode.parentNode.childNodes[i].style.backgroundColor = oldColor;
               }
            }
            //Set the clicked cell to the highlighted color
            document.getElementById(ques).parentNode.style.backgroundColor = newColor;
         }
      }
   }
}

//This function is used for allowing cell clicking in 3d grids with singles
function firefox3DGridHandler(ques, ques2, pcodes)
{
   pcodes = pcodes.split(",");
   if(document.getElementById(ques))
   {
      //Check the radio button on grid cell click
      document.getElementById(ques).checked = true;

      var oldColor;
      if(document.getElementById(ques).parentNode)
      {
         if(document.getElementById(ques).parentNode.parentNode.childNodes[1].currentStyle)
         {
            //If IE, do nothing
         }
         else if(window.getComputedStyle(document.getElementById(ques).parentNode.parentNode.childNodes[1], null))
         {       
            //Loop through the column and reset each cell to the original row color
            for(var i = 0; i < pcodes.length; i++)
            {               
               if(window.getComputedStyle(document.getElementById(ques2 + '_' + pcodes[i]).parentNode.parentNode.childNodes[1], null))
               {
   	          oldColor = window.getComputedStyle(document.getElementById(ques2 + '_' + pcodes[i]).parentNode.parentNode.childNodes[1], null).getPropertyValue("backgroundColor");
                  document.getElementById(ques2 + '_' + pcodes[i]).parentNode.style.backgroundColor = oldColor;
               }
            }
            //Set the clicked cell to the highlighted color
            document.getElementById(ques).parentNode.style.backgroundColor = newColor;
         }
      }
   }
}

function addFirefoxGridHandler(ques, pcodes, scodes)
{
   for(var i = 0; i < pcodes.length; i++)
   {
      for(var j = 0; j < scodes.length; j++)
      {
         if(document.getElementById(ques + '_' + pcodes[i] + '_' + scodes[j]) && document.getElementById(ques + '_' + pcodes[i] + '_' + scodes[j]).parentNode)
         {
            //Add the Grid Handler
            document.getElementById(ques + '_' + pcodes[i] + '_' + scodes[j]).parentNode.onclick = Function("firefoxGridHandler('" + ques + "_" + pcodes[i] + "_" + scodes[j] + "', '" + pcodes[i] + "', '" + scodes[j] + "')");
            //Highlight the already checked grid cells
            if(document.getElementById(ques + '_' + pcodes[i] + '_' + scodes[j]).parentNode.parentNode.childNodes[1].currentStyle)
            {
	       //If IE, do nothing
            }
            else if(window.getComputedStyle(document.getElementById(ques + '_' + pcodes[i] + '_' + scodes[j]).parentNode.parentNode.childNodes[1], null))
            {
               if(document.getElementById(ques + '_' + pcodes[i] + '_' + scodes[j]).checked == true)
                  document.getElementById(ques + '_' + pcodes[i] + '_' + scodes[j]).parentNode.style.backgroundColor = newColor;
            }
         }
      }
   }
}

function addFirefox3dGridHandler(ques, pcodes)
{
   for(var i = 0; i < pcodes.length; i++)
   {
      if(document.getElementById(ques + '_' + pcodes[i]) && document.getElementById(ques + '_' + pcodes[i]).parentNode)
      {
         document.getElementById(ques + '_' + pcodes[i]).parentNode.onclick = Function("firefox3DGridHandler('" + ques + "_" + pcodes[i] + "','" + ques + "','" + pcodes + "')");
         //Highlight the already checked grid cells
         if(document.getElementById(ques + '_' + pcodes[i]).parentNode.parentNode.childNodes[1].currentStyle)
         {
            //If IE, do nothing
         }
         else if(window.getComputedStyle(document.getElementById(ques + '_' + pcodes[i]).parentNode.parentNode.childNodes[1], null))
         {
            if(document.getElementById(ques + '_' + pcodes[i]).checked == true)
               document.getElementById(ques + '_' + pcodes[i]).parentNode.style.backgroundColor = newColor;
         }
      }
   }
}

//resize columns for 3d girds
function resizeCol(ques,cols,width) {
  for(var i = 0; i < cols.length; i++)
  {
     if(document.getElementById(ques + cols[i]))
       document.getElementById(ques + cols[i]).width=width;
  }
}

//shade the header cell if you want a column to be a different color
//shade the rest of the column in the scale list
function shadeHeaderCell(ques,color) {
  if(document.getElementById(ques)) {
    document.getElementById(ques).style.backgroundColor = color;
  }
}

//object properties
function getElementLeft(p_elm) {
	var x = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		x+= elm.offsetLeft;
		elm = elm.offsetParent;
	}
	return parseInt(x);
}


function getElementWidth(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetWidth);
}


function getElementRight(p_elm){
	return getElementLeft(p_elm) + getElementWidth(p_elm);
}


function getElementTop(p_elm) {
	var y = 0;
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	while (elm != null) {
		y+= elm.offsetTop;
		elm = elm.offsetParent;
	}
	return parseInt(y);
}


function getElementHeight(p_elm){
	var elm;
	if(typeof(p_elm) == "object"){
		elm = p_elm;
	} else {
		elm = document.getElementById(p_elm);
	}
	return parseInt(elm.offsetHeight);
}


function getElementBottom(p_elm){
	return getElementTop(p_elm) + getElementHeight(p_elm);
}
