function isNull(a) { return typeof a == 'object' && !a; }

var is = { init:function() {
  this.ua = navigator.userAgent.toLowerCase();
  this.major = parseInt(navigator.appVersion);
  this.minor = parseFloat(navigator.appVersion);
  this.nav   = ( (this.ua.indexOf('mozilla')!=-1) && ((this.ua.indexOf('spoofer')==-1) && (this.ua.indexOf('compatible') == -1)));
  this.nav4  = (this.nav && (this.major == 4));
  this.nav4up= (this.nav && (this.major >= 4));
  this.nav5up= (this.nav && (this.major >= 5));
  this.gecko = (this.ua.indexOf('gecko') != -1);
  this.ie    = (this.ua.indexOf("msie") != -1);
  this.ie3   = (this.ie && (this.major == 2));
  this.ie4   = ( this.ie && (this.major == 4) && (this.ua.indexOf("msie 5.0")==-1));
  this.ie4up = (this.ie  && (this.major >= 4));
  this.ie5up = (this.ie && !this.ie3 && !this.ie4);
  this.opera = (this.ua.indexOf("opera") != -1);
  this.hotjava = (this.ua.indexOf("hotjava") != -1);
  this.webtv = (this.ua.indexOf("webtv") != -1);
  this.aol   = (this.ua.indexOf("aol") != -1);
  this.safari= (this.ua.indexOf("safari") != -1);
} }
is.init();

function create_window(url,wdth,hght,name,scrlbr,centerXoffset,centerYoffset) 
{   // can be improved to center vertically:
	if (document.all)
	{
		var xMax = screen.width, yMax = screen.height;
	}
	else
	{
		if (document.layers) var xMax = window.outerWidth, yMax = window.outerHeight;    // else var xMax = 640, yMax=480;
	}
	var centerXset = centerXoffset;
	var centerYset = centerYoffset;
	var xOffset    = ((xMax - wdth)/2)-centerXset;
	var yOffset    = ((yMax - hght)/2)-centerYset;
	var features   = 'width=' + wdth + ',height=' + hght + ',directories=' + 0 + ',location=' + 0 + ',menubar=' + 0 +
                     ',scrollbars=' + scrlbr + ',status=' + 0 + ',toolbar=' + 0 + ',resizable=' + 1 + ',titlebar=' + name +
                     ',screenX=' + xOffset + ',screenY=' + yOffset + ',top=' + yOffset + ',left=' + xOffset;
	var aWindow = window.open (url, name, features);
}
function picture(width,height,location,description) {
    var options;
    var needscroll=false;
    var top = (screen.availHeight - height)/2, left = (screen.availWidth - width)/2;
    options = "top="+top+",left="+left+",location=0,status=0,";
    options = options+"toolbar=0,menubar=0,directories=0,width=";
    if (width <= screen.availWidth) {options = options + width;}
    else
        {
        value = screen.availWidth-12;
        options=options + value;
        needscroll=true;
        }
    options = options + ",height=";
    if (height <= screen.availHeight) {options = options + height;}
    else {
        value = screen.availHeight-30;
        options = options + value;
        needscroll = true;
        }
    if (needscroll == true) {options = "scrollbars,resizable," + options;}
    else {options = "scrollbars=0,resizable=0," + options;}
    if (window.pici) pici.close();
    pici=window.open("","Picture",options);
    pici.document.open();
    pici.document.write("<html><head><title>"+description+"</title></head><body marginwidth=\"0\" marginheight=\"0\" leftmargin=\"0\" topmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\" onload=\"window.focus();\"><a href=\"javascript:window.close();\"><img src="+location+" border=\"0\" hspace=\"0\" vspace=\"0\" alt=\"Click image to close\" /></a></body></html>");
    pici.document.close();
    }

function popup(width,height,location) {
    var options;
    var needscroll=false;
    var top = (screen.availHeight - height)/2, left = (screen.availWidth - width)/2;
    options = "top="+top+",left="+left+",location=0,status=0,";
    options = options+"toolbar=0,menubar=0,directories=0,width=";
    if (width <= screen.availWidth) {options = options + width;}
    else
        {
        value = screen.availWidth-12;
        options=options + value;
        }
    options = options + ",height=";
    if (height <= screen.availHeight) {options = options + height;}
    else {
        value = screen.availHeight-30;
        options = options + value;
        }
    options = "scrollbars,resizable," + options;
    if (window.newwin) newwin.close();
    newwin=window.open(location,"popup",options);
    }

