// The following functions were written by Tom Wittbrodt
// Copyright (c) 1998, 1999 Tom Wittbrodt
// License is granted if and only if this entire
// copyright notice is included.

function check_year_validity(d){
  if(d.getFullYear() < 1970 || d.getFullYear() > 2100){
    return false;
  }
  return true;
}
function isEmpty(Field){
  if(Field != null){
    Field.value = trim(Field.value);
    if(Field.value == "")
      return true;
  }
  else return true;
}

function trim ( inputStringTrim ) {
  fixedTrim = "";
  lastCh = " ";
  for (x=0; x < inputStringTrim.length; x++) {
    ch = inputStringTrim.charAt(x);
    if ((ch != " ") || (lastCh != " ")) {
      fixedTrim += ch;
    }
    lastCh = ch;
  }
  if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
    fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1); }

  return fixedTrim;
}

function validate_date(date_field) {
        var d_backUp = date_field.value;  
        if (!date_field.value)
                return true;
        var in_date = stripCharString(date_field.value," ");
        in_date = in_date.toUpperCase();
        var date_is_bad = 0;

        if (!allowInString(in_date,"/0123456789T+-"))
                date_is_bad = 1; // invalid characters in date
        if (!date_is_bad) {
                var has_rdi = 0;
                if (in_date.indexOf("T") >= 0){
                        has_rdi = 1;
                }
                if (!date_is_bad && has_rdi && (in_date.indexOf("T") != 0)) {
                        date_is_bad = 2; // relative date index character is not in first position
                }
                if (!date_is_bad && has_rdi && (in_date.length == 1)) {
                        var d = new Date();
						var return_month = parseInt(d.getMonth() + 1).toString();
						return_month = (return_month.length==1 ? "0" : "") + return_month;
						var return_date =  parseInt(d.getDate()).toString();
						return_date = (return_date.length==1 ? "0" : "") + return_date;
				        in_date = return_month + "/" + return_date + "/" + get_full_year(d);
                        has_rdi = 0; // date doesn't have rdi char anymore (will also cause failure of add'l rdi checks, which is a good thing)
                }
                if (!date_is_bad && has_rdi && (in_date.length > 1) && !(in_date.charAt(1) == "+" || in_date.charAt(1) == "-")) {
                        date_is_bad = 3; // length of rdi string is greater than 1 but second char is not "+" or "-"
                }
                if (!date_is_bad && has_rdi && isNaN(parseInt(in_date.substring(2,in_date.length),10))) {
                        date_is_bad = 4; // rdi value is not a number
                }
                if (!date_is_bad && has_rdi && (parseInt(in_date.substring(2,in_date.length),10) < 0)) {
                        date_is_bad = 5; // rdi value is not a positive integer
                }
                if (!date_is_bad && has_rdi) {
                        var d = new Date();
                        ms = d.getTime();
                        offset = parseInt(in_date.substring(2,in_date.length),10);
                        if(in_date.charAt(1) == "+") {
                                ms += (86400000 * offset);
                        } else {
                                ms -= (86400000 * offset);
                        }
                        d.setTime(ms);
						var return_month = parseInt(d.getMonth() + 1).toString();
						return_month = (return_month.length==1 ? "0" : "") + return_month;
						var return_date =  parseInt(d.getDate()).toString();
						return_date = (return_date.length==1 ? "0" : "") + return_date;
				        in_date = return_month + "/" + return_date + "/" + get_full_year(d);
                        has_rdi = 0;
                }
        }
        if (!date_is_bad) {
                var date_pieces = new Array();
                date_pieces = in_date.split("/");
                if (date_pieces.length == 2) {
                        var d = new Date();
                        in_date = in_date + "/" + get_full_year(d);
                        date_pieces = in_date.split("/");
                }
                if (date_pieces.length != 3 || parseInt(date_pieces[0],10) < 1 || parseInt(date_pieces[0],10) > 12
                                || parseInt(date_pieces[1],10) < 1 || parseInt(date_pieces[1],10) > 31
                                || (date_pieces[2].length != 2 && date_pieces[2].length != 4)) {
                        date_is_bad = 6;  // date is not in format of m[m]/d[d]/yy[yy]
                }
        }
        if (date_is_bad) {
      //          alert(desc + " must be in the format of mm/dd/yy, mm/dd/yyyy!");
                return (false);
        }

        var ms = Date.parse(in_date);
        var d = new Date();
        d.setTime(ms);
		var return_date = d.toLocaleString();
		var return_month = parseInt(d.getMonth() + 1).toString();
		return_month = (return_month.length==1 ? "0" : "") + return_month;
		var return_date =  parseInt(d.getDate()).toString();
		return_date = (return_date.length==1 ? "0" : "") + return_date;
        return_date = return_month + "/" + return_date + "/" + get_full_year(d);
        if(check_year_validity(d)){
          date_field.value = return_date;
        }else{
          date_field.value = d_backUp;
          return false;
        }
        return true;
}

