﻿// proper case string prptotype (JScript 5.5+)
String.prototype.toProperCase = function() { return this.toLowerCase().replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); } ); }
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }
String.prototype.ltrim = function() { return this.replace(/^\s+/,""); }
String.prototype.rtrim = function() { return this.replace(/\s+$/,""); }
String.prototype.count = function(s1) { return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length; }  

function specialFeature_AutoSelectCountryDDL(provVal, countryID, countryNAME) {
    var country = '';
    if (countryNAME != '') {
        var countryTemp, countryItem, countryContainer = Safe_GetElementsByName(countryNAME);
        if (countryContainer.length > 0) {
            if ( countryContainer.constructor.toString().indexOf('Array') == -1 ) { countryItem = countryContainer.item(0); } else { countryItem = countryContainer[0]; }
            countryTemp = Safe_GetElementByTagName('select', countryItem);
            if (countryTemp == null) { countryTemp = Safe_GetElementByTagName('input', countryItem); }
            if (countryTemp != null) { country = countryTemp.item(0); }
        }
    } else {
        country = Safe_GetElementById(countryID);
    }

    if (country != null && country != 'undefined') {
        countryVal = country.options[country.selectedIndex].value;
        if (provVal == 'AB' || provVal == 'BC' || provVal == 'MB' || provVal == 'NB' || provVal == 'NL' ||
            provVal == 'NT' || provVal == 'NS' || provVal == 'NU' || provVal == 'ON' || provVal == 'PE' ||
            provVal == 'QC' || provVal == 'SK' || provVal == 'YT') {
            specialFeature_AutoSelectDDL_SelectItem(country, 'CA');
        } else if (provVal == 'OTE') {
            if (countryVal == 'CA' || countryVal == 'US') { specialFeature_AutoSelectDDL_SelectItem(country, ''); }
        } else if (provVal != '') {
            specialFeature_AutoSelectDDL_SelectItem(country, 'US');
        }
    }
    return (true);
}

function specialFeature_AutoSelectProvinceDDL(countryVal, provinceID, provinceNAME) {
    var province = '';
    if (provinceNAME != '') {
        var provinceTemp, provinceItem, provinceContainer = Safe_GetElementsByName(provinceNAME);
        if (provinceContainer.length > 0) {
            if ( provinceContainer.constructor.toString().indexOf('Array') == -1 ) { provinceItem = provinceContainer.item(0); } else { provinceItem = provinceContainer[0]; }
            provinceTemp = Safe_GetElementByTagName('select', provinceItem);
            if (provinceTemp == null) { provinceTemp = Safe_GetElementByTagName('input', provinceItem); }
            if (provinceTemp != null) { province = provinceTemp.item(0); }
        }
    } else {
        province = Safe_GetElementById(provinceID);
    }

    if (province != null && province != 'undefined') {
        provVal = province.options[province.selectedIndex].value;
        if ( (countryVal == 'CA' && (provVal != 'AB' && provVal != 'BC' && provVal != 'MB' && provVal != 'NB' && provVal != 'NL' &&
                                     provVal != 'NT' && provVal != 'NS' && provVal != 'NU' && provVal != 'ON' && provVal != 'PE' &&
                                     provVal != 'QC' && provVal != 'SK' && provVal != 'YT') ) ||
             (countryVal == 'US' && (provVal == 'AB' || provVal == 'BC' || provVal == 'MB' || provVal == 'NB' || provVal == 'NL' ||
                                     provVal == 'NT' || provVal == 'NS' || provVal == 'NU' || provVal == 'ON' || provVal == 'PE' ||
                                     provVal == 'QC' || provVal == 'SK' || provVal == 'YT' || provVal == 'OTE')) ) {
            specialFeature_AutoSelectDDL_SelectItem(province, '');
        } else if (countryVal != 'CA' && countryVal != 'US') {
            specialFeature_AutoSelectDDL_SelectItem(province, 'OTE');
        }
    }
    return (true);
}