function MM_findObj(n, d) {
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n);
    return x;
}

function MM_jumpMenu(targ,selObj,restore) {
    eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
    if (restore) selObj.selectedIndex=0;
}

function MM_jumpMenuGo(selName,targ,restore) {
    var selObj = MM_findObj(selName);
    if (selObj) MM_jumpMenu(targ,selObj,restore);
}

// generic

function isFunction(a) { return typeof a == 'function'; }
function isObject(a) { return (a && typeof a == 'object') || isFunction(a); }
function isString(a) { return typeof a == 'string'; }
function isUndefined(a) { return typeof a == 'undefined'; }
function isBoolean(a) { return typeof a == 'boolean'; }
function isArray(a) { return isObject(a) && a.constructor == Array; }
function StoA(a) { b = a.split(','); c = new Array(); for(var d in b) c[c.length] = parseInt(b[d]); return c; }

function confirmLink(data) { if (confirm(data)) { return true; } else { return false; } }

function toggleVis()
    { 
    var argv = toggleVis.arguments;
    for(var j=0;j<argv.length;j++)
        {
        if (document.getElementById)
            {
            document.getElementById(argv[j]).style.display = (document.getElementById(argv[j]).style.display == '') ? 'none' : '';
            document.getElementById(argv[j]+'_').style.display = (document.getElementById(argv[j]+'_').style.display == '') ? 'none' : '';
            }
        else if (document.all)
            {
            document.all[argv[j]].style.display = (document.all[argv[j]].style.display == '') ? 'none' : '';
            document.all[argv[j]+'_'].style.display = (document.all[argv[j]+'_'].style.display == '') ? 'none' : '';
            }
        }
    }

// autocomplete functions


function getXMLHTTP(){
  var A=null;
  try{
    A=new ActiveXObject("Msxml2.XMLHTTP")
  }catch(e1){
    try{
      A=new ActiveXObject("Microsoft.XMLHTTP")
    } catch(e2){
      A=null
    }
  }
  if(!A && typeof XMLHttpRequest != "undefined") {
    A=new XMLHttpRequest()
  }
  return A
}

var http = getXMLHTTP();
var t = null;
var lastSearch = '';
var thisSearch = '';
var handleFrm = '';

function handleStart(frm)
    {
    handleFrm = 'entryForm'+frm;
    if (t) { window.clearTimeout(t); }
    t = window.setTimeout("handleRequest()",200);
    }

function handleRequest()
    {
    var e = document.getElementById(handleFrm);
    var l = 't';
    if (isString(e.entryType.value))
        {
        l = e.entryType.value;
        }
    else
        {
        for(i=0;i<e.entryType.length;i++)
            {
            if (e.entryType[i].checked) { l = e.entryType[i].value; }
            }
        }
    thisSearch = l + document.getElementById(e.entryField.value).value;
    if (thisSearch.length <= 1)
        {
        lastSearch = thisSearch;
        return false;
        }
    if (thisSearch != lastSearch)
        {
        if (http && http.readyState < 4) { http.abort(); }
        http.open("GET","/auto/entry?"+thisSearch,true);
        http.onreadystatechange = handleResponse;
        http.send(null);
        lastSearch = thisSearch;
        }
    }

function handleResponse()
    {
    if (http.readyState==4 && http.responseText) { if (http.responseText.charAt(0)!='<') { eval(http.responseText); } }
    }

function newData()
    {
    var list = newData.arguments[0];
    if (!list) return;
    var item = ''; var v = '';
    for (var i in list) { v = v + list[i] + ' (' + i + ')' + '<br />'; }
    var e = document.getElementById(handleFrm);
    e.entryID.options.length = 0;
    for (var i in list) { e.entryID.options[e.entryID.options.length] = new Option(list[i],i); }
    }

function handleGeneric(url)
    {
    if (http && http.readyState < 4) { http.abort(); }
    http.open('GET',url,true);
    http.onreadystatechange = handleResponse;
    http.send(null);
    }








