var whitespace = " \t\n\r";
var digits = "0123456789";

function isEmpty(s) {   
	return ((s == null) || (s.length == 0))
}

function isEmptyOrWhitespace (s) {   
	var i;
    if (isEmpty(s)) return true;
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;
}

function isRadioSelected(radiofield) {   
	for (var i = 0; i < radiofield.length; i++) {   
		if (radiofield[i].checked) return true
    }
    return false
}

// functions that return true or false indicating validity of a specific 
// type of input 
//
// to use, call the function and pass it input.
// 
// document.myForm.onSubmit = function() {
//	if (!isValidPhoneNumber(document.myForm.phone_number.value)) {
//		alert("the phone number you entered sucks!");
//		return false;
//	}
//	return true;
// }



function isValidPhoneNumber(s) {
	var temp = s.replace(/\D/g, "")
	return temp.length > 9 && temp.length < 26
}

function isValidZipCode(s) {
	var temp = s.replace(/\D/g, "")
	return temp.match(/^\d{5}$|^\d{9}$/) != null
}

function isValidSelectBox(o) {
	return (o.options[o.selectedIndex].value != "_none_" && 
			o.options[o.selectedIndex].value.trim() != "")
}

function isValidEmailAddress(s) {
	var temp = s.replace(/\s/g, "")
	return (temp.match(/^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)) && 	
			temp.charAt(0) != "." && !(temp.match(/\.\./))			
}
	
