/*初始化*/
//取得浏览器版本，供dz编辑器使用
var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

$(document).ready(function(){
	$('body').append("<div style=\"display:none;\" id=\"china186msgwindow\"></div>");
});

/*函数*/
function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name)
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen){
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0)
			break;
	}
	return "";
}

function setCookie(cookieName,cookieValue,nDays)
{
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";path=/;domain="+cookieDomain+";expires="+expire.toGMTString();
}

function _saveUrl()
{
	var argv = _saveUrl.arguments;
	var argc = _saveUrl.arguments.length;
	var url = (0<argc)? argv[0]:null;
	if(url==null)
		url = location.href;
	setCookie('prev',url);
}

function goto(url)
{
	parent.location.href = url;
}

function include(sFileName)
{
	var objHead = document.getElementsByTagName('head');
	var objScript = document.createElement('script');
	objScript.type='text/javascript';
	objScript.src = sFileName;
	objHead[0].appendChild(objScript);
}

function copyToClipBoard(str)
{
	window.clipboardData.setData("Text",str);
	return true;
}

function showMsg(msg)
{
  // 添加一个确定按钮
  msg+="<br><br><br><div style=\"text-align:center;\" ><input type=\"button\" value=\"确 定\" onclick=\"tb_remove()\" /></div>"
	$('#china186msgwindow').html('<p style=\"text-align:center;\">' + msg + '</p>');
	var argv = showMsg.arguments;
	var argc = showMsg.arguments.length;
	var width = (1<argc)? argv[1]:null;
	var height = (2<argc)?argv[2]:null;

	if(width==null)
		width = 300;
	if(height==null)
		height = 200;
	tb_show('温馨提示','#TB_inline?height='+height+'&width='+width+'&inlineId=china186msgwindow');
}

function showMsg2(msg)
{
	$('#china186msgwindow').html('<p style=\"text-align:center;\" >' + msg + '</p>');
	var argv = showMsg2.arguments;
	var argc = showMsg2.arguments.length;
	var width = (1<argc)? argv[1]:null;
	var height = (2<argc)?argv[2]:null;

	if(width==null)
		width = 300;
	if(height==null)
		height = 150;
	tb_show('温馨提示','#TB_inline?height='+height+'&width='+width+'&inlineId=china186msgwindow&modal=true');
}

function showMsg3(msg)
{
  $('#china186msgwindow').html(msg);
  var argv = showMsg3.arguments;
  var argc = showMsg3.arguments.length;
  var width = (1<argc)? argv[1]:null;
  var height = (2<argc)?argv[2]:null;
  if(width==null)
    width = 300;
  if(height==null)
    height = 150;
  tb_show('温馨提示','#TB_inline?height='+height+'&width='+width+'&inlineId=china186msgwindow&modal=true');
}

function showLogin()
{
	showMsg2('<br />您尚未<a href="'+userLoginURL+'" target="_top">登录家园网</a>，您需要先<a href="'+userLoginURL+'" target="_top">登录</a>，才能使用。<br /><br />如果您不是家园会员，<a href="'+userRegisterURL+'" target="_top">欢迎注册</a>。<br /><br /><input type="button" value="登录" onclick="goto(\''+userLoginURL+'\');">&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="注册"  onclick="goto(\''+userLoginURL+'\');">&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="返回"  onclick="ownerGoto();">');
}

function showNotPMApi()
{
	showMsg2('<br />您所在小区尚未开通“在线物业管理系统”，<br />需申请开通后才能使用本栏目功能。<br /><br /><br /><input type="button" value="立即申请" onclick="goto(\'http://www.china186.com/help/join.html\');">&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="返   回"  onclick="ownerGoto();">');
}

function showNotOwner()
{
	showMsg2('<br />目前您不是本小区的业主，<br> 暂不能使用本功能，<br />请联系本小区物管人员。<br /><br /><input type="button" value="返   回"  onclick="ownerGoto();">');
//	tb_show('温馨提示','#TB_inline?height='+height+'&width='+width+'&inlineId=china186msgwindow');
}

