var backmode="normal";
var pageIsLoaded = false;
// -------------------- library functions
function isIE6() {
  var av = navigator.appVersion;
  var i = av.indexOf('MSIE 6');
  return (i < 50 && i > -1);
}
function hide(el,swapto) {
  if (typeof(el) == 'string') { el = document.getElementById(el); }
  if (el && el.style) { el.style.display = 'none'; }
  if (swapto) {
    if (typeof(swapto) == 'string') { var elto = document.getElementById(swapto); } else { var elto = swapto; }
    if (elto) {elto.style.display = 'block'; }
  }
}
//
function hideSelects() {
  if (isIE6()) {
    for(var m = 0; m < document.all.tags("SELECT").length; ++m) {
        document.all.tags("SELECT")[m].style.visibility='hidden';
    }
  }
}
//
function showSelects() {
  if (isIE6()) {
    for(var s = 0; s < document.all.tags("SELECT").length; ++s) {
        document.all.tags("SELECT")[s].style.visibility='visible';
    }
  }
}
//
function mouseX(evt) {
if (evt.pageX) return evt.pageX;
else if (evt.clientX)
   return evt.clientX + (document.documentElement.scrollLeft ?
   document.documentElement.scrollLeft :
   document.body.scrollLeft);
else return null;
}
//
function mouseY(evt) {
if (evt.pageY) return evt.pageY;
else if (evt.clientY)
   return evt.clientY + (document.documentElement.scrollTop ?
   document.documentElement.scrollTop :
   document.body.scrollTop);
else return null;
}
//--------------------------------------
function getBounds(ele) {
  var res = { x:0, y:0, w:0, h:0 }
  if (typeof(ele) == 'string') { var el = document.getElementById(ele); } else { var el = ele; }
  if (el) {
    res.x = el.offsetLeft; res.y = el.offsetTop;  res.h = el.offsetHeight;  res.w = el.offsetWidth;
    while((el=el.offsetParent) != null) {
      res.x += el.offsetLeft+(el.clientLeft ? el.clientLeft : 0);
      res.y += el.offsetTop+(el.clientTop ? el.clientTop : 0);
    }
  }
  return res;
}
//--------------------------------------
function xOnPage(x,w,centerIt,ele,fixed) {
  var sw = -1;
  if (self.innerWidth){sw = self.innerWidth;}
  else if (document.documentElement && document.documentElement.clientWidth){sw = document.documentElement.clientWidth;}
  else if (document.body){sw = document.body.clientWidth;}
  var wmin = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : (window.pageXOffset ? window.pageXOffset : 0));
  if (typeof(wmin) != 'number' || wmin < 0) { wmin = 0; }

  if (isIE6()) { fixed = false; }
  if (fixed) { wmin = 0; }

  var sw = sw + wmin;
  if (centerIt) { x = (Math.round((sw - wmin - w) / 2)) + wmin; }
  if ((sw > -1)) {	if (x<0) { x=0; }		if ((x + w) > sw - 10) {	x = sw - w - 10;	} }
		if (x < wmin) { x = wmin; }
  if (ele && w > sw) { ele.style.width = sw-10+'px'; }
		return x;
}
function yOnPage(y,h,centerIt,ele,fixed) { // pass ele to make sure max height is not exceeded
  var sh = -1;
  if (self.innerHeight){sh = self.innerHeight;}
  else if (document.documentElement && document.documentElement.clientHeight){sh = document.documentElement.clientHeight;}
  else if (document.body){sh = document.body.clientHeight;}
  var hmin = (document.documentElement.scrollTop ? document.documentElement.scrollTop : (window.pageYOffset ? window.pageYOffset : 0));
  if (typeof(hmin) != 'number' || hmin < 0) { hmin = 0; }
//alert(hmin+'|'+sh);
  if (isIE6()) { fixed = false; }
  if (fixed) { hmin = 0; }

  var sh = sh + hmin;
  if (centerIt) { y = (Math.round((sh - hmin - h) / 2)) + hmin; }
  if ((sh > -1)) {	if (y<0) { y=0; }		if ((y + h) > sh - 10) {	y = sh - h - 10 ; } }
		if (y < hmin) { y = hmin; }
//alert(ele.offsetHeight+' '+h+' '+sh);
  if (ele && h > sh) { ele.style.height = sh-6+'px'; }
//alert(y);
 	return y;
}
//--------------------------------------
// popup.... are routines to use if you want a mouse listener and optional hotspot.
// use: call popupInit(closefunc, hotid) at the start of your popup show function and pass it your hide/close function.
// hotId is the id of the click-to-close area - pass '' for anywhere on the page or !divid to close anywhere EXCEPT divid.
// If you want to manually close the popup, call popupClose('','myresult') NOT your hide/close function
// 'myresult' will be put in global var popupResult so you can control what to do in the hide/close function
var popupSafeToClose = false;
var popupOMD = null;
var popupDiv = '';
var popupCloseFunc = null;
var popupResult = '';
var popupClosing = false;
var popupPending = null;
var popupClosedTime = 0;
//
function popupInit(closeFunc, hotId) {
  if (popupClosing) { return false;  }
  popupPending = null;
  // tidy up any open popup
  if (popupSafeToClose) { popupRemoveListener(); if (popupCloseFunc) { popupCloseFunc();} popupCloseFunc = null;  popupSafeToClose = false; }
  // prep the listener for the new popup
  popupCloseFunc = closeFunc;  popupDiv = hotId;  popupAttachListener();  hideSelects();  popupSafeToClose = true;
  return true;
}
//
function popupClose(e,closeResult) {
  if (popupSafeToClose) {
    if (e == undefined) var e = window.event; // make sure IE has the event in e
    if (e && e.button && e.button == 2) return;
    if (popupDiv.match('!')) { var pd = popupDiv.substring(1,100); var inpd = false; } else { var pd = popupDiv; var inpd = true; }
    if (pd == '' || (pd != '' && (e == '' || e == 'timer' || (popupInDiv(e,pd,inpd))))) {
      popupClosing = true;
      popupResult = (closeResult==undefined ? null : closeResult);
//      popupResult = ((closeResult == '') || (closeResult==null) || (closeResult==undefined) ? '' : closeResult);
      popupRemoveListener();
      popupSafeToClose = false;
      if (popupCloseFunc) { popupCloseFunc();}
      popupCloseFunc = null;
      showSelects();
      popupClosing = false;
      popupClosedTime = Date.parse(new Date());

      if (popupPending) { popupPending(); popupPending = null; }
    }
  }
}
function popupJustClosed() {
  var now = Date.parse(new Date());
//  alert(now+' '+popupClosedTime);
  var i = Math.abs(now - popupClosedTime);
//  alert(i);
  popupClosedTime = 0;
  return (i < 300);
}
//
function popupAttachListener() {
   	if (document.layers) {	document.captureEvents(Event.MOUSEDOWN);	}
   	popupOMD = document.onmousedown;
   	if (popupOMD != null) {	document.onmousedown = function(event) { popupOMD(event); popupClose(event); } }
    else { document.onmousedown = function(event) { popupClose(event); } }
}
//
function popupRemoveListener() {
   	if (popupOMD != null) {	document.onmousedown = popupOMD; } else { document.onmousedown = null; }
}
//
function popupInDiv(e, divID, testbool) {
  if (!e) var e = window.event; // make sure IE has the event in e
  if (!divID) { divID = popupDiv; }
  if (document.layers) {
			var clickX = e.pageX;
			var clickY = e.pageY;
			var t = document.layers[divID];
			if ((clickX > t.left) && (clickX < t.left+t.clip.width) && (clickY > t.top) && (clickY < t.top+t.clip.height)) {
				return testbool;
			}
		}
		else if (e && e.srcElement) {
			var t = e.srcElement;
			while (t && t.parentElement != null) {
				if (t.id==divID) {	return testbool; }
				t = t.parentElement;
			}
		}
		else if (e && e.originalTarget) {
			var t = e.originalTarget;

   try { if (t.id) {} } // will get error here if firefox and input box is clicked on mar 2010
   catch(errr) { /*alert('err: '+errr);*/ return false;}

   try {
   		while (t && (t.parentNode != null) && (t.parentNode != undefined) && (t.attributes)) {
   				if (t.id==divID) { return testbool; }
 	  			t = t.parentNode;
 	  	}
  	}
   catch(err)
   { alert('error: '+err.description);}
		}
		return !testbool;
}
// end popup listener
//--------------------------------------
function getCookie( check_name ) {
   var a_all_cookies = document.cookie.split( ';' );
   var a_temp_cookie = '';
   var cookie_name = '';
   var cookie_value = '';
   var b_cookie_found = false; // set boolean t/f default f

   for ( i = 0; i < a_all_cookies.length; i++ )
   {
      a_temp_cookie = a_all_cookies[i].split( '=' );
      cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');  // trim
      if ( cookie_name == check_name )
      {
         b_cookie_found = true;
         if ( a_temp_cookie.length > 1 ) { cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') ); }
//alert(document.cookie);
         return cookie_value;
         break;
      }
      a_temp_cookie = null;
      cookie_name = '';
   }
   if ( !b_cookie_found ) { return ''; }
}
//--------------------------------------
// expires is in days
function setCookie( name, value, expires, path, domain, secure ) {
  var today = new Date();
  today.setTime( today.getTime() );
  if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
  var expires_date = new Date( today.getTime() + (expires) );

  var s = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );
  document.cookie = s;
}
//--------------------------------------
function deleteCookie( name, path, domain ) {
  if ( getCookie( name ) ) document.cookie = name + "=" +
     ( ( path ) ? ";path=" + path : "") +
     ( ( domain ) ? ";domain=" + domain : "" ) +
     ";expires=Thu, 01-Jan-1990 00:00:01 GMT";
}
//----
function cssStyle(el, property) {
  var res = el.style[property];
  if (!res) {
    if (el.currentStyle && el.currentStyle[property]) {
      res = el.currentStyle[property];
    } else {
      // this method uses 'background-image' not the passed format of 'backgroundImage'
      prop = "";
      for (var i=0; i < property.length; i++) {
        if (property.charAt(i) == property.charAt(i).toUpperCase()) {
          prop += '-' + property.charAt(i).toLowerCase(); }
        else {
          prop += property.charAt(i); }
      }
      try {
      res = getComputedStyle(el,'').getPropertyValue(prop);
      } catch(err) {}
    }
  }
  if (!res) { res = ''; }
  if (typeof(res) == 'string') { res = res.replace('px',''); }
  return res;
}
//------------
var isModal = true;
var shadowOffset = 8;
var shadowBox = null;

