﻿// requires com.iskitz.ajile v0.7.8 or later
// All functions above namespace declaration left here for legacy purposes - direct calls are declared as deprecated

Namespace("com.spamfighter.translate.common");

// Globals
var waitDiv; // DIV
var waitText; // SPAN
var frameWidth; // window width
var frameHeight; // window height
var xmlpush; // xmlhttp object
var errorElement; // container element for showing error messages without pop-ups

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try {
		xmlpush = new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
	try {
		xmlpush = new ActiveXObject("Microsoft.XMLHTTP")
	} catch (E) {
		xmlpush = false
	}
}
@else
	xmlpush=false
@end @*/
if (!xmlpush && typeof XMLHttpRequest != 'undefined') {
	try {
		xmlpush = new XMLHttpRequest();
	} catch (e) {
		xmlpush = false;
	}
}

function calcSize() {
	// find screen size
    if (self.innerWidth) {
        frameWidth = self.innerWidth;
        frameHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth) {
        frameWidth = document.documentElement.clientWidth;
        frameHeight = document.documentElement.clientHeight;
    }
    else if (document.body) {
        frameWidth = document.body.clientWidth;
        frameHeight = document.body.clientHeight;
    }
    // align at top middle of viewport
	frameWidth = frameWidth/2 - 60;
	frameHeight = 5;
	// add scroll position
	if (document.documentElement) {
		frameWidth += document.documentElement.scrollLeft;
		frameHeight += document.documentElement.scrollTop;
	}
	else if (document.body.scrollTop && document.body.scrollLeft) {
		frameWidth += document.body.scrollLeft;
		frameHeight += document.body.scrollTop;
	}
	else if (window.pageYOffset && window.pageXOffset) {
		frameWidth += window.pageXOffset;
		frameHeight += window.pageYOffset;
	}
}

function toggleWait(show, text) {
    if (!waitDiv) {
        if (document.body.textContent)
            return;
        waitDiv = document.createElement("<div style=\"z-index:500;cursor:default;position:absolute;width:120px;height:120px;padding:0\" />");
        waitDiv.appendChild(document.createElement("<img title=\"\" alt=\"White background with rounded corners\" src=\"images/waitbg.gif\" width=\"120\" height=\"120\" />"));
        waitDiv.appendChild(document.createElement("<img unselectable=\"on\" src=\"images/wait.gif\" width=\"32\" height=\"32\" alt=\"Animated circle\" title=\"Please wait...\" style=\"position:absolute;top:28px;left:44px\" />"));
        waitText = document.createElement("<span unselectable=\"on\" style=\"font-family:Tahoma;font-size:10pt;width:100px;height:30px;position:absolute;left:10px;top:80px;text-align:center\" />");
        waitDiv.appendChild(waitText);
        document.body.appendChild(waitDiv);
    }
    if (show) {
        calcSize();
        waitText.innerText = text ? text : "";
        waitDiv.style.display = "block"; // show before positioning due to a browser bug
        waitDiv.style.left = frameWidth;
        waitDiv.style.top = frameHeight;
    }
    else
		waitDiv.style.display = "none";
}

function push(form, target) {
	if (xmlpush) {
		xmlpush.open("POST", target, true);
		xmlpush.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xmlpush.onreadystatechange = function() {
			if (xmlpush.readyState == 4) {
				eval(xmlpush.responseText);
				toggleWait(false, null);
            }
		}
		toggleWait(true, "Please wait...");
		xmlpush.send("ajax=1&" + form);
	}
	else
		upgradeBrowser();
}

function upgradeBrowser() {
	if (confirm('Sorry! Your browser doesn\'t support the minimum requirements for this page. Please install the newest version of Mozilla Firefox or Microsoft Internet Explorer.\n\nWould you like to go to the download page now?'))
	    location.href = "http://www.getfirefox.com/";
}

function keepAlive() {
	window.setTimeout("keepAlive();", 120000);
	AjaxPage.KeepAlive();
}

clientError = function(msg) {
    return com.spamfighter.translate.common.clientError(msg);
};

com.spamfighter.translate.common.clientError = function(msg) {
    if (msg == undefined || msg == null || typeof(msg) != "string" || msg.length == 0) {
        alert("A client-side error occured, but no error message was available.\nPlease report this to the support.");
        return;
    }
    if (errorElement != undefined && errorElement != null && typeof(errorElement) == "object" && errorElement.style && typeof(errorElement.style) == "object" && errorElement.style.display && typeof(errorElement.style.display) == "string" && (errorElement.textContent || errorElement.innerText)) {
        errorElement.style.display = "block";
        if (errorElement.textContent)
            errorElement.textContent = msg;
        else
            errorElement.innerText = msg;
    }
    else
        alert("Error:\n" + msg);
};

com.spamfighter.translate.common.hasIE = function() {
  var ua = navigator.userAgent.toLowerCase();
  return ((ua.indexOf('msie') != -1) && (ua.indexOf('opera') == -1) && 
          (ua.indexOf('webtv') == -1) &&
          (location.href.indexOf('seenIEPage') == -1));
};

com.spamfighter.translate.common.stopIE = function() {
    if (com.spamfighter.translate.common.hasIE()) {
        if (confirm("Your browser does not seem to be compatible with this page.\n\nWould you like to download a compatible version?"))
            location.href = "http://www.getfirefox.com/";
        return false;
    }
};
