// number: number;Pretty Field Name;isrequired?;minlen;maxlen
// none: none

var use_fastvalidate = false;
var fastvalidate_array = null;

//Translation string vars
//var req_field_translation;
var req_field_str;

// this takes a field, validates it, adds to the search results
function search_validation(mysearchstring, myform, myfield, validationRule) {
	if (mysearchstring==null) {
		return null;
	}
	var myelement = document.getElementsByName(myfield)[0];
	if (VerifyObject(myform, myelement, validationRule) && ConvertObject (myform, myelement, validationRule)) {
		if (myelement.value != '') {
			mysearchstring += '&' + myelement.value;
		}
		return mysearchstring;
	}
	return null;
}

function add_fastvalidate(myelement) {
	if (fastvalidate_array == null) {
		fastvalidate_array = new Array();
	}
	fastvalidate_array[fastvalidate_array.length] = myelement;
}

function is_form_changed() {
	return (fastvalidate_array != null);
}

function confirm_form_cancel() {
	if (is_form_changed()) {
		return confirm ('Are you sure you want to cancel without saving your changes?');
	}
	return true;
}

function _isThereTranslation (str) {
  if (typeof window['req_field_translation'] != 'undefined') {
  	return ((typeof (window.req_field_translation[str]) != 'undefined') ? window.req_field_translation[str] : str);
  } else {
  	return str;
  }
}

function dcSubmit(myform) {
//  	$.getJSON('', function(data) {
//	    req_field_translation = data;
//	});
	req_field_str = _isThereTranslation('is a required field');
	return (verifyObjects(myform) && convertObjects(myform) && dcOneSubmit());
}

function convertObjects(myform) {
	return traverseForm(myform, "convert");
}

function verifyObjects(myform) {
	return traverseForm(myform, "verify");
}

function traverseForm(myform, traverseAction) {
		var traverseElements;
			
		if (use_fastvalidate == true && traverseAction != "convert") {
			if (fastvalidate_array == null) {
				return true;
			}
			traverseElements = fastvalidate_array;
		} else {
			traverseElements = myform.elements;
		}
		
		var len = traverseElements.length;

		for (z = 0; z < len; z++) {
			var check_element = traverseElements[z];
		
	        var myname  = check_element.name;
	        var myvalue = check_element.value;
			if (myname != myform.name) {
       		   if(check_element.type == "hidden" && myname.substring(0,8) == "uweinfo_") {
						formInfo[myname.substring(8,myname.length)] = myvalue;
            	}
				else if (isDefined(formInfo[myname])) {
			         var validationRule = formInfo[myname];
					 if (traverseAction == "verify") {
						 if (VerifyObject (myform, check_element, validationRule) == false) {
						 	return false;
						 }
					 } else if (traverseAction == "convert") {
						 if (ConvertObject (myform, check_element, validationRule) == false) {
						 	return false;
						 }
					}
		        }
		   }
		}
		
		return true;
}

function VerifyPassword(password1, password1PrettyName, password2, password2PrettyName) {
	if (password1.value != password2.value) {
		_showMessage (password1, password1PrettyName+" differs from "+password2PrettyName);
		return false;
	}
	if (password1.value == "") {
		_showMessage (password1, password1PrettyName+" cannot be empty!");
		return false;
	}
	return true;
}