function showShadowBox( id ) {
  var div = createDynamicPopup('shadowBox');  div.style.zIndex = '2000';
  if (div) {
    if (typeof(id) == 'string') { var el = document.getElementById(id); } else {var el = id; }
    var i = cssStyle(el,'zIndex');
    if (i-0 > 0) { div.style.zIndex = (i-1) + ''; }
    var fixed = cssStyle(el,'position');
    if (fixed = 'fixed') {
      hmin = 0; wmin = 0;
    } else {
      var hmin = (document.documentElement.scrollTop ? document.documentElement.scrollTop : (window.pageYOffset ? window.pageYOffset : 0));
      if (typeof(hmin) != 'number' || hmin < 0) { hmin = 0; }
      var wmin = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : (window.pageXOffset ? window.pageXOffset : 0));
      if (typeof(wmin) != 'number' || wmin < 0) { wmin = 0; }
    }
    div.style.top = (el.offsetTop+shadowOffset+hmin) + 'px';
    div.style.left = (el.offsetLeft+shadowOffset+wmin) + 'px';
    div.style.height = el.offsetHeight + 'px';
    div.style.width = el.offsetWidth + 'px';
    div.style.display = 'block';
    shadowBox = div;      // keep a global reference to it
    el.shadow = shadowBox;  // so drag can find the div
  }
}
function hideShadowBox( ) {
  var div = document.getElementById('shadowBox');
  if (div) {
    div.style.display = 'none';
  }
}