function ItemSelector_SetCSSClass(controlID) {

    var parentContainer = document.getElementById('MainSection_Content');
    var itemID, itemSelector, SelectorControl, SelectorControl_Double; 
    if (parentContainer != null) {
        for (var j = 0; j < parentContainer.childNodes.length; j++) {
            if (parentContainer.childNodes[j].id != null) {
                var p2 = parentContainer.childNodes[j].getElementsByTagName('div');
                for (var i = 0; i < p2.length; i++) {   
                    if (p2[i].attributes['name'] != null) {
                        switch(p2[i].attributes['name'].nodeValue) {
                            case "ShowProducts":
                            case "SeminarSessions":
                                itemID = p2[i].id.substring(p2[i].id.indexOf('_') + 1);
                                itemSelector = document.getElementById('Item_Selector_' + itemID);
                                SelectorControl = itemSelector.getElementsByTagName('select')[0];
                                if ( SelectorControl != null && itemSelector.getElementsByTagName('input').length > 0 ) { if(itemSelector.getElementsByTagName('input')[0].id == controlID) { SelectorControl = itemSelector.getElementsByTagName('input')[0]; } }
                                if ( SelectorControl == null ) { SelectorControl = itemSelector.getElementsByTagName('input')[0]; }
                                
                                document.getElementById('Item_' + itemID).className = document.getElementById('Item_' + itemID).className.replace(' ItemSelected', '');
                                switch (SelectorControl.type) {
                                    case "text": // running on switch till it hits the break
                                    case "select":  // running on switch till it hits the break
                                    case "select-one":  // running on switch till it hits the break
                                        SelectorControl_Double = itemSelector.getElementsByTagName('input')[0];
                                        if (SelectorControl.value > 0 || SelectorControl.value == "on") { 
                                            document.getElementById('Item_' + itemID).className += ' ItemSelected'; 
                                            if (SelectorControl_Double != null) { SelectorControl_Double.checked = true; }
                                        } else { if (SelectorControl_Double != null) { SelectorControl_Double.checked = false; } } break;
                                    default: // checkbox / radiobutton
                                        SelectorControl_Double = itemSelector.getElementsByTagName('select')[0];
                                        if (SelectorControl.checked) { 
                                            document.getElementById('Item_' + itemID).className += ' ItemSelected'; 
                                            if (SelectorControl_Double != null) { SelectorControl_Double.value = '1'; }
                                        } else { if (SelectorControl_Double != null) { SelectorControl_Double.selectedIndex = 0; } } break;
                                }
                        }
                    }
                }
            }
        }
    }
    
}