// normalize the year to yyyy OLD Working code
function get_full_year(d) {
		var y = ""
		if (d.getFullYear() != null)
		{
		  y = d.getFullYear();
		} else
		{
	          y = d.getYear();
	          if (y > 69  && y < 100) y += 2000;
	          if (y < 1000) y += 2000;
		}
        return y;
}

/* normalize the year to yyyy OLD Working code
function get_full_year(d) {
		var y = ""
		if (d.getFullYear() != null)
		{
			y = d.getFullYear();
			if (y < 1970) y+= 100;
		} else
		{
	        y = d.getYear();
	        if (y > 69  && y < 100) y += 1900;
	        if (y < 1000) y += 2000;
		}
        return y;
}
*/

//normalize the year to yyyy
function get_full_year(d) {
		var y = ""
		if (d.getFullYear() != null)
		{
			y = d.getFullYear();
			if (y < 1970) y+= 100;
		} else
		{
	        y = d.getYear();
	        if (y > 69  && y < 100) y += 1900;
	        if (y < 1000) y += 2000;
		}
        return y;
}

// The following functions were written by Gordon McComb
// More information can be found here: http://www.javaworld.com/javaworld/jw-02-1997/jw-02-javascript.html
function stripCharString (InString, CharString)  {
        var OutString="";
   for (var Count=0; Count < InString.length; Count++)  {
        var TempChar=InString.substring (Count, Count+1);
      var Strip = false;
      for (var Countx = 0; Countx < CharString.length; Countx++) {
        var StripThis = CharString.substring(Countx, Countx+1)
         if (TempChar == StripThis) {
                Strip = true;
            break;
         }
      }
      if (!Strip)
        OutString=OutString+TempChar;
   }
        return (OutString);
}
function allowInString (InString, RefString)  {
        if(InString.length==0) return (false);
        for (var Count=0; Count < InString.length; Count++)  {
        var TempChar= InString.substring (Count, Count+1);
      if (RefString.indexOf (TempChar, 0)==-1)
        return (false);
   }
   return (true);
}

/*
-----------------------------------------
|     By Mattias Sjöberg 28/11-96       |
|You're welcome to use/edit this script.|
| Keep the comments and drop me a note. |
-----------------------------------------
|      mattias.sjoberg@swipnet.se       |
| www.geocities.com/SiliconValley/7116  |
|     Visit  The JavaScript Planet      |
-----------------------------------------
*/

function checkdate(date_field){
//	window.onerror=null // for all other strange errors
	var err=0
	var psj=0;
	a=date_field.value
	if (a.length != 8) err=1
	b = a.substring(0, 2)// month
	c = a.substring(2, 3)// '/'
	d = a.substring(3, 5)// day
	e = a.substring(5, 6)// '/'
	f = a.substring(6, 8)// year

	//basic error checking
	if (b<1 || b>12) err = 1
	if (c != '/') err = 1
	if (d<1 || d>31) err = 1
	if (e != '/') err = 1
	if (f<0 || f>99) err = 1

	//advanced error checking

	// months with 30 days
	if (b==4 || b==6 || b==9 || b==11){
		if (d==31) err=1
	}

	// february, leap year
	if (b==2){
		// feb
		var g=parseInt(f/4)
		if (isNaN(g)) {
			err=1
		}

		if (d>29) err=1
		if (d==29 && ((f/4)!=parseInt(f/4))) err=1
	}

	if (err==1){
		return false;
	}
	else{
		return true;
	}

}

function SetFocus(object)
{
	object.focus();
}

function CompareDates(d1, d2)
{
 if (d1=="") d1=new Date(0);
 if (d2=="") d2=new Date(0);
 return new Date(d1)- new Date(d2);
}

function isDateBetween(testdate,d1,d2) {
   if (CompareDates(testdate,d1)<0) return false;
   if (CompareDates(testdate,d2)>0) return false;
    
   return true;       
}
   
function CheckDateField(elem, required) {
  if (isEmpty(elem) && required)
  {
    return 0; // missing
  }
 else
 {
   if (!validate_date(elem))
   {
     return -1; // invalid
  }
 }

 return 1; // OK
}


