// --------------------------------------
// Validation.js start
// --------------------------------------

// **** OBSOLETE ****  Use IsNull or IsNullShowAlertSetFocus
function CheckForNull (src, errMsg)
{
	//assumes 'src' is an object or a string
	var str;
	var bSetFocus;
	
	if (typeof(src) == "object") {
		str = src.value;
		bSetFocus = true;
	} else {
		str = src;
		bSetFocus = false;
	}
	
	if (str.length == 0) {
		alert(errMsg);
		if (bSetFocus) {
			src.focus();
		}			
		return false;
	}	

	return true;
}

function IsNull (src)
{
	//assumes 'src' is an object or a string
	var str;
	
	if (typeof(src) == "object") {
		str = src.value;
	} else {
		str = src;
	}
	
	if (str.length == 0) {
		return false;
	}	

	return true;
}

function IsNullShowAlertSetFocus (src, alertMsg, setFocus)
{
	//assumes 'src' is an object or a string
	var str;
	
	if (typeof(src) == "object") {
		str = src.value;
	} else {
		str = src;
		setFocus = false;
	}
	
	if (str.length == 0) {
		alert(alertMsg);
		if (setFocus) {
			src.focus();
		}			
		return true;
	}	

	return false;
}

function GenIsValidEmail(strEmail, strFldName)
{
	// validate email address
	
	//var re = /^\w+@\w+(\.\w+)+$/;
	var re = /^[A-Za-z0-9_.\-]+@[A-Za-z0-9_\-]+(\.[A-Za-z0-9_\-]+)+$/;
	
	if (strEmail.length <= 0)
	{
		if (strFldName.length <= 0)
			alert('Please enter your E-mail address.');
		else
			alert(strFldName + ' cannot be blank.');
		return false;
	}
		
	if (!re.test(strEmail))
	//if (!((strEmail.indexOf("@") != -1) && (strEmail.indexOf(".") != -1)))
	{
		if (strFldName.length <= 0)
			alert('The E-mail address you specified is not valid.');
		else
			alert(strFldName + ' is not a valid E-mail address.');
		return false;
	}

	return true;	
}

// **** OBSOLETE ****  Use IsValidPhoneFormat
function IsValidPhone(control, message)
{
	//create regular expression
	var re = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
	
	//check for empty text box
	if (control.value.length <= 0)
	{
		return true;
	}	
	
	//test against reg expression
	if (!re.test(control.value))
	{
		alert(message + "  Phone numbers must be in the following format: (614) 123-4567");
		control.focus();
		return false;
	}
	
	//return
	return true;	
}

function IsValidPhoneFormat(control, ctrlName, setFocus)
{
	//create regular expression
	var re = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
	
	//check for empty text box
	if (control.value.length <= 0)
	{
		return true;
	}	
	
	//test against reg expression
	if (!re.test(control.value))
	{
		if (ctrlName.Length == 0) {
		  alert("Phone numbers must be in the following format: (614) 123-4567");
    } else {
		  alert(ctrlName + " must be in the following format: (614) 123-4567");
		}
		if (setFocus) {
			src.focus();
		}			
		return false;
	}
	
	return true;	
}

function checkPhone(strPhone) {

	//strip all non-numeric characters
	var i;
	var newPhone = '';	
	var c = '';
	for (i=0;i<strPhone.length;i++) {
		c=strPhone.charAt(i);
		if (!isNaN(c) && c != ' ') {
			newPhone+=c;	
		}
	}

	// Check to make sure there are at least 10 characters entered
	if (newPhone.length < 10) {
		return false;
	} 
	else {
		return true;
	}
}

//should be used after a call to checkPhone returns true
function formatPhone(strPhone) {
	//strip all non-numeric characters
	var i;
	var newPhone = '';	
	var c = '';
	
	for (i=0;i<strPhone.length;i++) {
		c=strPhone.charAt(i);
		if (!isNaN(c) && c != ' ') {
			newPhone+=c;	
		}
	}

	//if the phone number is greater than 10 characters, just return the original value
	if (newPhone.length > 10) {
		return strPhone;
	}
	
	// Reformat the phone number
	newPhone = "(" + newPhone.substring(0,3) + ") " + newPhone.substring(3,6) + "-" + newPhone.substring(6,10) ;
	
	return newPhone;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
	var c = s.charAt(i);
	if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
// --------------------------------------
// Validation.js end
// --------------------------------------
