//var lastObj = "";
//var inProcess = false;	
var dateErr = "";
var MyDateAssemble = "";
	function pageSubmitDates(DatesArray, showError){
		dateErr = "";
		baddata = false;
		for (var i = 0; i < DatesArray.length; i++) {      
			MyTemp = DatesArray[i];
			
			visName = MyTemp[0];
			required = MyTemp[1];
			MyMonth = MyTemp[2]; 
			MyDay = MyTemp[3];
			MyYear = MyTemp[4];
			SendingObj = MyTemp[2]
			bErrorCodeArray = validateDATETIME(SendingObj, required, MyMonth, MyDay, MyYear, true, visName);
			ErrAction = triStateDate(bErrorCodeArray,SendingObj,required,MyMonth, MyDay, MyYear, true, visName);
			
			if(ErrAction !=1){
				baddata = true;
			}
		}
		if(baddata && dateErr != ""){
			if(showError){
				alert(dateErr);
			}else{
				
					globalErrMSG = globalErrMSG  + dateErr;
				
			}
			return false;
		}else{
			return true;
		}
	}
	
	//function inLineDate(sendingObject, required, month, day, year, visName){
	function inLineDate(sendingObj, MyDateArray){
		sendingObject = sendingObj
		required = MyDateArray[1]
		month = MyDateArray[2]
		day = MyDateArray[3]
		year = MyDateArray[4]
		visName = MyDateArray[0];
		
		var bErrorCode;
		dateErr = "";
		if(inProcess != true || sendingObject == lastObj){
			inProcess = true;
			lastObj = sendingObject;
			var bfullDate = false;	
			var bGoodDate = true;
			bErrorCodeArray = validateDATETIME(sendingObject, required, month, day, year, false, visName);
			ErrAction = triStateDate(bErrorCodeArray,sendingObject,required,month, day, year, false, visName);
			if(ErrAction ==0){
				alert(dateErr);
				sendingObject.focus();
			}else{
				inProcess = false;
			}
		}
	}
	

	function validateDATETIME(sendingObject, required, month, day, year, pageValidation, visName){	
			
			if (month.name == day.name && day.name == year.name && year.name== month.name)
			{
				MyDate = month.value;
				if(MyDate!=""){
					bfullDate = true;
					MyMonthValue = stripMonth(MyDate);
					MyDayValue =   stripDay(MyDate);
					MyYearValue =  stripYear(MyDate);
				}else{
					return ErrArray = new Array (-1,-1,-1);
				}
			}else{
				if(month.type == "text"){
					MyMonthValue = month.value;
					if(isNaN(MyMonthValue)){
						MyMonthValue = GetMonthValue(MyMonthValue);
					}
				}else{
					MyMonthValue = LoopThroughSelect(month, false);
					if(isNaN(MyMonthValue)){
						MyMonthValue = GetMonthValue(MyMonthValue);
					}
				}
				if(day.type == "text"){
					MyDayValue = day.value; 
				}else{
					MyDayValue = LoopThroughSelect(day, false);
				}
				if(year.type == "text"){
					MyYearValue = year.value; 
				}else{
					MyYearValue = LoopThroughSelect(year, false);
				}
			}

			ErrArray = new Array (validateMonth(MyMonthValue),validateDay(MyDayValue),validateYear(MyYearValue));
			MyDateAssemble = MyMonthValue+"/"+MyDayValue+"/"+MyYearValue;
			return ErrArray;
	}
	
	function triStateDate(ErrArray,sendingObject,required,month, day, year, pageValidation, visName){
		
		//return a erraction var, either 1 or 0 to stop the program
		MonthErrorCode =ErrArray[0];
		DayErrorCode = ErrArray[1];
		YearErrorCode = ErrArray[2];
		
		//for (var z = 0; z < ErrArray.length; z++) { 
		//	alert("err - "+ErrArray[z]);
		//}
		oneDate = false;
		tempDate = "";
		if(month.name == year.name){
			tempDate = month.value;
			oneDate = true;
		}
		if(MonthErrorCode== 1 && DayErrorCode==  1 && YearErrorCode==1){ //good data
			// validate good date
			strDate = MyDateAssemble
			if(validatedate(strDate, true)){
				return 1;
			}else{
				dateErr = dateErr + visName +" requires a valid date.  Please enter a valid day.\n";
				return 0;
			}
						
			}else if(MonthErrorCode ==0){
				dateErr = dateErr + visName +" requires a valid date.  Please enter a valid month.\n";
				return 0;
			}else if(DayErrorCode ==0){
				dateErr = dateErr + visName +" requires a valid date.  Please enter a valid day.\n";
				return 0;
			}else if(YearErrorCode == 0){
				dateErr = dateErr + visName +" requires a valid date.  Please enter a valid year.\n";
				return 0;
			}else{
				//deal with null - do nothing untill page validation
				if(MonthErrorCode== -1 && DayErrorCode== -1 && YearErrorCode==-1 ){ // all null
					if (required){ 
						dateErr = dateErr + visName +" requires a valid date.  Please complete all the information.\n";
						return 0;
					}else{
						if(tempDate != ""){
							if(oneDate){
								dateErr = dateErr + visName +"  requires a valid date.  Please enter a valid date.\n";
								return 0;
							}else{
								return -1;
							}
						}else{
							return -1;
						}
					}
					return -1;
				}else if(MonthErrorCode== -1 && DayErrorCode== 1 && YearErrorCode== 1){// null month
					dateErr = dateErr + visName +"  requires a valid date.  Please complete all the information.\n";
					return 0;
				}else if(MonthErrorCode== 1 && DayErrorCode== -1 && YearErrorCode== 1){// null day
					dateErr = dateErr + visName +"  requires a valid date.  Please complete all the information.\n";
					return 0;
				}else if(MonthErrorCode== 1 && DayErrorCode== 1 && YearErrorCode== -1){// null year
					dateErr = dateErr + visName +"  requires a valid date.  Please complete all the information.\n";
					return 0;
				}else if(MonthErrorCode== -1 && DayErrorCode== 1 && YearErrorCode== -1){// null month/year
					dateErr = dateErr + visName +"  requires a valid date.  Please complete all the information.\n";
					return 0;
				}else if(MonthErrorCode== -1 && DayErrorCode== -1 && YearErrorCode== 1){// month/day null
					dateErr = dateErr + visName +"  requires a valid date.  Please complete all the information.\n";
					return 0;
				}else if(MonthErrorCode== 1 && DayErrorCode== -1 && YearErrorCode== -1){// day/year null
					dateErr = dateErr + visName +"  requires a valid date.  Please complete all the information.\n";
					return 0
				}else{
					dateErr = dateErr + visName +" occured and error while processing.  Please re-enter the data again.\n";
					return 0;
				}
			}
		}