function ItemSelector_CalculateTotal() {
            
    // get to the main parent section container
    var language = eval(Safe_GetElementById('Website_Language')) ? Safe_GetElementById('Website_Language').value : 'english' ;
    var parentContainer = document.getElementById('MainSection_Content'); 
    var SelectorControl, DiscountVisible;
    var GrandTotalAmount = 0, SectionTotalAmount = 0, TotalAmount = 0, FeeAmount = 0, DiscountedAmount = 0, GrandDiscountAmount = 0;
    
    if (parentContainer != null) {
    
        for (var j = 0; j < parentContainer.childNodes.length; j++) {
            
            if (parentContainer.childNodes[j].id != null) {
                
                var p2 = parentContainer.childNodes[j].getElementsByTagName('div');
                SectionTotalAmount = 0;
                                        
                for (var i = 0; i < p2.length; i++) {   
                    
                    if (p2[i].attributes['name'] != null) {
                        var itemID = p2[i].id.substring(p2[i].id.indexOf('_') + 1);
                        FeeAmount = 0;  
                        TotalAmount = 0;
                        DiscountedAmount = 0;
                        DiscountVisible = 'none';          
                        
                        switch(p2[i].attributes['name'].nodeValue) {
                            case "ShowProducts":
                            case "SeminarSessions":
                                if (language == 'french') {
                                    FeeAmount = document.getElementById('Item_Fee_' + itemID).innerHTML.replace(/\$/, "").replace(/ /g, "").replace(/\,/, ".");
                                    DiscountedAmount = (eval(document.getElementById('Item_Discount_' + itemID))) ? document.getElementById('Item_Discount_' + itemID).innerHTML.replace(/\$/, "").replace(/ /g, "").replace(/\,/, ".") : null ;
                                } else {
                                    FeeAmount = document.getElementById('Item_Fee_' + itemID).innerHTML.replace(/\$/, "").replace(/\,/, "");
                                    DiscountedAmount = (eval(document.getElementById('Item_Discount_' + itemID))) ? document.getElementById('Item_Discount_' + itemID).innerHTML.replace(/\$/, "").replace(/\,/, "") : null ;
                                }
                                DiscountVisible = (eval(document.getElementById('Item_Discount_' + itemID))) ? document.getElementById('Item_Discount_' + itemID).style.display : 'none' ;
                                
                                if (FeeAmount == "FREE" || FeeAmount == "" || FeeAmount == "undefined" || FeeAmount == null) { FeeAmount = 0; }
                                if (DiscountedAmount == "FREE" || DiscountedAmount == "" || DiscountedAmount == "undefined" || DiscountedAmount == null) { DiscountedAmount = null; }
                                
                                SelectorControl = document.getElementById('Item_Selector_' + itemID).getElementsByTagName('select')[0];
                                if (SelectorControl == null) { SelectorControl = document.getElementById('Item_Selector_' + itemID).getElementsByTagName('input')[0]; }
                                
                                document.getElementById('Item_' + itemID).className = document.getElementById('Item_' + itemID).className.replace(' ItemSelected', '');
                                
                                switch (SelectorControl.type) {
                                    case "text": // running on switch till it hits the break
                                    case "select":  // running on switch till it hits the break
                                    case "select-one":  // running on switch till it hits the break
                                        TotalAmount = (DiscountedAmount != null && DiscountVisible != 'none') ? (SelectorControl.value * FeeAmount) - (SelectorControl.value * DiscountedAmount) : SelectorControl.value * FeeAmount;
                                        if (SelectorControl.value > 0 || SelectorControl.value == "on") { 
                                            document.getElementById('Item_' + itemID).className += ' ItemSelected'; 
                                            if ( DiscountedAmount != null) { GrandDiscountAmount = parseFloat(GrandDiscountAmount) + parseFloat(DiscountedAmount); }
                                        }
                                        break;
                                    default: // checkbox / radiobutton
                                        if (SelectorControl.checked) { 
                                            TotalAmount = (DiscountedAmount != null && DiscountVisible != 'none') ? FeeAmount - DiscountedAmount : FeeAmount; 
                                            document.getElementById('Item_' + itemID).className += ' ItemSelected';
                                            if ( DiscountedAmount != null) { GrandDiscountAmount = parseFloat(GrandDiscountAmount) + parseFloat(DiscountedAmount); }
                                        } 
                                }
                                
                                if (TotalAmount < 0) { TotalAmount = 0; } // can not go below 0
                                
                                SectionTotalAmount = parseFloat(SectionTotalAmount) + parseFloat(TotalAmount);
                                GrandTotalAmount = parseFloat(GrandTotalAmount) + parseFloat(TotalAmount);
                                if ( eval(document.getElementById('Item_Total_' + itemID)) ) { document.getElementById('Item_Total_' + itemID).innerHTML = CurrencyFormatted(TotalAmount, language); }
                                                    
                                break;
                            case "ItemSelector_SectionTotal":
                                document.getElementById('GridFooter_Total_' + itemID).innerHTML = CurrencyFormatted(SectionTotalAmount, language);
                                break;
                            case "ItemSelector_GrandTotal":
                                // calculate discount section                                
                                if (document.getElementById('GridFooter_Total_DiscountTotal_' + itemID) ) {
                                    document.getElementById('GridFooter_Total_DiscountSubTotal_' + itemID).innerHTML = CurrencyFormatted(GrandTotalAmount, language);
                                    document.getElementById('GridFooter_Total_DiscountTotal_' + itemID).innerHTML = CurrencyFormatted(GrandDiscountAmount, language);
                                    GrandTotalAmount = parseFloat(GrandTotalAmount) - parseFloat(GrandDiscountAmount);
                                }
                                
                                // assign total value
                                document.getElementById('GridFooter_Total_' + itemID).innerHTML = CurrencyFormatted(GrandTotalAmount, language);
                                
                                // calculate tax if they exist
                                if (document.getElementById('GridFooter_Total_GrandTotal_' + itemID) ) {
                                    var Tax1 = 0, Tax2 = 0;
                                    if ( document.getElementById('GridFooter_Total_Tax1_' + itemID + '_TaxAmount') ) {
                                        Tax1 = (document.getElementById('GridFooter_Total_Tax1_' + itemID + '_TaxAmount').value / 100 ) * GrandTotalAmount;
                                        document.getElementById('GridFooter_Total_Tax1_' + itemID).innerHTML = CurrencyFormatted(Tax1, language);
                                    }
                                    if ( document.getElementById('GridFooter_Total_Tax2_' + itemID + '_TaxAmount') ) {
                                        Tax2 = (document.getElementById('GridFooter_Total_Tax2_' + itemID + '_TaxAmount').value / 100 ) * GrandTotalAmount; 
                                        document.getElementById('GridFooter_Total_Tax2_' + itemID).innerHTML = CurrencyFormatted(Tax2, language);
                                    }
                                    document.getElementById('GridFooter_Total_GrandTotal_' + itemID).innerHTML = CurrencyFormatted(GrandTotalAmount + Tax1 + Tax2, language);
                                }
                                break;
                            default:
                               // do nothing
                        } // end switch
                    } // end if (null)
                }  // end for
            } // end if (null)
        } // end for
    } // end if (null parentcontainer)
}

