/* CORE AJAX FUNCTIONS */
var request = false;
var requested_page = null;

function ajaxSend(url,data,form_type) {
  if (form_type.length == 0) {
    form_type = "GET";
  }
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request) {
    //Error
  } else {
    //ajax commands available.
    if (form_type == "POST") {
      request.open(form_type, url, true);
      request.onreadystatechange = ajaxReceive;
      request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      request.send(data+"&ajax_call=true");
    } else {
      request.open(form_type, url+"?"+data+"&ajax_call=true", true);
      request.onreadystatechange = ajaxReceive;
      request.send(null);
    }
    requested_page = data;
  }
}
function ajaxReceive() {
  if (request.readyState == 4) {
    if (request.status == 200) { //everything went smoothly
      var response = request.responseText;
      receivePage(response);
    }
    // else an error accessing the URL.
  }
  // else still processing, so do not execute routine
}
/* WEB 2.0 CART - SPECIFIC FUNCTIONS */
//Ajax functions
function fetchPage(which, subdirectory) {
  if (subdirectory.length > 0) {
    subdirectory = subdirectory+'/';
  } else {
    subdirectory = '';
  }
  ajaxSend(subdirectory+'index.php','page='+which,'GET');
  document.getElementById("page_content").innerHTML = '<img src="images/ajax-loading.gif" alt="Loading Page" id="ajax_gif"/>';
}

function receivePage(returnObject) {
  content = eval('('+returnObject+')');
  if (content.action == "update node") {
    //Update Content
    document.getElementById(content.targetNode).innerHTML = content.newContent;
    //Update Class
    if (content.className) {
      document.getElementById(content.targetNode).className = content.className;
    }
  }
  if (content.action == "call function" || content.callFunction == "true") {
    //Used to call custom functions by add on scripts
    window[content.functionName](content);
  }
  initializePage();
}

function editField(which) {
  which.readOnly = false;
  which.className = "";
}
function saveField(page,which,subdirectory) {
  if (subdirectory.length > 0) {
    subdirectory = subdirectory+'/';
  } else {
    subdirectory = '';
  }
  if (which.tagName == "INPUT") {
    if (which.readOnly == false) {
      which.className = "inline_edit";
      which.readOnly = true;
      newValue = which.value;
    }
  } else if (which.tagName == "SELECT") {
    newValue = which.options[which.options.selectedIndex].value;
  }
  document.getElementById("ajax_message").style.visibility = "visible";
  ajaxSend(subdirectory+'index.php?page='+page,"field_update=true&"+which.id+"="+newValue,'POST');
}
function saveForm(formId) {
  return true;
  /**
   * NOT IMPLEMENTED
  postString = ""; prefix = "";

  inputs = document.getElementById(formId).getElementsByTagName('INPUT');
  for (i=0;i<inputs.length;i++) {
    if (inputs[i].name.length > 0) {
      if (inputs[i].type != "checkbox" || (inputs[i].type == "checkbox" && inputs[i].checked))
      postString += prefix+inputs[i].name+"="+escape(inputs[i].value);
      prefix = "&";
    }
  }
  selects = document.getElementById(formId).getElementsByTagName('SELECT');
  for (i=0;i<selects.length;i++) {
    if (selects[i].name.length > 0) {
      postString += prefix+selects[i].name+"="+escape(selects[i].value);
      prefix = "&";
    }
  }
  textareas = document.getElementById(formId).getElementsByTagName('TEXTAREA');
  for (i=0;i<textareas.length;i++) {
    if (textareas[i].name.length > 0) {
      postString += prefix+textareas[i].name+"="+escape(textareas[i].value);
      prefix = "&";
    }
  }

  alert(document.getElementById(formId).action+"\n\n\n"+postString);
  ajaxSend(document.getElementById(formId).action,postString,'POST');
  */
}

function hideAjaxMessage(rValue) {
  setTimeout("setVisibilityHidden('ajax_message');",3000);
}
function setVisibilityHidden(box_id) {
  document.getElementById(box_id).style.visibility = "hidden";
}

/* MOUSE TRACKING FUNCTIONS */
var mousex, mousey;

