/////
// LEGACY LIBRARY
////

///////////////////////////////////////////////
// helps to easilly slice out substring
///////////////////////////////////////////////
function str_pull(string,start,end) {
  if (string.indexOf(start) > 0) {
    result = string.substring(eval(string.indexOf(start)+start.length));
    result = result.substring(0,result.indexOf(end));
    return result;
  } else {
    return "";
  }
}


//////////////////////////////////////////////////////////////////////
// passes either GET or POST variables to the script and returns response text
//////////////////////////////////////////////////////////////////////
function DataPost(vars,element_id,preloader) {
  DataTrade(vars,element_id,preloader,'POST');
}

function DataTrade(vars,element_id,preloader,mode) {   
    
  var xmlHttp = GetXmlHttpObject();
  
  // determining whether to use GET or POST
  if (mode == undefined) { // if mode is not specified
    if (vars.length > 5000) {  // if request is way long, using post
      var mode = "POST"; 
    } else { // otherwise, using get
      var mode = "GET";
    }  
  } 
  
  if (mode == "POST") {
    var url = vars.substring( 0, vars.indexOf("?"));
    var vars = vars.substring( eval(url.length+1), vars.length);
    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", vars.length);
    xmlHttp.setRequestHeader("Connection", "close");
  
  } else {
    var url=vars+"&sid="+Math.random();  
  }
  
  // workaround - in the past, some of those might have been specified as blank
  if (element_id == undefined)  { element_id = ""; }
  if (preloader == undefined)   { preloader = ""; }
      
  xmlHttp.onreadystatechange = function () {   
    // when data comes back
    if (xmlHttp.readyState == 4) {
      
      // if target was innerHTML of DOM id or the return function has been used
      if (element_id != "") { 
        if (element_id.indexOf("(") == -1) { // if no special function was used
          document.getElementById(element_id).innerHTML = xmlHttp.responseText;
        } else {
          eval(element_id+UrlEncode(xmlHttp.responseText)+"')");
        }
        
        if (jquery_jtip_used) { // if jtips are being used, regenerating them
          JT_init();
        }
      } else {
      // if only return of the value is needed
        return "";
      }
  
    } else if(preloader != "") { 
    // while waiting for data, will show preloader if needed
      document.getElementById(element_id).innerHTML = preloader; 
    }
    
    // after AJAX is retrieved, some custom js could be executed
    var custom_js = $('.FMW_js_afterload').html();
    $('.FMW_js_afterload').remove();      
    if (custom_js != "") {
      eval(custom_js);
    }
    
  }
  
  if (mode == "POST") {
    xmlHttp.send(vars);
  } else { 
    xmlHttp.open("GET",url,true);   
    xmlHttp.send(null); 
  }
  
}

////////////////////////////////////////////////////////
// used to start cronjobs and so on
///////////////////////////////////////////////////////
function DataStart(remote_url,redirect) {
  var xhr = $.ajax({
     url: remote_url
  });
  xhr.abort(); 
  if (redirect) {
    GoTo(redirect);
  }
}

function DataExec(vars)
{
  var xmlHttp = GetXmlHttpObject();
  var url=vars;
  //+"&sid="+Math.random();
  
  xmlHttp.onreadystatechange=function () 
  {   
    // when data comes back
    if (xmlHttp.readyState==4) {
      eval(xmlHttp.responseText);
    } 
  }
  xmlHttp.open("GET",url,true);   
  xmlHttp.send(null);  
}





//////////////////////////////////
// gateway to php
/////////////////////////////////
function TradeData(vars,element_id,preloader)
{
  var xmlHttp = GetXmlHttpObject();
  var url=vars+"&sid="+Math.random();
  xmlHttp.onreadystatechange=function () 
  {   
    if ((xmlHttp.readyState==4)&&(element_id!=""))   
    { document.getElementById(element_id).innerHTML = xmlHttp.responseText;  delete xmlHttp;}  
    else if(preloader!="") { document.getElementById(element_id).innerHTML = preloader; }
  }
  xmlHttp.open("GET",url,true);   xmlHttp.send(null);  
}

      // internal AJAX function
      function GetXmlHttpObject()
      {
      var xmlHttp=null;
      try { xmlHttp=new XMLHttpRequest(); } // Firefox, Opera 8.0+, Safari
      catch (e)  {  // Internet Explorer
      try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } }
      return xmlHttp;
      }