function ItemSelector_ConflictingTimes(controlType, theControl, ConflictMessage, SkipTimeConflictCodes) {

    if ( SkipTimeConflictCodes === undefined ) { SkipTimeConflictCodes = ''; } else { if (SkipTimeConflictCodes.trim() != '') { SkipTimeConflictCodes = ',' + SkipTimeConflictCodes + ','; } }

    var validateItem = false;
    var language = eval(Safe_GetElementById('Website_Language')) ? Safe_GetElementById('Website_Language').value : 'english'; 
    switch (theControl.type) {
        case "text": // running on switch till it hits the break
        case "select":  // running on switch till it hits the break
        case "select-one":  // running on switch till it hits the break
            if (theControl.value > 0 || theControl.value == "on") { validateItem = true; } break;
        default: // checkbox / radiobutton
            if (theControl.checked) { validateItem = true; }
    }
    
    if ( validateItem ) {

        // get id of the control
        var controlID = theControl.id.substring(theControl.id.lastIndexOf("_") + 1);
        var Control_StartDate = new Date( Safe_GetElementById('Item_' + controlID + '_' + controlType + '_StartDateTime').value );
        var Control_EndDate = new Date( Safe_GetElementById('Item_' + controlID + '_' + controlType + '_EndDateTime').value );
        var Control_Code = Safe_GetElementById('Item_' + controlID + '_' + controlType + '_Code').value;
        //var Control_Name = Safe_GetElementById('Item_' + controlID + '_' + controlType + '_Name').value;
        
        if (Control_StartDate != 'Invalid Date' && Control_EndDate != 'Invalid Date') {
        
            var p2, itemID, SelectorControl, itemSelector, SelectorControl_Double, skipTimeConflict = false;
            var Selector_StartDate, Selector_EndDate, Selector_Code, Selector_Name;
            
            // get to the main parent section container
            var parentContainer = document.getElementById('MainSection_Content');
            if (parentContainer != null) {
            
                for (var j = 0; j < parentContainer.childNodes.length; j++) {
                    
                    if (parentContainer.childNodes[j].id != null) {
                        
                        p2 = parentContainer.childNodes[j].getElementsByTagName('div');
                                                
                        for (var i = 0; i < p2.length; i++) {   
                            
                            if (p2[i].attributes['name'] != null) {
                                itemID = p2[i].id.substring(p2[i].id.indexOf('_') + 1);
                                switch(p2[i].attributes['name'].nodeValue) {
                                    case "ShowProducts":
                                    case "SeminarSessions":
                                        
                                        if ( controlType.indexOf(p2[i].attributes['name'].nodeValue) != -1 ) {
                                        
                                            if ( itemID != controlID ) {
                                                itemSelector = document.getElementById('Item_Selector_' + itemID);
                                                SelectorControl = itemSelector.getElementsByTagName('select')[0];
                                                if ( SelectorControl != null && itemSelector.getElementsByTagName('input').length > 0 ) {  if(theControl.type == 'checkbox') { SelectorControl = itemSelector.getElementsByTagName('input')[0]; } }
                                                if ( SelectorControl == null ) { SelectorControl = itemSelector.getElementsByTagName('input')[0]; }
                                                
                                                Selector_StartDate = new Date( Safe_GetElementById('Item_' + itemID + '_' + controlType + '_StartDateTime').value );
                                                Selector_EndDate = new Date( Safe_GetElementById('Item_' + itemID + '_' + controlType + '_EndDateTime').value );
                                                Selector_Code = Safe_GetElementById('Item_' + itemID + '_' + controlType + '_Code').value;
                                                //Selector_Name = Safe_GetElementById('Item_' + itemID + '_' + controlType + '_Name').value;
                                                
                                                // check if to skip timeconflict check
                                                if (SkipTimeConflictCodes != '' && SkipTimeConflictCodes.indexOf(','+Selector_Code+',') != -1 ) { skipTimeConflict = true; }
                                                
                                                if ( skipTimeConflict == false ) {
                                                
                                                    if (ConflictMessage == '') { 
                                                        if(language == 'french') { ConflictMessage = "CONFLIT D'HORAIRE!\r\n\r\nLa session que vous venez de choisir a lieu en même temps qu'une autre que vous avez choisie."; } else { ConflictMessage = 'TIME CONFLICT!\r\n\r\nThe event you just selected occurs at the same time as a previous selection.'; } 
                                                    } else {
                                                        if(ConflictMessage.indexOf('[Selector_Code]') != -1) { ConflictMessage = ConflictMessage.replace('[Selector_Code]', Selector_Code); }
                                                        //if(ConflictMessage.indexOf('[Selector_Name]') != -1) { ConflictMessage = ConflictMessage.replace('[Selector_Name]', Selector_Code); }
                                                        if(ConflictMessage.indexOf('[Control_Code]') != -1) { ConflictMessage = ConflictMessage.replace('[Control_Code]', Control_Code); }
                                                        //if(ConflictMessage.indexOf('[Control_Name]') != -1) { ConflictMessage = ConflictMessage.replace('[Control_Name]', Control_Code); }
                                                    }
                                                
                                                    switch (SelectorControl.type) {
                                                        case "text": // running on switch till it hits the break
                                                        case "select":  // running on switch till it hits the break
                                                        case "select-one":  // running on switch till it hits the break
                                                            SelectorControl_Double = document.getElementById('Item_Selector_' + controlID).getElementsByTagName('input')[0];
                                                            if (SelectorControl.value > 0 || SelectorControl.value == "on") { 
                                                                if ( (( Control_StartDate >= Selector_StartDate ) && ( Control_StartDate < Selector_EndDate )) ||
                                                                     (( Control_StartDate < Selector_StartDate ) && ( Control_EndDate > Selector_StartDate )) ) {
                                                                    document.getElementById('Item_' + controlID).className = document.getElementById('Item_' + controlID).className.replace(' ItemSelected', '');
                                                                    if (theControl.type == 'text' ) { theControl.value = 0; } else { theControl.selectedIndex = 0; }
                                                                    if (SelectorControl_Double != null) { SelectorControl_Double.checked = false; }
                                                                    alert( ConflictMessage );
                                                                    return (false);
                                                                }
                                                            }
                                                            break;
                                                        default: // checkbox / radiobutton
                                                            SelectorControl_Double = document.getElementById('Item_Selector_' + controlID).getElementsByTagName('select')[0];
                                                            if (SelectorControl.checked) { 
                                                                if ( (( Control_StartDate >= Selector_StartDate ) && ( Control_StartDate < Selector_EndDate )) ||
                                                                     (( Control_StartDate < Selector_StartDate ) && ( Control_EndDate > Selector_StartDate )) ) {
                                                                    document.getElementById('Item_' + controlID).className = document.getElementById('Item_' + controlID).className.replace(' ItemSelected', '');
                                                                    theControl.checked = false;
                                                                    if (SelectorControl_Double != null) {  if (SelectorControl_Double.type == 'text' ) { SelectorControl_Double.value = 0; } else { SelectorControl_Double.selectedIndex = 0; } }
                                                                    alert( ConflictMessage );
                                                                    return (false);
                                                                }
                                                            } 
                                                    }
                                                }
                                            }
                                        }
                                        break;
                                } // end switch
                            } // end if (null)
                        }  // end for
                    } // end if (null)
                } // end for
            } // end if (null parentcontainer)
        } // end if ( null start date and null end date
    }
    return true;
    
}

