/**
 * 登录的弹出div
 * 需先调用以下js:
 * <script type="text/javascript" src="/js/common.js"></script>
 * <script type="text/javascript" src="/js/xmlHttp.js"></script>
 * <script type="text/javascript" src="/js/checkLogin.js"></script>
 */

/**
 * 用户登录的通用弹出div
 * 根据用户是否登录来弹出div
 * 登录成功后在同一窗口中弹开
 * @param {Object} destionUrl 登录后的目标url null 表示无任何url链接
 */
function popLoginDiv(destionUrl) {
	if(!isLogin()) {
		createLoginDiv(destionUrl,false);
	}
	else 
		window.location.href=destionUrl;
}

/**
 * 用户登录的通用弹出div
 * 根据用户是否登录来弹出div
 * 登录成功后在新窗口中弹开
 * @param {Object} destionUrl 登录后的目标url null 表示无任何url链接
 */
function popLoginDivByNewWindow(destionUrl) {
	if(!isLogin()) {
		createLoginDiv(destionUrl,true);
	}
	else 
		window.open(destionUrl);
}


function getScrollPos(){
        /**
     * 在页面中加入<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     * 之后 document.body.scrollTop 都为0
     * 故取其top应用以下方法
     * @type 
     */
    var scrollPos; 
    if (typeof window.pageYOffset != 'undefined') { 
       scrollPos = window.pageYOffset; 
    } 
    else if (typeof document.compatMode != 'undefined' && 
         document.compatMode != 'BackCompat') { 
       scrollPos = document.documentElement.scrollTop; 
    } 
    else if (typeof document.body != 'undefined') { 
       scrollPos = document.body.scrollTop; 
    } 
    return scrollPos;
}

/**
 * 为评论而创建的DIV
 */
function createLoginDivByComment(destionUrl) {
	
	var buttomDiv = document.createElement("div");
	buttomDiv.setAttribute("align","left");
	var href = document.createElement("a");
	href.setAttribute("href",destionUrl);
	
	href.innerHTML = "<font color='#0066FF' align='left'>暂时不想登录，匿名评论>></font>"
	buttomDiv.appendChild(href);
    var scrollPos = getScrollPos();
	var objPos =new Position(document.body.scrollWidth/2+250,window.screen.availHeight/2+scrollPos-100);
	showMessageBox(createLoginDom("登录后写评论您可以对商品打分",destionUrl,buttomDiv,false),objPos,303);
	getName();
}


/**
 * 创建登录DIV
 */
function createLoginDiv(destionUrl,newWindow) {
    var scrollPos = getScrollPos();
	var objPos =new Position(document.body.scrollWidth/2+250,window.screen.availHeight/2+scrollPos-100);
	showMessageBox(createLoginDom("请先登录",destionUrl,null,newWindow),objPos,303);
	getName();
}

