﻿/// <reference path="jquery-1.4.1-vsdoc.js" />

var isIe = (window.ActiveXObject) ? true : false;
var pageLinkUrl = "";
var ajaxUrl = "";
var IntNull = -999999;
var ajaxSuccess = "bohinetsuccess";
var ajaxError = "bohineterror";
var ajaxExist = "bohinetExist";
var ajaxNoResult = "bohinetNotExist";
var stopTime;
var nowTime;
        
//**根据ID获取对象
function g(nodeId) {
    //alert("yyyy");
    var $abj = $("#" + nodeId);
    //alert($abj);
    if ($abj.length > 0) { return $abj[0]; }
    else { return null }
    //return document.getElementById(nodeId);
}
//**根据对象赋植内容
function setInnerHtml(objName, dcontent) {
    if (g(objName) != null) {
        $(objName).html(dcontent);
        //g(objName).innerHTML = dcontent; 
    }
}

function checknumber(String) {
    //alert(String);
    if (trimTxt(String) == "") {
        return false;
    }
    var Letters = "1234567890";
    var i;
    var c;
    for (i = 0; i < String.length; i++) {
        c = String.charAt(i);
        if (Letters.indexOf(c) == -1) {
            return false;
        }
    }
    return true;
}

function trimTxt(txt) {
    return txt.replace(/(^\s*)|(\s*$)/g, "");
}
//**调用Ajax简化函数
function bohinetAjaxWholeUrlPost(wholeUrl, requst, requstData, onSuccessRunCode) {
    bohinetAjaxBase(wholeUrl, "POST", requst, requstData, false, null, null, onSuccessRunCode, null);
}

function bohinetAjaxPost(requst, requstData, onSuccessRunCode) {
    bohinetAjaxBase(null, "POST", requst, requstData, false, null, null, onSuccessRunCode, null);
}

function bohinetAjaxLoad(requst,requestID, requstData,onSuccessRunCode) {
    bohinetAjaxBaseLoad(null, "POST", requst,requestID,requstData, false, null, null, onSuccessRunCode, null);
}

//**Load**/
function bohinetAjaxBaseLoad(wholeUrl, postType, requst,requestID, requstData, isXml, onPrevRunCode, onErrorRunCade, onSuccessRunCode, onCompleteRunCode) {
    //**xml格式暂时没有完成,wood 2010-03-26
    var requestUrl = "";
    //alert(ajaxUrl);
    if (wholeUrl == null) {
        requestUrl = pageLinkUrl + ajaxUrl + '?bohinet=' + Math.random().toString() + "&" + requst + " " + requestID;
    }
    else {
        requestUrl = wholeUrl + '?bohinet=' + Math.random().toString() + "&" + requst + " " + requestID;
    }

    if (isXml) {
        $(requestID).load(requestUrl, requstData, onSuccessRunCode);
    }
    else {
        //alert(requestUrl);
        $(requestID).load(requestUrl, requstData, onSuccessRunCode);
    }
}

//**Ajax处理部分基础函数
function bohinetAjaxBase(wholeUrl, postType, requst, requstData, isXml, onPrevRunCode, onErrorRunCade, onSuccessRunCode, onCompleteRunCode) {
    //**xml格式暂时没有完成,wood 2010-03-26
    var requestUrl = "";
    //alert(ajaxUrl);
    if (wholeUrl == null) {
        requestUrl = pageLinkUrl + ajaxUrl + '?bohinet=' + Math.random().toString() + "&" + requst;
    }
    else {
        requestUrl = wholeUrl + '?bohinet=' + Math.random().toString() + "&" + requst;
    }
    //alert(requestUrl);
    if (isXml) {
        $.ajax({ url: pageLinkUrl + ajaxUrl + '?bohinet=' + Math.random().toString(),
            type: postType,
            data: postXml,
            dataType: 'xml',
            timeout: 1000
        });
    }
    else {
        //alert(requestUrl);
        $.ajax({
            url: requestUrl,
            type: postType,
            data: requstData,
            dataType: "html",
            timeout: 3000,
            beforeSend: function() { bohinetAjaxBeforeSend(onPrevRunCode); },
            error: function() { bohinetAjaxError(onErrorRunCade); },
            success: function(responseText) { bohinetAjaxSuccess(onSuccessRunCode, responseText); },
            complete: function() { bohinetAjaxComplete(onCompleteRunCode); }
        });
    }
}

//处理提交前基本的事件
function bohinetAjaxBeforeSend(RunCode){
//设置为不可操作状态;
    if (RunCode != null) { eval(RunCode); }
}

//处理完成基本的事件
function bohinetAjaxComplete(RunCode){
//设置为不可操作状态;
    if (RunCode != null) { eval(RunCode); }

}