function VerifyObject(myform, field, validationRule) {
	validationRule = validationRule.replace('&nbsp;', ' ');
	var resultArray = _parseString(validationRule, ";");
	if (resultArray[0] == "required") {
		return VerifyRequired(field, resultArray[1], resultArray[3], resultArray[4]);
	} else if (resultArray[0] == "currency") {
		return VerifyNumber(field, resultArray[1], resultArray[2], null, null, resultArray[3], resultArray[4], true);
	} else if (resultArray[0] == "number") {
		return VerifyNumber(field, resultArray[1], resultArray[2], resultArray[3], resultArray[4], resultArray[5], resultArray[6]);
	} else if (resultArray[0] == "double") {
		return VerifyNumber(field, resultArray[1], resultArray[2], resultArray[3], resultArray[4], resultArray[5], resultArray[6]);
	} else if (resultArray[0] == "date") {
		return VerifyDate(field, resultArray[1], resultArray[2]);
	} else if (resultArray[0] == "timestamp") {
		return VerifyTimestamp(field, resultArray[1], resultArray[2]);
	} else if (resultArray[0] == "phone") {
		return VerifyPhoneNumber(field, resultArray[1], resultArray[2]);
	} else if (resultArray[0] == "email") {
		return VerifyEmail(field, resultArray[1], resultArray[2]);
	} else if (resultArray[0] == "username") {
		return VerifyFormUsername(field, resultArray[1], resultArray[2]);
	} else if (resultArray[0] == "password") {
		return VerifyFormPassword(myform, field, resultArray[1], resultArray[2]);
	} else if (resultArray[0] == "none") {
		return true;
	} else if (resultArray[0] == "file") {
	  return VerifyFileUpload(field, resultArray[1],resultArray[2]);
	} else {
		alert("Illegal validation rule found of type: ["+resultArray[0]+"] in rule: ["+validationRule+"] for ["+myform.name+"."+field.name+"]");
	}
}

function VerifyFileUpload(field, fi_name, upfield) {
       if (! fi_name in formInfo) return true;

       var resultArray = _parseString(formInfo[fi_name], ";");

       // check file
       if (field.value != "") {
               return true;
       }

       // check upfield
       var up_field = document.getElementById(upfield);

       if (up_field.value != "") {
               return true;
       }

       alert("File for " + resultArray[1] + " is required!");

       return false;
}


function ConvertObject(myform, field, validationRule) {
	var resultArray = _parseString(validationRule, ";");
	if (resultArray[0] == "date") {
		return ConvertDate(field, false);
	} else if (resultArray[0] == "timestamp") {
		return ConvertDate(field, true);	
	} else if (resultArray[0] == "currency") {
		return ConvertCurrency(field);
	} else if (resultArray[0] == "none") {
		return true;
	}
}

function ConvertCurrency(currencyField) {
	var convertvalue = _ConvertCurrency(currencyField);
	if (convertvalue == null) {
		return false;
	}
	currencyField.value = convertvalue;
	return true;
}

function _ConvertCurrency(currencyField) {
	var checkStr = _trim (currencyField.value);
	if (checkStr == "") {
		currencyField.value = "";
		return checkStr;
	}
	// remove any currency indicators
	checkStr = _replaceString (checkStr, "$", "");
	checkStr = _replaceString (checkStr, ",", "");
// if there is no decimal, add one otherwise make sure it is valid
	var decimalLocation = checkStr.indexOf ('.');
	if (decimalLocation == -1) {
		checkStr += ".00";
	} else {
		var decimalValue = checkStr.substring (decimalLocation+1);
		if (_verifyLength (decimalValue, 2, 2) == false) {
			_showMessage (field, prettyFieldName+" is not a valid currency value, it must have exactly two digits following the decimal place.");
			return null;
		}
	}
	return checkStr;
}

function ConvertDate(dateField, hasTime) {
	var convertvalue = _ConvertDate(dateField, hasTime);
	if (convertvalue == null) {
		return false;
	}
	dateField.value = convertvalue;
	return true;	
}

