//90731 RKR added condition and alert when Euro style numbering is suspected (comma as decimal point).
//90723 RKR elim dup ckPhone and set up use of ckEmail/Phone in onSubmits for ContactUs and Deal... pages
//90406 RKR augmented ckPhone and added field coloration to valWin();
//81015 SRC added a trim strip routine to remove whitespace before/after eamil addresses prior to data validation 
//80805 SRC added a check email in list function that is a wrapper for ckEmail when a list is provided.
//80527 RKR changed background coloration of textboxes for data validation from FF4444 to FFCCCC
//50304 RKR removed replaced radio-button functionality; revised Euro-decimal style processing in ckNum
//50122 RKR added rm at bottom for radio button unchecking
//40707 RKR added comset at bottom for csv text boxes

 String.prototype.trim = function() {
       return this.replace(/^\s+|\s+$/g,"");
   }

var whitespace = " \t\n\r";
function isWhitespace (s)
{   var i;
    if ((s == null) || (s.length == 0)) return true;
    for (i = 0; i < s.length; i++)
    {	var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1) return false;
    };
    return true;
};
function hasSpace(s) {
	if (s.indexOf(" ")== -1) return false
	else return true;
};
function replacechar(string,re,str) {
    
    var expression = new RegExp(re, "gi");
    string = string.replace(expression,str);
    return string;
}

/* function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	// otherwise, update 'characters left' counter
	else
	{
		 if (document.getElementById)
	  		document.getElementById(cntfield).innerHTML = '<b>' + (maxlimit - field.value.length) + '</b>&nbsp;characters left';
     } 
} */

