var g_login_init = null;
var g_myBodyInstance = null;
var g_myBodyInstanceString = null;
var isMinNS4 = null;
var isMinNS5 = null;
var isMinIE4 = null;
var isMinIE5 = null;
var isMacIE = null;
var getFFVersion = null;
var FFextraHeight = null;
var isOpera = null;

var w_rs = null;
var w_sc = null;

var encjs = "http://qzone.qq.com/js/ps.js";
var dflt_css = "http://qzone.qq.com/js/login.css";
var css_obj = null;
var css_rel = null;
var login_window = null;
var mask_layer = null;
var drag_div = null;
var fix_div = null;
var module_div = null;
var timer = null;

var d_init = false;

function qq_form(aid)
{
	var argv = qq_form.arguments;

	// get redirect url
	if ( argv[1] )
	{
		redirect_url = argv[1];
	}
	else
	{
		redirect_url = window.location;
	}
	
	// get err_callback url
	if ( argv[2] )
	{
		fp_val = argv[2];
	}
	else
	{
		fp_val = 'loginerroralert';
	}

	// get target window
	if ( argv[3] )
	{
		target_win = argv[3];
	}
	else
	{
		target_win = "_self";
	}

	// get css url
	if ( argv[4] )
	{
		css_url = argv[4];
	}
	else
	{
		css_url = dflt_css;
	}
	
	//judge action mode #1 登陆 #2投票
	if(argv[5])
	{
		amode = argv[5];	
	}
	else
	{
		amode = 1;
	}
	
	//judge action name 操作名称
	if(argv[6])
	{
		aname = argv[6];	
	}
	else
	{
		aname = "登录";
	}
	
	
	//judge action url
	if(argv[7])
	{
		aurl = argv[7];	
	}
	else
	{
		aurl = "http://ptlogin2.qq.com/login";
	}
	
	//get vid
	if(argv[8])
	{
		_vid = argv[8];	
	}
	else
	{
		_vid = "";
	}
	
	if(argv[12])
	{
		_fileByQQ = argv[12];	
	}
	else
	{
		_fileByQQ = "";
	}
	
	if(argv[13])
	{
		_fileType = argv[13];	
	}
	else
	{
		_fileType = "";
	}

	
g_myBodyInstance = document.body;
		isMinNS4 = (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 4) ? 1 : 0;
		isMinNS5 = (navigator.appName.indexOf("Netscape") >= 0 && parseFloat(navigator.appVersion) >= 5) ? 1 : 0;
		isMinIE4 = (document.all) ? 1 : 0;
		isMinIE5 = (isMinIE4 && navigator.appVersion.indexOf("5.") >= 0) ? 1 : 0;
		isMacIE = (isMinIE4 && navigator.userAgent.indexOf("Mac") >= 0) ? 1 : 0;
		isOpera = (navigator.appName.indexOf("Opera") >= 0) ? 1 : 0;	
		if (document.compatMode != "BackCompat" && !isOpera )
		{	 
		    g_myBodyInstance = document.documentElement;
		}
	
	if ( !css_obj || (css_rel != css_url) )
	{
		css_obj = document.getElementById('login_css');
		
		if ( !css_obj )
		{
			// add login css
			var login_css = document.createElement('link');
			login_css.id = 'login_css';
			login_css.rel = 'stylesheet';
			login_css.type = 'text/css';
			login_css.media = 'screen';
			login_css.href = css_url;
			document.body.appendChild(login_css);
			
			css_obj = login_css;
		}
		else
		{
			css_obj.href = css_url;
		}
		
		css_rel = css_url;
	}


	login_window = document.getElementById('login_container');
	//alert("login_window="+login_window);
	//if (( !login_window )||(d_init))
	if ( !login_window )
	{
		// add encrypt js
		var encScript = document.createElement('script');
		encScript.src = encjs;
		encScript.type = "text/javascript";
		document.body.appendChild(encScript);
		
		// add div element
		var divE = document.createElement('div');
		divE.id = 'login_container';
		//divE.style.display = 'none';
		divE.style.visibility = 'hidden';
		//divE.style.filter = 'Alpha(opacity=0)';
		divE.style.opacity = 0;
	
		
		
		var login_HTML = "";
		
		if ( navigator.userAgent.indexOf('MSIE') != -1 )
		{
			login_HTML += "<iframe id=\"mask_iframe\" src=\"\"></iframe>";
		}
		login_HTML += "	<div id=\"login_header\">";
		login_HTML += "		<h4>请输入验证码完成加分操作</h4>";
		login_HTML += "		<span id=\"close\" onclick=\"javascript:setLoginFormVisible('hide');\"><span class=\"none\">关闭</span></span>";
		login_HTML += "	</div>";
		login_HTML += "	<div id=\"login_form\">";
		login_HTML += "	<form id=\"user_login\" name=\"user_login\" action=\"" + aurl + "\"method=\"post\" enctype=\"application/x-www-form-urlencoded\" target=\"" + target_win + "\" autocomplete=\"off\" >";
		login_HTML += "		<input type=\"hidden\" name=\"h\" value=\"1\" />";
		login_HTML += "		<input type=\"hidden\" name=\"FVoteCounts\" value=\"1\" />";
		login_HTML += "		<input type=\"hidden\" name=\"FType2\" value=\"1\" />";
		login_HTML += "		<input type=\"hidden\" name=\"u1\" value=\"" + redirect_url + "\" />";
		login_HTML += "		<input type=\"hidden\" name=\"ep\" value=\"\" />";
		login_HTML += "		<input type=\"hidden\" name=\"fp\" value=\"" + fp_val + "\" />";
		login_HTML += "		<input type=\"hidden\" name=\"tm\" value=\"31313437373631393337\" />";
		login_HTML += "		<input type=\"hidden\" name=\"type\" value=\"\" />";
		login_HTML += "		<input type=\"hidden\" name=\"aid\" value=\"" + aid + "\" />";
		login_HTML += "   <input type=\"hidden\" id=\"vid\" name=\"vid\" value=\"" + _vid + "\" />";
		login_HTML += "   <input type=\"hidden\" id=\"fileByQQ\" name=\"fileByQQ\" value=\"" + _fileByQQ + "\" />";
		login_HTML += "   <input type=\"hidden\" id=\"fileType\" name=\"fileType\" value=\"" + _fileType + "\" />";
		login_HTML += "   <input type=\"hidden\" name=\"RtnUrl_Value\" value=\"" + location.href + "\" />";
		login_HTML += "		<ul>";      
		
		
		
		login_HTML += "			<li>";
		login_HTML += "				<span class=\"code_span\">验证图片：</span>";
		login_HTML += "				<img id=\"loginVerifyImg\" src=\"http://ptlogin2.qq.com/getimage?aid=" + aid + "&" + Math.random() + "\" />";
		login_HTML += "				<p><a href=\"javascript:_nchangeImg(" + aid + ");\" tabindex=\"-1\">看不清,换一张</a></p>";
		login_HTML += "			</li>";
		login_HTML += "			<li>";
		login_HTML += "				<span>附加字符：</span>";
		login_HTML += "				<label accesskey=\"v\" for=\"vrfycode\">";
		login_HTML += "					<input id=\"vrfycode\" name=\"verifycode\" type=\"text\" class=\"verifycode\" maxlength=\"4\" style=\"ime-mode:disabled;\" />*";
		login_HTML += "				</label>";
		login_HTML += "				<p>请输入上图中字符，大小写忽略</p>";
		login_HTML += "			</li>";
		login_HTML += "			<li>";
		login_HTML += "				<input name=\"imageField\" type=\"submit\" class=\"bt_login\" value=\"提  交\" border=\"0\">";
		login_HTML += "			</li>";
		login_HTML += "		</ul>";
		login_HTML += "	</form>";
		login_HTML += "	</div>";
		
		divE.innerHTML = login_HTML;
    	
    	document.body.appendChild(divE);
		
		login_window = document.getElementById('login_container');
		
		timer = setTimeout("setLoginFormVisible('show')", 500);
	}
	else
	{
		_nchangeImg(aid);
		_nchangeArgs(_vid,_fileByQQ,_fileType)
		timer = null;
	}	
	

}