// google map functions

var map;
var mapRefreshData = '';
var mapRefreshCurr = '';
var mapRefreshLast = '';
var mapIcons = new Array();

function mapRefresh(force)
    {
    var b = map.getBounds(); var sw = b.getSouthWest(); var ne = b.getNorthEast();
    var d = new Array(mapRefreshData,map.getZoom(),mapRound(sw.lat()),mapRound(sw.lng()),mapRound(ne.lat()),mapRound(ne.lng()));
    mapRefreshCurr = d.join(';');
    if (force || (mapRefreshCurr != mapRefreshLast)) { mapRefreshLast = mapRefreshCurr; GDownloadUrl('/auto/map.xml?'+mapRefreshCurr,mapProcess); }
    }

function mapRound(x) { return Math.round(x*1000)/1000; }

function mapProcess(data,responseCode)
    {
    // mapBusy(true);
    map.clearOverlays();
    var xml = GXml.parse(data);
    var markers = xml.documentElement.getElementsByTagName('marker');
    for (var i=0; i<markers.length; i++)
        {
        var pos = markers[i].getAttribute('pos'); if (!pos) { pos = 'C'; }
        var tip = markers[i].getAttribute('tip'); if (!tip) { tip = ''; }
        mapMarker(markers[i].getAttribute('lat'),markers[i].getAttribute('lng'),markers[i].getAttribute('col'),tip,pos,GXml.value(markers[i]));
        }
    // mapBusy(false);
    }

function mapBusy(state) { document.getElementById('mapOverlay').style.display = state ? 'block' : 'none'; }

function mapMarker(lat,lng,col,tip,align,html)
    {
    lat = parseFloat(lat); lng = parseFloat(lng);
    var marker = new GMarker(new GLatLng(lat,lng),{icon:mapIcons[col],title:tip});
    if (html)
        {
        if (col == col.toUpperCase())
            {
            GEvent.addListener(marker,'click',function() { map.zoomIn(); map.setCenter(marker.getPoint()); } );
            GEvent.addListener(marker,'mouseover',function() { marker.openInfoWindowHtml('<div class="bubble'+align+'">'+html+'</div>'); } );
            GEvent.addListener(marker,'mouseout',function() { map.closeInfoWindow(); } );
            }
            else
            {
            if (isString(html))
                {
                GEvent.addListener(marker,'click',function() { marker.openInfoWindowHtml('<div class="bubble'+align+'">'+html+'</div>'); } );
                }
                else
                {
                var tabs = new Array();
                for (var i in data) tabs[tabs.length] = new GInfoWindowTab(i,'<div class="bubble'+align+'">'+html[i]+'</div>');
                GEvent.addListener(marker,'click',function() { marker.openInfoWindowTabsHtml(tabs); } );
                }
            }
        }
    map.addOverlay(marker);
    return marker;
    }

function mapInit(Full,Lat,Lng,Zoom,Pin,Data)
    {

    if (!GBrowserIsCompatible()) return false;
    map = new GMap2(document.getElementById('map'));
    mapRefreshData = Data;

    if (Full)
        {
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GScaleControl());
        map.setCenter(new GLatLng(Lat,Lng),Zoom);
        }
        else
        {
        map.addControl(new GSmallZoomControl());
        map.setCenter(new GLatLng(Lat,Lng),Zoom);
        }

    if (document.getElementById('mapOverlay'))
        {
        var pos = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT,new GSize(1,1));
        pos.apply(document.getElementById('mapOverlay'));
        map.getContainer().appendChild(document.getElementById('mapOverlay'));
        }

    if (document.getElementById('mapKey'))
        {
        var pos = new GControlPosition(G_ANCHOR_TOP_RIGHT,new GSize(7,33));
        pos.apply(document.getElementById('mapKey'));
        map.getContainer().appendChild(document.getElementById('mapKey'));
        document.getElementById('mapKey').style.display = 'block';
        }

    var colour = new Array('blue','green','purple','red','white','yellow','kblack','cyan','orange');
    for (i=0;i<colour.length;i++)
        {
        tmp = new GIcon();
        tmp.image = '/images/google/20_'+colour[i]+'.png';
        tmp.shadow = '/images/google/20_shadow.png';
        tmp.iconSize = new GSize(12,20);
        tmp.shadowSize = new GSize(22,20);
        tmp.iconAnchor = new GPoint(6,20);
        tmp.infoWindowAnchor = new GPoint(5,1);
        mapIcons[colour[i].charAt(0)] = tmp;
        }
    for (i=0;i<colour.length;i++)
        {
        tmp = new GIcon();
        tmp.image = '/images/google/50_'+colour[i]+'.png';
        tmp.shadow = '/images/google/50_shadow.png';
        tmp.iconSize = new GSize(20,34);
        tmp.shadowSize = new GSize(37,34);
        tmp.iconAnchor = new GPoint(9,34);
        tmp.infoWindowAnchor = new GPoint(9,2);
        // tmp.infoShadowAnchor = new GPoint(18,25);
        mapIcons[colour[i].charAt(0).toUpperCase()] = tmp;
        }

    if (Pin) mapMarker(Lat,Lng,Pin,'','','');

    GEvent.addListener(map,'moveend',function() { mapRefresh(false); } );
    GEvent.addListener(map,'zoomend',function() { mapRefresh(true); } );
    GEvent.addListener(map,'click',function(marker,point) { if (!marker) { map.closeInfoWindow(); } } );

    mapRefresh(true);
    }















