String.prototype.trim = function () {
	var newStr;
	if(this)
		newStr=this.replace(/(^\s*)|(\s*$)/g, "");
	else
		newStr="";

	return newStr;
}

function autoResize(ifrm) {
	if (typeof ifrm == "string")
		ifrm = document.getElementsByName(ifrm)[0];

	if (ifrm == null) return;
	if (ifrm.init == "false") {
		ifrm.init = "true";
		return;
	}

	var sheight = ifrm.document.body.scrollHeight;
	var oheight = ifrm.document.body.offsetHeight;
	ifrm.style.height = oheight;
}

function DrawImage(FitWidth,FitHeight,element){
	var ImgD = element || event.srcElement;
 	var image=new Image();
 	image.src=ImgD.src;
 	if(image.width>0 && image.height>0){
 		if(image.width/image.height>= FitWidth/FitHeight){
 			if(image.width>FitWidth){
 				ImgD.width=FitWidth;
 				ImgD.height=(image.height*FitWidth)/image.width;
 			}else{
 				ImgD.width=image.width;
 				ImgD.height=image.height;
 			}
 		} else{
 			if(image.height>FitHeight){
 				ImgD.height=FitHeight;
 				ImgD.width=(image.width*FitHeight)/image.height;
 			}else{
			ImgD.width=image.width;
 			ImgD.height=image.height;
 			}
 		}
 	}
}

/**
 * 用户登录
 */
function login(context, newWindow) {
	context = context ? context + "/" : "";
	newWindow = newWindow == null ? true : newWindow;

	if (loginFrm.loginName.value.trim().length == 0) {
		alert("登录名不能为空!");
		loginFrm.loginName.focus();
		return false;
	}

	if (loginFrm.password.value.trim().length == 0) {
		alert("密码不能为空!");
		loginFrm.password.focus();
		return false;
	}

	var user = {
		loginName: loginFrm.loginName.value,
		password: loginFrm.password.value
	};

	ajaxRequest("login!login.action", {
		waiting: false,
		sync: false,
		params: {
			validCode: loginFrm.validCode.value,
			user: toString(user)
		},

		onSuccess: function(event) {
			if (!event.value.registerUser) {
				var url = context + "admin.admin";

				/*if (newWindow)
					window.open(url);
				else
					top.location.href = url;*/
			}

			location.reload();
		}
	});

	return false;
}

function logout(options) {
	var _options = options || {};
	_options.waiting = false;

	if (!_options.onSuccess) {
		_options.onSuccess = function() {
			location.reload();
		}
	}

	ajaxRequest("login!logout.action", _options);
}

function regsiter() {
	window.open("register.action?type=registerUser");
}

/////////////////////////////////// 企业网 ////////////////////////////////////////

function smover(cellbar) {
	if(cellbar==s_1){
		this.document.all.s_1.className="searchAct";
		this.document.all.s_2.className="searchNal";
		this.document.all.s_3.className="searchNal";
		this.document.all.s_4.className="searchNal";
		this.document.all.s_5.className="searchNal";

		//quickQueryFrm.key.value = "关键字";
		quickQueryFrm.key.value = "";
		quickQueryFrm.path.value = "company/companyInfo";
		quickQueryFrm.vocation.selectedIndex = 0;
		quickQueryFrm.key.style.width = "";
		quickQueryFrm.vocation.style.display = "";

	} else if(cellbar==s_2){
		this.document.all.s_2.className="searchAct";
		this.document.all.s_1.className="searchNal";
		this.document.all.s_3.className="searchNal";
		this.document.all.s_4.className="searchNal";
		this.document.all.s_5.className="searchNal";

		//quickQueryFrm.key.value = "关键字";
		quickQueryFrm.key.value = "";
		quickQueryFrm.path.value = "resourceMessage";
		quickQueryFrm.vocation.selectedIndex = 0;
		quickQueryFrm.key.style.width = quickQueryFrm.key.offsetWidth + quickQueryFrm.vocation.offsetWidth + 5;
		quickQueryFrm.vocation.style.display = "none";

	} else if(cellbar==s_3){
		this.document.all.s_3.className="searchAct";
		this.document.all.s_1.className="searchNal";
		this.document.all.s_2.className="searchNal";
		this.document.all.s_4.className="searchNal";
		this.document.all.s_5.className="searchNal";

		//quickQueryFrm.key.value = "关键字";
		quickQueryFrm.key.value = "";
		quickQueryFrm.path.value = "company/supplyDemand";
		quickQueryFrm.vocation.selectedIndex = 0;
		quickQueryFrm.key.style.width = "";
		quickQueryFrm.vocation.style.display = "";

	} else if(cellbar==s_4){
		this.document.all.s_4.className="searchAct";
		this.document.all.s_1.className="searchNal";
		this.document.all.s_3.className="searchNal";
		this.document.all.s_2.className="searchNal";
		this.document.all.s_5.className="searchNal";

		//quickQueryFrm.key.value = "关键字";
		quickQueryFrm.key.value = "";
		quickQueryFrm.path.value = "company/product";
		quickQueryFrm.vocation.selectedIndex = 0;
		quickQueryFrm.key.style.width = quickQueryFrm.key.offsetWidth + quickQueryFrm.vocation.offsetWidth + 5;
		quickQueryFrm.vocation.style.display = "none";

	} else if(cellbar==s_5){
		this.document.all.s_5.className="searchAct";
		this.document.all.s_1.className="searchNal";
		this.document.all.s_3.className="searchNal";
		this.document.all.s_2.className="searchNal";
		this.document.all.s_4.className="searchNal";

		//quickQueryFrm.key.value = "关键字";
		quickQueryFrm.key.value = "";
		quickQueryFrm.path.value = "company/exhibit";
		quickQueryFrm.vocation.selectedIndex = 0;
		quickQueryFrm.key.style.width = quickQueryFrm.key.offsetWidth + quickQueryFrm.vocation.offsetWidth + 5;
		quickQueryFrm.vocation.style.display = "none";
	}
}

