function loadInDialog(link, width, height) {
    var dialog = $('<div class="dialog"></div>');
    var width = (!width)?465:width;
    var height = (!height)?270:height;
    dialog.dialog({
        autoOpen: false,
        closeOnEscape: true,
        title: link.attr('title'),
        modal: true,
        width: width,
        height: height,
        open: function () {
            $(this).load(link.attr('href'));
        }
    });

    link.click(function(e) {
        e.preventDefault();
  dialog.dialog('open');
    });
}

function render_openx(banner_host, place_holder, zone, width, height) {
    var random1 = Math.floor(Math.random()*99999999999);
    var src='//'+banner_host+'/delivery/afr.php?zoneid='+zone+'&amp;target=_blank&amp;cb='+random1;
    render_iframe(src, place_holder, width, height)
}

function render_iframe(src, place_holder, width, height) {
    var random = Math.floor(Math.random()*99999999999);
    style = '';
    if (width) {
      style += "width:"+ width + "px;"
    }
    if (height) {
      style += "height:"+ height + "px;"
    }
    var iframe = $('<iframe />', {
                name: 'f'+random,
                id:   'f'+random,
                scrolling: 'no',
                frameborder: 0,
                style: style,
                allowtransparency:'true',
                src:src}).appendTo(place_holder);
    var ua = $.browser;
  if (ua.mozilla && ua.version.slice(0,3) == "1.9") {
      //workaround firefox iframe reloading 3.x bug https://bugzilla.mozilla.org/show_bug.cgi?id=279048
        iframe.attr('src', iframe.attr('src'));
  }
}

function setUserPreference(key, value) {
    var cookieName = 'careesma_' + key;

    document.cookie = cookieName + "=" + escape(value) + ";path=/";
}

function getUserPreference(key, def) {
    var cookieName = 'careesma_' + key;

    if (document.cookie.length > 0) {
        c_start=document.cookie.indexOf(cookieName + "=");
        if (c_start != -1) {
            c_start = c_start + cookieName.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) {
                c_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return def;
}

function postToUrl(path, params, method) {
    method = method || "post"; // Set method to post by default, if not specified.

    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);

    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);

        form.appendChild(hiddenField);
    }

    document.body.appendChild(form); // Not entirely sure if this is necessary
    form.submit();
}

function isFuture(m, y) {
    var d = new Date();
    var now = new Date();
    d.setMonth(eval(m.value) -1);
    d.setYear(eval(y.value));
    return d > now;
}

function isDateRangeInvalid(startD, startM, startY, endD, endM, endY) {
  var start = new Date();
  var end = new Date();

  if(startD.value) start.setDate(eval(startD.value));
  if(startM.value) start.setMonth(eval(startM.value) -1);
  if(startY.value) start.setYear(eval(startY.value));

  if(endD.value) end.setDate(eval(endD.value));
  if(endM.value) end.setMonth(eval(endM.value) -1);
  if(endY.value) end.setYear(eval(endY.value));

  return start > end;
}

function dialog(selector, hash, width) {
    var elem = $(selector);
    var title = elem.attr('title');
    elem.dialog({
        width: width,
        autoOpen: false,
        modal: true,
        closeOnEscape: true,
        title: title,
        cache: false,
        beforeClose: function (event) {
            if (event.originalEvent.type !== 'hashchange') {
                window.location.hash = '';
                return false;
            }
        },
    });
    $(window).hashchange(function(event) {
        elem.dialog(
            (window.location.hash === hash) ? 'open' : 'close', event);
    });
}

function showUrlInDialog(url){
    var tag = $("<div></div>");
    $.ajax({
      url: url,
      success: function(data) {
        // TODO(Luis): Hack to pass the url to post to the template.
        data = data.replace('action=""','action="'+url+'"')
        tag.html(data).dialog({closeOnEscape: true, modal: false}).dialog('open');
      }
    });
}

