﻿// JavaScript Document
/* Create a new XMLHttpRequest object to talk to the Web server */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
  try {
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (e2) {
    xmlHttp = false;
  }
}
@end @*/
if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
  xmlHttp = new XMLHttpRequest();
}

function add_contact(add_id){
	
	var url='/agen/chat_processing.asp?action=add_id&add_id='+add_id;
	// Open a connection to the server
	xmlHttp.open("GET", url, true);
	// Setup a function for the server to run when it's done
	//xmlHttp.onreadystatechange = update_add_contact;
	// Send the request
	xmlHttp.send(null);
	if ( GetCookie('onpenchat')!='opened'){popWin('/agen/chat.asp','chat',530,530,true)		
	}
}
function update_add_contact(){
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200 && xmlHttp.responseXML ) {
				if(xmlHttp.responseXML.getElementsByTagName( 'state' ).item(0).firstChild.nodeValue=="true")
					{ 
					   if ( xmlHttp.responseXML.getElementsByTagName( 'me_open' ).item(0).firstChild.nodeValue=="false") 
					  {open_im_window()}
					 
					}
					
		}
}
function open_im(){
	if ( GetCookie('onpenchat')!='opened'){popWin('/agen/chat.asp','chat',550,530,true)		
	}
}
function open_im_window(){
	var win=window.open('/agen/chat.asp','chat');
	if (win &&  win.open  &&  !win.closed) 
	{}else 
	{ show_request('提示','<a href="/agen/chat.asp" target="_blank" id="open_im" onclick="document.getElementById(\'Msg\').style.display=\'none\'">点击打开即时聊天</a>');
	}
}


// =========================================================================================================
// 通用模式弹出窗口(适用于IE,Firefox,Opera,Netscape)
// ==========================================================================================================
function popWin(theURL,winName,theW,theH,showAsModal) {
    theTop = (window.screen.height-theH)/2;
    theLeft = (window.screen.width-theW)/2;
    var features = "toolbar=0,scrollbars=yes,left=" + theLeft + ",top=" + theTop + ",width=" + theW + ",height=" + theH;
    
    window.SubWin = window.open(theURL,winName,features);
	if(window.SubWin.closed == true || typeof(window.SubWin.closed) == "undefined"){
		show_request('提示','<a href="/agen/chat.asp" target="_blank" id="open_im" onclick="document.getElementById(\'Msg\').style.display=\'none\'">点击打开即时聊天</a>');return false;
		}
    window.SubWin.focus();
}


function SetCookie(name,value)//两个参数，一个是cookie的名子，一个是值
{
    var Days = 7; //此 cookie 将被保存7 天
    var exp  = new Date();    //new Date("December 31, 9998");
    exp.setTime(exp.getTime() + Days*24*60*60*1000);
    document.cookie = name + "="+ escape (value) + ";path=/;expires=" + exp.toGMTString();
}
function GetCookie(name)//取cookies函数        
{
    var arr = document.cookie.match(new RegExp('(^| )'+name+'=([^;]*)(;|$)'));
    if(arr != null) return unescape(arr[2]); return null;
}
function delCookie(name)//删除cookie
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval=GetCookie(name);
    if(cval!=null) document.cookie= name + '='+cval+';expires='+exp.toGMTString();
}

function add_detail_history(add_no,add_title,add_type){
	if (add_no==''||add_title=='出租 '||add_title=='出售 '||add_title=='')return
	if (add_type=='search'){
		var add_a='search_history_url_new';
		var add_b='search_history_title_new';
		var add_c='search_history_date_new';
	}else{
		var add_a='details_history_no_new';
		var add_b='details_history_title_new';
		var add_c='details_history_date_new';
	}
	var history_no=GetCookie(add_a);
	var history_title=GetCookie(add_b);
	var history_date=GetCookie(add_c);
	var myDate = new Date();
	now=myDate.getTime();
	if (history_no==null){
		SetCookie(add_a,add_no+'\\/');
		SetCookie(add_b,add_title+'\\/');
		SetCookie(add_c,now+'\\/');
		}
	else{
		var history_no_arr=history_no.split('\\/');
		var history_title_arr=history_title.split('\\/');
		var history_date_arr=history_date.split('\\/');
		var list_size=0;
		history_no=add_no+'\\/';
		history_title=add_title+'\\/';
		history_date=now+'\\/';
		//alert(history_no_arr.length);
		for(i=0;i<history_no_arr.length-1;i++){
			if (list_size>=6){break}
			if (history_no_arr[i]==add_no||history_no_arr[i]==''||history_title_arr[i]==''){continue}
			list_size++;
			history_no+=history_no_arr[i]+'\\/';
			history_title+=history_title_arr[i]+'\\/';
			history_date+=history_date_arr[i]+'\\/';
		}
		//alert(list_size);
		SetCookie(add_a,history_no);
		SetCookie(add_b,history_title);
		SetCookie(add_c,history_date);//alert(history_date);
	}
}