/*********************************************************** 
 ckNum
 parms:
 (0) obj - the form field to check
 (1) isint - check for valid int
 (2) posval - check for nonnegative
 (3) noshowmsg - suppress default messaging flag
 (4) bintlarge - do not show 32000 limit on data
**********************************************************/
function ckNum(obj,isint) {
    
	var numstr = obj.value;
	//document.getElementById('pricelbl').innerHTML = ""; 
	obj.style.background = "#ffffff"; 
	
	
	if (!numstr) return true;
				
	//grab the parameter list
	var args=ckNum.arguments;
	// if we have allowance for integer>32000, capture to local var
	var bintlarge = 0;
	if (args[4]) bintlarge=args[4];
	// if we have a check for show msg, capture to local var
	var bnoshowmsg = 0;
	if (args[3]) bnoshowmsg=args[3];
	
	// if we have a check for positive integer arg passed, capture to local var
	var nispos = 0;
	if (args[2]) nispos=args[2];
	
	nlen = numstr.length;
	// repair whitespace to empty string
	if (nlen >0 && isWhitespace(numstr)) {
		obj.value = "";
		return true;
	}; 
	// comma processing
 	com = numstr.indexOf(',');
	if (com>=0 && isint==0) {
		dot = numstr.indexOf('.');
		// IF a period precedes a comma OR comma without period is not 3 digits from end (like US integer)
		// THEN Euro style; trap and convert to US style. (will not catch Euro nn,nnn for decimal)
		if ((dot>=0 && dot<com) || (dot==-1 && nlen-com != 4))
	 	{
			//Replace decimals with pipes
			numstr = substitute(numstr, ".", "|");
	 	  	// convert commas used as decimal to decimals
			numstr = substitute(numstr, ",", ".");
			//Replace pipes with commmas
			numstr = substitute(numstr, "|", ",");
			// send it back to form element before carrying on
			obj.value = numstr;
			alert("NUMBER PUNCTUATION REVERSED\n\nWe have interpreted a number as written with\na comma to indicate a decimal and\periods as whole-number separators:\n\nPlease confirm correctness of resulting number entry\nin English usage (period as decimal point) or\nrewrite your entry in English usage.");			
		};
		//strip remaining commas for NaN test if present, regardless of original data entry style
	 	numstr = replacechar(numstr, ",", "");
	};		
	// check for number 
	if (isNaN(numstr)) {
		msg = "IMPROPER NUMERIC DATA\n\nPlease edit your entry so it\nis a number without text or punctuation";
		if (isint==1) msg= msg + "."
		else msg = msg + "\nother than a '.' decimal point.";
		// alert(whichBrs());
		//  if (whichBrs() == "Internet Explorer"){ obj.focus(); obj.select()}
		//  else {setTimeout("obj.focus();obj.select()", 1); }
		// document.getElementById('pricelbl').innerHTML = " Error "; 
		obj.style.background = "#FFCCCC"; 
		if (bnoshowmsg == 0) alert(msg);
		return false;
	} 
	
	// check for integer (decimal and range)
	 if (isint==1) {
			i = numstr.indexOf(".");
			if (i>=0) {
				msg = "INTEGER CAN'T HAVE DECIMAL\n\nPlease edit your entry so it\nis an integer without text or punctuation";
				if (!bintlarge)
					msg += "\nunder 32000, the limit for this data.";
				else
					msg += '.';
				//obj.focus();
				//obj.select();
				obj.style.background = "#FFCCCC"; 
				if (bnoshowmsg == 0) alert(msg);
				return false;
			} 
			else if (!bintlarge && parseInt(numstr)>32000) {
				msg = "INTEGER OUT OF RANGE\n\nPlease edit your entry so it\nis an integer without text or punctuation";
				msg = msg + "\nunder 32000, the limit for this data.";
				//obj.focus();obj.select();
				obj.style.background = "#FFCCCC"; 
				if (bnoshowmsg == 0) alert(msg);
				return false;
		    } 
		}  
			
	 // check for postive values if required (args[2])
	  if (nispos) {
		  
		    if (!isPosValue(numstr)) {
				msg = "VALUE MUST BE POSITIVE\n\nPlease edit your entry so it\nis a positive numeric value.";
				//obj.focus();obj.select();
				obj.style.background = "#FFCCCC"; 
				if (bnoshowmsg == 0) alert(msg);
			    return false;
				 }
		}
		
   return true;
};
function isPosInteger(inputVal) {
	inputStr = inputVal.toString();
	if (inputStr.length==0) return false; 
		
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i);
		if (oneChar < "0" || oneChar > "9") {
			return false;
		};
	};
	return true;
};
function isPosValue(inputVal) {
	inputStr = inputVal.toString();
	i = inputStr.indexOf("-");
			if (i>=0) return false;
	return true;
};
function nocommas(str) {
	var i = str.indexOf(',');
	while (i>-1) {
		str = str.substring(0,i)+str.substring(i+1,str.length)
		i = str.indexOf(',',i);
	};
	return str;
};
function clean(st,len) {	
	var i = st.indexOf('"');
	if (i>0) {
		var temp = st; var stt = 0; var acount = 0;
		while (i > 0) {
			temp = temp.substring(stt,i) + "''" + temp.substring(i+1);
			stt = i+1; i = st.indexOf('"',stt);
			acount += 1;
	};
	if (temp.length > len+acount)
		alert("CAN\'T SQUEEZE DOUBLEQUOTE INTO LIMIT\n\nEncoding a quotation mark puts this entry over\nits size limit.  Edit your entry\nto be shorter by one character for each quotation mark used.");
		return st;
	} else return temp;
};

/* isemail
function isEmail(theField) {
	s = theField.value;
	if (isWhitespace(s)) {
       return false;
	};
	if (hasSpace(s)) return false;
    var i = 1;
    var sLength = s.length;
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    };
    if ((i >= sLength) || (s.charAt(i) != "@")) {
        return false;
	} else if (s.indexOf('@',i+1)>0) {
		return false;
	} else {
		i += 2;
	};
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    };
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
        return false;
	}
    else return true;
};
*/

// valid email check for list of emails
function cklstofEmails (theField) {
	if (!theField.value) return true;
   
    rtnvalue = true;
	var strEmail = theField.value.split(",");
	
	for(i = 0; i < strEmail.length; i++){
		theField.value = strEmail[i];
		if (!ckEmail(theField)) 
		 rtnvalue = false;
	}
	theField.value = strEmail;
    return rtnvalue;	
};


