// Ver: $Id: default.js,v 1.19 2009-11-26 21:39:57 martin Exp $
var opera = (document.all && navigator.userAgent && navigator.userAgent.indexOf("Opera")>=0);
var ie = (document.all && !opera);
var moz = !document.all;

if (!(document.getElementById && document.images)) {
 location.href=ROOT + "/oldbrowser.html";
}

function openBrowserWindow(url,name,opt) {
 var w=window.open(url,name,opt); if (w) { w.focus(); }; return w;
}

function clientPopup(url,name) {
 return openBrowserWindow(url,name,CLIENT_POPUP_PARAMS);
}

function validateString(f, msg, min, max) {
 if (!min) { min=1; } 
 if (!max) { max=65535; }
 var v=true;
 if (!f.value || f.value.length<min || f.value.max>max) {
  alert(msg);
  try {
   f.focus();
   if (f["select"]) { f.select(); }
  } catch (e) {}
  v=false;
 }
 return v;
}

function validateRegexp(f, msg, re, opt) {
 var v=true;
 if ((!f.value && !opt) || (f.value && !re.test(f.value))) {
  alert(msg);
  try {
   f.focus();
   if (f["select"]) { f.select(); }
  } catch (e) {}
  v=false;
 }
 return v;
}

function changeBackground(divId,imgPath) {
 var obj = document.getElementById(divId);
 if (obj) {
  if (ie) {
   var s = ROOT + "/img/" + imgPath + "'";
   obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=" + s + ", sizingMethod='scale')";
  } else {
   var i = "url('" + ROOT + "/img/" + imgPath + "')";
   obj.style.backgroundImage = i;
  }
 }
}

// Toggle elements display on/off by CSS display.
// Arguments: varargs string[element id]
function toggle() {
 for (var i = 0; i < arguments.length; i++) {
  var elementId = arguments[i];
  var element = elementId ? document.getElementById(elementId) : null;
  if (element) element.style.display = (element.style.display == 'none' ? '' : 'none');
 }
}

// Toggle element display to explicit state by CSS visibility.
// Arguments: id string[element id], visibilityFlag string["1"|"0"]
function toggleVisibility(id, visibilityFlag) {
    var e = false;
    if (id) e = document.getElementById(id);
    if (!e || !e.style.visibility) return;
    e.style.visibility = visibilityFlag == "1" ? "visible" : "hidden";
}

function addClassName(element, className) {
 if (!element) return;
 removeClassName(element, className);
 element.className += ' ' + className;
}

function removeClassName(element, className) {
 if (!element) return;
 var newClassName = '';
 var a = element.className ? element.className.split(' ') : '';
 for (var i = 0; i < a.length; i++) {
  if (a[i] != className) {
   if (i > 0) newClassName += ' ';
   newClassName += a[i];
  }
 }
 element.className = newClassName;
}

/* TODO: Encapsulate all functions in Cuwing class */
function Cuwing() {
}

/* addEventListener: cross-browser function for adding listeners in event chain */
Cuwing.prototype.addEventListener = function(element, eventType, eventListener, useCapture) {
    if (element) {
        if (element.addEventListener) {
            if (!useCapture) {
                useCapture = false;
            }
            element.addEventListener(eventType, eventListener, useCapture);
        } else if (element.attachEvent) {
            element.attachEvent("on" + eventType, eventListener);
        }
    }
}

/* getCookie: returns value (or null) of cookie with specified name */
Cuwing.prototype.getCookie = function(name) {
    if (!document.cookie) return null;
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

/* log: non-intrusive alert using (in priority order): FireBug console, window status */
Cuwing.prototype.log = function(message) {
    if (typeof(console) != "undefined") {
        console.info(message)
    } else {
        window.status = message;
    }
}

var cuwing = window.cuwing || new Cuwing();
