 /////////////////
// VARIABLES 
/////////////////
var preloaderBig = "<center><br><br><br><br><br><br>";
  preloaderBig = preloaderBig+"<img src='../images/loading-big.gif'><br><br><h3>Loading...</h3><br></center>";


/////////////////////////////////////////////////////////////////////
// waits for the specified element to be available and then executes specified action
///////////////////////////////////////////////////////////////////////////////////////
function WaitFor(what,limit,action) {
  // if the object, that is created by executing the action that we are waiting for exists..
  if (document.getElementById(limit)) { return; }

  if (document.getElementById(what)){
    action = ReplaceChars(action,"|","'");
    eval(action);
  } else {
    setTimeout("WaitFor('"+what+"','"+limit+"','"+action+"')","200");
  }
}

////////////////////////////////////////////////////////////////////////
// used to refresh images
////////////////////////////////////////////////////////////////////////
function RefreshImage(object_id,name,path)
{
  var full_path = path+name;
  document.getElementById(object_id).src = "http://i.digitalpipelines.com/img/loading-big.gif";
  document.getElementById(object_id).src = full_path;
}


/////////////////////////////////////////////////////////////////////////
// main function, gets HTML from php script using predifined variables
/////////////////////////////////////////////////////////////////////////
function ajax_table_refresh(arguments)
{
  var target_div = "results";
  TradeData(file_name+"&"+arguments,target_div,preloaderBig);
  //document.getElementById("messages").innerHTML = "";
  
}
/////////////////////////////////////////////////////////////////////////
// only if the page is realoaded, messages div will not be cleaned
/////////////////////////////////////////////////////////////////////////
function ajax_table_initialize()
{
  var target_div = "results";
  TradeData(file_name,target_div,preloaderBig);  
}

//////////////////////////////////////////////////////////////////////
// passes GET variables to the script and returns response text
//////////////////////////////////////////////////////////////////////
function DataTrade(vars,element_id,preloader)
{
  var xmlHttp = GetXmlHttpObject();
  var url=vars+"&sid="+Math.random();
  
  xmlHttp.onreadystatechange=function () 
  {   
    // when data comes back
    if (xmlHttp.readyState==4)   
    {
      // if target was innerHTML of DOM id
      if (element_id != "") { 
        document.getElementById(element_id).innerHTML = xmlHttp.responseText;
      } 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; 
    }
  }
  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);
}

/////////////////////////////////
// replaces those characters that can't be passed in the url without parsing
/////////////////////////////////
function SafeInput(entry) {
  // encoding + sign
  entry = ReplaceChars(entry,"+","%2B");
  // encoding & sign
  entry = ReplaceChars(entry,"&","%26");
  // encoding = sign
  entry = ReplaceChars(entry,"&","%3d");

  return(entry);
}

///////////////////////////
// hides object
///////////////////////////
function Hide(id)
{ 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"; }
}

/////////////////////////////
// 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) {
  return(document.getElementById(id).options[document.getElementById(id).selectedIndex].value);
}
////////////////////////////////////
// gets element value
////////////////////////////////////
function Value(id) {
  return(document.getElementById(id).value);
}

function ValueSet(id,value_new) {
  document.getElementById(id).value = value_new;
}

function ValueUse(id,id_source) {
  document.getElementById(id).value = document.getElementById(id_source).value;
}

function ValueSafe(id) {
  return(SafeInput(document.getElementById(id).value));
}

////////////////////////////////////
// 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;
}

////////////////////////////////////////////////
// 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"; }
}

/////////////////////////////
// opens up new window
////////////////////////////
function NewWin(id,x,y) {
  if((x*1 == NaN) || (x == "")) x = 800;
  if((y*1 == NaN) || (y == ""))  y = 400;
  window.open("_popup.php?id="+id, "Langas", "resizable=yes,scrollbars=yes,status=no,menubar=yes,titlebar,height="+y+",width="+x+"");
}

/////////////////////////////
// redirects to specified address
/////////////////////////////
function GoTo(loc) { window.location.href = loc; }

////////////////////////////
// confirms action
////////////////////////////
function ConfirmAction(actionDescription,url)
  {  if(window.confirm("Are you sure you want "+actionDescription)){GoTo(""+url+"");}    }
  
//////////////////////////////////////////////////////////////////////////////////
// on confirm executes another ajax action and outputs (if is set) success message
//////////////////////////////////////////////////////////////////////////////////
function ConfirmAjax(text,action,param,success_message)
{  
  conf = confirm(text); 
  if(conf == true) { eval(action+"('"+param+"')"); }
  if (typeof success_message != 'undefined')
  { 
    document.getElementById('messages').innerHTML = 
      "<div class='success'>"+success_message+"</div>"; 
  }  
}

////////////////////////////
// gets user input via prompt
////////////////////////////
function EnterText(promptTitle,confirmText,url,defaultText)
{
  var answer = prompt (promptTitle,defaultText)
  if (answer != null) {ConfirmAction(confirmText+"\n\n "+answer,url+answer);}
}

/////////////////////////////////////////////
// checks whether email address is properly formated
//////////////////////////////////////////////
function CheckEmail(email)
{
  var testresults
  var str=email
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str))
  testresults=true
  else{
  testresults=false
  }
  return (testresults)
}

/////////////////
//change the opacity for different browsers 
/////////////////
function ChangeOpac(object_id, opacity) {
    var object = document.getElementById(object_id).style; 
    object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
}



////////////////// for items
function save_item_updates(item_id)
{
  // preparing vars
  var name = SafeInput(document.getElementById('name_'+item_id).value);
  var url = SafeInput(document.getElementById('url_'+item_id).value);
  var preloader = '<img src="http://i.digitalpipelines.com/img/loading-small.gif" height="16">';  
  
  // preparing query
  var query = 'misc-menu_.php?&update='+item_id+'&name='+name+'&url='+url;


  // restoring input fields to normal style
  document.getElementById("name_"+item_id).style.border = "1px solid #7B9EBD";
  document.getElementById("name_"+item_id).style.padding = "1px";
  document.getElementById("url_"+item_id).style.border = "1px solid #7B9EBD";
  document.getElementById("url_"+item_id).style.padding = "1px";
  
  // passing via AJAX
  DataTrade(query,'status_'+item_id,preloader);
  
  // different AJAX usage depending item level
  if (document.getElementById("url_"+item_id).style.display == "none") {
    DataTrade(query,'','');
  } else { 
    DataTrade(query,'status_'+item_id,preloader);
  }
}