function screenSize() {
  var sw = -1;
  if (self.innerWidth){sw = self.innerWidth;}
  else if (document.documentElement && document.documentElement.clientWidth){sw = document.documentElement.clientWidth;}
  else if (document.body){sw = document.body.clientWidth;}
  var wmin = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : (window.pageXOffset ? window.pageXOffset : 0));
  if (typeof(wmin) != 'number' || wmin < 0) { wmin = 0; }
  var sh = -1;
  if (self.innerHeight){sh = self.innerHeight;}
  else if (document.documentElement && document.documentElement.clientHeight){sh = document.documentElement.clientHeight;}
  else if (document.body){sh = document.body.clientHeight;}
  var hmin = (document.documentElement.scrollTop ? document.documentElement.scrollTop : (window.pageYOffset ? window.pageYOffset : 0));
  if (typeof(hmin) != 'number' || hmin < 0) { hmin = 0; }
  return { 'x':wmin, 'y':hmin, 'w':sw, 'h':sh };
}

function documentSize() {
//alert(window.innerHeight+'|'+window.scrollMaxY+'|'+document.body.scrollHeight+'|'+document.body.offsetHeight);
  if (window.innerHeight != undefined && window.scrollMaxY != undefined) {// Firefox
    var y = window.innerHeight + window.scrollMaxY;
    var x = window.innerWidth + window.scrollMaxX;
//  } else if (document.documentElement.clientHeight) {  // dumb opera return the window size not the document
//    var x = document.documentElement.clientHeight;
//    var y = document.documentElement.clientHeight;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    var y = document.body.scrollHeight;
    var x = document.body.scrollWidth;
  } else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    var y = document.body.offsetHeight;
    var x = document.body.offsetWidth;
  }
  if (y == 0)  y = 5000;
  return { 'width':x, 'height':y };
}

