﻿var wfRequestPage;

function loadWidgetForm(wfRequest, popupWidth, popupTopOffset) {
    wfRequestPage = wfRequest;
    
    stageWidget(popupWidth, popupTopOffset);    

    var xmlHttp;
    try {  // Firefox, Opera 8.0+, Safari 
        xmlHttp = new XMLHttpRequest(); 
    }
    catch (e) {  // Internet Explorer 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;   
            }
        } 
    }
    
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4)
            widgetFormLoaded(xmlHttp.responseText);    
    }
 
    xmlHttp.open("post", wfRequestPage, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send(""); 
}

function stageWidget(popupWidth, popupTopOffset) {
    var element = document.createElement("div");
    element.innerHTML = "<center><div style=\"width: " + popupWidth + "px; height: 240px; background-color: white; padding-top: 135px; color: black; font-size: 12pt\"><img src=\"WidgetForms/loading.gif\" alt=\"Loading...\" /></div></center>";
    element.id = "wf_popup";
    element.style.width = popupWidth + "px";
    element.style.left = ((getPageWidthWithScroll() - popupWidth) / 2) + "px";
    element.style.top = (popupTopOffset + getCurrentScrollYOffset()) + "px";
    document.body.appendChild(element);

    var darkeningOverlay = document.createElement("div");          
    darkeningOverlay.id = "wf_darkening_overlay";         
    darkeningOverlay.style.height = (getPageHeightWithScroll() + 60) + "px";
    document.body.appendChild(darkeningOverlay);  
}

function widgetFormLoaded(responseText) {
    document.getElementById("wf_popup").innerHTML = responseText; 
}

function getPageHeightWithScroll() {
    if (window.innerHeight && window.scrollMaxY) {
        // Firefox        
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight){
        // all but Explorer Mac
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    } 
    else {
        // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        yWithScroll = document.body.offsetHeight;
        xWithScroll = document.body.offsetWidth;
    }
    
    return yWithScroll;
}

function getPageWidthWithScroll() {
    if (window.innerHeight && window.scrollMaxY) {
        // Firefox        
        yWithScroll = window.innerHeight + window.scrollMaxY;
        xWithScroll = window.innerWidth + window.scrollMaxX;
    }
    else if (document.body.scrollHeight > document.body.offsetHeight){
        // all but Explorer Mac
        yWithScroll = document.body.scrollHeight;
        xWithScroll = document.body.scrollWidth;
    } 
    else {
        // works in Explorer 6 Strict, Mozilla (not FF) and Safari
        yWithScroll = document.body.offsetHeight;
        xWithScroll = document.body.offsetWidth;
    }
    
    return xWithScroll;
}

function wf_close() {
    var wf_popup = document.getElementById('wf_popup');
    wf_popup.parentNode.removeChild(wf_popup);
    
    var wf_darkening_overlay = document.getElementById('wf_darkening_overlay');
    wf_darkening_overlay.parentNode.removeChild(wf_darkening_overlay);    
}

function wf_submit() {
    var xmlHttp;
    try {  // Firefox, Opera 8.0+, Safari 
        xmlHttp = new XMLHttpRequest(); 
    }
    catch (e) {  // Internet Explorer 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");   
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;   
            }
        } 
    }
    
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState == 4)
            widgetFormPostResponseReceived(xmlHttp.responseText)  
    }
    
    var fields = document.getElementById("wf_fields").value.split(",");
    var poststring = "";
        
    for (var i = 0; i < fields.length; i++) {
        if (i > 0)
            poststring += "&";
            
        poststring += fields[i] + "=" + document.getElementById(fields[i]).value;
    }

    xmlHttp.open("post", wfRequestPage, true);
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
    xmlHttp.send(poststring); 
}

function widgetFormPostResponseReceived(responseText) {
    if (responseText.indexOf("<wf:success>") != -1) {
        document.getElementById("wf_popup").innerHTML = responseText;
    }
    else if (responseText.indexOf("<wf:redirect") != -1) {
        var split1 = responseText.split("url=\"");
        var split2 = split1[1].split("\">");
        
        window.location = split2[0];
    }
    else {
        clearErrorHighLights();
    
        var msgs = responseText.split("&");
        var errorMsg;
        var alertMsg = "";
        for (var i = 0; i < msgs.length; i++) {
            errorMsg = msgs[i].split("=");
            
            alertMsg += errorMsg[1] + "\n";
            
            document.getElementById(errorMsg[0]).style.border = "2px solid red";
        }
        
        alert(alertMsg);
    }
}

function clearErrorHighLights() {
    var fields = document.getElementById("wf_fields").value.split(",");
        
    for (var i = 0; i < fields.length; i++) {
        document.getElementById(fields[i]).style.border = document.getElementById("wf_noerror_border_style").value
    }
}

function getCurrentScrollYOffset() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
    return scrOfY;
}

