//    Foundation utilitary CMS Console File
//    Copyright (C) 2010  Dan Angelescu

//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.

//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.

//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
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;
}


function ajaxFormFunction(phpfile,parent,close_box,width){
  var str='';
  xmlHttp=GetXmlHttpObject();if (xmlHttp==null){alert ("Your browser does not support AJAX!");return;}
  xmlHttp.abort();
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==1){
	  show_ldr();
	}else
    if(xmlHttp.readyState==4)
    {
      str=unescape(xmlHttp.responseText);
	  var loc=str.split("!@#location!@#");
	  if(loc[1])document.location=loc[1];
	  else show_popup('<div align="center">'+str+'</div>',width,close_box);
    }
  }
  xmlHttp.open("GET",phpfile+checknodes(parent),true);
  xmlHttp.send(null);
}

function ajaxLoadFile(phpfile,divId){
  var str='';
  xmlHttp=GetXmlHttpObject();if (xmlHttp==null){alert ("Your browser does not support AJAX!");return;}
  xmlHttp.abort();
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==1){
	  show_ldr();
	}else
    if(xmlHttp.readyState==4)
    {
      str=unescape(xmlHttp.responseText);
	  var div=document.getElementById(divId);
	  if(div)div.innerHTML=str;
	  hide_popup();
    }
  }
  xmlHttp.open("GET",phpfile+checknodes(parent),true);
  xmlHttp.send(null);
}

function ajaxRedirect(phpfile){
  var str='';
  xmlHttp=GetXmlHttpObject();if (xmlHttp==null){alert ("Your browser does not support AJAX!");return;}
  xmlHttp.abort();
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
    {
      str=unescape(xmlHttp.responseText);
	  window.open(str);
    }
  }
  xmlHttp.open("GET",phpfile+checknodes(parent),true);
  xmlHttp.send(null);
}

function checknodes(parent){
	var objtypes=new Array('input','textarea','select');
	var obj=document.getElementById(parent);
	if(!obj)return('');
	var i=0,a=0;
	var inputs;
	var str='';
	for(a=0;a<objtypes.length;a++){
		inputs=obj.getElementsByTagName(objtypes[a]);
		for(i=0;i<inputs.length;i++){
			if((inputs[i].type=='radio')&&(!inputs[i].checked))continue;
			if(str!='')str+='&';
			str+=inputs[i].name+'='+escape(inputs[i].value);
		}
	}
	return(str);
}
