/**
* $Workfile: my1440Home.js $
* $Revision: 24 $  
* $Date: 2010-08-31 11:02:29 -0400 (Tue, 31 Aug 2010) $
* $Author: jyingling $
* Copyright 2007 G1440, Inc. All rights reserved.
* This provides common javascript functions 
* used across all My1440Home "faces" (admin, public, widget)
**/
    /* flag that is set when a form field is changed */
    var gFormChanged = false;
    function setChanged() {
        gFormChanged = true;
    }
    
    /* checks if any form fields have changed 
        and warns user before navigating */
    function confirmNavigation(href) {
        var navigate = false;
        if (gFormChanged) {
            navigate = confirm(navigationConfirmText);
        } else {
            navigate = true;
        }
        
        if (navigate) {
            document.location.href = href;
        }
    }
    
    /* checks if any form fields have changed 
        and warns user before canceling */
    function confirmCancel(href) {
        var cancel = false;
        if (gFormChanged) {
            cancel = confirm(cancelConfirmText);
        } else {
            cancel = true;
        }
        
        if (cancel) {
            if (href != null && href != "") {
                document.location.href = href;
            } else {
                history.back();
            }
        }
    }    
    
    /* sets form focus to a particular field 
        if doSelect is true then it also selects that field */
    function setFormFocus(formName, fieldName, doSelect) {
        document.getElementById(formName).elements[fieldName].focus();
        if (doSelect != null && doSelect == true) {
           document.getElementById(formName).elements[fieldName].select();
        }
    }
    
    /* form submit function for homescenario specific view community action */
    function viewCommunity(id) {
        if (id != null) {
            document.actionForm.communityId.value = id;
        }
        document.actionForm.action = "view.htm"
        document.actionForm.submit();      
    }
    
    /* form submit function for homescenario specific view spec action */
    function viewSpec(id) {
        if (id != null) {
            document.actionForm.id.value = id;
        }
        document.actionForm.action = "/homescenario/spec/view.htm"
        document.actionForm.submit();    
    }
    
    /* form submit function for homescenario specific view plan action */
    function viewPlan(id) {
        if (id != null) {
            document.actionForm.id.value = id;
        }
        document.actionForm.action = "/homescenario/plan/view.htm"
        document.actionForm.submit();    
    }
    
    /* form submit function for homescenario specific view lot action */
    function viewLot(id) {
        if (id != null) {
            document.actionForm.id.value = id;
        }
        document.actionForm.action = "/homescenario/lot/view.htm"
        document.actionForm.submit();    
    }    
    
    /* form submit function for view action */
    function viewEntity(id) {
        if (id != null) {
            document.actionForm.id.value = id;
        }
        document.actionForm.action = "view.htm"
        document.actionForm.submit();    
    }

    /* form submit function for update action */
    function updateEntity(id) {
        if (id != null) {
            document.actionForm.id.value = id;
        }
        document.actionForm.action = "edit.htm"
        document.actionForm.submit();    
    }
    
    /* safeguard for delete action */
    function confirmDelete() {
        return confirm(deleteConfirmText);
    } 

    /* form submit function for delete action */
    function deleteEntity(id) {
        if (confirmDelete()) {
            if (id != null) {
                document.actionForm.id.value = id;
            }
            document.actionForm.action = "delete.htm"
            document.actionForm.submit();
        }      
    }
    
    /* safeguard for remove action */
    function confirmRemove() {
        return confirm(removeConfirmText);
    }        

    /* form submit function for remove action */
    function removeEntity(id) {
        if (confirmRemove()) {
            if (id != null) {
                document.actionForm.id.value = id;
            }
            document.actionForm.action = "remove.htm"
            document.actionForm.submit();
        }      
    }
    
    /* form submit function for create scenario action */
    function createScenario() {
        document.actionForm.action = "/homescenario/edit.htm"
        document.actionForm.submit();    
    }
    
    /* 
        handles form submission for single select list criteria searches
        if there is a selected option for the element 
        it will submit the element's parent form
        it will also show the ProcessIndicator
    */
    function searchSelected(element) {
        if (element.value != null && element.value != "") {
            showProcessIndicator(); 
            element.form.submit();
        }
    }
    
    /* show process indicator image */ 
    function showProcessIndicator(elementId) {
        // if no elementId given, use default
        if (elementId == null) {
            elementId = "processIndicator";
        }
        document.getElementById(elementId).style.display = "inline";
    }
    
    /* hide process indicator image */ 
    function hideProcessIndicator(elementId) {
        // if no elementId given, use default
        if (elementId == null) {
            elementId = "processIndicator";
        }
        document.getElementById(elementId).style.display = "none";
    }    
    
    /* limits textarea entries */
    function textCounter(field, countfield, maxlimit) {
        if (field.value.length > maxlimit) {
            // if too long...trim it!
            field.value = field.value.substring(0, maxlimit);
        } else {
            // otherwise, update 'characters left' counter
            countfield.value = maxlimit - field.value.length;
        }
    } 
    
    /* Toggles lists of checkboxes to match the state of the
        supplied masterCheckbox.  Sets the title of the masterCheckbox.
        params:
        masterCheckbox - master checkbox control
        checkboxGroup - name of checkbox group */
    function toggleCheckboxes(masterCheckbox, checkboxGroup) {    
        var checkboxGroup = document.getElementsByName(checkboxGroup);        
        try {
            var state = masterCheckbox.checked;
            if ( isNaN(checkboxGroup.length) ) {
                // make sure that the checkbox isn't disabled
                if (!checkboxGroup.disabled) {
                    checkboxGroup.checked = state;
                }
            } else {
                for(var ctr=0; ctr<checkboxGroup.length; ctr++) {
                    // make sure that the checkbox isn't disabled
                    if (!checkboxGroup[ctr].disabled) {
                        checkboxGroup[ctr].checked = state;
                    }
                }
            }
            masterCheckbox.title = (state ? unCheckAllText : checkAllText);
        } catch (e) {
            ret = false;
        }
        
        return false;
    }
    
    /*  Popup Window */
    function showPopup(uri, popupWidth, popupHeight) {
        if (!popupWidth || isNaN(parseInt(popupWidth))) {
            popupWidth = 800; // default width
        }
        
        if (!popupHeight || isNaN(parseInt(popupHeight))) {
            popupHeight = 600; // default height
        }
        window.open(uri, "Popup", "width=" + popupWidth + ",height=" + popupHeight + ""); 
    }
    
    /* Provides dynamic UI support for entity graphic selection */
    function handleGraphicType(graphicTypeElement, graphicUriElement) {
        var selectGraphicSpan = document.getElementById("selectGraphicSpan");
                
        switch(graphicTypeElement.value) {
        case "1": // internal              
            if (selectGraphicSpan == null) {
                // create select graphic button
                selectGraphicSpan = document.createElement("span");
                selectGraphicSpan.setAttribute("id", "selectGraphicSpan");
                graphicTypeElement.parentNode.insertBefore(selectGraphicSpan, graphicTypeElement.nextSibling);
                selectGraphicSpan.innerHTML = "<input id=\"selectGraphicButton\" type=\"button\" class=\"button\" value=\"" + selectGraphicButtonText + "\" title=\"" + selectGraphicButtonText + "\" onclick=\"selectGraphic(document.getElementById('" + graphicUriElement.id + "'));\" />";
                //selectGraphicSpan.innerHTML = "<a href=\"/admin/file/manage.htm?id=" + graphicUriElement.id + "\" id=\"selectGraphicButton\" title=\"" + selectGraphicButtonText + "\"  rel=\"lyteframe\" rev=\"width: 800px; height: 500px; scrolling: no;\">" + selectGraphicButtonText + "</a>";
            }
            graphicUriElement.disabled = false;
            graphicUriElement.readOnly = true;
            previewGraphic(graphicUriElement);
            break;
        case "2": // external
            if (selectGraphicSpan != null) {
                // remove select graphic button
                selectGraphicSpan.parentNode.removeChild(selectGraphicSpan);
            }
            graphicUriElement.disabled = false;
            graphicUriElement.readOnly = false;
            previewGraphic(graphicUriElement);
            break;
        default: // none
            if (selectGraphicSpan != null) {
                // remove select graphic button
                selectGraphicSpan.parentNode.removeChild(selectGraphicSpan);
            }
            graphicUriElement.value = "";
            graphicUriElement.disabled = true;
            graphicUriElement.readOnly = true;
            previewGraphic(graphicUriElement);
        }
    }
    
    /* Provides dynamic UI support for previewing entity graphics */
    function previewGraphic(graphicUriElement) {
        var graphicNAUri = "/common/images/image_na.gif";
        var graphicUri = graphicUriElement.value;
        var previewGraphic = document.getElementById("previewGraphic");        
        
        if (previewGraphic != undefined && previewGraphic != null) {
            if (graphicUri != null && graphicUriElement.value != "") {
                // include graphic
                previewGraphic.innerHTML = "<img src=\"" + graphicUri + "\" />";
            } else {
                // include "image not available" graphic
                previewGraphic.innerHTML = "<img src=\"" + graphicNAUri + "\" />";
            }
        }
    }
    
    /* Entity image select popup */
    function selectGraphic(graphicUriElement) {
        var imageManageUri = "/admin/file/manage.htm?id=";    
        var uri = imageManageUri + graphicUriElement.id;    
        window.open(uri, "manageFile", "menubar=no,status=yes,toolbar=no,location=no,width=800,height=500");
    }
    
    /* Entity image insert from popup */
    function insertGraphic(graphicUriElementId, graphicUri) {
        var graphicUriElement = opener.document.getElementById(graphicUriElementId);
        graphicUriElement.value = graphicUri;
        opener.previewGraphic(graphicUriElement);
        this.close();
    }
    
    /* Entity image upload popup */
    function uploadImage(imageUriElement) {
        var imageManageUri = "/admin/upload/manage.htm?id=";    
        var uri = imageManageUri + imageUriElement.id;    
        window.open(uri, "manageUpload", "menubar=no,status=yes,toolbar=no,location=no,width=800,height=500");
    }
    
    /* Provides dynamic UI support for previewing CSS color entries */
    function previewCssColor(element) {
        var elementId = element.id
        var preview = document.getElementById(elementId + "_preview");

        preview.style.backgroundColor = element.value;
    }
    
    // function to print contents of window
    function printWindow() {  
        if (window.print) {
            window.print();  
        } else {
            var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH="0" HEIGHT="0" CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
            document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
            WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
        }
    }    
    
    /* populates state select list based on selected country */
    function setStateList(selectedCountry, selectedState) {   
        var countrySelect = document.getElementById("country"); 
        //Get the selected country code
        var selectedCountryCode = countrySelect.options[countrySelect.selectedIndex].value;
        
        var defaultSelectedCountryCode = false;
        if(selectedCountryCode==selectedCountry){
            defaultSelectedCountryCode = true;
        }
        
        var stateSelect = document.getElementById("state");
        if(stateSelect != null){
            //Set the options list to zero
            stateSelect.length = 0;
            stateSelect.options[0] = new Option(defaultSelectHeaderText, "");
            //Retrieve an array of state codes for the selected country
            var stateArray = masterStates[selectedCountryCode];
            
            if(stateArray != null){
                //Re-build the option list
                for(var i=1; i<=stateArray.length; i++){
                    stateSelect.options[i] = new Option(stateArray[i-1], stateArray[i-1]);
                    if(defaultSelectedCountryCode && stateArray[i-1] == selectedState){
                        stateSelect.options[i].selected = true;
                    }
                    // If we're working with a country that has no "real" state/province codes defined,
                    // then pre-default selection to the "-" value
                    if(stateArray.length == 1 && stateArray[i-1] == "-"){
                        stateSelect.options[i].selected = true;
                    }
                }
            }
            else{
                stateSelect.options[0].selected = true;
            }
        }
    }
        
    /* i18n Country/State Pairing */
    ADStates = ["-"];
    AGStates = ["-"];
    ALStates = ["-"];
    AMStates = ["-"];
    ANStates = ["-"];
    ARStates = ["-"];
    ATStates = ["-"];
    AUStates = ["ACT","NSW","NT","QLD","SA","TAS","VIC","WA"];
    AWStates = ["-"];
    AZStates = ["-"];
    BAStates = ["-"];
    BBStates = ["-"];
    BEStates = ["-"];
    BGStates = ["-"];
    BMStates = ["-"];
    BOStates = ["-"];
    BRStates = ["AC","AL","AM","AP","BA","CE","DF","ES","GO","MA","MG","MS","MT","PA","PB","PE","PI","PR","RJ","RN","RO","RR","RS","SC","SE","SP","TO"];
    BSStates = ["-"];
    BYStates = ["-"];
    BZStates = ["-"];
    CAStates = ["AB","BC","MB","NB","NL","NS","NT","NU","ON","PE","QC","SK","YT"];
    CHStates = ["-"];
    CLStates = ["-"];
    CNStates = ["-"];
    COStates = ["-"];
    CRStates = ["-"];
    CSStates = ["-"];
    CUStates = ["-"];
    CYStates = ["-"];
    CZStates = ["-"];
    DEStates = ["-"];
    DKStates = ["-"];
    DMStates = ["-"];
    DOStates = ["-"];
    ECStates = ["-"];
    EEStates = ["-"];
    ESStates = ["-"];
    FIStates = ["-"];
    FRStates = ["-"];
    GDStates = ["-"];
    GEStates = ["-"];
    GFStates = ["-"];
    GLStates = ["-"];
    GPStates = ["-"];
    GRStates = ["-"];
    GTStates = ["-"];
    GYStates = ["-"];
    HKStates = ["-"];
    HNStates = ["-"];
    HRStates = ["-"];
    HTStates = ["-"];
    HUStates = ["-"];
    IEStates = ["-"];
    ILStates = ["-"];
    INStates = ["-"];
    ISStates = ["-"];
    ITStates = ["-"];
    JMStates = ["-"];
    JPStates = ["-"];
    KNStates = ["-"];
    KZStates = ["-"];
    LCStates = ["-"];
    LIStates = ["-"];
    LTStates = ["-"];
    LUStates = ["-"];
    LVStates = ["-"];
    MCStates = ["-"];
    MDStates = ["-"];
    MKStates = ["-"];
    MQStates = ["-"];
    MSStates = ["-"];
    MTStates = ["-"];
    MXStates = ["AGS","BCN","BCS","CAM","CHIH","CHIS","COAH","COL","DF","DGO","GRO","GTO","HGO","JAL","MEX","MICH","MOR","NAY","NL","OAX","PUE","QRO","QROO","SIN","SLP","SON","TAB","TAMP","TLAX","VER","YUC","ZAC"];
    NAStates = ["-"];
    NIStates = ["-"];
    NLStates = ["-"];
    NOStates = ["-"];
    NZStates = ["-"];
    PAStates = ["-"];
    PEStates = ["-"];
    PLStates = ["-"];
    PTStates = ["-"];
    PYStates = ["-"];
    ROStates = ["-"];
    RUStates = ["-"];
    SEStates = ["-"];
    SIStates = ["-"];
    SKStates = ["-"];
    SMStates = ["-"];
    SRStates = ["-"];
    SVStates = ["-"];
    TCStates = ["-"];
    TJStates = ["-"];
    TMStates = ["-"];
    TTStates = ["-"];
    UAStates = ["-"];
    UKStates = ["-"];
    USStates = ["AA","AE","AK","AL","AP","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","PR","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY"];
    UYStates = ["-"];
    UZStates = ["-"];
    VCStates = ["-"];
    VEStates = ["-"];
    VGStates = ["-"];
    
    var masterStates = new Array();
    masterStates.AD = ADStates;
    masterStates.AG = AGStates;
    masterStates.AL = ALStates;
    masterStates.AM = AMStates;
    masterStates.AN = ANStates;
    masterStates.AR = ARStates;
    masterStates.AT = ATStates;
    masterStates.AU = AUStates;
    masterStates.AW = AWStates;
    masterStates.AZ = AZStates;
    masterStates.BA = BAStates;
    masterStates.BB = BBStates;
    masterStates.BE = BEStates;
    masterStates.BG = BGStates;
    masterStates.BM = BMStates;
    masterStates.BO = BOStates;
    masterStates.BR = BRStates;
    masterStates.BS = BSStates;
    masterStates.BY = BYStates;
    masterStates.BZ = BZStates;
    masterStates.CA = CAStates;
    masterStates.CH = CHStates;
    masterStates.CL = CLStates;
    masterStates.CN = CNStates;
    masterStates.CO = COStates;
    masterStates.CR = CRStates;
    masterStates.CS = CSStates;
    masterStates.CU = CUStates;
    masterStates.CY = CYStates;
    masterStates.CZ = CZStates;
    masterStates.DE = DEStates;
    masterStates.DK = DKStates;
    masterStates.DM = DMStates;
    masterStates.DO = DOStates;
    masterStates.EC = ECStates;
    masterStates.EE = EEStates;
    masterStates.ES = ESStates;
    masterStates.FI = FIStates;
    masterStates.FR = FRStates;
    masterStates.GD = GDStates;
    masterStates.GE = GEStates;
    masterStates.GF = GFStates;
    masterStates.GL = GLStates;
    masterStates.GP = GPStates;
    masterStates.GR = GRStates;
    masterStates.GT = GTStates;
    masterStates.GY = GYStates;
    masterStates.HK = HKStates;
    masterStates.HN = HNStates;
    masterStates.HR = HRStates;
    masterStates.HT = HTStates;
    masterStates.HU = HUStates;
    masterStates.IE = IEStates;
    masterStates.IL = ILStates;
    masterStates.IN = INStates;
    masterStates.IS = ISStates;
    masterStates.IT = ITStates;
    masterStates.JM = JMStates;
    masterStates.JP = JPStates;
    masterStates.KN = KNStates;
    masterStates.KZ = KZStates;
    masterStates.LC = LCStates;
    masterStates.LI = LIStates;
    masterStates.LT = LTStates;
    masterStates.LU = LUStates;
    masterStates.LV = LVStates;
    masterStates.MC = MCStates;
    masterStates.MD = MDStates;
    masterStates.MK = MKStates;
    masterStates.MQ = MQStates;
    masterStates.MS = MSStates;
    masterStates.MT = MTStates;
    masterStates.MX = MXStates;
    masterStates.NA = NAStates;
    masterStates.NI = NIStates;
    masterStates.NL = NLStates;
    masterStates.NO = NOStates;
    masterStates.NZ = NZStates;
    masterStates.PA = PAStates;
    masterStates.PE = PEStates;
    masterStates.PL = PLStates;
    masterStates.PT = PTStates;
    masterStates.PY = PYStates;
    masterStates.RO = ROStates;
    masterStates.RU = RUStates;
    masterStates.SE = SEStates;
    masterStates.SI = SIStates;
    masterStates.SK = SKStates;
    masterStates.SM = SMStates;
    masterStates.SR = SRStates;
    masterStates.SV = SVStates;
    masterStates.TC = TCStates;
    masterStates.TJ = TJStates;
    masterStates.TM = TMStates;
    masterStates.TT = TTStates;
    masterStates.UA = UAStates;
    masterStates.UK = UKStates;
    masterStates.US = USStates;
    masterStates.UY = UYStates;
    masterStates.UZ = UZStates;
    masterStates.VC = VCStates;
    masterStates.VE = VEStates;
    masterStates.VG = VGStates;    
    