function showModalCover( ) {
  var div = document.getElementById('modalCover');
  if (div) {
    div.style.left = '0px';
    div.style.top = '0px';

    var s = screenSize();
    var d = documentSize();
    var h = s.h;
    if (h < d.height) { h = d.height; }
    var w = s.w;
    if (w < d.width) { w = d.width; }
    div.style.width = w+'px';
    div.style.height = h+'px';
    div.style.display = 'block';
  }
}
function hideModalCover( ) {
  var div = document.getElementById('modalCover');
  if (div) {
    div.style.display = 'none';
  }
}

var globZindex = 7000;
function createDynamicPopup(id, modal, style, parent ) {
  if (modal) { var cover = createDynamicPopup('modalCover');  cover.style.zIndex = ''; }

//  var div = document.getElementById(id);
// 	if (div) { div.parentNode.removeChild(div);

  var div = document.getElementById(id);
  if (!div) {
    var div = document.createElement('div');
    div.setAttribute('id', id);
    div.setAttribute('name', id);
    if (style) { div.className = style; }
    if (!parent || parent == undefined) { parent = document.body }
    parent.appendChild(div);
  }
  div.style.display = 'none';
  globZindex++;
  div.style.zIndex = globZindex;
  return div;
}

function integerOnly(event) {  // onkeydown event
  event = event || window.event;
  var key = (event.keyCode ? event.keyCode : event.which);
  var shift = (event.shiftKey ? event.shiftKey : event.shiftKey);
// 0..9 with no shift, 0-9 keypad, return-13, tab-9, del-46, backspace-8, left-37, right-39, home-36, end-35
//document.title = key+'x'+shift;
  if ((((key >= 48) && (key <= 57)) && shift!=1) || ((key >= 96) && (key <= 105)) || (key == 13) || (key == 8) || (key == 9) || (key == 46) || (key == 39) || (key == 37) || (key == 35) || (key == 36))
    { return true; }
  else
    { event.returnValue = false; return false; }
}

function poundsOnly(event) {  // onkeydown event
  event = event || window.event;
  var key = (event.keyCode ? event.keyCode : event.which);
//document.title = key;
  var shift = (event.shiftKey ? event.shiftKey : event.shiftKey);
  if ((((key >= 48) && (key <= 57)) && shift!=1) || ((key >= 96) && (key <= 105)) || (key == 13) || (key == 8) || (key == 9) || (key == 46) || (key == 39) || (key == 37) || (key == 35) || (key == 36) || (key == 110) || (key == 190 && shift!=1))
    { return true; }
  else
    { event.returnValue = false; return false; }
}