function _nchangeArgs(vid,fileByQQ,fileType)
{
	$('#vid').val(vid);
	$('#fileByQQ').val(fileByQQ);
	$('#fileType').val(fileType);
}

function _nchangeImg(aid)
{
	var img = document.getElementById("loginVerifyImg");
	img.src = "http://ptlogin2.qq.com/getimage?aid=" + aid + "&" + Math.random();
	var ctrl = document.getElementById("code");
	if (ctrl != null)
	{
		ctrl.focus();
	}
}

function setCookie(key,value)
{
	document.cookie = key + "=" + escape(value);
}

function getCookie(name)
{
	var pt_str = "(^|.*;)\\s*" + name + "=([^;]*)(;.*|$)";
	var pt_obj = new RegExp(pt_str);
	var rs = document.cookie.match(pt_obj);
	if (rs)
	{
		return rs[2];
	}
	return '';
}

function CheckForm()
{
	var u_ipt = document.getElementById('u');
	if(u_ipt != null)
	{
    	var str = u_ipt.value;
    	setCookie("saveduin", u_ipt.value);
    	if (str == "")
    	{
    		alert("请正确填写您的QQ号码！");
    		u_ipt.focus();
    		u_ipt.select();
    		return false;
    	}
    	if (str.search( /^[1-9]{1}\d{2,9}$/) < 0)
    	{
    		alert("请正确填写您的QQ号码！");
    		u_ipt.focus();
    		u_ipt.select();
    		return false;
    	}
    	var p_ipt = document.getElementById('p');
    	if (p_ipt.value == "")
    	{
    		alert("您还没有输入密码!");
    		p_ipt.focus();
    		p_ipt.select();
    		return false;
    	}
  }
	var vcode = document.getElementById('vrfycode');
	if (vcode.value.length != 4)
	{
		alert("请正确填写验证字符！");
		vcode.focus();
		vcode.select();
		return false;
	}
	return true;
}

