var xmlHttp=new Array(20);
var isIE;
var callingObject=new Array(20);

var totLDNums=0;
var pagesLoading=0;

var pageloaded=false;

function AjaxRequest(link, obj, LDNum){
	showLoading();
	//callingObject=obj
	re=/\?/
	if(re.test(link)){
		link=link+"&sid="+Math.random()
	}else{
		link=link+"?sid="+Math.random()
	}
	/*if (xmlHttp[LDNum].status != 200)  {
		xmlHttp[LDNum].abort()
	}*/
	xmlHttp[LDNum].open("GET",link,true)
	xmlHttp[LDNum].onreadystatechange=function(){stateChanged(obj.LDNum)}
	xmlHttp[LDNum].send(null)
}

function stateChanged(LDNum){
	var head = document.getElementsByTagName('head').item(0);
	if (xmlHttp[LDNum].readyState==4 || xmlHttp[LDNum].readyState=="complete"){
		re=/<title>Error Occurred While Processing Request/ig;
		if(re.test(xmlHttp[LDNum].responseText)){
			document.write(xmlHttp[LDNum].responseText);
		}
		
		
			elem = document.getElementById('code')
			rand=Math.floor(Math.random()*1000)
			elem = document.getElementById('code')
			elem.innerHTML = xmlHttp[LDNum].responseText
			
			var divs = elem.childNodes
			for(var i=0; i < divs.length;i++){
				if(divs[i].nodeName=='DIV'){
					//alert(divs[i].id)
					if(divs[i].id == "message"){
						displayMessage(divs[i].innerHTML);
					}else if(divs[i].id == "location"){
						window.location=divs[i].innerHTML;
					}else if(divs[i].id == "error"){
						alert(divs[i].innerHTML);
					}else{
						try{
							document.getElementById(divs[i].id).innerHTML=divs[i].innerHTML
						}catch(err){
							alert("missing div id:"+divs[i].id)
						}
					}
				}
			}
			
			re=/<script[.\n]*?script>/ig
			re=/<script[\s\S]*?script>/ig
			str = xmlHttp[LDNum].responseText.match(re);
			//if(re.test(xmlHttp[LDNum].responseText)){
			if(str){
				//alert("hi")
				//if(str){alert("yes!")}else{alert("no")}
				for(x=0;x<str.length;x++){
					//re2=/^[^]*src=\"(.*)\"[^]*$/ig
					//strs2 = str[x].match(re2);
					//alert(strs2[0])
					//re2.exec(strs)
					//alert(re2.$1);
					//re=/src=/ig;
					
					
					//alert(str[x])
					re=/^[\s\S]*src *?=\"/ig
					if(re.test(str[x])){
						scr=str[x].replace(re,"")
						re=/\"[\s\S]*$/ig
						scr=scr.replace(re,"")
						//alert(scr)
						script = document.createElement('script');
						script.src = scr
						script.type = 'text/javascript';
						script.defer = true;
						head.appendChild(script);
					}
					re=/<script[\s\S]*?>/ig
					scr=str[x].replace(re,"")
					re=/<\/script>/ig
					scr=scr.replace(re,"")
					//alert(scr)
					script = document.createElement('script');
					script.text = scr
					script.type = 'text/javascript';
					//script.defer = true;
					head.appendChild(script);
				}
			}
			//setTimeout("loadimgs()",30000)
			
			//document.getElementsByTagName("head")[0]._CF_checkCFForm_1=_CF_checkCFForm_1
		
		
		if(callingObject[LDNum] != null){
			callingObject[LDNum].AjaxUpdate()
			callingObject[LDNum]=null;
		}
		hideLoading();
	}
} 

function dhtmlLoadScript(url){
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   e.charset="utf-8";
   document.getElementsByTagName("head")[0].appendChild(e);
}

