// FormChek.js
/* GLOBAL VARS */
var whitespace = " "  // whitespace characters  e.g.: \t \n\r
var defaultEmptyOK = false
var sEmail, sComment, xVar1, xVar2  // unused vars reserved for future use

/* MESSAGE DISPLAY VARS */
//REUSABLE VARS
var AlertPrefix0 = "HO!!  HO!!  HO!! \n\n";
var AlertPrefix2 = AlertPrefix0 + "Did you forget to tell Santa ";
var AlertPrefix3 = AlertPrefix0 + "You wouldn\'t be fibbing to Santa about ";
var AlertSuffix0 = " now would you?  *wink*";
var AlertSuffix2 = "\n(After all, he does need to know where to leave your presents!)";
var AlertSuffix3 = "you live in?\nYou can select your ";
var AlertSuffix4 = "from the drop-down list";
var AlertFName0 = "your first name";
var AlertFName2 = "send letters individually (instead of by group) so Santa can reply to each person.";
var AlertAge = "Enter your age as a number (e.g. 10) instead of as a word (e.g. ten).  Enter baby ages as decimals (e.g. 0.5).";
var AlertGender0 = "whether you are a boy or girl";
var AlertGender1 = "(so you don't get the wrong presents Christmas morning!)";
var AlertCity0 = "what city you live in";
var AlertRegion0 = "OR choose \"Not in US/Canada\" if you don\'t live in the U.S. or Canada.";
var AlertGood0 = "how good you\'ve been this year";
var AlertGood1 = "Choose the description " + AlertSuffix4 +" that best describes "+ AlertGood0 +".";
var AlertPrez0 = " (that way Santa will have a few ideas of what you might like for Christmas).";
var AlertConsent0 = "Is it ok if Santa shares your comments?  (He promises to be good!)";

//STATUS BAR DISPLAYS
var pFirstName= "Please enter "+ AlertFName0 +" only and "+ AlertFName2;
var pEmail = "You don't have to enter your email address to get a reply.  Nor will Santa give your address to anyone else.";
var pAge = AlertAge;
var pGender="Make sure you tell Santa "+ AlertGender0 +" "+AlertGender1 +".";
var pCity = "Please tell Santa "+ AlertCity0 +"."+ AlertSuffix2;
var pRegion = "Select the State/Province you live in "+AlertRegion0;
var pCountry = "Please select your country " + AlertSuffix4 +".";
var pGood = AlertGood1
var pPresent = "Please enter three Christmas wishes"+ AlertPrez0;
var pComment = "If you have any special messages for Santa you can type them here!";
var pConsent = AlertConsent0;

//ERROR MSG DISPLAYS
var sFirstName=  AlertPrefix2 + AlertFName0 +"?\n\nYou can "+ AlertFName2;
var tFirstName=  AlertPrefix3 + AlertFName0 + AlertSuffix0;
var sAge = AlertPrefix2 + "how old you are?\n\n"+AlertAge;
var tAge = AlertPrefix3 + "your age" + AlertSuffix0 + "\n\n("+ AlertAge +")";
var sGender=AlertPrefix2 + AlertGender0 +"?\n"+AlertGender1;
var sCity =  AlertPrefix2 + AlertCity0 +"?" + AlertSuffix2;
var tCity =  AlertPrefix3 + "where you live" + AlertSuffix0 + AlertSuffix2;
var sRegion = AlertPrefix2 + "what State or Province, if any, " + AlertSuffix3 + "state/province " + AlertSuffix4 + ",\n"+AlertRegion0;
var sCountry = AlertPrefix2 + "what country " + AlertSuffix3 + "country " + AlertSuffix4 +".";
var sGood = AlertPrefix2 + AlertGood0 +"?\n\nPlease help Santa keep his \'Naughty and Nice List\' up to date.  "+ AlertGood1;
var sPresent = AlertPrefix2 + "what you want for Christmas?\n\nYou can ask Santa for three wishes in this part of your letter" + AlertPrez0;
var sConsent = AlertPrefix0 + AlertConsent0;

