function postwith(to,p) {
  var myForm = document.createElement("form");
  myForm.method="post";
  myForm.action = to;
  for (var k in p) {
    var myInput = document.createElement("input");
    myInput.setAttribute("name", k);
    myInput.setAttribute("value", p[k]);
    myForm.appendChild(myInput);
  }
  document.body.appendChild(myForm);
  myForm.submit();
  document.body.removeChild(myForm);
}

function format_date_relative(then_in_epoch){
  if(typeof(window['baseTime']) != "undefined") {
    offset = (baseTime - then_in_epoch);
  } else {
    var now       = new Date();
    var now_epoch = now.getTime();
    offset  = ((now_epoch/1000) - then_in_epoch);
  }

  if (offset < 0) 
    return 'in the future';

  seconds = offset;
  if (seconds < 60)
    return seconds + (seconds == 1 ? ' second ' : ' seconds ' ) + 'ago';

  minutes = Math.floor(offset / 60);
  if (minutes < 60)
    return minutes + (minutes == 1 ? ' minute ' : ' minutes ' ) + 'ago';

  hours = Math.floor(offset / 3600);
  if (hours < 24)
    return hours + (hours == 1 ? ' hour ' : ' hours ' ) + 'ago';

  days = Math.floor(offset / 86400);
  if (days < 7) 
    return days + (days == 1 ? ' day ' : ' days ' ) + 'ago';

  weeks = Math.floor(offset / 604800); 
  if (weeks < 5)
    return weeks + (weeks == 1 ? ' week ' : ' weeks ' ) + 'ago';

  months = Math.floor(offset / 2592000); // 30 days
  if (months < 24)
    return months + (months == 1 ? ' month ' : ' months ' ) + 'ago';

  years = Math.floor(offset / 31449600); // 365 days
  if (years == 0) 
    return '12 months';

  return years + (years == 1 ? ' year ' : ' years ' ) + 'ago';
};

function checkEmpty(Object) {
  return ((Object.value.length==0) || (Object.value==null)) ? false : true;
}
function decideEntryAction(obj){
 if (document.getElementById('image_file') && document.getElementById('image_file').value != '') {  
    obj.submit();
    return true;
 }
}

function loading_layer() {
    smokeScreen = false;
    CloseModal(); 
    var load_content = '<div style="background: #fff; padding: 50px; border: 5px solid #ccc;"><img src="/images/icons/indicator.gif" width="20" align="absmiddle"  /> <strong>Saving changes&hellip;</strong></div>'
    var load_load_load = document.createElement('div'); 
    load_load_load.innerHTML = load_content; 
    new ModalDialog('', load_load_load);
}

function toggleDisplay(id1,id2) {
	// if id1 is hidden, then show it and hide id2. else do the opposite.
	if (document.getElementById(id1).style.display == 'none') {
		document.getElementById(id1).style.display = 'block'
		document.getElementById(id2).style.display = 'none'
	} else {
		document.getElementById(id1).style.display = 'none'
		document.getElementById(id2).style.display = 'block'
	}
	return false;
}

function toggleDisplayInline(id1,id2) {
	// if id1 is hidden, then show it and hide id2. else do the opposite.
	if (document.getElementById(id1).style.display == 'none') {
		document.getElementById(id1).style.display = 'inline'
		document.getElementById(id2).style.display = 'none'
	} else {
		document.getElementById(id1).style.display = 'none'
		document.getElementById(id2).style.display = 'inline'
	}
	return false;
}

// For more explicit on and off switching
function turnDisplay(id, onOrOff)  {
    if (onOrOff == 'off') {
		 document.getElementById(id).style.display = 'none'
    } else if (onOrOff == 'on') {
		 document.getElementById(id).style.display = 'block'
    }
    return false;
}

// Yellow Fade Technique (tm 37 Signals)
var Color= new Array();
Color[2] = "ee";
Color[3] = "dd";
Color[4] = "cc";
Color[5] = "bb";
Color[6] = "aa";
Color[7] = "99";

function waittofade() {
	if (document.getElementById('fade')) {
    setTimeout("fadeIn(10)", 1000);
	 }
}

function fadeIn(where) {
    if (where >= 1) {
        document.getElementById('fade').style.backgroundColor = "#ffff" + Color[where];
		  if (where > 1) {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 200);
			} else {
			  where -= 1;
			  setTimeout("fadeIn("+where+")", 200);
			  document.getElementById('fade').style.backgroundColor = "transparent";
			}

    }
}

function popWindow(url,name,options){
        var ContextWindow = window.open(url,name,options);
        ContextWindow.opener = this;
        ContextWindow.focus();
}

function isNotEmpty(elem) {
    var str = elem.value;
    if(str == null || str.length == 0) {
      alert("Please, supply your query.");
      return false;
    } else {
      return true;
    }
}

function ajax_status(node,status_string,replace)
{
  if (!node) return false;
  if (typeof node == 'string')
    node = document.getElementById(node);
  if (node) {
    if (replace)
      node.innerHTML = status_string
    else
      node.innerHTML = status_string + node.innerHTML 
  }
}

/**
* Functions dealing with login and signup.
*/
function redirect_to(location) {
	if (/Safari/.test(navigator.userAgent)) {
		var special_redirect_form = document.createElement("form");
		special_redirect_form.action = location;
		special_redirect_form.method = "POST";
		special_redirect_form.name = "hidden_redirect_form";
		document.body.appendChild(special_redirect_form);
		special_redirect_form.submit();
	} else {
		window.location.href = location;
	}
}

