      $(document).ready(function(){
            $('.hintable').hinty();

		$("#logo").click(function() {
		window.location = "/index.php";
		});

      $("#generalNotesDiv").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			width: 640,
			resizable: true,
			draggable: true
			});

      	$("#notepadlink").click(function() {
      	
      		showGeneralNotes();
      	
      	});
      
      });

$.fn.hinty = function() {
	// applies a text hint to the text input field by using the input's title attribute
	return this.each(function(){
		var $this = jQuery(this);
		var title = this.title;
		
		
		if($this.val() == "" || $this.val() == title) {
		$this.addClass('hint_text');
		$this.val(title);
		}
		$this.focus(function(){
			if ($this.val() == title) {
				$this.removeClass('hint_text');
				$this.val('');
			}
		});
		
		$this.blur(function(){
			if ($this.val() == '') {
				$this.addClass('hint_text');
				$this.val(title);
			}
		});
	});
}


$.fn.collapse = function() {
    return this.each(function() {
        $(this).find("a.toggle").addClass('collapsible').click(function() {
            if ($(this).parent().hasClass('collapsed'))
                $(this).parent().removeClass('collapsed').addClass('collapsible');
     
            $(this).removeClass('collapsed');
     
            $(this).parent().children().not('a.toggle').toggle("fast", function() {
              
                 if ($(this).is(":visible"))
                    $(this).parent().find("a.toggle").addClass('collapsible');
                 else
                    $(this).parent().addClass('collapsed').find("a.toggle").addClass('collapsed');
             });
        });
    });
};


function limitChars(textid, limit, infodiv) {
var text = $('#'+textid).val(); 
var textlength = text.length;
if(textlength > limit) {
$('#' + infodiv).html('You cannot write more then '+limit+' characters!');
$('#'+textid).val(text.substr(0,limit));
return false;
}
else
{
$('#' + infodiv).html((limit - textlength) +' characters left');
return true;
}
}

function changeClass(id, newClass) {
        identity=document.getElementById(id);
        identity.className=newClass;
}

function showNotes() {
	$("#notesDiv").dialog('open');
	$("#noteList").load('noteServer.php?itemIdx=' + document.noteTaker.itemIdx.value + '&zone=' + document.noteTaker.zone.value);		

}


function showGeneralNotes() {
	$("#generalNotesDiv").dialog('open');
	$("#generalNoteList").load('noteServer.php?itemIdx=' + document.generalNoteTaker.itemIdx.value + '&zone=' + document.generalNoteTaker.zone.value);		

}

function saveNote() {
$("#noteList").load('noteServer.php?specialCommand=add+note&itemIdx=' + document.noteTaker.itemIdx.value + '&zone=' + document.noteTaker.zone.value + '&body=' + escape(document.noteTaker.newNote.value));
	document.noteTaker.newNote.value = "";
}

function saveGeneralNote() {
	var noteURL = '';
	if (document.generalNoteTaker.addLink.checked) {
		noteURL = escape(location.href);
	}
$("#generalNoteList").load('noteServer.php?specialCommand=add+note&itemIdx=' + document.generalNoteTaker.itemIdx.value + '&zone=' + document.generalNoteTaker.zone.value + '&body=' + escape(document.generalNoteTaker.newNote.value) + '&url=' + noteURL);
	document.generalNoteTaker.newNote.value = "";
}

function deleteNote(noteIdx) {
$("#noteList").load('noteServer.php?specialCommand=delete+note&noteIdx=' + noteIdx);

}

function deleteGeneralNote(noteIdx) {
$("#generalNoteList").load('noteServer.php?specialCommand=delete+note&noteIdx=' + noteIdx);
}

var formRequirements = new Array();

function verifyAndSubmit(formName) {
	var errors = 0;
	var passmismatch = 0;
	for (i=0; i < formRequirements.length; i++) {
		test = eval("document." + formName + "." + formRequirements[i].field);
		var re = /^[\w ]+$/;
		if (test) {
			switch(formRequirements[i].req) {
			case 'no special chars':
				if (!re.test(test.value)) {	
					errors++;
				}
				break;
			case 'not blank':
				if (test.value == '') {
					errors++;
				}
				break;
			case 'is number':
				if (isNaN(parseInt(test.value)) || test.value == '') {
					errors++;
				}
				break;
			case 'non-zero index':
				if (test.selectedIndex == 0) {
					errors++;
				}
				break;
			case 'is checked':
				if (!test.checked) {
					errors++;
				}
				break;		
			case 'password verify':
			thepassword = eval("document." + formName + ".password");
				if (test.value != thepassword.value) {
					passmismatch++;
					}
				break;	
			case 'proper format':
				if ((test.value.indexOf('@') == -1) || (test.value.indexOf('.') == -1)){
					alert('Please use a properly formatted e-mail address');
					errors++;
				}
				break;
			default:
			}
		}
	}

	if (errors > 0) {
		alert("Hey! Fill out all the required fields! Read the instructions! Listen to your mom!");
	}
	else if (passmismatch > 0) {
		alert("Passwords don't match!");
	}
	else  {
		workingForm = eval("document." + formName);
		workingForm.submit();
	}
}

function formRequirement(field, req) {
	this.field = field;
	this.req = req;
}

