

var True = true;
var False = false;
var spacerImageUrl = "/images/spacer.gif";

var modalPopup = null;
var nonModalPopup = null;

// validateAndPostBack
function validateAndPostBack(controlName, postBackArgument) {
	var okToSubmit = true;
	if (typeof(VAM_ValOnClick) == 'function') {
		VAM_ValOnClick('', '');
		okToSubmit = VAM_ValOnSubWGrp('*');
	}
	else {
		if (typeof(Page_ClientValidate) == 'function') {
			Page_ClientValidate();
			okToSubmit = okToSubmit && Page_IsValid;
		}
	}
	if (okToSubmit) {
		__doPostBack(controlName, postBackArgument);
		return true
	}
	else {
		return false;
	}
}

// validatePage
function validatePage() {
	var pageValid = true;
	if (typeof(Page_ClientValidate) == 'function') {
		Page_ClientValidate();
		pageValid = Page_IsValid;
	}
	if (typeof(VAM_ValOnClick) == 'function') {
		VAM_ValOnClick('', '');
		pageValid = VAM_ValOnSubWGrp('*') && pageValid;
	}
	return pageValid;
}

// openPopup
function openPopup(popupUrl, popupName, popupWidth, popupHeight, modal, useScrollBar) {
	// temp fix for popup help link
	popupHeight = popupHeight + 20;
	
	var left = Math.floor((screen.width - popupWidth) / 2);
	var top = Math.floor((screen.height - popupHeight) / 2) - 50;
	var features = "width=" + popupWidth + ",height=" + popupHeight + ",left=" + left + ",top=" + top + ",resizable=yes,status=no,toolbar=no,menubar=no,location=no";
	if (useScrollBar == true) {
		features = features + ",scrollbars=yes";
	}
	if (modal == true) {
		modalPopup = window.open(popupUrl, popupName, features);
		modalPopup.focus();
		return modalPopup;
	}
	else {
		nonModalPopup = window.open(popupUrl, popupName, features);
		nonModalPopup.focus();
		return nonModalPopup;
	}
}

// popupCalendar
function popupCalendar(url) {
	var left = Math.floor((screen.width - 200) / 2);
	var top = Math.floor((screen.height - 180) / 2) - 50;
	features = "width=200,height=180,left=" + left + ",top=" + top + ",resizable=yes,status=no,toolbar=no,menubar=no,location=no";
	modalPopup = window.open(url, "calendar", features);
}

// popupCalendarForTextbox
function popupCalendarForTextbox(textBoxControl) {
	popupCalendar("/controls/calendar.aspx?textbox=" + textBoxControl.id);
}

// popupCalendarForFunction
function popupCalendarForFunction(functionName) {
	popupCalendar("/controls/calendar.aspx?functionName=" + functionName);
}

// bodyOnFocus
function bodyOnFocus() {
	if (modalPopup == null) {
		return;
	}
	
  setTimeout(returnToModalPopup, 50);
 }
 
 // returnToModalPopup
 function returnToModalPopup() {
  if ((modalPopup != null) && (!modalPopup.closed)) {
    modalPopup.focus();
  }
}