function list_detail_history(list_id,max_size){
	var history_no=GetCookie('details_history_no_new');
	var history_title=GetCookie('details_history_title_new');
	var history_date=GetCookie('details_history_date_new');
	document.write('<dl class="his_list" id="'+list_id+'"><dt class="title_r">我曾经看过</dt>')
	if (history_no==null){document.write('<dd>没有记录</dd></dl>');return
	}else{
		var history_no_arr=history_no.split('\\/');
		var history_title_arr=history_title.split('\\/');
		var history_date_arr=history_date.split('\\/');
		var list_size=1;
		if (check_view_time(history_date_arr[0])==false){document.write('<dd>没有记录</dd></dl>');return}
		document.write('<dd class="top1"><div class="name"><a title="'+history_title_arr[0]+'" href="/details-'+history_no_arr[0]+'.html">'+history_title_arr[0]+'</a></div><span class="time">'+check_view_time(history_date_arr[0])+'</span></dd>')
		if (history_no_arr.length>2){
			for (i=1;i<history_no_arr.length-1;i++){
				if (list_size>=max_size)break;
				if (check_view_time(history_date_arr[i])==false){break}
				document.write('<dd><div class="name"><a title="'+history_title_arr[i]+'" href="/details-'+history_no_arr[i]+'.html">'+history_title_arr[i]+'</a></div><span class="time">'+check_view_time(history_date_arr[i])+'</span></dd>')
				list_size++;
			}
		}
		document.write('<dd class="history_clear"><a href="javascript:clear_history(\''+list_id+'\',\'details\')">清空查看记录</a></dd>')
	}
	document.write('</dl>')
}

function list_search_history(max_size){
	var history_no=GetCookie('search_history_url_new');
	var history_title=GetCookie('search_history_title_new');
	var history_date=GetCookie('search_history_date_new');
	document.write('<dl class="his_list" id="left_search_his"><dt class="title_r">我曾经看过</dt>')
	if (history_no==null){document.write('<dd>没有记录</dd></dl>');return
	}else{
		var history_no_arr=history_no.split('\\/');
		var history_title_arr=history_title.split('\\/');
		var history_date_arr=history_date.split('\\/');
		var list_size=1;
		if (check_view_time(history_date_arr[0])==false){document.write('<dd>没有记录</dd></dl>');return}
		document.write('<dd class="top1"><div class="name"><a title="'+history_title_arr[0]+'" href="'+history_no_arr[0]+'">'+history_title_arr[0]+'</a></div><span class="time">'+check_view_time(history_date_arr[0])+'</span></dd>')
		if (history_no_arr.length>2){
			for (i=1;i<history_no_arr.length-1;i++){
				if (list_size>=max_size)break;
				if (check_view_time(history_date_arr[i])==false){break}
				document.write('<dd><div class="name"><a title="'+history_title_arr[i]+'" href="'+history_no_arr[i]+'">'+history_title_arr[i]+'</a></div><span class="time">'+check_view_time(history_date_arr[i])+'</span></dd>')
				list_size++;
			}
		}
		document.write('<dd class="history_clear"><a href="javascript:clear_history(\'left_search_his\',\'search\')">清空搜索记录</a></dd></dl>')
	}
}

function clear_history(clear_id,clear_type){
	if (clear_type=='search'){
		delCookie('search_history_url_new');
		delCookie('search_history_title_new');
		delCookie('search_history_date_new');
	}else{
		delCookie('details_history_no_new');
		delCookie('details_history_title_new');
		delCookie('details_history_date_new');
	}
	document.getElementById(clear_id).innerHTML='<dt class="title_r">我曾经看过</dt><dd>没有记录</dd>';
}
function check_view_time(check_time){
	var endDate= new Date();
	var df=parseInt((endDate.getTime()-check_time)/1000);
	if (df<3*60){return '刚刚看过'}
	if (df<30*60){return parseInt(df/60)+'分钟前'}
	if (df<60*60){return '半小时前'}
	if (df<24*3600){return parseInt(df/3600)+'小时前'}
	if (df<48*3600){return '昨天'}
	if (df<72*3600){return '前天'}
	if (df<168*3600){return parseInt(df/86400)+'天前'}
	return false
}