var xmlHttp;
var loadstatustext="<p style='float:left;text-align:center'><img src='/images/ajax-loader.gif' /></p>"

function setAndExecute(divId, innerHTML)  
{  
	var div = document.getElementById(divId);  
	div.innerHTML = innerHTML; 
	var x = div.getElementsByTagName("script");   
	for(var i=0;i<x.length;i++)  
	{  
	    eval(x[i].text); 
	}  
 }
 
function ajaxSelectNoLoader(page_id,filename,displayid,param1,param2,param3,param4,param5,param6,param7,param8)
{ 
	
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url=filename;
	url=url+"?page_id="+page_id;
	url=url+"&sid="+Math.random();	
	if (param1) url=url+"&param1="+param1;	
	if (param2) url=url+"&param2="+param2;
	if (param3) url=url+"&param3="+param3;
	if (param4) url=url+"&param4="+param4;
	if (param5) url=url+"&param5="+param5;
	if (param6) url=url+"&param6="+param6;
	if (param7) url=url+"&param7="+param7;
	if (param8) url=url+"&param8="+param8;
	
	//document.getElementById(displayid).innerHTML=loadstatustext;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	
	function stateChanged() {
		if (xmlHttp.readyState==4) {
			setAndExecute(displayid, xmlHttp.responseText);
		}
	}
}

