// input ie6点击样式处理
$(function(){
	$('input.inputbox3,input.inputbox4').focus(function(){
		$(this).addClass('inputbox3-focus');
	}).blur(function(){
		$(this).removeClass('inputbox3-focus');
	});
});
var CustomFn = {
	setHomePage: function(){
		var url = window.location, domain, d = document, w = window;
		domain = url.protocol + '//' + url.hostname;
		if (d.all){
			d.body.style.behavior='url(#default#homepage)';
			d.body.setHomePage(domain);
		}else if (w.sidebar){
			if(w.netscape){
				try{
					w.netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
				}catch(e){
					alert( "该操作被浏览器拒绝，如果想启用该功能，请在地址栏内输入 about:config,然后将项 signed.applets.codebase_principal_support 值改为true" );
				//return false;
				}
			}
			try{
				var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
				prefs.setCharPref('browser.startup.homepage', domain);
			}catch(e){
			//return false;
			}
		}
	},

	addFavorite: function() {
		if (document.all){
			window.external.addFavorite(window.location.href, document.title);
		}else if (window.sidebar){
			window.sidebar.addPanel(document.title, window.location.href, '');
		}
	},

	/**
	 * 退出登录
	 *
	 * @author dengxh at 2010-1-23
	 * @param mixed back 是否返回,false 不返回,true 返回,string 返回到指定地址
	 * @return void
	 */
	logout: function(back){
		var url = '';
		if(typeof back == 'string')
			url = back;
		else if(back == undefined || back === true)
			url = document.location.href;
		
		document.location.href = 'http://passport.5pk.com/login.php?action=loginOut&url=' + url;
	}
}

var Menu = {
	speed: 450,

	/**
	 * 菜单事件触发
	 *
	 * @author dengxh at 2009-12-16
	 * @param object obj 事件触发对象
	 * @param string selector 菜单内容选择器
	 * @param string openIcon 菜单打开的图标
	 * @param string closeIcon 菜单关闭的图标
	 * @return void
	 */
	toggle: function(obj, selector, openIcon, closeIcon){
		var selector = $(selector);
		var fn =  function(){
			if(selector[0].style.display == 'none')
				$(obj).attr('src', closeIcon);
			else
				$(obj).attr('src', openIcon);
			
		}
		selector.stop(true, true).slideToggle(this.speed, fn);
	},

	/**
	 * 设置菜单样式
	 *
	 * @author dengxh at 2009-12-21
	 * @param string selector
	 * @param string className
	 * @return void
	 */
	setClass: function(selector, className){
		$(selector).find('li a').attr('class', className);
	},

	/**
	 * 设置菜单项选中状态，菜单项必需是li标签
	 *
	 * @author dengxh at 2009-12-16
	 * @param string selector 菜单框选择器
	 * @param int index 菜单索引
	 * @param string selectedClass 菜单选中类名
	 * @param string defaultSelector 默认菜单框选择器
	 * @return void
	 */
	selectItem: function(selector, index, selectedClass, defaultSelector){
		if(!selector || isNaN(index = parseInt(index))){
			selector = $.cookie('pay_menu_selector');
			index = $.cookie('pay_menu_index');
		}
		if(!selector || isNaN(index = parseInt(index)))
			$($(defaultSelector).find('li a').get(0)).attr('class', selectedClass);
		else
			$($(selector).find('li a').get(isNaN(index) ? 0 : index)).attr('class', selectedClass);

		$.cookie('pay_menu_selector', selector || defaultSelector);
		$.cookie('pay_menu_index', isNaN(index) ? 0 : index);
	}
}

var Ajax = {
	tipSelector: null,
	tipText: '请稍候...',

	/**
	 * 在指定元素内显示一个提示
	 *
	 * @author dengxh at 2009-12-22
	 * @param string selector 元素选择器
	 * @param string text 显示的文字
	 * @return void
	 */
	tip: function(selector, text){
		this.tipSelector = selector || this.tipSelector;
		this.tipText = text || this.tipText;
		return this;
	},

	/**
	 * 显示提示
	 *
	 * @author dengxh at 2009-12-22
	 * @return void
	 */
	showTip: function(){
		if(this.tipSelector != null){
			$(this.tipSelector).find('*').css('display', 'none').find('input').css('disabled', true);
			$(this.tipSelector).append('<span class="ajax-tip">'+ this.tipText +'</span>');
		}
		return this;
	},

	/**
	 * 隐藏提示
	 *
	 * @author dengxh at 2009-12-22
	 * @return void
	 */
	hideTip: function(){
		if(this.tipSelector != null){
			$(this.tipSelector).find('span.ajax-tip').remove();
			$(this.tipSelector).find('*').css('display', 'inline').find('input').css('disabled', false);
		}
	},
	
	/**
	 * 发送POST请求
	 *
	 * @author dengxh at 2009-6-21
	 * @return void
	 */
	post: function(url, params, callback){
		this.showTip();
		$.post(url, $.extend(params, {
			caller: 'ajax'
		}), function(d){
			callback(d), Ajax.hideTip();
		}, 'json');
	},

	/**
	 * 发送GET请求
	 *
	 * @author dengxh at 2009-6-21
	 * @return void
	 */
	get: function(url, params, callback){
		this.showTip();
		$.get(url, $.extend(params, {
			caller: 'ajax'
		}), function(d){
			callback(d), Ajax.hideTip();
		}, 'json');
	},

	/**
	 * 发送GET JSON请求
	 *
	 * @author dengxh at 2009-6-21
	 * @return void
	 */
	getJSON: function(url, params, callback, type){
		this.showTip();
		type = type || 'jsonp';
		if( type == 'jsonp' ){
			if( url.indexOf('?') == -1 )
				url += '?callback=?';
			else
				url += '&callback=?'
		}
		$.getJSON(url, $.extend(params, {
			format: type,
			caller: 'ajax'
		}), function(d){
			callback(d), Ajax.hideTip();
		});
	},
	/**
	 * 发送ajax post表单请求
	 *
	 * @author dengxh at 2009-3-30
	 * @param string formID 表单ID ( FIX BY QINCHH AT 2010-02-09 允许传入对象 )
	 * @param function fn 回调函数
	 * @param submit 提交按钮 ( ADD BY QINCHH AT 2010-02-09 )
	 * @return void
	 */
	postForm: function(formId, fn){
		fn = fn || function(){};
		var f = typeof(formId) == 'object' ? formId : $('#' + formId);
		f.ajaxSubmit({
			success: fn,
			data: {
				caller: 'ajax'
			},
			dataType : 'json'
		});
	}
};
/**
 * 按年月得出当月天数
 *
 * @author dengxh at 2009-1-28
 * @return int
 */
function getDays(year, month){
	year = parseInt(year);
	month = parseInt(month);
	if(isNaN(year) || isNaN(month) || year < 1 || month < 1 || month > 12)
		return null;
	var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	if(year % 4 == 0)
		days[1] = 29;
	return days[month-1];
};

function shuffle(arr) {
	for(
		var j, x, i = arr.length; i;
		j = parseInt(Math.random() * i),
		x = arr[--i], arr[i] = arr[j], arr[j] = x
	);
	return arr;
}

String.prototype.utfLength = function(){
	return this.replace(/[^\x00-\xff]/ig, '***').length;
}
