/**
 * SUBMODAL v1.6
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Subimage LLC
 * http://www.subimage.com
 *
 * Contributions by:
 * 	Eric Angel - tab index code
 * 	Scott - hiding/showing selects for IE users
 *	Todd Huss - inserting modal dynamically and anchor classes
 *
 * Up to date code can be found at http://submodal.googlecode.com
 */

// Popup code
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gReturnFunc;
var gPopupIsShown = false;
var gDefaultPage = "loading.html";
var gHideSelects = false;
var gReturnVal = null;

var gTabIndexes = new Array();
// Pre-defined list of tags we want to disable/enable tabbing into
var gTabbableTags = new Array("A","BUTTON","TEXTAREA","INPUT","IFRAME");	
var reUrl	= "http://uc.hp009.com";

/**
 * Initializes popup code on load.	
 */
function initPopUp(fromId,fromUrl) {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popmaskInfo = document.createElement('div');
	popmaskInfo.id = 'popupMaskInfo';
	popcontInfo = document.createElement('div');
	popcontInfo.id = 'popupContainerInfo';
	popcss	='<style type="text/css">'+
			'html{text-align:left;}ul,li,dl,dt,dd{list-style:none;}'+									
		    '.boxy-wrapper{width:405px;-moz-border-radius-bottomleft:4px;-moz-border-radius-bottomright:4px;-moz-border-radius-topleft:4px;-moz-border-radius-topright:4px;background-color:white;border:1px solid #EAEAEA;position:fixed;}'+
			'.boxy-wrapper .title-bar{background:#ffffff url(./uniteLoginFile/boxy_title.gif) repeat-x bottom; height:28px; position:relative;cursor:move;}'+
			'.boxy-wrapper .title-bar h2{font-size:14px;color:#666666;line-height:1;margin:0;font-weight:bold;padding:8px 0 0 10px;}'+
			'#popupControls .close{position:absolute;top:5px;right:10px;font-size:12px;background-image:url(./uniteLoginFile/boxy_close.gif);width:16px;height:16px;background-repeat:no-repeat;background-position:0px 0px;-webkit-transition:all 0.5s ease-out;-moz-transition:all 0.5s ease-out;}'+
			'.boxy-wrapper .title-bar .close:hover{*background-position:0px -23px;-webkit-transform:rotate(360deg); -moz-transform:rotate(360deg);}'+
			'.blog_login{width:389px;height:200px;padding:7px;font-size:12px;background:#ffffff url(./uniteLoginFile/login_bg_img.gif) no-repeat left bottom; overflow:hidden;}'+
			'.blog_login a{color:#ff6501;}'+
			'.blog_login a:hover{color:#e45d04;text-decoration:none;}'+
			'.blog_login .loginTxt{padding:5px 0 10px 58px;margin:0;}'+
			'.blog_login .loginTxt li{padding-top:10px!important;*padding-top:8px!important;_padding-top:8px;width:260px; text-align:right;}'+
			'.blog_login .loginTxt li input{height:24px;line-height:24px;width:204px;padding:0 3px;vertical-align:middle;background:url(./uniteLoginFile/login_input_bg.gif) repeat-x;border-width:1px;border-style:solid;border-color:#b5b8c8;*font-size:14px;}'+
			'.blog_login .loginTxt li input:focus{border-color:#333333 #a9a59d #a9a59d #333333;}'+
			'.blog_login .loginTips{padding-left:108px;margin:0;text-align:left;}'+
			'.blog_login .loginTips input{vertical-align:middle;margin-right:5px;margin-top:-2px;}'+
			'.blog_login .loginTips a{margin-left:20px;}'+
			'.blog_login .loginBtn {text-align:center;padding:20px 0 10px 0;margin:0;}'+
			'.blog_login .loginBtn input{background:url(./uniteLoginFile/loginBtn.gif) no-repeat center;width:105px;height:30px;line-height:28px;border:0;color:#ffffff;font-size:13px;font-weight:bold;cursor:pointer;}'+
			'.blog_login .loginBtn input:hover,.loginBtn input:focus{opacity:0.8;filter:Alpha(Opacity=80);}'+
			'.blog_login .loginRegiste li{text-align:center;margin-top:9px;margin:0;}'+
			'.blog_login .loginRegiste input{background-position:0px -142px;width:140px;height:32px;font-weight:bold;color:#FFF;font-size:14px;border:0;}'+		'.boxy-inner{-moz-border-radius-bottomleft:4px;-moz-border-radius-bottomright:4px;-moz-border-radius-topleft:4px;-moz-border-radius-topright:4px;background-color:white;border:1px solid #EAEAEA; }'+
			'.boxy-inner {margin:0;}'+
			'.boxy-inner .question{min-height:60px; _height:60px;color:#5f5e5e;background:url(./uniteLoginFile/alertBg.gif) 0px -165px no-repeat;padding-left:45px;padding-top:7px;margin-left:70px;margin:15px 30px 0 30px;line-height:16px;font-size:13px;}'+
			'.boxy-content{margin:0;}'+
			'.boxy-inner .answers{text-align:center;padding-bottom:15px;margin:0;}'+
			'.boxy-btn{background-image:url(./uniteLoginFile/btnBg.gif);background-repeat:no-repeat;width:67px;height:27px;line-height:27px;background-position:0px 0px;color:#000000;border:0;cursor:pointer;}'+
			'.boxy-inner .boxy-btn:hover,.boxy-btn:focus{background-position:-68px 0px;color:#2a6295;}'+
'.info-wrapper{width:303px;height:114px;-moz-border-radius-bottomleft:4px;-moz-border-radius-bottomright:4px;-moz-border-radius-topleft:4px;-moz-border-radius-topright:4px;background-color:white;border:1px solid #EAEAEA;}'+
			'#popupMask,#popupMaskInfo{position: absolute;z-index: 10000;top: 0px;left: 0px;width: 100%;height: 100%;opacity: .4;filter: alpha(opacity=40);'+
				'background-color:transparent !important;'+
				'background-color: #333333;'+
				'background-image/**/: url("./uniteLoginFile/maskBG.png") !important; '+
				'background-image:none;'+
				'background-repeat: repeat;display:none;'+
			'}'+
			'#popupMaskInfo{z-index:10030;display:none;}'+
			'#popupContainer{position: absolute;z-index: 10010;top: 0px;left: 0px;display:none;padding: 0px;}'+
			'#popupFrame{margin: 0px;width: 100%;height: 100%;z-index: 10020;}'+
			'</style>';
	popinfocont	= '' +		
				'<div id="popupFrameInfo" name="popupFrameInfo" width="100%" height="100%" style="display:none;z-index:10040;position:fixed;">'+
					'<div class="boxy-inner" id="popoupInfo" style="width:276px;" >'+
						'<div style="display: block;" class="boxy-content"><div class="question" id="question">用户名或密码错误，请重新输入。</div>'+
						'<form class="answers"><input type="button" value="确认" class="boxy-btn"  onclick="popoInfoHide();"></form></div>'+
					'</div>'+
				'</div>' ;
	// login html
	popcontent	='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'+
		'<html xmlns="http://www.w3.org/1999/xhtml" style="text-align:center;">'+
			'<head>'+
				'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'+
				'<title>无标题文档</title>'+popcss+
				'</head>'+
				'<body style="margin:0;padding:0; font-size:12px; font-family:arial,'+"宋体"+'; ">'+
					'<form id="form1" name="form1" method="post" >'+
					'<div class="blog_login boxy-content">'+
						'<div class="loginContent">'+
							'<ul class="loginTxt">'+
							  '<li><label for="username">用户名：</label><input type="text" name="username" id="username" ></li>'+
								'<li><label for="userpass">密&nbsp;&nbsp;码：</label><input type="password" name="passwd" id="userpass"></li>'+
							'</ul>'+
							'<p class="loginTips"><label><input type="checkbox" value="1" name="autologin" id="autologin">下次自动登录</label><a title="找回密码" href="http://uc.hp009.com/elife/fpwdOne/" target="_blank">忘记密码？</a></p>'+
							'<p class="loginBtn">'+
							'<input type="button" value="登 录" title="登录" id="btnLogin" onclick="checkLoginInfo('+fromId+');">'+
							'</p>'+
							'<ul class="loginRegiste">'+
							 '<li>还没有注册？ <a title="立即注册" target="_blank" href="http://uc.hp009.com/elife/regOne/'+fromId+'/'+fromUrl+'" id="pop_register">立即注册</a></li>'+
							'</ul>'+
					   '</div>'+
					'</div>'+
					'</form>'+
				'</body>'+
		'</html>';
	popcont.innerHTML = ''+
		'<div id="popupInner" class="boxy-wrapper">' +
			'<div id="popupTitleBar" class="title-bar">' +
				'<div id="popupTitle" ><h2 style="text-align:left">HP009用户登录</h2></div>' +
				'<div id="popupControls">' +
					'<a onclick="hidePopWin(false);" id="popCloseBox" class="close" href="javascript:void(0)" onfocus="blur()"></a>' +
				'</div>' +
			'</div>' +
			'<div  style="width:100%;height:100%;background-color:transparent;" scrolling="auto" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%">'+popcontent
'</div>' +
		'</div>';
	popcontInfo.innerHTML	= ''+popinfocont;
	
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	theBody.appendChild(popmaskInfo);
	theBody.appendChild(popcontInfo);
	
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	
	gPopupMaskInfo = document.getElementById("popupMaskInfo");
	
	// check to see if this is IE version 6 or lower. hide select boxes if so
	// maybe they'll fix this in version 7?
	var brsVersion = parseInt(window.navigator.appVersion.charAt(0), 10);
	if (brsVersion <= 6 && window.navigator.userAgent.indexOf("MSIE") > -1) {
		gHideSelects = true;
	}
}
//addEvent(window, "load", initPopUp);

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument returnFunc - function to call when returning true from the window.
	* @argument goUrl - location url
	*/
