//-----------------------------------------------------------------------------
// common_functions.js
//
//    Common javascript fuctions 
//
// 001: ??-???-2001	SWeir		Initial creation
//
// Revision History: 
//
// 002: 06-Feb-2002  	TStone		Changed IE version required for Hot Sheets
// 003: 13-Nov-2003		APawson		Added function for pop-ups
// 004: 10-Feb-2004     TStone      isEmail now checks for spaces and duplicate @ 
//-----------------------------------------------------------------------------

//default value for functions that optionally allow empty parameters
var defaultEmptyOK = false;

function PopIt(e,url,w,h){
// **********************************************************
// Use:
// <a onclick="PopIt(this.href,424,294);" href="/common/Popups/sompage.htm">...</a>
// 
// Alternatively, you can include a third argument to override default options for your new window
// <a onclick="PopIt(this.href,424,294,'toolbar=yes,scrollbars=yes');" href="/common/Popups/sompage.htm">...</a>
// **********************************************************
	var options = new Array();
	var tmpOpts = '';
	var opts = '';
	// Set up options array
	options['toolbar'] = 'no';
	options['location'] = 'no';
	options['directories'] = 'no';
	options['status'] = 'no';
	options['menubar'] = 'no';
	options['scrollbars'] = 'no';
	options['resizable'] = 'yes';
	options['copyhistory'] = 'no';
	options['width'] = w;
	options['height'] = h;
	options['top'] = 143;
	options['left'] = 55;
	
	//  Override defaults
	if(arguments.length==5){
		tmpOpts = arguments[4].split(",");
		for(var i = 0; i < tmpOpts.length; i++){
			var eq = tmpOpts[i].indexOf("=");
			var key = tmpOpts[i].substr(0,eq);
			var val = tmpOpts[i].substr(eq+1);
			options[key] = val;
		}
	}
	// build string
	for (k in options)   {
      opts += k + "=" + options[k] + ",";
   	}
	// strip final comma from end
	opts = opts.substr(0,opts.length-1);
	
   	// Cancel event bubbling
	// This is necessary since we have a parent element (<td></td>) and 
	// a child element (<a></a>) with the same onclick event handler. Otherwise
	// the event would fire twice.
	e.cancelBubble=true;
	
	// open new window
	var w = window.open(url,"popWin", opts);
	// give it focus in case user leaves it open and clicks again...
	w.focus();
	
}


function FormatNumber(Number,Decimals,Separator)
{
// **********************************************************
// Placed in the public domain by Affordable Production Tools
// March 21, 1998
// Web site: http://www.aptools.com/
//
// November 24, 1998 -- Error which allowed a null value
// to remain null fixed. Now forces value to 0.
//
// This function accepts a number to format and number
// specifying the number of decimal places to format to. May
// optionally use a separator other than '.' if specified.
//
// If no decimals are specified, the function defaults to
// two decimal places. If no number is passed, the function
// defaults to 0. Decimal separator defaults to '.' .
//
// If the number passed is too large to format as a decimal
// number (e.g.: 1.23e+25), or if the conversion process
// results in such a number, the original number is returned
// unchanged.
// **********************************************************
Number += ""          // Force argument to string.
Decimals += ""        // Force argument to string.
Separator += ""       // Force argument to string.
 
if((Separator == "") || (Separator.length > 1))
   Separator = ".";
 
if(Number.length == 0)
   Number = "0";
 
var OriginalNumber = Number  // Save for number too large.
var Sign = 1
var Pad = ""
var Count = 0
 
// If no number passed, force number to 0.
if(parseFloat(Number))
{
   Number = parseFloat(Number);
} 
else 
{
   Number = 0;
}

// If no decimals passed, default decimals to 2.
if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0))
{
	Decimals = parseInt(Decimals,10);
} 
else 
{
	Decimals = 2;
}

if(Number < 0)
{
	Sign = -1;         // Remember sign of Number.
	Number *= Sign;    // Force absolute value of Number.
}

if(Decimals < 0)
	Decimals *= -1;    // Force absolute value of Decimals.

// Next, convert number to rounded integer and force to string value.
// (Number contains 1 extra digit used to force rounding)
Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)

if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
	return(OriginalNumber); // Number too large to format as specified.
	
// If length of Number is less than number of decimals requested +1,
// pad with zeros to requested length.
if(Number.length < Decimals +1) // Construct pad string.
{
	for(Count = Number.length; Count <= Decimals; Count++)
		Pad += "0";
}

Number =  Number + Pad; // Pad number as needed.

