﻿/// <reference path="jquery.js" />
(function ($) {
    $.fn.ajaxForm = function (ajaxSuccess) {
        var form = $(this);
        form.submit(function () {
            $.ajax({
                url: form.attr("action"),
                type: form.attr("method"),
                data: form.serialize(),
                beforeSend: function (xhr) {
                },
                success: function (data) {
                    if (ajaxSuccess) { ajaxSuccess(data); }
                },
                error: function () {
                    alert("请求出错，请重试");
                },
                complete: function () {
                }
            });
            return false;
        });
    };
})(jQuery);


//得到Cookie值
function GetCookie(name) {
    var aCookie = document.cookie.split(";");
    for (var i = 0; i < aCookie.length; i++) {
        var str = aCookie[i].replace(/(^\s*)|(\s*$)/g, "");
        var aCrumb = str.split("=");
        if (name == aCrumb[0])
            return unescape(aCrumb[1]);
    }
    return "";
}

//设定Cookie值
function SetCookie(name, value) {
    var expdate = new Date();
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    if (expires != null) expdate.setTime(expdate.getTime() + (expires * 1000));
    document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString()))
    + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "ya247.com" : ("; domain=" + domain))
    + ((secure == true) ? "; secure" : "");
}

//读取Cookie值
function readCookie(name) {
    var cookieValue = "";
    var search = name + "=";
    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search);
        if (offset != -1) {
            offset += search.length;
            end = document.cookie.indexOf(";", offset);
            if (end == -1) {
                end = document.cookie.length;
            }
            cookieValue = unescape(document.cookie.substring(offset, end))
        }
    }
    return cookieValue;
}

//写入Cookie值
function writeCookie(name, value, hours) {

    var expire = "";
    if (hours != null) {
        expire = new Date((new Date()).getTime() + hours * 3600000);
        expire = ";expires=" + expire.toGMTString();
    }
    document.cookie = name + "=" + escape(value) + expire + ";path=/;domain=ya247.com"; //
}
//删除Cookie
function DeleteCookie(name) {
    writeCookie(name, null, -1);
}






//获取表单对象
function gID(getID){
	return document.getElementById(getID);
}



//回车提交
function InputKeyPress(aFrmObj){
	var currKey=0,CapsLock=0; 
	var e = arguments[1];
	e=e||window.event; 
	var kCode=e.keyCode||e.which||e.charCode; 
	if(kCode == '13'){
	    //checkLogin(aFrmObj);
        var loginInfo = GetCookie("ya247login");
        if (loginInfo == "") {
            $.ajax({
            url: $(aFrmObj).attr("action"),
            type: $(aFrmObj).attr("method"),
            data: $(aFrmObj).serialize(),
                beforeSend: function(xhr) {
                },
                success: function(data) {
                    if (ajaxSuccess) { ajaxSuccess(data); }
                },
                error: function() {
                    alert("请求出错，请重试");
                },
                complete: function() {
                }
            });
            return false;
        }
	}
}

//添加收藏夹
function addBookmark(url,title){
	if (window.sidebar) { 
		window.sidebar.addPanel(title, url,""); 
	} else if( document.all ) {
		window.external.AddFavorite( url, title);
	} else if( window.opera && window.print ) {
		return true;
	}
}


//设为首页
function setHomepage(url,title){
	if (document.all)
    {
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage(url);
    }
    else if (window.sidebar)
    {
		if(window.netscape)
		{
			 try
			{ 
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
			 } 
			 catch (e) 
			 { 
				alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值该为true" ); 
			 }
		}
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref(title,url);
	}
}


//组对象显示隐藏
function showDiv(tag,num,tid){
	for(var i=0;i<num;i++){
		gID(tag+i).style.display='none';
	}
	if(tid!=null){
		gID(tag+tid).style.display="block";
	}
}

function setClass(tag,classname){
	gID(tag).className=classname;
}

//检查字符长度
function funCheckLength(strTemp) {
    var i, sum;
    sum = 0;
    for (i = 0; i < strTemp.length; i++) {
        if ((strTemp.charCodeAt(i) >= 0) && (strTemp.charCodeAt(i) <= 255))
            sum = sum + 1;
        else
            sum = sum + 2;
    }
    return sum;
}

//得到GET方法的传递参数
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