//##################################################################################################################	

	function validatingDate(MyDate){
	//the date comes in pre assembled ex. 12/12/2000 or 12/12/00 and this function will return a 
	// three digit number.  The reason for 3 digits is that the first digit will represent the month,
	// the second digit represents the day and the third digit represents the year.  In representing 
	// the date type that they may they return a 1 for that place if the value is  good or they return
	// a 0 or null if the value is bad.  ex.  111 all values are good, 100 the only value that is good is the month
	// 1 the only value that is good is the year..... and so on........
	
		var goodDate;
		goodDate = 111;
		if(MyDate.indexOf("/") != MyDate.lastIndexOf("/"))
		{
			if (MyDate.indexOf("/") > 2 || MyDate.indexOf("/") < 1){
				goodDate = goodDate - 100;
			}else{
				MyMonth = MyDate.slice(0,MyDate.indexOf("/"))
				isValidMonth = validateMonth(MyMonth);
				if(isValidMonth){
				
				}else{
					goodDate = goodDate - 100;
				}
			}
			
			if ((MyDate.indexOf("/") > 2 || MyDate.indexOf("/") < 1)||(MyDate.lastIndexOf("/") > 5 || MyDate.lastIndexOf("/") < 3)){
				goodDate = goodDate - 10;
			}else{
				MyDay = MyDate.slice(MyDate.indexOf("/")+1,MyDate.lastIndexOf("/"))
				isValidDay = validateDay(MyDay)
				if(isValidDay){
				
				}else{
					goodDate = goodDate - 10;
				}
			}
			MyYear = MyDate.slice(MyDate.lastIndexOf("/")+1,MyDate.length)
			isValidYear = validateYear(MyYear);
			if(isValidYear){
				
			}else{
				goodDate = goodDate - 1;
			}
		}else{
			goodDate = 0;
		}
		return goodDate;
	}
	
//##################################################################################################################	

	function validateMonth(MyMonth){
	// returns true or false depending if a valid number is entered in the month section
	// MyMonth = a number indicating a month ex. January = 1, December = 12.
		if(eval(MyMonth.length == 0 )){
			return -1;
		}else if(eval(isNaN(MyMonth)))
		{
			return 0;
		}else{
			if (eval((MyMonth > 12 || MyMonth < 1)||(MyMonth.length > 2 || MyMonth.length <1)))
			{
				return 0;
			}else{
				return 1;
			}
		}
	}
	