function ajaxSelect(page_id,filename,displayid,param1,param2,param3,param4,param5,param6,param7,param8) 
{ 
	
	xmlHttp=GetXmlHttpObject();

	if (xmlHttp==null) {
		  alert ("Your browser does not support AJAX!");
		  return;
	  } 

	var url=filename;
	url=url+"?page_id="+page_id;
	url=url+"&sid="+Math.random();	
	if (param1) url=url+"&param1="+param1;	
	if (param2) url=url+"&param2="+param2;
	if (param3) url=url+"&param3="+param3;
	if (param4) url=url+"&param4="+param4;
	if (param5) url=url+"&param5="+param5;
	if (param6) url=url+"&param6="+param6;
	if (param7) url=url+"&param7="+param7;
	if (param8) url=url+"&param8="+param8;
	
	document.getElementById(displayid).innerHTML=loadstatustext;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	
	function stateChanged() {
		if (xmlHttp.readyState==4) {
			setAndExecute(displayid, xmlHttp.responseText);
		}
	}
}
 
 
function easyAjaxNoLoader(url,displayid,requestMethod,form,fieldList,statusText,action){
	
	xmlHttp=GetXmlHttpObject();
	
	if(!statusText) statusText=loadstatustext;
	if(!action) action = void(0);
	
	if (xmlHttp==null) {
		  alert ("Your browser does not support AJAX!");
		  return;
	} 
		
	
	if (url.indexOf("?")=='-1')
		url=url+"?sid="+Math.random();
	else
		url=url+"&sid="+Math.random();
	
	//document.getElementById(displayid).innerHTML=statusText;
	xmlHttp.onreadystatechange=stateChanged;
	
	if (!requestMethod) requestMethod='GET' 
	
	if (requestMethod=='POST')
	{
		xmlHttp.open("POST", url, true);		
		var params ="";
		var frm= document.forms[form];
		
		if (fieldList)
		{			
			valueArray = fieldList.split(",");		
			
			for(var i=0; i<valueArray.length; i++){
				var fldtemp=valueArray[i];
				currentfield=frm[fldtemp];				
				if (currentfield) 
				{					
					if (params=='')					
				 		params=valueArray[i]+"="+currentfield.value;
					else
						params=params+"&"+valueArray[i]+"="+currentfield.value;
				}							
			}	
		}	
			
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");		
	}
	else
	{
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	
	
	function stateChanged() {
		if (xmlHttp.readyState==4) { 
			setAndExecute(displayid, xmlHttp.responseText);
			eval(action);
		}
	}

	params=encodeURI(params);
	
	if (requestMethod=='POST')
	{
		xmlHttp.send(params);
		//document.getElementById(displayid).innerHTML=statusText
	}
}

function easyAjax(url,displayid,requestMethod,form,fieldList,statusText,action){
	xmlHttp=GetXmlHttpObject();
	
	if(!statusText) statusText=loadstatustext;
	if(!action) action = void(0);
	
	if (xmlHttp==null) {
		  alert ("Your browser does not support AJAX!");
		  return;
	} 
		
	
	if (url.indexOf("?")=='-1')
		url=url+"?sid="+Math.random();
	else
		url=url+"&sid="+Math.random();
	
	//document.getElementById(displayid).innerHTML=statusText (This is a mistake being here. It produces a javascript error in the case you replace the dive that contains the form)
	xmlHttp.onreadystatechange=stateChanged;
	
	if (!requestMethod) requestMethod='GET' 
	
	if (requestMethod=='POST')
	{
		xmlHttp.open("POST", url, true);		
		var params ="";
		var frm= document.forms[form];
		
		if (fieldList)
		{			
			valueArray = fieldList.split(",");		
			
			for(var i=0; i<valueArray.length; i++){
				var fldtemp=valueArray[i];
				currentfield=frm[fldtemp];				
				if (currentfield) 
				{					
					if (params=='')					
				 		params=valueArray[i]+"="+currentfield.value;
					else
						params=params+"&"+valueArray[i]+"="+currentfield.value;
				}							
			}	
		}	
			
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");		
	}
	else
	{
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
	
	
	function stateChanged() {
		if (xmlHttp.readyState==4) { 
			setAndExecute(displayid, xmlHttp.responseText);
			eval(action);
		}
	}

	params=encodeURI(params);
	
	if (requestMethod=='POST')
	{
		xmlHttp.send(params);
		document.getElementById(displayid).innerHTML=statusText
	}
	
	document.getElementById(displayid).innerHTML=statusText
}


function easyAjaxPost(url,displayid)
{ 
	var http = new XMLHttpRequest();
	var text='';
 	var n="wysiwyg" +document.getElementById('editor').value;
	var text=document.getElementById(n).contentWindow.document.body.innerHTML;
	
	//var enter = new RegExp("chr(10)");
	//text = text.replace( enter , "<br />" );  //replaces all enters to <br />
	
	var space = new RegExp("&nbsp;");
	var br = new RegExp("<BR >");
	
	text = text.replace( br , '<br /> ' );  //replaces all <BR> to <br />
	text=text.replace(/\"/g,'\'');	//replaces all double quotes to single
	text=text.replace('\'','a');	//replaces all double quotes to single
	text = text.replace( space , ' ' );  //replaces all enters to 
	
	http.open("POST", url, true);
	var params = "text="+text;
	

	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			
			document.getElementById(displayid).innerHTML=http.responseText;
			generate_wysiwyg(document.getElementById('editor').value);
		}
	}

	http.send(params);
}


function hiddenAction(filename,target,action,parameters)
{	
	var xmlhttp;
	var dt = new Date()    //today's date
	var mill=new Date(3000, 00, 01, 00, 00, 00)    //Next millennium start date
	var diff = mill-dt    //difference in milliseconds
	var mtg = new String(diff/86400000)  

	xmlhttp= GetXmlHttpObject();
	var params;				
	params="action="+action+"&target="+target+'&mtg='+mtg;				
	if (parameters)
	{
		params=params+"&"+parameters;
	}

	xmlhttp.open("GET", filename+"?"+params,true);				
	xmlhttp.send(null);
}		


function loadProperties(filename,form,idfield,name,page_id,propertyDiv,defDir) 
{	
	var xmlhttp;
	var wt="";
	var st="";
	
	if (!propertyDiv) propertyDiv='property';	
	
	if (!defDir) defDir='/console';
	if (defDir=='[root]') defDir='';	
	
	var w = document.getElementById(propertyDiv);	
	
	if (document.all.dwtitle) wt = document.all.dwtitle;
	if (document.all.propertyStatusBar) st=document.all.propertyStatusBar;
	
	var dt = new Date()    //today's date
	var mill=new Date(3000, 00, 01, 00, 00, 00)    //Next millennium start date
	var diff = mill-dt    //difference in milliseconds
	var mtg = new String(diff/86400000)  
	
	id=eval("document.forms[form]."+idfield+".value");
	
	if (document.forms[form].t_id) 	{t_id_val=document.forms[form].t_id.value;}	else {t_id_val=0;}
	if (document.forms[form].ct_id) 	{ct_id_val=document.forms[form].ct_id.value;}	else {ct_id_val=0;}
	if (document.forms[form].cale_id) 	{cale_id_val=document.forms[form].cale_id.value;}	else {cale_id_val=0;}
	
	var table;
	if (idfield=='pr_id') table='easy_profile'
	if (idfield=='nlt_id') table='easy_nl_type'				
	if (idfield=='f_id') table='easy_form'
	if (idfield=='v_id') table='easy_page_version'
	if (idfield=='t_id') table='easy_table'
	if (idfield=='page_id') table='page'
	if (idfield=='ff_id') table='easy_form_field'		
	if (idfield=='tf_id') table='easy_table_field'			
	if (idfield=='cs_id') table='crm_section'	
	if (idfield=='ct_id') table='crm_tab'	
	if (idfield=='cto_id') table='crm_tab_object'
	if (idfield=='ctof_id') table='crm_tab_object_field'
	if (idfield=='sof_id') table='crm_source_object_field'
	if (idfield=='ctof_id') table='crm_tab_object_field'
	if (idfield=='msg_id') table='crm_message'			
	
	var params= "table="+table+"&idfield="+idfield+"&id="+id+"&mtg="+mtg+"&page_id="+page_id;
	if (t_id_val) params=params+"&t_id_val="+t_id_val;
	if (ct_id_val) params=params+"&ct_id="+ct_id_val;
	
	if (document.forms[form].custom_propertyload_params && document.forms[form].custom_propertyload_params!='')
	{
		params=params+'&'+document.forms[form].custom_propertyload_params.value;
	}	
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp.readyState==1 && st!='') {
		st.innerHTML=loadstatustext;
	}
	
	w.innerHTML=loadstatustext;
	
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET", defDir+filename+"?"+params,true);	
	
	xmlHttp.send(null);
	
    function stateChanged() {
		if (xmlHttp.readyState==4) { 
			if (w) w.innerHTML=xmlHttp.responseText; 						 					
			if (wt) wt.innerHTML=document.forms[form].section.value+" "+name;		 		
			if (st!='') st.innerHTML=" ";
		}
	}
}


