function getIeEdtion() {
    var Brower;
    if(navigator.appName == "Microsoft Internet Explorer"){
        verStr=navigator.appVersion;
        msie=verStr.indexOf("MSIE");
        ver=verStr.substring(msie+5,msie+8);
        if(ver>=5.5){
            Brower="IE55up";
        }else{
            Brower="IE55down";
        }
    }else{
        Brower="other";
    }
    return Brower;
}

function detect_browser_type()
{
    var tmp = document.createElement("div");
    if (typeof document.var_browser_type != "undefined") return document.var_browser_type;
    try {
        tmp.innerHTML = "&";
        for (attr in tmp) {
            t += attr + " ";
            if ("innerText" == attr) {
                if ('&' == tmp.innerText) {
                    delete tmp;
                    document.var_browser_type = 1;
                    return 1;
                } else {
					delete tmp;
					document.var_browser_type = 3;
					return 3; 
				}
			} else if ("textContent" == attr) {
				delete tmp;
				document.var_browser_type = 2;
				return 2;
			} 
		}
	}catch(e) {}
	delete tmp;
	document.var_browser_type = 0;
	return 0;
}



function show_exception(e)
{
	alert(e.message);
} 

function fromID(id)
{
	try {
		return document.getElementById(id);
	} catch (e) {
		return null;
	}
}

function newnode(tag)
{
	try { 
		return document.createElement(tag);
	} catch(e) {
		return null;
	}
}

function htmlchar2text(h) 
{
	return h.replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&nbsp;/g, " ").replace(/&amp;/g, "&").replace(/&quot;/g, "\"");
}

function getText(item)
{
	try {
		switch(detect_browser_type()) {
			case 1:
				return item.innerText;
			case 2:
				return item.textContent; 
			default: 
				return htmlchar2text(item.innerText); 
		}
	} catch(e) {}
	try {
		return htmlchar2text(item.innerText); 
	} catch(e) {}
	return "";
}

function text2html(t)
{
	return t.replace(/&/g, "&amp;").replace(/ /g, "&nbsp;").replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\r?\n/g, "<br />"); 
}

function setText(item, value)
{
	try {
		if (item.innerText) {
			item.innerText = value;
			return ;
		} 
		item.innerHTML = text2html(value);
	} catch(e) {} 
}