//##################################################################################################################	

	function validateDay(MyDay){
	// returns true or false depending if a valid number is entered in the day section
	// MyDay = a number indicating a day valid days are 1 to 31.
		while (MyDay.indexOf(" ") > -1) {
			strLoc = MyDay.indexOf(" ");
			temp1 = MyDay.slice(0,strLoc);
			temp2 = MyDay.slice(strLoc + 1,MyDay.length);
			MyDay = temp1 + temp2;
			}
		while (MyDay.indexOf(",") > -1) {
			strLoc = MyDay.indexOf(",");
			temp1 = MyDay.slice(0,strLoc);
			temp2 = MyDay.slice(strLoc + 1,MyDay.length);
			MyDay = temp1 + temp2;
			}
		if(MyDay.length == 0 ){
			return -1;
		}else if(isNaN(MyDay))
		{
			return 0;
		}else{
			if ((MyDay > 31 || MyDay < 1)||(MyDay.length > 2 || MyDay.length <1))
			{
				return 0;
			}else{
				return 1;
			}
		}
	}
	
//##################################################################################################################	

	function validateYear(MyYear){
	// returns true or false depending if a valid number is entered in the year section
	// MyYear = a number indicating a year.  Valid years are between 00 and 99 or 1000 and 9999
		while (MyYear.indexOf(" ") > -1) {
			strLoc = MyYear.indexOf(" ");
			temp1 = MyYear.slice(0,strLoc);
			temp2 = MyYear.slice(strLoc + 1,MyYear.length);
			MyYear = temp1 + temp2;
			}
			
		if(MyYear.length == 0 ){
			return -1;
		}else if(isNaN(MyYear))
		{
			return 0;
		}else{
			if (MyYear.length == 4 || MyYear.length == 2)
			{  
				if ((MyYear > 99 && MyYear < 1000) || MyYear > 9999 || MyYear < 0)  
				{
					return 0;
				}else{
					return 1;
				}
			}else{
				return 0;
			}
		}
	}
	
//##################################################################################################################	
	
	function validatedate(strDate, except2digitYears)
	{
	//strDate = is the string value of the date, ex 12/12/00 or 12/12/2000
	//except2digitYears = a boolean value if the field will accept a 2 digit year or not.
	
		var blnGoodDate;
		var lngPos;
		var strMonth;
		var strDay;
		var strYear;
		var intDaysInMonth = 0;

		blnGoodDate = false;
		if(((strDate != "mm/dd/yyyy")||(strDate != "mm/dd/yy")) && (strDate != ""))
		{
			lngPos = strDate.split("/");
			if(lngPos.length < 3)
				return false;
			else
			{
				strMonth = lngPos[0];
				strDay = lngPos[1];
				strYear = lngPos[2];
			}
			
			if(strMonth > 0 && strMonth <= 12)
			{
				if (except2digitYears){
					if (strYear >= 0 && strYear < 49){
						strYear = parseInt(strYear) + 2000; 
					}else if(strYear > 50 && strYear < 100){
						strYear = parseInt(strYear) + 1900;
					}
				}
				if(strYear > 1899 && strYear < 99999)
				{
					//alert(strMonth+" in funct");
					if(strMonth == 2)
					{
						if (((strYear % 4 == 0) && strMonth == 2 && (strYear % 100 != 0)) || (strYear % 400 == 0)) 
							intDaysInMonth = 29;
						else
							intDaysInMonth = 28;
					}
					else if(strMonth % 2 == 0)
					{
						if(strMonth < 8)
							intDaysInMonth = 30;
						else
							intDaysInMonth = 31;
					}
					else
					{
						if(strMonth < 8)
							intDaysInMonth = 31;
						else
							intDaysInMonth = 30;
					}

					if(strDay > 0 && strDay <= intDaysInMonth)
						blnGoodDate = true;
				}
			}

			if(blnGoodDate == false)
				return false;
			else
				return true;
		}
		else
			return false;
	}
//##################################################################################################################	
	function GetMonthValue(MyMonth){
		while (MyMonth.indexOf(".") > -1) {
			strLoc = MyMonth.indexOf(".");
			temp1 = MyMonth.slice(0,strLoc);
			temp2 = MyMonth.slice(strLoc + 1,MyMonth.length);
			MyMonth = temp1 + temp2;
		}
		switch(MyMonth.toUpperCase())
		{
			case "JANUARY":
				return 1;
				break;
			case "JAN":
				return 1;
				break;
			case "FEBRUARY":
				return 2;
				break;
			case "FEB":
				return 2;
				break;
			case "MARCH":
				return 3;
				break;
			case "MAR":
				return 3;
				break;
			case "APRIL":
				return 4;
				break;
			case "MAY":
				return 5;
				break;
			case "JUNE":
				return 6;
				break;
			case "JULY":
				return 7;
				break;
			case "AUGUST":
				return 8;
				break;
			case "AUG":
				return 8;
				break;
			case "SEPTEMBER":
				return 9;
				break;
			case "SEPT":
				return 9;
				break;
			case "OCTOBER":
				return 10;
				break;
			case "OCT":
				return 10;
				break;
			case "NOVEMBER":
				return 11;
				break;
			case "NOV":
				return 11;
				break;
			case "DECEMBER":
				return 12;
				break;
			case "DEC":
				return 12;
				break;
			default :
				return 13;
				break;
		}
	}

