
function validateEmpty(theObject, theMessage)
{
theObject.value=trim(theObject.value);
if (theObject.value=="")
   {
   alert(theMessage);
   theObject.focus();
   return false;
   }
return true;
}
 
//------------------------------------------------------------------------
function formatDate(theDate)
{
	var theMonth=theDate.getMonth()+1;
		if(theMonth<10)theMonth="0"+theMonth;
	var theDay=theDate.getDay();
		if(theDay<10)theDay="0"+theDay;
	var theYear=theDate.getYear();
	return theMonth+"/"+theDay+"/"+theYear;
}
//---------------------------Time Validation
function IsValidTime(timeStr) {
	// Checks if time is in HH:MM:SS AM/PM format.
	// The seconds and AM/PM are optional.
	//var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var timePat = /^(\d{1,2})(\d{2})?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) {
	alert("Time is not in a valid format.");
	return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	
	//if (second=="") { second = null; }
	//if (ampm=="") { ampm = null }
	
	if (hour < 0  || hour > 23) {
	alert("Hour must be between 0 and 23 for military time");
	return false;
	}
	
	//}
	if  (hour > 12 && ampm != null) {
	alert("You can't specify AM or PM for military time.");
	return false;
	}
	if (minute<0 || minute > 59) {
	alert ("Minute must be between 0 and 59.");
	return false;
	}
	if (second != null && (second < 0 || second > 59)) {
	alert ("Second must be between 0 and 59.");
	return false;
	}
	return true;
}
//----------------------------------------------------------------------

function phoneValidation(phone) {
	//var dash= "-";
	//var matchArr =  phone.match(/^(\d{3})+dash(\d{3)}+dash(\d{4})$/);
	var matchArr =  phone.match(/^(\d{3})-\d{3}-\d{4}$/);
	if (matchArr == null) {
	   alert('Invalid Phone Number. Must be 10 digits in the format ###-###-####.');
	   }
}

//------------------------Get Weekday------------------------
function calculateWeekday(){
	var theElement = document.frm.text1.value
	var days = new Array(8);
	days[1] = "Sunday";
	days[2] = "Monday";
	days[3] = "Tuesday";
	days[4] = "Wednesday";
	days[5] = "Thursday";
	days[6] = "Friday";
	days[7] = "Saturday";
	var dateObj = new Date(theElement)
	var wday = days[dateObj.getDay() + 1]
	document.write(wday)
}
 
function makeWin(url, windowName, p_Width, p_Height) {
// windowName must not contain any spaces
  agent = navigator.userAgent;
  windowName = windowName

  params  = "";
  params += "toolbar=0,";
  params += "location=0,";
  params += "directories=0,";
  params += "status=0,";
  params += "menubar=0,";
  params += "scrollbars=1,";
  params += "resizable=1,";
  if (p_Width == null) {
	params += "width=600,";
	params += "height=500";
  }
  else {
	params += "width=" + p_Width + ",";
	params += "height="+ p_Height;
  }

	//center popup
	var leftprop, topprop, screenX, screenY, cursorX, cursorY, padAmt;
	if(navigator.appName == "Microsoft Internet Explorer") {
	//screenY = document.body.offsetHeight;
	screenY = window.screen.availHeight;
	screenX = window.screen.availWidth;
	}
	else {
	screenY = window.outerHeight
	screenX = window.outerWidth
	}
	leftvar = (screenX - p_Width) / 2;
	rightvar = (screenY - p_Height) / 2;
	if(navigator.appName == "Microsoft Internet Explorer") {
		leftprop = leftvar;
		topprop = rightvar;
	}
	else {
		leftprop = (leftvar - screenX);
		topprop = (rightvar - screenY);
	}
	params = params + ", left = " + leftprop;
	params = params + ", top = " + topprop;

  // close the window to vary the window size
  if (typeof(win) == "object" && !win.closed){
	win.close();
  }

  win = window.open(url, windowName , params);

  if (agent.indexOf("Mozilla/2") != -1 && agent.indexOf("Win") == -1) {
	  win = window.open(url, windowName , params);
  }

  if (!win.opener) {
	  win.opener = window;
  }

  // bring the window to the front
  win.focus();	
}
	  
	  
	  
function checkdate(objName) {
  var datefield = objName;
  if (datefield.value == "" ) return true;
  if (isDate(datefield)==false){
	  	//alert("The date format should be : mm/dd/yyyy")
		datefield.focus();
		return false
	}
    return true
    
}
function stripCharString (InString, CharString)  {
	OutString="";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring (Count, Count+1);
		Strip = false;
		for (Countx = 0; Countx < CharString.length; Countx++) {
			StripThis = CharString.substring(Countx, Countx+1)
			if (TempChar == StripThis) {
				Strip = true;
				break;
			}
		}
		if (!Strip)
			OutString=OutString+TempChar;
	}
	return (OutString);
}