function createLoginDom(message,destionUrl,buttomDiv,newWindow) {
	var loginDiv = document.createElement("div");
	if(buttomDiv)
		setStyle(loginDiv,"border:1px solid #999999; background-color:#ffffff; padding:1px; width:300px; height:240px;");
	else 
		setStyle(loginDiv,"border:1px solid #999999; background-color:#ffffff; padding:1px; width:300px; height:210px;");
	var form = document.createElement("form");
	form.setAttribute("method","post");
	form.setAttribute("action","javascript:;");
	form.setAttribute("id","loginform");
	form.onsubmit = function() {
		return checkL(destionUrl,newWindow);
	}
	loginDiv.appendChild(form);
	var div1 = document.createElement("div");
	setStyle(div1,"position:relative;");
	form.appendChild(div1);
	
	var div2 = document.createElement("div");
	div2.setAttribute("id","messagelabel");
	setStyle(div2,"position:absolute; margin:1px 0 0 1px; background-color:#efefef; width:296px; height:26px; line-height:26px; font-size:12px; font-weight:bold; color:#333333;color:red");
	div2.innerHTML=message;
	div1.appendChild(div2);
	
	
	var div3 = document.createElement("div");
	setStyle(div3,"position:absolute; margin:5px 0 0 280px;");
	div1.appendChild(div3);
	var closeHref = document.createElement("a");
	closeHref.setAttribute("href","javascript:;");
	div3.appendChild(closeHref);
	var closeImg = document.createElement("img");
	closeImg.setAttribute("src","/images/green/close.gif");
	closeImg.setAttribute("border","0");
	closeImg.onclick = function() {
		closeWindow();
	}
	closeHref.appendChild(closeImg);
	
	var div4 = document.createElement("div");
	setStyle(div4,"position:absolute; margin:40px 0 0 10px; width:70px; height:20px; line-height:20px; color:#333333; font-size:12px; text-align:right;");
	div4.appendChild(document.createTextNode("用户名："));
	div1.appendChild(div4);
	
	var div5 = document.createElement("div");
	setStyle(div5,"position:absolute; margin:40px 0 0 85px; text-align:left; height:20px;");
	div1.appendChild(div5);
	var div5_label = document.createElement("lable");
	div5.appendChild(div5_label);
	var div5_input =  document.createElement("input");
	div5_input.setAttribute("name","registername");
	div5_input.setAttribute("id","registername");
	div5_input.setAttribute("type","text");
	setStyle(div5_input,"border:1px solid #cccccc; line-height:18px; height:20px; width:180px;");
	div5_label.appendChild(div5_input);
	
	var div6 = document.createElement("div");
	setStyle(div6,"position:absolute; margin:70px 0 0 10px; width:70px; height:20px; line-height:20px; color:#333333; font-size:12px; text-align:right;");
	div6.appendChild(document.createTextNode("密码："));
	div1.appendChild(div6);
	
	var div7 = document.createElement("div");
	setStyle(div7,"position:absolute; margin:70px 0 0 85px; text-align:left;  height:20px;");
	div1.appendChild(div7);
	var div7_label = document.createElement("lable");
	div7.appendChild(div7_label);
	var div7_input =  document.createElement("input");
	div7_input.setAttribute("name","password");
	div7_input.setAttribute("id","password");
	div7_input.setAttribute("type","password");
	setStyle(div7_input,"border:1px solid #cccccc; line-height:18px; height:20px; width:180px;");
	div7_label.appendChild(div7_input);
	
	var div8 = document.createElement("div");
	setStyle(div8,"position:absolute; margin:100px 0 0 10px; width:70px; height:20px; line-height:20px; color:#333333; font-size:12px; text-align:right;");
	div8.appendChild(document.createTextNode("验证码："));
	div1.appendChild(div8);
	
	var div9 = document.createElement("div");
	setStyle(div9,"position:absolute; margin:100px 0 0 85px; text-align:left;  height:20px;");
	div1.appendChild(div9);
	var div9_label = document.createElement("lable");
	setStyle(div9_label,"position:absolute; margin:0px;");
	div9.appendChild(div9_label);
	var div9_input =  document.createElement("input");
	div9_input.setAttribute("name","testcode");
	div9_input.setAttribute("id","testcode");
	div9_input.setAttribute("type","text");
	setStyle(div9_input,"border:1px solid #cccccc; line-height:18px; height:20px; width:90px;");
	div9_label.appendChild(div9_input);
	
	var div9_label2 = document.createElement("lable");
	setStyle(div9_label2,"position:absolute; margin:1px 0 0 110px; text-align:left; height:18px;");
	div9.appendChild(div9_label2);
	
	
	var validateNumber = document.createElement("img");
	validateNumber.setAttribute("id","validateNumber")
	setStyle(validateNumber,"cursor:pointer;");
	validateNumber.setAttribute("src","/customer/getValidateNumber.htm")
	validateNumber.onclick = function() {
		changeValidateCode(this);
	}
	
	div9_label2.appendChild(validateNumber);
	
	var div11 = document.createElement("div");
	setStyle(div11,"position:absolute; margin:130px 0 0 85px;");
	div1.appendChild(div11);
	
	var div11_img = document.createElement("input");
	div11_img.setAttribute("type","image");
	div11_img.setAttribute("src","/images/bt_user2.jpg");
	div11_img.setAttribute("border","0");
	div11.appendChild(div11_img);
	
	var div12 = document.createElement("div");
	setStyle(div12,"position:absolute; margin:180px 0 0 10px; width:280px; border-top:1px dashed #cccccc; line-height:30px; font-size:12px; text-align:center;");
	div12.appendChild(document.createTextNode("还不是文轩网的用户？　"));
	div1.appendChild(div12);
	var div12_href = document.createElement("a");
	var url = window.location.pathname+window.location.search;
	url = encodeURIComponent(url)
	div12_href.setAttribute("href","/customer/register/register.jsp?from="+url);
	div12.appendChild(div12_href);
	var div12_font = document.createElement("font");
	div12_font.setAttribute("color","#0066FF");
	div12_font.appendChild(document.createTextNode("马上注册 >>"));
	div12_href.appendChild(div12_font);
	
	if(buttomDiv){
		setStyle(buttomDiv,"position:absolute; margin:210px 0 0 10px; width:280px; border-top:1px dashed #cccccc; line-height:30px; font-size:12px; text-align:center;");
		div1.appendChild(buttomDiv);
	}
	return loginDiv;
}
function setStyle(el,style) {
	el.setAttribute("style",style);
	el.style.cssText=style;
}