function specialFeature_AutoSelectDDL_SelectItem(theControl, selectVal) { 
    if(theControl) {
        for (var i = 0; i < theControl.length; i++) {
            if(theControl[i].value == selectVal) {
                theControl[i].selected = true; 
                break; 
            }
        }
    }
    return true;
}

function Safe_GetElementsByName(elementName, searchElement) {
    if (searchElement == null) { searchElement = document; }
    if (elementName != "") {
        if (searchElement.getElementsByName) {
            return searchElement.getElementsByName(elementName);
        }
        if (searchElement.all) {
            return searchElement.all.getElementsByName(elementName);
        }
    }
    return null;
}

function Safe_GetElementByTagName(tagName, searchElement) {
    if (searchElement == null) { searchElement = document; }
    if (tagName != "") {
        if (searchElement.getElementsByTagName) { 
            return searchElement.getElementsByTagName(tagName);
        }
        if (searchElement.all && searchElement.all.tags) {
            return searchElement.all.tags(tagName);
        }
    }
    return null;
}

function Safe_GetElementById(elementID, searchElement) { 
    if (searchElement == null) { searchElement = document; }
    if (elementID != "") {
        if (typeof WebForm_GetElementById == 'function') { 
            element = WebForm_GetElementById(elementID); 
            if (element) { 
                return element; 
            } else { 
                return null; 
            }  
        } else if(eval(searchElement.getElementById(elementID))) { 
            return searchElement.getElementById(elementID); 
        } else { 
            return null; 
        } 
    } else { 
        return null; 
    } 
}