function process(form)
{
	if (!CheckForm())
	{
		return false;
	}
	//preprocess(form);
	document.user_login.imageField.disabled = true;
	return true;
}

function processAjax()
{

	if (!CheckForm())
	{
		return false;
	}
	document.user_login.imageField.disabled = true;
	
	var verifycodeValue = $('#vrfycode').val();
	var vidValue = $('#vid').val();
	var fileByQQValue = $('#fileByQQ').val();
	var fileTypeValue = $('#fileType').val();

	$.ajax({
		type: "POST",
		dataType: "json",
		url: '/con/vote/act/saveAjax',
		data: {verifycode:verifycodeValue,vid:vidValue,fileByQQ:fileByQQValue,fileType:fileTypeValue},
		success: function(json){
			//var aa = data.parseJSON();
			alert(json.message);
			
			if(json.code == 9)
			{
				var vote_count = parseInt($('#'+vidValue).attr("innerHTML"));
				$('#'+vidValue).attr("innerHTML",vote_count+1);
			}
		}
	}); 

	setLoginFormVisible('hide');
	return false;
}

function showTips(flag)
{
	var div = document.getElementById("passwordTips");
	if (flag == 1)
	{
		div.style.display = "block";
	}
	else
	{
		div.style.display = "none";
	}
}

function setLoginFormVisible(vis)
{
	if (vis != 'hide')
	{
		login_window.style.display = '';
		login_window.style.visibility = 'visible';
		login_window.style.opacity = 1;

		if ( getWindowWidth() != getWidth(login_window) )
		{
			moveLayerTo(login_window,(getWindowWidth()-getWidth(login_window))/2,getPageScrollY()+(getWindowHeight()-getHeight(login_window))/2);
		}
		
		if ( module_div )
		{
			mask_layer.style.display = '';
			//mask_layer.style.visibility = 'visible';
			mask_layer.style.width = getWindowWidth();
			mask_layer.style.height = getWindowHeight();
			
			moveLayerTo(mask_layer,(getWindowWidth()-getWidth(mask_layer))/2,getPageScrollY()+(getWindowHeight()-getHeight(mask_layer))/2);
		}
	}
	else
	{
		login_window = document.getElementById("login_container");
		
		login_window.parentNode.removeChild(login_window);
		login_window.style.display = 'none';
		//login_window.style.visibility = 'hidden';
		//document.body.removeChild(divE);
		
		//d_init = true;

		//login_window.style.visibility = 'hidden';
		
		if ( module_div )
		{
			mask_layer.style.display = 'none';
			//mask_layer.style.visibility = 'hidden';
		}
		
		detacheWinAct();
	}
}

// make module_div mask layer
function makeMaskLayer()
{
	if ( mask_layer )
	{
		return;
	}
	else
	{
		var w = getWindowWidth();
		var h = getWindowHeight();
		var m_layer = document.createElement('div');
		
		m_layer.style.width = w + 'px';
		m_layer.style.height = h + 'px';
		m_layer.style.background = '#DFE0E1';
		m_layer.style.position = 'absolute';
		m_layer.style.top = 0;
		m_layer.style.left = 0;
		m_layer.style.zIndex = 900;
		m_layer.style.filter = 'alpha(opacity=50)';
		m_layer.style.opacity = 0.5;
		
		var in_HTML = '';
		
		if ( navigator.userAgent.indexOf('MSIE') != -1 )
		{
			in_HTML = "<iframe id=\"module_iframe\" src=\"\"></iframe>";
			m_layer.innerHTML = in_HTML;
		}
		
		document.body.appendChild(m_layer);
		
		mask_layer = m_layer;
	}
}

