function showurl(url, divname)
{
	//alert(divname)
	//alert("geturl("+url+")-divname:"+divname)
	txt = geturl(url)  
	if(txt.indexOf("Parse error")>-1)	
		alert(txt);
	else if(txt.indexOf("Fatal error")>-1)	
		alert(txt);
	else if(txt.indexOf("(): Query failed")>-1)	
		alert(txt);
	else if(false)
		alert(txt)  
	run_txt(txt,divname)
} 
function run_txt(txt, divname)
{ 
	if( typeof(framehistory)=="object")
	{  
		var sessionFrame = document.getElementById("framehistory");
		var doc = sessionFrame.contentDocument;
		if (doc == undefined) 
		{ // Internet Explorer
			doc = sessionFrame.contentWindow.document;
		}
		doc.open();
		//create a script that will run after the page loads.  This should "run the text" which will basically put the text where it needs to go
		//if this ran in page then the document open and write will run recursively without ever closing the document.
		//so we use a timeout of 10 ms.
		stxt =  "<script>setTimeout(\"if(document!=top.document)top.run_txt2(top.document.getElementById('framehistory').contentWindow.document.body.innerText,'"+divname+"')\",100)</script>"
		otxt = txt 
		doc.write("EXCAPE:"+ escape(otxt)+stxt)
		doc.close();  
		//write the txt to the framehistory
	}
	else 
		run_txt2(txt,divname)
}
function framehistory_back()
{
	if(typeof(search_posted)!="undefined" && search_posted =="1")  //for advanced search to return to results
		showurl('list.php?use_search_array=1','inbox_content');
	else
		frames['framehistory'].history.back();
}
function debug(s)
{ 
	if(typeof(debugdiv) == "object")
	{
		debugdiv.style.display="";
		debugdiv.innerText =debugdiv.innerText + s + "\n----\n"
	}
}
function run_txt2(txt, divname)
{  
	if(txt.substring(0,7)=="EXCAPE:")
	{
		txt = txt.substring(7)
		txt = unescape(txt) 
	}
	//replace any text that we may have mess with
	while(txt.toLowerCase().indexOf("<sscript")>0)
		txt = txt.replace(/<sscript/i,"<script") 
		
	while(txt.toLowerCase().indexOf("</sscript")>0)
		txt = txt.replace(/<\/sscript/i,"</script") 
	//alert(txt.substring(8000,12000))
	if(txt.indexOf("Parse error")>0)
		alert("PRS:"+txt ) 
	try
	{
		div = document.getElementById(divname);
		if(div==null || !div)
			div = top.document.getElementById(divname);
		div.innerHTML = txt  
	}catch(e)
	{
		alert("invalid div:"+divname)
	}
	replace_divs();
    
	parse_js(txt);
}

//use this submit on any button,
//the function figures out the form and action from the button
//this currenlty only works in the inbox_content
function submit_debug(vl)
{ 
	if (typeof(submit_form)=="object")
	{
		submit_form.submit_value.value=vl 
		submit_form.action="/save_debug.php";
		submit_form.submit();
	}
}


function xmlsubmit(button, destinationDiv)
{
	if(destinationDiv=='' || typeof destinationDiv=='undefined')
		destinationDiv = 'inbox_content';
	
//  	for (var i in button) alert(i+': '+button[i])
	form = button.form
	var sBody =getRequestBody(form,button)  
    
    var oXmlHttp = XMLRequest();
   	if(button)
   		clicked = '&btnClicked='+button.value;
    oXmlHttp.open("POST", form.action+clicked, true);
    
    oXmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
   
   	
    oXmlHttp.onreadystatechange = function ()
	{
        if (oXmlHttp.readyState == 4) 
		{
            if (oXmlHttp.status == 200) 
			{
				//alert(form.onsubmit);
				if(typeof form.onsubmit=='function')
					form.onsubmit(button);
				txt = oXmlHttp.responseText;
				
				if(document.getElementById(destinationDiv))
					document.getElementById(destinationDiv).innerHTML = txt;
				else if(top.document.getElementById(destinationDiv))
					top.document.getElementById(destinationDiv).innerHTML = txt;
				
				replace_divs();  
				parse_js(txt);  
            } 
			else 
			{
                alert("An error occurred: " + oXmlHttp.statusText+' ('+form.action+')');
            }
        }            
    };
    oXmlHttp.send(sBody);        
	return false;
	
}


