// Character Counter for the text box.  
// FUTURE TASK: 
// Convert this to use the jQuery framework.
	maxKeys = 10000;
	var IE = (document.all) ? 1 : 0;
	var DOM = 0; 
	var total;
	
	
	
	if (parseInt(navigator.appVersion) >=5) {DOM=1};
				
	function pleaseWait() {
		var pleaseWait = document.getElementById("pleaseWait");
		pleaseWait.style.display = 'block';
	}
		
	function txtshow( txt2show ) {
		// Detect Browser
		if (DOM) {
			var viewer = document.getElementById("txtmsg");
			viewer.innerHTML=txt2show;
		} else if(IE) {
			document.all["txtmsg"].innerHTML=txt2show;
		}
	}
	
	function keyup(what) {
		var str = new String(what.value);
		var len = str.length;
		var showstr = len + " characters of " + maxKeys + " entered";
		if (len > maxKeys) showstr += "<br>Some information will be lost,<br> please revise your entry";
			txtshow( showstr );
		}
			
	$(document).ready(function() {
		
		$("a#imageDelete").click(function() {
			
			var agree=confirm("Are you sure?");
			
			if (agree)
				return true ;
			else
				return false ;
				
		});
		
	
		
	
	// Initialize some global variables
		var total = 0;
		var totalFlag = 0;
		var passwordFlag = 0;
				
	$("a#memberProfileToggle").click(function() {
		$("#memberProfile").toggle();
		$("#memberProfileEdit").toggle();
	});
			
	// Check to make sure passwords are confirmed
	$(".password").keyup(function() {
		var string1 = $("#adminPassword").val();
		var string2 = $("#adminPasswordConfirm").val();
			if(string1 == string2 && string1 != "") {
				$("#confirmPassword").html("Password OK").css("background-color", "green");
				passwordFlag = 0;
			} else {
				$("#confirmPassword").html("Passwords don't match").css("background-color", "#c00");
				passwordFlag = 1;
			}
	});
	
	// Set up questionmark box hints
	// In the stylesheet, the "#helpMe" style will change the attributes
	// of the hover boxes
		$(".help").mouseover(function(e) {
			var t = jQuery(this);
			$("#helpMe").html(t.attr("longdesc")).css("display", "block").css("top", e.pageY).css("left", (e.pageX + 25));
		});
		
		$(".help").mouseout(function(e) {
			$("#helpMe").css("display", "none");
		});
	
	
	// Initialize the percentage boxes
		$(".percentage").each(function() {
			total += this.value * 1;
		});			
	
		$('#percentTotal').html("Total: " + total + "%");
		if(total == 100) {
			$('#percentTotal').css("color", "green");
			totalFlag = 1;
		} else {
			$('#percentTotal').css("color", "red");
			totalFlag = 0;
		}
	
	// Add percentages each time the percentage boxes change
		$(".percentage").keyup(function() {
			total = 0;
			$(".percentage").each(function() {
				total += this.value * 1;
			});
			$('#percentTotal').html("Total: " + total + "%");
			
			if(total == 100) {
				$('#percentTotal').css("background-color", "green").css("color", "white");
				totalFlag = 1;
			} else {
				$('#percentTotal').css("background-color", "white").css("color", "red");
				totalFlag = 0;
			}
		});
		
	jQuery.fn.hint = function () {
		  return this.each(function (){
			// get jQuery version of 'this'
			var t = jQuery(this); 
			// get it once since it won't change
			var title = t.attr('title'); 
			// only apply logic if the element has the attribute
			if (title) { 
			  // on blur, set value to title attr if text is blank
			  t.blur(function (){
				if (t.val() == '') {
				  t.val(title);
				  t.addClass('blur');
				}
			  });
			  // on focus, set value to blank if current value 
			  // matches title attr
			  t.focus(function (){
				if (t.val() == title) {
				  t.val('');
				  t.removeClass('blur');
				}
			  });
		
			  // clear the pre-defined text when form is submitted
			  t.parents('form:first()').submit(function(){
				  if (t.val() == title) {
					  t.val('');
					  t.removeClass('blur');
				  }
			  });
		
			  // now change all inputs to title
			  t.blur();
			}
		  });
		}
	
	// Apply hint behavior to all items of class "hint"
		$(".hint").hint();
	
	
	// add stripes to the appropriate tables
		$(".stripe tr:even").addClass("tint");
		
	// automate tracking of download links by google analytics
		
		// what file types should we track?
		filetypes = /\.doc$|\.xls$|\.exe$|\.zip$|\.pdf$|\.mp3$|\.ppt$/i;
		
		$("#content a").each(function(){
		
			// Track external links
			if (location.host != this.host) {
				var url = $(this).attr("href").replace(/^http\:\/\/(www\.)*/i, "")
				$(this).click(function() {urchinTracker("/outgoing/" + url);})
			}
			// Track downloads (links with a given extension)
			else if ($(this).attr("href").match(filetypes)) {
				// The URL needs to be changed for each site this is applied to.
				var url = $(this).attr("href").replace(/^(http\:\/\/)*(www\.)*(s23610\.gridserver\.com")*\//i, "")
				$(this).click(function() {urchinTracker("/downloads/" + url);})
			}
			
    	});
	});
	
	function passwordConfirm()
	{
	  var frm = document.forms["memberEdit"];
	  if(frm.adminPassword.value != frm.adminPasswordConfirm.value)
	  {
		alert('Please make sure your passwords match.');
		return false;
	  }
	  else
	  {
		return true;
	  }
	}
	
	function percentTotal()
	{
		var total = 0;
		
		$(".percentage").each(function() {
			total += this.value * 1;
		});
		
		if(total != 100)
		{
			alert('Please make sure your percentage of work adds up to 100 (currently ' + total + ')');
			return false;
		}	else if($("#approve").attr("checked") != 1) {
			alert("Please check the \"Approve\" box to verify your information.");
			$("#approve").focus();
			return false;
		} else {
			return true;
		}
		
		
	}
	