function _ConvertDate(dateField, hasTime) {
	var checkStr = _trim (dateField.value);
	if (checkStr == "" || checkStr.substring(0,10) == "mm-dd-yyyy") {
		dateField.value = "";
		return checkStr;
	}

	var dayStarts = checkStr.indexOf ('-');
	var yearStarts = checkStr.indexOf ('-', dayStarts+1);
	if (dayStarts == -1 || yearStarts == -1) {
		return null;
	}

	var yearValue = checkStr.substring (yearStarts+1,yearStarts+5);
	var monthValue = checkStr.substring (0,dayStarts);
	var dayValue = checkStr.substring (dayStarts+1,yearStarts);
	
	if (_isValidNumber (dayValue, 1, 2) == false || _isValidNumber (monthValue, 1, 2) == false || _isValidNumber (yearValue, 4, 4) == false || monthValue < 1 || monthValue > 12 || dayValue < 1 || dayValue > 31) {
		return null;
	}
	
	var dateString = yearValue + "/" + monthValue + "/" + dayValue;

	if (hasTime) {
		var hourStarts = checkStr.indexOf (' ', yearStarts);
		for (i = hourStarts; i < checkStr.length; i++) {
			if (checkStr.charAt(i) == ' ') {
				hourStarts = i;
			} else {
				break;
			}
		}

		var minuteStarts = checkStr.indexOf (':', minuteStarts);
		var ampmStarts = checkStr.indexOf ('am', ampmStarts);
		if (ampmStarts == -1) {
			ampmStarts = checkStr.indexOf ('AM', ampmStarts);
		}
		if (ampmStarts == -1) {
			ampmStarts = checkStr.indexOf ('pm', ampmStarts);
		}
		if (ampmStarts == -1) {
			ampmStarts = checkStr.indexOf ('PM', ampmStarts);
		}		
		if (hourStarts == -1 || minuteStarts == -1 || ampmStarts == -1) {
			return null;
		}
		
		var hourValue = new Number(checkStr.substring (hourStarts+1,minuteStarts));
		var minuteValue = checkStr.substring (minuteStarts+1,ampmStarts);
		var ampmValue = checkStr.substring (ampmStarts).toLowerCase();
		
		if (_isValidNumber (hourValue, 1, 2) == false || _isValidNumber (minuteValue, 2, 2) == false || (ampmValue != "am" && ampmValue != "pm" && ampmValue != "AM" && ampmValue != "PM") || hourValue > 12 || hourValue < 1 || minuteValue > 60 || minuteValue < 0) {
			return null;
		}
		
		if ((ampmValue == "pm" || ampmValue == "PM") && hourValue != 12) {
			hourValue += 12;
		}
		
		if ((ampmValue == "am" || ampmValue == "AM") && hourValue == 12) {
			hourValue = 0;
		}
		
		dateString += " " + hourValue + ":" + minuteValue;
	}

	return dateString;	
}

function VerifyRequired(field, prettyFieldName, minLength, maxLength) {
	if (_emptyField(field)) {
		_showMessage (field, prettyFieldName+" "+req_field_str);
		return false;
	}
	if (_verifyLength (_trim(field.value), minLength, maxLength) == false) {
		if (minLength == maxLength) {
			_showMessage (field, prettyFieldName+" is "+field.value.length+_plural(" character",field.value.length)+" in length.  The value in this field must be exactly "+minLength+_plural(" character", minLength)+" in length.");
		} else {
			_showMessage (field, prettyFieldName+" is "+field.value.length+_plural(" character",field.value.length)+" in length.  The value in this field must be at least "+minLength+_plural(" character", minLength)+" long, but not longer than "+maxLength+_plural(" character", maxLength)+".");
		}
		return false;
	}
	return true;
}

function VerifyFormUsername(field, prettyFieldName, isRequired) {
	if (isDefined (isRequired) && (isRequired == "true" || isRequired == true) && _emptyField (field)) {
		_showMessage (field, prettyFieldName+" "+req_field_str);
		return false;
	} else if (field.value == "") {
		return true;
	}

	var checkStr = _trim (field.value);
	checkStr = checkStr.toLowerCase();	
	checkStr = _replaceString (checkStr, " ", "");	
	checkStr = checkStr.replace (/[^_a-z0-9.\@-]+/ig, '');
	
	field.value = checkStr;
	
	return true;
}