if(Decimals == 0)
{
	// Drop extra digit -- Number is formatted.
	Number = Number.substring(0, Number.length -1);
} 
else 
{
	// Or, format number with decimal point and drop extra decimal digit.
	Number = Number.substring(0,Number.length - Decimals -1) +
	         Separator +
	         Number.substring(Number.length - Decimals -1,
	         Number.length -1);
}

if(Sign == -1)
	Number = "-" + Number;  // Set sign of number.
	
if(Number.length == 0)
	Number="0";
		
return(Number)
}


function isEmail (s)
{   
// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.

	var counter;
    var i;

	if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // Search through string's characters one by one
    // searching for a space and duplicate @.
    // If we find a space or more than one @, return false
    counter = 0;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        
		// Keep count of the number of @ charaters found.
		if (c == "@") counter++;
		
		// Check if the current character is a space.
        if (c == " ") return false;
    }
    // Check if more than one @ was found
    if (counter > 1) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var sLength = s.length;
    i = 1;
    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}


function isInteger (s){   
// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

	var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isWhitespace (s)  {   
// Returns true if string s is empty or 
// whitespace characters only.

	var i;
	// whitespace characters
	var whitespace = " \t\n\r";
	
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

function isAlphabetic (s)  {   
// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

	var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!(isLetter(c)))
        return false;
    }

    // All characters are letters.
    return true;
}


function isAlphabeticPlus (s)  {   
// isAlphabetic (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and space, "_" or "-".
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

	var i;

    if (isEmpty(s)) 
       if (isAlphabetic.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphabetic.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!(isLetter(c) || (c == "-") || (c == " ") || (c == ".") || (c == "_")))
        return false;
    }

    // All characters are letters.
    return true;
}

 
function isAlphanumeric (s)   {   
// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z) and numbers only.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

	var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (!(isLetter(c) || isDigit(c)))
        return false;
    }

    // All characters are numbers or letters.
    return true;
}

function isAlphanumericPlus (s)   {   
// isAlphanumeric (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is English letters 
// (A .. Z, a..z), numbers and space, "_" or "-".
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

	var i;

    if (isEmpty(s)) 
       if (isAlphanumeric.arguments.length == 1) return defaultEmptyOK;
       else return (isAlphanumeric.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-alphanumeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number or letter.
        var c = s.charAt(i);

        if (!(isLetter(c) || isDigit(c) || (c == " ") || (c == "_") || (c == "-")))
        return false;
    }

    // All characters are numbers or letters.
    return true;
}


function isEmpty(s)	{   
// Check whether string s is empty.
	
	return ((s == null) || (s.length == 0))
}


function isLetter (c)  {   
// Returns true if character c is an English letter 
// (A .. Z, a..z).
//
// NOTE: Need i18n version to support European characters.
// This could be tricky due to different character
// sets and orderings for various languages and platforms.

	return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}


function isDigit (c)   {
// Returns true if character c is a digit 
// (0 .. 9).

   return ((c >= "0") && (c <= "9"))
}

function isLetterOrDigit (c)   {   
// Returns true if character c is a letter or digit.

	return (isLetter(c) || isDigit(c))
}

function popup(page, name, details)	{
	
	if (details)
		newWin=window.open(page, name, details);
	else
		newWin=window.open(page, name);
		
	newWin.focus();
		
}
	
	
//****************************************************************
//sports action functions	
//****************************************************************
function printMenu(menunumber)	{
//this function pops up the pritable oddset/ps menu
//if the menunumber var is given a value then the print menu page will use this value, otherwise todays menu is shown

	popup("/Lottery/SportsActionOddset/OddsetPointSpreadMenuPrint.asp?printmenunum=" + menunumber, "Print");
}

function GamePicks_onsubmit() {
//this function submits the GamePicks form off of the sports action pages
//it is used in the navigation to submit the information to the HotSheet		

	if ((is_nav4up) || (is_ie4up))
	{

		if (is_nav4)
		{
			if (!document.bet)
			{
				alert("You can only view Hot Sheets from the Sports Action Menus.");
				return;
			}
		}
		else
		{
			if (!document.GamePicks)
			{
				alert("You can only view Hot Sheets from the Sports Action Menus.");
				return;
			}
		}
				
		if (adobe)
		{
			if (submit_OK)
			{
				if (is_nav4)
					document.bet.document.GamePicks.submit();
				else
					document.GamePicks.submit();
			}
			else
			{
				alert(msg);
			}	
		}
		else
		{
			alert("You need to have Adobe Acrobat Reader installed to use this feature. If you don't have this software on your system, visit www.adobe.com");
		}
	}
	else
		alert("You need to have Internet Explorer 5.0 or higher or Netscape 4.0 or higher to use this feature.");
}
//****************************************************************
//end sports action functions
//****************************************************************
