//start text area character counter
function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else
countfield.value = maxlimit - field.value.length;
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// ##############################
function checkAll(field) 
{
if (field.length > 1) {
    for (i = 0; i < field.length; i++)
    	field[i].checked = true ;
} else {
    field.checked = true;
}
}

// ##############################
function uncheckAll(field) 
{
if (field.length > 1) {
    for (i = 0; i < field.length; i++)
    	field[i].checked = false ;
} else {
    field.checked = false;
}
}

// ##############################
function showHide(foo,bar) {
    if (foo.checked) {
		bar.value = "";
		bar.disabled = false;
	} else {
		bar.value = "";
		bar.disabled = true;
	}
}



// ##############################
function checkrequiredStandard(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#dddddd';
  var bgGood = "white";
 
if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
	}
}

  
if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist+" is required.");
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else {
    return true;
}
}

// ##############################
function checkrequiredMemberLogin(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#dddddd';
  var bgGood = "white";
 
if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.name=="email" && tempobj.value=='') {
			fieldlist = "Login/Email";
			pass=false;
			break;
		}
		if (tempobj.name=="password" && tempobj.value=='') {
			fieldlist = "Password";
			pass=false;
			break;
		}

	}
}

  
if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist+" is required.");
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else {
    return true;
}
}
// ##############################
function checkrequiredMemberProfile(which) {

var pass=true;
fieldlist = "";

  var bgBad = '#dddddd';
  var bgGood = "white";
 
if (document.images) {
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if (tempobj.name=="organizationID" && tempobj.value=='X') {
			fieldlist = "Organization";
			pass=false;
			break;
		}
		if (tempobj.name=="FirstName" && tempobj.value=='') {
			fieldlist = "First Name";
			pass=false;
			break;
		}
		if (tempobj.name=="LastName" && tempobj.value=='') {
			fieldlist = "Last Name";
			pass=false;
			break;
		}
		if (tempobj.name=="positionID" && tempobj.value=='X') {
			fieldlist = "Position";
			pass=false;
			break;
		}
		if (tempobj.name=="Email" && tempobj.value=='') {
			fieldlist = "Email";
			pass=false;
			break;
		}
		if (tempobj.name=="Direct_Phone" && tempobj.value=='') {
			fieldlist = "Direct Phone";
			pass=false;
			break;
		}
	}
}

  
if (!pass) {
	setColor(tempobj, bgBad);
	alert(""+fieldlist+" is required.");
	tempobj.focus();
//	tempobj.blur();
	return false;
}
else {
    return true;
}
}

// ##############################
function setColor(el, bg) {
  if (el.style) el.style.backgroundColor = bg;
}

// ##############################
function validate(field) {
var valid = "0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are allowed!");
field.value = "";
field.focus();
field.select();
   }
}

// ##############################
function validate2(field) {
var valid = "0123456789."
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are allowed!");
field.value = "";
field.focus();
field.select();
   }
}

// ##############################
function validate3(field) {
var valid = "0123456789. "
var ok = "yes";
var temp;
var decfound=field.value.indexOf(".");
if (decfound>0) {
    var decvalue = field.value.substring(decfound+1);
    if (decvalue=="0" || decvalue=="00" || decvalue=="25" || decvalue=="5" || decvalue=="50" || decvalue=="75") {
    } else { 
        alert("Invalid entry: Hours must be entered in multiples of quarter hours (.25, .50, .75)");
        field.value=""; 
        field.focus();
        field.select();
        return false;
    }    
} 
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only numbers are allowed!");
field.value = "";
field.focus();
field.select();
   }
}

// ##############################
function validateZip(field) {
var valid = "0123456789"
var ok1 = "yes";
var ok2 = "yes";
var temp;
if (field.value.length == 0) {
} else {
	for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok1 = "no";
	}
	if (field.value.length == 5) {
	} else {
	  ok2 = "no";
	}
	if (ok1 == "no") {
	alert("Invalid entry!  Only numbers are accepted!");
    field.value = "";
	field.focus();
	field.select();
	   }
	if (ok2 == "no") {
	alert("Field must contain five numbers");
	field.value = "";
	field.focus();
	field.select();
	   }
	}
}

// ##############################
function gtpage(url,frname,height,width) {

        var p1 = 'toolbar=no,'
        var p2 = 'location=no,'
        var p3 = 'directories=no,'
        var p4 = 'status=no,'
        var p5 = 'menubar=no,'
        var p6 = 'resizable=no,'
        var p7 = 'scrollbars=no,'
        var p8=  'copyhistory=no,'
        var p9 = 'width='+width+',';
        var p10 = 'height='+height
        var props = p1+p2+p3+p4+p5+p6+p7+p8+p9+p10;

        themainwindow = window.open(url,frname,props);
        themainwindow.focus();

     }

// ##############################
function gtpage2(url,frname,height,width) {

        var p1 = 'toolbar=no,'
        var p2 = 'location=no,'
        var p3 = 'directories=no,'
        var p4 = 'status=no,'
        var p5 = 'menubar=no,'
        var p6 = 'resizable=yes,'
        var p7 = 'scrollbars=yes,'
        var p8=  'copyhistory=no,'
        var p9 = 'width='+width+',';
        var p10 = 'height='+height
        var props = p1+p2+p3+p4+p5+p6+p7+p8+p9+p10;

        themainwindow = window.open(url,frname,props);
        themainwindow.focus();

     }

// ##############################
function printpage() {
	window.print();
}

