// Cuwing client-side JavaScript for pop-ups, string validation and DHTML handling
// Ver: $Id: default.js,v 1.13 2006/05/08 14:48:06 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(field, msg, min, max) {
 if (min==null) { min=1; }
 if (max==null) { max=65535; }
 if (!field.value || field.value.length < min || field.value.max > max) {
  alert(msg);
  try {
    field.focus();
	if (field["select"]) {
	  field.select();
	}
  } catch (e) {
    // ignore
  }
  return false;
 }
 return true;
}

function validateRegexp(field, msg, regexp, optional) {
 if ((!field.value && !optional) || (!optional && !regexp.test(field.value))) {
  alert(msg);
  field.focus();
  field.select();
  return false;
 }
 return true;
}

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. 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');
 }
}

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;
}