function showPopWin(width, height, returnFunc, fromId,fromUrl) {
	var	initPop	= document.getElementById("popupTitleBar");
	if(!initPop){
		initPopUp(fromId,fromUrl);
	}
	var isIE = (document.all) ? true : false;
	if(isIE && ([/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1] == 6)){
		document.getElementById("popupFrameInfo").style.position = "absolute";
	}
	else {
		document.getElementById("popupFrameInfo").style.position = "fixed";
	}
	// hidden  scoller in IE6 or IE7
	/*
	var isIE = (document.all) ? true : false;
	var ieVer	= [/MSIE (\d)\.0/i.exec(navigator.userAgent)][0][1];
	if(isIE && (ieVer == 6 || ieVer == 7)){
		document.getElementsByTagName('HTML')[0].style.overflow = 'hidden';
	}
	*/
	// show info
	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
	gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";

	document.getElementById("popCloseBox").style.display = "block";
	
	gPopupIsShown = true;
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	// calculate where to place the window on screen
	centerPopWin(width, height);
	
//	var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);


	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = (height+titleBarHeight) + "px";

	setMaskSize();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
//	gPopFrame.style.width = parseInt(document.getElementById("popupTitleBar").offsetWidth, 10) + "px";
	gPopFrame.style.height = (height) + "px";
	
	// set the url
//	gPopFrame.src = url;
	
	gReturnFunc = returnFunc;
	// for IE
	if (gHideSelects == true) {
		hideSelectBoxes();
	}
	
	window.setTimeout("setPopTitle();", 600);
}
//
var gi = 0;
function centerPopWin(width, height) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		//var theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		//theBody.style.overflow = "hidden";
		/*
		var scTop = parseInt(getScrollTop(),10);
		var scLeft = parseInt(theBody.scrollLeft,10);
		*/
		var scTop	= 0;
		var scLeft	= 0;
		setMaskSize();
		
		//window.status = gPopupMask.style.top + " " + gPopupMask.style.left + " " + gi++;
		var titleBarHeight = parseInt(document.getElementById("popupTitleBar").offsetHeight, 10);
		var fullHeight = 0;
		var fullWidth = 0;
		//提示框的top和left的位置取决于登录框的长宽
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();
		var containTop	= (scTop + ((fullHeight - (height+titleBarHeight)) / 2));
		var containLeft	= (scLeft + ((fullWidth - width) / 2));
		gPopupContainer.style.top = containTop+ "px";
		gPopupContainer.style.left = containLeft  + "px";
		//popoInfo
		var popoWidth	= parseInt(document.getElementById('popoupInfo').style.width,10);
		var containInfoTop	= (scTop + ((fullHeight - (80+titleBarHeight)) / 2));
		var containInfoLeft	= (scLeft + ((fullWidth - popoWidth) / 2));
		document.getElementById("popupFrameInfo").style.top = containInfoTop+ "px";
		document.getElementById("popupFrameInfo").style.left = containInfoLeft  + "px";
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}
addEvent(window, "resize", centerPopWin);
/*
addEvent(window, "scroll", centerPopWin);
window.onscroll = centerPopWin;
*/