function ownerGoto()
{
	url = location.href;
	if(url.indexOf('property')!=-1)
	    document.location=url;	
	    //history.go(-1);
	else
		tb_remove();
	
}

/*显示验证码*/

function reloadValidate()
{
	$('#imgvalidate').attr('src','/Index/validate/'+ new Date().getTime());
}
/*文章浏览页面显示评论*/
function loadComment(type,cid,p){
	
	//$('#commentList').slideUp("slow",function (){
		//$('#loadWaiting').slideDown("fast",function(){
			//$.getScript('/Ajax/loadComment/type/'+type+'/cid/'+cid+'/p/'+p+'/showPage/'+ showPage +'/showMax/'+  showMax);
			$.post(
				'/Ajax/loadComment',
				{type:type,cid: cid,p:p,showPage:showPage,showMax:showMax},
				function(data){
					eval(data);
				}
			);
		//});
	//});
}

/*发布评论*/
cnt = 0;
function addComment(type,cid,validate){

	var oEditor = FCKeditorAPI.GetInstance('commentContent');
    var content = oEditor.GetXHTML(false);
	
	if(cnt==1){
		alert('正在发表评论，请稍后');
		return;
	}
	if(content.replace('/\s+/','')==''){
		alert('评论不能为空');
		$('#commentContent').focus();
		return;
	}
	if(validate.replace('/\s+/','')==''){
		alert('请填写验证码');
		$('#validate').focus();
		return;
	}

	cnt++;

	$.post(
		'/Ajax/addComment/',
		{commId:commId,type: type,cid: cid,content:content,validate:validate,quoteId:quoteId},
		function(data){
			eval(data);
		}
	);
}

/*引用回复*/
var quoteId = 0;
function commentQuote(id)
{
	quoteId = id;
	//$('#commentContent').focus();
}

/*快速提交*/

function quickpost(event){
	if((event.ctrlKey && event.keyCode == 13)||(event.altKey && event.keyCode == 83)){
		$('#d_validate').hide();
		$('#commentContent').focus();
		addComment(commentType,commentCid,$('#validate').val(),$('#hidename').attr('checked'));
	}
}

/*载入用户信息*/
function loadUserInfo(mainServer,userId)
{
	url = 'http://'+mainServer+'/Ajax/loadUserInfo/';
	$.post(
		url,
		{userId: userId},
		function(data){
			$('.userInfo_'+userId).html(data);
		}
	);
}

/* 投诉 */
function commentLitigate(commentId)
{
//	$.getScript('/Ajax/commentLitigate/commentId/'+  commentId);
	$.post(
		'/Ajax/commentLitigate/',
		{commentId:commentId},
		function(data){
			eval(data);
		}
	);
}
/*支持*/
function commentAgree(commentId,a){
	//$.getScript('/Ajax/commentAgree/commentId/'+  commentId);
	$('#a_commentDisaccord_'+commentId).attr('onclick','');
	$('#a_commentAgree_'+commentId).attr('onclick','');
	$.post(
		'/Ajax/commentAgree/',
		{commentId:commentId},
		function(data){
			eval(data);
		}
	);
}
/*反对*/
function commentDisaccord(commentId,a){
	//$.getScript('/Ajax/commentDisaccord/commentId/'+  commentId);
	$('#a_commentDisaccord_'+commentId).attr('onclick','');
	$('#a_commentAgree_'+commentId).attr('onclick','');
	$.post(
		'/Ajax/commentDisaccord/',
		{commentId:commentId},
		function(data){
			eval(data);
		}
	);
}

/*查询物管费用*/
function loadPMCharge(){
	$('#PMbody').hide();
	$('#loadWaiting').show();	
	$.ajax({
		type: "POST",
		url: "/Ajax/loadPMCharge",
		data: "date=" + $('#pmdate').val() ,
		success: function(msg){
			$('#PMbody').html(msg);
			$('#loadWaiting').hide();
			$('#PMbody').show();
		}
	});
}