/////////////////////////////////
// replaces characters
/////////////////////////////////
function ReplaceChars(entry,out,add) {
temp = "" + entry;

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add + 
temp.substring((pos + out.length), temp.length));
}
return(temp);
}

//////////////////////////////////////////////////////////////////////////////////////////////////
// this function should replace SafeInput in the future - urlencode handles everything except '
//////////////////////////////////////////////////////////////////////////////////////////////////
function UrlEncode(entry) {
  entry = urlencode(entry);
  entry = ReplaceChars(entry,"'","--singlequotehere--");
  return(entry);
}

function UrlDecode(entry) {
  entry = ReplaceChars(entry,"--singlequotehere--","'");
  entry = urldecode(entry);

  return(entry);
}


/////////////////////////////////
// replaces those characters that can't be passed in the url without parsing
/////////////////////////////////
function SafeInput(entry) {
  // encoding line break
  //entry = entry.replace(new RegExp( "\n", "g" ),"");
  // encoding + sign
  entry = ReplaceChars(entry,"+","%2B");
  // encoding & sign
  entry = ReplaceChars(entry,"&","%26");
  // encoding = sign
  entry = ReplaceChars(entry,"=","%3d");
  // encoding ' sign
  entry = ReplaceChars(entry,"'","%27");
  // encoding " sign
  entry = ReplaceChars(entry,'"',"%27");
  
  return(entry);
}


function SafeOutput(entry) {
  // decoding &
  entry = ReplaceChars(entry,"&amp;","&");
  // decoding & sign
  entry = ReplaceChars(entry,"%26","&");
  // decoding = sign
  entry = ReplaceChars(entry,"%3d","=");
  // decoding +
  entry = ReplaceChars(entry,"%2B","+"); 
  // decoding ' sign
  entry = ReplaceChars(entry,"%27","'"); 
  // decoding " sign
  entry = ReplaceChars(entry,"%22",'"'); 

  return(entry);
}

///////////////////////////
// hides object
///////////////////////////
function Hide(id) { 
  if (document.getElementById(id) == undefined) {
    eval("Hide: "+id);
  } else {
    document.getElementById(id).style.display = "none"; 
  }
}

//////////////////////////
// unhides object as a block
//////////////////////////
function ShowBlock(id)
{ document.getElementById(id).style.display = "block"; }

///////////////////////////
// unhides object as inline
///////////////////////////
function ShowInline(id)
{ document.getElementById(id).style.display = "inline"; }

///////////////////////////
// switches between "block" and "none" values for display property
///////////////////////////
function ToogleBlock(id)
{
  var style2 = document.getElementById(id).style;
  if (style2.display == "none") { style2.display = "block"; }
  else { style2.display = "none"; }
}
////////////////////////////////////////////////
// same function tailored for hiding table rows
////////////////////////////////////////////////
function ToogleRow(id) {
  var style2 = document.getElementById(id).style;
  if (style2.display == "none") {style2.visibility = "visible"; style2.display="";}
  else {  style2.display = "none"; }
}
//////////////////////////////////////////////////////
// showing collapsed table rows
//////////////////////////////////////////////////////
function ShowRow(id) {
  var style2 = document.getElementById(id).style;
  style2.visibility = "visible"; style2.display="";
}

/////////////////////////////
// switches between "inline" and "none" values for display property 
/////////////////////////////
function ToogleInline(id)
{
  var style2 = document.getElementById(id).style;
  if (style2.display == "none") { style2.display = "inline"; }
  else { style2.display = "none"; }
}
/////////////////////////////////////////////
// returns droplist option id
/////////////////////////////////////////////
function DroplistId(id) {
  if (typeof(id) == 'object') { // can be used with 'this' as parameter
    if (id.selectedIndex == '-1') {
      return -1;
    } else {
      return(id.options[id.selectedIndex].value);
    } 
  } else {
    if (document.getElementById(id).selectedIndex == '-1') {
      return -1;
    } else {
      return(document.getElementById(id).options[document.getElementById(id).selectedIndex].value);
    } 
  }
}
////////////////////////////////////
// gets element value
////////////////////////////////////
function Value(id) {
  if (typeof(id) == 'object') { // can be used with 'this' as parameter
    return(id.value);
  } else if(document.getElementById(id)) {
    return(document.getElementById(id).value);
  } else {
    exec('Value: '+id);
  }
}