//处理错误基本的事件
function bohinetAjaxError(RunCode){
//设置为不可操作状态;
    if (RunCode != null) { eval(RunCode); }

}
//处理正确基本的事件
function bohinetAjaxSuccess(RunCode, responseText) {
//设置为不可操作状态;
    var returnVaule = responseText;
    //alert(responseText);
    
    //**正常执行，返回错误提示的处理，此部分需要继续考虑中
    if (RunCode != null) {
        RunCode(responseText);
        //eval(RunCode); 
    }
}




//组合信息
function CombineStr(keyName, keyValue, originStr) {
    if (originStr == "") {
        return keyName + splitSecond + keyValue;
    }
    else {
        return originStr + splitFirst + keyName + splitSecond + keyValue;
    }   
}


//分拆信息,不存在返回null
function GetKeyValueFromCombineStr(keyName, originStr) {
    var keyValue = ajaxNoResult;

    if (keyName == "" || originStr == "")
        return keyValue;
        
    //继续执行
    var strList = originStr.toString().split(splitFirst);
    for (i = 0; i < strList.length; i++) {
        var strItem = strList[i].toString().split(splitSecond);
        if (strItem.length == 2) {
            if (strItem[0] == keyName) {
                keyValue = strItem[1].toString();
                break;
            }
        }
        else if (strItem.length == 1) {
            if (strItem[0] == keyName) {
                keyValue = "";
                break;
            }
        }
    }

    return keyValue;
    
}

function cutZero(oldValue){   
    //拷贝一份 返回去掉零的新串
    newstr = oldValue;
    //判断是否有效数
    //alert(oldValue);
    if (oldValue.indexOf(".") > -1) {
        //循环变量 小数部分长度
        var leng = oldValue.length - oldValue.indexOf(".") - 1 
        //循环小数部分   
        for (i = leng; i > 0; i--) {
            //如果newstr末尾有0   
            if (newstr.lastIndexOf("0") > -1 && newstr.substr(newstr.length - 1, 1) == 0) {
                var k = newstr.lastIndexOf("0");
                //如果小数点后只有一个0 去掉小数点   
                if (newstr.charAt(k - 1) == ".") {
                    return newstr.substring(0, k - 1);
                } else {
                    //否则 去掉一个0   
                    newstr = newstr.substring(0, k);
                }
            } else {
                //如果末尾没有0   
                return newstr;
            }
        }
    }
    return oldValue;
}


//jquery事件如何传递参数,采用闭包可实现
Function.prototype.binding = function() {
    if (arguments.length < 2 && typeof arguments[0] == "undefined") return this;
    var __method = this, args = jQuery.makeArray(arguments), object = args.shift();
    return function() {
        return __method.apply(object, args.concat(jQuery.makeArray(arguments)));
    }
}

// 团购倒计时
function TimeSpan(secs) {
    this.hour = Math.floor(secs / 3600);
    this.minute = Math.floor((secs - 3600 * this.hour) / 60);
    this.second = (secs - 3600 * this.hour - 60 * this.minute) % 60;
}


//团购倒计时
function show_date_time(time, id) {
    window.setTimeout("show_date_time(" + time + ",'" + id + "')", 1000);
    timeold = (time - (new Date()).getTime() / 1000);
    sectimeold = timeold
    secondsold = Math.floor(sectimeold);
    msPerDay = 24 * 60 * 60
    e_daysold = timeold / msPerDay
    daysold = Math.floor(e_daysold);
    //e_hrsold = (e_daysold - daysold) * 24;

    if (e_daysold > 0) {
        e_hrsold = (e_daysold) * 24;
        hrsold = Math.floor(e_hrsold);
        e_minsold = (e_hrsold - hrsold) * 60;
        minsold = Math.floor((e_hrsold - hrsold) * 60);
        seconds = Math.floor((e_minsold - minsold) * 60);
        document.getElementById(id).innerHTML = "<span class='hours'>"
        + hrsold + "</span><span  class='minutes'>"
        + minsold + "</span><span class='seconds'>"
        + seconds + "</span></h2>";
    }
}

//竞拍倒计时
function showauction_date_time(time, id) {

    var hours = Math.floor(time / 3600).toString();
    var Minute = Math.floor((time % 3600) / 60).toString();
    var Second = Math.floor((time % 60)).toString();
    if (time > 0) {
        document.getElementById(id).innerHTML = "<span>"
+ PadLeft(hours, 2, 0) + ":</span><span >"
+ PadLeft(Minute, 2, 0) + "</span><span>:"
+ PadLeft(Second, 2, 0) + "</span>";
        if (time < 10) {
            $(id).addClass("deadline");
        }
    }
    time = time - 1;
    stopTime = window.setTimeout("showauction_date_time(" + time + ",'" + id + "')", 1000);

    }




//补位
function PadLeft(str, lenght, padChar) {
    if (str.toString().length >= lenght)
        return str;
    else
        return PadLeft(padChar.toString() + str.toString(), lenght, padChar.toString());
}
