/* JavaScript Document
10/19/2010 gjoh added makeDatePlusVal
*/
function valDateFmt(datefmt) {
	myOption = -1;
	for (i=0; i<datefmt.length; i++) {
		if (datefmt[i].checked) {myOption = i;}
	}
	if (myOption == -1) {alert("You must select a date format");return ' ';}
	return datefmt[myOption].value;
}
function valDateRng(daterng) {
	myOption = -1;
	for (i=0; i<daterng.length; i++) {
		if (daterng[i].checked) myOption = i;
	}
	if (myOption == -1) {
		alert("You must select a date range");
		return ' ';
	}
	return daterng[myOption].value;
}
function stripBlanks(fld) {
	var result = "";
	var c=0;
	for (i=0; i<fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") c = result.length;
		}
	}
	return result.substr(0,c);
}
var numb = '0123456789';
function isValid(parm,val) {
	if (parm == "") return true;
	for (i=0; i<parm.length; i++) {
		if (val.indexOf(parm.charAt(i),0) == -1) return false;
	}
	return true;
}
function isNumber(parm) {return isValid(parm,numb);}
var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var Mth = new Array(' ','January','February','March','April','May','June','July','August','September','October','November','December');
var Mo = new Array(' ','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var aryDateErrors=["Only blanks were supplied",//-1,
	'Invalid date format. Two separators were not found ("/", ".", "-"), or there were spaces between month, day and year',//-2
	"Program error: required value not supplied for format ('W', 'J' or 'U')",//-3
	"Day of month could not be resolved as a number",//-4
	"Month could not be resolved as a number",//-5
	"Year could not be resolved as a number",//-6
	"Year is outside the range of 1582 to 4881",//-7
	"Month is not between 1 and 12",//-8
	"Day of month is not valid for the month",//-9
	"Your provided date for this field must be but is not in the past",//-10
	"Your provided date for this field must be but is not in the future",
	"Program error: required value not supplied for range ('P', 'A' or 'F')"];//-12
