//ajax function
//basic ajax get content function that allows changing a layer content
//call this function to update content in one layer within a page
//call it number of times in another function to update
//more than one layer in a page

function ajaxFunction(url,lyrUI){

	//setting up XmlHttp object
	var xmlHttp;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
                        
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}        

	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==0){
			document.getElementById(lyrUI).innerHTML = "&nbsp;Loading ...&nbsp;";
		}

		if(xmlHttp.readyState==1){
			document.getElementById(lyrUI).innerHTML = "&nbsp;Loading ...&nbsp;";
		}

		if(xmlHttp.readyState==2){
			document.getElementById(lyrUI).innerHTML = "&nbsp;Loading ...&nbsp;";
		}

		if(xmlHttp.readyState==3){
			document.getElementById(lyrUI).innerHTML = "&nbsp;Loading ...&nbsp;";
		}

		if(xmlHttp.readyState==4){
			//alert(xmlHttp.responseText);
			document.getElementById(lyrUI).innerHTML = xmlHttp.responseText;
		}
	}

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

}

function requestHandler(req_str) {
        
        i = req_str.lastIndexOf('=');
        link_url = req_str.substring(i+1);
        n = link_url.lastIndexOf('.');
        doc_name = link_url.substring(0, n);
	
        if(doc_name !== "sitemap") {
                document.getElementById('headername').innerHTML = "<img src=\"images/hdr_" + doc_name + ".gif\" />";
        }
        else {
                //alert("This must be the sitemap section of the site.");
                document.getElementById('headername').innerHTML = "<img src=\"images/hdr_sitemap.gif\" />";
        }

        menu_url = "menu.php" + req_str;
        ajaxFunction(menu_url, 'ajax_menu');
        
        content_url = "content.php" + req_str;
        ajaxFunction(content_url, 'ajax_content');
}

function swapImage(img_name, sexn_id) {
        
        //sexn_id = parseInt(img_name.substr(-1, 1));
        //alert(sexn_id);
        sexn_div_name = "sexn_" + sexn_id;
        //alert(document.getElementById(sexn_div_name).style.color);
        
        img_path = document.images[img_name].src;
        
        i = img_path.lastIndexOf('/');
        img_path_part = img_path.substr(0, i+1);
        img_src = img_path.substr(i+1);
        //alert("Source Before Swap: " + img_src);
        
        img_filename = (img_src == "blank.gif")?"bull.gif":"blank.gif";
        document.images[img_name].src = img_path_part + img_filename;
        //alert("Source After Swap: " + img_filename);
        
        type_color = document.getElementById(sexn_div_name).style.color;        
        document.getElementById(sexn_div_name).style.color = (type_color == "rgb(205, 25, 25)") ? "rgb(105, 105, 105)":"rgb(205, 25, 25)";
        
}

function displayImage(imgfilepath) {
	externalwindow = window.open(imgfilepath,'ewindow','target=_blank,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,copyhistory=no,resizable=yes')
}

function toggleVisibility(div_id) {
        
        styleVal = document.getElementById(div_id).style.visibility;
        document.getElementById(div_id).style.visibility = (styleVal=="visible") ? "hidden" : "visible";
}

function displayThumbnails(pgnum, folder) {
        
        thumbnails = "<table width=\"100%\" cellpadding=\"4\" cellspacing=\"0\" border=\"0\">";
        counter = (pgnum<2) ? 0 : 9;
        for(i=1; i<=3; i++) {
                thumbnails += "\n<tr>";
            
                for(n=1; n<=3; n++) {
                        counter++;
                        image_src = "images/" + folder + "/" + counter + "_thumb.jpg";
                        image_filepath = "images/" + folder + "/" + counter + ".jpg";
                        thumbnails += "\n\t<td align=\"center\" valign=\"middle\">";
                        thumbnails += "<a href=\"#\" onClick=\"displayImage('" + image_filepath + "')\"><img src=\"" + image_src + "\" width=\"186\" height=\"130\" border=\"0\"></a>";
                        thumbnails += "</td>";
                }
                thumbnails += "\n</tr>";
        }
        thumbnails += "</table>";
        
        links = "";
        if(pgnum<2) {
                links = "<span class=\"gallery_links_disabled\">Previous Set</span> | <a href=\"#\" class=\"gallery_links\" onClick=\"displayThumbnails(2,'" + folder + "'); return false;\">Next Set</a>";
        }
        else {
                links = "<a href=\"#\" class=\"gallery_links\" onClick=\"displayThumbnails(1,'" + folder + "'); return false;\">Previous Set</a> | <span class=\"gallery_links_disabled\">Next Set</span>";
        }
        
        document.getElementById('thumbnails').innerHTML = thumbnails;
        document.getElementById('links').innerHTML = links;        
}