function VerifyFormPassword(myform, field, prettyFieldName, isRequired) {
	if (isDefined (isRequired) && (isRequired == "true" || isRequired == true) && _emptyField (field)) {
		_showMessage (field, prettyFieldName+" "+req_field_str);
		return false;
	} else if (field.value == "") {
		return true;
	}
	// first we need to find the other password field then verify they match
	var secondpassword = null;
	var secondpasswordPrettyName = null

    var len = myform.elements.length;
	for (z = 0; z < len; z++) {
	      var myname  = myform.elements[z].name;
	      var myvalue = myform.elements[z].value;
		  if (myname != myform.name && myname != field.name) {
               if(myform.elements[z].type == "hidden") {
			       if (myname.substring(0,8) == "uweinfo_") {
						formInfo[myname.substring(8,myname.length)] = myvalue;
		       	   }
            	} else if (formInfo[myname] != undefined) {
					var resultArray = _parseString(formInfo[myname], ";");
					if (resultArray[0] == "password") {
						secondpassword = myform.elements[z];
						secondpasswordPrettyName = resultArray[1];
						break;
					}
		        }
		   }
		}

	if (secondpassword == null) {
		alert ('Form does not contain a validation password');
		return false;
	}

	return VerifyPassword (field, prettyFieldName, secondpassword, secondpasswordPrettyName);
}

function VerifyNumber(field, prettyFieldName, isRequired, minLength, maxLength, minValue, maxValue, isCurrency) {
	if (isDefined (isRequired) && (isRequired == "true" || isRequired == true) && _emptyField (field)) {
		_showMessage (field, prettyFieldName+" "+req_field_str);
		return false;
	} else if (field.value == "" || field.value == "NULL") {
		return true;
	}
	var checkStr = _trim (field.value);
	checkStr = _replaceString (checkStr, ",", "");	
	if (_verifyLength (_trim(field.value), minLength, maxLength) == false) {
		if (minLength == maxLength) {
			_showMessage (field, prettyFieldName+" is "+field.value.length+_plural(" digit",field.value.length)+" in length.  The value in this field must be exactly "+minLength+_plural(" digit", minLength)+" in length.");
		} else {
			_showMessage (field, prettyFieldName+" is "+field.value.length+_plural(" digit",field.value.length)+" in length.  The value in this field must be at least "+minLength+_plural(" digit", minLength)+" long, but not longer than "+maxLength+_plural(" digit", maxLength)+".");
		}
		return false;
	}
	if (isCurrency) {
		// remove any currency indicators
		checkStr = _replaceString (checkStr, "$", "");
		// if there is no decimal, add one otherwise make sure it is valid
		var decimalLocation = checkStr.indexOf ('.');
		if (decimalLocation == -1) {
			checkStr += ".00";
		} else {
			var decimalValue = checkStr.substring (decimalLocation+1);
			if (_verifyLength (decimalValue, 2, 2) == false) {
				_showMessage (field, prettyFieldName+" is not a valid currency value, it must have exactly two digits following the decimal place.");
				return false;
			}
		}
	}
	if (isNaN (checkStr)) {
		_showMessage (field, prettyFieldName+" is not a valid number.  Please enter a valid number.");
		return false;
	}
	if (isDefined (minValue) && new Number(checkStr) < new Number(minValue)) {
		_showMessage (field, prettyFieldName+" is too small, it must be a number between "+minValue+" and "+maxValue+".");
		return false;
	}
	if (isDefined (maxValue) && new Number(checkStr) > new Number(maxValue)) {
		_showMessage (field, prettyFieldName+" is too big, it must be a number between "+minValue+" and "+maxValue+".");
		return false;
	}

	field.value = checkStr;
	return true;
}

