﻿
var IsDebug=false;

window.onerror=function(msg,url,num) {
	//$.post('/errorhandling/reporterr.aspx', { error: msg, location: url, line: num});
	return !IsDebug;
}


$(function() {
	general_pageLoad();
	var prm = Sys.WebForms.PageRequestManager.getInstance();
	prm.add_endRequest(function() { general_pageLoad(); });
});
function general_pageLoad() {
	setInterval(function() { $.get('/scripts/ajax_controls/keepalive.aspx'); }, 840000); // 14 mins * 60 * 1000

	setupWater();
	setupHelp();

	$(".selectAll").each(function() {
		$(this).click(function() {
			this.select();
		});
	});
}

function setupHelp() {

	var popHelpReset = function(obj) {
		$('.popHelp').each(function() {
			var $this = $(this);
			if ($this != obj) {
				var strOrig = $this.find('.popHelpInner').html();
				if (strOrig != null && strOrig != '') { $this.html(strOrig) }
				$this.hide();
			}
		});
		setupWater();
	};

	$('.iHelp').each(function() {
		$(this).click(function(event) {
			var $ph = $(this).next('.popHelp');
			popHelpReset($ph);
			$ph.wrapInner('<div class="popHelpInner" />').show();
			var dClose = $('<div class="closePop">').append('x').click(function() { popHelpReset(); });
			$ph.append(dClose);
			event.stopPropagation();
			$ph.click(function(ph_event) { ph_event.stopPropagation(); });
			$(document).click(function(doc_event) {
				popHelpReset();
				$(this).unbind(doc_event);
			});
		});
	});
}
/*
function popHelpReset(obj) {
$('.popHelp').each(function() {
var $this = $(this);
if ($this != obj) {
var strOrig = $this.find('.popHelpInner').html();
if (strOrig != null && strOrig != '') { $this.html(strOrig) }
$this.hide();
}
});
setupWater();
}
*/
function setupWater() {
	$(".water").each(function() {
		$(this).WaterMark();
		/*
		$tb = $(this);
		if ($tb.val() != this.title && $tb.val() == '') {
		//$tb.removeClass("water");
		$tb.val(this.title);
		}
		*/
	});
	/*
	$(".water").focus(function() {
	$tb = $(this);
	if ($tb.val() == this.title) {
	$tb.val("");
	//$tb.removeClass("water");
	}
	});
	$(".water").blur(function() {
	$tb = $(this);
	if ($.trim($tb.val()) == "") {
	$tb.val(this.title);
	//$tb.addClass("water");
	}
	});
	*/
	$("form").submit(function() { clearWater(); });
}
function clearWater() {
	$(".water").each(function() {
		$tb = $(this);
		if ($tb.val() == this.title) {
			$tb.val("");
		}
	});
	return true;
}

(function($) {
	$.fn.WaterMark = function(options) {
		var defaults = {
			CssClass: 'water'
		};

		var settings = $.extend(defaults, options);

		gotFocus = function($tb) {
			if ($tb.val() == $tb.attr('title')) {
				$tb.val('')
				.removeClass(settings.CssClass);
			}
		};

		lostFocus = function($tb) {
			if ($.trim($tb.val()) == '') {
				$tb.val($tb.attr('title'))
				.addClass(settings.CssClass);
			}
		};

		return this.each(function() {
			var $tb = $(this);
			lostFocus($tb);
			$tb.focus(function() { gotFocus($(this)) })
			.blur(function() { lostFocus($(this)) });
		});

		/*
		if (this.form) {
		var form = this.form,
		$form = $(form);

			if (!$form.data(dataFormSubmit)) {
		$form.submit($.watermark.hideAll);

				// form.submit exists for all browsers except Google Chrome
		// (see "else" below for explanation)
		if (form.submit) {
		$form.data(dataFormSubmit, form.submit);

					form.submit = (function(f, $f) {
		return function() {
		var nativeSubmit = $f.data(dataFormSubmit);

							$.watermark.hideAll();

							if (nativeSubmit.apply) {
		nativeSubmit.apply(f, Array.prototype.slice.call(arguments));
		}
		else {
		nativeSubmit();
		}
		};
		})(form, $form);
		}
		else {
		$form.data(dataFormSubmit, 1);

					// This strangeness is due to the fact that Google Chrome's
		// form.submit function is not visible to JavaScript (identifies
		// as "undefined").  I had to invent a solution here because hours
		// of Googling (ironically) for an answer did not turn up anything
		// useful.  Within my own form.submit function I delete the form's
		// submit function, and then call the non-existent function --
		// which, in the world of Google Chrome, still exists.
		form.submit = (function(f) {
		return function() {
		$.watermark.hideAll();
		delete f.submit;
		f.submit();
		};
		})(form);
		}
		}
		}
		*/

	};
})(jQuery);
