function changeLanguage()
{
  document.formLanguage.submit();
}
function countChar(textField, lengthField, maxlength)
{
  if (document.theForm.elements[textField].value.length > maxlength)
  {
     var val;
     val =  document.theForm.elements[textField].value;
     document.theForm.elements[textField].value = val.substring(0,maxlength);
  }
 
  document.all[lengthField].innerText=document.theForm.elements[textField].value.length;
  return true;
}

function y2k(number)    { return (number < 1000) ? number + 1900 : number; }

var today = new Date();
var day   = today.getDate();
var month = today.getMonth();
var year  = y2k(today.getYear());

var fieldToUpdate;

function padout(number) { return (number < 10) ? '0' + number : number; }

function restart() {
    // Code with padding to allow for months and day with single digits..
    document.theForm.elements[fieldToUpdate].value = '' + padout(month - 0 + 1) + '/' + padout(day) + '/' + year;
    // Code changed to remove leading zeros from dates ---  document.theForm.elements[fieldToUpdate].value = '' + (month - 0 + 1) + '/' + (day) + '/' + year;    
    mywindow.close();
}


function showCalendar(pField) {
    fieldToUpdate = pField;

    mywindow=open('cal.htm','myname','resizable=no,width=250,height=170');
    mywindow.location.href = 'cal.htm';
    if (mywindow.opener == null) mywindow.opener = self;
}

function confirmNoPlan()
{
  return confirm("You have not selected a provider for this service.\n\nDo you want to return to the services list\nwith selecting a provider?");
}

function total(pID)
{
  var currentTotal = 0, count=0, totaloptions=0, optionPrice=0;
  var includedMovies = 1, planNum, option131, option132, checks=0;
  var mylength = document.theForm.elements.length;
  //check for non-listed or non-listed and non-published
  while (0 <= --mylength)
  {
    if (pID == document.theForm.elements[mylength].id)
    {
      planNum = (pID.substring(2));
      if ("checkbox" == document.theForm.elements[mylength].type
	     && document.theForm.elements[mylength].checked)
	  {
	    if ("plop_" + planNum + "_131" == document.theForm.elements[mylength].name)
	    {
	      option131 = mylength;
	      checks += 1; //counter for "non-listed" options
	    }
	    if ("plop_" + planNum + "_132" == document.theForm.elements[mylength].name)
	    {
	      option132 = mylength;
	      checks += 1; //counter for "non-listed" options
	    }
	  }
    }
  }
  if (checks == 2)
  {
    alert("Either select non-listed or non-listed and non-published");
	document.theForm.elements[option131].checked = false;
  }
  mylength = document.theForm.elements.length;  
  while (0 <= --mylength)
  {
    if (pID + "total" == document.theForm.elements[mylength].id)
    {
      totaloptions = document.theForm.elements[mylength].value;
    }
    if (pID == document.theForm.elements[mylength].id)
    {
		if ("text" == document.theForm.elements[mylength].type)
		{
			  currentTotal += Number(document.theForm.elements[mylength].xvalue)
		}
		
		if ("hidden" == document.theForm.elements[mylength].type)
		{
			  //max options on the plan
			  alert("here");
			  totaloptions = document.theForm.elements[mylength].value;
		}
		if ("checkbox" == document.theForm.elements[mylength].type
			&&   document.theForm.elements[mylength].checked
		   )
		{
		    optionPrice = document.theForm.elements[mylength].xvalue
		    if (optionPrice == 000) //count options without a price
		    {
		      count +=1;
		    }
			if ("Yes" == document.theForm.elements[mylength].IsMovieChannel)
			{
				//alert(Number(document.theForm.elements[mylength].IncludesMovies)); 
				if (0 == Number(document.theForm.elements[mylength].IncludesMovies)) 
					{
					    //alert("adding");
						currentTotal += Number(document.theForm.elements[mylength].xvalue);
					}
					else
					{
						//alert("else");
						if (includedMovies > Number(document.theForm.elements[mylength].IncludesMovies))
						{
							currentTotal += Number(document.theForm.elements[mylength].xvalue);
						}
						includedMovies += 1;
						//alert(includedMovies);
					}
			}		
			else
			{	
				//alert("adding"); 	
				currentTotal += Number(document.theForm.elements[mylength].xvalue)
			}
		 }
		if ("select-one" == document.theForm.elements[mylength].type
		&& ("" != document.theForm.elements[mylength].value)
		  )
		{
			var vpos, vregexp, vqty, vprice;
			var vvalue = document.theForm.elements[mylength].value;
			//alert(vvalue.lenght);
			vregexp = /,/i;            //Create regular expression pattern.
			vpos = vvalue.search(vregexp);
			vqty = vvalue.substr(vpos+1);
			//vprice = (document.theForm.elements[mylength].xvalue * Number(vqty) );
			//alert(typeof(vprice));    
			currentTotal += (document.theForm.elements[mylength].xvalue * Number(vqty) );
		}
	}
  } 
  mylength = document.theForm.elements.length;
  while (0 <= --mylength)
  {
    if (pID == document.theForm.elements[mylength].id
    &&  "text" == document.theForm.elements[mylength].type
       )
    {
      document.theForm.elements[mylength].value = niceFormat(currentTotal);
    }
  }
  //JTS 06/20/06 enable/disable if max options selected
  if (totaloptions > 0)
  {
    mylength = document.theForm.elements.length;
    if (totaloptions <= count)
    {
      while (0 <= --mylength)
      {
        if (pID == document.theForm.elements[mylength].id)
        {
          if ("checkbox" == document.theForm.elements[mylength].type)
		  {
		    if (!(document.theForm.elements[mylength].checked))
		    {
		      if (document.theForm.elements[mylength].xvalue == 000)
		      { //only disable options w/o price
		        document.theForm.elements[mylength].disabled = true;
		      }
		    }
		  }
	    }
       }   
     }
     else
     {
        while (0 <= --mylength)
        {
          if (pID == document.theForm.elements[mylength].id)
          {
            if ("checkbox" == document.theForm.elements[mylength].type)
		    {
		      document.theForm.elements[mylength].disabled = false;
		    }
          }
        }
      }
  }
}



