// GLOBAL FORM FUNCTIONS
//********************************************************************************************************
// Created: June 12, 2008
// By: Keith Valley
// Purpose: To create one document that can be included in all form pages to for java form verification 
//
//********************************************************************************************************
// 		TOC
/*
1. isBlank (inString)															
2. isWithinRange (inString, rangeMin, rangeMax)
3. trim (string)
6. advance(str)
7. cent(number)
8. round(number)
9. isNumeric(str)
10. isNumberString(inString)
11. isLetter(c)
12. isEmpty(val, name, title) - DEPRECATED
13. warnInvalid (theField, s) - DEPRACATED
14. isAlphabetic (s, name, title) - DEPRACATED
15. stripCharsInBag (s, bag) - DEPRECATED
16. stripWhitespace (s) - DEPRECATED
17. hidediv(id) 
18. showdiv(id) 
19. showDiet() 
20. popBoard(url)
21. 



*/

// whitespace characters
var whitespace = " \t\n\r";

/*
 * Function: isBlank (inString)
 * Purpose: Is the String Blank or empty
 * Arguments: String
 * Returns: boolean
 **/
function isBlank (inString) {
	if (inString == null || inString.length == 0) {
		return true;
	} else {
		return false;
	}
}
//********************************************************************************
//Function: isWithinRange (inString, rangeMin, rangeMax)
//Purpose: Is the String within the range
//Arguments: String, integer, integer
//Returns: boolean
//********************************************************************************
function isWithinRange (inString, rangeMin, rangeMax)  {
	if ((inString == null) || (inString == "")) { 
		return (false);
	}
	if((inString >= rangeMin) && (inString <= rangeMax)) {
		return true;
	} else {
		return false;
	}
}

//********************************************************************************
//Function: trim (string)
//Purpose: Removes the white space at the edges
//Arguments: String
//Returns: String
//********************************************************************************
function trim(sString) 
{ 
  while (sString.substring(0,1) == ' ') 
  { 
    sString = sString.substring(1, sString.length); 
  } 
  while (sString.substring(sString.length-1, sString.length) == ' ') 
  { 
    sString = sString.substring(0,sString.length-1); 
  } 
return sString; 
} 
//*************************************************
// If textbox reaches 2 in length go to next
//*************************************************
function advance(currentField,nextField) {
    if (currentField.value.length == 3)
        document.regForm[nextField].focus();
}

//****************************************************
// returns the amount in the .99 format
//****************************************************
function cent(amount) {
  amount -= 0;
  return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

//**********************************************************
// returns a number rounded to 2 decimal places
//**********************************************************
function round(number) {
  var X
  X = (!X ? 2 : X);
  return Math.round(number*Math.pow(10,X))/Math.pow(10,X);
}


//********************************************************************************
// Function: isNumeric(str)
// Purpose: is string a numeric (integer or float)
// Arguments: String
// Returns: boolean
//********************************************************************************
function isNumeric(str) {
  var num;

  /* matches 99.999 */
  if (str.indexOf(".") > 0) {
    num = str.split(".");
    if (num.length == 2) {
      for (var i = 0; i < num.length; i++) {
        if (!isNumberString(num[i])) {
	  return false;
	}
      }
      return true;
    } else if (num.length > 2) {
      return false;
    }
    /* matches .999 */
    } else if (str.indexOf(".") == 0) {
      var temp = str.substring(1, str.length);
      if (isNumberString(temp)) {
        return true;
      } else {
	false;
      }
      
    /* matches 999 */
    } else if (isNumberString(str)) {
      return true;
    } else {
    return false;
  }
}
//*******************************************************************************
// Function: isNumberString(inString)
// Purpose: is the String a number (Integer)
// Arguments: String
// Returns: boolean
//*******************************************************************************
function isNumberString(inString)  {
  if (inString.length == 0) { 
    return false;
  }
  var refString = "1234567890";
  for (var count=0; count < inString.length; count++)  {
    var tempChar = inString.substring (count, count + 1);
    if (refString.indexOf(tempChar, 0) == -1) {  
      return false;
    }
  }
  return true;
}


//********************************************************************************
// Function: isLetter(c)
// Purpose: to verify that a character is a letter
// Arguments: a single Character
// Returns: true if character c is an English letter (A .. Z, a..z).
//********************************************************************************
function isLetter (c)
{   
  if (c == "-")
  {
    return true;
    //alert("got here");
  }
  else
  {
   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
  }
}





//****************************************************************************
// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, put focus in it, and return false.
//****************************************************************************
function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

//********************************************************************************
//
//********************************************************************************
function isAlphabetic (s, name, title) {   
  var i;
  var send
  send = "The Field '" + title +"' is not in a Valid format.  Please include only Letters (A-Z or a-z) in this field."
  
  for (i = 0; i < s.length; i++)
  {   
        // Check that current character is letter.
        var c = s.charAt(i);
        
        if (!isLetter(c))
        return warnInvalid(name, send);
  }
    // All characters are letters.
    return true;
}



//********************************************************************************
// Removes all characters which appear in string bag from string s.
//********************************************************************************
function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
//********************************************************************************
// Removes all whitespace characters from s.
// Global variable whitespace (see above)
// defines which characters are considered whitespace.
//********************************************************************************
function stripWhitespace (s)
{   return stripCharsInBag (s, whitespace)
}






//*****************************************************************
// Hide block of HTML code within div tag
//*****************************************************************
function hidediv(id) {
  //safe function to hide an element with a specified id
  if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById(id).style.display = 'none';
  } else {
    if (document.layers) { // Netscape 4
      document.id.display = 'none';
    } else { // IE 4
      document.all.id.style.display = 'none';
    }
  }
}

//*****************************************************************
// show block of HTML code within div tag
//*****************************************************************
function showdiv(id) {
  //safe function to show an element with a specified id
  if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById(id).style.display = 'block';
  } else {
    if (document.layers) { // Netscape 4
      document.id.display = 'block';
    } else { // IE 4
      document.all.id.style.display = 'block';
    }
  }
}

//***************************************************************
// Show or hide Diet information
//***************************************************************
function showDiet() {
  if (document.regForm.diet[1].checked)
  {
    showdiv('diet_display');
  } else {
    document.regForm.diet_specify.value="";
    //document.regForm.pnametag.value="";
    hidediv('diet_display');
  }
}

function showSpecial() {

  if (document.regForm.special_needs[1].checked)
  {
    showdiv('special_display');
  } else {
    document.regForm.special_specify.value="";
    hidediv('special_display');
  }
}

//***************************************************************
// A pop up window function
//***************************************************************
function popBoard(url)
{
	window.open(url, "winn", 'toolbar=0,location=0,directories=0,status=0,menubar=1,scrollbars=0,resizable=0,width=800,height=565');
}


//***************************************************************
// Surf to a different page based on drop down selection
//***************************************************************
function surfto(form) {
       var myindex=form.dest.selectedIndex
       window.open(form.dest.options[myindex].value, 
         target="_blank");
}




function isEmail(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;

  // arrayString = argvalue.split("@"); (works only in netscape3 and above.)
  var retSize = customSplit(argvalue, "@", "arrayString");

  if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }

  return true;

}