//##################################################################################################################
	
	function LoopThroughSelect(MyObject, MultiSelect){
		temp = "";
		for(i=0;i < MyObject.options.length ; i++){
			if(MyObject.options[i].selected){
				temp = MyObject.options[i].value;
				if(MultiSelect == false){
					break;
				}
			}
		}
		return temp;	
	}

//##################################################################################################################
	
	function stripMonth(MyDate){
		//Delimiters "," " " "-" "/" 
		// look for the first space, / , -, or comma
		var MyDelimiter = "/";
		
		if(MyDate.indexOf("-") != -1 && MyDate.indexOf("-") != MyDate.lastIndexOf("-")){
			MyDelimiter = "-";
		}else if(MyDate.indexOf("/") != -1 && MyDate.indexOf("/") != MyDate.lastIndexOf("/")){
			MyDelimiter = "/";
		}else if(MyDate.indexOf(",") != -1 && MyDate.indexOf(",") != MyDate.lastIndexOf(",")){
			MyDelimiter = ",";
		}else if(MyDate.indexOf(" ") != -1){
			MyDelimiter = " ";
		//}else{
				//MyDelimiter = "";
		}
		
		if(MyDate.indexOf(MyDelimiter) != MyDate.lastIndexOf(MyDelimiter) || MyDate.indexOf(MyDelimiter) != -1)
		{
			MyMonth = MyDate.slice(0,MyDate.indexOf(MyDelimiter));
			if(isNaN(MyMonth)){
				MyMonth = GetMonthValue(MyMonth);
			}
			return MyMonth; //as a number
		}else{
			
			return MyDate;
		}
	}
	
//##################################################################################################################	

	function stripDay(MyDate){
		//Delimiters "," " " "-" "/" 
		// look for the first space, / , -, or comma
		var MyDelimiter = "/";
		
		if(MyDate.indexOf("-") != -1 && MyDate.indexOf("-") != MyDate.lastIndexOf("-")){
			MyDelimiter = "-";
		}else if(MyDate.indexOf("/") != -1 && MyDate.indexOf("/") != MyDate.lastIndexOf("/")){
			MyDelimiter = "/";
		}else if(MyDate.indexOf(",") != -1 && MyDate.indexOf(",") != MyDate.lastIndexOf(",")){
			MyDelimiter = ",";
		}else if(MyDate.indexOf(" ") != -1){
			MyDelimiter = " ";
		}else{
				MyDelimiter = "";
		}
		
		if(MyDate.indexOf(MyDelimiter) != MyDate.lastIndexOf(MyDelimiter) && MyDate.indexOf(MyDelimiter) != -1)
		{
			MyDay = MyDate.slice(MyDate.indexOf(MyDelimiter)+1, MyDate.lastIndexOf(MyDelimiter));
		}else{
			MyDay = MyDate.slice(MyDate.indexOf(" ")+1, MyDate.lastIndexOf(" "));
		}	
		return MyDay;
	}
	
//##################################################################################################################
	
	function stripYear(MyDate){
		//Delimiters "," " " "-" "/" 
		// look for the first space, / , -, or comma
		
		if(MyDate.indexOf("-") != -1 && MyDate.indexOf("-") != MyDate.lastIndexOf("-")){
			MyDelimiter = "-";
		}else if(MyDate.indexOf("/") != -1 && MyDate.indexOf("/") != MyDate.lastIndexOf("/")){
			MyDelimiter = "/";
		}else if(MyDate.indexOf(",") != -1 && MyDate.indexOf(",") != MyDate.lastIndexOf(",")){
			MyDelimiter = ",";
		}else if(MyDate.indexOf(" ") != -1){
			MyDelimiter = " ";
		}else{
				MyDelimiter = "";
		}
		
		if (MyDate.lastIndexOf(MyDelimiter) != -1){
			MyYear = MyDate.slice(MyDate.lastIndexOf(MyDelimiter)+1,MyDate.length);
		}else{
			MyYear = MyDate.slice(MyDate.lastIndexOf(MyDelimiter)+1,MyDate.length);
		}
		return MyYear;
	}