/********************************************************************
* Copyright ©  Acsys, Inc.  All rights reserved.
* 
* This material contains the valuable properties and trade secrets of
* Acsys, Inc. embodying substantial creative efforts and confidential 
* information, ideas, and expressions, no part of which may be 
* reproduced or transmitted in any form or by any means, electronic, 
* mechanical, or otherwise, including photocopying and recording or 
* in connection with any information storage or retrieval system 
* without the permission in writing of Acsys, Inc.
*/

/********************************************************************
* This is the main Acsys javascript library. When working with any
* of the javascript libraries, it is recommended that you include
* this file. This library also defines a class for sniffing the 
* browser type and plug-in support.  It is roughly meant to be a 
* client side browser capabilities object.
*/

/********************************************************************
* 
* Dependencies: 
*	None
*/

// Constants used by this file
var MAX_FLASH_VERSION = 6

// Create a global browser capabilities.
var browser = new BrowserCapabilities();
// array for storing references to onload functions
var onLoadFunctionsAcsys=[];


/********************************************************************
* The input argument must be a string type.
*/
function AddWindowOnload(f)
{
	if (typeof(f) != "string") return;
	
	if (browser.IsIEmac && browser.IsIE4)
	{
		// IE 4.5 blows out on testing window.onload, I'm not sure I care
		window.onload = WindowOnload;
	}
	else if  (window.onload && (window.onload != AcsysWindowLoad))
	{
		onLoadFunctionsAcsys[onLoadFunctionsAcsys.length] = window.onload.toString();
		window.onload = AcsysWindowLoad;
	}
	else
	{
		window.onload = AcsysWindowLoad;
	}
	onLoadFunctionsAcsys[onLoadFunctionsAcsys.length] = f;
}

function AcsysWindowLoad()
{
	for (var i=0,len=onLoadFunctionsAcsys.length;
		i<len;
		eval(onLoadFunctionsAcsys[i++])
	);
}

/********************************************************************
* This class encapsulates the version of the current browser and also
* provides information javascript and flash capabilities.
*/
function BrowserCapabilities()
{
	this.IsMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	this.IsWindows = (navigator.appVersion.indexOf("Windows") != -1) ? true : false;
	this.IsNN4 = (document.layers) ? true : false;
	this.IsIE = (document.all) ? true : false;
	this.IsIE4 = this.IsIE && !document.getElementById ? true : false;
	this.IsDOM = document.getElementById ? true : false;
	this.IsIEmac = ((this.IsIE)&&(this.IsMac)) ? true : false;
	this.JavaScript = new JavaScriptSupport(this);
	this.Flash = new FlashSupport(this);
}

function JavaScriptSupport(browserCapabilities)
{
	this.SniffVersions = _JavaScriptSupport_SniffVersion;
	this.SniffVersions();
	this.Browser = browserCapabilities;
	this.Version1_0 = window["JavaScript1_0"];
	this.Version1_1 = window["JavaScript1_1"];
	this.Version1_2 = window["JavaScript1_2"];
	this.Version1_3 = window["JavaScript1_3"];
	this.Version1_4 = window["JavaScript1_4"];
}

function _JavaScriptSupport_SniffVersion()
{
	window["JavaScript1_0"] = false;
	window["JavaScript1_1"] = false;
	window["JavaScript1_2"] = false;
	window["JavaScript1_3"] = false;
	window["JavaScript1_4"] = false;

	document.write('<SCR' + 'IPT LANGUAGE="JavaScript"\>\n');
	document.write('JavaScript1_0 = true;\n');
	document.write('</SCR' + 'IPT\>\n');

	document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.1"\>\n');
	document.write('JavaScript1_1 = true;\n');
	document.write('</SCR' + 'IPT\>\n');

	document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.2"\>\n');
	document.write('JavaScript1_2 = true;\n');
	document.write('</SCR' + 'IPT\>\n');

	document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.3"\>\n');
	document.write('JavaScript1_3 = true;\n');
	document.write('</SCR' + 'IPT\>\n');

	document.write('<SCR' + 'IPT LANGUAGE="JavaScript1.4"\>\n');
	document.write('JavaScript1_4 = true;\n');
	document.write('</SCR' + 'IPT\>\n');
}

