/* fadeout fadein effects */
var Fadeout = new Class({
    Implements: [Options],
    
    options: {
        menu_speed: 300
    },
    
    initialize: function(id, options){
        this.setOptions(options);
        var anchors = $$('.anchor');
        anchors.each(function(el){
            el.addEvent('click', function(el){
                // effect removed per Tricia's request 4/12/2010
                if (el.id) {
                    window.location = el.id;
                    /*var frame = $(id);
                    frame.set('morph', {
                        duration: this.options.menu_speed,
                        onComplete: function(){
                            window.location = el.id;
                        }
                    });
                    frame.morph({
                        opacity: 0
                    });*/
                }
            }.bind(this, el));
        }, this);
    }
});

var Fadein = new Class({
    Implements: [Options],
    
    options: {
        menu_speed: 300,
		foregroundID: 'hidepage'
    },
    
    initialize: function(id, options){
        this.setOptions(options);
        var menu = $(id);
        menu.setOpacity(0);
        menu.set('morph', {
            duration: this.options.menu_speed
        });
        menu.morph({
            opacity: 1
        });
        var hidepage = $(this.options.foregroundID);
        hidepage.setOpacity(1);
        hidepage.set('morph', {
            duration: this.options.menu_speed
        });
        hidepage.morph({
            opacity: 0
        });
        
    }
});

var ValidAndSubmit = new Class({
    initialize: function(el){
		var form = $(el);
		form.addEvent('submit', function(e){
			//Prevents the default submit event from loading a new page.
			e.stop();
            var errorMessages = new Hash({
                mandatory: " is mandatory",
                email: " doesn't have a valid email format"
            });
            var inputFieldArray = form.getElements('.input').extend(form.getElements('.errorfield'));
			var roarMessage='';
            inputFieldArray.each(function(inputfield){
				if (!inputfield.hasClass('submitbutton')) {
	                inputfield.addClass('input');
	                inputfield.removeClass('errorfield');
	                if (inputfield && inputfield.hasClass('mandatory') && !this.checkMandatory(inputfield)) {
	                    labelname = inputfield.getParent().getPrevious().get('text').clean().replace("*", "");
						var errorDiv=inputfield.getParent().getNext();
	                    inputfield.removeClass('input');
	                    inputfield.addClass('errorfield');
						if (errorDiv.hasClass('error')) {
							errorDiv.set('html', '<strong>'+labelname+'</strong>', errorMessages.get('mandatory'));
						} else {
							// If no error div: we use a notification as designed in the roar library
							roarMessage += '<strong>'+labelname+'</strong>' +  errorMessages.get('mandatory') + "<br/>";
						}
	                }
				}
            }, this);
			if (roarMessage) {
				var roar = new Roar({
					position: 'upperLeft',
					duration: 3000
				});
				roar.alert('Please correct the following:', roarMessage);
			} else if (!form.getElements('input.errorfield').length){
				//Empty the log and show the spinning indicator.
				var log = $('loader').empty().addClass('ajax-loader');
				log.set('html', 'sending...');
				//Set the options of the form's Request handler. 
				form.set('send', {onComplete: function(response) { 
					log.removeClass('ajax-loader');
					log.set('html', response);
		            inputFieldArray.each(function(inputfield){
		                inputfield.addClass('input');
		                inputfield.removeClass('errorfield');
		                inputfield.value='';
					});
				}});
				//Send the form.
				form.send();
            }
        }.bind(this));
    },
    checkMandatory: function(element){
        return (element.value);
    }
});

/* format date (like simpledateformat in JDK) */

Date.MONTH_NAMES = [
"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December" ];

Date.WEEKDAY_NAMES = [
"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday",
"Saturday" ];

Date.prototype.clone = function () {
  return new Date(this.getTime());
};

Date.prototype.clearTime = function () {
  this.setHours(0); this.setMinutes(0);
  this.setSeconds(0); this.setMilliseconds(0);
  return this;
};

Date.prototype.lastDay = function () {
  var tempDate = this.clone();
  tempDate.setMonth(tempDate.getMonth()+1);
  tempDate.setDate(0);
  return tempDate.getDate();
};

Date.prototype.getYearDay = function () {
  var today = new Date(this);
  today.setHours(0); today.setMinutes(0); today.setSeconds(0);
  var tempDate = new Date(today);
  tempDate.setDate(1);
  tempDate.setMonth(0);
  return Math.round(
  (today.getTime() - tempDate.getTime())
  / 86400 / 1000) + 1; // Jan/1 is day 1
};

Date.prototype.format = function(formatString) {
  var out = new String();
  var token = "";
  for (var i = 0; i < formatString.length; i++) {
    if (formatString.charAt(i) == token.charAt(0)) {
      token = token.concat(formatString.charAt(i));
      continue;
    }
    out = out.concat(this.convertToken(token));
    token = formatString.charAt(i);
  }
  return out + this.convertToken(token);
};

Date.prototype.convertToken = function (str) {
  switch(str.charAt(0)) {
    case 'y': // set year
      if (str.length > 2)
      return this.getFullYear();
      return this.getFullYear().toString().substring(2);
    case 'd': // set date
      return Date.zeroPad(this.getDate(),str.length);
    case 'D': // set day in year
      return this.getYearDay();
    case 'a':
      return this.getHours() > 11 ? "PM" : "AM";
    case 'H': // set hours
      return Date.zeroPad(this.getHours(),str.length);
    case 'h':
      return Date.zeroPad(this.get12Hours(),str.length);
    case 'm': // set minutes
      return Date.zeroPad(this.getMinutes(),2);
    case 's': // set secondes
      return Date.zeroPad(this.getSeconds(),2);
    case 'S': // set milisecondes
      return Date.zeroPad(this.getMilliseconds(),str.length);
    case 'x': // set epoch time
      return this.getTime();
    case 'Z': // set time zone
      return (this.getTimezoneOffset() / 60) + ":" +
      Date.zeroPad(this.getTimezoneOffset() % 60,2);
    case 'M': // set month
      if (str.length > 3) return this.getFullMonthName();
      if (str.length > 2) return this.getShortMonthName();
      return Date.zeroPad(this.getMonth()+1,str.length);
    case 'E': // set dow
      if (str.length > 3) return this.getDOWName();
      if (str.length > 1) return this.getShortDOWName();
      return this.getDay();
      default:
      return str;
  }
};

Date.prototype.getFullMonthName = function() {
  return Date.MONTH_NAMES[this.getMonth()];
};

Date.prototype.getShortMonthName = function() {
  return Date.MONTH_NAMES[this.getMonth()].substring(0,3);
};

Date.prototype.getDOWName = function () {
  return Date.WEEKDAY_NAMES[this.getDay()];
};

Date.prototype.getShortDOWName = function () {
  return Date.WEEKDAY_NAMES[this.getDay()].substring(0,3);
};

Date.prototype.get12Hours = function () {
  return this.getHours() == 0 ? 12 :
  (this.getHours() > 12 ? this.getHours() - 12 : this.getHours());
};

Date.zeroPad = function(num, width) {
  num = num.toString();
  while (num.length < width)
  num = "0" + num;
  return num;
};

