﻿
jQuery.fn.ImageAutoSize = function(width,height)
{
    $("img",this).each(function()
    {
        var image = $(this);
        if(image.width()>width)
        {
            image.width(width);
            image.height(width/image.width()*image.height());
        }
        if(image.height()>height)
        {
            image.height(height);
            image.width(height/image.height()*image.width());
        }
    });
} 



function FilterScripts(content)
    {
        var sz = "";
        sz = content.replace(/<script(.|\n)*\/script>\s*/ig, "");
        sz = sz.replace( /<[^>]+/ig, function($0){return $0.replace(/\s*on\w+=[^ ]+/ig, "")});
        return sz;
    }
    
    

function obj2str(o) {
    var r = [];
    if (typeof o == "string" || o == null) {
        return o;
    }
    if (typeof o == "object") {
        if (!o.sort) {
            r[0] = "{"
            for (var i in o) {
                r[r.length] = i;
                r[r.length] = ":";
                r[r.length] = obj2str(o[i]);
                r[r.length] = ",";
            }
            r[r.length - 1] = "}"
        } else {
            r[0] = "["
            for (var i = 0; i < o.length; i++) {
                r[r.length] = obj2str(o[i]);
                r[r.length] = ",";
            }
            r[r.length - 1] = "]"
        }
        return r.join("");
    }
    return o.toString();
}


function HTMLEncode(s) {
    if (s != null && s != "") {
        return s.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
    }
    else {
        return "";
    }

};

function HTMLDecode(s) {

    if (s != null && s != "") {
        //return s.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/<br.*?>/g, '\r\n').replace(/&nbsp;/g, ' ');
        return s.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&nbsp;/g, ' ');
    } else {
        return "";
    }
};

function GetText(strHtml) {
    if (strHtml == null && strHtml == undefined) {
        return strHtml;
    }
    strHtml = strHtml.replace(/<\/?[^>]*>/g, ''); //去除HTML tag
    strHtml = strHtml.replace(/(^\s*)|(\s*$)/g, ""); //去除两端空行
    return strHtml;
}






//var EventUtil = {};
//EventUtil.getEvent = function() {
//    if (window.event) { return window.event; }
//    else { return EventUtil.getEvent.caller.arguments[0]; }
//}
//function changeFocus() {

//    var oEvent = EventUtil.getEvent();

//    var src = oEvent.srcElement ? oEvent.srcElement : oEvent.target;

//    /*判断按下回车键的控件类型：不能是提交、重置按钮等类型*/
//    if (oEvent.keyCode == 13 && src.type != '') {

//        if (oEvent.srcElement) {
//            oEvent.keyCode = 9;
//        } else {
//            var ele = document.forms[0].elements;
//            for (var i = 0; i < ele.length; i++) {
//                var q = (i == ele.length - 1) ? 0 : i + 1; //   if   last   element   :   if   any   other   
//                if (src == ele[i]) { ele[q].focus(); break }
//            }

//            oEvent.preventDefault();
//        }
//    }
//}

//document.onkeydown = changeFocus;  //dcument对象的onkeydown事件




//  


$(document).ready(function() {
    $(document).keydown(function(e) {
    
    e = e || window.event;
        //alert(e.target.type);
        if (e.keyCode == 13) {
        
//         if (e.target.type == 'submit' || e.target.type == 'button') {
//                $(e.target).click();
//            } else if (e.target.type == 'textarea') {
//                event.keyCode = '13';
//            }
//            else {
//                event.keyCode = '9';
//            }
        
            if (e.target.type == 'submit' || e.target.type == 'button'|| e.target.type == 'a') {
            
                //alert(e.target.type);
                $(e.target).click();
            } else if (e.target.type == 'textarea') {
                event.keyCode = 13;
            }
            else {
                //alert(e.keyCode);
                //event.keyCode = '9';
                window.event.keyCode=9
            }


        }
    })
});


/*将String类型解析为Date类型.   
parseDate('2006-1-1') return new Date(2006,0,1)   
parseDate(' 2006-1-1 ') return new Date(2006,0,1)   
parseDate('2006-1-1 15:14:16') return new Date(2006,0,1,15,14,16)   
parseDate(' 2006-1-1 15:14:16 ') return new Date(2006,0,1,15,14,16);   
parseDate('2006-1-1 15:14:16.254') return new Date(2006,0,1,15,14,16,254)   
parseDate(' 2006-1-1 15:14:16.254 ') return new Date(2006,0,1,15,14,16,254)   
parseDate('不正确的格式') retrun null   
*/
function parseDate(str) {
    if (typeof str == 'string') {
        var results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) *$/);
        if (results && results.length > 3)
            return new Date(parseInt(results[1]), parseInt(results[2]) - 1, parseInt(results[3]));
        results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2}) *$/);
        if (results && results.length > 6)
            return new Date(parseInt(results[1]), parseInt(results[2]) - 1, parseInt(results[3]), parseInt(results[4]), parseInt(results[5]), parseInt(results[6]));
        results = str.match(/^ *(\d{4})-(\d{1,2})-(\d{1,2}) +(\d{1,2}):(\d{1,2}):(\d{1,2})\.(\d{1,9}) *$/);
        if (results && results.length > 7)
            return new Date(parseInt(results[1]), parseInt(results[2]) - 1, parseInt(results[3]), parseInt(results[4]), parseInt(results[5]), parseInt(results[6]), parseInt(results[7]));
    }
    return null;
}