function VerifyTimestamp(timestampField, prettyFieldName, isRequired) {
	if (isDefined (isRequired) && (isRequired == "true" || isRequired == true) && VerifyRequired (timestampField, prettyFieldName) == false) {
		return false;
	}
	var checkStr = _trim (timestampField.value);

	if (isDefined (isRequired) && (isRequired == "true" || isRequired == true) && checkStr.substring(0,10) == "mm-dd-yyyy") {
		_showMessage (field, prettyFieldName+" "+req_field_str);
	}

	if (checkStr == "" || checkStr.substring(0,10) == "mm-dd-yyyy") {
		timestampField.value = "";
		return true;
	}
	if (_verifyDate (timestampField, checkStr, true) == false) {
		message = prettyFieldName + " is not a valid date and time value.  Please enter a date and time in the format mm-dd-yyyy hh:mmam or mm-dd-yyyy hh:mmpm.";
		_showMessage (timestampField, message);
		return false;
	}
	return true;
}


function VerifyDate(dateField, prettyFieldName, isRequired) {
	if (isDefined (isRequired) && (isRequired == "true" || isRequired == true) && VerifyRequired (dateField, prettyFieldName) == false) {
		return false;
	}

	var checkStr = _trim (dateField.value);

	if (isDefined (isRequired) && (isRequired == "true" || isRequired == true) && checkStr.substring(0,10) == "mm-dd-yyyy") {
		_showMessage (field, prettyFieldName+" "+req_field_str);
		return false;
	}

	if (checkStr == "" || checkStr.substring(0,10) == "mm-dd-yyyy") {
		dateField.value = "";
		return true;
	}
	if (_verifyDate (dateField, checkStr, false) == false) {
		message = prettyFieldName + " is not a valid date.  Please enter a date in the format mm-dd-yyyy.";
		_showMessage (dateField, message);
		return false;
	}
	return true;
}

function _verifyDate(dateField, checkStr, hasTime){
	// accept this format: mm/dd/yyyy
	// but convert to mm-dd-yyyy
	if ( checkStr.indexOf ('/') != -1) {
		checkStr = _replaceString (checkStr, "/", "-");
	}

	// if we find this yyyy/mm/dd convert it as well
	if (checkStr.indexOf('-') == 4) {
		var monthStarts = checkStr.indexOf ('-');
		var dayStarts = checkStr.indexOf ('-', monthStarts+1);
        if (dayStarts == -1 || monthStarts == -1)
			return false;

		var hourStarts = checkStr.indexOf (' ', dayStarts+1);
		var yearValue = checkStr.substring (0,4);
		var monthValue = checkStr.substring (5,dayStarts);
		
		if (hourStarts == -1) {
			var dayValue = checkStr.substring (dayStarts+1);
		} else {
        	var dayValue = checkStr.substring (dayStarts+1,hourStarts);
		}
	} else {
		var dayStarts = checkStr.indexOf ('-');
		var yearStarts = checkStr.indexOf ('-', dayStarts+1);
		if (dayStarts == -1 || yearStarts == -1)
			return false;

	        var yearValue = checkStr.substring (yearStarts+1,yearStarts+5);
        	var monthValue = checkStr.substring (0,dayStarts);
        	var dayValue = checkStr.substring (dayStarts+1,yearStarts);
	}
	
	if (_isValidNumber (dayValue, 1, 2) == false || _isValidNumber (monthValue, 1, 2) == false || _isValidNumber (yearValue, 4, 4) == false || monthValue < 1 || monthValue > 12 || dayValue < 1 || dayValue > 31) {
		return false;
	}
	
	var dateString = monthValue + "-" + dayValue + "-" + yearValue;
	
	if (hasTime && checkStr.length == Number(yearValue.length) + Number(monthValue.length) + Number(dayValue.length) + 2) {
		dateString = monthValue + "-" + dayValue + "-" + yearValue + " 12:00am";
	} else if (hasTime) {
		var lowerCheck = checkStr.toLowerCase();
		var hourStarts = checkStr.indexOf (' ', yearStarts);
		for (i = hourStarts; i < checkStr.length; i++) {
			if (checkStr.charAt(i) == ' ') {
				hourStarts = i;
			} else {
				break;
			}
		}

		var minuteStarts = checkStr.indexOf (':', hourStarts);
		var ampmStarts = lowerCheck.indexOf ('am', minuteStarts);
		if (ampmStarts == -1) {
			ampmStarts = lowerCheck.indexOf ('pm', minuteStarts);
		}
		
		if (hourStarts == -1 || minuteStarts == -1) {
			return false;
		}
		
		var hourValue = checkStr.substring (hourStarts+1,minuteStarts);
		var minuteValue = checkStr.substring (minuteStarts+1,minuteStarts+3);
		var ampmValue;
		
		if (ampmStarts != -1) {
			ampmValue = checkStr.substring (ampmStarts).toLowerCase();
		} else {
			// we are going to assume it is 24 hour time and convert to the proper am and pm values
			if (hourValue == 12) {
				ampmValue = 'pm';
			} else if (hourValue > 12) {
				hourValue = hourValue - 12;
				ampmValue = 'pm';
			} else {
				ampmValue = 'am';			
			}
		}

		if (_isValidNumber (hourValue, 1, 2) == false || _isValidNumber (minuteValue, 2, 2) == false || (ampmValue != "am" && ampmValue != "pm" && ampmValue != "AM" && ampmValue != "PM") || hourValue > 12 || hourValue < 1 || minuteValue > 60 || minuteValue < 0) {
			return false;
		}
		
		dateString += " " + hourValue + ":" + minuteValue + ampmValue;
	}
	
	dateField.value = dateString;
	return true;
}

