var xmlHttp;
var loadstatustext="<table width='100%'><tr><td align='center'><img src='/images/loading.gif' alt=''></td></tr></table>"

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 setAndExecuteParent(divId, innerHTML)  
{  
	var div = parent.document.getElementById(divId);  
	div.innerHTML = innerHTML; 
	var x = div.getElementsByTagName("script");   
	for(var i=0;i<x.length;i++)  
	{  
	    eval(x[i].text); 
	}  
 } 
 
function parenteasyAjax(url,displayid,statusText){
	xmlHttp=GetXmlHttpObject();
	
	if(!statusText) statusText=loadstatustext;
	
	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();

	parent.document.getElementById(displayid).innerHTML=statusText;
	xmlHttp.onreadystatechange=stateChanged;
	
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	
	function stateChanged() {
		if (xmlHttp.readyState==4) { 
			setAndExecuteParent(displayid, xmlHttp.responseText);
		}
	}
}

/*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
	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);
	}
}*/

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();
	
	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);
		document.getElementById(displayid).innerHTML=statusText
	}
	
	
	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 easyAjaxPostComponents(url,displayid,recordcount,tab_id)
{ 
	var http = new XMLHttpRequest();
	var list_content;
	var params = "?dummy="+1+'&tab_id='+tab_id;
	var col_id_;
	var counter;
	
	for (x=1;x<=recordcount;x=x+1) {
	 	var temp="ul"+x;
		
	 	list_content=document.getElementById(temp+"_list").value;
		col_id_=document.getElementById(temp+"_id").value;
		params = params+'&col_id_'+col_id_+'='+list_content;
	 }
	
	http.open("POST", url, true);
	
	//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;
		}
	}

	http.send(params);
}


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,event) 
{	
	
	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(event) ;	
	}
}

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=='recordTableCol' || 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' || e.target.parentNode.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,field1,field2,field3,field4,field5,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]."+field1+".value");
	groupavailability=get_check_value(field2);
	useravailability=get_check_value(field3);
	parent_id = field4;
	availability = field5;
	
	if(filtering_title!='' && t_id!='')
	{
		var params= "filtering_title="+filtering_title+"&availability="+availability+"&groupavailability="+groupavailability+"&useravailability="+useravailability+"&parent_id="+parent_id+"&t_id="+t_id+"&mtg="+mtg;					

		xmlHttp=GetXmlHttpObject();
		
		function stateChanged1() {
			if (xmlHttp.readyState==4) {			
				updateStatusBar(xmlHttp.responseText);
				refreshTree();
				removeElement('filter_properties')
			}
		}
	
		xmlHttp.onreadystatechange=stateChanged1;
		xmlHttp.open('GET', '/'+consoleFolder+'/'+filename+'?'+params,true);	
		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);
		params = 't_id='+t_id+'&ftitle='+title;
		params=encodeURI(params);
		easyAjax('/console/databases/records/record_filter_delete.cfm?'+params,'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;
}