function niceFormat(pValue)
{
  var myCents;
  var myDollars;
  var myValue = String(pValue);
  myCents = myValue.substring(myValue.length-2);
  myDollars = myValue.substring(0, myValue.length-2);
  return ("$" + myDollars + "." + myCents);
}


// ****************************************************************************
// this routines are used for client-side validation of input fields.
// ****************************************************************************
// validate a name field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null name is acceptable.
// ****************************************************************************
function ValidName(nameIndex, maxLength, allowBlank)
{
  var myName;
  var returnCode = false;
  var msg = "";
  myName = document.theForm.elements[nameIndex].value;
  while (myName.substring(-1, 1) == " ")
  {
    myName = myName.substring(0, myName.length - 1);
  }

  if (myName != "")
  {
    if (myName.length <= maxLength)
    {
      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
                    "abcdefghijklmnopqrstuvwxyz" +
                    "ƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓ" +
                    "ÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíî" +
                    "ïðñòóôõöøùúûüýþ .,-\'\r\n";
      if (HasOnly(myName, checkOK))
      {
        returnCode = true;
      }
      else
      {
        returnCode = false;
        msg = "Names must be made of letters only."
      }
    }
    else
    {
      returnCode = false;
      msg = "Names must be " + maxLength + " characters or less.";
    }
  }
  else
  {
    // name is blank.  return caller validity of this state.
    returnCode = allowBlank;
    // this message is not displayed if the caller can accept a blank name
    msg = "A name is required.";
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}

// ****************************************************************************
// validate a price field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null name is acceptable.
// ****************************************************************************
function ValidPrice(nameIndex, maxLength, allowBlank)
{
  var myPrice;
  var returnCode = false;
  var msg = "";
  myPrice = document.theForm.elements[nameIndex].value;
  while (myPrice.substring(-1, 1) == " ")
  {
    myPrice = myPrice.substring(0, myPrice.length - 1);
  }

  if (myPrice != "")
  {
    if (myPrice.length <= maxLength)
    {
      var checkOK = "01234567890.";
      if (HasOnly(myPrice, checkOK))
      {
        returnCode = true;
      }
      else
      {
        returnCode = false;
        msg = "Price must be numeric (no signs or blanks)."
      }
    }
    else
    {
      returnCode = false;
      msg = "Maximum length:  " + maxLength ;
    }
  }
  else
  {
    // field is blank.  return caller validity of this state.
    returnCode = allowBlank;
    // this message is not displayed if the caller can accept a blank name
    msg = "This field is required.";
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}

// ****************************************************************************
// validate an integer field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null name is acceptable.
// ****************************************************************************
function ValidInteger(nameIndex, maxLength, allowBlank)
{
  var myInteger;
  var returnCode = false;
  var msg = "";
  myInteger = document.theForm.elements[nameIndex].value;
  while (myInteger.substring(-1, 1) == " ")
  {
    myInteger = myInteger.substring(0, myInteger.length - 1);
  }

  if (myInteger != "")
  {
    if (myInteger.length <= maxLength)
    {
      var checkOK = "01234567890";
      if (HasOnly(myInteger, checkOK))
      {
        returnCode = true;
      }
      else
      {
        returnCode = false;
        msg = "This field must be numeric."
      }
    }
    else
    {
      returnCode = false;
      msg = "Maximum length:  " + maxLength ;
    }
  }
  else
  {
    // field is blank.  return caller validity of this state.
    returnCode = allowBlank;
    // this message is not displayed if the caller can accept a blank name
    msg = "This field is required.";
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}

// ****************************************************************************
// In general, phone numbers are not validated in the system.  This
// is an example of a possible routine that could be used.
// ****************************************************************************
// validate a phone field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null name is acceptable.
// ****************************************************************************
function ValidPhone(nameIndex, allowBlank)
{
  returnCode = false;
  msg = ""
  myPhone = document.theForm.elements[nameIndex].value;
  while (myPhone.substring(-1, 1) == " ")
  {
    myPhone = myPhone.substring(0, myPhone.length - 1);
  }

  if (myPhone != "")
  {
     returnCode = true;
  }
  else
  {
    // Phone number is blank.  return caller validity of this state.
    returnCode = allowBlank;
    // this message is not displayed if the caller can accept a blank name
    msg = "A phone number is required.";
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}

// ****************************************************************************
// validate a city or state field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null name is acceptable.
// ****************************************************************************
function ValidPlace(nameIndex, allowBlank)
{
  returnCode = false;
  msg = ""
  myPlace = document.theForm.elements[nameIndex].value;
  while (myPlace.substring(-1, 1) == " ")
  {
    myPlace = myPlace.substring(0, myPlace.length - 1);
  }

  if (myPlace != "")
  {
    if (myPlace.length >= 2)
    {
      var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
                    "abcdefghijklmnopqrstuvwxyz" +
                    "ƒŠŒšœŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓ" +
                    "ÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíî" +
                    "ïðñòóôõöøùúûüýþ .-\'\r\n";
      if (HasOnly(myPlace, checkOK))
      {
        returnCode = true;
      }
      else
      {
        returnCode = false;
        msg = "Locations must be alphabetic.";
      }
    }
    else
    {
      returnCode = false;
      msg = "Locations must be 2 characters.";
    }      
  }
  else
  {
    // name is blank.  return caller validity of this state.
    returnCode = allowBlank;
    // this message is not displayed if the caller can accept a blank name
    msg = "Location is required.";
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}

// ****************************************************************************
// validate an email field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null name is acceptable.
// ****************************************************************************
function ValidEmail(nameIndex, allowBlank)
{
  returnCode = false;
  msg = ""
  myEmail = document.theForm.elements[nameIndex].value;
  while (myEmail.substring(-1, 1) == " ")
  {
    myEmail = myEmail.substring(0, myEmail.length - 1);
  }

  if (myEmail != "")
  {
    if (Has(myEmail, " \t\r\n\f"))
    {
      msg = "Internet Mail address cannot contain whitespace."
      returnCode = false;
    }
    else
    {
      returnCode = true;
    }
  }
  else
  {
    // e-Mail address is blank.  return caller validity of this state.
    returnCode = allowBlank;
    // this message is not displayed if the caller can accept a blank name
    msg = "An Internet Mail address is required.";
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}
// ****************************************************************************
// validate a URL field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null URL is acceptable.
// ****************************************************************************
function ValidURL(nameIndex, allowBlank)
{
  returnCode = false;
  msg = ""
  myURL = document.theForm.elements[nameIndex].value;
  while (myURL.substring(-1, 1) == " ")
  {
    myURL = myURL.substring(0, myURL.length - 1);
  }

  if (myURL != "")
  {
    if (Has(myURL, " \t\r\n\f"))
    {
      msg = "Internet URLs cannot contain whitespace."
      returnCode = false;
    }
    else
    {
      if (Has(myURL, ":"))
      {
        returnCode = true;
      }
      else
      {
        msg = "Internet URLs must contain a colon (:)\ne.g http://www.eds.com"
        returnCode = false;
      }
    }
  }
  else
  {
    // URL address is blank.  return caller validity of this state.
    returnCode = allowBlank;
    // this message is not displayed if the caller can accept a blank name
    msg = "An Internet URL is required.";
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}

// ****************************************************************************
// validate a name field.
// nameIndex - HTML name of field
// allowBlank - true or false as whether or not a null name is acceptable.
// ****************************************************************************
function ValidSize(nameIndex, maxLength)
{
  returnCode = true;
  msg = ""
  myField = document.theForm.elements[nameIndex].value;
  while (myField.substring(-1, 1) == " ")
  {
    myField = myField.substring(0, myField.length - 1);
  }

  if (myField != "")
  {
    if (myField.length > maxLength)
    {
      msg = "Maximum length for this field is " + maxLength + " characters.";
      returnCode = false;
    }
  }

  // if invalid, position cursor to field in error.
  if (false == returnCode)
  {
    alert(msg);
    document.theForm.elements[nameIndex].focus();
  }
  return(returnCode);
}

// ****************************************************************************
// search a string to see if it is made up of only a certain
// set of characters. 
// return TRUE or FALSE
// ****************************************************************************
function HasOnly(theString, validChars)
{
  var returnCode = true;
  for (i = 0;  i < theString.length;  i++)
  {
    ch = theString.charAt(i);
    for (j = 0;  j < validChars.length;  j++)
      {
      if (ch == validChars.charAt(j))
        {
        j = validChars.length + 1;  // character is OK, stop looking
        }
      }
    if (j == validChars.length)
    {
      // we went off the end of the list without finding
      // the current character(i) in theString.  It must not
      // not be a valid character.
      returnCode = false;
      i = theString.length + 1;    // terminate loop
    }
  }
  return (returnCode);
}

// ****************************************************************************
// search a string to see if it has invalid characters in it.
// return TRUE or FALSE
// ****************************************************************************
function Has(theString, invalidChars)
{
  var returnCode = false;
  for (i = 0;  i < theString.length;  i++)
  {
    ch = theString.charAt(i);
    for (j = 0;  j < invalidChars.length;  j++)
    {
    if (ch == invalidChars.charAt(j))
      {
      j = invalidChars.length + 1;  // character is invalid, stop looking
      i = theString.length + 1;     // stop both loops
      returnCode = true;
      }
    }
  }
  return (returnCode);
}

// ****************************************************************************
// Determine if a string is composed of valid characters
// return TRUE or FALSE
// set showAlert to true to cause an error message to be displayed if necessary
//
// This function can be used to verify input for a text or textarea
// field.  It prevents entry of non-ASCII characters as the 1st character
// of a field.  There are various ways to insert these non-printable
// characters and having them in the data fields causes undesireable results.
// ****************************************************************************

function IsValidText(nameIndex, allowBlank)
{

  rc = false;
  myField = document.theForm.elements[nameIndex].value;
  while (myField.substring(-1, 1) == " ")
  {
    myField = myField.substring(0, myField.length - 1);
  }

  if (myField.length > 0)
  {
    FirstChar = myField.charAt(0);
    if ((FirstChar <= ' ') ||
        (FirstChar >  '~'))
    {
      rc = false;
    }
    else
    {
      rc = true;
    }
  }
  else
  {
    rc = allowBlank;
  }
  if (!rc)
  {
     document.theForm.elements[nameIndex].focus();
     alert("This field is required.\r\r" +
           "No leading spaces or special characters\r" +
           "are allowed.");
     return(false);
  }
  return (rc);
}

function DatesConsistent(pEarlierDate, pLaterDate, pInform)
{

  var startDate = new Date(document.theForm.elements[pEarlierDate].value);
  var endDate = new Date(document.theForm.elements[pLaterDate].value);
  if (pInform && (startDate.valueOf() > endDate.valueOf()))
  {
    theForm.elements[pEarlierDate].style.backgroundColor='#FF0000';
    theForm.elements[pLaterDate].style.backgroundColor='#FF0000';
    theForm.elements[pEarlierDate].focus();
    alert("Dates are inconsistent.");
    return(false);
  }

  return (startDate.valueOf() <= endDate.valueOf());
}

function protectIfChecked(pCheckBox, pPrompt)
{
  if (document.theForm.elements[pCheckBox].checked)
  { 
    if (pPrompt)
    {
      alert("This field cannot be modified now.");
    }
    return false;
  }
  return true;
}
