﻿//******************************************//
//             全局公共函数及成员           //
//******************************************//

//当前路径
var ApplicationPath = '';
//获取当前文档指定ID的HTML控件
function GetControl(id)
{
    return document.getElementById(id);
}

//获取当前页面父页面的Control
function GetParentControl(id)
{
    return parent.document.getElementById(id);
}

//创建CheckBox的HTML(或RadioButton)
function BuildCheckBoxHTML(id, val, txt, clickHandle, chkStatus, isRadio, groupName)
{
    var html = '';
    
    if(isRadio != null && isRadio == true)
    {
        html = "<input id='"+ id + "' type='radio' value='"+ val +"' onclick=\""+ clickHandle + "\" name='" + groupName + "'";
        if( chkStatus == true )
        {
            html += ' checked ';
        }
        html += " />" + txt;
    }
    else
    {
        html = "<input id='"+ id + "'  type='checkbox' value='"+ val + "' onclick=\""+ clickHandle + "\"";
        if( chkStatus == true )
        {
            html += ' checked ';
        }
        html += " />" + txt;
    }
    return html;
}

//替换字符串
function ReplaceString(s, oldStr, newStr)
{
    while(s.indexOf(oldStr) >= 0)
    {
        s = s.replace(oldStr, newStr);
    }
    return s;
}

//映射目标路径
function MapPath(path)
{
    var val = ApplicationPath + path;
    return val;
}

//判断一个值是否在一个数组里
function Contains(ary, val)
{
    var i;
    for(i = 0; i < ary.length; i++)
    {
        if(ary[i] == val)
        {
            return true;
        }
    }
    
    return false;
}

//******************************************//
//              控制行业                    //
//******************************************//
//行业Div名称
var IndustryLayerDivName = 'divIndustryLayer';
//行业Layer表格名称
var IndustryLayerTableName = 'tableIndustryLayer';
//已选择行业表格名称
var SelectedIndustryLayerTableName = 'tableSelectedIndustryLayer';
//行业底层表格名称
var IndustryBottomTable = 'tableIndustryBottomTable';
//行业底层Iframe名称
var IndustryLayerIframeName = 'ifrmIndustry';
//行业Layer表格列数
var IndustryLayerTableColumnCount = 3;
//行业选择数量限制
var SelectedIndustryLimit = 5;
//行业ID字符串存储隐藏TextBox
var SelectedIndustryIDsInputID;
//行业Div是否已经初始化
var IndustryIsInit = false;
//所选行业DivID
var SelectedIndustryDivID = 'divSelectedIndustry';
//不限CheckBoxID
var UnlimitCheckBoxID = 'chkIndustryUnlimit';
//不限CheckBoxID的Value
var UnlimitCheckBoxValue = '255';
//不限的TR的ID
var IndustryUnlimitTRID = 'trIndustryUnlimit';
//显示行业最大数量Span的ID
var IndustryMaxCountSpanID = 'spIndustryMaxCount';
//是否显示不限
var IsShowIndustryUnlimited;
//用于保存客户端控件ID的InputID
var IndustryClientControlInputID;
//已选行业临时存储数组
var TempSelectedIndustryList = new Array();
//行业是否单选(0=否,1=是)
var IsIndustrySelectSingle = 0;
//行业最大选择项提示Lable的ID(多选)
var IndustryMaxLableID = 'h3IndustryMaxLable';
//行业最大选择项提示Lable的ID(单选)
var IndustryMaxLableSingleID = 'h3IndustryMaxLableSingle';
//行业代码类型(0="新版本",1="老版本",2="新老混合版本")
var IndustryDataVersion = 0;

//初始化临时保存IndustryID数组
function InitTempSelectedIndustryList()
{
    TempSelectedIndustryList = new Array();
    var selectedIDs = GetControl(SelectedIndustryIDsInputID).value.split(',');
    
    var i;
    for(i = 0; i < selectedIDs.length; i++)
    {
        //检查传入的ID是否有效
        if( selectedIDs[i] != '')
        {
            AddTempSelectedIndustryList(selectedIDs[i]);
        }
    }
}

//获取所选择的行业ID数组
function GetSelectedIndustryIDs()
{
    if(TempSelectedIndustryList == null)
    {
        TempSelectedIndustryList = new Array();
    }
    
    return TempSelectedIndustryList;
}

