//initialize any variables

LAST_RECORD = 0;
FIRST_RECORD = 0;
isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;

function rewrite (stringtmp) {
	//many organizations like the ampersand
	stringtmp = stringtmp.replace(/&/g,"q7x");
	//we used the semicolon an ampersand placeholder
	stringtmp = stringtmp.replace(/;/g,"&");
	return (stringtmp);
}
	
function div_switch (grow, max) {

	//set shrink
	if (getheight('dt1') > 1) {shrink = 'dt1';}
	if (getheight('dt2') > 1) {shrink = 'dt2';}
	if (getheight('dt3') > 1) {shrink = 'dt3';}
	if (getheight('dt4') > 1) {shrink = 'dt4';}
	
	if (shrink.charAt(2)*1 == grow.charAt(2)*1) {
		return false;
		
	} else {
		maxdivtext = max;
		finalbig=1;
		finalsmall= maxdivtext;
		growlayer = grow;
		shrinklayer = shrink;
		i=0;
		shift_layer();
	}
	
}


function shift_layer() {
	finalbig = finalbig+10;
	i++;
	if (finalbig > maxdivtext) {finalbig = maxdivtext;}
	finalsmall = finalsmall-10;
	if (finalsmall < 1) {finalsmall = 1;}
	finalbigpx = finalbig + 'px';
	finalsmallpx = finalsmall + 'px';
	toggleLayer(growlayer, finalbigpx);
	toggleLayer(shrinklayer, finalsmallpx);
	if (finalbig < maxdivtext) {
		//alert('here');
		//document.getElementById('DEBUG').innerHTML = i+' '+finalsmall+' ' + maxdivtext +  ' ' + finalbig;
		setTimeout("shift_layer()",1);
	} 
}

function grow_layer(thelayer1) {
	
	finalbig = getwidth(thelayer1);
	finalbig = finalbig+10;
	this_layer1 = thelayer1;
	finalbigpx = finalbig + 'px';
	toggleLayerW(thelayer1, finalbigpx);
	if (finalbig < 270) {
		//alert('here');
		//document.getElementById('DEBUG').innerHTML = i+' '+finalsmall+' ' + maxdivtext +  ' ' + finalbig;
		setTimeout("grow_layer(this_layer1)",1);
	} 
	if (finalbig > 270) {finalbig = 270;}
	
}

function shrink_layer(thelayer) {
	finalsmall = getwidth(thelayer);
	finalsmall = finalsmall-10;
	this_layer = thelayer;
	finalsmallpx = finalsmall + 'px';
	toggleLayerW(thelayer, finalsmallpx);
	if (finalsmall > 250) {
		//alert('here');
		//document.getElementById('DEBUG').innerHTML = i+' '+finalsmall+' ' + maxdivtext +  ' ' + finalbig;
		setTimeout("shrink_layer(this_layer)",1);
	} 
	if (finalsmall < 250) {finalsmall = 250;}
	
}

function toggleLayer(whichLayer, height)
{
	//document.getElementById('DEBUG').innerHTML = whichLayer;
if (document.getElementById)
{
// this is the way the standards work
document.getElementById(whichLayer).style.height = height;
}
else if (document.all)
{
// this is the way old msie versions work
document.all[whichLayer].style.height = height;
}
else if (document.layers)
{
// this is the way nn4 works
document.layers[whichLayer].style.height = height;
}
}

function toggleLayerW(whichLayer, width)
{
	//document.getElementById('DEBUG').innerHTML = whichLayer;
if (document.getElementById)
{
// this is the way the standards work
document.getElementById(whichLayer).style.width = width;
}
else if (document.all)
{
// this is the way old msie versions work
document.all[whichLayer].style.width = width;
}
else if (document.layers)
{
// this is the way nn4 works
document.layers[whichLayer].style.width = width;
}
}

