﻿
// Called by the validation code
function checkDropDown(name, errorLabelName, sDisplayText)
{
  // If the box is visible
  if (document.getElementById(name) != null)
  {
    // If no selection was made
    if (document.getElementById(name).value == -1)
    {
      processMessage(errorLabelName, false, sDisplayText);
      return false;
    }
    else
    {
      processMessage(errorLabelName, true, sDisplayText);
      return true;
    }
  }
  else
  {
    return true;
  }
}

function checkEmailTextBox(name, errorLabelName, sDisplayText)
{
  // Check for empty string
  if (isEmpty(name))
  {
		processMessage(errorLabelName, false, sDisplayText);
		return false;
  }
  else
  {
  // Get the text
    var str = document.getElementById(name).value;
  // Build the regex
    var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    
    if (filter.test(str))
    {
      processMessage(errorLabelName, true, sDisplayText);
      return true;
    }
    else
    {
      processMessage(errorLabelName, false, sDisplayText);
      return false;
    }
  }
}

function checkFiscalYear(month, day)
{
  try
  {
    var nMonth = parseInt(document.getElementById(month).value);
    var nDay = parseInt(document.getElementById(day).value);

    // 28 day months
    if (nMonth == 2)
      if (nDay > 28) return false;
    // 30 day months
    else if (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
      if (nDay > 30) return false;

    return true;
  }
  catch (err)
  {
    alert(err.description);
  }
}

function checkPhoneNumber(AreaCode, Phone, errorLabelName, sDisplayText)
{
  if (isEmpty(AreaCode) || isEmpty(Phone))
  {
    processMessage(errorLabelName, false, sDisplayText);
    return false;
  }
  else
  {
    var bIsValid = true;
    
    var txtAreaCode = document.getElementById(AreaCode);
    var txtPhone = document.getElementById(Phone);
    
    var sAreaCode = txtAreaCode.value;
    var sPhone = txtPhone.value.replace(" ", "").replace("-", "").replace("(", "").replace(")", "").replace(".", "");
    
    bIsValid = (sAreaCode.length == 3 && sPhone.length == 7);
    if (bIsValid) bIsValid = isNumeric(sAreaCode + sPhone, false, false);
    
	  processMessage(errorLabelName, bIsValid, sDisplayText);
	  
	  return bIsValid;
  }
}

// Called by the validation code to ensure a 
// textbox has a numeric value within the given range
// If there is no required range pass a zero 
// to the min and max value fields
function checkNumericTextBox (name, errorLabelName, minValue, maxValue, allowDecimal, allowThousandsComma, sDisplayText)
{
	var elem = document.getElementById(name);
	
	if (isEmpty(name))
	{
		processMessage(errorLabelName, false, sDisplayText);
		return false;
	}
	else
	{ 
		if (!isNumeric(elem.value, allowDecimal, allowThousandsComma))
		{
			processMessage(errorLabelName, false, sDisplayText);
			return false;
		}
		if (minValue != 0 || maxValue != 0)
		{
			if (!numberWithinRange(elem.value, minValue, maxValue))
			{
				processMessage(errorLabelName, false, sDisplayText);
				return false;
			}
		}
	}
	processMessage(errorLabelName, true, sDisplayText);
	return true;
}


// Called by the validation code
function checkTextBox (name, errorLabelName, sDisplayText)
{
	if (isEmpty(name))
	{
		processMessage(errorLabelName, false, sDisplayText);
		return false;
	}
	else
	{
		processMessage(errorLabelName, true, sDisplayText);
		return true;
	}
}


// Called by the validation code
function checkDateTextBox (name, errorLabelName, sDisplayText)
{
	if (isEmpty(name))
	{
		processMessage(errorLabelName, false, sDisplayText);
		return false;
	}
	else
	{
	  if (!isDate(document.getElementById(name).value))
	  {
		  processMessage(errorLabelName, false, sDisplayText);
		  return false;
	  }
	  else
	  {
		  processMessage(errorLabelName, true, sDisplayText);
		  return true;
		}
	}
}


// Called by functions within this script 
// to show or hide the missed field label
function processMessage (lblErrorLabel, bIsValid, sDisplayText)
{
  try
  {
	  var elem = document.getElementById(lblErrorLabel);
    if (document.all)
      elem.innerText = bIsValid == true ? " " : sDisplayText;
    else
      elem.textContent = bIsValid == true ? " " : sDisplayText;
	  elem.className = bIsValid == true ? "errorhide" : "errorshow";
	}
	catch (err) { }
}

// Called from other functions within this script 
// to ensure a text element isn't empty
function isEmpty (name)
{
	var elem = document.getElementById(name);
	return (elem.value.length == 0 || elem.value == null);
}

// Called from other functions within this script 
// to check whether a string is numeric
function isNumeric (sText, allowDecimal, allowThousandsComma)
{
	var sValidChars = "0123456789" + (allowDecimal ? "." : "") + (allowThousandsComma ? "," : "");
	var Char;	
	
	if (sText.length > 0)
	{
		for (var i = 0; i < sText.length; i++) 
		{
			Char = sText.charAt(i); 
			if (i == 0 && Char == "-") {}
			else if (sValidChars.indexOf(Char) == -1)
			{
				return false;
			}
		}
		return true;
	}
	else
	{
		return false;
	}
}

// Called from other functions within this script 
// to check whether a number is within a given range
function numberWithinRange (number, minValue, maxValue)
{
	return (parseInt(number) <= maxValue && parseInt(number) >= minValue);
}

function setMaxDays(month, day)
{
  try
  {
    var elemDays = document.getElementById(day);
    var nMonth = parseInt(document.getElementById(month).value);
    var nDays = elemDays.options.length;
    var optn;

    // 28 day months
    if (nMonth == 2 && nDays != 28)
    {
      if (nDays == 31) document.getElementById(day).remove(30);
      document.getElementById(day).remove(29);
      document.getElementById(day).remove(28);
    }
    // 30 day months
    else if ((nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11) && nDays != 30)
    {
      if (nDays == 28)
      {
        optn = document.createElement("OPTION");
        optn.text = "29";
        optn.value = "29";
        elemDays.options[28] = optn;

        optn = document.createElement("OPTION");
        optn.text = "30";
        optn.value = "30";
        elemDays.options[29] = optn;
      }
      else if (nDays == 31)
      {
        document.getElementById(day).remove(30);
      }
    }
    // 31 day months
    else if ((nMonth == 1 || nMonth == 3 || nMonth == 5 || nMonth == 7 || nMonth == 8 || nMonth == 10 || nMonth == 12) && nDays != 31)
    {
      if (nDays == 28)
      {
        optn = document.createElement("OPTION");
        optn.text = "29";
        optn.value = "29";
        elemDays.options[28] = optn;

        optn = document.createElement("OPTION");
        optn.text = "30";
        optn.value = "30";
        elemDays.options[29] = optn;
      }

      optn = document.createElement("OPTION");
      optn.text = "31";
      optn.value = "31";
      elemDays.options[30] = optn;
    }
  }
  catch (err)
  {
    alert(err.description);
  }
}


function isInteger(s)
{
  for (var 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 returnString = "";
  
  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.
  for (var 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)
{
  var daysInMonth = new Array(12);
  
  for (var i = 1; i <= n; i++)
  {
    daysInMonth[i] = 31;
    if (i == 4 || i == 6 || i == 9 || i == 11) { daysInMonth[i] = 30; }
    if (i == 2) { daysInMonth[i] = 29; }
  } 
  return daysInMonth;
}


function isDate(dtStr)
{
  try
  {
    var minYear = 1900;
    var maxYear = 2100;
    var dtCh = "/";
	  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)
	  {
		  return false;
	  }
	  if (strMonth.length < 1 || month < 1 || month > 12)
	  {
		  return false;
	  }
	  if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
	  {
		  return false;
	  }
	  if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
	  {
		  return false;
	  }
	  if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false)
	  {
		  return false;
	  }
  }
  catch (err)
  {
      alert(err.description);
  }
	
  return true;
}

function numbersonly(e)
{
  var unicode = e.charCode ? e.charCode : e.keyCode;
  
  // To allow for tabbing change (unicode != 8) to (unicode != 8 && unicode != 9)
  if (unicode != 8)
  { //if the key isn't the backspace key (which we should allow)
    if (unicode < 48 || unicode > 57) //if not a number
    {
      e.returnValue = false;
      return false; //disable key press
    }
    else
    {
      return true;
    }
  }
}