// ###################################
function check_date(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(0,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(2,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = month + seperator + day + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is incorrect!\n\nMust use format: mm/dd/yyyy");
      DateField.select();
	  DateField.focus();
   }
}

// ###################################
function check_date2(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(0,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(2,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = month + seperator + day + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is incorrect!\n\nMust use format: mm/dd/yyyy");
      err = 0; day = ""; month = ""; year = ""; seperator = "";
      DateField.value = month + seperator + day + seperator + year;
      DateField.select();
	  DateField.focus();
   }
}

// ###################################
 function check_time(field) { 
    var TimeField = field;
 
    // regular expression to match required time format 
    re = /^\d{1,2}:\d{2}([ap]m)?$/; 
    if (TimeField.value != '' && !TimeField.value.match(re)) { 
        alert("Invalid time format: " + TimeField.value+"\n\nTimes should be entered using 12:34am format"); 
        TimeField.value=""; 
        TimeField.focus(); 
        return false; 
    }
}  
 
 
// ###################################
function check_birthdate(field){
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '19' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(0,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(2,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = month + seperator + day + seperator + year;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is incorrect!\n\nMust use format: mm/dd/yyyy");
      DateField.select();
	  DateField.focus();
   }
}

// ###################################
 function check_time(field) { 
    var TimeField = field;
 
    // regular expression to match required time format 
    re = /^\d{1,2}:\d{2}([ap]m)?$/; 
    if (TimeField.value != '' && !TimeField.value.match(re)) { 
        alert("Invalid time format: " + TimeField.value+"\n\nTimes should be entered using 12:34am format"); 
        TimeField.focus(); 
        return false; 
    }
}  
 
 
// ##############################
function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid e-mail address entered")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid e-mail address entered")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    alert("Invalid e-mail address entered")
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    alert("Invalid e-mail address entered")
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    alert("Invalid e-mail address entered")
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    alert("Invalid e-mail address entered")
	    return false
	 }
		
	 if (str.indexOf(" ")!=-1){
	    alert("Invalid e-mail address entered")
	    return false
	 }

 	 return true					
}



// ###################################
function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

// ###################################
function MM_goToURL_deleteEvent() { //v3.0
	var agree=confirm("Are you sure you want to delete this item?\n\nThis action cannot be undone!");
	if (agree) {
      var i, args=MM_goToURL_deleteEvent.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ###################################
function MM_goToURL_copySurvey() { //v3.0
	var agree=confirm("Are you sure you want to copy this Survey to a new Survey?");
	if (agree) {
      var i, args=MM_goToURL_copySurvey.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ###################################
function MM_goToURL_submitProgress() { //v3.0
	var agree=confirm("Are you sure you want to submit this progress report for State Commission approval?");
	if (agree) {
      var i, args=MM_goToURL_submitProgress.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ###################################
function MM_goToURL_submitMDMonthly() { //v3.0
	var agree=confirm("Are you sure you want to submit this report for UMDHSA approval?");
	if (agree) {
      var i, args=MM_goToURL_submitMDMonthly.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ###################################
function MM_goToURL_enrollMember() { //v3.0
	var agree=confirm("Are you sure you want to enroll this prospective member?");
	if (agree) {
      var i, args=MM_goToURL_enrollMember.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ###################################
function MM_goToURL_unlockTimesheet() { //v3.0
	var agree=confirm("Are you sure you want to unlock this time sheet?");
	if (agree) {
      var i, args=MM_goToURL_unlockTimesheet.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ###################################
function MM_goToURL_removeNotification() { //v3.0
	var agree=confirm("Are you sure you want to remove this notification?");
	if (agree) {
      var i, args=MM_goToURL_removeNotification.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ###################################
function MM_goToURL_removeFile() { //v3.0
	var agree=confirm("Are you sure you want to remove this file?");
	if (agree) {
      var i, args=MM_goToURL_removeFile.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
	} else {
		return false ;
    }
}

// ##############################
var type = "IE";	//Variable used to hold the browser name

BrowserSniffer();

// ##############################
//detects the capabilities of the browser
function BrowserSniffer() {
	if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) type="OP";		//Opera
	else if (document.all) type="IE";														//Internet Explorer e.g. IE4 upwards
	else if (document.layers) type="NN";													//Netscape Communicator 4
	else if (!document.all && document.getElementById) type="MO";							//Mozila e.g. Netscape 6 upwards
	else type = "IE";		//I assume it will not get here
}

// ##############################
//Displays the generic browser type
function whatBrows() {
	window.alert("Browser is : " + type);
}

// ##############################
//Puts the contents of str into the layer id
//id is the name of the layer
//str is the required content
//Works with all browsers except Opera
function ChangeContent(id, str) {
	if (type=="IE") {
		document.all[id].innerHTML = str;
	}
	if (type=="NN") { 
		document.layers[id].document.open();
		document.layers[id].document.write(str);
		document.layers[id].document.close();
	}
	if (type=="MO" || type=="OP") {
		document.getElementById(id).innerHTML = str;
	}
}

// ##############################
//Change the color of the layer background
//id is the name of the layer
//color is the required color
//Works with all browsers except NN4
function ChangeLayerBgColor(id, color){
	if (type=="IE") document.all[id].style.backgroundColor=color;
	if (type=="NN") document.layer['id'].bgColor=color;
	if (type=="MO" || type=="OP") document.getElementById(id).style.backgroundColor=color;
}

// ##############################
//Show and hide a layer
//id is the name of the layer
//action is either hidden or visible
//Seems to work with all versions NN4 plus other browsers
function ShowLayer(id, action){
	if (type=="IE") eval("document.all." + id + ".style.visibility='" + action + "'");
	if (type=="NN") eval("document." + id + ".visibility='" + action + "'");
	if (type=="MO" || type=="OP") eval("document.getElementById('" + id + "').style.visibility='" + action + "'");
}
// ##############################
function RefreshImage(valImageId) {
	var objImage = document.images[valImageId];
	if (objImage == undefined) {
		return;
	}
	var now = new Date();
	objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
}
