function cursor(ctype)
{ 
 if (ctype == 1)
 {
   document.body.style.cursor="wait";
   document.getElementById("vb_error").innerHTML = "";
 }
 else  
   document.body.style.cursor="";
  
}

function fckeditor_init (htmlContent)
{
 if (FCKeditorAPI.GetInstance('body'))
 {
    var ed = FCKeditorAPI.GetInstance('body');
    ed.SetHTML(htmlContent);  
 }
 else
 {
  FCKeditorAPI = null;
  __FCKeditorNS = null; 
 }
}

var xmlHttp;

function makeRequest() // --- Informatiea de tip text/html
{
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType)
         {
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      else 
      {
        return http_request;
      }
}

function makeRequestXML() // --- Intormatia de tip text/xml 
{
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType)
         {
            http_request.overrideMimeType('text/xml');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      else 
      {
        return http_request;
      }
}

function makeGETRequest(sp,redirect,url)
{  
  if (url.length==0)
  {
    document.getElementById(sp).innerHTML=""
    return
  }
  
  xmlHttp=makeRequest();
  if (xmlHttp==null)
  {
    alert ("Browserul nu suporta cererile prin AJAX")
    return
  }
  url = "index.php?ac="+url;
//  alert(url);
  xmlHttp.open("GET",url,true)
  xmlHttp.onreadystatechange = function(){ alertContents(sp,redirect); }
  xmlHttp.send(null)
  // xinha_init('');
}

function makePOSTRequest(url, parameters,sp,redirect) {
   xmlHttp=makeRequest();
   if (xmlHttp==null)
   {
     alert ("Browserul nu suporta cererile prin AJAX")
     return
   }
   url = "index.php?ac="+url;
   xmlHttp.open("POST",url, true);
   xmlHttp.onreadystatechange = function(){ alertContents(sp,redirect); }   
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", parameters.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(parameters);
}

function alertContents(sp,redirect)  //--- Redesenarea Informatiei 
{
  if (xmlHttp.readyState == 4)
  {
     if (xmlHttp.status == 200) 
     {
        result = xmlHttp.responseText;

        document.getElementById(sp).innerHTML = result;
        
       if ( (redirect != '') || (redirect != 'false'))      
	    if (document.getElementById(redirect) && document.getElementById(redirect).value == 'true')
	     header(redirect);//--- Pentru redectionare daca nus erori
      }
      else 
      {
        alert('Nu poate fi gasit fisierul');
      }
  }
}


function alertContentsPrint(sp,redirect) /// --- Schimbarea valorilor fara a redesena forma 
{
  if (xmlHttp.readyState == 4)
  {
     if (xmlHttp.status == 200) 
     {    
        result  = xmlHttp.responseXML;
//        result = xmlHttp.responseText;
//	alert (xmlHttp.responseText);
	ResultXML(result)
	
        if (redirect != '')
	  if (document.getElementById(redirect).value == 'true')
	  {
	     header();//--- Pentru redectionare daca nus erori
	  }
      }
      else 
      {
        alert('Nu poate fi gasit fisierul');
      }
  }
}


function ResultXML(xmlobject) /// --- Citeste valorile dintrun fisier XML
{

//xmlstring = xmlobject;
//var xmlobject = (new DOMParser()).parseFromString(xmlstring, "text/xml");

var root = xmlobject.getElementsByTagName('root')[0];
/*           
for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
{
   var node = root.childNodes.item(iNode);
   for (i = 0; i < node.childNodes.length; i++) 
   {
     var sibling = node.childNodes.item(i);
//     alert(sibling.childNodes.length);
     for (x = 0; x < sibling.childNodes.length; x++) 
     {
        var sibling2 = sibling.childNodes.item(x);
//	alert(sibling2.childNodes.length);
        if (sibling2.childNodes.length > 0) 
	{
	  var sibling3 = sibling2.childNodes.item(0);
	  alert(sibling3.data+' -> '+sibling3.nodeValue);
	}
     }
    }
}

/*
 // alert (str_xml);
 
 // var root = str_xml.getElementsByTagName('root').item(0);
 var root = str_xml.documentElement.childNodes.item(0);
 alert (root.childNodes.length);
 for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
 {
   recNodeChild = root.childNodes.item(iNode).childNodes;
   for (var iChild = 0; iChild < recNodeChild.length; iChild++) 
   {
    //     recChild = root.childNodes.item(iChild).childNodes;
     
     if (recNodeChild.item(iChild).tagName)
     {
       alert (recNodeChild.item(iChild).tagName+' -> '+ recNodeChild.item(iChild).value+" -> "+recNodeChild.item(iChild).length);
       
     }
   }
   
 }
 */ 
//  alert (root);
  
   for (var iNode = 0; iNode < root.childNodes.length; iNode++) 
   {
     var node = root.childNodes.item(iNode);
     for (i = 0; i < node.childNodes.length; i++)
     {
       var sibl = node.childNodes.item(i);
       var len = parseInt(sibl.childNodes.length / 2);
       var arr = new Array(len);
       var cnt = 0;
       
       for (x = 0; x < sibl.childNodes.length; x++) 
       {
         var sibl2 = sibl.childNodes.item(x);
         var sibl3;
         if (sibl2.childNodes.length > 0) 
	 {
	   sibl3 = sibl2.childNodes.item(0);
	   arr[cnt] = sibl3.nodeValue;
//	   alert (sibl3.);
	   cnt++;
	 }
       }
       if (len == cnt) addInput(arr);
     }
   }
   
}