// showPrintablePage
function showPrintablePage(contentContainerName, pageTitle, addTable, tableAttributes, clubThemeFolder, clubName, clubUrl, styleContainer) {
	var contentContainer = document.getElementById(contentContainerName);
	if (contentContainer == null) {
		return;
	}
	var styles = null;
	if (styleContainer) {
		styles = document.getElementById(styleContainer);
	}
	var printWindow = window.open("", "print", "menubar=yes,width=500,height=400,resizable=yes,scrollbars=yes");
	printWindow.document.write("<html><head>");
	printWindow.document.write("<title>" + pageTitle + "</title>");
	printWindow.document.write("<link rel='stylesheet' type='text/css' href='/themes/common_ui.css'>");
	printWindow.document.write("<link rel='stylesheet' type='text/css' href='" + clubThemeFolder + "club.css'>");
	printWindow.document.write("<link rel='stylesheet' type='text/css' href='/themes/printable.css'>");
	if (styles != null) {
		printWindow.document.write("<style>" + styles.innerHTML + "</style>");
	}
	printWindow.document.write("</head><body class='printable-body'>");
	printWindow.document.write("<div style='font-size: larger; font-weight: bold; border-bottom: 1px solid black; padding-bottom: 4px; margin-bottom: 4px;'>" + clubName + "<br>" + clubUrl + "<br>" + pageTitle + "</div>");
	
	if (addTable == true) {
		printWindow.document.write("<table " + tableAttributes + ">");
	}
	
	var text = contentContainer.innerHTML;
	var regex;
	
	// hide print link
	regex = new RegExp("<span class=[\"']?print-link.*<\/span>");
	text = text.replace(regex, "");

	// remove all scripts
	regex = new RegExp("<script(.|\\s)*?</script>", "ig");
	text = text.replace(regex, "");
	
	// hide style buttons
	regex = new RegExp("class=[\"']?style-button-table[\"']?.* id", "g");
	text = text.replace(regex, "class='style-button-table' style='display:none;visibility:hidden;' id");

	// kill onclick and onchange handlers
	regex = new RegExp("onclick=([\"']).*?\\1", "ig");
	text = text.replace(regex, "");

	regex = new RegExp("onchange=([\"']).*?\\1", "ig");
	text = text.replace(regex, "");

	// remove image maps
	regex = new RegExp("<map(.|\s)*?<\/map>", "ig");
	text = text.replace(regex, "");
	
	// kill links
	regex = new RegExp("href=([\"']).*?\\1", "ig");
	text = text.replace(regex, "");
	
	printWindow.document.write(text);
	if (addTable == true) {
		printWindow.document.write("</table>");
	}
	printWindow.document.write("</body></html>");
	printWindow.document.write("<script>function killEvent(e) { e.target.disabled = true; e.preventDefault(); return false; } document.addEventListener('mousedown', killEvent, true);document.addEventListener('click', killEvent, true);document.addEventListener('mouseup', killEvent, true);document.addEventListener('keypress', killEvent, true);</script>");
	if (window.print) {
		printWindow.document.write("<script>window.print();</script>");
	}
	else {
		printWindow.document.write("<script>alert('Press Ctrl-P (or Apple-P), or select File->Print from the menu to print this page');</script>");
	}
	printWindow.document.close();
}

// image management stuff

var fileInputControl = null;
var imagePreviewControl = null;
var testImage = null;
var resizeRequired = false;
var maxImageWidth;
var maxImageHeight;
var imageSuccessFunction;
var imageErrorFunction;
var showResizeMessage;

// browserCanShowPreview
function browserCanShowPreview() {
	return (window.navigator.appName == "Microsoft Internet Explorer");
}

// getPreviewFileName
function getPreviewFileName(fullFileName) {
	var startPosition = fullFileName.lastIndexOf("\\");
	return fullFileName.substring(startPosition + 1);
}

// previewImage
function previewImage(fileInputControlName, imagePreviewControlName, maxWidth, maxHeight, successFunction, errorFunction, resizeMessageRequired) {
	fileInputControl = document.getElementById(fileInputControlName);
	previewImageWithFileName(fileInputControl.value, imagePreviewControlName, maxWidth, maxHeight, successFunction, errorFunction, resizeMessageRequired);
}

// previewImageWithFileName
function previewImageWithFileName(fileName, imagePreviewControlName, maxWidth, maxHeight, successFunction, errorFunction, resizeMessageRequired) {
	imagePreviewControl = document.getElementById(imagePreviewControlName);
	maxImageWidth = maxWidth;
	maxImageHeight = maxHeight;

	if (successFunction) {
		imageSuccessFunction = successFunction;
	}
	if (errorFunction) {
		imageErrorFunction = errorFunction;
	}
	if (resizeMessageRequired == true) {
		showResizeMessage = true;
	}
	else {
		showResizeMessage = false;
	}
	
	imagePreviewControl.style.visibility = "hidden";
	testImage = new Image();
	testImage.onload = resizePreview;
	if (imageErrorFunction) {
		testImage.onerror = imageErrorFunction;
	}
//	fileName = "file:///" + fileName.replace(/\\/g, "/");
	testImage.src = fileName;
}

// resizePreview
function resizePreview() {
	testImage.onload = null;
	testImage.onerror = null;
	var originalWidth = testImage.width;
	var widthRatio = 1;
	if (originalWidth > maxImageWidth) {
		testImage.width = maxImageWidth;
		widthRatio = originalWidth / maxImageWidth;
		testImage.height = testImage.height / widthRatio;
		resizeRequired = true;
	}
	
	if (testImage.height > maxImageHeight) {
		var heightRatio = testImage.height / maxImageHeight;
		testImage.height = testImage.height / heightRatio;
		testImage.width = testImage.width / heightRatio;
		resizeRequired = true;
	}

	imagePreviewControl.onload = showImage;
	if (imageErrorFunction) {
		imagePreviewControl.onerror = imageErrorFunction;
	}

	imagePreviewControl.width = testImage.width;
	imagePreviewControl.height = testImage.height;
	imagePreviewControl.src = testImage.src;
	if ((resizeRequired == true) && (showResizeMessage))  {
		alert("The image will be resized as shown to fit the available space");
	}
	resizeRequired = false;
}

