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);
//var IS_IE = window.ActiveXObject ? true : false;

/*add by arook*/

/*Cookie 操作函数*/
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 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 getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function checkLogin(a)
{
	if(isLogin==0){
		if(/property|service|repair\.html$/.test(a.href)){
			O('prev').value = location.href;
		}else
			O('prev').value = a.href;
		to_center('loginDIV',20);
		setLoginMsg('请登录');
		mask_screen_no_msg({zIndex:10});
		return false;
	}else{
		if(/property|service|repair\.html$/.test(a.href)){
			if(isOwner==0){
				showNotOwner();
				return false;
			}
		}
	}
	
	return true;
}

function checkLoginSubmit(f)
{
	if(!(/^\w{5,20}$/.test(f.username.value))){
		setLoginMsg('请输入正确的用户名');
		f.username.select();
		return false;
	}else if(f.password.value.replace(/\s+/,'')==''){
		setLoginMsg('请输入正确的密码');
		f.password.select();
		return false;
	}
	
	return true;
}

function doLogin()
{
	if(checkLoginSubmit(O('loginForm')))
		O('loginForm').submit();
}

function setLoginMsg(msg)
{
	$('#loginMsg').html(msg);
	$('#loginMsg').css('color','red');
}

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)
{
	openWindow('/'+commDomain+'/property/voteView/voteId/'+voteId,'查看投票',450,560);
}

function openWindow( 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 );  
}

/*add by arook copy from sunny*/
function hiddenLoginDiv(nId){
	unmask_screen_no_msg();
	document.getElementById(nId).style.display = 'none';
}

/*add by arook copy from  http://kevin.vanzonneveld.net */

function base64_encode( data )
{
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
    data = utf8_encode(data);
    
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1<<16 | o2<<8 | o3;
 
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
    
    enc = tmp_arr.join('');
    
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
 
    return enc;
}

function base64_decode( data )
{    
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
 
    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);
    
    dec = tmp_arr.join('');
    dec = utf8_decode(dec);
    
    return dec;
}

function utf8_encode ( string )
{
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";
    var start, end;
 
    start = end = 0;
    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        var enc = null;
 
        if (c < 128) {
            end++;
        } else if((c > 127) && (c < 2048)) {
            enc = String.fromCharCode((c >> 6) | 192) + String.fromCharCode((c & 63) | 128);
        } else {
            enc = String.fromCharCode((c >> 12) | 224) + String.fromCharCode(((c >> 6) & 63) | 128) + String.fromCharCode((c & 63) | 128);
        }
        if (enc != null) {
            if (end > start) {
                utftext += string.substring(start, end);
            }
            utftext += enc;
            start = end = n+1;
        }
    }
    
    if (end > start) {
        utftext += string.substring(start, string.length);
    }
 
    return utftext;
}

/*add by weicky*/
function O(id){return document.getElementById(id);}

function autoCheck(eleName,checkedValues)
{
	var eles = document.getElementsByName(eleName);
	for(var i=0;i<checkedValues.length;i++)
	{
		for(var j=0;j<eles.length;j++)
		{
			if(eles[j].value==checkedValues[i])
			{
				eles[j].checked = true;
				break;
			}
		}
	}
}

function autoSelect(id,selectedValue)
{
	var ele = document.getElementById(id);
	for(var i=0;i<ele.options.length;i++)
	{
		if(ele.options[i].value==selectedValue)
		{
			ele.options[i].selected = true;
			break;
		}
	}
}

function make_day_option(dayId,yearId,monthId,defaultValue)
{
	var year = document.getElementById(yearId).value;
	var month = document.getElementById(monthId).value;
	var date = new Date(year,month,0);
	var days = date.getDate();
	var dayEle = document.getElementById(dayId);
	for(var i=0;i<dayEle.options.length;i++)
	{
		dayEle.remove(0);
	}
	for(var i=0;i<days;i++)
	{
		dayEle.options[i] = document.createElement("OPTION");
		if(i<9)
		{
			dayEle.options[i].text = "0" + (i+1);
			dayEle.options[i].value = "0" + (i+1);
			if(defaultValue && "0"+(i+1)==defaultValue) dayEle.options[i].selected = true;
		}
		else
		{
			dayEle.options[i].text = i+1;
			dayEle.options[i].value = i+1;
			if(defaultValue && i+1==defaultValue) dayEle.options[i].selected = true;
		}
	}
}

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.documentElement.scrollLeft==0 ? document.body.scrollLeft : document.documentElement.scrollLeft);
	obj.y = obj.top - (document.documentElement.scrollTop==0 ? document.body.scrollTop : document.documentElement.scrollTop);
	return obj;
}