function strip_email(email)
{
	var tmp = email.replace(/\s*\<.*\>/g, "").replace(/[\"\']/g, "");
	if ("" != tmp.replace(/\s/g, "")) return tmp.replace(/\s*$/g, "");

	tmp = email.replace(/.*\</g, "").replace(/\>.*/g, "");
	if ("" != tmp.replace(/\s/g, "")) return tmp.replace(/\s*$/g, "");

	return email;
}

function strip_sender(item)
{
	var a = item.getElementsByTagName("a");
	var sender = getText(a[0]);
	item.title = sender;
    var viewEmail = strip_email(sender);
    if(viewEmail.length > 32) {
        viewEmail = viewEmail.replace(/@.*/g, "");
    }
    if(viewEmail.length > 32) {
        viewEmail = viewEmail.substr(0,18)+"...";
    }
    setText(a[0], viewEmail);
        
}

function strip_subject(item)
{
	var subject = getText(item);
	item.title = subject;
	var l = 0;
	for (var i = 0; i < subject.length; i ++) {
		l ++;
		if (subject.charCodeAt(i) > 256) {
			l ++;
		} else if ("\t" == subject[i]) {
			i += 3;
		}
		if (48 <= l) {
			var newsub = subject.substr(0, i)+"...";
			setText(item, newsub);
			break;
		}
	}
}

var req = null;

function get_xml_http(url, xmlhttp_handler)
{
	if (null != req) return false;
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		return false;
	}

	req.onreadystatechange = xmlhttp_handler;
	req.open("GET", url, true);
	req.send('');
}

var focus_item = null;
function set_focus_handler()
{
    try {
        if (null == req) return ;
        if (4 != req.readyState) return ;
        if (200 != req.status) return;
        req = null;
        set_focus(focus_item);
    } catch(e) {}
}

function set_focus(item)
{
	try {
		var mails = document.getElementsByTagName("tr");
		for (var i = 0; i < mails.length; i ++) {
			var tr = mails[i];
			if (6 > tr.cells.length) continue;
			if ("TD" != tr.cells[2].tagName||"TD" != tr.cells[3].tagName) continue;
			if (item != tr) del_class(tr, "readback"); 
		}
		add_class(item, "readback");
	} catch(e) {}
}

function try_set_focus(item)
{
	if (item_have_class(item, "readback")) return;
	focus_item = item;
	var folder = fromID("src_folder").value;
	var msg = item.getElementsByTagName("input");
	msg = msg[0].value;
	get_xml_http("exxmlhttp.php?folder="+folder+"&msg="+msg, set_focus_handler);
}
function strip_list()
{
	try {
        var brower = getIeEdtion();
		var mails = document.getElementsByTagName("tr");
		for (var i = 0; i < mails.length; i ++) {
			var tr = mails[i];
			if (6 > tr.cells.length) continue;
			if ("TD" != tr.cells[2].tagName||"TD" != tr.cells[3].tagName) continue;
			strip_sender(tr.cells[2]);
			strip_subject(tr.cells[3]);

            tr.cells[3].onclick=set_focus_mail;

            tr.cells[4].onclick=set_focus_mail;
            tr.cells[5].onclick=set_focus_mail;
            
            if(getIeEdtion() == "IE55down") mouseHand = "hand";
            else mouseHand = "pointer";
            tr.cells[3].style.cursor = mouseHand;
            tr.cells[4].style.cursor = mouseHand;
            tr.cells[5].style.cursor = mouseHand;

            if (6 < tr.cells.length) {
				tr.cells[6].onclick=set_focus_mail; 
				tr.cells[6].style.cursor = 'hand';
			}

		}
	} catch(e) {}
}

function hide_menu(evt)
{
	try {
		for (var i = 0; i < document.menu_list.length; i ++) {
			document.menu_list[i].style.visibility = "hidden";
		}
		document.onclick = null;
	} catch(e) {}
	return true;
}

function show_menu(evt)
{

	try {
		if (evt) {
			evt.stopPropagation();
		} else {
			window.event.cancelBubble = true;
		}
		var x = 0;
		var y = 0;
		for (var i = this; i != document.body; i = i.offsetParent) {
			x += parseInt(i.offsetLeft);
			y += parseInt(i.offsetTop);
		}
		y += parseInt(this.offsetHeight);
		this.menu_item.className = "dropmenu";
		this.menu_item.style.left = x + "px";
		this.menu_item.style.top = y + "px";
		this.menu_item.style.visibility = "visible";
		this.menu_item.style.display = "block";

		document.onclick = hide_menu;
	} catch(e) {}
}

function append_menu(item)
{
	if ("undefined" != typeof document.menu_list) {
		document.menu_list[document.menu_list.length] = item;
		return ;
	}

	document.menu_list = new Array(item);
}

function bind_menu(m_title, m_item)
{
	try {
		var m = document.getElementById(m_title);
		var item = document.getElementById(m_item);

		item.style.position = "absolute";
		item.style.zindex = 100;
		item.style.visibility = "hidden";
		item.style.display = "none";

		append_menu(item);
		m.menu_item = item;
		item.launcher = m;
		m.onclick = show_menu; 

	} catch(e) {}
}
function search_init()
{
	bind_menu("move", "pop_menu");
	strip_list();
}
function dump_item(item)
{
	var l = '';
	for (x in item) {
		var v = item[x];
		v = null == v?"NULL":v.toString();
		if (v.indexOf("function") == 0) v = "function";
		l += x + "=" + v + "<br>\n";;
	}
	document.write(l);
}

function set_focus_mail(sender)
{
	if (sender == null) {
		try_set_focus(window.event.srcElement.parentNode);
	} else {
		try_set_focus(sender.target.parentNode);
	}
}

function listmail_init()
{
	bind_menu("move", "pop_menu");
	strip_list();
}
function del_class4id(id, class_name)
{
	var item = fromID(id);
	if (null == item) return;
	del_class(item, class_name);	
}
function add_class4id(id, class_name)
{
	var item = fromID(id);
	if (null == item) return;
	add_class(item, class_name);	
}
function del_class(item, class_name)
{
	try {
		var new_classes = new Array();
		var classes = item.className.split(" ");
		for (var i = 0; i < classes.length; i ++) {
			if (class_name != classes[i]) new_classes[new_classes.length] = classes[i];
		}
		item.className = new_classes.join(" "); 
	} catch(e) {}
}
function add_class(item, class_name)
{
	try {
		var new_classes = new Array();
		var classes = item.className.split(" ");
		for (var i = 0; i < classes.length; i ++) {
			if (class_name != classes[i]) new_classes[new_classes.length] = classes[i];
		}

		new_classes[new_classes.length] = class_name;
		item.className = new_classes.join(" ");
	} catch(e) {alert(e.message);}
}
function id_have_class(id, class_name)
{
	try {
		return item_have_class(fromID(id), class_name);
	} catch(e) {
		return false;
	}
}
function item_have_class(item, class_name)
{
	try {
		return have_class(item.className, class_name);
	} catch(e) {
		return false; 
	}
}
function have_class(classes, class_name)
{
	try {
		var cs = classes.split(" ");
		for (i in cs) {
			if (cs[i] == class_name) return true;
		}
		return false;
	} catch (e) {
		return false; 
	}
}
function fake_func()
{
}

function init_page(fileName) {
    //move_right_panel();
}

/*function move_right_panel() {
	try{
      var winWidth = screen.width;
      if (winWidth < 1024) {
          var el = document.getElementById('sidebar');
          el.style.width="0px";
          //eval(el).innerHTML = "";
          //el.style.display = "none";
      }
    }catch (e) {alert(e.message);}
}*/
function hide_bill(evt)
{
	fromID("kq_dm").style.display = "none";
	document.onclick=null;
}
function show_bill(event)
{
	try {
		event.stopPropagation();
	} catch(e) {}
	try {
		window.event.cancelBubble = true;
	} catch(e) {}

	fromID("kq_dm").style.display = "block";
	document.onclick=hide_bill;
}