//general upload attachment
function allowedType(filename,type) {
	allowSubmit = false;

	if(type=="memberlist")extArray = new Array(".txt", ".cvs");
	else extArray = new Array(".gif", ".jpeg", ".jpg", ".png",".pdf",".doc",".ppt",".xls",".zip",".txt",".mov",".mp3",".swf",".xml",".xsl");

	if (!filename) return false;
	while (filename.indexOf("\\") != -1)
		filename = filename.slice(filename.indexOf("\\") + 1);
		ext = filename.slice(filename.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) {
			if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	return allowSubmit;
}

//attachment for flash embedding of image - must be jpg
function allowedType2(filename) {
	allowSubmit = false;
	extArray = new Array(".jpeg", ".jpg");
	if (!filename) return false;
	while (filename.indexOf("\\") != -1)
		filename = filename.slice(filename.indexOf("\\") + 1);
		ext = filename.slice(filename.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) {
			if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	return allowSubmit;
}



// masking functions - attempt to parse and reformat 
// element's values as a specific datatype
// to use, tack them to the form field's onblur handler.
// document.myForm.phone_number.onblur = phoneFieldBlurHandler	
//   -> this field will now mask for phone numbers onblur
function textFieldBlurHandler() {
	this.value = this.value.trim()
}

function dateFieldBlurHandler() {
	var d = new Date(this.value)
	if (!isNaN(d)) {
		// if the user didn't specify the century/millenium,
		// then assume the present.
		if (this.value.search(/\d{3}/) == -1) 
			d.setYear(Math.floor((new Date()).getFullYear() / 100) * 100 + d.getFullYear() % 100);

		this.value = d.getHumanDateString()
	}
}

function timeFieldBlurHandler() {
	var t = new Time(this.value)
	if (!isNaN(t)) this.value = t.getHumanTimeString()
}

function phoneFieldBlurHandler() {
	var temp = this.value.replace(/\D/g, "")

	if (temp.length > 9 && temp.length < 26)
		if (temp.length == 10) {
			this.value = "(" + temp.substring(0,3) + ") " 
			this.value += temp.substring(3,6) + "-" + temp.substring(6,10)
		}
	}

function zipcodeFieldBlurHandler() {
	var temp = this.value.replace(/\D/g, "")

	if (temp.length == 5 || temp.length == 9) { 
		if (temp.length == 5) this.value = temp
		else this.value = temp.substring(0,5) + "-" + temp.substring(5,9)
	}
}


// convenience function.
// attaches textFieldBlurHandler to the onblur event 
// of every text or textarea element in f
// var myForm = document.myForm
// attachAllTextHandlers(myForm) 
//		-> every text element in myForm now will call 
//		   textFieldBlurHandler (above) onblur
function attachAllTextHandlers(f) {
	var el
	for (var i = 0; (el = f.elements[i]); i++) {
		if (el.type == "text" || el.type == "textarea") 
			el.onblur = textFieldBlurHandler
	}
}






// as convenient a place as any to store this...
var STD_ERROR_PREFIX = "There were one or more problems with the form values "
STD_ERROR_PREFIX += "you entered.\nPlease check the following and try submitting "
STD_ERROR_PREFIX += "the form again:\n\n"





// Extensions to the Date class:
// var myDate = new Date();
// myDate.getMonthName(); -> "February"
// myDate.getHumanDateString(); -> "February 29, 2000"
// myDate.getHumanTimeString(); -> "4:00 pm"


// returns the name of the month
Date.prototype.getMonthName = function() {
	return ["January","February","March","April","May","June","July","August",
			"September","October","November","December"][this.getMonth()]
}


// returns a nicely formatted date string
Date.prototype.getHumanDateString = function() {
return this.getMonthName() + " " + this.getDate() + ", " + this.getFullYear()
}


// returns a nicely formatted time string
Date.prototype.getHumanTimeString = function() {
	var h = this.getHours()
	var m = this.getMinutes()
	var t = h >= 12 ? "pm" : "am"

	if (h == 0) h = 24
	if (h > 12) h -= 12
	h = String(h)
	m = String(m)
	if (m.length == 1) m = "0" + m

	return h + ":" + m + " " + t
}





// Time is a wrapper class for the Date object
// it accepts a wide variety of strings representing the time
// and returns a date object representing the current date with the
// specified time.
//
// var myTime = new Time('4p');
// myTime now contains a date object representing today at 4PM.
//
// accepted formats include 4p, 4:p, 4:1p, 4:10p, 4:10pm, 16:00, 
// and a bunch more...
/*
function Time(sTimeStr) {
	var a = sTimeStr.match(/(\d{1,2})\s*\x3A?\s*(\d{0,2})\s*(am|pm|a|p)?/i)
	if (a) {
		var d = new Date()
		var h = a[1]
		var m = a[2]
		var t = a[3]
		if (t && t.charAt(0).toLowerCase() == "p") h = h % 12 + 12
		d.setHours(h)
		d.setMinutes(m)
		return d
	}
	return null
}
*/
function Time(sTimeStr) {
	if (sTimeStr.trim() != "")
	{
		var a = sTimeStr.match(/\d{1,2}/g)
		var t = sTimeStr.match(/(am|pm|a|p)/ig)
		if (a) {
			var d = new Date()
			var h = a[0]
			var m = a[1] ? a[1] : 0;
			if (t) t = String(t[0]);
			if (t && t.charAt(0).toLowerCase() == "p") h = h % 12 + 12
			d.setHours(h)
			d.setMinutes(m)
			return d
		}
		return null
	}
}




// Extensions to the String class:
// var myStr = "  yomomma.mpg "
// myStr.trim() -> myStr now contains "hotdogs!"
// myStr.hasFileExtension(".mpg") -> true


// like Trim( ) in vbscript. removes trailing and 
// leading whitespace from a string
String.prototype.trim = function() {
	return this.replace(/^\s*|\s*$/g, "")
}


// determines whether or not a filename has one of the specified extensions
String.prototype.endsWith = function() {
	var bOk = false
	for (var i = 0; i < arguments.length; i++) {
		if (this.indexOf(arguments[i]) == this.length - arguments[i].length) {
			bOk = true
			break
		}
	}
	return bOk
}



// push is a quite useful method of arrays in newer 
// javascript implementations, but not in ie5-
Array.prototype.push = function(v) {
	this[this.length] = v
	return v
}

//because sometimes you need this
function stripCharsInBag (s, bag)
{   var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function stripCharsNotInBag (s, bag) {   
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (bag.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

function stripWhitespace (s) {   
	return stripCharsInBag (s, whitespace)
}


function checkDate1BeforeDate2(date1Label,date1,date2Label,date2) {
	compareresult = checkdateorder( date1.value, date2.value );
	if ((compareresult == -1)||(compareresult == 0)) {
		//alert('order is good');
		return true;
	} else {
		//alert(date1Label + ' is after ' + date2Label)
		return false;
	}
}

//this function returns 
//0 if the dates are the same
//1 if the first date is greater
//-1 if the second date is greater
//-2 if either of the dates are invalid
function checkdateorder( date1, date2 ) {
	split1 = new Array();
	split2 = new Array();
	
	if (date1.indexOf("/")!= -1) {
		split1 = date1.split( '/' );
	} else if (date1.indexOf("-")!= -1) {
		split1 = date1.split( '-' );
	} else {
		//alert('No date delimiter was found');
		return 50
	}
	
	if (date2.indexOf("/")!= -1) {
		split2 = date2.split( '/' );
	} else if (date2.indexOf("-")!= -1) {
		split2 = date2.split( '-' );
	} else {
		//alert('No date delimiter was found');
		return 50
	}
	
	if( eval( split1[2] ) == eval( split2[2] ) ) { // same year
		if( eval( split1[0] ) == eval( split2[0] ) ) { //same year and same month
			if( eval( split1[1] ) == eval( split2[1] ) ) { //same year and same month and same date
				return 0;
				}
			else if( eval( split1[1] ) > eval( split2[1] ) ) { //same year and same month and first date after second
				return 1;
				}
			else
				return -1;
			}
		else if( eval( split1[0] ) > eval( split2[0] ) ) { //same year and 1st month greater than 2nd month
			return 1;
			}
		else 
			return -1;
		}
	else if( eval( split1[2] ) > eval( split2[2] ) ) { //year 1 greater than year 2
		return 1;
		}
	else
		return -1;
}

function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      //var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	  var reg1 = /^\d{2}(\/)\d{1,2}\1\d{1,2}$/
      //var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
	  var reg2 = /^\d{4}(\/)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\/)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\/)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\/)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\/)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}