//UI Alert Messages
var CONFIRM_APP_CANCEL = 'By pressing OK, you will cancel your Online Lease application, and the apartment you selected will be returned to our available inventory. If you choose to use Online Lease in the future, you will need to select a new apartment. Press Cancel to return to your application.' 
var CONFIRM_APP_LOGOUT = 'If you log out now, your lease information will be saved, but the apartment you selected may not be available when you return. Select OK to log out. Select Cancel to return to your online lease application.';
var CONFIRM_SEC_APP_LOGOUT = 'You have not completed your lease application.  You will need to log back in to pay your application charges and sign your lease.  Select OK to log out.  Select Cancel to return to your online lease application.';
//Developer Messages
var TEST_MESSAGE = 'This is a test and only a test.  Please do not change your channel.';
var PARAMETER_MISSING = 'The parameter {0} is missing and is required.';

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function addUnloadEvent(func) {
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      if (oldonunload) {
        oldonunload();
      }
      func();
    }
  }
}

//Sets focus to a particular control while
// handling the dotnet unique control names.
function setFocus(controlId)
{
    try
    {
	    var id = ConvertDotNetId(controlId);
	    var o = document.getElementById(id);
	    if(o) o.focus();
	}
	catch (Exception)
	{
	}
	
	return;
}

function ConvertDotNetId(id)
{
	var index = id.indexOf(':');

    //According to documentation, "replace()" should replace all
    //occurences of ":", but it does not always work in practice.
    //Now, loop through, replacing all instances of ":" with "_"
	while (index != -1)
	{
	    id = id.replace(':', '_');
	    index = id.indexOf(':');
	}

	return id;
}

function openDialog(url, width, height, resizable, dlgName)
{
    var isResizable = false;
    var windowName = "archstoneDlg";
    var newUrl = "";
    if (openDialog.arguments.length < 3)
    {
        width = 550;
        height = 400;
    }
    if (openDialog.arguments.length > 3)
    {
        isResizable = resizable;
    }
    if (openDialog.arguments.length > 4)
    {
        windowName = dlgName;
    }

    if (width == 0)
    {
        width = 550;
    }
    if (height == 0)
    {
        height = 400;
    }

    var openPosition = '';
    try
    {
        var x = getScreenLeftX();
        var y = getScreenTopY();
        var elBody = document.getElementById("vBody");
        if(elBody) {
            x += getElementX(elBody) + 22;
            y += getElementY(elBody) + 2;
        }
        else {
            x += 20;
            y += 20;
        }

        openPosition = 'left=' + x + ',top=' + y;
                
        var i = url.indexOf("?");
        if(i > -1) {
            newUrl = url.substring(0, i + 1);
            newUrl += url.substring(i + 1, url.length).replace("/", "%2f");
        } else {
            newUrl = url;
        }
    }
    catch(e) {}

	var features = new String();
	features += 'channelmode=no,';
	features += 'directories=no,';
	features += 'fullscreen=no,';
	features += 'location=no,';
	features += 'menubar=no,';
	features += 'resizable=' + (isResizable ? 'yes' : 'no') + ',';
	features += 'scrollbars=' + (isResizable ? 'yes' : 'no') + ',';
	features += 'status=no,';
	features += 'titlebar=yes,';
	features += 'toolbar=no,';
	features += 'width=' + width + 'px,';
	features += 'height=' + height + 'px';
	features += ',' + openPosition;
	
	var wnd = window.open(newUrl, windowName, features);
	if(wnd==null){
	  alert('You must enable popup windows to continue.');
	} else {
	  wnd.focus();
	}
}

function openDialogUncentered(url, width, height, resizable, dlgName)
{
    var isResizable = false;
    var windowName = "archstoneDlg";
    var newUrl = "";
    if (openDialogUncentered.arguments.length < 3)
    {
        width = 550;
        height = 400;
    }
    if (openDialogUncentered.arguments.length > 3)
    {
        isResizable = resizable;
    }
    if (openDialogUncentered.arguments.length > 4)
    {
        windowName = dlgName;
    }

    if (width == 0)
    {
        width = 550;
    }
    if (height == 0)
    {
        height = 400;
    }

    var openPosition = '';
    try
    {
        var x = getScreenLeftX();
        var y = getScreenTopY();

        openPosition = 'left=' + x + ',top=' + y;
                
        var i = url.indexOf("?");
        if(i > -1) {
            newUrl = url.substring(0, i + 1);
            newUrl += url.substring(i + 1, url.length).replace("/", "%2f");
        } else {
            newUrl = url;
        }
    }
    catch(e) {}

	var features = new String();
	features += 'channelmode=no,';
	features += 'directories=no,';
	features += 'fullscreen=no,';
	features += 'location=no,';
	features += 'menubar=no,';
	features += 'resizable=' + (isResizable ? 'yes' : 'no') + ',';
	features += 'scrollbars=' + (isResizable ? 'yes' : 'no') + ',';
	features += 'status=no,';
	features += 'titlebar=yes,';
	features += 'toolbar=no,';
	features += 'width=' + width + 'px,';
	features += 'height=' + height + 'px';
	features += ',' + openPosition;
	
	var wnd = window.open(newUrl, windowName, features);
	if(wnd==null){
	  alert('You must enable popup windows to continue.');
	} else {
	  wnd.focus();
	}
}

function getElementX(el) {
    var x = 0;
    try {
        for(var e = el; e; e = e.offsetParent) {
            x += e.offsetLeft;
        }
        for(e = el.parentNode; e && e!= document.body; e = e.parentNode) {
            if(e.scrollLeft) {
                x -= e.scrollLeft;
            }
        }
    }
    catch(e) {}
    return x;
}

function getElementY(el) {
    var y = 0;
    try {
        for(var e = el; e; e = e.offsetParent) {
            y += e.offsetTop;
        }
        for(e = el.parentNode; e && e!= document.body; e = e.parentNode) {
            if(e.scrollTop) {
                y -= e.scrollTop;
            }
        }
    }
    catch(e) {}
    return y;
}

function getScreenLeftX()
{
	var x;
	try
	{
		x = parseInt(window.screenX);
        if (isNaN(x))
        {
			x = parseInt(window.screenLeft);
			if (isNaN(x))
			{
				x = 0;
			}
        }
	}
	catch(e)
	{
	  x = 0;
	}
	return x;
}

function getScreenTopY()
{
	var y;
	try
	{
		y = parseInt(window.screenY);
        if (isNaN(y))
        {
			y = parseInt(window.screenTop);
			if (isNaN(y))
			{
				y = 0;
			}
        }
	}
	catch(e)
	{
	  y = 0;
	}
	return y;
}

