function validDateRange(firstprefix, secondprefix) {

	var yearval = document.getElementById(prefix + "_year").value;
	var monthval = document.getElementById(prefix + "_month").value;
	var dayval = document.getElementById(prefix + "_day").value;

	var isdate = new Date();
	isdate.setFullYear(yearval, monthval, dayval);

	if ( (yearval == 0) || (monthval == 0) || (!dayval) ) {
		return false;
	}

	if (isdate.getMonth() != monthval) {
		return false;
	} else {
		return true;
	}
	
}

function validRadio(radioname){
	var radio = document.getElementsByName(radioname);

	var radioSelected = 0;

	for (i=0 ; i<radio.length ; i++) {
			if (radio[i].checked == true) {
				radioSelected = 1;
			}
	  	}

	if ( radioSelected > 0 )
		return true;
	else
		return false;

}

function validDate(prefix) {

	var yearval = document.getElementById(prefix + "_year").value;
	var monthval = document.getElementById(prefix + "_month").value;
	var dayval = document.getElementById(prefix + "_day").value;

	var isdate = new Date();
	isdate.setFullYear(yearval, monthval, dayval);

	if ( (yearval == 0) || (monthval == 0) || (!dayval) ) {
		return false;
	}

	if (isdate.getMonth() != monthval) {
		return false;
	} else {
		return true;
	}

}


/**--------------------------
//* Validate Date Field script- By JavaScriptKit.com
//* For this script and 100s more, visit http://www.javascriptkit.com
//* This notice must stay intact for usage
// DATES MUST BE MM/DD/YYYY
---------------------------**/

function validMMDDYYYY(inputdate){
	//Basic check for format validity
	var validFormat = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	var regex = new RegExp(validFormat);
	var returnval = false;
	if(regex.test(inputdate) == false)
		returnval = false;
	else{ //Detailed check for valid date ranges
		var monthfield=inputdate.split("/")[0];
		var dayfield=inputdate.split("/")[1];
		var yearfield=inputdate.split("/")[2];
		var dayobj = new Date(yearfield, monthfield-1, dayfield);
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
			returnval = false;
		else
			returnval=true;
	}
	return returnval;
}

// date must be mm/dd/yyyy hh:mm AM/PM
function validDateTime(inputdate){
	//Basic check for format validity
	var validFormat = /^\d{1,2}\/\d{1,2}\/\d{4}\s(1|2|3|4|5|6|7|8|9|10|11|12):[0-5]\d\s(AM|PM|am|pm)$/;
	var regex = new RegExp(validFormat);
	var returnval = false;
	if(regex.test(inputdate) == false){
		alert("format was wrong");
		returnval = false;
	}
	else{ //Detailed check for valid date ranges
		var monthfield = inputdate.split("/")[0];
		var dayfield = inputdate.split("/")[1];
		var yearfield = Left(inputdate.split("/")[2],4);
		var hourfield = parseInt(Right(inputdate.split(":")[0],2));
		var minutefield = parseInt(Left(inputdate.split(":")[1],2));
		var dayobj = new Date(yearfield, monthfield-1, dayfield);
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
			returnval = false;
		else if(hourfield <= 0 || hourfield > 12)
			returnval = false;
		else if(minutefield < 0 || minutefield > 60)
			returnval = false;			
		else
			returnval = true;
	}
	return returnval;
}

function validEmail(email) {

    var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
    var regex = new RegExp(emailReg);

	if (regex.test(email) == false)
		return false;
	else
		return true;
	
}

