﻿/**********************************************************************
Interstitial.js
This script allows for interstitial to be popped when a form is 
submitted. This occurs after the existing submit occured and was 
successful.

To enable the interstitial, invoke the method attachInterstitialToSubmit,
with the arguments formName being the name of the form to be submitted,
and interstitialUrl being the url to pop.

This will result in that url being popped upon submission of the form,
with all of the form values appended onto the end of the url.
***********************************************************************/

/*******Event binding methods************/

function attachInterstitialToSubmit(formName, interstitialUrl){

    var interstitialSubmitEvent = function(){
        popInterstitial(interstitialUrl, formName, 'int1', true, true);
    }
    
    //addSubmitEvent(formName, interstitialSubmitEvent);
    var formToAttachTo = document.forms[formName];
    if(!formToAttachTo) return false;
    
    formToAttachTo.originalSubmitAndValidationEvent = formToAttachTo.onsubmit;
    
    // if has existing submit event, if submit succeeds then pop interstitial
    if(formToAttachTo.originalSubmitAndValidationEvent){
        formToAttachTo.onsubmit = function(){
            var submitSuccess = formToAttachTo.originalSubmitAndValidationEvent();
            if(submitSuccess){
                interstitialSubmitEvent();
                detachWaitPopFromClose();
            }
            // return success of original submit to allow the form to submit or not.
            return submitSuccess;
        }
    }
    else{
        formToAttachTo.onsubmit = function(){
            interstitialSubmitEvent();
            detachWaitPopFromClose();
            // always return true to cause form to submit.
            return true;
        }
    }
}

function attachWaitPopToClose(formName, waitPopUrl){
    window.onunload = function(){
        popWait(waitPopUrl);
        return true;
    }
}

function detachWaitPopFromClose(){
    window.onunload = null;
}

/*******End Event binding methods************/


function popInterstitial(interstitialUrl, formName, windowName, shouldBlur, shouldPrePop){
    var fullInterstitialUrl = getAndAttachPrePopStringToUrl(interstitialUrl, formName);
    var interstitialWindow = window.open(fullInterstitialUrl, windowName, "status=1,toolbar=1,scrollbars=1");
    if(interstitialWindow)
        if(shouldBlur) 
            interstitialWindow.blur();
    return interstitialWindow;
}

function popWait(waiturl){
    var waitWindow = window.open(waiturl, 'wait', 'status=1,toolbar=1,scrollbars=1');
    return waitWindow;
}

function getAndAttachPrePopStringToUrl(urlToAttachTo, formName){
    var prePopString = getPrePopString(formName);
    
    var fullUrl = urlToAttachTo;
    // append '?' if no query string exists so far
    if(fullUrl.indexOf('?') < 0)
        fullUrl += '?';
    fullUrl += prePopString;
    
    return fullUrl;    
}

// gets all form values on page and return a query string with all of the keys and values
function getPrePopString(formName){
    var formToPullFrom = document.forms[formName];
    if(!formToPullFrom) return null;
    
    var prePopString = '';
    
    var prePopValues = getFieldValuesFromForm(formToPullFrom);
    
    if(!prePopValues) return null;
    for(var fieldName in prePopValues){
        if(prePopValues[fieldName].value)
            prePopString += '&' + fieldName + '=' + prePopValues[fieldName].value;
    }
        
    return prePopString;
}

// returns array of all field values on the page.
function getFieldValuesFromForm(formToPullFrom){
    if(!formToPullFrom)
        return null;
    
    var fieldValues = new Array();
        
    for(var i = 0; i < formToPullFrom.elements.length; i++){
        var field = formToPullFrom.elements[i];
        var fieldValue = null;
        
        if(field){
            var fieldName = field.name;
            if(field.type){
                if(field.type.toLowerCase().match('radio')){
                    if(field.checked){
                        fieldValue = field.value;
                    }
                }
                else
                    fieldValue = field.value;  
            }
                    
            if(fieldValue){
                fieldValues[fieldName] = new Object();
                fieldValues[fieldName].value = fieldValue;
            }
        }
    }    
        
    return fieldValues;
}

function getValueFromRadioField(field){
    if(!field) return null;
    if(!field.length) return null;
    
    var fieldValue = null;
    
    for(var i = 0; i < field.length; i++)
    {
        if(field[i].checked){
            fieldValue = field[i].value;
            break;
        }
    }
    return fieldValue;             
}