function changeValidateCode(obj) {  
	var timenow = new Date().getTime();
	obj.src="/customer/getValidateNumber.htm?d="+timenow;  
} 

function checkL(destionUrl,newWindow) {
	var loginform=document.getElementById("loginform");
	var registername=document.getElementById("registername");
	var password=document.getElementById("password");
	var testcode=document.getElementById("testcode");
    if(!ForceEntry(registername,"用户名不能为空")){
	  registername.focus();
	  return false;
	}
    if(!ForceEntry(password,"密码不能为空")) {
	  password.focus();
	  return false;
	}
    if(!ForceEntry(testcode,"验证码不能为空")) {
	  testcode.focus();
	  return false;
	}
	loginCommit(registername.value,password.value,testcode.value,destionUrl,newWindow);
    return false;
}

function loginCommit(registername,password,testcode,destionUrl,newWindow){
	var request = createHttpRequest();
	var url = "/customer/login_json.htm";
	request.open("POST", url, true);
	request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
	request.onreadystatechange = function(){
		if(request.readyState == 4){
			if(request.status == 200){
				var result = eval('(' + request.responseText + ')');
				if(result.success) {
					closeWindow();
					if(destionUrl) {
						if(newWindow)
							window.open(destionUrl);
						else 
							window.location.href=destionUrl;
					}
					
					//更新loginDiv状态
					var loginDiv = document.getElementById("loginDiv");
					if(loginDiv != undefined) {
						check(loginDiv);
					}	
					
				}
				else {
					document.getElementById("messagelabel").innerHTML=result.message;
					var validateNumber = document.getElementById("validateNumber");
					changeValidateCode(validateNumber);
				}
			}
		}	
	};
	request.send(encodeURI("registername="+registername+"&password="+password+"&testcode="+testcode));
}


var isIe=(document.all)?true:false; 
function mousePosition(ev){ 
	if(ev.pageX || ev.pageY){ 
		return {x:ev.pageX, y:ev.pageY}; 
	} 
	return { 
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,y:ev.clientY + document.body.scrollTop - document.body.clientTop 
	}; 
} 

function setSelectState(state){
	var objl=document.getElementsByTagName('select');
	for(var i=0;i<objl.length;i++){
		objl[i].style.visibility=state;
	}
}

//弹出方法 
function showMessageBox(content,pos,wWidth){ 
	closeWindow(); 
	var bWidth=parseInt(document.body.scrollWidth); 
	var bHeight=parseInt(document.body.scrollHeight); 
	if(isIe){
		setSelectState('hidden');
	}
	var back=document.createElement("div"); 
	back.id="back"; 
	var styleStr="top:0px;left:0px;position:absolute;width:"+bWidth+"px;height:"+bHeight+"px;"; 
	styleStr+=(isIe)?"filter:alpha(opacity=0);":"opacity:0;"; 
	back.style.cssText=styleStr; 
	document.body.appendChild(back); 
	showBackground(back,50); 

	var mesW=document.createElement("div"); 
	mesW.id="mesWindow"; 
	mesW.className="mesWindow";
	
	var mesTop=document.createElement("div"); 
	mesTop.className="mesWindowTop";
	mesW.appendChild(mesTop);

	var mesTable=document.createElement("table"); 
	mesTable.setAttribute("width","100%");
	mesTable.setAttribute("height","100%");
	mesW.appendChild(mesTable);

	mesW.appendChild(content);
 
	styleStr="left:"+(((pos.x-wWidth)>0)?(pos.x-wWidth):pos.x)+"px;top:"+(pos.y)+"px;position:absolute;width:"+wWidth+"px;"; 
	mesW.style.cssText=styleStr; 
	document.body.appendChild(mesW); 
} 
//让背景渐渐变暗 
function showBackground(obj,endInt) { 
	if(isIe) { 
		obj.filters.alpha.opacity+=1; 
		if(obj.filters.alpha.opacity<endInt){ 
			setTimeout(function(){showBackground(obj,endInt)},2); 
		} 
	}else{ 
		var al=parseFloat(obj.style.opacity);al+=0.01; 
		obj.style.opacity=al; 
		if(al<(endInt/100)) {
			setTimeout(function(){showBackground(obj,endInt)},2);
		} 
	} 
} 
//关闭窗口 
function closeWindow() { 
	if(document.getElementById('back')!=null) { 
		document.getElementById('back').parentNode.removeChild(document.getElementById('back')); 
	} 
	if(document.getElementById('mesWindow')!=null) { 
		document.getElementById('mesWindow').parentNode.removeChild(document.getElementById('mesWindow')); 
	}  
	if(isIe){
		setSelectState('');
	}
}
function Position(x,y){
	this.x=x;
	this.y=y;
}
