var okFunc = new Function();
var cancelFunc = new Function();

$(document).ready(function() {
	makeLabeledPassword("");
	makeDates();

	$("#fobo_logo").bind("click", function() {
		window.location.href = BASE_URL + "home";
	});

	$("#dialog-notification").dialog({
		autoOpen:false,
		resizable: false,
		modal: true,
		buttons: {
			'Close': function() {
				$(this).dialog('close');
			}
		}
	});

    $("#dialog-confirmation").dialog({
		autoOpen:false,
		resizable: false,
		modal: true,
		buttons: {
            'Continue': function() {
                okFunc();                
            },
			'Cancel': function() {
                cancelFunc();
				$(this).dialog('close');
			}
		}
	});
});

function makeLabeledPassword(suffix) {
	$("#fake_password" + suffix).bind("focus", function() {
		var $this = $(this);
		$this.hide();
		$("#real_password" + suffix).show().focus();
	});

	$("#real_password" + suffix).bind("blur", function() {
		var $this = $(this);
		if($this.val()=="") {
			$this.hide();
			$("#fake_password" + suffix).show();
		}
	});
	
	$("#fake_password_2" + suffix).bind("focus", function() {
	var $this = $(this);
	$this.hide();
	$("#real_password_2" + suffix).show().focus();
	});

	$("#real_password_2" + suffix).bind("blur", function() {
		var $this = $(this);
		if($this.val()=="") {
			$this.hide();
			$("#fake_password_2" + suffix).show();
		}
	});
}

function makeDates() {
	$(".auto-date").each(function() {
		var $this = $(this);
		var prevval = $this.val();
		$this.datepicker({
             onSelect: function() { $(".ui-datepicker a").removeAttr("href"); }
        });
		if ($this.hasClass("mindate")){
			$this.datepicker('option', { dateFormat: 'd M yy', minDate: +4 });
		} else {
			$this.datepicker('option', { dateFormat: 'd M yy' });
		}
		
		$this.val(prevval);
	});
	$("#ui-datepicker-div").hide();
}

function selectAll(form) {
	$("input[type='checkbox']", $(form)).each(function() {
		this.checked = true;
	});
}

function selectNone(form) {
	$("input[type='checkbox']", $(form)).each(function() {
		this.checked = false;
	});
}

function page(p) {
	$("#pagerForm input[name=pager.page]").val(p);
	$("#pagerForm input[type=submit]").click();
}

function preload(images) {
    if (document.images) {
        var i = 0;
        var imageArray = new Array();
        imageArray = images.split(',');
        var imageObj = new Image();
        for(i=0; i<=imageArray.length-1; i++) {
            //document.write('<img src="' + imageArray[i] + '" />');// Write to page (uncomment to check images)
            imageObj.src=images[i];
        }
    }
}

var months = { "Jan": 0, "Feb": 1, "Mar": 2, "Apr":3, "May": 4, "Jun":5,  "Jul":6,  "Aug":7,  "Sep":8,  "Oct":9,  "Nov":10,  "Dec":11 }; 
function isDateAfter(startDate, endDate) {
    var startDateParts = startDate.split(" ");
    var endDateParts = endDate.split(" ");

    var d1 = new Date(startDateParts[2], months[startDateParts[1]], startDateParts[0]);
    var d2 = new Date(endDateParts[2], months[endDateParts[1]], endDateParts[0]);

    if(d1.getTime() > d2.getTime()) {
        return true;
    }

    return false;
}


function myalert(msg) {
	$("#dialog-notification").html("<p>" + msg + "</p>");
	$('#dialog-notification').dialog('open');
}

function myconfirm(msg, ok, cancel) {
    okFunc = ok;
    cancelFunc = cancel;
    $("#dialog-confirmation").html("<p>" + msg + "</p>");
	$('#dialog-confirmation').dialog('open');
}

function loginByFb(callback) {
    myalert('Please wait while we are receiving authentication from Facebook');
    
    $.get(BASE_URL + "login?loginByFb=1"
        , function(data) {
            if(data.ok == 'yes') {
                LOGGED_IN = true;
                window.location.reload();
            }
            else {
                LOGGED_IN = false;
                alert('Log in by Facebook was unsucsseful');
            }

			if(jQuery.isFunction(callback)) {
				callback(LOGGED_IN);
			}
            
            /*
            if( jQuery.isFunction(window["postLogon"]) ){
               postLogon(data); 
            }
            */
        }
    );        
}

