// this file requires the jquery library (written against 1.2.1)

// anything that needs to be fired off on dom load goes in here
$(document).ready(function() {
	// draw back button if the wrapper exists
	$(".backbuttonWrapper").html("<input type=\"button\" id=\"cmdBack\" class=\"button\" value=\"<< Back\" onclick=\"javascript:window.history.back();\" />&nbsp;");
	// js sandbox
	$("#jssandbox").click(function(){popMessage("Test Dialog","This is a test dialog. If you see this, things are A-OK")});
	$("#ctl00_T2Main_txtReason").keyup(function(e){var contents = $(e.target).val();updateCharCount(contents);});
 });

// ############################################################
// ## UI Stuff
// ############################################################

// counts number of characters left in a textbox (appeal reason)
function updateCharCount(value) {
	maxLength = 1000;
	newCount = maxLength - value.length;
	$("#lblReasonCounter").text(newCount + " characters left");
	if (newCount <= maxLength && newCount > 100) {
		$("#lblReasonCounter").css({"background-color":"transparent","color":"#006600"});
	}
	if (newCount <= 100 && newCount > 30) {
		$("#lblReasonCounter").css({"background-color":"transparent","color":"#660000"});
	}
	if (newCount <= 30) {
		$("#lblReasonCounter").css({"background-color":"#660000","color":"#ffffff"});
	}
};

// display a message with a yes/no (ok/cancel) buttonset. clicking buttons can call functions (passed as arguments)
function popConfirm(title,msg,oktext,okfunction,canceltext,cancelfunction) {
	$("div#popConfirmationDialogMsg").html(msg);
	if (oktext != "") { 
		$("div#confirmDialogButtonOk").html("<input type='button' value='"+oktext+"' name='popConfirmButtonOk' id='popConfirmButtonOk' />"); 
		$("input#popConfirmButtonOk").click(function(){
			$("div#popConfirmationWrapper:visible").hide();
			if (okfunction != "") { eval(okfunction); }
			return true;
		});
	} else {
		$("div#confirmDialogButtonOk").css("display","none");
	}
	if (canceltext != "") { 
		$("div#confirmDialogButtonCancel").html("<input type='button' class='warning' value='"+canceltext+"' name='popConfirmButtonCancel' id='popConfirmButtonCancel' />");
		$("input#popConfirmButtonCancel").click(function(){
			$("div#popConfirmationWrapper:visible").hide();
			if (cancelfunction != "") { eval(cancelfunction); }
			return false;
		});
	} else {
		$("div#confirmDialogButtonCancel").css("display","none");
	}
	$("div#popConfirmationWrapper:hidden").show();
	confirmationWrapperSize();
	$("div#popConfirmationDialog:hidden").show();
	confirmationDialog_position();
	if (title != "") { 
		$("div#popConfirmationTitle").html(title);
		$("div#popConfirmationTitle").prepend("<div style=\"color:#fff;display:block;font-weight:bold;float:right;cursor:pointer;\">X</div>").click(function(){
			$("div#popConfirmationWrapper:visible").hide();
			return false;
		});
	} else {
		$("div#popConfirmationTitle").css("display","none");
	}
}

// simple message 'window' with optional title. will return 'false' on close
function popMessage(title,msg) {
	popConfirm(title,msg,"","","","");
}

// ripped from cody lindley's thickbox (http://www.codylindley.com/)
function confirmationWrapperSize() {
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {  
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
	}
	$("div#popConfirmationWrapper").css({"height":yScroll +"px", "width":xScroll +"px"});
}
function confirmationDialog_position() {
	TB_WIDTH = 400;
	TB_HEIGHT = 200;
	var pagesize = getPageSize();  
	var arrayPageScroll = getPageScrollTop();
	$("div#popConfirmationDialog").css({width:TB_WIDTH+"px",left: (arrayPageScroll[0] + (pagesize[0] - TB_WIDTH)/2)+"px", top: (arrayPageScroll[1] + (pagesize[1]-TB_HEIGHT)/2)+"px" });
}
function getPageScrollTop(){
	var yScrolltop;
	var xScrollleft;
	if (self.pageYOffset || self.pageXOffset) {
		yScrolltop = self.pageYOffset;
		xScrollleft = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft ){   // Explorer 6 Strict
		yScrolltop = document.documentElement.scrollTop;
		xScrollleft = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScrolltop = document.body.scrollTop;
		xScrollleft = document.body.scrollLeft;
	}
	arrayPageScroll = new Array(xScrollleft,yScrolltop) 
	return arrayPageScroll;
}
function getPageSize(){
	var de = document.documentElement;
	var w = window.innerWidth || self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
	var h = window.innerHeight || self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight
	arrayPageSize = new Array(w,h) 
	return arrayPageSize;
}
// end thickbox code