function Trim(s)
{
	var trimmed="";
	var leading = true;
	var trailing = true;

	// strip leading spaces
	for(var i = 0; i < s.length; i++) {
        	var c = s.charAt(i);
        	if (c == ' ') {
			if (leading==false) {
				trimmed=trimmed+c;
			}
		}
		else {
			leading=false;
			trimmed=trimmed+c;
		}
    	}

    	// strip trailing spaces
    	for (var i = trimmed.length-1; i>0; i--)
    	{
		var c = trimmed.charAt(i);
		if (c != " ") {
			return trimmed;
		}
		else {
			trimmed=trimmed.substr(0,trimmed.length-1);
		}
    	}

	return trimmed;
}

function checkforphonenumber(a,that)
{
		if ((that.value.length==a) && (event.keyCode!=8))
			that.value+='-';
}

function CheckLifeIDLifeID(idno,that)
			{
				if (idno.length!=9)
				{

					//that.value='';

					//that.focus();
					return false;
				}
				else
				{

					var id1 = idno.charCodeAt(0) - 48 + idno.charCodeAt(2) - 48 + idno.charCodeAt(4) - 48 + idno.charCodeAt(6) - 48; 
					var id2 = (idno.charCodeAt(1) - 48) * 2;
					var id4 = (idno.charCodeAt(3) - 48) * 2;
					var id6 = (idno.charCodeAt(5) - 48) * 2;
					var id8 = (idno.charCodeAt(7) - 48) * 2;
					if (id2 > 9) 
						id2 = 1 + id2 % 10; 
					if (id4 > 9) 
						id4 = 1 + id4 % 10; 
					if (id6 > 9) 
						id6 = 1 + id6 % 10;
					if (id8 > 9) 
						id8 = 1 + id8 % 10; 
					var id9 = id1 + id2 + id4 + id6 + id8;
					if (id9 % 10 == 0) 
						id9 = 0; 
					else 
						id9 = 10 - (id9 % 10);
					if ((id9 != (idno.charCodeAt(8) - 48)) || (idno=='000000000'))
					{
						return false;
					//	that.value='';
					//	that.focus();
					}
				}
			}

function IsBlank(s)
{
    for(var i = 0; i < s.length; i++) {
    	var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t'))
        	return false;
    }
    return true;
}

function isValidEmail(email) {

    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 IsValidTime(strTime) {
	// Checks if time is in HH:MM:SS format.
	// The seconds are optional.

	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?$/;
	var matchArray = strTime.match(timePat);
	if (matchArray == null) {
		return false;
	}

	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];

	if (second=="") { second = null; }

	if (hour < 0  || hour > 23) {
		return false;
	}

	if (minute<0 || minute > 59) {
		return false;
	}

	if (second != null && (second < 0 || second > 59)) {
		return false;
	}
	return true;
}

function IsValidNumber(numval)
{
	if (numval==""){return false;}
	var myRegExp = new RegExp("^[/+|/-]?[0-9]*[/.]?[0-9]*$");
	return myRegExp.test(numval);
}

function IsValidPhone(numval)
{
	if (numval==""){return false;}
	var myRegExp = new RegExp("^[0-0]*[0-9]*[/-]?[0-9]*$");
	return myRegExp.test(numval);
}

function ReplaceDemo(){
   var r, re;                    //Declare variables.
   var ss = "- man hit - ball with the bat.\n";
   ss += "while - fielder caught - ball with the glove.";
   re = /-/g;             //Create regular expression pattern.
   alert(ss);
   r = ss.replace(re, "A");    //Replace "A" with "The".
   alert(r);
}

function IsValidPhoneNumber(numval)
{
	if (numval==""){return true;}

	var re;
	re = /-/g;
	numval = numval.replace(re, "");

	if (isNaN(numval))
		return false;
	else
		return true;
}

function IsValidDate(d) {
	//var strDatestyle = "US"; //United States date style
	var strDatestyle = "EU";  //European date style
	var strDateArray,strDay,strMonth,strYear,intDay,intMonth,intYear;
	var booFound = false;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var strDate= d;

	if (strDate.length < 1) { return false; }

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) { return false; }
			else {
				strDay = strDateArray[0]; strMonth = strDateArray[1]; strYear = strDateArray[2];
			}
			booFound = true;
		}
	}

	if (booFound == false) {
		if (strDate.length>5) {
			strDay = strDate.substr(0, 2); strMonth = strDate.substr(2, 2); strYear = strDate.substr(4);
		}
		else
			return false;
	}

	// verify year part 2 or 4 digits
	if (strYear.length != 2 && strYear.length != 4) { return false; }
	if (isNaN(strYear)){ return false; }

	// US style (swap month and day)
	if (strDatestyle == "US") {
		strTemp = strDay; strDay = strMonth; strMonth = strTemp;
	}

	// verify 1 or 2 digit integer day
	if (strDay.length<1 || strDay.length>2) { return false; }
	if (isNaN(strDay)){ return false; }

	// month may be digits of characters, hence following check
	intDay = parseInt(strDay,10);
	intMonth = parseInt(strMonth, 10);
	intYear = parseInt(strYear, 10);

	if (intMonth < 1 || 12 < intMonth) { return false; }
	if (intDay < 1   || 31 < intDay)   { return false; }

	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intDay > 30)) { return false; }

	if (intMonth == 2) {
		if (LeapYear(intYear)) {
			if (intDay > 29) { return false; }
		}
		else {
			if (intDay > 28) { return false; }
		}
	}

	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

function IsEarlierOrEqual(start,end) {

	if (true != IsValidDate(start))
		return false;
	if (true != IsValidDate(end))
		return false;

	var myStart = start;
	var myEnd = end;

	if (myStart=="" || myEnd=="") { return false; }

	var startparts= myStart.split("/");
	var endparts=myEnd.split("/");

	if (Date.UTC(startparts[2],startparts[1],startparts[0]) < Date.UTC(endparts[2],endparts[1],endparts[0]))
		return true;
	else
		return false;
}


