﻿$(document).ready(function() {

	$.ajaxSetup({cache: false});

	$('.famtitle').Tooltip({ delay: 0 });

	$(".DeleteAttachment").bind("click",function(event){
		event.preventDefault();
		$.get(this.href,{},function(response){ 
			$("#"+response).html("")
		})	
	})

	$('a.thickbox').click(
		function() {
			var href = $(this).attr("href");
			var subject = $(this).attr("rel");
			var title = $(this).attr("title");
			if(title=="") title = $(this).html();

			var re = new RegExp("width=([0-9]*)");
			var m = re.exec(href);
			if(m == null) {var width = 300} else {var width = m[1] * 1};

			var $dialog = $('<div></div>').appendTo('body');
			$dialog.load(href, function(){ 
				setAjaxForms(subject);
				$dialog.dialog({
					title: title,
					width: width
				});
			});

			return false;
		}
	);

	$("#contacts").accordion({
		collapsible: true,
		active: false
	});

});

function setAjaxForms(subject) {

    $("#"+subject+"Form")
	.validationAideEnable(null, {
		showInlineMessages : true,
		summaryElementId   : subject+"Validation"
	})
	.ajaxForm({ 
        beforeSubmit: function(formData, jqForm, options){
            if($('input[name=Action]').fieldValue()[0]=='Delete') {
                if(confirm('Really delete this '+subject+'?')==false) return false;
            }
			if(subject=="password") {
				if($('#Password1').val()!=$('#Password2').val()){
					$("#passwordValidation").html('The new password is not the same in both fields!').show();
				}
				else if($('#Password1').val()==''){
					$("#passwordValidation").html('The new password cannot be blank!').show();
				}
				else {
					$("#passwordValidation").html('').hide();
				}
			}
			if($("#"+subject+"Validation").html()==''){
                return true;
            }
            else {
                return false;
            }
        },
        success: function(responseText, statusText){
            if(responseText==''){
                document.location = document.location; //'directory.asp';
            }
            else {
                alert(responseText);
            }
        }
    }); 

	$("#ExpiresOn").datepicker();

}

function dialogClose(){
	$(".ui-dialog").remove();
}

function addHTML(html,parent){
	var newdiv = document.createElement("div");
	newdiv.innerHTML = html;
	var container = document.getElementById(parent);
	container.appendChild(newdiv);
}

function ShowFamily(source,target) {
    $('.famdetail').css('display','none');
    $('#'+target).css('display','block');
    $('.famlink').removeClass('selected');
    $('#'+source).addClass('selected');
    $('#'+source).blur();
}

function LaunchCourse() {
    var w = 800;
    var h = 600;
    var winl = (screen.availWidth - w) / 2;
    var wint = (screen.availHeight - h) / 2;
    winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=no,status=no,location=no,toolbar=no,resizable=no'
    window.open('/course/','CourseWindow',winprops)
}