function get_page_size()
{
	var ie = document.all ? true : false;
	var xmlns = ie ? !(document.documentElement.clientWidth==0 && document.documentElement.clientHeight==0) : (document.doctype && document.doctype.systemId);
	if(ie)
	{
		if(xmlns)
		{
			return {
					screen_width : document.documentElement.clientWidth,
					screen_height : document.documentElement.clientHeight,
					page_width : document.documentElement.scrollWidth,
					page_height : document.documentElement.scrollHeight,
					scroll_width : document.documentElement.scrollLeft,
					scroll_height : document.documentElement.scrollTop
					};
		}
		else
		{
			return {
					screen_width : document.body.clientWidth,
					screen_height : document.body.clientHeight,
					page_width : document.body.scrollWidth,
					page_height : document.body.scrollHeight,
					scroll_width : document.body.scrollLeft,
					scroll_height : document.body.scrollTop
					};
		}
	}
	else
	{
		if(xmlns)
		{
			return {
					screen_width : document.documentElement.clientWidth,
					screen_height : document.documentElement.clientHeight,
					page_width : document.documentElement.scrollWidth,
					page_height : document.documentElement.scrollHeight,
					scroll_width : document.documentElement.scrollLeft,
					scroll_height : document.documentElement.scrollTop
					};
		}
		else
		{
			return {
					screen_width : document.body.clientWidth,
					screen_height : document.body.clientHeight,
					page_width : document.body.scrollWidth,
					page_height : document.body.scrollHeight,
					scroll_width : document.body.scrollLeft,
					scroll_height : document.body.scrollTop
					};
		}
	}
}

function show_div(divId,relEle,x_dir,y_dir,zIndex)
{
	var pos = get_element_position(relEle);
	var size = get_page_size();
	var div = document.getElementById(divId);
	div.style.position = "absolute";
	div.style.display = "";
	if(zIndex) div.style.zIndex = zIndex;
	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 < size.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 < size.clientHeight)
			{
				div.style.top = pos.top + relEle.offsetHeight + "px";
			}
			else
			{
				div.style.top = pos.top - div.offsetHeight + "px";
			}
			break;
	}
}
function to_center(id,zIndex)
{
	var size = get_page_size();
	var ele = document.getElementById(id);
	ele.style.display = "";
	ele.style.position = "absolute";
	ele.style.zIndex = (zIndex ? zIndex : 10);
	ele.style.left = (size.screen_width - ele.offsetWidth)/2 + size.scroll_width + "px";
	ele.style.top = (size.screen_height - ele.offsetHeight)/2 + size.scroll_height + "px";
}