function trim(item)
{
  var tmp = "";
  var item_length = item.value.length;
  var item_length_minus_1 = item.value.length - 1;
  for (index = 0; index < item_length; index++)
  {
    if (item.value.charAt(index) != ' ')
    {
      tmp += item.value.charAt(index);
    }
    else
    {
      if (tmp.length > 0)
      {
        if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1)
        {
          tmp += item.value.charAt(index);
        }
      }
    }
  }
  //return tmp;
  item.value = tmp;
}

function TrimBlank(item)
{
  var tmp = "";
  var item_length = item.value.length;
  var item_length_minus_1 = item.value.length - 1;
  for (index = 0; index < item_length; index++)
  {
    if (item.value.charAt(index) != ' ')
    {
      tmp += item.value.charAt(index);
    }
    else
    {
      if (tmp.length > 0)
      {
        if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1)
        {
          tmp += item.value.charAt(index);
        }
      }
    }
  }
  
  item.value = tmp;
}


function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = 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
}
function checkdateSilence(objName) {
  var datefield = objName;
  if (datefield.value == "" ) return true;
  if (isDateSilence(datefield)==false){
	 	datefield.focus();
		return false
	}
    return true
    
}
function isDateSilence(objName){
	// Reformat date
	
	var dtStr = objName.value;	
	dtStr = replace(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)
	}
	*/	
	//Reformat Year.
	var strDatestyle = "US";
	if (strYear.length == 2 ) {
		var mToday = new Date();
		//If the year is greater than 30 years from now use 19, otherwise use 20
		var checkYear = mToday.getFullYear() + 30; 
		var mCheckYear = '20' + strYear;
		if (mCheckYear >= checkYear)
			strYear = '19' + strYear;		
		else 		
			strYear = '20' + strYear;				
		
	}else if (strYear.length == 1)
	{
		strYear = '200' + strYear;	
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYear)
	//year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1){
		
		return false
	}
	else if (strMonth.length<1 || month<1 || month>12){
		
		return false
	}
	else if  (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		
		return false
	}
	else if  (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		
		return false
	}
	else if  (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		
		return false
	}
	if (strDatestyle == "US") {
	objName.value = strMonth + "/" + strDay + "/" + strYear;
	}

return true
}

function isDate(objName){
	// Reformat date
	
	var dtStr = objName.value;	
	dtStr = replace(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)
	}
	*/	
	//Reformat Year.
	var strDatestyle = "US";
	if (strYear.length == 2 ) {
		var mToday = new Date();
		//If the year is greater than 30 years from now use 19, otherwise use 20
		var checkYear = mToday.getFullYear() + 30; 
		var mCheckYear = '20' + strYear;
		if (mCheckYear >= checkYear)
			strYear = '19' + strYear;		
		else 		
			strYear = '20' + strYear;				
		
	}else if (strYear.length == 1)
	{
		strYear = '200' + strYear;	
	}
	
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYear)
	//year=parseInt(strYr)
	
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	else if  (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	else if  (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	else if  (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	else if  (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	if (strDatestyle == "US") {
	objName.value = strMonth + "/" + strDay + "/" + strYear;
	}

return true
}

function replace(string, text, by) 
{
	// Replaces text with by in string
    	var strLength = string.length, txtLength = text.length;
    	if ((strLength == 0) || (txtLength == 0)) return string;

    	var i = string.indexOf(text);
    	if ((!i) && (text != string.substring(0,txtLength))) return string;
    	if (i == -1) return string;

    	var newstr = string.substring(0,i) + by;
    	if (i+txtLength < strLength)
        	newstr += replace(string.substring(i+txtLength,strLength),text,by);

    	return newstr;
}
function kp_numericsign() {
	if ((event.keyCode != 46 && event.keyCode != 45) && (event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
		
}
function kp_integer() {
	if ((event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
}
function kp_numeric() {
	if ((event.keyCode != 46) && (event.keyCode < 48 || event.keyCode > 57))
		event.returnValue = false;
	if (event.keyCode == 46) {
		if (event.srcElement.value.indexOf(".") > -1)
			event.returnValue = false;
	}
}


function kp_character() {
	if ((event.keyCode < 65 || event.keyCode > 90) && (event.keyCode < 97 || event.keyCode > 122))
		event.returnValue = false;
}
function kp_convert_upper() {
	if ((event.keyCode >= 97 && event.keyCode <= 122))
		event.keyCode -= 32;
}
function kp_convert_lower() {
	if ((event.keyCode >= 65 && event.keyCode <= 90))
		event.keyCode += 32;
}

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}