var activeModalDialog = null;
function CloseModal() {
  if (activeModalDialog) {
    activeModalDialog.close();
  }
}
var repositionModal = true;
var DialogAppearDuration = 0.5;
var Dialog = Class.create();
Dialog.prototype = {
	initialize: function(title, content, options) {
		this.options = Object.extend({
			onclose:     function() {},
			parent:      null,
			isModal:     false,
    original_parent: null
	    }, options || {});
	  content = $(content);
		this.content = content;
        this.options.original_parent = content.parentNode;

		this.element = document.createElement('div');
		if (!options.parent) {
    		options.parent = document.body;
		}

		// Set initial popup element
		this.element.id = "modalDialog";
		this.element.style.cssText = "position: absolute";
		//this.element.style.visibility = "hidden";
        this.element.style.display = "none";
		this.element.innerHTML = '<div class="dialog-body"></div>';

		// Body
		var body = Element.down(this.element);

		// Add dialog element to document
		options.parent.appendChild(this.element);
		
		// Add content to dialog
		body.appendChild(this.content);
		
		// Position
		this.setPosition();

		// Make visible
		this.element.style.zIndex = 1;
		//this.element.style.visibility = "visible";

        new Effect.Appear('modalDialog', {duration: DialogAppearDuration});
	},
	setPosition: function() {
		if (!this.element) return;
		var viewportDims = getViewportDimensions();

        var scrollingOffsets = getScrollXY();

		var left = (scrollingOffsets[0] + viewportDims.width / 2 - this.element.getWidth() / 2);
		this.element.style.left = left + "px";

		var top = (scrollingOffsets[1] + viewportDims.height / 2 - this.element.getHeight() / 2);
		this.element.style.top = top + "px";

		if (repositionModal && this.element) {
			window.setTimeout(this.setPosition.bindAsEventListener(this), 500);
		}
	},
	close: function() {
		if (this.options.isModal) activeModalDialog = null;
		this.element.parentNode.removeChild(this.element);
		if (this.content.placeholder) {
			var holder = this.content.placeholder;
			holder.parentNode.insertBefore(this.content, holder);
			holder.parentNode.removeChild(holder);
			this.content.placeholder = null;
		}

		if (this.options.original_parent) {
            this.options.original_parent.appendChild(this.content);
		}

		this.element = null;
		this.options.onclose();
	}
}

var ModalDialog = Class.create();
ModalDialog.prototype = {
	initialize: function (title, content, options) {
		this.options = options || {};
	  activeModalDialog = this;
		this.options.isModal = true;

		// The shade
		this.shade = new Shade();
		this.shade.show();
		this.options.onclose = this.shade.hide.bindAsEventListener();

		this.dialog = new Dialog(title, content, this.options);
	},
	close: function() {
		this.dialog.close();
	}
}

var smokeScreen = false;
var Shade = Class.create();
Shade.prototype = {
	element: null,
	initialize: function() {},
	show: function() {
		this.element = document.createElement('div');
		this.element.id = "modalDialogShade";
        if (smokeScreen) {
            this.element.style.cssText = "position: absolute; top: 0; left: 0; background-color:#fff; opacity: 0.7; filter: alpha(opacity=70);";
        } else {
            this.element.style.cssText = "position: absolute; top: 0; left: 0; background-color:#fff; opacity: 0.01; filter: alpha(opacity=1);";
        }
		this.setSize();
		document.body.appendChild(this.element);
	},
	hide: function() {
		document.body.removeChild($('modalDialogShade'));
		this.element = null;
	},
	setSize: function() {
		if (!this.element) return;
		this.element.style.width = document.body.scrollWidth + 'px';
		this.element.style.height = document.body.scrollHeight + 'px';
		if (this.element) {
			window.setTimeout(this.setSize.bindAsEventListener(this), 500);
		}
	}
}

function getViewportDimensions() {
	var x,y;
	if (self.innerHeight) // all except Explorer
	{
		x = self.innerWidth;
		y = self.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
	}
	else if (document.body) // other Explorers
	{
		x = document.body.clientWidth;
		y = document.body.clientHeight;
	}
	return {width: x, height: y};
}

/**
* Obtain scrolling offsets.
*/
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;

  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;

  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

/** Find object's Y position **/
function findPosY(obj) {
  var curtop = 0;
  if(obj.offsetParent)
      while(1) {
         curtop += obj.offsetTop;
         if(!obj.offsetParent)
           break;
         obj = obj.offsetParent;
       }
   else if(obj.y)
      curtop += obj.y;
   return curtop;
}
function initScroll() {
  scrolldelay = setInterval("scrollWindow()", 100);
}
function scrollWindow() {
  window.scrollBy(0, 100);

  wh = f_clientHeight();
  oh = findPosY($('reply'));
  so = f_scrollTop();

  if ((wh+so) > oh) {
   clearTimeout(scrolldelay);
  }
}
function f_clientWidth() {
  return f_filterResults (
           window.innerWidth ? window.innerWidth : 0,
           document.documentElement ? document.documentElement.clientWidth : 0,
           document.body ? document.body.clientWidth : 0
      );
}
function f_clientHeight() {
  return f_filterResults (
           window.innerHeight ? window.innerHeight : 0,
           document.documentElement ? document.documentElement.clientHeight : 0,
           document.body ? document.body.clientHeight : 0
      );
}
function f_scrollTop() {
  return f_filterResults (
           window.pageYOffset ? window.pageYOffset : 0,
           document.documentElement ? document.documentElement.scrollTop : 0,
           document.body ? document.body.scrollTop : 0
      );
}
function f_filterResults(n_win, n_docel, n_body) {
  var n_result = n_win ? n_win : 0;
  if (n_docel && (!n_result || (n_result > n_docel)))
     n_result = n_docel;
  return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

