//////////calendar///////////
function GreaterthanDate(start, end) {
    return Date.parse(start) >= Date.parse(end);
}
function EqualDate(start, end) {
    return Date.parse(start) == Date.parse(end);
}
$(function() {
     //system data type
     $(".div-left-menu-operation").find("a").each(function(index,elm){
         if(index==0) 
         {
            $(this).parent().find("span").removeClass("spanSelected");   
            $(this).find("span").addClass("spanSelected");  
         } 
         $(this).click(function(){
             $(this).parent().find("span").removeClass("spanSelected");   
             $(this).find("span").addClass("spanSelected");  
         }); 
     });
    //文本样式
    $(":text").each(function() {
        $(this).focus(function() { $(this).css("borderColor", "#7EADD9"); })
               .blur(function() { $(this).css("borderColor", "#B5B8C8") })
    })
    $("textarea").each(function() {
        $(this).focus(function() { $(this).css("borderColor", "#7EADD9"); })
               .blur(function() { $(this).css("borderColor", "#B5B8C8") })
    })
    //按键样式
    $(":button").each(function() {

        $(this).mouseover(function() { $(this).addClass("hover"); })
               .mouseout(function() { $(this).removeClass("hover"); })
    })
});
function GetSplitTableBySplitStr(strOriginal, split,pageSize,PageIndex,name) 
{
    var objArray = [];
    if (strOriginal != "") objArray = strOriginal.split(split);
    var fieldsCount = objArray.length;
    if (fieldsCount == 0) 
    {
        return '[{"result" :"0","returnval" :"暂时没有结果"}]';
    }
    var start = ((PageIndex <= 0 ? 1 : PageIndex)-1) * pageSize;
    var end = (start + pageSize) > fieldsCount ? fieldsCount : (start + pageSize);
    if (start > fieldsCount) { start = 0; end = (start + pageSize) > fieldsCount ? fieldsCount : (start + pageSize); }
    var result = [];
    result.push('[{"result" :"1","returnval" :"操作成功", "pageIndex":"' + PageIndex + '", "recordcount":"' + fieldsCount + '","table":[');
    for (n = start; n < end; n++) {

        if (n > start)
            result.push(",");
                 result.push('{');
                result.push('"' + "ID" + '"' + ':"' + n + '"');
                result.push(",");
                result.push('"' + name + '"' + ':"' + objArray[n] + '"');
                result.push(",");
                result.push('"' + "AllCount" + '"' + ':"' + fieldsCount + '"');
                result.push('}')  
        }
      
        result.push(']}]');
        //alert(result.join(""));
    return result.join("");
  
}
function showTime(startTime,endTime,userTime,timeWidth) {
   
    $("#" + startTime).datepicker({ picker: "<img class='picker' align='middle' src='../../themes/shared/images/s.gif' alt=''/>", applyrule: rule,isUserTime:userTime,width:timeWidth});
    $("#" + endTime).datepicker({ picker: "<img class='picker'align='middle' src='../../themes/shared/images/s.gif' alt=''/>", applyrule: rule,isUserTime:userTime,width:timeWidth});          
        function rule(id) {
            if (id == startTime) { //因为这是多个input框一起设置为日期选择框所以要区分一下是哪个展开了。
                var v = $("#"+endTime).val();
                if (v == "") {
                    return null;
                }
                else {
                    //获取结束时间，开始时间不能大于结束时间
                    var d = v.match(/^(\d{1,4})(-|\/|.)(\d{1,2})\2(\d{1,2})$/);
                    if (d != null) {
                        var nd = new Date(parseInt(d[1], 10), parseInt(d[3], 10) - 1, parseInt(d[4], 10));
                        return { enddate: nd};
                    }
                    else {
                        return null;
                    }
                }
            }
            else {
                var v = $("#"+startTime).val();
                if (v == "") {
                    return null;
                }
                else {
                   //获取开始时间，结束时间不能大于开始时间
                    var d = v.match(/^(\d{1,4})(-|\/|.)(\d{1,2})\2(\d{1,2})$/);
                    if (d != null) {
                        var nd = new Date(parseInt(d[1], 10), parseInt(d[3], 10) - 1, parseInt(d[4], 10));
                        return { startdate: nd };
                    }
                    else {
                        return null;
                    }
                }

            }
        }
}
//根据ID设置时间控件
function showRemindTime(time,userTime,timeWidth)
{
    $("#"+time).datepicker({ picker: "<img class='picker' align='middle' src='/themes/shared/images/s.gif' alt=''/>" ,isUserTime:userTime,width:timeWidth});
}
//根据Class设置时间控件
function showRemindTimeByList(time,userTime,timeWidth)
{
    $("."+time).datepicker({ picker: "<img class='picker' align='middle' src='/themes/shared/images/s.gif' alt=''/>" ,isUserTime:userTime,width:timeWidth});
}
//获取参数
function q(item){
	var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
    return svalue ? svalue[1] : svalue;

}
function query(strUrl,key) {
    var svalue =strUrl.match(new RegExp("[\?\&]" + key + "=([^\&]*)(\&?)", "i"));
    return svalue ? svalue[1] : svalue;
}
//根据用户ID
var userName = "admin";
var userID = "1";
var departmentID;
var departmentName;
//var topWin = (function(p, c) {
//    while (p != c) {
//        c = p
//        p = p.parent
//    }
//    return c
//})(window.parent, window);
//var stringUrl;
$(function() {
//    stringUrl =ShowWindow.opener.document(-1).location.href;
/*2011-08-29 由于影响获取当前用户信息，所以注释*/
/*
    var LoginUser = App.CurrentUser();//{};
//    LoginUser.UserID = query(stringUrl, "uid") == null ? 1 : query(stringUrl, "uid");
//    LoginUser.UserName = query(stringUrl, "uname") == null ? "IKE" : query(stringUrl, "uname");
//    LoginUser.DepartmentID = 1;
//    LoginUser.DepartmentName = "IKE";
    userName = LoginUser.UserName;
    userID = LoginUser.UserID;
    departmentID=LoginUser.DepartmentID;
    departmentName=LoginUser.DepartmentName;
*/
})

