var strMsg;

function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
    return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
    return typeof a == 'boolean';
}

function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}

function isFunction(a) {
    return typeof a == 'function';
}

function isNull(a) {
    return typeof a == 'object' && !a;
}

function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
    return typeof a == 'string';
}

function isUndefined(a) {
    return typeof a == 'undefined';
} 

function OnlyNumbers()
		{
			var CR=13;
			var keyCode=this.event.keyCode;
			return IsInteger(String.fromCharCode(keyCode))||keyCode==CR;
		}
		
function IsInteger(strValue)
	{
		var i, c;
		var str=new String(strValue);
			
		if(str.length<=0)
			return false;
		else
			{
				for(i=0;i<str.length;++i)
				{
					c=str.charAt(i);
					if(c<'0'||c>'9')
						return false;
				}
			}
		return true;
	}

function ValidateInt(fieldValue) {
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	for (var i=0; i<fieldValue.length; i++) 
	{
	temp = "" + fieldValue.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") 
	{
	return false;
	}
	return true;
}

/*function CheckSSN(strValue)
{
	if (strValue.length = 0 || strValue.length != 9) return false;
	return ValidateInt(strValue);
	
	
}*/

function CheckLastNameZip(strLastName, strZip)
{
	if (strLastName.length = 0 || strLastName.length > 15) return false;
	/*if (strZip.length = 0 || strZip.length != 5) return false;
	return ValidateInt(strZip);
	*/
	return ValidateNumbersLength(strZip, 5);
}

function trim(value) 
{
  // Removes Leading and trailing spaces
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2');}
   return temp;
}

function ValidateEmpty(str)
{
	if (trim(str).length != 0) return true;
	return false;
}

function ValidateNumbersLength(strZip, strlen)
{
	if ( !(ValidateEmpty(trim(strZip))) || strZip.length != strlen ) return false;
	return ValidateInt(strZip);
}
function ValidateStringLength(str, strlen)
{
	if ( !(ValidateEmpty(trim(str))) || str.length > strlen ) return false;
	return true;
}
/*function ValidateSSN(strSSN)
{
	if ( !(ValidateEmpty(trim(strSSN))) || strZip.length != 9) return false;
	return ValidateInt(strZip);
}
*/
function popUp(URL)
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=620,left = 426,top = 222');");
}

function formSubmit(form,mode,page)
{	
	if (!(mode==0) && !(mode==3) && !(mode==4))
	{
		if (!ValidateForm(form))
		{
			//window.location.replace('#');
			return;
		}
		
	}
	
	if (form.mode) // Added because the function is being used from other apps than OE
	form.mode.value = mode;
	
	form.action = page;
	
	form.submit();
	
	return true;
}

// 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
}
var winModalWindow;
function IgnoreEvents(e)
{
  return false;
}

function HandleFocus()
{
  if (winModalWindow)
  {
    if (!winModalWindow.closed)
    {
      winModalWindow.focus();
    }
    else
    {
      window.top.releaseEvents (Event.CLICK|Event.FOCUS);
      window.top.onclick = "";
    }
  }
  return false;
}
function showApplyWindowCore(page)
{
  if (window.showModalDialog)
  { 
    var winObject = new Object();	    
	window.showModalDialog(page,this,"dialogHeight=25;dialogWidth=43;center=yes;resizable=yes;status=no;");
  }
  else
  {    
    window.top.captureEvents (Event.CLICK|Event.FOCUS);	    
    window.top.onclick=IgnoreEvents;
    window.top.onfocus=HandleFocus;	    
	winModalWindow = window.open(page,"ViewWin","scrollbars=yes,location=no,resizable=yes,modal=yes,menubar=no,toolbar=no,status=no,width=700,height=400,left=250,top=150",true);
												
    winModalWindow.focus();
  }
	
}
	
function isDate(dtStr){
	strMsg = "";
	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){
		strMsg += "The date format should be : mm/dd/yyyy \n";
		//return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		strMsg += "Please enter a valid month \n";
		//return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		strMsg += "Please enter a valid day \n";
		//return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		strMsg += "Please enter a valid 4 digit year between " + minYear + " and " + maxYear + "\n";
		//return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		strMsg += "Please enter a valid date \n";
		//return false
	}
	if (strMsg != "")
		return false;
	else
		return true;
}

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		alert(str);
		alert(dot);
		alert(lat+2);
		alert(str.indexOf(dot,(lat+2)));
		
		if (str.indexOf(at)==-1){
			alert('1');
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert('2');
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert('3');
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		 alert('4');
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		 alert('5');
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		 alert('6');
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		 alert('7');
		    return false;
		 }

 		 return true;				
	}


function ValidateEmailString(txtBox){

	var emailID = new String(txtBox);
	var aryOut = new Array();
	var upperLimit;
	var i;
	var go = true;
	
	/*if ((emailID==null)||(emailID=="")){
		return false;
	}else{ */
		aryOut = emailID.split(",");
		upperLimit = aryOut.length;
		for (i = 0; i < upperLimit; i++){
			if (!(echeck(aryOut[i]))){
				go = false;
			}
		}
		/*if (!(go)){
			alert("Invalid E-mail Value");	
			txtBox.focus();
			txtBox.select();
		}*/
		return go;
	//}
}

function CheckEmail(str)
{

var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
if (filter.test(str))
 return true;
else
 return false;
}

function maskKeyPress(evt) 
{
  
  var keyCode = evt.which ? evt.which : evt.keyCode;               
  if((keyCode>=48 && keyCode<=57) || keyCode ==8 || keyCode ==11 || keyCode ==9 || keyCode == 46 ) return true;// 8 - backspace, 9 - h tab, 11 - v tab, 46 - Delete, 45 is dash
  return false;
}

function changeCSS(image,form)
{  
	largeCSS = !largeCSS;
	form.largecss.value = largeCSS;
	
	if (!document.styleSheets)
	{
		//alert('Sorry, your browser does not support dynamic style sheets so we can\'t increase the text size.');
		formSubmit(form,(largeCSS?3:4),form.action);
		return;
	}
	
	document.styleSheets[0].disabled=!largeCSS;
	document.styleSheets[1].disabled=largeCSS;

	//image.src = largeCSS?'/images/reducetxt.gif':'/images/enlargetxt.gif';
	image.src = largeCSS?'/enroll/images/button_reducefont_white.gif':'/enroll/images/button_increasefont_white.gif';

	image.alt = largeCSS?'Reduce Text':'Enlarge Text';

	/*var theRules = new Array();
	if (document.styleSheets[1].cssRules)
		theRules = document.styleSheets[1].cssRules
	else if (document.styleSheets[1].rules)
		theRules = document.styleSheets[1].rules
	else return;

	for (var i=0; i<theRules.length; i++)
	{
		if theRules[i].style.backgroundColor = 'transparent';
	}*/

	return;

}

function checkOverRideDate(oMonth, oDay, oYear)
{ 
	var currDate = new Date();
	
	currDate.setHours(00,00,00,00);
	
	if (oMonth != "" && oDay != "" && oYear != "")
	{
		var dtOR = new Date(oMonth + "/" + oDay + "/" + oYear);
		dtOR.setHours(00,00,00,00);
		  if (dtOR < currDate)
		  {
			alert("Override date must be a future date!");
			return false;
		  }
	}
	return true;
}


  function FocusToNextCtrl(input,len, e)
{
if(input.value.length >= len ) {

input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}
function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}

return true;
}