/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();

	//set default mask
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	popHeight	= Math.max(
					document.documentElement["clientHeight"],
					document.body["scrollHeight"], document.documentElement["scrollHeight"],
					document.body["offsetHeight"], document.documentElement["offsetHeight"]
				);
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
	gPopupMaskInfo.style.height = popHeight + "px";
	gPopupMaskInfo.style.width = popWidth + "px";
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin(callReturnFunc) {
	var theBody = '';
	theBody = document.getElementsByTagName("BODY")[0];
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	

	theBody.style.overflow = "";
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	//document.getElementsByTagName('HTML')[0].style.overflow = 'auto';
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}
function popoInfoHide()
{
	document.getElementById("popupMaskInfo").style.display	= 'none';
	document.getElementById("popupFrameInfo").style.display	= 'none';
}
/**
 * Sets the popup title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
	}
}



/**
 * Hides all drop down form select boxes on the screen so they do not appear above the mask layer.
 * IE has a problem with wanted select form tags to always be the topmost z-index or layer
 *
 * Thanks for the code Scott!
 */
function hideSelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++) {
    x[i].style.visibility = "hidden";
  }
}

/**
 * Makes all drop down form select boxes on the screen visible so they do not 
 * reappear after the dialog is closed.
 * 
 * IE has a problem with wanting select form tags to always be the 
 * topmost z-index or layer.
 */