function addPolyLine()
    {
    var argv = addPolyLine.arguments;
    if (isArray(argv[0]) && (argv.length == 1)) argv = argv[0];
    var points = new Array();
    for(var j = 0; j < argv.length; j+=2) points.push(new GPoint(argv[j+1],argv[j]));
    map.addOverlay(new GPolyline(points,'#000000',6,0.6));
// ff0000,8,0.35
// d02080,8,0.6
    }

function addPolyLines()
    {
    var argv = addPolyLines.arguments;
    if (isArray(argv[0]) && (argv.length == 1)) argv = argv[0];
    for(var i = 0; i < argv.length; i++) addPolyLine(argv[i]);
    }












var httpURL = null;

function httpObj()
    {
    var obj = null;
    try { obj = new ActiveXObject("Msxml2.XMLHTTP") }
    catch(e1)
        {
        try { obj = new ActiveXObject("Microsoft.XMLHTTP") }
        catch(e2) { obj = null }
        }
    if (!obj && typeof XMLHttpRequest != "undefined") { obj = new XMLHttpRequest() }
    return obj;
    }

function httpGet(url)
    {
    if (url.length <= 1) { return false; }
    if (url == httpURL) { return false; }
    http = httpObj();
    http.open("GET",url,true);
    http.onreadystatechange = function()
        {
        if (http.status == 200 && http.readyState == 4 && http.responseText && http.responseText.charAt(0)!='<')
            { eval(http.responseText); }
        }
    http.send(null);
    httpURL = url;
    }














function print_r(theObj,bReturnString,sIndent){
    if(undefined===bReturnString) bReturnString = true;
    if(undefined===sIndent) sIndent = '';
    if(undefined===theObj || null===theObj){// detect if it has no properties
        var s = theObj;
    } else {
        var s = '';
        if(theObj.constructor == Array ||
            theObj.constructor == Object){
            var sConstructor = Array === theObj.constructor
                ? 'Array'
                : 'Object'; // improve me
            s += sConstructor + "\n"+sIndent+"(";
            var sChildNameIndent = sIndent + str_repeat(' ',4);
            var sChildContentsIndent = sIndent + str_repeat(' ',8);
            for(var p in theObj){
                s += "\n"+ sChildNameIndent + "[" + p + "] => ";
                s += print_r(theObj[p],true,sChildContentsIndent);
            }
            s += "\n" + sIndent + ")";
        } else {
            s += theObj; // expect null, string, integer, boolean, etc.
        }
    }
    if(bReturnString) { return s; }
    else { document.write("<PRE>"+s+"</PRE>"); return null; }
}
function str_repeat(s,n)
{
    var sRet = '';
    while ( n-- > 0 ) sRet += s;
    return sRet;
}




