function RequireValue(element, msg) {
	field = $(element);

	if (field.value == '' || field.value == field.title) {
		__invalidateField(element, msg)
		return false;
	} else {
		__revalidateField(element, msg)
	}

	return true;
}

function RequireFieldsMatch(element1, element2, msg) {
	field1 = $(element1);
	field2 = $(element2);
	
	if (field1.value != field2.value) {
		__invalidateField(element2, msg)
		return false;
	} else {
		__revalidateField(element2, msg)
	}
	
	return true;
}

function RequireEitherValue(element1, element2, msg) {
	field1 = $(element1);
	field2 = $(element2);
	
	if (field1.value == '' && field2.value == '') {
		__invalidateField(element1, msg)
		return false;
	} else {
		__revalidateField(element1, msg)
	}
	
	return true;
}

function RequireValidEmailAddress(element, msg) {
	field = $(element);
	
	if (!ValidEmailAddressString(field.value)) {
		__invalidateField(element, msg)
		return false;
	} else {
		__revalidateField(element, msg)
	}
	
	return true;
}

function ValidEmailAddressString(email) 
{
	invalidChars = " /:,;"

	if (email == "") 
	{						// cannot be empty
		return false
	}
	
	for (i=0; i<invalidChars.length; i++) 
	{	// does it contain any invalid characters?
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) 
		{
			return false
		}
	}
	
	atPos = email.indexOf("@",1)			// there must be one "@" symbol
	if (atPos == -1) 
	{
		return false
	}
	
	if (email.indexOf("@",atPos+1) != -1) 
	{	// and only one "@" symbol
		return false
	}
	
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) 
	{					// and at least one "." after the "@"
		return false
	}
	
	if (periodPos+3 > email.length)	
	{		// must be at least 2 characters after the "."
		return false
	}
	
	return true
}

function __invalidateField(element, msg) {
	field = $(element);
	newId = '' + field.id + '-label';
	
	if ($(newId) != null) {
		Element.addClassName(newId, 'invalid');
	} else if(msg != undefined) {
		alert(msg);
	} else {
		Element.addClassName(field, 'invalid-input');
	}
	field.activate();
}

function __revalidateField(element, msg) {
	field = $(element);
	newId = '' + field.id + '-label';
	
	Element.removeClassName(field, 'invalid-input');
	Element.removeClassName(newId, 'invalid');
}


function commentValidateLogin(form) {
	if (!RequireValue('CommentContents', 'You must enter a comment.')) { return false; }
	
	if ($('CommentUserId').value == '') {
		if (!Element.visible('comment-login')) {
			Element.hide('comment-submit');
			Element.show('comment-login');
			RequireValue('CommentUserEmail');
			return false;
		}
		
		if (!RequireValue('CommentUserEmail')) { return false; }
		if (!RequireValue('CommentUserPassword')) { return false; }
	}
	
	return true;
}

function draftValidatePost(form) {
	if (!RequireEitherValue('DraftIdeaTitle', 'DraftIdeaDescription')) { return false; }

	return true;
}

function FieldListener(elem) {
  this.elem = elem;
  this.helper = "";
  this.active = false;
  
  var me = this;
  
  this.init = function() {
    this.helper = this.elem.getAttribute('title');
	if (this.helper == undefined || this.helper == '') {
		return;
	}
	
	this.redisplay();
    Event.observe(this.elem, "focus", this.clear);
    Event.observe(this.elem, "blur",  this.redisplay);
  }
  
  this.clear = function(evt) {
    if (me.helper == me.elem.value && me.active) {
      me.elem.value = "";  
	  Element.removeClassName(elem, 'placeholder');
	  me.active = false;
    }
  }
  
  this.redisplay = function(evt) {
    if (me.elem.value == "" && !me.active) {
      me.elem.value = me.helper;
	  Element.addClassName(elem, 'placeholder');
	  me.active = true;
    }  
  }
}

function register(collection) {
  var i, elem, listener;
  for (i = 0; elem = collection.item(i++); ) {
    if (elem.nodeName == "INPUT" && 
        (elem.getAttribute("type") != "text" && elem.getAttribute("type") != "search" && elem.getAttribute("type") != "password"))
        {continue;}
    listener = new FieldListener(elem);
    listener.init();  
  }
}

function __initFieldPlaceholders() {  
  if (document.getElementsByTagName) {
    var input_flds = document.getElementsByTagName('input');
    register(input_flds);
    var textarea_flds = document.getElementsByTagName('textarea');
    register(textarea_flds);
  }
}

function showDialog(url) {
	new Ajax.Updater('dialog-contents',url, {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'dialog-contents'], 
			onLoading:function(request) {
				$('dialog-contents').update(''); 
				$('dialog-contents').addClassName('loading'); 
				$('dialog').show();
				
				Position.clone('page', 'dialog-background');
				new Effect.Appear('dialog-background', {duration: 0.5});
			}, 
			
			onComplete:function(request){
				$('dialog-contents').removeClassName('loading');
			}
		});
}

function hideDialog() {
	$('dialog').hide();
	new Effect.Fade('dialog-background', {duration: 0.25});
}

function hideScreenMessage(mid) {
	$('message' + mid).hide();
	new Ajax.Request('/messages/read/' + mid);
}

Event.observe(window, 'load', __initFieldPlaceholders);