/*************************************************** ▌页面通用类型枚举 ▌************************************************/
var PageTypeEnum = 
{
   CommonArchiveTypeEditPage: "CommonArchiveTypeEditPage",   //通用类型档案编辑页面
   CommonArchiveEditPage: "CommonArchiveEditPage"            //通用单个档案编辑页面
}
/*************************************************** ▌根据状态加载控件的可见性 ▌****************************************/
//(参数1)：controlStringsObj [控件状态数组，声明格式:var arr=new Array();arr["New"]="btnSave,visible,disabled|btnModify,unvisible,enabled";]
//(参数1)：pageTypeEnum [页面类型枚举]
//创建：Charles [2010/10/23]
function LoadStateControls(controlArray,pageTypeEnum)
{ 
    switch(pageTypeEnum)
    {
        case PageTypeEnum.CommonArchiveTypeEditPage.toString():
            if(pageState=="New")
            {
                $("#Editbtn,#Addbtn,#Deletebtn,#UserInfomation").hide();
                $("#Savebtn,#Cancelbtn,#serachType").show();
                $("#frScheduleType").resetForm();
                $("#frScheduleType input").attr("disabled","");
                $("#htxtDescription").attr("disabled","");
            }
            else if(pageState=="Update")
            {  
                $("#Editbtn,#Addbtn").hide();
                $("#Savebtn,#Cancelbtn,#serachType").show();
                $("#frScheduleType input").attr("disabled","");
                $("#htxtDescription").attr("disabled","");
            }
            else if(pageState=="View")
            {
                $("#Deletebtn,#Editbtn,#Addbtn").show();
                $("#Savebtn,#Cancelbtn,#serachType").hide();
                $("#frScheduleType input").attr("disabled","disabled");
                $("#htxtDescription").attr("disabled","disabled");
            }
            break
        case PageTypeEnum.CommonArchiveTypeEditPage:
            if(pageState=="New")
            {
                $("#Editbtn,#Addbtn,#Deletebtn,#UserInfomation").hide();
                $("#Savebtn,#Cancelbtn,#serachType").show();
                $("#frScheduleType").resetForm();
                $("#frScheduleType input").attr("disabled","");
                $("#htxtDescription").attr("disabled","");
            }
            else if(pageState=="Update")
            {
                $("#Editbtn,#Addbtn").hide();
                $("#Savebtn,#Cancelbtn,#serachType").show();
                $("#frScheduleType input").attr("disabled","");
                $("#htxtDescription").attr("disabled","");
            }
            else if(pageState=="View")
            {
                $("#Deletebtn,#Editbtn,#Addbtn").show();
                $("#Savebtn,#Cancelbtn,#serachType").hide();
                $("#frScheduleType input").attr("disabled","disabled");
                $("#htxtDescription").attr("disabled","disabled");
            }
            break
        default:
            if (pageState == "New") {
                SetControlsState(controlArray["New"]);
            }
            else if (pageState == "Update") {
               
                SetControlsState(controlArray["Update"]);
            }
            else if (pageState == "View") {
                SetControlsState(controlArray["View"]);
            }
            else if (pageState == "Customer") {
                SetControlsState(controlArray["Customer"]);
            }
    }
}
//解释控件串的状态
//参数格式：btnSave,visible,disabled|btnModify,unvisible,enabled
function SetControlsState(controlStrings)
{
    var controlState = controlStrings.split('|');
    for(var i=0;i<controlState.length;i++)
    {
        if(controlState[i].split(',')[1]=="visible") {
           
          $("#" + controlState[i].split(',')[0]).show();
          $("#s_" + controlState[i].split(',')[0]).show();
        }
        else
        {
            $("#"+controlState[i].split(',')[0]).hide();
            $("#s_"+controlState[i].split(',')[0]).hide();
        }
        if(controlState[i].split(',')[2]=="disabled")
        {
            $("#"+controlState[i].split(',')[0]).attr("disabled","disabled");
        }
        else
        {
            $("#"+controlState[i].split(',')[0]).attr("disabled","");
        }
        if(controlState[i].split(',')[1]=="hideClass")
        {
            $(controlState[i].split(',')[0]).hide();
        }
        else
        {
            $(controlState[i].split(',')[0]).show();
        }
    }
}
/*************************************************** ▌HashTable类的实现 ▌***********************************************/
//创建：Charles [2010/10/23] 
/*该类的调用方法: 
    //实例化一个自定义的哈希表类
    var hashTable = new Hashtable();
    hashTable.put(0,'abc');     //0为key, 'abc'为value
    hashTable.put(1,'123');
    hashTable.put(2,'88a');
    hashTable.put(3,'88a');
    
    //遍历hashtable, 相当于c#和java中的foreach
    for (var key in hashTable.keys()){ 
        alert(hashTable.get(key));      //按key遍历value
    }
    
    //遍历hashtable, 相当于c#和java中的foreach
    for (var key in hashTable.hashtable)){  
        alert(hashTable.get(key));          //按key遍历value
    }
    alert(hashTable.containsKey(1));        //返回true
    alert(hashTable.containsKey(4));        //因为不存在key为4的，返回false
    alert(hashTable.containsValue('888'));  //返回true
    alert(hashTable.containsValue('mobidogs')); //因为不存在value为'mobidogs'的，返回false

    hashTable.remove(1);                //移除key为1的元素
    alert(hashTable.containsKey(1));    //因为key为1的元素已被上行的reomve()方法移除，所以返回false
*/
function Hashtable(){
    this.clear = hashtable_clear;
    this.containsKey = hashtable_containsKey;
    this.containsValue = hashtable_containsValue;
    this.get = hashtable_get;
    this.isEmpty = hashtable_isEmpty;
    this.keys = hashtable_keys;
    this.put = hashtable_put;
    this.remove = hashtable_remove;
    this.size = hashtable_size;
    this.toString = hashtable_toString;
    this.values = hashtable_values;
    this.hashtable = new Array();
}                
function hashtable_clear(){
    this.hashtable = new Array();
}
function hashtable_containsKey(key){
    var exists = false;
    for (var i in this.hashtable) {
        if (i == key && this.hashtable[i] != null) {
            exists = true;
            break;
        }
    }
    return exists;
}
function hashtable_containsValue(value){
    var contains = false;
    if (value != null) {
        for (var i in this.hashtable) {
            if (this.hashtable[i] == value) {
                contains = true;
                break;
            }
        }
    }
    return contains;
}
function hashtable_get(key){
    return this.hashtable[key];
}
function hashtable_isEmpty(){
    return (this.size == 0) ? true : false;
}
function hashtable_keys(){
    var keys = new Array();
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null) 
            keys.push(i);
    }
    return keys;
}
function hashtable_put(key, value){
    if (key == null || value == null) {
        throw 'NullPointerException {' + key + '},{' + value + '}';
    }else{
        this.hashtable[key] = value;
    }
}
function hashtable_remove(key){
    var rtn = this.hashtable[key];
    //this.hashtable[key] =null; 
    this.hashtable.splice(key,1);

    return rtn;
}
function hashtable_size(){
    var size = 0;
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null) 
            size ++;
    }
    return size;
}
function hashtable_toString(){
    var result = '';
    for (var i in this.hashtable)
    {      
        if (this.hashtable[i] != null) 
            result += '{' + i + '},{' + this.hashtable[i] + '}\n';   
    }
    return result;
}
function hashtable_values(){
    var values = new Array();
    for (var i in this.hashtable) {
        if (this.hashtable[i] != null) 
            values.push(this.hashtable[i]);
    }
    return values;
}