function reposDiv()
{
	try {
		if ( module_div )
		{
			mask_layer.style.width = getWindowWidth();
			mask_layer.style.height = getWindowHeight();
			moveLayerTo(mask_layer,(getWindowWidth()-getWidth(mask_layer))/2,getPageScrollY()+(getWindowHeight()-getHeight(mask_layer))/2);
		}
		
		if ( fix_div )
		{
			if ( drag_div )
			{
				moveLayerTo(login_window,login_window.offsetLeft,getPageScrollY()+(getWindowHeight()-login_window.offsetTop));
			}
			else
			{
				moveLayerTo(login_window,(getWindowWidth()-getWidth(login_window))/2,getPageScrollY()+(getWindowHeight()-getHeight(login_window))/2);
			}
		}
	} catch (e) {}
}

function attachWinAct()
{
	if ( w_rs || w_sc )
	{
		return;
	}
	
	var f_rs = window.onresize;
	var f_sc = window.onscroll;
	
	if ( typeof(f_rs) == 'function' )
	{
		w_rs = f_rs;
		window.onresize = function (){f_rs();reposDiv();};
	}
	else
	{
		window.onresize = reposDiv;
	}
	
	if ( typeof(f_sc) == 'function' )
	{
		w_sc = f_sc;
		window.onscroll = function (){f_sc();reposDiv();};
	}
	else
	{
		window.onscroll = reposDiv;
	}
}

function detacheWinAct()
{
	if ( typeof(w_rs) == 'function' )
	{
		window.onresize = w_rs;
	}
	else
	{
		window.onresize = null;
	}
	
	if ( typeof(w_sc) == 'function' )
	{
		window.onscroll = w_sc;
	}
	else
	{
		window.onscroll = null;
	}
	
	w_rs = null;
	w_sc = null;
}

// drag functions
function startMove(evt)
{
	evt = getCurrentEvent(evt);
		
	if ( !isLeftKeyDown(evt) )
		return;
	
	var alertDlg = document.getElementById("login_header");
	var loginCtnr = document.getElementById("login_container");
	setEventCapture(alertDlg);
	stopEventTransfer(evt);
	//alertDlg.$clientX = evt.clientX;
	//alertDlg.$clientY = evt.clientY;
	loginCtnr.$clientX = evt.clientX;
	loginCtnr.$clientY = evt.clientY;
	alertDlg.onmousemove = MoveIt;
	alertDlg.onmouseup = stopMove;
}

function MoveIt(evt)
{
	evt = getCurrentEvent(evt);

	if ( !isLeftKeyDown(evt) )
		return;
	
	var alertDlg = document.getElementById("login_container");//login_container
	
	var w_wid = document.body.scrollWidth;
	var w_hgt = document.body.scrollHeight;
	var xoffset = alertDlg.offsetLeft + (evt.clientX - alertDlg.$clientX);
	var yoffset = alertDlg.offsetTop + (evt.clientY - alertDlg.$clientY);
	
	if ( xoffset + getWidth(alertDlg) > w_wid )
	{
		alertDlg.style.left = w_wid - getWidth(alertDlg) - 4 + 'px';
		return;
	}
	else if ( yoffset + getHeight(alertDlg) > w_hgt )
	{
		alertDlg.style.top = w_hgt - getHeight(alertDlg) - 4 + 'px';
		return;
	}
	
	alertDlg.style.left = xoffset + "px";
	alertDlg.style.top = yoffset + "px";
	alertDlg.$clientX = evt.clientX;
	alertDlg.$clientY = evt.clientY;
}

function stopMove(evt)
{
	evt = getCurrentEvent(evt);

	if ( !isLeftKeyDown(evt) )
		return;

	var alertDlg = document.getElementById("login_header");
	alertDlg.onmousemove = null;
	alertDlg.onmouseup = null;
	releaseEventCapture(alertDlg);
}

function getElementPageOffset(x,y)
{
	return {x: x + document.body.scrollLeft - 2, y: y + document.body.scrollTop - 2};
}