/* VALIDATE INPUT & PROCESS FORM */
function sendInput(theForm)
{
   return (
      checkString(theForm.firstname,sFirstName,tFirstName,false,xVar1,xVar2) &&
      checkEmail(theForm.email,sEmail,true,xVar1,xVar2) &&		//not used but do not delete
      checkRadio(theForm.gender,sGender,false,xVar1,xVar2) &&
      checkNumber(theForm.age,sAge,tAge,false,xVar1,xVar2) &&
      checkString(theForm.city,sCity,tCity,false,xVar1,xVar2) &&
      checkDrop(theForm.region,sRegion,xVar1,xVar2) &&
      checkDrop(theForm.country,sCountry,xVar1,xVar2) &&
      checkDrop(theForm.good,sGood,xVar1,xVar2) &&
      checkPresent(theForm.Present1,sPresent,false,xVar1,xVar2) &&
      checkPresent(theForm.Present2,sPresent,false,xVar1,xVar2) &&
      checkPresent(theForm.Present3,sPresent,false,xVar1,xVar2) &&
      checkComment(theForm.comment,sComment,true,xVar1,xVar2) &&	//not used but do not delete
      checkRadio(theForm.consent,sConsent,false,xVar1,xVar2)
    )
}

/* NOTIFY USER OF INPUT REQ'TS & MISTAKES */
function promptEntry (p)
{   window.status = p
}

function warnEmpty (theField, s)
{	theField.focus(theField);
	theField.select(theField);
	alert(s);
	return (false);
}

function warnSelect (theField, s)
{	theField.focus(theField);
	alert(s);
	return (false);
}

function warnInvalid (theField,t)
{	theField.focus(theField);
	theField.select(theField);
	alert(t);
	return (false);
}

/* CHECK FIELDS. */
function checkString (theField, s, t, emptyOK)
{  if (checkString.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return (true);
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    if (isAlphabetic(theField.value) == false)  				//checks alphabet
       return warnInvalid (theField, t);
    if (theField.value.length < 2)
       return warnInvalid (theField, t);
    else return (true);
}

function checkNumber (theField, s, t, emptyOK)
{  if (checkNumber.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return (true);
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    if (isNumeric(theField.value) == false)
       return warnInvalid (theField, t);
    else return (true);
}

function checkRadio(theField,s,emptyOK)
 {var radioSelected = false;
  for (i = 0;  i < theField.length;  i++)
  {
    if (theField[i].checked)
        radioSelected = true;
  }
  if (!radioSelected)
  {	alert(s);
	return (false);
  }
    else return (true);
}

function checkDrop (theField, s)
{  if(isSelected(theField,s))
    return warnSelect (theField, s);
    else return (true);
}

function checkPresent (theField, s, emptyOK)
{  if (checkPresent.arguments.length == 2) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return (true);
    if (isWhitespace(theField.value)) 
       return warnEmpty (theField, s);
    else return (true);
}

/* CALLED FUNCTIONS */
function isSelected(s)
{    if(s.selectedIndex == 0)  return (true);
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)
{   var i;
    if (isEmpty(s)) return (true);
    for (i = 0; i < s.length; i++)
    {  var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return (false);
    }
    return (true);
}

function isAlphabetic(t)
{  var i,j;
    var checkOK = " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþÿ\.'- ";
    if (isEmpty(t)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);
    for (i = 0; i < t.length; i++)
    {   
        var c = t.charAt(i);
        for (j = 0;  j < checkOK.length;  j++)
        if (c == checkOK.charAt(j))
        break;
        if (j == checkOK.length)
        {
        return (false);
        break;
    }
}
    return (true);
}

function isNumeric(t)
{
  var checkOKnum = "0123456789.";
  var decPoints = 0;
  var allNum = "";
    if (isEmpty(t)) 
       if (isNumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isNumeric.arguments[1] == true);
  for (i = 0;  i < t.length;  i++)
  {
    var c = t.charAt(i);
    for (j = 0;  j < checkOKnum.length;  j++)
      if (c == checkOKnum.charAt(j))
        break;
    if (j == checkOKnum.length)
    {
      return (false);
      break;
    }
    if (c == ".")
    {
      allNum += ".";
      decPoints++;
    }
    else if (c!= ",")
      allNum += c;
  }
 if (decPoints > 1)
  {
    return (false);
  } 
	var chkVal = allNum;
	var prsVal = parseFloat(allNum);
   if (chkVal != "" && !(prsVal >= "0.0" && prsVal <= "99"))  //Sets limits on inputted number
  {
    return (false);
  }
}

/* UNUSED FUNCTIONS */
function checkEmail (theField, s)
{  return true;}
function checkComment(theField, s)
{  return true;}