//禁用回退和刷新功能 
function forbidBackSpace() 
{ //BackSpace //CtrlN,CtrlR //F5 
    if (((event.keyCode == 8) &&                                                    
         ((event.srcElement.type != "text" && 
         event.srcElement.type != "textarea" && 
         event.srcElement.type != "password") || 
         event.srcElement.readOnly == true)) || 
        ((event.ctrlKey) && ((event.keyCode == 78) || (event.keyCode == 82)) ) ||(event.keyCode == 116) 
        ) {                                                   
        event.keyCode = 0; 
        event.returnValue = false; 
    } 
}

function format(n)//格式化数字为两位字符表示
{
    var m = new String();
    var tmp = new String(n);
    if (n < 10 && tmp.length < 2) {
        m = "0" + n;
    }
    else {
        m = n;
    }
    return m;

}
//时间差
function timeSpace(strStartTime,h,method)
{
    var startTime=new Date(strStartTime); 
    var startTimeValue=startTime.valueOf();
    startTimeValue=method=="add"?startTimeValue+(h*3600*1000):startTimeValue-(h*3600*1000);     //可加可減 
    startTimeValue=new Date(startTimeValue);
    var strTime=startTimeValue.getFullYear()+ '-'+(format(startTimeValue.getMonth()+1))+ '-'+format(startTimeValue.getDate())+ ' ' 
    strTime+=format(startTimeValue.getHours())+ ':'+format(startTimeValue.getMinutes())+ ':'+format(startTimeValue.getSeconds()); 
    return strTime; 
}
//页面加载时显示默认短时间
function defaultTime(startTime,endTime)
{
    if(endTime!="")
    {
        $("#"+endTime).val(new Date().Format("yyyy-MM-dd"));
        var beginTime=timeSpace($("#"+endTime).val().replace("-","/"),24*7,"minus").toString().substring(0,10);
        $("#"+startTime).val(beginTime);
    }
    else
    {
        $("#"+startTime).val(new Date().Format("yyyy-MM-dd"));
    }
}
//页面加载时显示默认短时间
function customerDefaultTime(startTime,endTime,timePlace)
{
    if(endTime!="")
    {
        $("#"+endTime).val(new Date().Format("yyyy-MM-dd"));
        var beginTime=timeSpace($("#"+endTime).val().replace("-","/"),timePlace,"minus").toString().substring(0,10);
        $("#"+startTime).val(beginTime);
    }
    else
    {
        $("#"+startTime).val(new Date().Format("yyyy-MM-dd"));
    }
}
//页面加载时显示默认长时间
function defaultLongTime(startTime,endTime)
{
    $("#"+endTime).val(new Date().Format("yyyy-MM-dd hh:mm:ss"));
    var beginTime=timeSpace($("#"+endTime).val().replace("-","/"),24*7,"minus");
    $("#"+startTime).val(beginTime);
}
//输入框只允许输入数字或小数点
function keyCodeUp(obj)
{
    //先把非数字的都替换掉，除了数字和.
    obj.value = obj.value.replace(/[^\d.]/g,"");
    //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g,"");
    //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g,".");
    //保证.只出现一次，而不能出现两次以上
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}
//时间格式差
function TimeFormatSpace(currentTime,timeWay,timeSize,operateWay)
{
    var nowTime=DBReader.ServerTime(currentTime);
    var nowYear=nowTime.getYear();
    var nowMonth=nowTime.getMonth();
    var nowDate=nowTime.getDate();
    var currentYear=nowYear;
    var currentMonth=nowMonth;
    var currentDate=nowDate;
    switch(timeWay)
    {
            case "year":
            {
                if(operateWay=="minus")
                {
                    currentYear=nowYear-timeSize;
                }
                else if(operateWay=="add")
                {
                    currentYear=nowYear+timeSize;
                }
            }
            break;
            case "month":
                if(operateWay=="minus")
                {
                    currentMonth=nowMonth-timeSize;
                }
                else if(operateWay=="add")
                {
                    currentMonth=nowMonth+timeSize;
                }
            break;
            case "day":
                if(operateWay=="minus")
                {
                    currentDate=nowDate-timeSize;
                }
                else if(operateWay=="add")
                {
                    currentDate=nowDate+timeSize;
                }
            break;
    }
    if(currentMonth.toString().length==1)
    {
        currentMonth="0"+currentMonth;
    }
    if(currentDate.toString().length==1)
    {
        currentDate="0"+currentDate;
    }
    return currentYear+"-"+currentMonth+"-"+currentDate;
}