function getPageOffset(element)
{
	var left = 0,top = 0;

	do {
		top += element.offsetTop || 0;
		left += element.offsetLeft || 0;
		element = element.offsetParent;
	} while(element);

	return {x: left, y: top};
}

function getCurrentEvent(evt)
{
	return (!evt) ? window.event : evt;
}

function getEventTarget(evt)
{
	evt = ( (!evt) ? window.event : evt );
	return evt.target || event.srcElement;
}

function isLeftKeyDown(evt)
{
	return ( ( (evt.which) && (evt.which==1) ) || ( (evt.button) && (evt.button==1) ) );
}

function stopEventTransfer(evt)
{
	if (evt.preventDefault)
	{
		evt.preventDefault();
		evt.stopPropagation();
	}
	else
	{
		evt.returnValue = false;
		evt.cancelBubble = true;
	}
}

function setEventCapture(target)
{
	if (target.setCapture)
		target.setCapture();
	else if (window.captureEvents)
		window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP|Event.MOUSEDOWN);
}

function releaseEventCapture(target)
{
	if (target.releaseCapture)
		target.releaseCapture();
	else if (window.releaseEvents)
		window.releaseEvents(Event.MOUSEMOVE|Event.MOUSEUP|Event.MOUSEDOWN);
}

function isNULL(e)
{
	if (e==undefined || e==null)
		return true;

	return false;
}

// layer fix functions
function getLayer(name)
{
	if (isMinNS5)
		return document.getElementById(name);
	else if (isMinIE4)
		return eval('document.all.' + name);
	else if (isMinNS4)
		return findLayer(name, document);
	
	return null;
}

function findLayer(name, doc)
{
	var i, layer;
	
	for (i = 0; i < doc.layers.length; i++)
	{
		layer = doc.layers[i];
		if (layer.name == name)
			return layer;
		if (layer.document.layers.length > 0)
		{
			layer = findLayer(name, layer.document);
			if (layer != null)
				return layer;
		}
	}
	
	return null;
}

function moveLayerTo(layer, x, y)
{
	if (isMinIE4)
	{
		layer.style.left = x;
		layer.style.top  = y;
	}
	else if (isMinNS5)
	{
		layer.style.left = x+'px';
		layer.style.top  = y+'px';
	}
	else if (isMinNS4)
    	layer.moveTo(x, y);
}

function getPageLeft(layer)
{
	if (isMinIE4||isMinNS5)
		return (layer.offsetLeft);
	else if (isMinNS4)
		return (layer.pageX);
	
	return (-1);
}

function getPageTop(layer)
{
	if (isMinIE4||isMinNS5)
		return (layer.offsetTop);
	else if (isMinNS4)
		return (layer.pageY);
	
	return (-1);
}

function getPageScrollX()
{
	if (isMinIE4||isMinNS5)
		return (g_myBodyInstance.scrollLeft);
	else if (isMinNS4)
		return (window.pageXOffset);
	
	return (-1);
}

function getPageScrollY()
{
	if (isMinIE4||isMinNS5)
		return (g_myBodyInstance.scrollTop);
	else if (isMinNS4)
		return (window.pageYOffset);
	
	return (-1);
}

function getWidth(layer)
{
	if (isMinIE4||isMinNS5)
	{
		if (layer.style.pixelWidth)
			return (layer.style.pixelWidth);
		else
			return (layer.clientWidth);
	}
	else if (isMinNS4)
	{
		if (layer.document.width)
			return (layer.document.width);
		else
			return (layer.clip.right - layer.clip.left);
	}
	
	return (-1);
}

function getHeight(layer)
{
	if (isMinIE4||isMinNS5)
	{
		if (false && layer.style.pixelHeight)
			return (layer.style.pixelHeight);
		else
			return (layer.clientHeight);
	}
	else if (isMinNS4)
	{
		if (layer.document.height)
			return (layer.document.height);
		else
			return (layer.clip.bottom - layer.clip.top);
	}
	
	return(-1);
}

function getWindowWidth()
{
	if (isMinIE4||isMinNS5)
		return (g_myBodyInstance.clientWidth);
	else if (isMinNS4)
		return (window.innerWidth);
	
	return (-1);
}

function getWindowHeight()
{
	if (isMinIE4||isMinNS5)
		return (g_myBodyInstance.clientHeight);
	else if (isMinNS4)
		return(window.innerHeight);
	
	return(-1);
}

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}