function VerifyPhoneNumber(phoneField, prettyFieldName, isRequired) {
	if (isDefined (isRequired) && (isRequired == true || isRequired == "true") && VerifyRequired (phoneField, prettyFieldName) == false) {
		return false;
	}
	var checkStr = _trim (phoneField.value);
	if (checkStr == "") {
		phoneField.value = "";
		return true;
	}
	if (_verifyPhoneNumber (phoneField, checkStr) == false) {
		message = prettyFieldName + " is not a valid phone number.  Please enter a valid 10-digit phone number in the format xxx-xxx-xxx.  For international phone numbers please enter + and the number, for example: +44-(0)1224-XXXX-XXXX";
		_showMessage (phoneField, message);
		return false;
	}
	// _verifyPhoneNumber has already changed the value in the field
	return true;
}

function _verifyPhoneNumber(phoneField, checkStr){
	if ( checkStr.charAt(0) == '+' ) {
		// accept any format for international numbers
	} else if ( checkStr.charAt(0) == '(' ) {
		// accept this format: (512) 555-1212 or (512)555-1212
		// but convert to 512-555-1212

		if ( checkStr.length < 13 ) {
			return false;
		}
		if (_isValidNumber (checkStr.substring (1,4), 3, 3) == false) {
			return false;
		}
		var convertedPhone = checkStr.substring (1,4) + "-";
		var remainingPhone = _trim(checkStr.substring(5));	
		if (_isValidNumber (remainingPhone.substring (0,3), 3, 3) == false) {
			return false;
		}	
		convertedPhone += remainingPhone.substring (0,3) + "-";
		if (_isValidNumber (remainingPhone.substring (4,8), 4, 4) == false) {
			return false;
		}	
		convertedPhone += remainingPhone.substring (4,8);
		phoneField.value = convertedPhone;
	} else if ( checkStr.length == 10) {
		if (_isValidNumber (checkStr.substring (0,3), 3, 3) == false) {
			return false;
		}
		var convertedPhone = checkStr.substring (0,3) + "-";
		if (_isValidNumber (checkStr.substring (3,6), 3, 3) == false) {
			return false;
		}	
		convertedPhone += checkStr.substring (3,6) + "-";
		if (_isValidNumber (checkStr.substring (6), 4, 4) == false) {
			return false;
		}	
		convertedPhone += checkStr.substring (6);
		phoneField.value = convertedPhone;			
	} else {
		// check this format 512-555-1212
		// and convert these formats 512.xxx.xxx or 512 xxx-1212 etc to
		// the proper format
		// also convert 512.555.1212 to 512-555-1212
		// and 512 555 1212 to 512-555-1212
		if ( checkStr.length < 12 ) {
			return false;
		}
		if (_isValidNumber (checkStr.substring (0,3), 3, 3) == false || _isValidNumber (checkStr.substring (4,7), 3, 3) == false || _isValidNumber (checkStr.substring (8), 4, 4) == false) {
			return false;
		}
		phoneField.value = checkStr.substring (0,3) + "-" + checkStr.substring (4,7) + "-" + checkStr.substring (8);
	}
	return true;
}

