function updateEmailSaturation(val) {
   executeAction("/ajax.html?action=updateemailsaturation&val="+val, updateEmailSaturationAbort);
}
function updateEmailSaturationAbort() {
   ajaxabort("Error: Email saturation not updated.  Please try again.");
}

function updateEmailNotifications(notify) {
   executeAction("/ajax.html?action=updateemailnotifications&notify="+notify, updateEmailNotificationsAbort);
}

function updateEmailNotificationsAbort() {
   ajaxabort("Error: Email notifications change not saved.  Please try again.");
}

function updateAll(typeid, notify, ids) {
   executeAction("/ajax.html?action=updateallnotifications&typeid="+typeid+"&notify="+notify+ids, updateAllAbort);
}

function updateAllAbort() {
   ajaxabort("Error: Notification changes not saved.  Please try again.");
}

function updateNotification(typeid, objid, notification) {
   executeAction("/ajax.html?action=updatenotification&typeid="+typeid+"&objid="+objid+"&notify="+notification, updateNotificationAbort);
}

function updateNotificationAbort() {
   ajaxabort("Error: Notification change not saved.  Please try again.");
}

function updateVisibility(memid, vis) {
   executeAction("/ajax.html?action=updatevisibility&memoryid="+memid+"&visibility="+vis, updateVisibilityAbort);
}

function updateVisibilityAbort() {
   ajaxabort("Error: Visibility not saved.  Please try again.");
}

function updateText(memid, photoname, text) {
  executePostAction("/ajax.html","action=phototext&memoryid="+memid+"&filename="+photoname+"&text="+text, updateTextAbort);
}

function updateTextAbort() {
   ajaxabort("Error: Text not saved.  Please try again.");
}

function updateImgLoc(memid, img1, img2) {
   executePostAction("/ajax.html","action=swapimgs&memoryid="+memid+"&img1="+img1+"&img2="+img2, updateImgLocAbort);
}

function updateImgLocAbort() {
   ajaxabort("Error: Img Location not saved.  Please refresh your webpage.");
}

function rotateImageClockwise(memid, img) {
   executePostAction("/ajax.html","action=rotateclock&memoryid="+memid+"&img="+img, rotateAbort);
}
function rotateImageCounterClockwise(memid, img) {
    executePostAction("/ajax.html","action=rotatecounter&memoryid="+memid+"&img="+img, rotateAbort);
}

function memoryicon(memid, img) {
    executePostAction("/ajax.html","action=memoryicon&memoryid="+memid+"&filename="+img, memoryiconAbort);
}

function memoryiconAbort() {
   ajaxabort("Error: Memory Icon generation failed. Please refresh your webpage.");
}

function rotateAbort() {
   ajaxabort("Error: Rotate failed.  Please refresh your webpage.");
}

function hideImage(memid, img) {
   executePostAction("/ajax.html","action=photohide&memoryid="+memid+"&img="+img, hideImageAbort);
}

function hideImageAbort() {
   ajaxabort("Error: Failed to hide image.  Please refresh your webpage.");
}

function refreshScreen(id) {
   document.location = "/memorycontent.html?memoryid="+id+"&r="+Math.random()*Date.parse(new Date(), true);
}


function reloadImage(img, src) {
   var imgobj = document.getElementById(img);
   imgobj.src = src;
}

var _xmlHttp;
var _timeoutId;

function ajaxabort(errormsg) {
   if (ajaxCallInProgress()) {
      _xmlHttp.abort();
      alert(errormsg);
   }
}

function ajaxCallInProgress() {
   if (_xmlHttp.readyState >=1 && _xmlHttp.readyState <= 3) return true;
   return false;
}

function response(val) {
   if (val.indexOf("Error") >= 0) {
      alert(val);
   } else if (val.length == 0) {
      alert("Error: Visibility not saved.  Please try again.");
   }
}

function updateVisibility(memid, vis) {
   // executeAction("/ajax.html?action=updatevisibility&memoryid="+memid+"&visibility="+vis, updateVisibilityAbort);
   executePostAction("/ajax.html","action=updatevisibility&memoryid="+memid+"&visibility="+vis, updateVisibilityAbort);

}

function executePostAction(url, postdata, abortfunction) {
   if (_xmlHttp && _xmlHttp.readyState  != 0) {
      _xmlHttp.abort();
   }
   _xmlHttp = initAjax();
   if (_xmlHttp) {
      _timeoutId = window.setTimeout(abortfunction, 20000);
      _xmlHttp.open("POST", url);
      _xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
      _xmlHttp.onreadystatechange = function() {
        if (_xmlHttp.readyState == 4 && _xmlHttp.responseText) {
          window.clearTimeout(_timeoutId);
          eval(_xmlHttp.responseText);
        }
      }
      _xmlHttp.send(postdata);
   }
}

function executeAction(url, abortfunction) {
   if (_xmlHttp && _xmlHttp.readyState  != 0) {
      _xmlHttp.abort();
   }
   _xmlHttp = initAjax();
   if (_xmlHttp) {
      _timeoutId = window.setTimeout(abortfunction, 20000);
      _xmlHttp.open("GET", url + "&r="+Math.random()*Date.parse(new Date(), true));
      _xmlHttp.onreadystatechange = function() {
        if (_xmlHttp.readyState == 4 && _xmlHttp.responseText) {
          window.clearTimeout(_timeoutId);
          eval(_xmlHttp.responseText);
        }
      }
      _xmlHttp.send(null);
   }
}

function initAjax() {
        var xmlhttp = null;
	try {
            // Mozilla / Safari
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            // IE
            var MSXML_XMLHTTP_PROGIDS = new Array(
                'MSXML2.XMLHTTP.5.0',
                'MSXML2.XMLHTTP.4.0',
                'MSXML2.XMLHTTP.3.0',
                'MSXML2.XMLHTTP',
                'Microsoft.XMLHTTP'
            );
            var success = false;
            for (var i=0;i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
                try {
                    xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i]);
                    success = true;
                } catch (e) {}
            }
        }
        return xmlhttp;
}