function checkPoundsOnly() {  // onblur event
   var v = document.getElementById('manual_value').value;
   v = Math.floor(v);
   if (isNumeric(v)) {
     document.getElementById('manual_value').value = v+'.00';
   }
 }

function checkIsPounds() {  // onblur event
   var v = document.getElementById('manual_value').value;
   var v1 = Math.floor(v);
//   if (isNumeric(v1)) {
   if (v) {
     var vs = v+'.00';
     var a = vs.split('.');
     var i = a.length-2;
     if (i < 2 || !a[i]) { i = 1; }
     a[i] = a[i]+'00'; a[i] = a[i].substring(0,2);
     document.getElementById('manual_value').value = a[0]+'.'+a[i];
   }
 }

function checkWholePounds(ele) { // compatible with checkData customfunc
  var res = { ok : true, errstr : '' }
  var v = document.getElementById('manual_value').value;
  if (v) {
    var v1 = Math.floor(v);
    if (v1 != v || !isNumeric(trim(v))) {
      res.ok = false;
      res.errstr = 'Please enter an amount in whole pounds eg £'+v1+'.00 not £'+v+'<br>';
    }
  }
  return res;
}

function checkValidPounds(ele) { // compatible with checkData customfunc
  var res = { ok : true, errstr : '' }
  var v = document.getElementById('manual_value').value;
  if (v) {
    var v1 = Math.floor(v);
    var reg = new RegExp("^-?\\d+\\.\\d{2}?$");
    if (v1 == v && isNumeric(trim(v))) {
    }
    else if (!reg.test(v)) {
      res.ok = false;
      res.errstr = 'Please enter an valid pound amount eg 10.00 not '+v+'<br>';
    }
  }
  return res;
}