function addLoadEvent(func) { 
    var oldonload = window.onload; 
    if (typeof window.onload != 'function') {  
        window.onload = func;  
    } else {  
        window.onload = function() {  
            if (oldonload) {  
                oldonload();  
            }  
            func();  
        }  
    }  
}

function TextQuestion_MaxWords_CheckLength(elementID, textboxID, value, maxCount) {
    
    //Return System.Text.RegularExpressions.Regex.Matches(value, "(((^s*)*S+s+)|(S+))").Count()

    value = value.replace(/\s+/g, " ").trim();
    var words = value.split(" ");
    
    if (eval(document.getElementById('MaxWordsCount_Label_Count_' + elementID))) { document.getElementById('MaxWordsCount_Label_Count_' + elementID).innerHTML = words.length; }
    
    if (words.length >= maxCount) {
        document.getElementById(textboxID).value = value.trim();
        return false;
    } else {
        if (value == '' && eval(document.getElementById('MaxWordsCount_Label_Count_' + elementID))) { document.getElementById('MaxWordsCount_Label_Count_' + elementID).innerHTML = '0'; }
        return true;
    }
}

function TextQuestion_StripDoubleSpaces(textboxID, value) {
    document.getElementById(textboxID).value = value.replace(/[\s]{2}/g, " ").ltrim();
}

function Question_CheckOtherOption(theControl, QuestionID, OtherValue) {

    OtherValue =  ',' + OtherValue + ','; /* set searching string */
    var OtherQuestionContainer = Safe_GetElementById('OtherTextBox_' + QuestionID);
    if ( eval(OtherQuestionContainer) ) {
        var controlID, OtherQuestionTextbox = Safe_GetElementById('OtherTextBox_' + QuestionID + '_Textbox') ;
        switch (theControl.type) {
            case "select":
            case "select-one":
                if ( OtherValue.indexOf(','+theControl.options[theControl.selectedIndex].value+',') != -1  ) {
                    OtherQuestionContainer.style.display = '';
                    if (OtherValue.count(',') > 2 ) { OtherQuestionTextbox.value = ''; } // reset when changing between different other options
                    if (OtherQuestionTextbox.value == '') { OtherQuestionTextbox.focus(); }
                } else {
                    OtherQuestionContainer.style.display = 'none';
                    OtherQuestionTextbox.value = '';
                }
               break;
            case "checkbox":
                //TODO: add ability to have multiple other options
                if (theControl.checked == true && (theControl.value == OtherValue || theControl.value == 'on')) {
                    OtherQuestionContainer.style.display = '';
                    if (OtherQuestionTextbox.value == '') { OtherQuestionTextbox.focus(); }
                } else {
                    OtherQuestionContainer.style.display = 'none';
                    OtherQuestionTextbox.value = '';
                }
                break;
            default:
                var ShowOtherQuestion = false, QuestionElements = document.getElementsByName(theControl.id);            
                for(var i = 0; i < QuestionElements.length; i++) {
                    if (QuestionElements[i].checked == true && OtherValue.indexOf(','+QuestionElements[i].value+',') != -1) {
                        ShowOtherQuestion = true;
                        
                    }
                }            
                if (ShowOtherQuestion == true) {
                    OtherQuestionContainer.style.display = '';
                    if (OtherValue.count(',') > 2 ) { OtherQuestionTextbox.value = ''; } // reset when changing between different other options
                    if (OtherQuestionTextbox.value == '') { OtherQuestionTextbox.focus(); }
                } else {
                    OtherQuestionContainer.style.display = 'none';
                    OtherQuestionTextbox.value = '';
                }
                break;                
        }
    }
    return true;
}