function openTourDiv()
    {
    var myDiv = document.getElementById ? document.getElementById('tourD') : document.all['tourD'];
    var myLnk = document.getElementById ? document.getElementById('tourL') : document.all['tourL'];
    var x = 0;
    var y = myLnk.offsetHeight;
    while (myLnk.offsetParent && myLnk.tagName.toUpperCase() != 'BODY')
        {
        x += myLnk.offsetLeft;
        y += myLnk.offsetTop;
        myLnk = myLnk.offsetParent;
        }
    x += myLnk.offsetLeft;
    y += myLnk.offsetTop;
    x -= 120;
    y -= 16;
    myDiv.style.left = x + 'px';
    myDiv.style.top = y + 'px';
    myDiv.style.display = 'block';
    myDiv.onmouseout = function(e)
        {
        if (!e) var e = window.event;
        var tg = (window.event) ? e.srcElement : e.target;
        if (tg.id == 'tourD') tg.style.display = 'none';
        }
    }

function goTour()
    {
    var frm = document.getElementById ? document.getElementById('tourF') : document.all['tourF'];
    if ((frm.tourID && (frm.tourID.selectedIndex == 0)) && (frm.tourNew.value == ''))
        { alert('Please select an existing tour or enter a name for a new tour'); return false; }
    frm.submit();
    }


function orderTour(frm)
    {
    var myImageList = new Array();
    myImageList[myImageList.length] = -1;
    for (var i in myImageData) if (!isNull(myImageData[i])) myImageList[myImageList.length] = myImageData[i];
    frm.myImageSubmit.value = myImageList.join(',');
    return true;
    }

var viewport = {
  getWinWidth: function () {
    this.width = 0;
    if (window.innerWidth) this.width = window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth)
        this.width = document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth)
        this.width = document.body.clientWidth;
    return this.width;
  },

  getWinHeight: function () {
    this.height = 0;
    if (window.innerHeight) this.height = window.innerHeight - 18;
    else if (document.documentElement && document.documentElement.clientHeight)
        this.height = document.documentElement.clientHeight;
    else if (document.body && document.body.clientHeight)
        this.height = document.body.clientHeight;
    return this.height;
  },

  getScrollX: function () {
    this.scrollX = 0;
    if (typeof window.pageXOffset == "number") this.scrollX = window.pageXOffset;
    else if (document.documentElement && document.documentElement.scrollLeft)
        this.scrollX = document.documentElement.scrollLeft;
    else if (document.body && document.body.scrollLeft)
        this.scrollX = document.body.scrollLeft;
    else if (window.scrollX) this.scrollX = window.scrollX;
    return this.scrollX;
  },

  getScrollY: function () {
    this.scrollY = 0;
    if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
        this.scrollY = document.documentElement.scrollTop;
    else if (document.body && document.body.scrollTop)
        this.scrollY = document.body.scrollTop;
    else if (window.scrollY) this.scrollY = window.scrollY;
    return this.scrollY;
  },

  getAll: function () {
    this.getWinWidth(); this.getWinHeight();
    this.getScrollX();  this.getScrollY();
  }

}

var myImageData = ''; var myImageOffsetX = 185; var myImageOffsetY = 290; // rough positions

