var http = false;

if(navigator.appName == "Microsoft Internet Explorer") {
  http = new ActiveXObject("Microsoft.XMLHTTP");
} else {
  http = new XMLHttpRequest();
}


function ajax_loadContent(divId,url,callbackOnComplete){
  $("#"+divId).load(url);
    return;
}

function ajax_postContent(divId,url,callbackOnComplete){
  $.post(url, function(data){
  $("#"+divId).html(data);
  });
  return;
}


function removeLoadingGraphic(col) {

  var active = 'active_' + col;

  if(document.layers) {
    document.layers[active].display = 'none';
  } else if(document.all) {
    document.all[active].style.display = 'none';
  } else if(document.getElementById) {
    document.getElementById(active).style.display = 'none';
  }
}



function toggleHidden(pid) {

  var shortid = 'short_' + pid;
  var longid  = 'long_' + pid;

  if( document.getElementById(shortid).style.display != ''){
    document.getElementById(shortid).style.display = '';
    document.getElementById(longid).style.display = 'none';
  } else {
    document.getElementById(longid).style.display = '';
    document.getElementById(shortid).style.display = 'none';
  }
}


function toggleId(id) {

  if( document.getElementById(id).style.display != ''){
    document.getElementById(id).style.display = '';
  } else {
    document.getElementById(id).style.display = '';
    document.getElementById(id).style.display = 'none';
  }
}





function showLoadingGraphic(pid) {

  var execute = 'execute_' + pid;
  var loading = 'loading_' + pid;

  if(document.layers) {
    document.layers[execute].display = 'none';
    document.layers[loading].display = '';
  } else if(document.all) {
    document.all[execute].style.display = 'none';
    document.all[loading].style.display = '';
  } else if(document.getElementById) {
    document.getElementById(execute).style.display = 'none';
    document.getElementById(loading).style.display = '';
  }
}


function removeLoadingGraphic(pid) {

  var uploaded = 'uploaded_' + pid;
  var loading  = 'loading_' + pid;
  var error    = 'importerror_' + pid;

  document.getElementById(uploaded).style.display = '';
  document.getElementById(loading).style.display = 'none';
  document.getElementById(error).style.display = 'none';
}

// call the toggle record
function toggleRecord(table,pid,field,val) {

  var inactive = 'inactive_' + pid;
  var active   = 'active_' + pid;
  var loading  = 'toggleloading_' + pid;

  document.getElementById(active).style.display = 'none';
  document.getElementById(inactive).style.display = 'none';
  document.getElementById(loading).style.display = '';
  http.abort();
  http.open("GET", "index.php?action=toggle&field=" + field + "&val=" + val + "&mod=ajax&table=" + table + "&id=" + pid, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      if(http.responseText == 'ok') {
        if(val == 'N'){
          document.getElementById(inactive).style.display = '';
          document.getElementById(active).style.display = 'none';
          document.getElementById(loading).style.display = 'none';
        } else {
          document.getElementById(active).style.display = '';
          document.getElementById(inactive).style.display = 'none';
          document.getElementById(loading).style.display = 'none';
        }
      } else {
        document.getElementById('ajax_error').innerHTML = http.responseText;
        document.getElementById('ajax_error').style.display = '';
      }
    }
  }
  http.send(null);
}


function savePositions(table,positions){

        alert('ajax starting');
  http.abort();
  http.open("GET", "index.php?action=savepositions&table=" + table + "&positions=" + positions, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      if(http.responseText == 'ok') {
        alert('ok');
      } else {
        alert('not ok');
      }
    } else {
        alert('failed');
    }
  }
  http.send(null);
}


// call the reset importError pages
function resetRecord(pid,table) {

  var loading     = 'resetloading_' + pid;
  var recordRow   = 'recordRow' + pid;
  var oldcolor    = document.getElementById(loading).style.background;

  document.getElementById(loading).style.background = '#FF0000';
  http.abort();
  http.open("GET", "index.php?action=ajaxreset&table="+ table +"&id=" + pid, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      //alert(http.responseText);
      if(http.responseText == 'ok') {
        document.getElementById(recordRow).style.display = 'none';
      } else {
      }
    }
  }
  http.send(null);
}

