﻿// Override ASP.NET's ValidatorUpdateDisplay method
function NewValidatorUpdateDisplay(val) {
	if (typeof(val.display) == "string") {
		if (val.display == "None") { return; }

		if (val.display == "Dynamic") {
			// Check to see if there is a block style defined
			var displayType = "inline";
			switch(val.styleDisplay) {
				case "block" : { displayType = "block"; }
			}
			// Set the display type based on validity
			val.style.display = val.isvalid ? "none" : displayType;
			
			// Find parent row
			var tr = FindParentTag(val, "tr");
			if(tr != null) {
				// Add row class if invalid, but don't replace classes already there
				if(tr.className==null || tr.className.length<=0) {
					tr.className = (val.isvalid) ? "" : "validationRow";
				} else {
					// Existing classes - add or remove
					if(val.isvalid) {
						tr.className = tr.className.replace("validationRow", "");
					} else {
						tr.className = tr.className + " validationRow";
					}
				}
			}
			
			return;
		}
	}
	val.style.visibility = val.isvalid ? "hidden" : "visible";
}

var OldValidatorUpdateDisplay = null;
if(typeof(ValidatorUpdateDisplay) != "undefined") {
	OldValidatorUpdateDisplay = ValidatorUpdateDisplay;
	ValidatorUpdateDisplay = NewValidatorUpdateDisplay;
}


function FindParentTag(element, tagName) {
	if((element==null) || 
		(tagName==null) ||
		(element.parentNode == null) ||
		(element.parentNode.tagName == null)
		) { 
		return null; 
	}
	if(element.parentNode.tagName.toLowerCase() == tagName.toLowerCase()) {
		// Parent tag match
		return element.parentNode;
	} else {
		return FindParentTag(element.parentNode, tagName);
	}
	
}
function HighlightRow(clientID) {
	var validator = document.getElementById(clientID);
	if(validator != null) {
		var tr = FindParentTag(validator, "tr");
		if(tr != null) {
			tr.className = (tr.className==null || tr.className=="") ? "validationRow" : "validationRow";
		}
	}
}


function IsDigit(s) {
	// look at each character to see if it is 0-9
	for(var i=0; i<s.length; i++) {
		var c = s.substring(i,i+1);
      if(!(c>="0" && c<="9")) {
			return false; // encountered non-numeric character
		}
	}
	return true; // valid number
}
function GetDigits(s) {
	// create only a digit string (remove commas or periods)
	var digits = "";
	for(var i=0; i<s.length; i++) {
		var c = s.charAt(i);
		if(IsDigit(c)) {
			digits = digits + c; // add the character
		}
	}
	return digits;
}
function InvalidateControl(sender, args, message, example) {
	var errorText = message;
	var errorHTML = message;
	if(example!=null && example.length > 0) { 
		errorText += (" " + example);
		errorHTML += (" <div class=\"example\">" + example + "</div>");
	}
	sender.errormessage = errorText;
	args.IsValid = false;
	sender.innerText = sender.errormessage;
	sender.innerHTML = errorHTML;
}

function IsWhiteSpace(ch) {
	// return the same value, if not a string
	if( (typeof(ch) != "string") || (ch==null) ) { return false; }
	if(ch.length <= 0) { return false; }
	return( (ch.charCodeAt(0) <= 32) || (ch.charCodeAt(0) == 177) );
}
function Trim(str) {
	// return the same value, if not a string
	if( (typeof(str) != "string") || (str==null) ) { return str; }
	// only trim strings with a length
	if(str.length <= 0) { return str; }
	// Remove all preceding whitespace
	if(IsWhiteSpace(str.charAt(0))) {
		return Trim(str.slice(0));
	}
}

function ValidateEmail(sender, args) {
	var emailPattern = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if(! emailPattern.test(args.Value)) {
		InvalidateControl(sender, args, "Please enter a valid email address", "(ex. name@domain.com)");	
	} 
}

function ValidatePhoneNumber(sender, args) {
	var digits = GetDigits(args.Value);
	if(digits.length != 10) {
		// Invalid length
		InvalidateControl(sender, args, "Please enter a valid 10-digit phone number", "(ex. 123-456-7890)");
	} else {
		// Valid length
		// Make sure that it's not a repeated digit string (e.g. 111-111-1111)
		var allSameDigits = true;
		for(var i=0; i<digits.length; i++) {
			if(digits.substr(0,1)!=digits.substr(i,1)) {	
				allSameDigits = false; 
				break; 
			}
		}
		if(allSameDigits) {
			InvalidateControl(sender, args, "Phone number cannot be one digit repeated ten times.");
		}
	}
	return args.IsValid
}

function ValidateZipCode(sender, args) {
	var digits = GetDigits(args.Value);
	if((digits.length != 5) && (digits.length != 9)) {
		InvalidateControl(sender, args, "Please enter a valid 5 or 9-digit zip code", "(ex. 12345-6789)");
	}
	return args.IsValid
}