// display popup window
function ModualWindow_IFrame_OpenWithParameters(theControl, buttonName, height, width, LoadingOverlayHide, OverlayClose) {

    if (typeof OverlayClose == 'undefined') { OverlayClose = false; }
            
    if (theControl.id != '') {
        $("a[id="+theControl.id+"]").colorbox({
            iframe:true, innerWidth: width, innerHeight: height, CUSTOM_buttonName: buttonName, CUSTOM_overlayHide: LoadingOverlayHide, overlayClose: OverlayClose
        });
    } else {
        alert("Control has no id.  Please add id for the controll");
    }
                    
    return false;
}

function ModualWindow_Inline_OpenWithParameters(theControl, height, width, OverlayClose) {

    if (typeof OverlayClose == 'undefined') { OverlayClose = false; }
    
    if (theControl.id != '') {  
        $("a[id="+theControl.id+"]").colorbox({
            inline:true, href: "#" + theControl.id + "_TextContainer", innerWidth: width, innerHeight: height, overlayClose: OverlayClose
        });
    } else {
        alert("Control has no id.  Please add id for the controll");
    }
                
    return false;
}

function CurrencyFormatted(amount, language) {
    if (language === undefined) { language = eval(Safe_GetElementById('Website_Language')) ? Safe_GetElementById('Website_Language').value : 'english'; }
    var s = ''; var i = parseFloat(amount); var minus = '';
    if(isNaN(i)) { i = 0.00; } if(i < 0) { minus = '-'; }
    i = Math.abs(i); i = parseInt((i + .005) * 100); i = i / 100;
    s = new String(i); if(s.indexOf('.') < 0) { s += '.00'; } if(s.indexOf('.') == (s.length - 2)) { s += '0'; } s = minus + s;
    return (language == 'french') ? CommaFormatted(s, language) + ' $' : '$' + CommaFormatted(s) ;
}

function CommaFormatted(amount, language) {
    if (language === undefined) { language = eval(Safe_GetElementById('Website_Language')) ? Safe_GetElementById('Website_Language').value : 'english'; }
    var delimiter = (language == 'french') ? " " : ","; var decimalDelimiter = (language == 'french') ? "," : ".";
    var a = amount.split('.',2); var d = a[1]; var i = parseInt(a[0]); var minus = '';
    if(isNaN(i)) { return ''; } if(i < 0) { minus = '-'; }
    i = Math.abs(i); var n = new String(i); a = [];
    while(n.length > 3) { var nn = n.substr(n.length-3); a.unshift(nn); n = n.substr(0,n.length-3); }
    if(n.length > 0) { a.unshift(n); } n = a.join(delimiter);
    if(d.length < 1) { amount = n; } else { amount = n + decimalDelimiter + d; }
    amount = minus + amount;
    return amount;
}


// function will clear input elements on each form  
function clearForms(){  
   // declare element type  
   var type = null;  
   // loop through forms on HTML page  
   for (var x=0; x<document.forms.length; x++){  
     // loop through each element on form  
     for (var y=0; y<document.forms[x].elements.length; y++){  
       // define element type  
       type = document.forms[x].elements[y].type  
       // alert before erasing form element  
       //alert('form='+x+' element='+y+' type='+type);  
       // switch on element type  
       switch(type){  
         case "text":  
         case "textarea":  
         case "password":  
         //case "hidden":  
           document.forms[x].elements[y].value = "";  
           break;  
        case "radio":  
         case "checkbox":  
           document.forms[x].elements[y].checked = "";  
           break;  
         case "select-one":  
           document.forms[x].elements[y].options[0].selected = true;  
           break;  
         case "select-multiple":  
           for (z=0; z<document.forms[x].elements[y].options.length; z++){  
             document.forms[x].elements[y].options[z].selected = false;  
           }  
         break;  
       }  
     }  
   }  
}  