//插入临时行业数组
function AddTempSelectedIndustryList(id)
{
    //确认临时数组内没有要插入的ID
    var i;
    var flag = false;
    for(i = 0; i < TempSelectedIndustryList.length; i++)
    {
        if(TempSelectedIndustryList[i] == id)
        {
            flag = true;
            break;
        }
    }
    
    if(flag == false)
    {
        TempSelectedIndustryList[TempSelectedIndustryList.length] = id;
    }
}

//删除行业
function DeleleIndustry(selDivID, ClientControlInputID)
{
    RecoverIndustryClientControlID(ClientControlInputID);
    
    //删除所选项
    var divId = selDivID;
    var divSelected = GetControl(divId);
    var OccDiv = divSelected.parentNode;
    
    OccDiv.removeChild(divSelected);
    
    InitTempSelectedIndustryList();
    var id = selDivID.split('_')[2];
    RemoveTempSelectedIndustryList(id);
    
    //刷新Hidden
    RefreshSelectedIndustry();
}

//从CheckBox组刷新行业选择
function RefreshSelectedIndustry()
{
    //刷新已选择的ID字符串TextBox
    var i;
    var tbIds = GetControl(SelectedIndustryIDsInputID);
    tbIds.value = '';
    
    var ids = GetSelectedIndustryIDs();
    for(i = 0; i < ids.length; i++)
    {
        tbIds.value += ids[i];
        if( i < ids.length - 1 )
        {
            tbIds.value += ',';
        }
    }
}

//从临时行业数组删除目标ID
function RemoveTempSelectedIndustryList(id)
{
    var list = new Array();
    var i;
    var n = 0;
    for(i = 0; i<TempSelectedIndustryList.length; i++)
    {
        if(TempSelectedIndustryList[i] != id)
        {
            list[n] = TempSelectedIndustryList[i];
            n++;
        }
    }
    
    TempSelectedIndustryList = list;
}
//刷新底层挡控件的IFrame位置
function RefreshIndustryBottomIFrame()
{
}

//保存客户端控件ID
function SaveIndustryClientControlID(ClientControlInputID)
{
    var clientID = GetControl(ClientControlInputID);
    clientID.value = '';
    clientID.value += SelectedIndustryIDsInputID;
    clientID.value += ',';
    clientID.value += SelectedIndustryDivID;
    clientID.value += ',';
    clientID.value += SelectedIndustryLimit;
    clientID.value += ',';
    clientID.value += IsShowIndustryUnlimited;
    clientID.value += ',';
    clientID.value += IsIndustrySelectSingle;
    clientID.value += ',';
    clientID.value += IndustryDataVersion;
}

//恢复客户端控件ID
function RecoverIndustryClientControlID(ClientControlInputID)
{
    var clientID = GetControl(ClientControlInputID);
    var ids = clientID.value.split(',');
    
    SelectedIndustryIDsInputID = ids[0];
    SelectedIndustryDivID = ids[1];
    SelectedIndustryLimit = ids[2];
    IsShowIndustryUnlimited = ids[3];
    IsIndustrySelectSingle = ids[4];
    IndustryDataVersion = ids[5];
    
    if(GetControl(SelectedIndustryIDsInputID).value != '')
    {
        TempSelectedIndustryList = GetControl(SelectedIndustryIDsInputID).value.split(',');
    }
    else
    {
        TempSelectedIndustryList = new Array();
    }
}

//从客户端调用获取所选行业
function GetIndustryIDs(layerID)
{
    //恢复客户端状态
    var clientControlInputID = layerID + '_tbClientControlID';
    clientControlInputID = ReplaceString(clientControlInputID, '$', '_');
    RecoverIndustryClientControlID(clientControlInputID);

    var clientInputID = layerID + '_tbSelectedIndustry';
    clientInputID = ReplaceString(clientInputID, '$', '_');
    var input = GetControl(clientInputID);
    
    if(input.value == '')
    {
        return '0';
    }
    else
    {
        return input.value;
    }
}

//打开职业类别弹出窗口
function OpenIndustryLayerWindow(clientInputID, dataVersion, isUnlimited,parentID,ValueID)
{
    var url = MapPath('../Common/IndustryPop.htm?clientInputID=' + clientInputID + '&ver=' + dataVersion + '&unlimited' + isUnlimited +'&locklayer=' + parentID +'&datalayer=' + ValueID);
    //打开职位列表层遮罩隐藏层效果，返回值在另外的里面处理
    OpenDataLayer(url,parentID,ValueID);
}