function getRequestBody(oForm,button) 
{
	var aParams = new Array();
	if(button)
	{
    	var sParam = encodeURIComponent(button.name);
    	sParam += "=";
        sParam += encodeURIComponent(button.value);
	    aParams.push(sParam);
	}
 	for (var i=0 ; i < oForm.elements.length; i++) 
	{
		e=oForm.elements[i]
		if(e.type=="submit")
			continue;
		if(e.type=="button")
			continue;
		if(e.type=="checkbox")
			if(!e.checked)
				continue;
    	var sParam = encodeURIComponent(e.name);
    	sParam += "=";
        sParam += encodeURIComponent(e.value);
	    aParams.push(sParam);
    }  
	return aParams.join("&");
}



function runurl(url)
{ 
	showurl(url,"hiddenreplacediv")  
	document.all.hiddenreplacediv.innerHTML = ''; 
}

function showurl_async(url, ele) {
	/* asynchronous AJAX request... this function should be used instead of
	 * showurl because showurl runs synchronously and freezes the browser
	 * until the response is received from the server.
	 */
	var xmlhttp = new XMLRequest();
	var dt = new Date();
	
	if(url.indexOf("?")>0) //only put a ? on the url if there is not already one
		url += "&ts="+dt.getTime();
	else
		url += "?ts="+dt.getTime(); 
	
	if (xmlhttp) {	
		if(ele=='' || !ele) return false;
		node = (typeof ele=='string')?top.document.getElementById(ele):ele;
			
		if(!node) 
			return false;
		
		// show a nifty loading image while waiting for server response.
		// this will get overwritten
		node.innerHTML = "<table style='width: 100%; height: 100%'><tr><td style=\"font-size: 10.5px; text-align: center; vertical-align: middle; font-family: 'Trebuchet MS'\"><img src='/images/ajax-loader.gif' /><br />Loading</td></tr></table>";
		xmlhttp.onreadystatechange = function() {
			if(xmlhttp.readyState==4) {
				if(typeof ele == 'string')
					node = top.document.getElementById(ele)
				else
					node = ele;
				if(!node) 
					return false;
				node.innerHTML = xmlhttp.responseText;
				// execute any scripts in the response
				run_txt(xmlhttp.responseText,node.id)
			}
		}
        xmlhttp.open("GET",url,true);  
        xmlhttp.send(null);         
    }
}

function geturl(url)
{  
	var xmlhttp =new XMLRequest();
    var v1;
	var dt = new Date();
	v1 = url;
	if(url.indexOf("?")>0) //only put a ? on the url if there is not already one
		url += "&ts="+dt.getTime();
	else
		url += "?ts="+dt.getTime();
	if (xmlhttp)
    {	
		xmlhttp.open("GET",url,false); 
		xmlhttp.send(null);
		
        return xmlhttp.responseText; 
    }         
}
function XMLRequest() 
{
	req = false;
    if(window.XMLHttpRequest) {
    	try {
			req = new XMLHttpRequest();
        } catch(e) {}
    } 
	else if(window.ActiveXObject) {
       	try {
        	req = new ActiveXObject("Msxml2.XMLHTTP");
      	} catch(e) {
        	try {
          		req = new ActiveXObject("Microsoft.XMLHTTP");
        	} catch(e) {}
		}
    }
	return req;
}