function ckeckLogin()
{
	if(userId==0)
		showLogin();
}

function vote(voteId)
{
	var checked = false;
	var checkId = '';
	$("input[name=voteOption]").each(function(i){
		if($(this).attr('checked')){
			checked = true;
			checkId += ','+$(this).val();
		}
	});
	
	if(!checked){
		alert('请选择投票选项');
		return;
	}
	
	$.ajax({
		type: "POST",
		url: "/Ajax/vote/",
		data: "voteId="+voteId+"&checkId=" + checkId ,
		success: function(msg){
			alert(msg);
		}
	});
}

function apply()
{
	$.ajax({
		type: "POST",
		url: "/Ajax/apply/",
		data: "commId="+commDomain ,
		success: function(msg){
			alert(msg);
		}
	});
}

function voteView(voteId)
{
	MM_openBrWindow('/'+commDomain+'/property/voteView/voteId/'+voteId,'查看投票',450,560);
}

function MM_openBrWindow( url, winName, width, height)   
{  
	xposition=0; yposition=0;  
	if ((parseInt(navigator.appVersion) >= 4 )){  
		xposition = (screen.width - width) / 2;  
		yposition = (screen.height - height) / 2;  
	}
	
	theproperty= "width=" + width + ","   
	+ "height=" + height + ","   
	+ "location=0,"   
	+ "menubar=0,"  
	+ "resizable=1,"  
	+ "scrollbars=0,"  
	+ "status=0,"   
	+ "titlebar=0,"  
	+ "toolbar=0,"  
	+ "hotkeys=0,"  
	+ "screenx=" + xposition + "," //仅适用于Netscape  
	+ "screeny=" + yposition + "," //仅适用于Netscape  
	+ "left=" + xposition + "," //IE  
	+ "top=" + yposition; //IE   
	window.open( url,winName,theproperty );  
}

function isEmail(emailStr){
	//var re=/^[\w-.]+(\.*[\w-]+)*@([0-9a-z]+(([0-9a-z]*)|([0-9a-z-]*[0-9a-z]))+\.)+[a-z]{2,3}$/i;
	var re=/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
	if(re.test(emailStr))
		return true;
	else
		return false;
}

function countStrLen(str){
	var strlength = 0;
	for(var i=0;i<str.length;i++){
		var intCode=str.charCodeAt(i);
		if (intCode>256){
			strlength += 2;//双字节
		}else{
			strlength ++;
		}
	}
	return strlength;
}

function isNumberString (InString,RefString){
	if(InString.length==0) return (false);
	for (Count=0; Count < InString.length; Count++){
		TempChar= InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1)  
		return (false);
	}
	return (true);
}

function isIDString (idnum){
	var re=/^[\w][\w-.]*[\w]$/i;
	if(re.test(idnum))
		return true;
	else
		return false;
}

function getLocalTimezoneOffset(){
	var dateTimeObject = new Date();
	var timezoneOffset = new Date().getTimezoneOffset()/60*-1;
	return timezoneOffset;
}

///////////////////用户发表验证
function checkbbsuser()
{
	//此处还未添加大区验证，等待cookie包涵大区名或id后补充
	if(!isLogin)
		{showLogin();return false;}
	return true;
}

////////////ajax表单提交格式与thinkphp兼容，需包涵jquery.js jquery.form.js2 个文件
////////////参数form表单id,url 地址,complete 完成后的处理函数,obj 用来显示返回结果
$.sendForm = function (form,url,complete,obj){
	var url = url;
	var data = $('#' + form).formSerialize();
	$.ajax({
		type: "POST",
		url: url,
		data: data,
		dataType:"json",
		beforeSend: function(){
			$("#" + obj).text("正在提交数据");
			$("#" + obj).fadeIn('fast');	
			$('#' + form).click();			//执行存放在form表单上的同步内容函数
		},
		success: function(msg){
			$("#" + obj).text(msg.info);
			//$("#" + obj).fadeOut(3000); 	//淡出效果
			complete( msg.data,msg.status );
			
   		}
	}); 
}