function GetXmlHttpObject(){ 
	var objXMLHttp=null
	if (window.ActiveXObject){
		isIE=true;
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	else if (window.XMLHttpRequest){
		isIE=false;
		objXMLHttp=new XMLHttpRequest()
	}
	if (objXMLHttp==null){
		alert ("Browser does not support HTTP Request, some features will not work.")
	}
	return objXMLHttp
}

function showdiv(divid){
	document.getElementById(divid).style.display='block'
}

function hidediv(divid){
	document.getElementById(divid).style.display='none'
}

function LoadableDiv(editabledivid){
	var divid=editabledivid
	var LDNum=totLDNums++;
	this.LDNum=LDNum;
	xmlHttp[LDNum]=GetXmlHttpObject();
	callingObject[LDNum]=this;

	this.loaddivtoggle=function(link){//formerly showdivtoggle
		if (document.getElementById(divid).innerHTML != ""){
			document.getElementById(divid).innerHTML=""
			return
		}
		loaddiv(link)
	}
	
	this.loaddiv=function(link){//formerly showdiv
		if (link.length==0){ 
			document.getElementById(divid).innerHTML=""
			return
		}
		AjaxRequest(link, this, LDNum)
	}
	
	this.quickloaddiv=function(hiddendivid){//formerly showdivlocal
		document.getElementById(divid).innerHTML=document.getElementById(hiddendivid).innerHTML
	}
	
	this.AjaxUpdate=function(){}
}

function ClickableTabbar(){
	var tabnames = new Array(50);
	var divids = new Array(50);
	var listids = new Array(50);
	var totaltabs = 0;
	var clicked=-1;
	
	this.addTab=function(tabname,divid,listid){
		tabnames[totaltabs]=tabname;
		divids[totaltabs]=divid;
		listids[totaltabs]=listid;
		totaltabs++;
	}
	
	this.loadtab=function(mytabname){
		if(clicked != -1)document.getElementById(listids[clicked]).className="";
		for(var i=0; i<totaltabs; i++){
			if (mytabname==tabnames[i]){
				clicked=i;
				document.getElementById(listids[i]).className="tabbar_active";
				showdiv(divids[i]);
			}else{
				hidediv(divids[i]);
			}
		}
	}
}

function DynamicList(editabledivid, listid){
	var div=new LoadableDiv(editabledivid)
	var list=new LoadableDiv(listid)
	var clicked = false
	var divLoadOnRec = ""
	
	var LDNum=totLDNums++;
	this.LDNum=LDNum;
	xmlHttp[LDNum]=GetXmlHttpObject();
	callingObject[LDNum]=this;
	
	this.loaddiv=function(link){//formerly showdiv
		AjaxRequest(link, this, LDNum)
	}
	
	this.cleardiv=function(){
		div.loaddiv("")
	}
	
	this.highlight=function(hiddendivid){//showdivhightlight
		if(!clicked){
			div.quickloaddiv(hiddendivid)
		}
	}

	this.loaddivclick=function(link){//showdivclick
		AjaxRequest(link, this, LDNum)
		clicked=true
	}
	
	this.loaddivclickreset=function(hiddendivid){//showdivclickreset
		clicked=false
		div.quickloaddiv(hiddendivid)
	}
	
	this.loaddivclickresetOnRec=function(hiddendivid){//showdivclickreset
		clicked=false
		divLoadOnRec = hiddendivid
	}
	
	this.AjaxUpdate=function(){
		if(divLoadOnRec != ""){
			alert(divLoadOnRec)
			div.quickloaddiv(divLoadOnRec)
			divLoadOnRec=""
		}
	}
}

imgview=new Image;
imgview.src="images/icon_view_very_small.gif";
imgedit=new Image;
imgedit.src="images/icon_edit_very_small.gif";
imgdelete=new Image;
imgdelete.src="images/icon_delete.gif";
imgpdf=new Image;
imgpdf.src="../images/pdf_icon.gif";
imgarrow=new Image;
imgarrow.src="images/arrow_2.gif";

function delayexec(toexec){
	if(pageloaded){
		setTimeout(toexec,100);
	}else{
		setTimeout('delayexec("'+toexec+'")',100)
	}
}

function showLoading(){
	try{
		showdiv("loading");
	}catch(err){}
	pagesLoading++;
	//var loadingDiv = document.getElementById('loading');
	//loadingDiv.style.display='block'
	
	//loadingDiv.className="visibleloading";
	//var loadingDiv = document.createElement('div');
	//loadingDiv.innerhtml="Loading...";
	//body.appendChild(loadingDiv);
}

function hideLoading(){
	pagesLoading--;
	if(pagesLoading==0){
		try{
			hidediv("loading");
		}catch(err){}
	}
}

function EditorToHTML(id){
	var oEditor = FCKeditorAPI.GetInstance(id);
	document.getElementsByName(id)[0].value = oEditor.GetXHTML(true);
}

function FCKeditor_OnComplete( editorInstance )
{
	hideLoading();
}

function HTMLToEditor(id){
	showLoading();
	var oFCKeditor = new FCKeditor(id);
	oFCKeditor.BasePath	= "FCKeditor/";
	oFCKeditor.ToolbarSet = 'ECCToolbar';
	//oFCKeditor.ToolbarSet = 'Basic';
	oFCKeditor.Width = "600px";//was 95%
	oFCKeditor.Height = "400px";
	oFCKeditor.ReplaceTextarea();
}

var curMessageThread = 0;
function displayMessage(msgText){
	try{
		showdiv("message");
		msg = document.getElementById("message");
		msg.innerHTML = msgText;
		if(curMessageThread != 0){
			clearTimeout(curMessageThread);
		}
		curMessageThread = setTimeout("clearMessage()",10000);
	}catch(err){}
}

function clearMessage(){
	curMessageThread = 0;
	hidediv("message");
}

//function BrowsePictures( functionData )
//{
	//var finder = new CKFinder() ;
	//finder.BasePath = 'ckfinder/' ;
	//finder.StartupPath = "Pictures:/" ;
	//finder.SelectFunction = SetFileField ;
	//finder.SelectFunctionData = functionData ;
	//finder.Popup() ;
//}

//function BrowseDocuments( functionData )
//{
	//var finder = new CKFinder() ;
	//finder.BasePath = 'ckfinder/' ;
	//finder.StartupPath = "Documents:/" ;
	//finder.SelectFunction = SetFileField ;
	//finder.SelectFunctionData = functionData ;
	//finder.Popup() ;
//}

//function SetFileField( fileUrl, data )
//{
	//document.getElementById( data["selectFunctionData"] ).value = fileUrl ;
//}


function BrowsePictures( functionData )
{
	// You can use the "CKFinder" class to render CKFinder in a page:
	var finder = new CKFinder();

	// The path for the installation of CKFinder (default = "/ckfinder/").
	finder.basePath = 'ckfinder/';

	//Startup path in a form: "Type:/path/to/directory/"
	finder.startupPath = "Pictures:/";

	// Name of a function which is called when a file is selected in CKFinder.
	finder.selectActionFunction = SetFileField;

	// Additional data to be passed to the selectActionFunction in a second argument.
	// We'll use this feature to pass the Id of a field that will be updated.
	finder.selectActionData = functionData;

	// Launch CKFinder
	finder.popup();
}

function BrowseDocuments( functionData )
{
	// You can use the "CKFinder" class to render CKFinder in a page:
	var finder = new CKFinder();

	// The path for the installation of CKFinder (default = "/ckfinder/").
	finder.basePath = 'ckfinder/';

	//Startup path in a form: "Type:/path/to/directory/"
	finder.startupPath = "Documents:/";

	// Name of a function which is called when a file is selected in CKFinder.
	finder.selectActionFunction = SetFileField;

	// Additional data to be passed to the selectActionFunction in a second argument.
	// We'll use this feature to pass the Id of a field that will be updated.
	finder.selectActionData = functionData;

	// Launch CKFinder
	finder.popup();
}

// This is a sample function which is called when a file is selected in CKFinder.
function SetFileField( fileUrl, data )
{
	document.getElementById( data["selectActionData"] ).value = fileUrl;
}

// This is the code needed to send the URL of a file back to CKEditor when selected in custom file browser
// Helper function to get parameters from the query string.
function getUrlParam(paramName)
{
  var reParam = new RegExp('(?:[\?&]|&amp;)' + paramName + '=([^&]+)', 'i') ;
  var match = window.location.search.match(reParam) ;
  return (match && match.length > 1) ? match[1] : '' ;
}

// This was Josh's first hand-written javascript code ever!
// This is the code used to call the custom in-house File Browser
function NewFileBrowserWindow( functionData, foldertypes )
{
	window.open("file_browser.cfm?fieldname=" + functionData + "&Type=" + foldertypes, "NewWin", "toolbar=no, location=no, scrollbars=yes, width=1000, height=600");
}

// This was Josh's second hand-written javascript code ever!
// This is the code used to send the the file name back to a form when selected in the custom in-house File Browser
function ChooseFileField(fileURL, formfieldname)
{
	document.getElementById(formfieldname).value = fileURL;	
}
