﻿var EF_prevElement; // previous element
var EF_prevColor; //prev row colour
var EF_prevClass;
var SHD_Common_Delete_Image_Path = ''; 

/////////////////////////////////
function isDefined(variable)
{
    return eval('(typeof(' + variable+ ') != "undefined");');
}
/////////////////////////////////

function SHDIsUsingCSSTab()
{
    if (isDefined('dnn') && dnn.getVar('SHDCSSTab') != "")
        return true;
    else
        return false;
}

//init function
function __BBPInit()
{
    jQuery(document).ready(function(){
        if (SHDIsUsingCSSTab && isDefined('dnn'))
            jQuery('.' + dnn.getVar('SHDCSSTab') + ' > ul > li > a').corner('top 6px');

    });
}


function EF_HighLightRow(oRow){  
	
	//if prevElement has been set, reset it to original settings (ie. deselect row)
	if (typeof(EF_prevElement) != 'undefined') {						
		//EF_prevElement.style.backgroundColor = EF_prevColor;
		EF_prevElement.className = EF_prevClass;
	} 
	
	EF_prevClass = oRow.className;
	EF_prevElement = oRow;

	oRow.className = 'GridHighlight'; //'#ffff99';
}


//BBPI Selector functions
function __BBPAddSelectorCode(fieldId, codeId, codeText)
{
    var obj = jQuery('#' + fieldId);
    var hiddenObj = jQuery('#' + fieldId + 'Hidden');
  
    //remove the possible indentation
    obj.addOption(codeId, ltrim(ltrim(ltrim(ltrim(codeText, '|'), '-'), '>'), ' '));
    
    
    __BBPPopulateHiddenSelectorCodes(obj, hiddenObj);
    __BBPShowSelectedIndustries(fieldId);
}

function __BBPRemoveSelectorCode(fieldId)
{
    var obj = jQuery('#' + fieldId);
    var hiddenObj = jQuery('#' + fieldId + 'Hidden');
    var selected = obj.selectedValues();
    obj.removeOption(selected);
    
    __BBPPopulateHiddenSelectorCodes(obj, hiddenObj);
    __BBPShowSelectedIndustries(fieldId);
}


function __BBPPopulateHiddenSelectorCodes(listObj, hiddenObj)
{
    var result = '';
    var options = listObj.children('option');
        
    for (var i = 0; i < options.length; i++)
    {
        if (i == options.length - 1)
            result += options[i].text + '~' + options[i].value;
        else
            result += options[i].text + '~' + options[i].value + '^';
    }
        
    hiddenObj.val(result);
    
    var headerId = hiddenObj.attr('id').replace('lstCodesHidden', 'lblHeader');   
    var headerObj = jQuery('#' + headerId);
    if (headerObj != null)
    {
        if (result == '')
            headerObj.html('No Industry selected&nbsp;');
        else
            headerObj.html('Selected Industries&nbsp;');
    }
}

function __BBPShowSelectedIndustries(listObjId)
{
    var listObj = jQuery('#' + listObjId);
    var panelObj = jQuery('#' + listObj.attr('id').replace('lstCodes', 'lstHolder'));
    var options = listObj.children('option');
    var allHtml = ''
        
    for (var i = 0; i < options.length; i++)
    {
        var liId = listObj.attr('id') + "_" + i;
        
        var html = "<li id='" + liId + "' index='" + i + "'>" + options[i].text + "&nbsp;<img src='" + SHD_Common_Delete_Image_Path + "' onclick='__BBPRemoveIndustry(\"" + liId + "\", \"" + listObj.attr('id') + "\")' /> </li>"
        //alert(html);
        allHtml += '\n' + html;
    }      
    
    panelObj.html(allHtml);
}


function __BBPRemoveIndustry(liId, listObjId)
{
    var listObj = jQuery('#' + listObjId);
    var panelObj = jQuery('#' + listObj.attr('id').replace('lstCodes', 'lstHolder'));
    var options = listObj.children('option');
    var liObj = jQuery('#' + liId);
    var index = liObj.attr('index');
    
    //remove list obj from UL
    liObj.remove();
    
    //set selected
    for (var i = 0; i < options.length; i++)
    {
        if (i == index)
            options[i].selected = true
    } 
    
    //remove from list
    __BBPRemoveSelectorCode(listObjId);
}


function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function __AddLocation(countyId, stateId, panelId, hiddenId, imgPath)
{
    var county = jQuery('#' + countyId);
    var state = jQuery('#' + stateId);
    var panel = jQuery('#' + panelId);
    var hidden = jQuery('#' + hiddenId);
    var liId = panelId + "_" + state.val().replace(' ', '') + county.val().replace(/ /g, '') + hidden.children().length + 1;
    var hiddenText = hidden.val();
    
    var value = state.val() + "~" + county.val();
    var text = state.selectedTexts() + ", " + county.selectedTexts();
    
    var html = "<li id='" + liId + "' code='" + value + "'>" + text + "&nbsp;<img src='" + imgPath + "' onclick='__RemoveLocation(\"" + liId + "\", \"" + hiddenId + "\")' /> </li>"
    //alert(html);
    //add to list
    html = panel.html() + html;
    
    //add to hidden code
    if (hiddenText == "")
        hiddenText = value;
    else
        hiddenText += '^' + value;
        
    //alert(hiddenText);
    
    //save hidden
    hidden.val(hiddenText)
    //append to list
    panel.html(html);
}

