/* *
 * 通过对象ID获得对象引用
 * @param   {object}   id          操作对象ID
 */
function $ID(id) {
	return document.getElementById(id);
}


/* *
 * 显示与隐藏对象
 * @param   {string}   action      操作类型
 * @param   {object}   id          操作对象ID
 */
function shObj(action, id) {
	if(action == "show") {
		$ID(id).style.display = "block";
	}
	else {
		$ID(id).style.display = "none";
	}
}


/* *
 * 图片按比例缩放
 * @param   {object}   ImgD        图片对象
 * @param   {object}   iwidth      最大宽带
 * @param   {object}   iheight     最大高度
 */
function DrawImage(ImgD, iwidth, iheight) {
	
	//参数(图片, 允许的宽度, 允许的高度)
	var image = new Image();
	image.src = ImgD.src;
	
	if (image.width > 0 && image.height > 0) {
		
		if (image.width/image.height >= iwidth/iheight) {
			
			if (image.width > iwidth) {  
				ImgD.width = iwidth;
				ImgD.height = (image.height*iwidth)/image.width;
			}
			else {
				ImgD.width = image.width;  
				ImgD.height = image.height;
			}

		}
		
		else {
			
			if (image.height > iheight) {  
				ImgD.height = iheight;
				ImgD.width = (image.width*iheight)/image.height;        
			}
			else {
				ImgD.width = image.width;  
				ImgD.height = image.height;
			}

		}
	}
}


/* *
 * 页面顶部登录动作
 */
function login() {
	if (chkLgnWords($ID("frm_loginok")) == true) {
		//$ID("GoUrl").value = window.location.href;
		$ID("GoUrl").value = '/barmember/';
		document.loginok.submit();
	}
}


/* *
 * 登录时检测
 * @param   {object}   frm         表单对象
 */
function chkLgnWords(frm) {
	if (frm.UserId.value.length == 0) {
		alert('请输入用户名！');
		frm.UserId.focus();
		return false;
	}
	else if (frm.Pass.value.length == 0) {
		alert('请输入密码！');
		frm.Pass.focus();
		return false;
	}
	return true;
}


/* *
 * 查询时检测
 * @param   {object}    frm           表单对象
 * @param   {numeric}   cursel        被激活的选项卡的序号，需要从1开始计算
 */
function chkShearchWords(frm, cursel) {
	switch(cursel) {
		
		case 1 :
		if (frm.barname.value.length == 0 || frm.barname.value == '名称 地址  城市等（可用空格隔开多个关键字搜索）') {
			alert('请输入查询关键字');
			frm.barname.value = '';
			frm.barname.focus();
			return false;
		}
		break;
		
		case 2 :
		if (frm.searchall.value.length == 0 || frm.searchall.value == '请输入账号、昵称或常去的酒吧关键字') {
			alert('请输入查询关键字');
			frm.searchall.value = '';
			frm.searchall.focus();
			return false;
		}
		break;
		
		case 3 :
		if (frm.music.value.length == 0 || frm.music.value == '请输入歌曲、舞曲、上传者、歌手或DJ关键字') {
			alert('请输入查询关键字');
			frm.music.value = '';
			frm.music.focus();
			return false;
		}
		break;
		
		case 4 :
		if (frm.keyword.value.length == 0 || frm.keyword.value == '请输入标题、内容关键字') {
			alert('请输入查询关键字');
			frm.keyword.value = '';
			frm.keyword.focus();
			return false;
		}
		break;
		
	}
	
	return true;
}

/* *
 * 更改选项卡
 * @param   {string}    name            选项卡前缀名
 * @param   {numeric}   cursel          被激活的选项卡的序号，需要从1开始计算
 * @param   {numeric}   count           选项卡的数量 
 */
function selecMalltTag(name, curSel, count) {
	for (i = 1; i <= count; i++) {
		var menu = $ID(name + i);
		var cont = $ID("ct_" + name + "_" + i);
		menu.className = (i == curSel) ? "selectTag" : "";
		cont.className = (i == curSel) ? "tagContent selectTag" : "tagContent";
	}
}


/* *
 * 按下回车提交表单
 * @param   {object}   frm         事件对象
 */
function pswKeyUp(e) {
	var currKey = 0;
	var e = e || event;
	var currKey = e.keyCode || e.which || e.charCode;
	var currObj = e.target || e.srcElement;
	
	if (currKey == 13) {
		if (chkLgnWords($ID("frm_loginok")) == true) {
			//$ID("GoUrl").value = window.location.href;
			$ID("GoUrl").value = '/barmember/';
			document.loginok.submit();
		}
	}
}


/* *
 * 更改选项卡
 * @param   {string}    name            选项卡前缀名
 * @param   {numeric}   cursel          被激活的选项卡的序号，需要从1开始计算
 * @param   {numeric}   count           选项卡的数量 
 */
function selectSearchTag(name, curSel, count) {
	for (i = 1; i <= count; i++) {
		var menu = $ID(name + i);
		var cont = $ID("ct_" + name + "_" + i);
		var key = $ID("key_" + name + "_" + i);
		menu.className = (i == curSel) ? "search_on" : "";
		cont.className = (i == curSel) ? "search_fl" : "search_fl none";
		key.className = (i == curSel) ? "keyword" : "keyword none";
	}
}

/**
* 添加到收藏夹
* url：网站地址
* title：网站名称
*/
function addFavorite(url, title) {
    if (document.all) {
        window.external.addFavorite(url, title);
    } else if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    }
}


/**
* 设为主页
* url：网站地址
*/
function setHomepage(url) {
    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('browser.startup.homepage', url);
    }
}