function validURL(url) {

    var urlReg = /^[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?$/;
    var regex = new RegExp(urlReg);

	if (regex.test(url) == false)
		return false;
	else
		return true;
	
}

function validCurrency(currency) {
    var curReg = /^\$?\d{1,3}(\,?\d{3})*(\.?\d{2,})?$/;
	var regex = new RegExp(curReg);

	if (regex.test(currency) == false)
		return false;
	else
		return true;
	
}

function formValidator(form){
	// for each form element validated, add the following custom attributes
	// note: if a field does not need to be validated, simply omit the 'val' attribute
	// val="x", where x is how the field needs to be validated ... multiple validations can be applied to a field by delimiting via pipes
	// valtext="x", where x is the validation message to be presented. i.e. 'Please enter blah blah blah.' - these are indexed matching the indices of the val field
	// validation types (val="x"): (this list will grow)
	//
	// - text boxes/text areas/file inputs
	//   - 'notempty':verifies that a value has been entered
	//   - 'isemail': verifies that the value is a valid email
	//	 - 'isurl': verifies that the value is a valid url (no http://)
	//   - 'isnumber': verifies that the value is a number
	//   - 'isdate': verifies that the value is a date in mm/dd/yyyy format
	//   - 'isdatetime': verifies that the value is a date in mm/dd/yyyy hh:mm AM/PM format
	//   - 'iscurrency': verifies that the value is a currency
	//   - 'maxlength': requires an extra attribute, 'maxlength=x', where x is the maximum number of characters allowed
	//
	// - radio buttons
	//   - 'oneselected': requires that at least one radio button option has been selected
	//
	// - checkboxes
	//   - 'ischecked': requires that the specified checkbox be checked
	// - select-one dropdowns
	//   - 'notzeroindex': requires that an option other than option[0] is selected
	
	var strErrors, strErr, numElements, strVals, aVals, strMsgs, aMsgs, i, j;
	strErrors = "";
	numElements = form.elements.length;
	var strDebug;
	strDebug = "";
	
	// loop through all form fields
	for(i=0;i<numElements;i++)
	{
		//alert("val property of " + form.elements[i].name + ": " + form.elements[i].getAttribute('val'));
		//return false;
		
		// is a validation specified?
		if(form.elements[i].getAttribute('val') != null)
		{	
			// put value in string
			strVals = form.elements[i].getAttribute('val');	
			// split string into array on comma delimiter
			aVals = strVals.split("|");
			// put messages in string
			strMsgs = form.elements[i].getAttribute('valtext');
			//split string into array on comma delimeter
			aMsgs = strMsgs.split("|");
			
			master:
				switch(form.elements[i].type)
				{
					
					case "file":
					case "textarea":
					case "password":
					case "text":
						for(j=0;j<aVals.length;j++)
						{
							master2:
								switch(aVals[j])
								{
									case "notempty":
										if(form.elements[i].value == "")
											strErrors += aMsgs[j] + "\n";
										break;
									case "isemail":
										if(validEmail(form.elements[i].value) == false)
											strErrors += aMsgs[j] + "\n";
										break;
									case "isurl":
										if(validURL(form.elements[i].value) == false)
											strErrors += aMsgs[j] + "\n";
										break;										
									case "isnumber":
										if(isNaN(form.elements[i].value))
											strErrors += aMsgs[j] + "\n";
										break;
									case "maxlength":
										if(form.elements[i].value.length > form.elements[i].getAttribute('maxlength'))
											strErrors += aMsgs[j] + "\n";
										break;
									case "isdate":
										if(validMMDDYYYY(form.elements[i].value) == false)
											strErrors += aMsgs[j] + "\n";
										break;
									case "isdatetime":
										if(validDateTime(form.elements[i].value) == false)
											strErrors += aMsgs[j] + "\n";
										break;										
									case "iscurrency":
										if(validCurrency(form.elements[i].value) == false)
											strErrors += aMsgs[j] + "\n";
										break;
									case "filled-iscurrency":
										if(form.elements[i].value != ""){
											if(validCurrency(form.elements[i].value) == false)
												strErrors += aMsgs[j] + "\n";
										}
										break;
									case "match":
										if(form.elements[i].value != document.getElementById(form.elements[i].getAttribute('matchfield')).value)
											strErrors += aMsgs[j] + "\n"
										break;
								}
						}
						break;
					case "radio":
						for(j=0;j<aVals.length;j++)
						{
							master3:
								switch(aVals[j])
								{
									case "oneselected":
										if(validRadio(form.elements[i].name) == false)
											strErrors += aMsgs[j] + "\n";
										break;
								}
						}
					case "checkbox":
						for(j=0;j<aVals.length;j++)
						{
							master4:
								switch(aVals[j])
								{
									case "ischecked":
										if(!(form.elements[i].checked))
											strErrors += aMsgs[j] + "\n";
										break;
									case "onechecked":
										if(validRadio(form.elements[i].name) == false)
											strErrors += aMsgs[j] + "\n";
										break;									
								}
						}
					case "select-one":
						for(j=0;j<aVals.length;j++)
						{
							master5:
								switch(aVals[j])
								{
									case "notzeroindex":
										if(form.elements[i].selectedIndex == 0)
											strErrors += aMsgs[j] + "\n";
										break;
								}
						}						
					default:
						break master;
				}
		}
	}
	
	if(strDebug != "")
	{
		alert(strDebug);
	}
	
	if(strErrors != "")
	{
		alert(strErrors);
		return false;
	}
	else
	{return true;}
}