function addInput(arr) /// --- Scimba Valoarea curenta cu cea din XML
{
   if (arr[0])
   {    
//    alert(arr[0]+arr[1]);  
    
//alert (arr[1]);
      if (arr[0] == 'body')
      {
    //    xinha_init(arr[1]);
        fckeditor_init(arr[1]);
//	alert(document.getElementById('body').value)
      }
      else
       document.getElementById(arr[0]).value = arr[1];
   }
}

function get(obj,src,sp,redirect,str) 
{

 var poststr='';
 var element = document.getElementsByTagName('input');
 
 for (i=0; i<str.length; i++)
 {  
   if (i != 0) poststr+='&';   
   
   if (str[i] == 'body')      
   {
    param = FCKeditorAPI.GetInstance('body').GetXHTML();
    param = param.replace(/&/g,"%26");
   }   
   else
   {
    //alert(str[i]);
    param = document.getElementById(str[i]).value;
    param = param.replace(/&/g,"%26");
   }   
      
  // elem = document.getElementById[str[i]];
  // alert(str[i]);
   if (str[i] == 'isvisible')
   {
    if (document.getElementById(str[i]).checked) poststr+=str[i]+'=1';
    else poststr+=str[i]+'=0';
   }
   else
    poststr+=str[i]+'='+encodeURI(param);
   
 }
// alert (poststr);  
 
// var content = FCKeditorAPI.GetInstance('body').GetXHTML();
 
 // alert (FCKeditorAPI.GetInstance('body').GetXHTML());
  makePOSTRequest(src, poststr,sp,redirect);
}

function makePOSTRequestPrint(url, parameters,sp,redirect) //--- Face post fara a redesena forma si foloseste XML
{
   xmlHttp=makeRequestXML();
   if (xmlHttp==null)
   {
     alert ("Browserul nu suporta cererile prin AJAX")
     return
   }
   
//   FCKeditorAPI = null;
//   __FCKeditorNS = null;  
   
   url = "index.php?ac="+url;
   xmlHttp.open("POST",url, true);
   xmlHttp.onreadystatechange = function(){ alertContentsPrint(sp,redirect); }   
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", parameters.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(parameters);
//   alert(FCKeditorAPI);
}

function getPrint(obj,src,sp,redirect,str)  //--- Face post fara a redesena forma si foloseste XML
{
 var poststr='';
 for (i=0; i<str.length; i++)
 {  
   if (i != 0) poststr+='&';

   if (str[i] == 'body')      
   {
    param = FCKeditorAPI.GetInstance('body').GetXHTML();
    param = param.replace(/&/g,"%26");
   }   
   else
   {
    param = document.getElementById(str[i]).value;
    param = param.replace(/&/g,"%26");
   }   
   poststr+=str[i]+'='+encodeURI(param);   
 }

 makePOSTRequestPrint(src, poststr,sp,redirect);
}

function header(ac)
{
  var err = document.getElementById('error').innerHTML;
  if (err == "1")
  {
     if (ac == 'close')
     {
      window.location.replace('index.php');
     } 
     else
     {      
      var div_info = document.getElementById('div_'+ac).value;
      makeGETRequest(div_info,'false',ac);
     }  
  }
}

function PopupClose (div_info, class_ac)
{  
 if (document.getElementById(div_info))
 {    
   makeGETRequest(div_info,'false',class_ac);
 }
 else alert('Not exists div : ' + div_info);
 
}

function redirect()
{
  window.location.replace('index.php');
}

function get_1(obj,src,sp,redirect) 
{
  var getstr = "";
  var element = document.getElementsByTagName('input');

  for (i=0; i<element.length; i++) 
  {
  
       if (element[i].type == "text")     getstr += element[i].name + "=" + element[i].value + "&";
       if (element[i].type == "hidden")     getstr += element[i].name + "=" + element[i].value + "&";
       if (element[i].type == "checkbox") 
          if (element[i].checked) getstr += element[i].name + "=" + element[i].value + "&";
  	  else getstr += element[i].name + "=&";
       if (element[i].type == "radio") 
          if (element[i].checked)   getstr += element[i].name + "=" + element[i].value + "&";
  }
 // alert(getstr);     
  makePOSTRequest(src, getstr,sp,redirect);
}