var Drag = {

    obj : null,

    init : function(id)
    {
        if (id==0)
            {
            var ele = document.getElementById('orderImages');
            var x = 0;
            var y = ele.offsetHeight;
            while (ele.offsetParent && ele.tagName.toUpperCase() != 'BODY')
                {
                x += ele.offsetLeft;
                y += ele.offsetTop;
                ele = ele.offsetParent;
                }
            x += ele.offsetLeft;
            y += ele.offsetTop;
            myImageOffsetX = x;
            myImageOffsetY = y;
            return;
            }

        var o = document.getElementById('drag'+id);
        o.id = parseInt(id);
        if (isNaN(o.id)) o.id = 0;
        o.onmousedown   = Drag.start;

        if (isNaN(parseInt(o.style.left  ))) o.style.left   = "0px";
        if (isNaN(parseInt(o.style.top   ))) o.style.top    = "0px";

        o.style.opacity = 0;
        o.style.MozOpacity = 0;
        o.style.KhtmlOpacity = 0;
        o.style.filter = "alpha(opacity=0)";

        o.onDragStart  = new Function();
        o.onDragEnd    = new Function();
        o.onDrag       = new Function();
    },

    start : function(e)
    {
        var o = Drag.obj = this;
        e = Drag.fixE(e);
        var y = parseInt(o.style.top);
        var x = parseInt(o.style.left);
        o.onDragStart(x, y);

        if (o.id > 0)
            {
            o.style.opacity = 0.75;
            o.style.MozOpacity = 0.75;
            o.style.KhtmlOpacity = 0.75;
            o.style.filter = "alpha(opacity=75)";
            }

        o.lastMouseX    = e.clientX;
        o.lastMouseY    = e.clientY;

        document.onmousemove    = Drag.drag;
        document.onmouseup      = Drag.end;

        return false;
    },

    drag : function(e)
    {
        e = Drag.fixE(e);
        var o = Drag.obj;

        var ey  = e.clientY;
        var ex  = e.clientX;
        var y = parseInt(o.style.top);
        var x = parseInt(o.style.left);
        var nx, ny;

        nx = x + (ex - o.lastMouseX);
        ny = y + (ey - o.lastMouseY);

        Drag.obj.style["left"] = nx + "px";
        Drag.obj.style["top"] = ny + "px";
        Drag.obj.lastMouseX = ex;
        Drag.obj.lastMouseY = ey;

        Drag.obj.onDrag(nx, ny);
        return false;
    },

    end : function()
    {
        document.onmousemove = null;
        document.onmouseup   = null;

        if (Drag.obj.id > 0)
            {
            Drag.obj.style.opacity = 0;
            Drag.obj.style.MozOpacity = 0;
            Drag.obj.style.KhtmlOpacity = 0;
            Drag.obj.style.filter = "alpha(opacity=0)";
            Drag.obj.style['left'] = '0px';
            Drag.obj.style['top'] = '0px';
            if (!is.safari)
                {
                viewport.getAll();
                Drag.obj.lastMouseX = Drag.obj.lastMouseX + viewport.scrollX;
                Drag.obj.lastMouseY = Drag.obj.lastMouseY + viewport.scrollY;
                }
            var xe = Drag.obj.lastMouseX - myImageOffsetX;
            var ye = Drag.obj.lastMouseY - myImageOffsetY;
            var cs = parseInt(Drag.obj.id);
            // alert(xe+' ('+myImageMaxX+') '+ye+' ('+myImageMaxY+')');
            if (cs <= myImageTop && xe > 0 && xe < myImageMaxX && ye > 0 && ye < myImageMaxY)
                {
                while (ye > 106) { ye = ye - 106; xe = xe + (106*6); }
                var ce = 1000 + Math.floor(xe/106);
                var isTrash = false;
                if (ce == myImageTrash) if (confirmLink('Are you sure you want to delete this picture from the tour ?')) { var isTrash = true; ce = myImageTop; }
                if ((isTrash || (Drag.obj.id != ce)) && (ce <= myImageTop))
                    {
                    if (cs > ce)
                        {
                        var csid = myImageData[cs];
                        for(var i=(cs-1);i>=ce;i--)
                            {
                            changeBgImage('hold'+(i+1),'/img/S/'+myImageData[i]+'.jpg');
                            changeImgSrc('dimg'+(i+1),'/img/S/'+myImageData[i]+'.jpg');
                            myImageData[i+1] = myImageData[i];
                            }
                        changeBgImage('hold'+ce,'/img/S/'+csid+'.jpg');
                        changeImgSrc('dimg'+ce,'/img/S/'+csid+'.jpg');
                        myImageData[ce] = csid;
                        }
                        else
                        {
                        var csid = myImageData[cs];
                        for(var i=(cs+1);i<=ce;i++)
                            {
                            changeBgImage('hold'+(i-1),'/img/S/'+myImageData[i]+'.jpg');
                            changeImgSrc('dimg'+(i-1),'/img/S/'+myImageData[i]+'.jpg');
                            myImageData[i-1] = myImageData[i];
                            }
                        if (isTrash)
                            {
                            myImageData[myImageTop] = null;
                            changeVisibility('drag'+myImageTop,false);
                            changeVisibility('hold'+myImageTop,false);
                            changeVisibility('dimg'+myImageTop,false);
                            changeBgImage('hold'+myImageTop,'/images/pad.gif');
                            changeImgSrc('dimg'+myImageTop,'/images/pad.gif');
                            myImageTop--;
                            }
                            else
                            {
                            changeBgImage('hold'+ce,'/img/S/'+csid+'.jpg');
                            changeImgSrc('dimg'+ce,'/img/S/'+csid+'.jpg');
                            myImageData[ce] = csid;
                            }
                        }
                    }
                }
            }

        Drag.obj.onDragEnd(    parseInt(Drag.obj.style["left"]),
                               parseInt(Drag.obj.style["top"]));
        Drag.obj = null;
    },

    fixE : function(e)
    {
        if (typeof e == 'undefined') e = window.event;
        if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
        if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
        return e;
    }
};