function FlashSupport(browserCapabilities)
{
	this.SniffVersions = (browserCapabilities.IsWindows && 
		browserCapabilities.IsIE && 
		browserCapabilities.JavaScript.Version1_1)
		? _FlashSupport_SniffVersion_WindowsIE
		: _FlashSupport_SniffVersion_Other;

	this.VersionNumber = -1;
	this.MaxVersion = MAX_FLASH_VERSION;
	this.SniffVersions();
	this.Browser = browserCapabilities;
	this.Version2 = window["Flash2Installed"];
	this.Version3 = window["Flash3Installed"];
	this.Version4 = window["Flash4Installed"];
	this.Version5 = window["Flash5Installed"];
	this.Version6 = window["Flash6Installed"];
}

function _FlashSupport_SniffVersion_WindowsIE()
{
	window["Flash2Installed"] = false;
	window["Flash3Installed"] = false;
	window["Flash4Installed"] = false;
	window["Flash5Installed"] = false;
	window["Flash6Installed"] = false;

	document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Flash2Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.2"))) \n');
	document.write('Flash3Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.3"))) \n');
	document.write('Flash4Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.4"))) \n');
	document.write('Flash5Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.5"))) \n');
	document.write('Flash6Installed = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');
	document.write('</SCR' + 'IPT\> \n');

	for (var i = 2; i <= this.MaxVersion; i++) 
	{
		if (window["Flash" + i + "Installed"] == true) 
		{
			this.VersionNumber = i;
		}
	}
}

function _FlashSupport_SniffVersion_Other()
{
	window["Flash2Installed"] = false;
	window["Flash3Installed"] = false;
	window["Flash4Installed"] = false;
	window["Flash5Installed"] = false;
	window["Flash6Installed"] = false;

	if (navigator.plugins)
	{
		if (navigator.plugins["Shockwave Flash 2.0"] || 
			navigator.plugins["Shockwave Flash"]) 
		{
			// Set convenient references to flash 2 and the plugin description.
			var isVersion2 = navigator.plugins["Shockwave Flash 2.0"] ? true : false;
			var flashDescription = navigator.plugins["Shockwave Flash" + (isVersion2 ? " 2.0" : "")].description;

			var flashVersion = parseInt(flashDescription.charAt(flashDescription.indexOf(".") - 1));

			Flash2Installed = flashVersion == 2;
			Flash3Installed = flashVersion == 3;
			Flash4Installed = flashVersion == 4;
			Flash5Installed = flashVersion >= 5;
			Flash6Installed = flashVersion >= 6;
		}
	}

	for (var i = 2; i <= this.MaxVersion; i++) 
	{
		if (window["Flash" + i + "Installed"] == true) 
		{
			this.VersionNumber = i;
		}
	}

	if(navigator.userAgent.indexOf("WebTV") != -1)
	{
		this.VersionNumber = 3;
	}
}

function HideFlash(){
    document.getElementById('flash').style.visibility = 'hidden';
}

function ShowFlash(){
    document.getElementById('flash').style.visibility = 'visible';
}
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    
    if (pair[0] == variable) {
      return pair[1];
    }
    else {
     
    }
  
  } 
  return "";
}

function findControl(controlid)
{

	for (y=0; y < document.forms.length;y++)
	{
	f = document.forms[y] ;
	
	
	for (r=0;r < f.length ; r++)
	{
	
		if (f.elements[r].id.indexOf(controlid) > -1 )
		{
			return f.elements[r].id ;
		}
	
	}
	}


}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		//alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		//alert("Please enter a valid date")
		return false
	}
return true
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function iSSSNValidation(ssn) {
var matchArr = ssn.match(/^(\d{3})-?\d{2}-?\d{4}$/);
var numDashes = ssn.split('-').length - 1;
if (matchArr == null || numDashes == 1) {
    return false;
//msg = "does not appear to be valid";
}
else 
if (parseInt(matchArr[1],10)==0) {
//alert("Invalid SSN: SSN's can't start with 000.");
    return false;

}
else {
        return true;
   }
}



