﻿function onFocusEdit(edit)
{
    var empty = false;
    
    if (!edit.getAttribute("guideValue"))
    {
        edit.setAttribute("guideValue", edit.value);
        empty = true;
    }
    else if (edit.value == edit.getAttribute("guideValue"))
    {
        empty = true;
    }
    
    if (empty)
    {
        edit.value = "";
        edit.className = edit.className.replace(/emptyEdit/, "");
        edit.className = edit.className + " " + "filledEdit";
    }
}
function onBlurEdit(edit)
{
    edit.value = trim(edit.value);
    
    if (edit.value == "")
    {
        edit.value = edit.getAttribute("guideValue");
        edit.className = edit.className.replace(/filledEdit/, "");
        edit.className = edit.className + " " + "emptyEdit";                
    }
}
function hasEditGotContent(edit)
{
    return !edit.getAttribute("guideValue") || edit.value == edit.getAttribute("guideValue");
}
function onSubmitCallbackInfo()
{
//    var editName = document.getElementById("editName");
//    var editCompany = document.getElementById("editCompany");
//    var editTelephone = document.getElementById("editTelephone");

//    if (hasEditGotContent(editName))
//    {
//        alert("You must enter a name");
//        return;
//    }
//    
//    if (hasEditGotContent(editCompany))
//    {
//        alert("You must enter a company or organization");
//        return;
//    }
//    
//    if (hasEditGotContent(editTelephone))
//    {
//        alert("You must enter a telephone number or suitable alternative contact details");
//        return;
//    }

    var editRequest = document.getElementById("editRequest");
    if (hasEditGotContent(editRequest))
    {
        alert("You must enter your contact details and request");
        return;
    }
    
    var wsParams = new Object();
//    wsParams["name"] = editName.value;
//    wsParams["company"] = editCompany.value;
//    wsParams["telephone"] = editTelephone.value;
    wsParams["request"] = editRequest.value;
    
    _WsCallWebService(
        "RegisterForWhiteLabelSalesCallback",
        wsParams,
        function (response, params) {
            if (response.indexOf("EXECUTED_OK") != -1)
            {
                var oFeedbackForm = document.getElementById("callbackFormElems");
                var oThankYouMessage = document.getElementById("callbackSubmittedMsg");
                
                oFeedbackForm.style.display = "none";
                oThankYouMessage.style.display = "block";
            }
            else
            {
                alert("Problem\n" + response);
            }
        },
        false);
} 
function trim(s)
{
    s = s.replace(/(^\s*)|(\s*$)/gi,"");
    s = s.replace(/[ ]{2,}/gi," ");
    s = s.replace(/\n /,"\n");
    return s;
}
function _WsCallWebService(wsMethodName, wsParams, onHttpOkCallback, async, onHttpErrorCallback, onHttpStateChangeCallback)
{
    //var wsHref = window.location.protocol + "//" + window.location.host + "/SalesFeedbackService.asmx/" + wsMethodName;
    var wsHref = "SalesFeedbackService.asmx/" + wsMethodName;

    var postData = "";
    for (var paramName in wsParams)
    {
        postData += paramName + "=" + encodeURIComponent( wsParams[paramName] ) + "&";
    }

    var request =  _WsCreateXmlHttpRequest();
    try
    {
        request.open("POST", wsHref, async);
    }
    catch (e) {
        alert("Error opening AJAX request object for communicating with the sales feedback web service: " + e.message);
        return;
    }
    
    request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    request.setRequestHeader("Content-length", postData.length);
    
    var onHttpErrorCallback = function(httpStatus)
    {
        if (httpStatus == null)
        {
            alert("Remote function call failed because it was not possible to connect OR there was a problem with the connection");
        }
        else if (httpStatus == 401)
        {
            alert("The remote function call failed (with code " + httpStatus + ") because your browser could not log you in to the service");
        }
        else if (httpStatus == 403)
        {
            alert("The remote function call failed (with code " + httpStatus + ") because the server has forbidden your browser from using the resource");
        }
        else if (httpStatus == 404)
        {
            alert("The remote function call has failed (with code " + httpStatus + ") because the server resource was not found at the expected location");
        }
        else if (httpStatus == 500)
        {
            alert("The remote function call has failed (with code " + httpStatus + ") because an internal error occurred on the service");
        }
        else if (httpStatus == 12029) // IE on Windows
        {
            alert("The remote function call failed (with code " + httpStatus + ") because it was not possible to connect to the service");
        }
        else if (httpStatus == 12030) // IE on Windows
        {
            alert("The remote function call failed (with code " + httpStatus + ") because the connection to the server was broken");
        }
        else if (httpStatus == 12031) // IE on Windows
        {
            alert("The remote function call failed (with code " + httpStatus + ") because the server reset the connection.\n\nNote: This might be because an invalid Web service address was used.\nSee your administrator.");
        }
        else if (httpStatus == 12163) // IE on Windows
        {
            alert("The remote function call failed (with code " + httpStatus + ") because the Internet connection has been lost");
        }
        else {
            alert("The HTTP-based remote function called failed with status: " + httpStatus);
        }        
    }
    
    var fCompletionHanlder = function()
    {
        if (onHttpStateChangeCallback)
        {
            onHttpStateChangeCallback(paramsObj);
        }
        
        if (request.readyState == 4)
        {    
            var httpStatus = null;
            try {
                httpStatus = request.status;
            } catch (e) {
                ;
            }
            
            if (httpStatus && httpStatus == 200) 
            {
                if (onHttpOkCallback)
                {
                    onHttpOkCallback(request.responseText, wsParams);
                }  
            }
            else
            {
                onHttpErrorCallback(httpStatus);
            }
        }
    };
    
    if (async)
    {
        request.onreadystatechange = fCompletionHanlder;
        request.send(postData);
    }
    else
    {
        request.send(postData);
        fCompletionHanlder();
    }
}        
function _WsCreateXmlHttpRequest()
{
    var xmlHttp = null;
    if (window.XMLHttpRequest)
    {
        // If IE7, Mozilla, Safari, and so on: Use native object.
        xmlHttp = new XMLHttpRequest();
    }
    else
    {
        if (window.ActiveXObject)
        {
            // ...otherwise, use the ActiveX control for IE5.x and IE6.
            xmlHttp = new ActiveXObject('MSXML2.XMLHTTP.3.0');
        }
    }
    
    if (!xmlHttp)
    {
        alert("Unrecoverable problem: The use of AJAX callbacks has been prohibited in this Web browser...");
    }
    
    return xmlHttp;
}            