function VerifyEmail(emailField, prettyFieldName, isRequired)
{
	if (isDefined (isRequired) && (isRequired == true || isRequired == "true") && VerifyRequired (emailField, prettyFieldName) == false) {
		return false;
	}
	var checkStr = _trim(emailField.value);
	emailField.value = checkStr;
    
        if (checkStr == '$in[cartemail]') {
           return true;
        }
	
	if (emailField.value == "") {
		return true;
	}

	var Reason  = "Incorrect Email Address! Please click OK and re-enter a valid email address.\n\nReason:"
	var ix = (checkStr.length - 4)
	var RC = true;
	var x = BackQuote = AtSignValid = DoublePeriod = PeriodValid = SpaceValid = ExtValid = RL = 0;

	for (i = 0;  i < checkStr.length;  i++) {
		if ( checkStr.charAt(i) == '@' ) AtSignValid++;
		else if ( checkStr.charAt(i) == '`' ) BackQuote++;
		else if ( checkStr.charAt(i) == '.' ) {
			if ( x > 1 && x == (i-1) ) DoublePeriod++;
			else {
				x = i;
				PeriodValid++;
			}
		} else if ( checkStr.charAt(i) == ' ' )
			SpaceValid ++;
	}
	ExtValid++;

	RL = Reason.length;
	if ( AtSignValid != 1 )			 Reason += "\nOnly one '@' allowed, " + AtSignValid + " found.";
	if ( PeriodValid == 0 )			 Reason += "\nAddress must contain at least one period.";
	if ( SpaceValid > 0 )			 Reason += "\nNo Spaces allowed. Address contains " + SpaceValid + "space";
	if ( SpaceValid > 1 )			 Reason += "s.";
	if ( DoublePeriod > 0 )			 Reason += "\nAddress contains multiple periods in a row.";
	if ( ExtValid == 0 )			 Reason += "\nInvalid Domain Suffix entered. If you have an unverifiable suffix that is valid, email us your info at \n(Valid: .com, .edu, .net, .org, .gov, .de, .uk)";
	if ( checkStr.length > 120 )	 Reason += "\nPlease limit the Email Address to 120 characters.";
	if ( BackQuote > 0)				 Reason += "\nYour email address contains an invalid characters.";

	if ( RL != Reason.length ) {
		confirm(Reason); emailField.focus(); RC = false;
	} else
		RC = true;
	return RC;
}

function _trim(chkString) {
	var startString = 0;
	var endString = 0;
	var startFound = false;
	for (i = 0; i < chkString.length; i++) {
		if (chkString.charAt(i) == ' ') {
			if (startFound == false) {
				startString = i + 1;
			}
		} else {
			startFound = true;
			endString = i;
		}
	}
	if (chkString.substring (startString, endString+1) == ' ') {
		return '';
	}
	return chkString.substring (startString, endString+1);	
}

function _getendtrim(chkString) {
	for (i = chkString.length-1; i >= 0; i--) {
		if (chkString.charAt(i) != ' ') {
			return chkString.substring(i+1);
		}
	}
	return '';
}