// showImage
function showImage() {
	imagePreviewControl.onload = null;
	imagePreviewControl.onerror = null;
	if (testImage == null) {
		return;
	}
	imagePreviewControl.width = testImage.width;
	imagePreviewControl.height = testImage.height;
	imagePreviewControl.style.visibility = "visible";
	testImage = null;
	if (imageSuccessFunction) {
		imageSuccessFunction();
	}
}

// refreshPage
function refreshPage() {
	var submitButton = document.getElementById(submitButtonName);
	if (submitButton == null) {
		var currentUrl = window.location.href;
		if (currentUrl.indexOf("action=") == 0) {
			window.location.reload();
		}
		else {
			var regex = new RegExp("action=[^&]*&?");
			newUrl = currentUrl.replace(regex, "");
			window.location.href = newUrl;
		}
	}
	else {
		submitButton.onclick();
		//__doPostBack(submitButtonName, "");
	}
}

// formatNumber
function formatNumber(number, decimalPlaces, dollarSign, scaleFactor) {
	if (isNaN(parseFloat(number))) {
		return "NaN";
	}
	if (isNaN(parseInt(decimalPlaces))) {
		decimalPlaces = 2;
	}
	
	if (decimalPlaces == 0) {
		return addCommas(Math.round(number).toString());
	}
	
	if (isNaN(parseInt(scaleFactor))) {
		scaleFactor = 0;
	}
	var numberString = "" + Math.round(number * Math.pow(10, (decimalPlaces - scaleFactor)));
	while (numberString.length <= decimalPlaces) {
		numberString = "0" + numberString;
	}
	var decimalPosition = numberString.length - decimalPlaces;
	
	if (dollarSign == true) {
		dollarSign = "$ ";
	}
	else {
		dollarSign = "";
	}
	return dollarSign + addCommas(numberString.substring(0, decimalPosition) + "." + numberString.substring(decimalPosition, numberString.length));
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// getAjaxObject
function getAjaxObject() {
	if (window.ActiveXObject){ // IE 
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else {
		if (window.XMLHttpRequest) { // Non-IE browsers
			return new XMLHttpRequest(); 
		}
		else {
			return null;
		}
	}
}

// trimString
function trimString(input) {
	var regex = new RegExp("^\\s+");
	var output = input.replace(regex, "");

	regex = new RegExp("\\s+$");
	output = output.replace(regex, "");

	return output;
}

// resizeFileInput
function resizeFileInput(newSize) {
	for (var counter = 0; counter < document.forms[0].elements.length; counter ++) {
		var element = document.forms[0].elements[counter];
		if (element.type == "file") {
			element.size = newSize;
			break;
		}
	}
}

// keyDownHandler
function keyDownHandler(e) {
	var keyNumber = 0;
	var source;
	if (e) {
		// netscape/mozilla code
		keyNumber = e.which;
		source = e.target;
	}
	else {
		// ie code
		keyNumber = window.event.keyCode;
		source = window.event.srcElement;
	}
	
	// let alpha, numeric, and standard punctuation keys go
	if ((keyNumber >= 32) && (keyNumber <= 97)) {
		return true;
	}
	
	// escape
	if (keyNumber == 27) {
		var cancelButton = document.getElementById(cancelButtonName);
		if (cancelButton == null) {
			if (doCancel) {
				doCancel();
			}
		}
		else {
			window.setTimeout(cancelButton.onclick, 20);
		}
		return false;
	}
	
	// F1
	if (keyNumber == 112) {
		var helpControl = document.getElementById("help_link");
		if (helpControl != null) {
			helpControl.onclick();
			return false;
		}
		return true;
	}
	
	// enter (return)
	if (keyNumber == 13) {
		if (source.type == "textarea") {
			return true;
		}
		else {
			var submitButton = document.getElementById(submitButtonName);
			if (submitButton != null) {
				submitButton.onclick();
			}
			return false;  // return false for enter keys so that there are no accidental form submits
		}
	}
	
	// default
	return true;
}   // keyDownHandler

// connect keyHandler function to key down event
document.onkeydown = keyDownHandler;
