//
// send any client errors to server...
//
//
var errorCnt = 0;
window.onerror = function _myOnError(jsError, url, lineNumber) {
    try {
        if (errorCnt++ > 5)
            return true;
        var theERR = new Error;
        theERR.name = 'Unhandled error';
        theERR.message = jsError;
        return myOnError(theERR, url, lineNumber);
    }
    catch (err) {
        alert('_myOnError.catch(' + err + ')');
        return false
    }
}
//
//
var req;
var xmlSoapData;
// set by hosting page.,.
var imgErrorCtlName = '';
var wsUrl;
//
function logException() {
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch (e) {
            req = false;
        }
    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                req = false;
            }
        }
    }
    if (req) {
        var soapEnvelope = '<?xml version="1.0" encoding="utf-8"?>' +
                         '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ' +
                         'xmlns:xsd="http://www.w3.org/2001/XMLSchema" ' +
                         'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' +
                         '<soap:Body>' +
                         '<logJSException xmlns="http://healthsaas.net/gcFx.ws.errorLog">' + xmlSoapData +
                         '</logJSException>' +
                         '</soap:Body>' +
                         '</soap:Envelope>';
        req.open("POST", wsUrl, true);
        req.onreadystatechange = logExecptionComplete;
        req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
        req.setRequestHeader("Content-Length", soapEnvelope.length);
        req.setRequestHeader("SOAPAction", "gcFx.ws.errorLog/logJSException");
        req.send(soapEnvelope);
        return true;
    }
    alert(xmlSoapData + '\n\n' + url);
    return false;
}
function logExecptionComplete() {
    if (req.readyState != 4)
        return;
    if (req.responseText.indexOf('<logJSExceptionResult>false</logJSExceptionResult>', 0) > 0)
        alert(xmlSoapData + '\n\n' + wsUrl);
    if (imgErrorCtlName != '')
        $get(imgErrorCtlName).style.display = 'none';
    return true;
}
function myOnError(jsError, url, lineNumber) {
    try {
        if (errorCnt++ > 5)
            return true;
        if (!url)
            url = window.location;
        if (!lineNumber)
            lineNumber = 'Unknown';
        xmlSoapData = "<err_Type>" + jsError.name + "</err_Type>" +
                               "<err_Message>" + jsError.message + "</err_Message>" +
                               "<err_LineNumber>" + lineNumber + "</err_LineNumber>" +
                               "<err_Page>" + url + "</err_Page>";
        $get(imgErrorCtlName).style.display = '';
        return logException();
    }
    catch (err) {
        alert('myOnError.catch(' + err + ')');
        return false
    }
}