//////////thickbox显示url,url必须带一个?和参数
function showUrl(Url)
{
	var argv = showUrl.arguments;
	var argc = showUrl.arguments.length;
	var title = (1<argc)? argv[1]:null;
	var width = (2<argc)? argv[2]:null;
	var height = (3<argc)?argv[3]:null;
	if(title==null)
		title = "温馨提示";
	if(width==null)
		width = 300;
	if(height==null)
		height = 200;
	tb_show(title,Url + '&height='+height+'&width='+width);
}


/** 用户投诉 */
function complaint(cObj, urlOrAction, id ) {
  if(cObj.getAttributeNode("typeIndex")!=null) {
    var typeIndex = cObj.getAttributeNode("typeIndex").value;
  } else {
    var typeIndex = 1;
  }
  
  if(arguments.length==1) {
    url = document.location.href;
    url = url.replace(/[/]/g,"|");
    url = "/complaint/complaintUrl/typeIndex/"+typeIndex+"/cUrl/"+url+"/?TB_iframe=true&height=200&width=400";
  } else if(arguments.length==2) {
    urlOrAction=urlOrAction.replace(/[/]/g,"|");
    url = "/complaint/complaintUrl/cUrl/"+urlOrAction+"/typeIndex/"+typeIndex+"/?TB_iframe=true&height=200&width=400";
  } else if(arguments.length==3) {
    action = urlOrAction;
    id = id;
    url = "/complaint/index/action/"+urlOrAction+"/id/"+id+"/typeIndex/"+typeIndex+"/?TB_iframe=true&height=200&width=400";
  } else {
    alert("参数不对 ！");
    return;
  }
  cObj.href=url;
}

function tabSwitch(name, index, c1, c2)
{
	var i=1;
	var h;
	var p;

	while(true){
		h = document.getElementById(name + '_head_' + i);
		p = document.getElementById(name + '_page_' + i);
		
		try{
			if(i==index){	
				h.className = c2;
				p.style.display = 'block';
			}else{
				h.className = c1;
				p.style.display = 'none';
			}
		}catch(e){
			break;	
		}
		i++;
	}
}

currentSearchType = 1;
function setSearchType(type)
{
	currentSearchType = type;
	var f = document.getElementById('fTopsearch');
	type = parseInt(type);
	switch(type){
		case 2:
			f.action = 'http://index.china186.com/Search/news/';
			break;
		case 3:
			f.action = 'http://index.china186.com/Search/co/';
			break;
		case 4:
			f.action = 'http://index.china186.com/Search/user/';
			break;
		case 5:
			f.action = 'http://index.china186.com/Search/activitie/';
			break;
		case 1:
		default:
			f.action = 'http://index.china186.com/Search/index/';
	}
	setCookie('currentSearchType',type);
}

function checkSearch(f)
{
	iKey = document.getElementById('searchKey_'+currentSearchType);
	if(iKey.value.replace('/\s+/','')=='' || iKey.value=='请输入搜索关键字'){
		alert('请输入搜索关键字');
		iKey.focus();
		return false;
	}
	document.getElementById('k').value = iKey.value;
	return true;
}

function setSearchKey(iKey,action)
{
	if(action=='onfocus'){
		if(iKey.value=='请输入搜索关键字')
			iKey.value = '';
	}else if(action=='onblur'){
		if(iKey.value=='')
			iKey.value = '请输入搜索关键字';
	}
}

//复制到剪切板
function copyToClipBoard(msg){
	var clipBoardContent=''; 
	clipBoardContent += msg;
	clipBoardContent += window.location;
	window.clipboardData.setData("Text",clipBoardContent);
	alert("您已复制链接及标题，请粘贴到QQ/MSN/Yahoo等推荐好友！");
}