function validateDate(fld,fmt,rng,rtnType) {
	/*fld is date string value to be validated;
		fmt: "J"=ymd, "W"=dmy, "U"=mdy;
		rng: "P"=past "A"=any "F"=future
		rtnType: 
			"ymd" (default) returns date in yyyymmdd; (this is default to go w/ already coded chkDts();
			"dateObject" returns the date object; 
			"MDY" returns zero-padded mm/dd/yyyy; 
			"mdy" returns unpadded m/d/yyyy; 
			"Mmdy" returns unabbreviated init-cap Mo's (September 1, 1999
			"Mody" returns abbreviated init-cap Mo's (Dec 1, 1999)
	*/
	var dd, mm, yy, ret;
	var today = new Date;
	var t = new Date;
	fld = stripBlanks(fld);
	if (fld == '') return -1;
	if(rtnType=="undefined") rtnType = "ymd";
	var sepAry=[" ","\/",".","-"];
	for(var i=0;i<4;i++){
		var d1 = fld.split(sepAry[i]);
		if (d1.length == 3) break;
	}
	if (i==4) return -2;
	if (fmt == 'u' || fmt == 'U') {
	  dd = d1[1]; mm = d1[0]; yy = d1[2];
	}else if (fmt == 'j' || fmt == 'J') {
	  dd = d1[2]; mm = d1[1]; yy = d1[0];
	}else if (fmt == 'w' || fmt == 'W'){
	  dd = d1[0]; mm = d1[1]; yy = d1[2];
	}else return -3;
	var n = dd.lastIndexOf('st');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('nd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('rd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('th');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf(',');
	if (n > -1) dd = dd.substr(0,n);
	n = mm.lastIndexOf(',');
	if (n > -1) mm = mm.substr(0,n);
	if (!isNumber(dd)) return -4;
	if (!isNumber(yy)) return -5;
	if (!isNumber(mm)) {
	  var nn = mm.toLowerCase();
	  for (var i=1; i < 13; i++) {
			if (nn == mth[i] ||
   		nn == mth[i].substr(0,3)) {mm = i; i = 13;}
		}
	}
	if (!isNumber(mm)) return -6;
	dd = parseFloat(dd); 
	mm = parseFloat(mm); 
	yy = parseFloat(yy);
	if (yy < 100) yy += 2000;
	if (yy > today.getFullYear ()+60) yy-=100;//sliding window 60 future, 40 past
	if (yy < 1582 || yy > 4881) return -7;
	if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) day[mm-1]++;
	if (mm < 1 || mm > 12) return -8;
	if (dd < 1 || dd > day[mm-1]) return -9;
	t.setDate(dd); t.setMonth(mm-1); t.setFullYear(yy);
	if (rng == 'p' || rng == 'P') {
		if (t > today) return -10;
	}
	else if (rng == 'f' || rng == 'F') {
		if (t < today) return -11;
	}// else if (rng != 'a' && rng != 'A') return -12;
	if(rtnType  == "ymd") return (yy*10000)+(mm*100)+dd;
	else if(rtnType  == "mdy") return mm+"/"+dd+"/"+yyyy;// unpadded m/d/yyyy; 
	else if(rtnType  == "MDY") return leftPad0(mm)+"/"+leftPad0(dd)+"/"+yyyy;// zero-padded mm/dd/yyyy; 
	else if(rtnType  == "Mody") return Mo[mm]+" "+dd+", "+yy;//abbreviated init-cap Mo's (Dec 1, 1999)
	else if(rtnType  == "Mmdy") return Mth[mm]+" "+dd+", "+yy;//unabbreviated init-cap Mo's (September 1, 1999)
	else if(rtnType  == "dateObject") return t;//the date object
}
function leftPad0(inNum){
	if(inNum<10) return "0"+inNum;
	else return ""+inNum;
}
function chkDts(f1,f2,alrt,js,verbose){
	/* f1 and f2 can be either the ids of 2 fields with expected m/d/y format and any day; or array of field id, fmt (UWJ), rng (PAF) -- see above
	alrt if 1/true or not supplied, this function will display alert and return true or false; 
			if 0/false, will if true return true or if false will error message to append to main error message
	js call to function if you need to do more with values
	verbose 1/true | 0/false[default] adds the reason for the rejection
	Example calls: 
	chkDts(['FAQDateRange1',"U","P"],['FAQDateRange2',"U","P"],0,'PopHdnFaqDts(d1,d2)');
		this will check values of FAQDateRange1 and FAQDateRange2, 
		array format of args 1 & 2 dictates values to be mdy format for date in past and then 
		call PopHdnFaqDts with the d1 and d2 params (*numeric* yyyymmdd formatted date) being the created in validateDate.
	chkDts('FAQDateRange1','FAQDateRange2',1);
		this will check values of FAQDateRange1 and FAQDateRange2, expect m/d/yy format for any past or future dates,
		pop up alert if fail and
		return false
	*/
	var o1=document.getElementById(typeof f1=="string"?f1:f1[0]);
	var o2=document.getElementById(typeof f2=="string"?f2:f2[0]);
	var ErMsg="";
	var d1="";
	var d2="";
	if(alrt==null || alrt!=false && alrt!=true && alrt !=1 && alrt!=0) alrt=true;
	if(verbose==null || verbose!=false && verbose!=true && verbose !=1 && verbose!=0) verbose=false;
	if(o1.value.length) d1=validateDate(o1.value, (typeof f1=="string")?"U" : f1[1], (typeof f1=="string")?"A" : f1[2]);
	if(o2.value.length) d2=validateDate(o2.value, (typeof f2=="string")?"U" : f2[1], (typeof f2=="string")?"A" : f2[2]);
	if(o1.value.length && d1<0) ErMsg+="\nYour first date, "+o1.value+", is not valid (required format: m/d/yyyy)"+(verbose?"\nNot accepted because: "+aryDateErrors[-d1-1] : "");
	if(o2.value.length && d2<0) ErMsg+="\nYour second date, "+o2.value+", is not valid (required format: m/d/yyyy)"+(verbose?"\nNot accepted because: "+aryDateErrors[-d2-1] : "");
	if(o1.value.length && o2.value.length && d1>"" && d2>"" && d2<d1) ErMsg+="\nThe two dates you have entered are in reverse order";
	if(ErMsg.length>0){
		if(alrt){
			alert("There is a problem with the dates you entered:"+ErMsg);
			return false;
		}else return ErMsg;
	}
	if(js!=null) eval(js);//doSomething(d1,d2)
	return true;
}
function makeDatePlusVal(inVal){
	var now=new Date();
	if(inVal=="undefined" || isNaN(inVal) ) inVal = 0;
	return now.setDate(now.getDate()+inVal);
}