function changeImgSrc(id,v)     { var el = document.getElementById(id); if (el) el.setAttribute('src',v); }
function changeVisibility(id,v) { var el = document.getElementById(id); if (el) el.style.visibility = v ? 'visible' : 'hidden'; }
function changeBgImage(id,v)    { var el = document.getElementById(id); if (el) el.style.backgroundImage = 'url('+v+')'; }

function AC(frm){
    
    this.frm = document.getElementById(frm);
    this.elem = this.frm.entryString;
    this.id = this.frm.entryID;
    this.highlighted = -1;
    this.arrItems = new Array();
    this.div = document.getElementById(this.frm.name+'EntryDiv');
    var me = this;

    var TAB = 9;
    var ESC = 27;
    var KEYUP = 38;
    var KEYDN = 40;
    var ENTER = 13;
    
    this.div.style.width = this.elem.style.width;

    me.elem.setAttribute("autocomplete","off");
    
    this.ajaxReq = getXMLHTTP();

    me.elem.onkeydown = function(ev)
    {
        var key = me.getKeyCode(ev);
        switch(key)
        {
            case TAB:
            case ENTER:
                if (me.highlighted.id != undefined){
                    me.acChoose(me.highlighted.id);
                }
                me.hideDiv(); return false; break;
            case ESC: me.hideDiv(); return false; break;
            case KEYUP: me.changeHighlight('up'); return false; break;
            case KEYDN: me.changeHighlight('down'); return false; break;
        }
    };
    
    this.setElemValue = function()
    {
        var a = me.highlighted.firstChild;
        me.elem.value = a.innerTEXT;
    };
    
    this.highlightThis = function(obj,yn)
    {
        if (yn = 'y'){
            me.highlighted.className = '';
            me.highlighted = obj;
            me.highlighted.className = 'selected';
            me.setElemValue(obj);
        }else{
            obj.className = '';
            me.highlighted = '';
        }
    };
    
    this.changeHighlight = function(way){
        
        if (me.highlighted != '' && me.highlighted != null ){
            me.highlighted.className = '';
            switch(way){
                case 'up':
                    if(me.highlighted.parentNode.firstChild == me.highlighted){
                        me.highlighted = me.highlighted.parentNode.lastChild;
                    }else{
                        me.highlighted = me.highlighted.previousSibling;
                    }
                break;
                case 'down':
                    if(me.highlighted.parentNode.lastChild == me.highlighted){
                        me.highlighted = me.highlighted.parentNode.firstChild;
                    }else{
                        me.highlighted = me.highlighted.nextSibling;
                    }
                break;
            }
            if (me.highlighted.className == 'heading')
                {
            switch(way){
                case 'up':
                    if(me.highlighted.parentNode.firstChild == me.highlighted){
                        me.highlighted = me.highlighted.parentNode.lastChild;
                    }else{
                        me.highlighted = me.highlighted.previousSibling;
                    }
                break;
                case 'down':
                    if(me.highlighted.parentNode.lastChild == me.highlighted){
                        me.highlighted = me.highlighted.parentNode.firstChild;
                    }else{
                        me.highlighted = me.highlighted.nextSibling;
                    }
                break;
            }
                }
            me.highlighted.className = 'selected';
            me.setElemValue();
        }else{
            switch(way){
                case 'up':
                    me.highlighted = me.div.firstChild.lastChild;
                break;
                case 'down':
                    me.highlighted = me.div.firstChild.firstChild;
                break;
            }
            if (me.highlighted.className == 'heading')
                {
            switch(way){
                case 'up':
                    me.highlighted = me.highlighted.prevousSibling;
                break;
                case 'down':
                    me.highlighted = me.highlighted.nextSibling;
                break;
            }
                }
            me.highlighted.className = 'selected';
            me.setElemValue();
        }
        
    };
    
    me.elem.onkeyup = function(ev) 
    {
        var key = me.getKeyCode(ev);
        switch(key)
        {
        case TAB: case ESC: case KEYUP: case KEYDN: return;
        case ENTER: return false; break;
        default:
            if (me.elem.value.length < 2) { return; }
            if (me.ajaxReq.readyState > 0 && me.ajaxReq.readyState < 4)
                {
                me.ajaxReq.onreadystatechange = function () {}
                me.ajaxReq.abort();
                }
            me.ajaxReq = getXMLHTTP();
            if (me.ajaxReq != undefined)
                {
                me.ajaxReq.open('GET','/auto/ac/?'+me.frm.entryType.value+';'+me.elem.value,true);
                me.ajaxReq.onreadystatechange = me.acResult;
                me.ajaxReq.send(null);
                }
            me.highlighted = '';
        }
    };
    
    me.elem.onblur = function() { me.hideDiv(); }
    
    this.acResult = function(){

        if (me.ajaxReq.readyState != 4) { return false; }

        me.showDiv()
        var xmlRes = me.ajaxReq.responseXML;
        if (xmlRes == null) return false;
        if (xmlRes == undefined) return false;
        var items = xmlRes.getElementsByTagName('item');
        if (items.length == 0) { me.hideDiv(); return; }
        me.div.innerHTML = '';
        var ul = document.createElement('ul');
        me.div.appendChild(ul);
        for (i=0;i<items.length;i++){

            me.arrItems[items[i].getAttribute("id")] = new Array();
            me.arrItems[items[i].getAttribute("id")]['label'] = items[i].getAttribute("label");

            var li = document.createElement('li');
            li.id = items[i].getAttribute("id");

            if (li.id.length == 1) { li.className = 'heading'; } else
            {
            li.onmouseover = function(){ this.className = 'selected'; me.highlightThis(this,'y')}
            li.onmouseout  = function(){ this.className = '';  me.highlightThis(this,'n')}
            li.onmousedown = function() {
                me.acChoose(this.id);
                me.hideDiv();
                return false;
                }
            }

            var a = document.createElement('a');
            a.href = '#';
            a.onclick = function() { return false; }
            a.innerHTML = unescape(items[i].getAttribute("label"));
            a.innerTEXT = unescape(items[i].getAttribute("label"));
    
            li.appendChild(a);
            ul.appendChild(li);    
        }
    };
    
    this.acChoose = function (id)
    {
        me.id.value = id;
        me.hideDiv();
        me.elem.value = unescape(me.arrItems[id]['label']);    
    }

    this.positionDiv = function()
    {
        var el = this.elem;
        var x = 0;
        var y = el.offsetHeight;

        while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
        {
            x += el.offsetLeft;
            y += el.offsetTop;
            el = el.offsetParent;
        }

        x += el.offsetLeft;
        y += el.offsetTop;

        this.div.style.left = x + 'px';
        this.div.style.top = y + 'px';
    };

    this.hideDiv = function()
    {
        me.highlighted = '';
        me.div.style.display = 'none';
        me.handleSelects('');
    };

    this.showDiv = function()
    {
        me.highlighted = '';
        me.positionDiv();
        me.handleSelects('none');
        me.div.style.display = 'block';
    };
    
    this.handleSelects = function(state)
    {
        var selects    = document.getElementsByTagName('SELECT');
        for (var i = 0; i < selects.length; i++) { selects[i].style.display = state; }
    };
    
    this.getKeyCode = function(ev)
    {
        if(ev) { return ev.keyCode; } //Moz
        if(window.event) { return window.event.keyCode; } //IE
    };

    this.getEventSource = function(ev)
    {
        if(ev) { return ev.target; } //Moz
        if(window.event) { return window.event.srcElement; } //IE
    };

    this.cancelEvent = function(ev)
    {
        if(ev) { ev.preventDefault(); ev.stopPropagation(); } //Moz
        if(window.event) { window.event.returnValue = false; } //IE
    };
}

