/****************************************************************************
Basic JavaScript Function Library that holds functions common to all pages
in a Web site. 

Copyright (c) 2000 Robert Plates All Rights Reserved.

LastEdit: 5-21-2002
R.Plates added Form Functions
R.Plates added PromotionCode Address function
R.Plates 5-21-02 
	added ZipCode Checking
****************************************************************************/
/****************************************************************************
// The following variables insure that string names are
// constant throught the script
****************************************************************************/
var strFullName="Name";
var strGivenName="Given Name (First Name)";
var strFamilyName="Family Name (Last Name)";
var strContactName="Contact Name (Full Name)";
var strTitle="Title";
var strCompany="Company";
var strAddress="Street Address";
var strCity="City";
var strStateProvince="State or Province";
var strZip="Postal Code";
var strCountry="Country";
var strPhone="Phone Number";
var strFax="Fax Number";
var strEmail="Email address";
var strPrompt="Please enter your ";
var wEmptyPrefix="The field ";
var wEmptySuffix=" is required.";
var errPhone="The phone number is not valid.";
var errEmail="The e-mail address is not valid.";
var whitespace=" \t\n\r";

/****************************************************************************
// General functions
****************************************************************************/

function IEBrowser()
{
  if (navigator.appName.indexOf("Microsoft") != -1)
    return true;
  else
    return false;
}

/****************************************************************************
//form validation functions
****************************************************************************/

function isEmpty(str) 
{
  if ((str.length==0) || (str==null)) 
    return true;
  return false;
}

/****************************************************************************
// hasWhitespace returns true if any char in the string is a whitespace char
****************************************************************************/
function hasWhitespace(str) 
{
   for (var i=0; i<str.length; i++) 
      {
      var chr=str.charAt(i);
      if (whitespace.indexOf(chr) > -1) 
        return true;
      }
   return false;
}

/****************************************************************************
// isWhitespace returns true if all the char in the string are whitespace chars
****************************************************************************/
function isWhitespace(str) 
{
   var whitespacecount=0;
   for (var i=0; i<str.length; i++) 
      {
      var chr=str.charAt(i);
      //alert("Whitespace check char at pos:" + i + " is " + chr); 	
      if (whitespace.indexOf(chr) > -1) 
	whitespacecount++;        
      }
   //alert("Whitespace check: whitespace chars=" + whitespacecount + " length=" + str.length);
   return (whitespacecount == str.length);
}


// Functions that issue warnings
/****************************************************************************
// warnEmpty alerts to an empty string and sets focus to the Form Field
****************************************************************************/
function warnEmpty(elementObj, str) 
{
   elementObj.focus();
   alert(wEmptyPrefix+str+wEmptySuffix);
   return false;
}

/****************************************************************************
// warnInvalid alerts to an invalid string and sets focus to the Form Field
****************************************************************************/
function warnInvalid(elementObj, str) 
{
   elementObj.focus();
   elementObj.select();
   alert(str);
   return false;
}

// edit functions
/****************************************************************************
// isEmail returns true is valid email address
****************************************************************************/
function isEmail(emailStr) 
{
   if (isWhitespace(emailStr)) 
	return false;
   // There needs to be an @ sign after the first
   // charachter but before the end of the string
   var idx=emailStr.indexOf("@", 1);
   if (idx==-1) 
        return false;
   // Need a period to have a domain name
   idx+=2
   if (emailStr.indexOf(".", idx)==-1)
      return false;
   else
      return true;
}

// event handlers
function promptMsg(msg) 
{
   self.status=strPrompt+msg;
}


/****************************************************************************
// checkStr returns true if field has a value false if empty or all chars in the string are whitespace
****************************************************************************/
function checkStr(theObj, fieldStr, reqField) 
{
   if (isEmpty(theObj.value)) 
   {
      if (reqField==false) 
           return true;
      else 
           return warnEmpty(theObj, fieldStr);
   }
   if (isWhitespace(theObj.value))
       return warnEmpty(theObj, fieldStr);
   else
       return true;		
}

function checkNumber(theObj, fieldStr, reqField)
{
   if (checkStr(theObj, fieldStr, reqField))
      {
      if (isNaN(parseInt(theObj.value)))
         return warnInvalid(theObj, fieldStr + " is Not a Number.");
      else
         return true;
      }
   else
      return false;
}