//
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
//
// load most of the images after the main body has loaded.
// to use just change img src="??" to img data-src="??"
function postLoadImages() {
  var Coll = document.getElementsByTagName("IMG");
  for (var m = 0; m < Coll.length; ++m) {
    if (Coll[m].getAttribute('data-src')) {
      Coll[m].src = Coll[m].getAttribute('data-src');
      Coll[m].setAttribute('data-src','');
    }
  }
}
/*function addLoadEvent(func, first) {  // set first to true to add this event at the front of the queue
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
  window.onload = function() {
      if (first) { func(); }
      if (oldonload) { oldonload(); }
      if (!first) { func(); } // an error here usually means you have passed the func with ()
    }
  }
}
function pageLoaded() { pageIsLoaded = true; }
addLoadEvent(pageLoaded);
*/
//var loadEvents = new Array;
//function addLoadEvent(func, first) {  // set first to true to add this event at the front of the queue
//  loadEvents[loadEvents.length] = { 'func':func, 'first':(first==true ? true : false) }
//}
function backController() {
//alert(backmode);
  var el = document.getElementById('backctrl');
  if (el) {
    if (el.value=='LOADED') {
      if (backmode == 'normal') {
        refreshMiniBasket();
      }
      if ((backmode == 'basket') || (backmode == 'lock')){
        document.body.innerHTML = '';
        location.reload(true);
      }
    }
    else { var xx = window.setTimeout('backController2()',100); }
  }
}
function backController2() {
  var el = document.getElementById('backctrl');
  if (el) {
    if (el.value=='OK') { el.value = 'LOADED'; }
    else {
      if (backmode == 'normal') {
        refreshMiniBasket();
      }
      if ((backmode == 'basket') || (backmode == 'lock')) {
        document.body.innerHTML = '';
        location.reload(true);
      }
    }
  }
}
function doLoad() {
  pageIsLoaded = true;
  backController();

  for (var i=loadEvents.length-1; i >= 0; i--) {
    if (loadEvents[i].first) { loadEvents[i].func(); }
  }
  for (var i=0; i<loadEvents.length; i++) {
    if (!loadEvents[i].first) { loadEvents[i].func(); }
  }
}
function doUnLoad() {
// just here so ff onload fires when back button is clicked
}
//
function checkLock() {
  var el = document.getElementById('pagelock');
  if (el) {
    if (el.value=='LOCKED') {
      document.body.innerHTML = '';
      location = window.location.href;
    }
    else { var xx = window.setTimeout('checkLock2()',1); }
  }
}
function checkLock2() {
  var el = document.getElementById('pagelock');
  if (el) {
//    alert('onloadtimer'+el.value)
    if (el.value=='OK') { el.value = 'LOCKED'; }
    else {
      document.body.innerHTML = '';
      location = window.location.href;
    }
  }
}
function setHeight() {
var y = -1;
if (self.innerHeight){y = self.innerHeight;}
else if (document.documentElement && document.documentElement.clientHeight){y = document.documentElement.clientHeight;}
else if (document.body){y = document.body.clientHeight;}
if ((y > 100) && (document.body.offsetHeight) && (y > document.body.offsetHeight))
  { document.body.style.height = y + 'px'; }
}
//--------------------------------------
function noBack(path) {
// call this rather than loction= to stop the current page being added to the browser history
// not tested but the logic needs to be replace the url rather than locate to another
  window.URL.replace(path);
}
//
function imageErr(el, id, hideel, stopclick, restart) {
// usage = <img onerror="imageErr(this, 'imagesize', 'hide1;hide2', 'id1;id2;over|id1;out|id1');" >
// stopclick can be used to switch off mouseover and mouseout by adding 'over|' of out| before the id
  var safe = el.onerror;
  el.onerror = null;  // stop stack overflow if err image doesnt exist
  if (id == 'large' || id == 'zoom' || id == 'medium' || id == 'thumb' || id == 'small' || id == 'smallhero') {
    id = thisUrl+'products/images/coming_soon_'+id+'.gif';
  }
  if (id == 'tall' ) { id = thisUrl+'products/images/coming-soon-tall.jpg'; }
  if (id == 'swatch' ) { id = thisUrl+'products/images/no_swatch.jpg'; }
  el.src=id;
//hideel = '';
  if (hideel) {
    var ar = hideel.split(';');
    for (var i=0; i<ar.length; i++) {
      if (ar[i]) {
        ely = document.getElementById(ar[i].replace(' ',''));
        if (ely) {ely.style.display='none'; }
      }
    }
  }
  if (stopclick) {
    var ar = stopclick.split(';');
    for (var i=0; i<ar.length; i++) {
      if (ar[i]) {
        var typ = '';
        var nam = ar[i].replace(' ','');
        if (nam.match(/over\|/i)) { typ = 'min'; nam = nam.substr(5); }
        if (nam.match(/out\|/i)) { typ = 'mout'; nam = nam.substr(4); }
        ely = document.getElementById(nam);
        if (ely) {
          if (typ == '') { ely.onclick = null; ely.style.cursor = 'normal'; }
          if (typ == 'min') { ely.onmouseover = null; }
          if (typ == 'mout') { ely.onmouseout = null; }
        }
      }
    }
  }
  if (restart) {
    el.onerror = safe;
  }
}
function initAlertX(mode, buttons, justify, modal) {
  var id = 'alertX';
  el = createDynamicPopup(id, modal);
  var s = '';
  s+='<div id="alertXHead">';
  s+='<div id="alertXHeadText">Confirm</div>';
  s+='<div id="alertXClose"><img id="alertXCloseBtn" src="assets/images/headerbtn.jpg"  alt="close" title="close" /></div>';
  if (mode == 'alert') {  s+='<div id="alertXIconI"></div>'; } else {  s+='<div id="alertXIconQ"></div>'; }
  s+='</div>';
  s+='<div id="alertXMessage" style="text-align:'+justify+'">';
  s+='</div>';
  if (buttons) {
    s+='<div id="alertXPrompt"><table  id="alertXPromptTable" align="center" cellspacing="0" cellpadding="0" style="width:auto; margin:0 auto; padding:0;" ><tr>';
    for (var key in buttons) {
      var val = buttons[key];
      s+='<td align="center" style="padding:4px 5px 0 5px;"><a class="BMBtn" onclick="popupClose(\'\',\''+val+'\');" style="width:100px; margin:0px 0;" alt="" title="">'+key+'</a></div></td>';
    }
    s+='</tr></table></div>';
  }
  if (el) {
    el.innerHTML = s;
    var el1 = document.getElementById('alertXHead');
    AlertX = DragHandler.attach(el1,el); // make it draggable
  }
  return el;
}
function closeAlertX() {
  var el = document.getElementById('alertX');
  if (el) { el.style.display = 'none'; }
  hideShadowBox();
  hideModalCover();
  if (el.closer && el.closer != undefined) { el.closer(popupResult); }
}
function showAlertX(s,mode, buttons, width, justify, closer, modal ) {
  el = initAlertX(mode, buttons, justify, modal);
  if (el) {
    if (!popupInit(closeAlertX,'alertXCloseBtn')) {} //{ popupPending = new Function( "mode = '"+mode+"'; s= '"+s+"'; showAlertX(s,mode);" ) }
    el.closer = closer;
    var elm = document.getElementById('alertXMessage');

    showModalCover();
    var twid = 0;
    temp = createDynamicPopup('alertXT');
    if (temp) {
      temp.innerHTML = s;
      temp.style.display = 'block';
      twid = temp.offsetWidth-0;  // this is the width the text wants to try to be
    }
    var ss  = screenSize();
    el.style.display = 'block';

    var elp = document.getElementById('alertXPromptTable');
    if (elp && elp.offsetWidth > twid) { twid = elp.offsetWidth; }
    if (twid == 0) { twid = 400; }
    if (twid < 200) { twid = 200; }
    if (width) { twid = width; }
    elm.innerHTML = s;

    if (twid > ss.w-wborder(el)-16) { twid = ss.w-wborder(el)-16; }
    elm.style.width = twid + 'px';
    el.style.width = twid + wborder(elm) + 'px';
    if (el.offsetHeight > ss.h-16) {
      var elh = document.getElementById('alertXHead');
      var maxmh = ss.h-elp.offsetHeight-elh.offsetHeight-hborder(elm)-hborder(el)-16;
      if (elm.offsetHeight > maxmh) { elm.style.height = maxmh+'px'; }
//      el.style.height = ss.h-hborder(el)-16+'px';
    }

    var x = el.offsetLeft;
    var y = el.offsetTop;
    var w = el.offsetWidth;
    var h = el.offsetHeight;
    x = xOnPage(x,w,true);
    y = yOnPage(y,h,true);

    el.style.left = x + 'px';
    el.style.top  = y + 'px';
    if (!modal) { showShadowBox(el); }
  }
  return false;
}