function _getstarttrim(chkString) {
	for (i = 0; i < chkString.length; i++) {
		if (chkString.charAt(i) != ' ') {
			return chkString.substring(0,i);
		}
	}
	return '';
}

function _isValidNumber(numberString, minLength, maxLength) {
	if (isNaN (numberString) || _verifyLength (numberString, minLength, maxLength) == false) {
		return false;
	}
	return true;
}

function _verifyLength(checkStr, minLength, maxLength) {
	if (isDefined (minLength) && checkStr.length < minLength || isDefined (maxLength) && checkStr.length > maxLength) {
		return false;
	}
	return true;
}

function _getRadiogroupValue(radiogroup) {
	var useradio = document.getElementsByName(radiogroup.name);
	for (i = 0; i < useradio.length; i++) {
		if (useradio[i].checked) {
			return useradio[i].value;
		}
	}
	return null;
}

function _emptyField(field) {
	var verify_value;
	
	if(field.type=='radio') {
		verify_value = _getRadiogroupValue(field);
	} else if (field.type=='checkbox') {
		verify_value = field.checked == false ? null : field.value;
	} else {
		verify_value = field.value;
	}
	
	return(!field || verify_value == null || _trim(verify_value) == "") ? true : false;
}

function _showMessage (field, message) {
		alert (message);
		if (field.type != "hidden") {
			field.focus();
		}
}

function isDefined(value) {
	if (typeof value == "undefined" || value == "" || value == null) {
		return false;
	}
	return true;
}

function _quoteString (originalString) {
	var newString = "";
	for (i = 0; i < originalString.length; i++) {
		var mychar = originalString.charAt(i);
		if (mychar == '\'') {
			newString += "\\\'";
		} else {
			newString += mychar;
		}
	}
	return newString;
}

function _replaceString (replaceString, findString, replaceWithString) {
	var location = replaceString.indexOf (findString);
	while (location != -1) {
		replaceString = replaceString.substring (0, location) + replaceWithString + _replaceString (replaceString.substring(location+findString.length), findString, replaceWithString);
		location = replaceString.indexOf (findString);
	}
	return replaceString;
}

function _parseString (stringToParse, separator) {
	var currentResultIndex = 0;
	var resultArray = new Array();
	var currentResult = "";
	for (i = 0; i < stringToParse.length; i++) {
		var currentChar = stringToParse.charAt(i);
		if (currentChar == separator) {
			resultArray[currentResultIndex] = currentResult;
			currentResult = "";
			currentResultIndex++;
		} else {
			currentResult = currentResult + "" + currentChar;
		}
	}
	resultArray[currentResultIndex] = currentResult;
	return resultArray;
}

function _plural (originalString, numberToCheck) {
	if (numberToCheck == 1) {
		return originalString;
	} else {
		return originalString+"s";	
	}
}

var dcSubmissions = 1;
function dcOneSubmit() {
	if (dcSubmissions > 0) {
		dcSubmissions -= 1;
		return true;
	}
	else {
		return false;
	}
}

function goFormFirstField(myform) {
    var len = myform.elements.length;
	for (z = 0; z < len; z++) {
		if((myform.elements[z].type == "text" || myform.elements[z].type == "image")&& myform.elements[z].disabled == false && myform.elements[z].name != 'list_find_key') {
			myform.elements[z].focus();
			return true;
	   }
	   
		if(myform.elements[z].type == "select-one") {
			return true;
		}
	}
	return false;
}

function goFirstField() {
	for (i = 0; i < document.forms.length; i++) {
		var myform = document.forms[i];
		if (goFormFirstField(myform) == true) {
			return true;
		}
	}
	return false;
}

function forceMinimumAmount(field,minvalue,maxvalue) {
   return VerifyNumber(field, "Amount", true, 0, 0, minvalue, maxvalue, true);
}