function ValueSet(id,value_new) {
  if (value_new != undefined) {
    document.getElementById(id).value = SafeOutput(value_new);
  } else {
    document.getElementById(id).value = '';
  }
}

function ValueUse(id,id_source) {
  document.getElementById(id).value = document.getElementById(id_source).value;
}

function ValueSafe(id) {
  return(SafeInput(document.getElementById(id).value));
}

function ValueRadio(name) {
  var o = $("input:checked[type='radio'][name='"+name+"']").val();
  return(o);
}


////////////////////////////////////
// gets element innerHTML
////////////////////////////////////
function Content(id) {
  return(document.getElementById(id).innerHTML);
}
/////////////////////////////////////
// changes inner HTML
/////////////////////////////////////
function ContentSet(id,content_new) {
  document.getElementById(id).innerHTML = content_new;
}

/////////////////////////////////////
// toogles inner HTML
/////////////////////////////////////
function ContentToogle(id,content_a,content_b) {
  if (document.getElementById(id).innerHTML == content_a) {
    document.getElementById(id).innerHTML = content_b;
  } else {
    document.getElementById(id).innerHTML = content_a;
  }
}

/////////////////////////////
// redirects to specified address
/////////////////////////////
function GoTo(loc) { 
  window.location.href = loc; 
}
////////////////////////////////////////////////
// same, but also opens it in a new window
////////////////////////////////////////////////
function Open(url) {
  window.open(url); 
}


///////////////////////////////////////////////////////////
// performs AJAX search
// returns search results to a div under the search field
///////////////////////////////////////////////////////////
function FMW_Search(what,input) {
  
  input = SafeInput(input); 
  
  // handling custom target div id's 
  if (what.indexOf("&div_id=") != -1) {  // if custom id div id is used
    var div_id = what.substr(what.indexOf("&div_id=")+8);
  } else {  // otherwise, reusing 'what' variable
    var div_id = what;
  }
  
  // handling custom backend paths
  if (what.indexOf("?") != -1) {  // if a custom file is used as a search backend
    var path = what+"&input=";
  } else {
    var path = "+search.php?what="+what+"&input=";
  }
  
  // once search has started...
  ShowBlock(div_id+'_results'); // making results div visible
  if (Content(div_id+'_results') == "") { // if it's empty, showing preloader
    ContentSet(div_id+'_results','<center>loading...</center>'); 
  } 
  
  FMW_Search_execute(path,div_id,input); // executing the search
  
}


/////////////////////////////////////////////////////
// enables to have just one AJAX request at a time
/////////////////////////////////////////////////////
function FMW_Search_execute(path,div_id,input) {

  // if there is no search currently running, will proceed right away with search
  if ($("#"+div_id+"_results").data('current_search') == undefined) {
    
    
    $("#"+div_id+"_results").data('last_search', input); // this will be the last search that has been executed so far
    $("#"+div_id+"_results").data('current_search', input); // and for the duration of AJAX, it will be the "current" search
    $.get(path+input,"",function(data) {
      ContentSet(div_id+"_results",data); // show results in the results DIV
      $("#"+div_id+"_results").removeData('current_search'); // once done running, current_search becomes undefined
      if ($("#"+div_id+"_results").data('queued_search') != undefined) { // if there is a pending search
        FMW_Search_execute(path,div_id,$("#"+div_id+"_results").data('queued_search')); // calling itself
        $("#"+div_id+"_results").removeData('queued_search'); // and removing queued search
      } // end of processing queued searches
    });
  
    
  } else { // if search is currently running
    $("#"+div_id+"_results").data('queued_search', input); // will queue the latest input (and overwrite any previously pending searches)
  }
}