function loadProfileFieldProperties(filename,idfield,val,form) 
{	
	var xmlhttp;
	var st="";	
	var w = document.all.property;			
	
	if (!form) form="fieldslist";
	/* var wt = document.all.dwtitle; */
	
	if (document.all.propertyStatusBar) st=document.all.propertyStatusBar;
	
	var dt = new Date()    //today's date
	var mill=new Date(3000, 00, 01, 00, 00, 00)    //Next millennium start date
	var diff = mill-dt    //difference in milliseconds
	var mtg = new String(diff/86400000)  
	
	
		 
	if (idfield=='nlt_id' || idfield=='nltf_id' || idfield=='nlf_id')
	{
		nlt_id=document.forms[form].nlt_id.value;
		nlf_id=document.forms[form].nlf_id.value;
		nltf_id=document.forms[form].nltf_id.value;
	}
	else if (idfield=='so_id' || idfield=='cto_id' || idfield=='ct_id')
	{
		so_id=document.forms[form].so_id.value;
		cto_id=document.forms[form].cto_id.value;
		ct_id=document.forms[form].ct_id.value;
	}
	else
	{
		pr_id=document.forms[form].pr_id.value;
		f_id=document.forms[form].f_id.value;
		prf_id=document.forms[form].prf_id.value;
	}
	
	
	if (idfield=='nlt_id' || idfield=='nltf_id' || idfield=='nlf_id')
	{
		var params= "idfield="+idfield+"&nlt_id="+nlt_id+"&nlf_id="+nlf_id+"&nltf_id="+nltf_id+"&mtg="+mtg;
	}
	else if (idfield=='so_id' || idfield=='cto_id' || idfield=='ct_id')
	{
		var params= "idfield="+idfield+"&so_id="+so_id+"&cto_id="+cto_id+"&ct_id="+ct_id+"&mtg="+mtg;
	}
	else
	{
		var params= "idfield="+idfield+"&pr_id="+pr_id+"&f_id="+f_id+"&prf_id="+prf_id+"&mtg="+mtg;			
	}
		
	xmlhttp=GetXmlHttpObject();
	
	xmlhttp.open("GET", "/console"+filename+"?"+params,true);		
	xmlhttp.onreadystatechange=function() {		
	
	if (xmlhttp.readyState==1 && st!='') {
		st.innerHTML="Loading...";	
	}
	
    if (xmlhttp.readyState==4) {			
		 w.innerHTML=xmlhttp.responseText;				 						 	
		 // showPropertyTextAreas ('panelFields','field_type');			
		/*
		 if (idfield=='f_id')
		 	wt.innerHTML="Easy Field : "+val;												 
		 else if (idfield=='nlf_id') 
		 	wt.innerHTML="Newsletter Field : "+val;						 
		 else if (idfield=='nltf_id') 
		 	wt.innerHTML="Newsletter Type Field : "+val;						 
		 else
			 	wt.innerHTML="Profile Field : "+val;	
		*/	
		 if (st!='') st.innerHTML=" ";		
	  }
	}
	xmlhttp.send(null);
}

