/**
* $Workfile: eleads.js $
* $Revision: 2 $  
* $Date: 9/05/08 11:02a $
* $Author: Afort $
* @Copyright 2008 Builder1440, LLC. All rights reserved.
* Javascript include for use by builder websites. 
* It handles the initialization of the eLeads widget.
**/
var iFrameObj;
var eLeadsId = 'eLeads';
var iFrameName = 'eLeadsIFrame';
var baseUri = "http://eleads.builder1440.com/eleads/";

/*
creates and formats the eLeads widget iframe
*/
function initializeELeads(iFrameWidth, iFrameHeight) {
    if (!iFrameWidth || isNaN(parseInt(iFrameWidth)) || parseInt(iFrameWidth) < 550) {
        iFrameWidth = 550; // default min width
    }
    
    if (!iFrameHeight || isNaN(parseInt(iFrameHeight)) || parseInt(iFrameHeight) < 700) {
        iFrameHeight = 700; // default min height
    }

    var iFrameMarginWidth = '0';
    var iFrameMarginHeight = '0';
    var iFrameBorder = '0';
    var iFrameScrolling = 'auto';
    var iframeOnload = 'targetTop();'; 
    var eLeads = document.getElementById(eLeadsId);
      
    if (!iFrameObj && document.createElement) {       
        var eLeadsUri = baseUri + 'Default.aspx';      
        
        // create the IFrame and assign a reference to the
        // object to our global variable iFrameObj.
        // this will only happen the first time 
        // initializeELeads() is called
        try {
            var tempIFrame = document.createElement('iframe');
            tempIFrame.setAttribute('id', iFrameName);
            tempIFrame.setAttribute('name', iFrameName);
            tempIFrame.setAttribute('width', iFrameWidth);
            tempIFrame.setAttribute('height', iFrameHeight);
            tempIFrame.setAttribute('marginwidth', iFrameMarginWidth);
            tempIFrame.setAttribute('marginheight', iFrameMarginHeight);            
            tempIFrame.setAttribute('frameBorder', iFrameBorder);
            tempIFrame.setAttribute('scrolling', iFrameScrolling);
            tempIFrame.setAttribute('onload', iframeOnload);

            iFrameObj = eLeads.appendChild(tempIFrame);
            
            if (document.frames) {
                // this is for IE5 Mac, because it will only
                // allow access to the document object
                // of the IFrame if we access it through
                // the document.frames array
                iFrameObj = document.frames[iFrameName];
            }
        } catch(exception) {
            // This is for IE5 PC, which does not allow dynamic creation
            // and manipulation of an iframe object. 
            // Instead, we'll fake it by creating our own objects.
            iframeHTML = '\<iframe id="';
            iframeHTML += iFrameName;
            iframeHTML += '" name="';
            iframeHTML += iFrameName;
            iframeHTML += '" width="';
            iframeHTML += iFrameWidth;
            iframeHTML += '" height="';
            iframeHTML += iFrameHeight;
            iframeHTML += '" marginwidth="';
            iframeHTML += iFrameMarginWidth;
            iframeHTML += '" marginheight="';
            iframeHTML += iFrameMarginHeight;
            iframeHTML += '" frameBorder="';            
            iframeHTML += iFrameBorder;
            iframeHTML += '" scrolling="'; 
            iframeHTML += iFrameScrolling;
            iframeHTML+='"><\/iframe>';
            
            eLeads.innerHTML += iframeHTML;
            
            iFrameObj = new Object();
            iFrameObj.document = new Object();
            iFrameObj.document.location = new Object();
            iFrameObj.document.location.iframe = document.getElementById(iFrameName);
            iFrameObj.document.location.replace = function(location) {
                this.iframe.src = location;
            }       
        }
    }
    
    setTimeout('setupListener()', 10);
  
    if (navigator.userAgent.indexOf('Gecko') != -1 && !iFrameObj.contentDocument) {
        // we have to give NS6 a fraction of a second
        // to recognize the new IFrame
        setTimeout('initializeELeads(iFrameWidth, iFrameHeight)', 10);
        return false;
    }

    var iFrameDoc;
    if (iFrameObj.contentDocument) {
        // For NS6
        iFrameDoc = iFrameObj.contentDocument; 
    } else if (iFrameObj.contentWindow) {
        // For IE5.5 and IE6
        iFrameDoc = iFrameObj.contentWindow.document;
    } else if (iFrameObj.document) {
        // For IE5
        iFrameDoc = iFrameObj.document;
    } else {
        return true;
    }
    
    iFrameDoc.location.replace(eLeadsUri);

    try {  
        if (document.frames) {
            iFrameObj.location = eLeadsUri;
        } else {
            iFrameObj.src = eLeadsUri;
        }
    } catch(exception) {
        alert('It appears that your browser does not support this functionality.');
    }

    return false;
}

/*
registers event listener for readyState change
*/
function setupListener() { 
    var objIFrame = window.document.getElementById(iFrameName); 
    if (objIFrame.addEventListener) {
        objIFrame.addEventListener('onreadystatechange', handleStateChange, false);
    } else if (objIFrame.attachEvent) {
        objIFrame.attachEvent ('onreadystatechange', handleStateChange);
    } 
}

/*
handles iFrames readyState change
this is used to handle scroll to top of
parent page when content in the iFrame reloads
*/
function handleStateChange(){
    switch(document.getElementById(iFrameName).readyState) {
        case "complete":
        case "loading":
            targetTop();
            break;
        case "interactive":
        case "uninitialized":
        default:
    } 
}

/*
scrolls to top of page when the iFrame reloads
*/
function targetTop() {
    window.scroll(0,0);
}