// valid email check
function ckEmail (theField) {
	if (!theField.value) return true;
	
	s=theField.value.trim();
	if (!ckFilled(theField)) {
		valWin(theField, "Email Address", " so respondents\nmay reach you easily");
		return false;
	} else {
		if (theField.style.background == "#ffcccc") theField.style.background = "#FFFFFF";
	};

	// email format test using test() method and regular expressions
	var emailFilter=/^[a-zA-Z0-9\'\.\-\_]+@[a-zA-Z0-9\'\.\-\_]+\.[a-zA-Z]{2,6}$/;
	if (!(emailFilter.test(s)) || (hasSpace(s))) { 
		 valWin(theField, "Email Address", " in the form of\n'userID@domain.type'  or  'userID@domain.countryID'\nand without any illegal characters");
		 return false;
	} else {
		if (theField.style.background == "#ffcccc") theField.style.background = "#FFFFFF";
		return true;
	};
};
// required entry check
function ckFilled(theField) {
	if (isWhitespace(theField.value)) {
		return false;
	};
	return true;
};
function ckName(theField) {
	if (!ckFilled(theField)) {
		valWin(theField,"Name","\nso respondents may reach you easily")
		return false;
	} else {
		if (theField.style.background == "#ffcccc") theField.style.background = "#FFFFFF";
		return true;
	};
};

// tests phone number for min of 6 digits and more digits than text
function ckPhone(theField) {
	if (!theField.value) return true;

	s = theField.value;
	if (!ckFilled(theField)) {
		valWin(theField, "Phone Number", " so respondents\nmay reach you easily");
		return false;
	} else {
		if (theField.style.background == "#ffcccc") theField.style.background = "#FFFFFF";
	};
    var i = 0;var j=0;var k=0;
 	var s = theField.value;
	var sLength = s.length;
    while (i < sLength)  {
		if (isNaN(s.charAt(i)) && '.+-() '.indexOf(s.charAt(i)) < 0) j++ // invalid alpha
		else k++;  // digit
		i++;
    };
    if (k < 6 || j > k) {
		valWin(theField, "Phone Number", ".\n\n*** "+s+" ***\n does not look like a valid international telephone number.\n\nPlease resubmit this form with a valid phone number\nin a standard format without extraneous characters.\n\nIf you believe your submission is correct as\nsubmitted, please call +1.201.886.8820 for assistance");
		theField.style.background == "#ffcccc";
		return false
	} else {
		if (theField.style.background == "#ffcccc") theField.style.background = "#FFFFFF";
		return true;
	};
};

// pops up alert requesting complete entry of 'field' with qualification 'msg' and changes background of form element
function valWin(theField, fieldname, msg) {
	var notice = "CORRECT AND COMPLETE " + fieldname + " NEEDED\n\nPlease provide a complete " + fieldname + msg + ".\n\nThank you.";
	alert(notice);
	theField.style.background = "#ffcccc";
	theField.focus();
    theField.select();
};
// Checks for common HTML tag errors in Print Catalog (messes up RTF conversion)
function balTag(str,tok) {
	var i = str.indexOf(tok);
	var j=0;
	var jj=0;
	while (i+1) {
		j++;
		i=str.indexOf(tok,i+1)
	};
	tok = tok.substring(0,1)+'/'+tok.substring(1,10);
	i = str.indexOf(tok);
	while (i+1) {
		jj++;
		i=str.indexOf(tok,i+1)
	};
	if (j==jj) return true
	else return false;	
};
function valTags(str) {
	if (!balTag(str,'<b>') || !balTag(str,'<i>') || !balTag(str,'<td>') || !balTag(str,'<tr>') || !balTag(str,'<th>') || !balTag(str,'<table>')) {
		alert('HTML TAGS OUT OF BALANCE\n\nPlease be sure you have closed\nany opened HTML tag, such as\n"<b>" or "<i>".');
		return false;
	} else return true;
};

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}



