function xmlhttpPost(divName,file) {
    var xmlHttpReq = false;
    //var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', divName + ".php", true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
            updatepage(divName, xmlHttpReq.responseText);
        }
    }
    xmlHttpReq.send(getquerystring(file));
}


var red_link="";

var file="";

var page=1;
var listing_id = '';

function refresh(linkName, afile, lid) {    
    file=afile;



    obj=document.getElementById(linkName);
    if ( obj.className == "redcellborder" )
        {  return; }
    obj.className='redcellborder';
    obj.onmouseover="null" ;
    obj.onmouseout="null" ;

    xmlhttpPost("photo",file);

    if ( red_link != "" ) {
        obj=document.getElementById(red_link);
        obj.className="cellborder";
        obj.onmouseover=function() { this.className="cellover"; } ;
        obj.onmouseout=function() { this.className="cellborder"; } ;
    }

    red_link=linkName;

    xmlhttpPost("exif",file);

    }

function getquerystring(file) {
    qstr = 'listing_id=' + listing_id + '&ifile=' + escape(file) + '&page=' + page;  // NOTE: no '?' before querystring
    return qstr;
}


function nextpage() {
    page++;
    firstLoad();
}

function prevpage() {
    page--;
    firstLoad();
}


function firstpage() {
    page=1;
    firstLoad();
}

function lastpage() {
    page=maxpage;
    firstLoad();
}

function firstLoad(lid)  {
	 listing_id = lid
   xmlhttpPost("thumb",file);
}


function updatepage(divName, str){
    if ( divName == "thumb" ) {
        var data=str.split("|");
        file = data[0]; maxpage=data[1];
        document.getElementById(divName).innerHTML = data[2];
        refresh("a_0",file);
    }
    else {
    	if (document.getElementById(divName)) {
        document.getElementById(divName).innerHTML = str;
      }
    }
}