function getheight(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
height = document.getElementById(whichLayer).style.height;
}
else if (document.all)
{
// this is the way old msie versions work
height = document.all[whichLayer].style.height;
}
else if (document.layers)
{
// this is the way nn4 works
height = document.layers[whichLayer].style.height;
}
//alert(height);
height = height.substring(0,height.length-2);
return height;
}

function getwidth(whichLayer)
{
if (document.getElementById)
{
// this is the way the standards work
width = document.getElementById(whichLayer).style.width;
}
else if (document.all)
{
// this is the way old msie versions work
width = document.all[whichLayer].style.width;
}
else if (document.layers)
{
// this is the way nn4 works
width = document.layers[whichLayer].style.width;
}
//alert(width);
width = width.substring(0,width.length-2);
return width;
}


//alert(Size_Array[0]+" "+Size_Array[1]);
function findSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  
  Size_Array = new Array(2);
  Size_Array[0] = myWidth;
  Size_Array[1] = myHeight;
  return Size_Array;
}

//alertSize();
function createRequestObject() {
    var ro;
    var browser = navigator.appName;
	if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
 
}


var http = createRequestObject();

function sndReq(target, action) {
	http.open('get', action);
    http.onreadystatechange = function() {
    	if(http.readyState == 4){
        	var response = http.responseText;
//        	showMe(target);
        	document.getElementById(target).innerHTML = response;
        
    	}
    }
    
    http.send(null);
}

var http1 = createRequestObject();

function sndReq1(target1, action1) {
	setTimeout("sndReq2('"+target1+"', '"+action1+"')",500)
}
function sndReq2(target2, action2) {
	http1.open('get', action2);
    http1.onreadystatechange = function() {
    	if(http1.readyState == 4){
        	var response = http1.responseText;
        	//showMe(target2);
        	document.getElementById(target2).innerHTML = response;
        
    	}
    }
    
    http1.send(null);
}

var xmlReq = createRequestObject();


function postReq(target, action, string) {

//we need to do some encoding of special characters to get them to the database
	string = rewrite(string);
	
	//alert (string3);
	//return;
	xmlReq.open("POST",action,true);
	xmlReq.setRequestHeader("Content-Type",
	"application/x-www-form-urlencoded; charset=UTF-8");
	xmlReq.send(string);
	
	xmlReq.onreadystatechange = function() {
    	if(xmlReq.readyState == 4){
        	var response = xmlReq.responseText;
        	//showMe(target2);
        	document.getElementById(target).innerHTML = response;
        }
   }
    //xmlReq.send(null);

}

var xmlReq1 = createRequestObject();

function postReq1(target1, action1, string1) {
	setTimeout("postReq2('"+target1+"', '"+action1+"', '"+string1+"')",100)
}

function postReq2(target2, action2, string2) {

//we need to do some encoding of special characters to get them to the database
	string2 = rewrite(string2);
	
	//alert (string3);
	//return;
	xmlReq.open("POST",action2,true);
	xmlReq.setRequestHeader("Content-Type",
	"application/x-www-form-urlencoded; charset=UTF-8");
	xmlReq.send(string2);
	
	xmlReq.onreadystatechange = function() {
    	if(xmlReq.readyState == 4){
        	var response = xmlReq.responseText;
        	//showMe(target2);
        	document.getElementById(target2).innerHTML = response;
        }
   }
    //xmlReq.send(null);

}


function hideMe(element){
	isIE=document.all;
	isNN=!document.all&&document.getElementById;
	isN4=document.layers;
	whichDog=isIE ? eval("document.all."+element) : document.getElementById(element); 


  if (isIE||isNN) whichDog.style.visibility="hidden";
  else if (isN4) eval("document."+element+".visibility")="hide";
}

function showMe(element){
	isIE=document.all;
	isNN=!document.all&&document.getElementById;
	isN4=document.layers;
	whichDog=isIE ? eval("document.all."+element) : document.getElementById(element); 


  if (isIE||isNN) whichDog.style.visibility="visible";
  else if (isN4) eval("document."+element+".visibility")="show";
}