function displaySelectBoxes() {
  var x = document.getElementsByTagName("SELECT");

  for (i=0;x && i < x.length; i++){
    x[i].style.visibility = "visible";
  }
}
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}

function checkLoginInfo(fromId){
	$(function(){
		var reUrl	= document.location.href;
		var username	= $('#username').val();
		var	passwd		= $('#userpass').val();
		var autoLogin	= $('#autologin:checked').val();
		if($.trim(passwd) == ''){
			$('#popupMaskInfo').css('display','inline');
			$('#popupFrameInfo').css('display','inline');
			if($.trim(username) == ''){
				$('#question').html('请输入您的用户名和密码。');
			}
			else {
				$('#question').html('请输入您的密码。');
			}
			return false;
		}
		var url = 'http://uc.hp009.com/uniteLogin/checkUser.php?act=login&u='+username+'&p='+hex_md5(passwd)+'&reUrl='+reUrl+'&from='+fromId+'&r='+new Date().getTime()+'&callback=?';
		if(autoLogin	== '1'){
			url	= url+'&autoLogin=1';
		}
		$.getJSON(url, function(data){	
			callback	= data.callback;
			if(callback == 0){
				$('#popupMaskInfo').css('display','inline');
				$('#popupFrameInfo').css('display','inline');
				$('#question').html('您输入的用户名和密码不匹配，请重新输入。');
				return false;
			}
			else {
				 $('#hp009_unite_login').html(callback.nname);
				 $('#hp009_unite_login').attr('href','http://my.hp009.com/'+callback.i+'?main');
				$('#hp009_unite_logout').css('display','inline');
				$('#hp009_unite_logout').attr('href','http://uc.hp009.com/script/uniteLogin/checkUser.php?act=logout&reUrl='+reUrl);
				$('#popupMaskInfo').css('display','none');
				$('#popupFrameInfo').css('display','none');
				hidePopWin(false);
				window.location.reload();
			}
		});	      
	})
}