function parse_js(rtext)
{  
	scriptStart=rtext.toLowerCase().indexOf("<script");
	var scripts = new Array();
	while (scriptStart>=0)
	{
		scriptStart=rtext.toLowerCase().indexOf("\>",scriptStart);
		scriptEnd=rtext.toLowerCase().indexOf("</script",scriptStart);
		scriptText=rtext.substring(scriptStart+1,scriptEnd);
		scripts.push(scriptText);
		try
		{
			eval(scriptText);
		
		} catch(e)
		{
			alert("ERR:"+e.message+"\n"+scriptText)
			alert(rtext);
		}
		
			scriptStart=rtext.toLowerCase().indexOf("<script",scriptEnd);		
	}
	return scripts;

}
function replace_divs(doc)
{ 
	if(!doc)
	{
		doc=document; 
	}
	divs = document.getElementById('replace_div'); 
	if(typeof(divs)!="undefined") 
	{
		//alert(typeof(divs)) 
		if(typeof(divs.length)!="undefined")
		{ 
			for(i=0;i<divs.length;i++)
				transfer_div(divs[i],doc);
		}else
			transfer_div(divs,doc);
	}
	
	

}
function copy_div(div, doc)
{ 
	if(!doc)
	{
		doc=document; 
	}
	if(div.processed)  	
		return //already did this instruction 
	from = eval("doc.all."+div.from)  
	
	//to = document.all(div.to)  
	to = doc.all(div.to);
	if(typeof(div.finaljs)=="undefined")
		finaljs=false;
	else
		finaljs=div.finaljs
	
	//alert(div.id)	
//alert('from = ' + from.id)
	if(typeof(from)=="undefined") {
		
		return
	}
//alert('to = ' + to.id)
	if(typeof(to)=="undefined") {
	
		return    
	}
	if(to==null)
	{
		to = top.document.getElementById(div.to);
		if(!to) {
			alert("'to' "+div.to+" is null");
			return;
		}
	}
	if(typeof(to.length)!="undefined") 
	{
		for(j=0;j<to.length;j++) 
		{
		    
			value=from.innerHTML
			if(typeof(div.replaceval)!="undefined")
			{
				
				replaceval=eval("/"+div.replaceval+"/") 
				while (value.indexOf(div.replaceval) >=0 )
					value = value.replace(replaceval,div.replacewith); 
			}
			to[j].innerHTML=value;
			
			parse_js(value);
			
		} 
	}
	else 
	{  
		value=from.innerHTML
		
		if(typeof(div.replaceval)!="undefined")
		{
			
			replaceval=eval("/"+div.replaceval+"/")
			
			while (value.indexOf(div.replaceval) >=0 )
				value = value.replace(replaceval,div.replacewith); 
		} 
		if(typeof(div.replaceval)!="undefined")
		{
			
			replaceval=eval("/"+div.replaceval+"/")
			
			while (value.indexOf(div.replaceval) >=0 )
				value = value.replace(replaceval,div.replacewith); 
		}  
		to.innerHTML=value;

        parse_js(value); 
	}
	if(finaljs)
	//	eval(finaljs);
	//if(typeof(div.finaljs)!="undefined")  //if it exists run some final js
	{ 
		eval(div.finaljs)
    }
	div.processed=true;

	var ts2 = (new Date()).getTime();

	
}
function transfer_div(div,doc) //the document we pass in is the "from" document //always transfer to the top document
{
 	if(!doc)
		doc=document;  
	divname=div.transferto;

	if(div.processed)
	{
		return
	}
	if(!divname)
	{
		return;
	}
	//alert("replacing doc.all."+divname);
	//to = document.all(divname);
	to = doc.all(divname);
	if(to==null)
		to=document.all(divname);
		
	if(typeof(to)=="undefined" || to==null)
	{
		alert(divname+' not found');
		return; 
	} 
	if(typeof(to.length)!="undefined") 
	{

		for(j=0;j<to.length;j++) 
			to[j].innerHTML=div.innerHTML   
	}
	else
	{ 

		to.innerHTML=div.innerHTML;
	}
	div.processed=true;
	div.innerHTML=""; 
	
	//parse any js that might have been imported. 
	
}




