function GetRef(id) {
	return (document.all) ? document.all[id] : (document.getElementById) ? document.getElementById(id) : null;
}

function ShowHide(id, show) {
	var e = GetRef(id);
	if (e == null) return;
	var display = (show) ? "block" : "none";
	e.style.display = display;
}

function ShowHideWithStyle(id, show, style) {
	var e = GetRef(id);
	if (e == null) return;
	var display = (show) ? style : "none";
	e.style.display = display;
}


/******
	Validation helper functions
 ******/
function IsValidRadio(r) {
	if (r == null) return false;
	
	for (var i=0; i<r.length; i++) {
	    if (r[i].checked) return true;
	}
	
	return false;
}

function IsValidCheckbox(c) {
	if (c == null) return false;
	if (c.form[c.id][0] == undefined) {
		return c.checked;
	} else {
		return IsValidRadio(c);
	}
	
}

function IsValidSelect(s, firstVal) {
	if (s == null) return false;
	
	var valid = false;
	var firstValidIndex = (firstVal) ? 0 : 1;
	
	if (s.selectedIndex >= firstValidIndex) {
		if (IsValidString(s.options[s.selectedIndex].value)) {
			valid = true;
		} 
	}
	
	return valid; 
}

function IsValidTextArea(t) {
	if (t == null) return false;
	return IsValidStringMultiLine(t.value);
}

function IsValidEmail(e) {
	var sPattern = /^[a-z0-9]([a-z0-9_\-\.]*)@([a-z0-9_\-\.]*)(\.[a-z]{2,3}(\.[a-z]{2}){0,2})$/i;
	//var sPattern = /\b[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b/i;
	return IsValid(sPattern, e);
}

function IsValidPostalCode(p) {
	var sPattern = /^[a-ceghj-npr-tvxy]\d[a-z](\s)?\d[a-z]\d$/i;
	return IsValid(sPattern, p);
}

function IsValidPhoneNumber(p) {
	var sPattern = /^\d{10}$/;
	return IsValid(sPattern, p);
}

function IsValidString(s) {
	var sPattern = /^.+?$/i;
	return IsValid(sPattern, s);
}

function IsValidStringMultiLine(s) {
	var sTmp = s.trim();
	var sPattern = /^[\S]+?[\s\S]*?$/i;
	return IsValid(sPattern, sTmp);
}

function IsValidDate(d) {
	/* date format must be  mm[/-.]dd[/-.]yyyy  -- separators are not optional */
	//var sPattern = /^(19|20)\d\d([- \/.])(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$/;
	var sPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
	return IsValid(sPattern, d);
}

function IsValidAge(currentDate, birthDate, requiredAge) {
	var diff = Math.ceil(currentDate.getTime() - birthDate.getTime());
	var one_day = 1000*60*60*24;
	var one_year = one_day * 365;
	var diffYears = diff / one_year;
	if (diffYears < requiredAge) return false;
	return true;
}

function IsValid(sPattern, sTest) {
	var re = new RegExp(sPattern);
	return re.test(sTest);
}

/******
	Helper utility functions
 ******/
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}


// return true if has profanity
function check4Profanity(word2Check) {

    return false;
    var profane = false;

    var settings = {
        "async": false,
        "data": { "word2Check": word2Check },
        "dataType": "text",
        "type": "POST",
        "url": PROFANITY_CHECK_URL
    };

    var result = jQuery.ajax(settings).responseText;

    if (result.indexOf("true")>-1) {
        profane = true;
    }
    return profane;
}