function checkNumValue(theObj, minVal, maxVal)
{
   var numVal=parseInt(theObj.value);
   return (numVal >= minVal && numVal <= maxVal);
}

function isChecked(theObj)
{
   return theObj.checked;
}

function getListSelection(theObj)
{
   for(var s=0; s<theObj.length; s++)
      if(theObj[s].selected)
         return theObj[s].value;
}

function getSelectedRadioButton(theObj)
{
   for(var s=0; s<theObj.length; s++)
      if(theObj[s].checked)
         return theObj[s].value;
}

function checkEmail(theObj, fieldStr, reqField) 
{
   if (isEmpty(theObj.value)) 
   {
      if (reqField==false) 
          return true;
      else 
          return warnEmpty(theObj, fieldStr);
   }
   if (isEmail(theObj.value)) 
	return true;
   return warnInvalid(theObj, errEmail);
}

// This is an example of using this form checking library
// Usage at the button level:ONCLICK="if (checkForm(this.form)) submit()"

// Also note the names after frmObj. Typically they are set as such
// <td><input TYPE="TEXT" NAME="givenName" SIZE="20" MAXLENGTH="20" ONFOCUS="promptMsg(strGivenName)" ONCHANGE="checkStr(this, strGivenName, true)"></td>

//function checkForm(frmObj) {
//   return(
//      checkStr(frmObj.givenName, strGivenName, true) &&
//      checkStr(frmObj.familyName, strFamilyName, true) &&
//      checkStr(frmObj.Address, strAddress, true) &&
//      checkStr(frmObj.City, strCity, true) &&
//      checkStr(frmObj.statProvince, strStateProvince, true) &&
//      checkStr(frmObj.Zip, strZip, true) &&
//      checkStr(frmObj.Country, strCountry, true) &&
//      checkPhone(frmObj.Phone, strPhone, false) &&
//      checkPhone(frmObj.Fax, strFax, false) &&
//      checkEmail(frmObj.Email, false)
//   )
//}


/****************************************************************************
//Open Window Functions
****************************************************************************/
function OpenWin(sUrl, Height, Width, Top, Left, Name)
{
    var MyHeight;
    var MyWidth;
    var MyTop;
    var MyLeft;
    var Features;
    var WinName;

    //Capture the Window Name or Set the Default
    if(Name==null)
        {WinName="GenWindow";}
    else
        {WinName=Name;}

	// Capture the Height or Set the Default
  	if(isNaN(parseInt(Height)))	
		{
		//window.alert("No Height Given."); //For Testing Only
		//Set a Default Height
		MyHeight=250;
		}
	else
		MyHeight=Height;
	// Capture the Width or Set the default
	if(isNaN(parseInt(Width)))
		{
		//window.alert("No Width Given."); //For testing Only
		MyWidth=500;
		}
	else
		MyWidth=Width;

	//Calculate the Top Value
	if(isNaN(parseInt(Top)))
		MyTop=(screen.height-MyHeight)/2;
	else
		MyTop=Top;

	//Calculate Left Value
	if(isNaN(parseInt(Left)))
		MyLeft=(screen.width-MyWidth)/2;
	else
		MyLeft=Left;

	//For testing Only
	//alert("Top=" + MyTop + "\nLeft=" + MyLeft + "\nHeight=" + MyHeight + "\nWidth=" + MyWidth);
  	if (IEBrowser())
		{
		Features="height=" + MyHeight + ", width=" + MyWidth + ", left=" + MyLeft + ", top=" + MyTop+", resizeable=yes";
		}
	else
		{
		Features="height=" + MyHeight + ",width=" + MyWidth + ",screenX=" + MyLeft + ",screenY=" + MyTop+",resizeable=yes";
		}
        return window.open(sUrl, WinName, Features);
}