function __RemoveLocation(objId, hiddenId)
{
    var obj = jQuery('#' + objId);
    var hidden = jQuery('#' + hiddenId);
    
    var code = obj.attr('code');
    var hiddenText = hidden.val();
    
    hiddenText = hiddenText.replace("^" + code, "").replace(code + "^", "").replace(code, "");
    //remove obj from list panel
    obj.remove();
    //save to hidden object
    hidden.val(hiddenText)
}

function __AddCategory(countyId, stateId, panelId, hiddenId, imgPath, allowEmpty)
{
    var county = jQuery('#' + countyId);
    var state = jQuery('#' + stateId);
    var panel = jQuery('#' + panelId);
    var hidden = jQuery('#' + hiddenId);
    
    //do not allow add if is not empty specified
    if (!allowEmpty && (state.val() == null || state.val() == ''))
        return false;
    
    var liId = panelId + "_" + state.val().replace(' ', '') + county.val().replace(/ /g, '') + hidden.children().length + 1;
    var hiddenText = hidden.val();
      
    var value = state.val() + "~" + county.val();
    
    if (hiddenText.indexOf(value) > -1)
        return false;
   
    var text = ""
    if (county.selectedTexts() == "")
        text = state.selectedTexts();
    else
        text = state.selectedTexts() + ", " + county.selectedTexts();
    
    var html = "<li id='" + liId + "' code='" + value + "'>" + text + "&nbsp;<img src='" + imgPath + "' onclick='__RemoveLocation(\"" + liId + "\", \"" + hiddenId + "\")' /> </li>"
    //alert(html);
    //add to list
    html = panel.html() + html;
    
    //add to hidden code
    if (hiddenText == "")
        hiddenText = value;
    else
        hiddenText += '^' + value;
        
    //save hidden
    hidden.val(hiddenText)
    //append to list
    panel.html(html);

}

function __LoadCountiesWS(stateId, countyId, firstEntryBlank)
{
    var state = jQuery("#" + stateId);
    var county = jQuery("#" + countyId);

    var fullWSPath = dnn.getVar('SHDWSPath') + "/SHDWS.asmx/GetCountiesFromState";
    var postData = "{'key':'" + dnn.getVar('SHDWSAuthId') + "'"; 
    postData += ",'state':'" + state.val() + "'";   
    postData += ",'firstEntryBlank':'" + firstEntryBlank + "'";   
    postData += ",'ctrlid':'" + countyId + "'";   
    postData += "}";   
    
    
    jQuery.ajax({
    type: "POST",
    url: fullWSPath,
    data: postData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        if (msg != '')
        {
            var message = '';
            if (typeof msg == 'object')
                message = msg['d'];
            else
                message = msg;
            var parts = message.split('|');

            if (parts.length > 1)
            {
                var county = jQuery("#" + parts[0]);
                
                if (county != null)
                {
                
                    county.removeOption(/./);
                    
                    var pairs = parts[1].split('^');
                    var containsAllCounties = false;
                    
                    for (i = 0; i < pairs.length; i++)
                    {
                        var keyvalue = pairs[i].split('~');
                        county.addOption(keyvalue[0], keyvalue[1]);
                        
                        if (keyvalue[0] == 'All Counties')
                            containsAllCounties = true;
                    }
                    if (containsAllCounties)
                        county.selectOptions('All Counties');
                    else
                        county.selectOptions('');
                }
            }
        }
    }
    });
}

function __LoadSubCategoriesWS(mainId, subId, firstEntryBlank)
{
    var main = jQuery("#" + mainId);
    var sub = jQuery("#" + subId);

    var fullWSPath = dnn.getVar('SHDWSPath') + "/SHDWS.asmx/GetSubCategories";
    var postData = "{'key':'" + dnn.getVar('SHDWSAuthId') + "'"; 
    postData += ",'parentId':'" + main.val() + "'";   
    postData += ",'firstEntryBlank':'" + firstEntryBlank + "'";   
    postData += ",'ctrlid':'" + subId + "'";   
    postData += "}";   
    
    
    jQuery.ajax({
    type: "POST",
    url: fullWSPath,
    data: postData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        if (msg != '')
        {
            var message = '';
            if (typeof msg == 'object')
                message = msg['d'];
            else
                message = msg;
            var parts = message.split('|');

            if (parts.length > 1)
            {
                var county = jQuery("#" + parts[0]);
                
                if (county != null)
                {
                
                    county.removeOption(/./);
                    county.removeOption('');
                    
                    var pairs = parts[1].split('^');
                    var containsAllSubCategories = false;
                    var firstItemValue = '';

                    for (i = 0; i < pairs.length; i++)
                    {
                        var keyvalue = pairs[i].split('~');
                        county.addOption(keyvalue[0], keyvalue[1]);
                        
                        if (i == 0)
                           firstItemValue = keyvalue[0];
                           
                        //if (keyvalue[0] == 'All Sub Categories')
                        //    containsAllSubCategories = true;
                    }
                    
                    county.selectOptions(firstItemValue);
                    
                    //if (containsAllSubCategories)
                    //    county.selectOptions('All Sub Categories');
                    //else
                    //    county.selectOptions('');
                }
            }
        }
    }
    });
}


function __BBPAddContact(hiddenId, labelId, codeId, codeText)
{
    var hidden = jQuery("#" + hiddenId);
    var label = jQuery("#" + labelId);
    
    if (hidden != null)
        hidden.val(codeId);
        
    if (label != null)
        label.html(codeText);
}