function formatCents(obj) {
	alert('formattingCents');
	str = obj.value;
	i = str.length;
	j = str.indexOf('.');
	if (isNaN(str)) {
		ckNum(obj,0);
		return;
	};
	if (j >= 0) {
		k = i-j;
		if (k==1) {
			obj.value = str+'00'
		} else if (k==2) {
			obj.value = str+'0'
		} else if (k>6 && !isNaN(str)) {
			obj.value = Math.round(parseFloat(str) * 100000) / 100000
		};	
	} else obj.value = str+'.00'
};
// new date routines by Scott Conklin	
 var monName = new Array ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
 var MONTH_NAMES = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
 var daysoftheweek = new Array ("Sun","Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
 var daysofweek_sh = new Array ("Su","Mo", "Tu", "We", "Th", "Fr", "Sa");
 var daysofweek_lg = new Array ("Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	
// ------------------------------------------------------------------
// isDate ( date_string, format_string )
//
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
//
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ------------------------------------------------------------------
function isDate(obj, format) {
    val = obj.value;
	 if (val == "")
		  return;
  
	val = val+"";

	var date = getDateFromFormat(val,format);
	
	if (date == 0) 
	  {
	    alert ("Invalid Date/Time");
		obj.focus();
	    return false; 
	   }
 
    obj.value = formatDate(date, format); 
   	return true;
	}
// ------------------------------------------------------------------
// formatDate (date_object, format)
//
// Returns a date in the output format specified.
// The format string uses the same abbreviations as in getDateFromFormat()
// ------------------------------------------------------------------
function formatDate(date,format) {
	format = format+"";
	var result = "";
	var i_format = 0;
	var c = "";
	var token = "";
	var y = date.getFullYear()+"";
	var m = date.getMonth()+1;
	var d = date.getDate();
	var nday = date.getDay();
	var H = date.getHours();
	var M = date.getMinutes();
	var s = date.getSeconds();
	var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k,ddd,dddd,mmm,mmmm;
   // Convert real date parts into formatted versions
  	
   // Year
   if (y.length < 4) {
  		y = y-0+1900;
	  }
	yyyy = ""+y;
	y = ""+y;
  	if (y < 10) { yy = "0"+y; }
		else { yy = y; }
    y = y.substring(2,4);
	yy = yy.substring(2,4);
	// Month
	if (m < 10) { mm = "0"+m; }
		else { mm = m; }
	
	ddd = daysoftheweek[nday];
	dddd = daysofweek_lg[nday];
		
	mmm = monName[m-1];
	mmmm = MONTH_NAMES[m-1];
	
	// Date
	if (d < 10) { dd = "0"+d; }
		else { dd = d; }
   
	// Hour
	h=H+1;
	K=H;
	k=H+1;
	
	if (h > 12) { h-=12; }
	if (h == 0) { h=12; }
	
	if (h < 10) { hh = "0"+h; }
		else { hh = h; }
	
	if (H < 10) { HH = "0"+K; }
		else { HH = H; }
	
	if (K > 11) { K-=12; }
	
	if (K < 10) { KK = "0"+K; }
		else { KK = K; }
	
	if (k < 10) { kk = "0"+k; }
		else { kk = k; }
	
	// AM/PM
	if (H > 11) { ampm="PM"; }
		else { ampm="AM"; }
	
	// Minute
	if (M < 10) { MM = "0"+M; }
		else { MM = M; }
	
	// Second
	if (s < 10) { ss = "0"+s; }
		else { ss = s; }
	
	// Now put them all into an object!
	
	var value = new Object();
	value["yyyy"] = yyyy;
	value["yy"] = yy;
	value["y"] = parseInt(y);
	value["mmm"] = mmm;
	value["mmmm"] = mmmm;
	value["mm"] = mm;
	value["m"] = m;
	value["dddd"] = dddd;
	value["ddd"] = ddd;
	value["dd"] = dd;
	value["d"] = d;
	value["hh"] = hh;
	value["h"] = h;
	value["HH"] = HH;
	value["H"] = H;
	value["KK"] = KK;
	value["K"] = K;
	value["kk"] = kk;
	value["k"] = k;
	value["MM"] = MM;
	value["M"] = M;
	value["ss"] = ss;
	value["s"] = s;
	value["tt"] = ampm;
	value["/"] = '/';
	value["-"] = '-';
   	while (i_format < format.length) {
		// Get next token from format string
		c = format.charAt(i_format);
		token = "";
		 
		while ((format.charAt(i_format) == c) && (i_format < format.length)) {
			token += format.charAt(i_format);
			i_format++;
			}
		if (value[token] != null) {
			result = result + value[token];
			}
		else {
			result = result + token;
			}
		}
	return result;
	}

function _isInteger(val) {
	var digits = "1234567890";
	for (var i=0; i < val.length; i++) {
		if (digits.indexOf(val.charAt(i)) == -1) { return false; }
		}
	return true;
	}
function _getInt(str,i,minlength,maxlength) {
  	for (x=maxlength; x>=minlength; x--) {
		var token = str.substring(i,i+x);
		if (token.length < minlength) {
			return null;
			}
		if (_isInteger(token)) { 
			return token;
			}
		}
	return null;
	}
function substitute(string, match, replacement) {
  // Within STRING, replace any MATCHing string with the REPLACEMENT.
 
  var result = '';
  var index = 0;
  var lastIndex = index;
  
 while (string.length > lastIndex) {
    index = string.indexOf(match, lastIndex);
    if (index == -1) { break }
	if (string.charAt(index-1) != ' ') {
	   result += string.substring(lastIndex, index) + replacement;
	   lastIndex = index + match.length;
	  
	}else
	{
	  break; 
	}
	 	
  } 
  
  result += string.substring(lastIndex, string.length);
  return result;
} 
// compare 2 dates
//a < b = -1 : a = b = 0 : a > b = 1 
function compareDate(a,b) 
{ 
	var aDate = new Date(a); 
	var bDate = new Date(b); 
	if(aDate.getTime() == bDate.getTime()) return 0; 
	return (aDate.getTime() < bDate.getTime() ? -1 : 1); 
} 
 
// ------------------------------------------------------------------
// getDateFromFormat( date_string , format_string )
//
// This function takes a date string and a format string. It matches
// If the date string matches the format string, it returns the 
// getTime() of the date. If it does not match, it returns 0.
// ------------------------------------------------------------------
function getDateFromFormat(val,format) {
    
	val = val+"";
	format = format+"";
	var i_val = 0;
	var i_format = 0;
	var c = "";
	var token = "";
	var token2= "";
	var x,y;
	var now   = new Date();
	var year  = now.getYear();
	var month = now.getMonth()+1;
	var date  = now.getDate();
	var hh    = now.getHours();
	var MM    = now.getMinutes();
	var ss    = now.getSeconds();
	var dayofweek  = now.getDay();
	var ampm  = "";
	var chk_dayinmonth = false;
	var chk_dayinyear = false;
	var chk_days = false;
	var diffindays = 0;
	
	/* val= substitute(val, 'P', ' P');
	val= substitute(val, 'p', ' p');
	val = substitute(val, 'A', ' A');
	val = substitute(val, 'a', ' A');
	 */ 
	// Extract contents of value based on format token
	while (i_format < format.length) {
		// Get next token from format string
		c = format.charAt(i_format);
		token = "";
		while ((format.charAt(i_format) == c) && (i_format < format.length)) {
			token += format.charAt(i_format);
			i_format++;
			
			}

		// YEAR 
		if (token=="yyyy" || token=="yy" || token=="y") {
			chk_dayinyear = true;
			if (token=="yyyy") { x=4;y=4; }// 4-digit year
			if (token=="yy")   { x=2;y=2; }// 2-digit year
			if (token=="y")    { x=1;y=2; }// 2-or-4-digit year
			year = _getInt(val,i_val,x,y);
			//alert(year);
			if (year == null) 
			 {  x=1; y=1;
			    year = _getInt(val,i_val,x,y);         
			 }
			if (year == null) { return 0; }
			
			i_val += year.length;
			if (year.length == 2) {
				if (year > 70) {
					year = 1900+(year-0);
					}
				else {
					year = 2000+(year-0);
					}
				}
			}
			if (year.length == 4) {
				if (year < 1900 || year > 3000) { return 0; } 			
			}
	
   	     // Month name long and short			
		 else if (token=="mmmm" || token=="mmm"){
			month = 0;
			chk_dayinmonth = true;
			var bok = false;
			for (var i=0; i<monName.length; i++) {
				var month_name = MONTH_NAMES[i];
				if (val.substring(i_val,i_val+month_name.length).toLowerCase() == month_name.toLowerCase()) 
				{
				    month = i+1;
					if (month>12) { month -= 12; 
					}
						i_val += month_name.length;
						bok = true;
						break;
					}
				}
			if (!bok) {
			month = 0;	
			//alert(val)
			for (var i=0; i<monName.length; i++) {
				var month_name = monName[i];
				
				if (val.substring(i_val,i_val+month_name.length).toLowerCase() == month_name.toLowerCase()) {
					
				    month = i+1;
					if (month>12) { month -= 12; }
					i_val += month_name.length;
					break;
					}
				}				
			}
		   	if (month == 0) { return 0; }
		 	if ((month < 1) || (month>12)) { return 0; }
		 } 
		// Month name as integer  
		else if (token=="mm" || token=="m") {
			chk_dayinmonth = true;
			x=token.length; y=2;
			month = _getInt(val,i_val,x,y);
			if (month == null) 
			 {  x=1; y=1;
			    month = _getInt(val,i_val,x,y);         
			 }
			if (month == null) { return 0; }
			if ((month < 1) || (month > 12)) { return 0; }
			i_val += month.length;
			}
			
		// Day as integer  	
		else if (token=="dd"  || token=="d") {
			x=token.length; y=2;
			date = _getInt(val,i_val,x,y);
			if (date == null) 
			 {  x=1; y=1;
			    date = _getInt(val,i_val,x,y);         
			 }
			
			if (date == null) 
    			 { return 0; }
			
			if ((date < 1) || (date>31)) { return 0; }
			i_val += date.length;
			 
			}
			
		// day name long and abbreviation  			
		else if (token=="dddd" || token=="ddd"){
			nday = 0;
			var bok = false;
			for (var i=0; i<daysofweek_lg.length; i++) {
				var day_name = daysofweek_lg[i];
				if (val.substring(i_val,i_val+day_name.length).toLowerCase() == day_name.toLowerCase()) {
				    nday = i+1;
					if (nday>7) { nday -= 7; }
					i_val += day_name.length;
					bok = true;
					break;
					
					}
				}
			
			if (!bok){
			nday = 0;
			for (var i=0; i<daysoftheweek.length; i++) {
				var day_name = daysoftheweek[i];
				if (val.substring(i_val,i_val+day_name.length).toLowerCase() == day_name.toLowerCase()) {
				    nday = i+1;
					if (nday>7) { nday -= 7; }
					i_val += day_name.length;
					break;
					}
				}		
			}
							
			if (nday == 0) { return 0; }
			if ((nday < 1) || (nday>7)) { return 0; }
			diffindays = (nday-1) - dayofweek;
		  }	
		  
		// hour as integer 	1 -based (12 clock)  
		else if (token=="hh" || token=="h") {
			x=token.length; y=2;
			hh = _getInt(val,i_val,x,y);
			
			if (hh == null) 
			 {  x=1; y=1;
			    hh = _getInt(val,i_val,x,y);         
			 }
			 
			if (hh == null) { return 0; }
			if ((hh < 1) || (hh > 12)) { return 0; }
			i_val += hh.length;
			hh--;
			}
		
		// hour as integer 	1-based (24 clock)
		else if (token=="HH" || token=="H") {
			x=token.length; y=2;
			hh = _getInt(val,i_val,x,y);

			if (hh == null) 
			 {  x=1; y=1;
			    hh = _getInt(val,i_val,x,y);         
			 }
			 
			if (hh == null) { return 0; }
			if ((hh < 0) || (hh > 23)) { return 0; }
			i_val += hh.length;
			}
		// hour as integer  0-based (12 clock)
		else if (token=="KK" || token=="K") {
			x=token.length; y=2;
			hh = _getInt(val,i_val,x,y);
			if (hh == null) { return 0; }
			if ((hh < 0) || (hh > 11)) { return 0; }
			i_val += hh.length;
			}
		
		// hour as integer 	0-based (24 clock)
		else if (token=="kk" || token=="k") {
			x=token.length; y=2;
			hh = _getInt(val,i_val,x,y);
			if (hh == null) { return 0; }
			if ((hh < 1) || (hh > 24)) { return 0; }
			i_val += hh.length;
			h--;
			}
		// minute  as integer 
		else if (token=="MM" || token=="M") {
			x=token.length; y=2;
			MM = _getInt(val,i_val,x,y);
				
			if (MM == null) 
			 {  x=1; y=1;
			    MM = _getInt(val,i_val,x,y);         
			 }

			if (MM == null) { return 0; }
			if ((MM < 0) || (MM > 59)) { return 0; }
			i_val += MM.length;
			
			}
			
	    // second  as integer 	
		else if (token=="ss" || token=="s") {
			x=token.length; y=2;
			ss = _getInt(val,i_val,x,y);
			if (ss == null) 
			 {  x=1; y=1;
			    ss = _getInt(val,i_val,x,y);         
			 }
						
			if (ss == null) { return 0; }
			if ((ss < 0) || (ss > 59)) { return 0; }
			i_val += ss.length;
			}
			
		// am/pm marker 
		else if (token=="tt") {
			if (val.substring(i_val,i_val+2).toLowerCase() == "am") {
				ampm = "AM";
				i_val += ampm.length;
				}
			else if (val.substring(i_val,i_val+2).toLowerCase() == "pm") {
				ampm = "PM";
				i_val += ampm.length;
				}
			else {
				return 0;
				}
		 }
		else {
			if (val.substring(i_val,i_val+token.length) != token) {
				return 0;
				}
			else {
				i_val += token.length;
				}
			}
		}
		
	// If there are any trailing characters left in the value, it doesn't match
	if (i_val != val.length) {
		// alert(val.length);
		return 0;
		}
		
	// only check days in combination with passed in month
	if (format=="dddd" || format=="ddd") {
		date+=diffindays;
	}
	else {
		if (!chk_dayinyear) { year=now.getFullYear(); }
		if (!chk_dayinmonth) { month = 1}
     }
	
 
	 	// Is date valid for month?
		if (month == 2) {
			// Check for leap year only if year given, if not assume nonleapyear
		  	if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) { // leap year
					     if (date > 29){ return false; }
					}
				else {
					     if (date > 28) { return false; }
		 	        }
		  }
		if ((month==4)||(month==6)||(month==9)||(month==11)) {
			if (date > 30) { return false; }
			}
	// Correct hours value
	if (hh<12 && ampm=="PM") {
		hh+=12;
		}
	else if (hh>11 && ampm=="AM") {
		hh-=12;
	
		}
	
	var newdate = new Date(year,month-1,date,hh,MM,ss);
	return newdate;	
	}
	
function comset(str) {
	var temp = str;
	if (str.length > 0) {
		temp = replacechar(temp,' ','');
		com = temp.substring(0,1);
		if (com != ",") temp = "," + temp;
		com = temp.substring(temp.length-1);
		if (com != ",") temp = temp + ",";
	};
	return temp;
	};
	
// rm (radioMouseover) paints Confirm to uncheck a checked radio button
function rm(obj) {
	if (obj.checked) if(confirm('DESELECT this option?')) obj.checked=false;
};
function whichBrs() {
var agt=navigator.userAgent.toLowerCase();
if (agt.indexOf("opera") != -1) return 'Opera';
if (agt.indexOf("staroffice") != -1) return 'Star Office';
if (agt.indexOf("webtv") != -1) return 'WebTV';
if (agt.indexOf("beonex") != -1) return 'Beonex';
if (agt.indexOf("chimera") != -1) return 'Chimera';
if (agt.indexOf("netpositive") != -1) return 'NetPositive';
if (agt.indexOf("phoenix") != -1) return 'Phoenix';
if (agt.indexOf("firefox") != -1) return 'Firefox';
if (agt.indexOf("safari") != -1) return 'Safari';
if (agt.indexOf("skipstone") != -1) return 'SkipStone';
if (agt.indexOf("msie") != -1) return 'Internet Explorer';
if (agt.indexOf("netscape") != -1) return 'Netscape';
if (agt.indexOf("mozilla/5.0") != -1) return 'Mozilla';
if (agt.indexOf('\/') != -1) {
if (agt.substr(0,agt.indexOf('\/')) != 'mozilla') {
return navigator.userAgent.substr(0,agt.indexOf('\/'));}
else return 'Netscape';} else if (agt.indexOf(' ') != -1)
return navigator.userAgent.substr(0,agt.indexOf(' '));
else return navigator.userAgent;
};