function alertX(s,closeFunc, width) {
  if (!s || s == undefined) { s = ''; }
  s = s.replace(/\n/g,'<br>');
  showAlertX(s,'alert',{ 'OK':'ok' }, width, 'center', closeFunc, isModal);
}
function confirmX(s, closeFunc, width) {
  s = s.replace(/\n/g,'<br>');
  showAlertX(s,'confirm',{ 'OK':'ok', 'Cancel':'cancel' }, width, 'left', closeFunc, isModal);
}
function promptX(s, closeFunc, width, buttons) {
  s = s.replace(/\n/g,'<br>');
  showAlertX(s,'confirm',buttons, 'left', closeFunc, isModal);
}

function makeTable(fmt, s, tclass) {
  if (tclass) { tclass = ' class='+tclass+'"'; }
  res = '<table cellspacing="1" cellpadding="0" border="1" bgcolor="black" '+tclass+'>';

/*  var a = fmt.split(',');
  res += '<tr>';
  for (var i=0; i<a.length; i++) {

    var wid = 0; var just = 'L';
    for (var j=0; j<a[i].length; j++) {

    }
    
    res += '<td width="">'+a[i]+'</td>';
  }
  res += '</tr>';
*/
  res += '</table>';
  return res;
}
//stopRightClick();
//addLoadEvent(templateSwitch);
//addLoadEvent(preLoad);
//addLoadEvent(setHeight);
//addLoadEvent(postLoadImages);
//