function checkAll(inputName,checked)
{
	var inputors = document.getElementsByName(inputName);
	for(var i=0;i<inputors.length;i++)
	inputors[i].checked = checked;
}
function removeChildren(ele)
{
	 while(ele.hasChildNodes())
	 {
		  if(ele.lastChild.hasChildNodes()) removeChildren(ele.lastChild);
		  else ele.removeChild(ele.lastChild);
	 }
}
function removeSelf(ele)
{
	 removeChildren(ele);
	 ele.parentNode.removeChild(ele);
}
function show_mask_msg(msg,icon,zIndex)
{
	hide_mask_msg();
	var table = document.createElement("TABLE");
	table.id = "MASK_SCREEN_MESSAGE_ID";
	table.style.display = "none";
	table.style.border = "#666666 solid 2px";
	table.style.color = "#000000";
	table.bgColor = "#DDFFDD";
	table.cellPadding = 0;
	table.cellSpacing = 0;
	var tbody = document.createElement("TBODY");
	var tr = document.createElement("TR");
	if(icon)
	{
		var iconTd = document.createElement("TD");
		iconTd.align = "center";
		iconTd.vAlign = "middle";
		iconTd.style.paddingLeft = "20px";
		iconTd.style.paddingRight = "20px"
		iconTd.style.paddingTop = "20px";
		iconTd.style.paddingBottom = "20px";
		var iconImg = new Image();
		iconImg.src = icon;
		iconImg.border = 0;
		iconTd.appendChild(iconImg);
		tr.appendChild(iconTd);
	}
	msg ? msg : msg="正在处理，处理稍候……";
	var msgTd = document.createElement("TD");
	msgTd.width = 200;
	msgTd.align = "left";
	msgTd.vAlign = "middle";
	msgTd.style.paddingLeft = icon ? "0px" : "20px";
	msgTd.style.paddingRight = "20px";
	msgTd.style.paddingTop = "20px";
	msgTd.style.paddingBottom = "20px";
	msgTd.innerHTML = msg;
	tr.appendChild(msgTd);
	tbody.appendChild(tr);
	table.appendChild(tbody);
	document.body.appendChild(table);
	to_center("MASK_SCREEN_MESSAGE_ID",zIndex?zIndex:10);
}
function hide_mask_msg()
{
	var msgObj = document.getElementById("MASK_SCREEN_MESSAGE_ID");
	if(msgObj) removeSelf(msgObj);
}
function set_mask_options(opts)
{
	var size = get_page_size();
	opts ? 1 : opts = { opacity : 0.3, zIndex : 10, bgColor : "#000000" };
	opts.opacity ? 1 : opts.opacity = 0.3;
	opts.zIndex ? 1 : opts.zIndex = 10;
	opts.bgColor ? 1 : opts.bgColor = "#000000";
	var mask_obj = document.getElementById("SCREEN_MASK_ID");
	mask_obj.style.cssText = "filter:alpha(opacity:" + opts.opacity*100 + "); opacity:" + opts.opacity + ";";
	mask_obj.style.display = "block";
	mask_obj.style.position = "absolute";
	mask_obj.style.left = "0px";
	mask_obj.style.top = "0px";
	mask_obj.style.width = (size.screen_width>size.page_width?size.screen_width:size.page_width) + "px";
	mask_obj.style.height = (size.screen_height>size.page_height?size.screen_height:size.page_height) + "px";
	mask_obj.style.zIndex = opts.zIndex;
	mask_obj.style.backgroundColor = opts.bgColor;
}
function create_mask(options)
{
	var mask_div = document.createElement("DIV");
	mask_div.id = "SCREEN_MASK_ID";
	document.body.appendChild(mask_div);
	set_mask_options(options);
}
function mask_screen_no_msg(options)
{
	var mask_div = document.getElementById("SCREEN_MASK_ID");
	if(mask_div) set_mask_options(options);
	else create_mask(options);
}
function unmask_screen_no_msg()
{
	var mask_div = document.getElementById("SCREEN_MASK_ID");
	if(mask_div) mask_div.style.display = "none";
}
function mask_screen(options,msg,icon,msg_zIndex)
{
	mask_screen_no_msg(options);
	hide_mask_msg();
	show_mask_msg(msg,icon,msg_zIndex);
}
function unmask_screen()
{
	hide_mask_msg();
	unmask_screen_no_msg();
}
function disable_screen(options,msg,icon)
{
	mask_screen(options);
}
function enable_screen()
{
    unmask_screen();
}
function bindEvent(object,eventType,eventHandle)
{
	var ie = document.all ? true : false;
	if(ie) object.attachEvent("on"+eventType,eventHandle);
	else object.addEventListener(eventType,eventHandle,false);
}
function include(src)
{
    var scriptTag = document.createElement("SCRIPT");
    scriptTag.type = "text/javascript";
    scriptTag.src = src;
    var head = document.getElementsByTagName("HEAD");
    head = head[0];
    head.appendChild(scriptTag);
}