function OpenScrollWin(sUrl, Height, Width, Top, Left, Name)
{
    var MyHeight;
    var MyWidth;
    var MyTop;
    var MyLeft;
    var Features;
    var WinName;

    //Capture the Window Name or Set the Default
    if(Name==null)
        {WinName="GenWindow";}
    else
        {WinName=Name;}

	// Capture the Height or Set the Default
  	if(isNaN(parseInt(Height)))	
		{
		//window.alert("No Height Given."); //For Testing Only
		//Set a Default Height
		MyHeight=250;
		}
	else
		MyHeight=Height;
	// Capture the Width or Set the default
	if(isNaN(parseInt(Width)))
		{
		//window.alert("No Width Given."); //For testing Only
		MyWidth=500;
		}
	else
		MyWidth=Width;

	//Calculate the Top Value
	if(isNaN(parseInt(Top)))
		MyTop=(screen.height-MyHeight)/2;
	else
		MyTop=Top;

	//Calculate Left Value
	if(isNaN(parseInt(Left)))
		MyLeft=(screen.width-MyWidth)/2;
	else
		MyLeft=Left;

	//For testing Only
	//alert("Top=" + MyTop + "\nLeft=" + MyLeft + "\nHeight=" + MyHeight + "\nWidth=" + MyWidth);
  	if (IEBrowser())
		{
		Features="height=" + MyHeight + ", width=" + MyWidth + ", left=" + MyLeft + ", top=" + MyTop + ", scrollbar=yes, resizeable=yes";
		}
	else
		{
		Features="height=" + MyHeight + ",width=" + MyWidth + ",screenX=" + MyLeft + ",screenY=" + MyTop + ",scrollbar=yes,resizeable=yes";
		}
        return window.open(sUrl, WinName, Features);
}

function PromotionCodeAddress(sPromotionCode)
{
   // for debugging-->alert("Prom Code=" + '"' + sPromotionCode.value + '"');
   switch (sPromotionCode.value)
   {
     case "Witness":
     case "witness":
     case "WITNESS":
        return new String("http://www.gen-e-sys.com/MinistryTracker/default.htm");
        break;
     case "ssdemo":
     case "SSDEMO":
        return new String("https://argos.hmdns.net/~dishmann/Scheduler/DemoQueryOld.htm");
        break;
     case "popdemo":
     case "POPDEMO":
        return new String("https://argos.hmdns.net/~dishmann/Projects/ProjectPro/RegiClear.htm");
        break;
		
     default: 
     	return new String("");
   }
}

function CCValid(sCardNum, sCardType)
{
	var iDigitCount=0;
	for(var i=0; i < sCardNum.length; i++)
		{
		var chr=sCardNum.charCodeAt(i);
		if(chr >= 48 && chr <= 57)
			iDigitCount++;
		}
	if(sCardType.toUpperCase()=="AMEX")
		{
		if(iDigitCount==15)
			return true;
		else
			return false;
		}
	else
		{
		if(iDigitCount==16)
			return true;
		else
			return false;
		}		
}

function CCValidYear(sYearVal)
{
	//Check for 4 digit Year
	if(sYearVal.length != 2)
		{
		alert("Please enter a 2 digit year for your Credit Card's expiration date.");
		return false;
		}
	var d=new Date();
	if(parseInt("20"+sYearVal) < d.getYear())
		{
		alert("Your Credit Card is expired.");
		return false;
		}
	return true;
}

function ValidPromoCode(sPromoCode)
{
	switch(sPromoCode)
	{
		case "TTEM01":
			return true;
		case "ttem01":
			return true;
		case "SSDM01":
			return true;
		case "ssdm01":
			return true;
		default:
			return false;
	}
}

function ValidUSZipCode(sZipCode)
{
	var i;
	var chr;
	var retVal;
	
	if(sZipCode.length < 5)
		return false;
	if(sZipCode.length > 10)
		return false;
	
	for(i=0;i<5;i++)
		{
		chr=sZipCode.charCodeAt(i);
		if(chr < 48)
			return false;
		if(chr > 57)
			return false;
		}
	//1st 5 characters are digits
	if(sZipCode.length > 5)
		{
		if(sZipCode.length != 10)
			return false;
		for(i=i;i < sZipCode.length-1;i++)
			{
			chr=sZipCode.charCodeAt(i);
			if(i==5)
				{
				if(chr==46 || chr==32)
					continue;
				}
			if(chr < 48)
				return false;
			if(chr > 57)
				return false;
			}
		}
	return true;
}