/**
 * 企业网快速搜索
 */
function quickQuery() {
	//if (quickQueryFrm.key.value.trim().length == 0 || quickQueryFrm.key.value.trim() == "关键字") {
	if (quickQueryFrm.key.value.trim().length == 0) {
		alert("请输入查询关键字!");
		return false;
	}

	// 如果采用Form提交的话则在看完内容无法后退至查询结果界面
	window.open(
		"companyPortal!quickQuery.action" +
		"?path=" + quickQueryFrm.path.value +
		"&key=" + quickQueryFrm.key.value +
		"&vocation=" + (typeof quickQueryFrm.vocation == "undefined" ? "-1" : quickQueryFrm.vocation.value)
	);

	return false;
}

/**
 * 询盘
 */
function ask() {
	if (askFrm.message.value.trim().length == 0) {
		alert("请输入询盘内容");
		return false;
	}

	var entity = {
		sender: {id: Number(askFrm.sender.value)},
		receiver: {id: Number(askFrm.receiver.value)},
		subject: askFrm.subject.value,
		message: askFrm.message.value,
		state: Message.ST_ASK
	}

	ajaxRequest("message!saveMessage.action", {
		params: {
			waiting:false,
			entity: toString(entity)
		},

		onSuccess: function() {
			askFrm.message.value = "";
			alert("询盘信息已经成功发送，请耐心等对方企业的回复!");
		}
	});

	return false;
}

/////////////////////////////////// 人才网 ////////////////////////////////////////

/**
 * 人才网快速搜索
 * 用于人才和岗位的搜索
 * @param path
 * @param newWindow 是否在新窗口中打开
 */
function jobQuickSearch(path, advanced, newWindow) {
	newWindow = newWindow == null ? false : newWindow;

	var url = "jobPortal!quickQuery.action" +
		"?key=" + (typeof quickQueryFrm.key == "undefined" ? "" : quickQueryFrm.key.value) +
		"&needStation=" + (typeof quickQueryFrm.needStation == "undefined" ? "" : quickQueryFrm.needStation.value) +
		"&station=" + (typeof quickQueryFrm.station == "undefined" ? "" : quickQueryFrm.station.value) +
		"&workAddress=" + (typeof quickQueryFrm.workAddress == "undefined" ? "" : quickQueryFrm.workAddress.value) +
		"&day=" + (typeof quickQueryFrm.day == "undefined" ? "-1" : quickQueryFrm.day.value) +
		"&path=" + path +
		"&advanced=" + advanced;

	if (newWindow)
		window.open(url);
	else
		location.href = url;

	return false;
}

/**
 * 文章集搜索
 */
function jobArticleSearch(newWindow) {
	if (articleQueryFrm.akey.value.trim().length == 0) {
		alert("请输入查询关键字!");
		return false;
	}

	newWindow = newWindow == null ? true : newWindow;
	var url = "jobPortal!articleQuery.action" +
		"?key=" + articleQueryFrm.akey.value +
		"&day=" + (typeof articleQueryFrm.aday == "undefined" ? "-1" : articleQueryFrm.aday.value);

	if (newWindow)
		window.open(url);
	else
		location.href = url;

	return false;
}

/**
 * 申请/收藏岗位
 */
function jobStation(station, company, apply) {
	ajaxRequest("vip!station.action", {
		params: {
			sid: station,
			companyId: company,
			apply: apply,
			waiting:false
		},

		onSuccess: function(event) {
			if (apply)
				alert("成功申请此岗位,请耐心等企业的回复!");
			else
				alert("成功收藏此岗位!");
		}
	});
}

/**
 * 查看会员的联系方式
 */
function jobViewLink(contentId) {
	location.href = "jobPortal!resume.action?viewLink=true&id=" + contentId;
}

/////////////////////////////////// 政务频道 ////////////////////////////////////////

function govMakeVisible(cur,which) {
	if (which==0)
		cur.filters.alpha.opacity=100
	else
		cur.filters.alpha.opacity=30
}

/**
 * 投票
 */
function govVote(voteId) {
	var ids = [];
	var items = voteFrm.single;
	var isSelect = false;
	for (var i = 0; i < items.length; i++) {
		var item = items[i];
		if (!item.checked) continue;

		ids.push(item.value);
		isSelect = true;
	}

	if (!isSelect) {
		alert("请选择投票项目!");
		return false;
	}

	ajaxRequest("vote!vote.action", {
		waiting: false,

		params: {
			id: voteId,
			ids: ids.join(",")
		},

		onSuccess: function() {
			viewVote(voteId);
		}
	});
}

/**
 * 查看投票结果
 */
function viewVote(voteId) {
	window.open("govPortal!viewVote.action?id=" + voteId, "", "scrollbars=yes,Height="+(screen.height/2 + 10)+",Width="+(screen.width/2)+",top=50,left=50");
}

/////////////////////////////////// 镇门户 ////////////////////////////////////////

function townQuickQuery() {
	var key = quickQueryFrm.key.value;
	if (key.trim().length == 0) {
		alert("请输入查询关键字!");
		return false;
	}

	var path = quickQueryFrm.path.value;
	if (path.trim().length > 0) {
		if (path.startWith("company")) {
			quickQuery();

		} else if (path.startWith("job")) {
			jobQuickSearch(path, false, true);
		}
	} else {
		var url = "townPortal!queryArticles.action?key=" + quickQueryFrm.key.value;
		window.open(url);
	}

	return false;
}