// call the publish/unpublish toggle page
function retrieveListContent(page) {

alert('here');
  document.getElementById('listContent').innerHtml = "<img src='img/loadingContent.png' />test";
alert('here');
return false;
  http.abort();
  http.open("GET", page, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      //alert(http.responseText);
     document.getElementById('listContent').innerHtml = http.responseText;
    } else {
     document.getElementById('listContent').innerHtml = 'error';
    }
  }
  http.send(null);
}



// call the publish/unpublish toggle page
function deleteRecord(pid,table) {

  var loading     = 'deleteloading_' + pid;
  var recordRow   = 'recordRow' + pid;
  var deleteSpan  = 'delete_' + pid;
  var oldcolor    = document.getElementById(recordRow).style.background;

  document.getElementById(loading).style.display = '';
  document.getElementById(deleteSpan).style.display = 'none';
  http.abort();
  http.open("GET", "index.php?action=ajaxdelete&table="+ table +"&id=" + pid, true);
  http.onreadystatechange=function() {
    if(http.readyState == 4) {
      //alert(http.responseText);
      if(http.responseText == 'ok') {
        document.getElementById(recordRow).style.display = 'none';
        document.getElementById('ajax_error').style.display = 'none';
      } else {
        document.getElementById('ajax_error').innerHTML = http.responseText;
        document.getElementById('ajax_error').style.display = '';
      }
    }
  }
  http.send(null);
}


// call the publish/unpublish toggle page
function showUrl(name) {

  var span = 'uploadedFile' + name;
  document.getElementById(span).style.display = '';
  document.getElementById(span).innerHTML = document.getElementById(name).value;
}


var currentlyActiveInputRef = false;
var currentlyActiveInputClassName = false;

function highlightActiveInput() {
  if(currentlyActiveInputRef) {
    currentlyActiveInputRef.className = currentlyActiveInputClassName;
  }
  currentlyActiveInputClassName = this.className;
  this.className = 'inputHighlighted';
  currentlyActiveInputRef = this;
}

function highlightActiveInputTextarea() {
  if(currentlyActiveInputRef) {
    currentlyActiveInputRef.className = currentlyActiveInputClassName;
  }
  currentlyActiveInputClassName = this.className;
  this.className = 'inputHighlightedTextarea';
  currentlyActiveInputRef = this;
}


function blurActiveInput() {
  this.className = currentlyActiveInputClassName;
}


function initInputHighlightScript() {

	var tags = ['INPUT','TEXTAREA'];
	
	for(tagCounter=0;tagCounter<tags.length;tagCounter++){
		var inputs = document.getElementsByTagName(tags[tagCounter]);
		for(var no=0;no<inputs.length;no++){
			if(inputs[no].className && inputs[no].className=='doNotHighlightThisInput')continue;
			
			if(inputs[no].tagName.toLowerCase()=='textarea') {
				inputs[no].onfocus = highlightActiveInputTextarea;
				inputs[no].onblur = blurActiveInput;
			}
      if(inputs[no].tagName.toLowerCase()=='input' && inputs[no].type.toLowerCase()=='text'){
				inputs[no].onfocus = highlightActiveInput;
				inputs[no].onblur = blurActiveInput;
			}
		}
	}
}



function uploadImage(){
	document.getElementById('uploadForm').style.display = 'none';
	document.getElementById('loadingImage').style.display = '';
	return true;
}

function stopUpload(content){
	var result = '';
	  document.getElementById('imageFile').value = '';
	  document.getElementById('imageTitle').value = '';
	  document.getElementById('imageDesc').value = '';
	  document.getElementById('uploadForm').style.display = '';
	  document.getElementById('loadingImage').style.display = 'none';
	  var newDiv = document.createElement("div");
	  document.getElementById("thumbnails").appendChild(newDiv);
    newDiv.innerHTML = content;
    initGallery();
	return true;
}


function tellAFriend(){
  window.open('tell_a_friend.php', 'tellafriend_script',
      'scrollbars=1,statusbar=1,resizable=1,width=400,height=410');
}