function getMouseXY(e)  {
  if (!e) e = window.event;
  if (e) {
    if (e.pageX || e.pageY) {
      mousex = e.pageX;
      mousey = e.pageY;
    } else if (e.clientX || e.clientY) {
      if (document.documentElement.scrollTop) {
        mousex = e.clientX + document.documentElement.scrollLeft;
        mousey = e.clientY + document.documentElement.scrollTop;
      } else {
        mousex = e.clientX ;
        if (document.body) {
          mousex += document.body.scrollLeft;
        }

        mousey = e.clientY;
        if (document.body) {
          mousey += document.body.scrollTop;
        }
      }
    }
  }
}

/* FUNCTIONS TO CALL ONLOAD */
/* EX: onLoadCalls.push(new onLoadCall('function_name','parameter passed to function')); */

var onLoadCalls = new Array();

//Default onLoad functions
onLoadCalls.push(new onLoadCall('ajaxMessage',''));
//onLoadCalls.push(new onLoadCall('ajaxEnable',''));
onLoadCalls.push(new onLoadCall('w2cCoreOnLoad',''));

function initializePage() {
  //Called for body onload
  if (onLoadCalls.length > 0) {
    for(i=0;i<onLoadCalls.length;i++) {
      window[onLoadCalls[i].functionName](onLoadCalls[i].content);
    }
  }
}

function onLoadCall(functionName, functionParams) {
  this.functionName = functionName;
  this.content = functionParams;
}
/* FUNCTIONS TO CALL ONRESIZE */
/* EX: onResizeCalls.push(new onResizeCall('function_name','parameter passed to function')); */

var onResizeCalls = new Array();

function pageResized() {
  //Called for body onResize
  if (onResizeCalls.length > 0) {
    for(i=0;i<onResizeCalls.length;i++) {
      window[onResizeCalls[i].functionName](onResizeCalls[i].content);
    }
  }
}

function onResizeCall(functionName, functionParams) {
  this.functionName = functionName;
  this.content = functionParams;
}

function w2cCoreOnLoad() {
  if (document.getElementsByTagName('BODY')[0].addEventListener) {
    document.getElementsByTagName('BODY')[0].addEventListener('onMouseMove', getMouseXY, false);
  } else if (document.getElementsByTagName('BODY')[0].attachEvent) {
    document.getElementsByTagName('BODY')[0].attachEvent('onmousemove', getMouseXY);
  }
}
function ajaxMessage() {
  if (!document.getElementById("ajax_message")) {
    //In case programmer didn't add tag to body, add it automatically
    ajaxMessageDiv = document.createElement("DIV");
    ajaxMessageDiv.id = "ajax_message";
    document.body.appendChild(ajaxMessageDiv);
  }
}
var ajaxEnabled = true;
function ajaxEnable() {
  if (ajaxEnabled) {
    page_links = document.getElementsByTagName('A');
    if (page_links.length > 0) {
      for (i=0;i<page_links.length;i++) {
        if (page_links[i].className.search(/no\_ajax/) < 0) {
          page_links[i].onclick = function () {page_id = this.href.split("index.php?page="); fetchPage(page_id[1],'');return false;};
        }
      }
    }
  }
}
//Show popup functions
function showPopup(text) {
  document.getElementById('popup_message').style.display = "block";
  document.getElementById('popup_message').style.top = (mousey+10)+"px";
  document.getElementById('popup_message').style.left = (mousex+10)+"px";

  document.getElementById('popup_message').innerHTML = text;
  setTimeout('hidePopup()',7000);
}

function hidePopup() {
  document.getElementById('popup_message').style.display = "none";
}
function dhtmlConfirm(message) {
  return confirm(message);
}

//Function to set first field to be focused
first_field_focus = false;
function loadFocusField(field_name) {
  if (!first_field_focus && document.getElementById(field_name)) {
    document.getElementById(field_name).focus();
    first_field_focus = true;
  }
}
//Visible area height & width functions
function getWidth() {
  if (window.innerWidth) {
    return window.innerWidth;
  } else if (document.documentElement && document.documentElement.clientWidth != 0) {
    return document.documentElement.clientWidth;
  } else if (document.body) {
    return document.body.clientWidth;
  }
  return 0;
}

function getHeight() {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight != 0) {
    return document.documentElement.clientHeight;
  } else if (document.body) {
    return document.body.clientHeight;
  }
  return 0;
}