function showMovePopup(idfield,id,selected,source,form,path,id2,idfield2,val2,idfield3,val3) 
{	
	
	var d = document.getElementById('moveDiv');
	var dt = new Date()    //today's date
	var mill=new Date(3000, 00, 01, 00, 00, 00)    //Next millennium start date
	var diff = mill-dt    //difference in milliseconds
	var mtg = new String(diff/86400000)  
	var params= "selected="+selected+"&id="+id+"&frm="+form+"&mtg="+mtg;
	
	if (id2) { var params= params+"&id2="+id2; }
	
	if (idfield2 && val2) {params= params+"&"+idfield2+"="+val2;}		
	if (idfield3 && val3) {params= params+"&"+idfield3+"="+val3;}
	
	var a=document.forms[form];	
	
	a.selected.value=selected;	
	document.getElementsByName(idfield)[0].value=id;	
	
	var thispath='';
	if (path) thispath=path;	
	
	
	
	if (source!='')
	{ 				
		
		xmlhttp.open("GET", thispath+source+"_popup.cfm?"+params,true);										
		
		var preInc="rect(auto #request.fields.popup_width#px #request.fields.profile.popup.";		
		
		var postInc=".height#px auto)";
		if (d){
			d.style.zIndex = 1000;				
			d.style.clip = ""+preInc+source+postInc+"";						
		}
	}
	
	xmlhttp.onreadystatechange=function() {
	  if (xmlhttp.readyState==4) { 	  	
	 	 if(d) d.innerHTML=xmlhttp.responseText;				
	  }
	}
	
	
	
	xmlhttp.send(null);			
	
	if (isIE) 
	{
		document.oncontextmenu = showMove;		 
	}
	else
	{
		document.captureEvents(Event.MOUSEDOWN);
		document.onmousedown = showMove ;	
	}
}



function showMove (e) {	
	var element=document.getElementById('moveDiv'); 	

	if (document.all)//IE
	   {
	   	if(event.srcElement.parentNode.parentNode.parentNode.className == 'genericTBLeft' || event.srcElement.parentNode.parentNode.parentNode.parentNode.className == 'genericTBLeft' || event.srcElement.className=='rTC' || event.srcElement.parentNode.className=='rTC'){			
			element.style.visibility = 'visible'
		    element.style.pixelLeft = event.clientX+20+ document.body.scrollLeft; 					  
		    element.style.pixelTop = event.clientY+ document.body.scrollTop;				
		    element.style.display = 'table-header-group'; 	
		 
		    return false; 
				
		}
		else {
			element.style.display='none';  	
			element.style.visibility = 'hidden';
		}
	  }
	  else if (document.getElementById && !document.all) //Mozilla
	  { 
	  		
			if (e.target.parentNode.parentNode.parentNode.className == 'genericTBLeft' || e.target.className=='rTC'){
				if (e.which == 3) { 
					// Temporary variables to hold mouse x-y pos.s
					var tempX = 0;
					var tempY = 0;
	
					tempX = e.pageX;
		   			tempY = e.pageY+20;
					
					// catch possible negative values in NS4
					if (tempX < 0){tempX = 0}
					if (tempY < 0){tempY = 0}  
					
					element.style.left = tempX; 
					element.style.top = tempY;	 	
					element.style.display='block';  	
					element.style.visibility = 'visible';
					element.display = 'table-header-group';    	
					return false; 
				} 
			}
			else{
				element.style.display='none';  	
				element.style.visibility = 'hidden';
			}
	  } 
	  return true; 
} 

function saveFilterV3(filename,form,t_id,field,name,selectfield,consoleFolder) 
{	
	var xmlhttp;
	var wt="";
	
	var dt = new Date()    //today's date
	var mill=new Date(3000, 00, 01, 00, 00, 00)    //Next millennium start date
	var diff = mill-dt    //difference in milliseconds
	var mtg = new String(diff/86400000)  
	var id2="";
	
	if (!selectfield) selectfield='filtering_select';
	if (!consoleFolder) consoleFolder='console';
	
	if (!name) name="Report";
	
	filtering_title=eval("document.forms[form]."+field+".value");
	if(filtering_title!='' && t_id!='')
	{
		var params= "filtering_title="+filtering_title+"&t_id="+t_id+"&mtg="+mtg;					

		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		
		xmlhttp.open("GET", "/"+consoleFolder+"/"+filename+"?"+params,false);		
		xmlhttp.onreadystatechange=function() {

		if (xmlhttp.readyState==1) {
			updateStatusBar("Saving "+name+" "+filtering_title+"...");
		}
		
	    if (xmlhttp.readyState==4) {					
			updateStatusBar(xmlhttp.responseText);
		  }
		}
		
		xmlhttp.send(null);					
		
	}
	else
	{
		updateStatusBar("Please specify "+name+" name");	
	}	
}


function deleteReportV3 (tr_id,t_id,title) {
var answer = confirm("Delete report "+title+"?");
if (answer){
	hideDeletedRow(tr_id);
	easyAjax('/console/databases/records/record_filter_delete.cfm?t_id='+t_id+'&ftitle='+title,'dummyFilterScreen',false);
}
	
}

function GetXmlHttpObject()
{
	var xmlHttp=null;

	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
	    {
		
	    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	    }
	  catch (e)
	    {
	    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	    }
	  }
return xmlHttp;
}