function ownerShop(shopId)
{
	tb_show('商铺认领','/Index/owner/shopId/'+shopId+'?TB_iframe=true&height=300&width=420');
}

function setProvince(sId)
{
	makeOptByURL("province","/xml/province.xml","option","value","text",sId,"请选择省");
}

function setCity(pId,sId)
{
	if(parseInt(pId)>0)
		makeOptByURL("city","/Area/xml/pid/"+pId,"option","value","text",sId,"请选择市");
}

function setDistrict(cId,sId)
{
	if(parseInt(cId)>0)
		makeOptByURL("district","/Area/xml/pid/"+cId,"option","value","text",sId,"请选择区");
}

function setCommunity(dId,sId)
{
	if(parseInt(dId)>0)
		makeOptByURL("community","/Area/xml/did/" + dId,false,false,false,sId,"请选择小区" );
}

function setBCode(sId)
{
	makeOptByURL("bcode","/xml/mcode.xml","option","value","text",sId,"请选择一级分类");
}

function setMCode(bId,sId)
{
	if(parseInt(bId)>0)
		makeOptByURL("mcode","/Area/poi/pid/"+bId,"option","value","text",sId,"请选择二级分类");
}

function setSCode(mId,sId)
{
	if(parseInt(mId)>0)
		makeOptByURL("scode","/Area/poi/pid/"+mId,"option","value","text",sId,"请选择三级分类");
}

function copyUrl(url,title){
	var clipBoardContent='';
	if(title)
	clipBoardContent += title+'\n';
	clipBoardContent += url;
	window.clipboardData.setData("Text",clipBoardContent);
	alert("您已复制链接及标题，请粘贴到QQ/MSN/Yahoo等推荐好友！");
}
function get_element_position(ele)
{
	var obj = {left:ele.offsetLeft,top:ele.offsetTop,x:0,y:0};
	var parent = ele;
	while(parent = parent.offsetParent)
	{
		obj.left += parent.offsetLeft;
		obj.top += parent.offsetTop;
	}
	obj.x = obj.left - document.body.scrollLeft;
	obj.y = obj.top - document.body.scrollTop;
	return obj;
}

function hideAll()
{
	$("#newLinkDiv,#home,#comm").hide();
}

function show_div(divId,relEle,x_dir,y_dir)
{
	var pos = get_element_position(relEle);
	var div = document.getElementById(divId);
	div.style.position = "absolute";
	div.style.display = "";
	//div.style.visibility = "visiable";
	switch(x_dir)
	{
		case "left":
			div.style.left = pos.left + "px";
			break;
		case "right":
			div.style.left = pos.left + relEle.offsetWidth - div.offsetWidth + "px";
			break;
		case "auto":
		default:
			if(pos.x + div.offsetWidth < document.body.clientWidth)
			{
				div.style.left = pos.left + "px";
			}
			else
			{
				div.style.left = pos.left + relEle.offsetWidth - div.offsetWidth + "px";
			}
			break;
	}
	switch(y_dir)
	{
		case "top":
			div.style.top = pos.top - div.offsetHeight + "px";
			break;
		case "bottom":
			div.style.top = pos.top + relEle.offsetHeight + "px";
			break;
		case "auto":
		default:
			if(pos.y + relEle.offsetHeight + div.offsetHeight < document.body.clientHeight)
			{
				div.style.top = pos.top + relEle.offsetHeight + "px";
			}
			else
			{
				div.style.top = pos.top - div.offsetHeight + "px";
			}
			break;
	}
}

/** 标注地图 */
function mapPosition(x, y, cn) {
  url="/map/commuReg.php?x="+x+"&y="+y+"&cn="+cn+"&TB_iframe=true&height=450&width=600";
  tb_show("地图定位", url);
}