function reset_S2(){
	document.forms[1].SEARCH2.selectedIndex = 0;
}

//here is the function that gets the navigation data from the php include and acts on them appropriately
//see the array definitions

//get the data from the XML sheet
function varReq(action) {

if (window.ActiveXObject){
	// ...otherwise, use the ActiveX control for IE5.x and IE6
    var req = new ActiveXObject("Microsoft.XMLHTTP"); 
} else {
	var req = new XMLHttpRequest();
}

req.open('GET', action, false);
//req.overrideMimeType('text/xml');
req.send(null);
//alert(req.responseXML);
returnArray = new Array(9);
//var headRef = document.getElementsByTagName('head')[0];

returnArray[0] = req.responseXML.getElementsByTagName('left_start')[0].firstChild.data; //number of last record returned on the left page

returnArray[1] = req.responseXML.getElementsByTagName('left_end')[0].firstChild.data; //number of last record returned on the right page
returnArray[2] = req.responseXML.getElementsByTagName('right_start')[0].firstChild.data; //number of last record returned on the left page
returnArray[3] = req.responseXML.getElementsByTagName('right_end')[0].firstChild.data; //number of last record returned on the right page
returnArray[4] = req.responseXML.getElementsByTagName('total_records')[0].firstChild.data; //total records
returnArray[5] = req.responseXML.getElementsByTagName('criteria_1')[0].firstChild.data; //search1
returnArray[6] = req.responseXML.getElementsByTagName('criteria_2')[0].firstChild.data; //search2
returnArray[7] = req.responseXML.getElementsByTagName('image_width')[0].firstChild.data; //search2
returnArray[8] = req.responseXML.getElementsByTagName('image_height')[0].firstChild.data; //search2


document.getElementById('CONTENT').innerHTML = '<img src="images/empty.gif" border=0 style="height:'+(returnArray[8]-150)+'px;" width="1"><img id="LEFT_PAGE" name="LEFT_PAGE" src="images/empty.gif" border=0><img id="RIGHT_PAGE" name="RIGHT_PAGE" src="images/empty.gif" border=0>';
//alert(returnArray[8]);
// using the XML data, change the images, the Navigation data, and navigation display appropriately

//change images
document['LEFT_PAGE'].src='http://wilkesbarre.org/ed/diversity/image.php?W='+returnArray[7]+'&H='+returnArray[8]+'&ID='+returnArray[0]+'&S1='+returnArray[5]+'&S2='+returnArray[6];
document['RIGHT_PAGE'].src='http://wilkesbarre.org/ed/diversity/image.php?W='+returnArray[7]+'&H='+returnArray[8]+'&ID='+returnArray[1]+'&S1='+returnArray[5];

//update current record
FIRST_RECORD = returnArray[0];
LAST_RECORD = returnArray[3];

//change navigation display elements


// show current records
//document.getElementById('RECORDS').innerHTML = "LEFT PAGE RECORDS: "+returnArray[0]+" TO "+returnArray[1]+"<br>RIGHT PAGE RECORDS: "+returnArray[2]+" TO "+returnArray[3];
if (FIRST_RECORD == 0) {
	if (isIE||isNN) document.getElementById('PREV').style.visibility = "hidden";
 	else if (isN4) eval("document.PREV.visibility")="hide";
} else {
	if (isIE||isNN) document.getElementById('PREV').style.visibility = "visible";
 	else if (isN4) eval("document.PREV.visibility")="show";
}

// show/hide the 'NEXT' link
if (LAST_RECORD*1 >= returnArray[4]*1) {
	if (isIE||isNN) document.getElementById('NEXT').style.visibility = "hidden";
 	else if (isN4) eval("document.NEXT.visibility")="hide";
} else {
	if (isIE||isNN) document.getElementById('NEXT').style.visibility = "visible";
 	else if (isN4) eval("document.NEXT.visibility")="show";
}

// reset the S2 search block to match the S1 choice



//alert (returnArray[5]);
return (returnArray);
}

