// ================================
// EMAIL ADDRESS VALIDATION
// ================================


function isValidEmail(email) {
	return /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email);
}

// ================================
// VISIBILITY
// ================================

function setVisibility(id,visibility){
  var style = document.getElementById(id).style;
  //style.visibility = visibility?'visible':'hidden';
  style.display = visibility?'block':'none';
}
function isVisible(id,initiallyVisible){
  var display = document.getElementById(id).style.display;
  return !(display && display=='none');
}
function toggleVisibility(id,initiallyVisible){
  setVisibility(id,!isVisible(id,initiallyVisible));
}
function toggleVisibilityImg(id,initiallyVisible,img,plusImg,minusImg){
  toggleVisibility(id,initiallyVisible);
  $(img).src = isVisible(id,initiallyVisible)?minusImg:plusImg;
  //alert([img.src, plusImg, minusImg,isVisible(id,initiallyVisible)].toSource());
}
function toggleVisibilityFocus(id,initiallyVisible,focusEltId){
  var v = !isVisible(id,initiallyVisible);
  setVisibility(id,v);
  if(v) document.getElementById(focusEltId).focus()
}

function toggleVisibilityTimeout(id,initiallyVisible,millisecs){
  var v = !isVisible(id,initiallyVisible);
  setVisibility(id,v);
  if(v) setTimeout(function(){setVisibility(id,false)},millisecs);
}

// =======================================
// DOM MANIPULATION
// Copyright Amergin Software 2005
// =======================================

function clearChildren(elt){
  var to_remove = new Array();

  var childNodes = elt.childNodes;
  for(var i = 0; i < childNodes.length; i++){
  	to_remove.push(childNodes[i]);
  }

  for(var i = 0; i < to_remove.length; i++){
  	Element.remove(to_remove[i]);
  }
}

var element = function(){
  
  var name = arguments[0];
  var extras = [];
  for(var i = 1; i < arguments.length; i++){
    extras.push(arguments[i]);
  }
  return function(){
    var result = document.createElement(name);
    for(var i = 0; i < extras.length; i++){
      extras[i](result);
    }
    try{
    if(arguments.length>0)
      arguments[0].appendChild(result);
    }catch(e){
      //alert(arguments[0]);
    }
    return result;
  }
};

var attribute = function(name,value){
  return function(xml){
    try{
      xml[name]=value;
    }catch(e){
      xml.setAttribute(name,value);
    }
    return null;
  };
};

var text = function(string){
  return function(xml){
    var result = document.createTextNode(string);
    xml.appendChild(result);
    return result;
  }
};



// ================================
// FCK EDITOR
// ================================

var fckarea = function(textarea_id){
	var editor = new FCKeditor('item_content');
  editor.BasePath = "/commonassets/fck/";
	editor.ToolbarSet = 'Rich' 
  editor.ReplaceTextarea() ;
};


// ================================
// STRFTIME: 
// http://whytheluckystiff.net/ruby/strftime.js
// ================================

/* other support functions -- thanks, ecmanaut! */
var strftime_funks = {
  zeropad: function( n ){ return n>9 ? n : '0'+n; },
  a: function(t) { return ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][t.getDay()] },
  A: function(t) { return ['Sunday','Monday','Tuedsay','Wednesday','Thursday','Friday','Saturday'][t.getDay()] },
  b: function(t) { return ['Jan','Feb','Mar','Apr','May','Jun', 'Jul','Aug','Sep','Oct','Nov','Dec'][t.getMonth()] },
  B: function(t) { return ['January','February','March','April','May','June', 'July','August',
      'September','October','November','December'][t.getMonth()] },
  c: function(t) { return t.toString() },
  d: function(t) { return this.zeropad(t.getDate()) },
  H: function(t) { return this.zeropad(t.getHours()) },
  I: function(t) { return this.zeropad((t.getHours() + 12) % 12) },
  m: function(t) { return this.zeropad(t.getMonth()+1) }, // month-1
  M: function(t) { return this.zeropad(t.getMinutes()) },
  p: function(t) { return this.H(t) < 12 ? 'AM' : 'PM'; },
  S: function(t) { return this.zeropad(t.getSeconds()) },
  w: function(t) { return t.getDay() }, // 0..6 == sun..sat
  y: function(t) { return this.zeropad(this.Y(t) % 100); },
  Y: function(t) { return t.getFullYear() },
  '%': function(t) { return '%' }
};

Date.prototype.strftime = function (fmt) {
    var t = this;
    for (var s in strftime_funks) {
        if (s.length == 1 )
            fmt = fmt.replace('%' + s, strftime_funks[s](t));
    }
    return fmt;
};

if (typeof(TrimPath) != 'undefined') {
    TrimPath.parseTemplate_etc.modifierDef.strftime = function (t, fmt) {
        return new Date(t).strftime(fmt);
    }
}


