//ajax 开始
//Examine the browser


var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);


//Get absolute path, fix bug of IE when base path is set
//global parameter
var bbs_login = false;
var crm_login = false;
var crm_chooseRole = false;
var website_login = false;
var gotourl = location.href;
var absbaseurl_bbs = 'http://www.nutriciachina.com/bbs/ajaxlogin.aspx';
//var absbaseurl_bbs = 'http://127.0.0.1:81/iprai/test2.php';
var absbaseurl_crm = 'http://www.nutriciachina.com/nutriciaWeb/ajaxlogin.aspx';
//var absbaseurl_website = 'http://www.nutriciachina.com/ajaxlogin.aspx';
 absbaseurl_website = 'http://www.nutriciachina.com/ajaxlogin.aspx';
var absbaseurl='';
var username="";
var password="";

//Is Ajax supported?
if ((is_ie & !is_ie4) || is_moz || is_saf || is_opera) var shutajax=0;
else var shutajax=1;
//For firefox, tell Firefox not to display the content you input in last session
if (is_moz) {
	var tmp_c=document.getElementById('content');
	if (tmp_c) tmp_c.value='';
}
//encode string
function blogencode (str) {
	str=encodeURIComponent(str);
	if (is_moz) str=str.replace(/%0A/g, "%0D%0A"); //In IE, a new line is encoded as \r\n, while in Mozilla it's \n
	return str;
}
var http_request = false;
function makeRequest(url, functionName, httpType, sendData) {

	http_request = false;
	if (!httpType) httpType = "GET";

	if (window.XMLHttpRequest) { // Non-IE...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/plain');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Cannot send an XMLHTTP request');
		return false;
	}

	var changefunc="http_request.onreadystatechange = "+functionName;
 
	eval (changefunc);
 	//alert(changefunc);
	//http_request.onreadystatechange = alertContents;
	http_request.open(httpType, url, true);
	http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http_request.send(sendData);
}

function getReturnedText () {
	
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var messagereturn = http_request.responseText;
			return messagereturn;
		} else {
			alert('There was a problem with the request.');
		}
	}
}
//first login website
function ajaxLogin_submit (job) 
{	 
	 password = document.getElementById('password').value;
	 username=document.getElementById('username').value;
	 ValidateCode = document.getElementById('ValidateCode').value;
	if(username==""){
		alert("username empty!");
		return false;	
	}
	if(ValidateCode==""){
		alert("验证码 empty!");
		return false;	
	}
		//username = blogencode(username);
		username = escape(username);
		//username="户名不能为";
		//alert(username);
	 document.getElementById('divloginform').innerHTML="logging,wait...";
	//document.getElementById('loginsubmit').disabled='disabled';
	// alert("Server do not support your browser!");
	if (shutajax==0) { //Currently for IE, Safari, Mozilla and Opera
		 var gourl = absbaseurl_website+"?do=login";
 
		var postData = "username="+username+"&password="+password+"&ValidateCode="+ValidateCode; 
 
		  makeRequest(gourl, 'dologinWebsite', 'POST', postData);
	} else {
 
		alert("服务器不支持你的浏览器进行登录,请使用Firefox或者IE");
	}
}
//判断是否登录开始，先看website有没有登录i

function dologinWebsite () { 	 
	if (http_request.readyState == 4) {	 		
		if (http_request.status == 200) {			
			var messagereturn = http_request.responseText;
            if (messagereturn.indexOf("<ajax::error>ValidateCode")!=-1) 
			{
				        alert('验证码错误');
				        ShowLogin();
	        }else{
			    if (messagereturn.indexOf("<ajax::error>")!=-1) 
			    {
				    website_login = false; //website status not logged	
   			    
			    } else if (messagereturn.indexOf("<ajax::success>")!=-1) {
				    website_login = true;
			    }				
			   loginBBS();
			}
		}  else {
			alert('There was a problem with the request1.');
		}
	}
 }
function loginBBS(){
	    var gourl = absbaseurl_bbs;
		 
		var postData = "username="+username+"&password="+password; 
 
		 makeRequest(gourl, 'dologinBBS', 'POST', postData);
}
function dologinBBS(){			
 
	if (http_request.readyState == 4) {	 	
		if (http_request.status == 200) {
			var messagereturn = http_request.responseText;
			// alert(messagereturn);
			if (messagereturn.indexOf("<ajax::error>")!=-1) {
				bbs_login = false; //website status not logged
			} else if (messagereturn.indexOf("<ajax::success>")!=-1) {
				bbs_login = true;
			}
			 //LoginEnd();
			 loginCRM();
		}  else {
			alert('There was a problem with the request2.');
		}
	}
}
function loginCRM(){
	    var gourl = absbaseurl_crm+"?do=login";
		 
		var postData = "username="+username+"&password="+password; 
 
		 makeRequest(gourl, 'dologinCRM', 'POST', postData);
}
function dologinCRM(){			
   
	if (http_request.readyState == 4) {	 	
 
		if (http_request.status == 200) {
			var messagereturn = http_request.responseText;
			  //alert(messagereturn);
			if (messagereturn.indexOf("<ajax::error>")!=-1) {
				crm_login = false; //website status not logged
				crm_chooseRole =false;
			} else if (messagereturn.indexOf("<ajax::success>login_success3")!=-1) {
				crm_login = true;
			}else if (messagereturn.indexOf("<ajax::success>login_success2")!=-1) {
				crm_chooseRole = true;
			}
 		
			 //LoginEnd();
			 LoginEnd();
		}  else {
			alert('There was a problem with the request3.');
		}
	}
}

function LoginEnd()
{
	// alert(bbs_login+" "+ website_login+" "+ crm_login)
	if(bbs_login||website_login||crm_login)
	{
		document.getElementById('divloginsucess').style.display="block" ;
		document.getElementById('divloginform').style.display="none" ;
		document.getElementById('div_username').innerHTML = username;
		if(bbs_login)
		{
			document.getElementById('div_bbs').style.display="block" ;
		}
		if(website_login)
		{
			document.getElementById('div_web').style.display="block" ;
		}
		if(crm_login)
		{
			document.getElementById('div_crm').style.display="block" ;
		}
		if(crm_chooseRole)
		{
			document.getElementById('div_crm_chooseRole').style.display="block" ;
		}
		
	}else{
	     alert("login faild");
	     ShowLogin();
	}
}
 
function ShowLogin()
{
 		document.getElementById('divloginsucess').style.display="none" ;
		document.getElementById('divloginform').style.display="block" ;
		document.getElementById('divloginform').innerHTML="	<table border=0 cellspacing=6 cellpadding=0>        <tr>              <td>用户名</td>              <td><input  name=username type=text id=username size=15 /></td> </tr>   <tr>  <td>密 &nbsp;码</td>        <td><input  name=password type=password id=password size=15 /></td>        </tr>  <tr>              <td>验证码</td>          <td align=left><input  name=ValidateCode type=text id=ValidateCode style=width:45px /> <img src=admin/ValidateCode.aspx  align=\"absmiddle\"/></td>            </tr>          <tr>          <td></td>          <td><table border=0 cellspacing=0 cellpadding=0>                <tr>                  <td><img src=images/index-icon_21.gif onclick=\"javascript:ajaxLogin_submit('login')\" style=\"cursor:hand;\"/></td>                  <td>&nbsp;<a href=# onclick=\"MM_popupMsg('请与Jason联系，获取新的密码')\">忘记密码</a></td>                </tr>              </table></td>            </tr>          </table>";
}

function ajaxLogout(){
	var postData = "";
	document.getElementById('logindiv').innerHTML="正在注销,请稍候...";
	var gourl=absbaseurl+"ajaxlogin.aspx?do=logout";
	makeRequest(gourl, 'showLogoutinfo', 'POST', postData);
}
//判断是否登录开始，先看website有没有登录i
function isLogin(){
	var postData = "";
	var gourl=absbaseurl_website;
	 
	makeRequest(gourl, 'doisLoginWebsite', 'POST', postData);
}
function doisLoginWebsite () { 	 
	if (http_request.readyState == 4) {	 		
		if (http_request.status == 200) {			
			var messagereturn = http_request.responseText;

			if (messagereturn.indexOf("<ajax::error>")!=-1) {
				website_login = false; //website status not logged				 
			} else if (messagereturn.indexOf("<ajax::success>")!=-1) {
				website_login = true;
			}				
			  isLoginBBS();
		}  else {
			alert('There was a problem with the request4.');
		}
	}
}
//转执行判断BBS是否登录
function isLoginBBS(){
	var postData = "";
	var gourl=absbaseurl_bbs ;
	makeRequest(gourl, 'doisLoginBBS', 'POST', postData);
}
function doisLoginBBS () {		

	if (http_request.readyState == 4) {	 
	 	//alert(http_request.status);
		if (http_request.status == 200) {
			var messagereturn = http_request.responseText;
			//alert(messagereturn);
			if (messagereturn.indexOf("<ajax::error>")!=-1) {
				bbs_login = false; //website status not logged
			} else if (messagereturn.indexOf("<ajax::success>")!=-1) {
				bbs_login = true;
			}
			isLoginEnd();
		}  else {
			alert('There was a problem with the request5.');
		}
	}
}
function isLoginEnd()
{
	//alert(bbs_login+" "+ website_login+" "+ crm_login)
	if(bbs_login||website_login||crm_login)
	{
		document.getElementById('divloginsucess').style.display="block" ;
		document.getElementById('divloginform').style.display="none" ;
		document.getElementById('div_username').innerHTML = username;
		if(bbs_login)
		{
			document.getElementById('div_bbs').style.display="block" ;
		}
		if(website_login)
		{
			document.getElementById('div_web').style.display="block" ;
		}
		if(crm_login)
		{
			document.getElementById('div_crm').style.display="block" ;
		}
		
	}else{
		document.getElementById('divloginsucess').style.display="none" ;
		document.getElementById('divloginform').style.display="block" ;
	}
}

function logout(){
	var postData = "";
	document.getElementById('divloginform').innerHTML="正在注销,请稍候...";
 
	var gourl=absbaseurl_website+"?do=logout";
	makeRequest(gourl, 'doLogoutWebsite', 'POST', postData);
}
function doLogoutWebsite() { 	 
	if (http_request.readyState == 4) {	 		
		if (http_request.status == 200) {			
			var messagereturn = http_request.responseText;
			if (messagereturn.indexOf("<ajax::error>")!=-1) {
				website_login = false; //website status not logged				 
			} else if (messagereturn.indexOf("<ajax::success>")!=-1) {
				website_login = false;
			}				
			  logoutBBS();
		}  else {
			alert('There was a problem with the request6.');
		}
	}
}
//转执行判断BBS是否登录
function logoutBBS(){
	var postData = "";
	var gourl=absbaseurl_bbs+"?do=logout";
	 
	makeRequest(gourl, 'dologoutBBS', 'POST', postData);
}
function dologoutBBS () {			
	if (http_request.readyState == 4) {	 	
		if (http_request.status == 200) {
			var messagereturn = http_request.responseText;
			//alert(messagereturn);
			if (messagereturn.indexOf("<ajax::error>")!=-1) {
				bbs_login = false; //website status not logged
			} else if (messagereturn.indexOf("<ajax::success>")!=-1) {
				bbs_login = false;
			}
			LogoutEnd();
		}  else {
			alert('There was a problem with the request7.');
		}
	}
}
function LogoutEnd()
{
    crm_login = false;
    bbs_login = false;
	// alert(bbs_login+" "+ website_login+" "+ crm_login)
	if(bbs_login||website_login||crm_login)
	{
		document.getElementById('divloginsucess').style.display="block" ;
		document.getElementById('divloginform').style.display="none" ;
		document.getElementById('div_username').innerHTML = username;
		if(bbs_login)
		{
			document.getElementById('div_bbs').style.display="block" ;
		}
		if(website_login)
		{
			document.getElementById('div_web').style.display="block" ;
		}
		if(crm_login)
		{
			document.getElementById('div_crm').style.display="block" ;
		}
		
	}else{
	     //alert("登陆失败,请检查账号");
		//document.getElementById('divloginsucess').style.display="none" ;
		//document.getElementById('divloginform').style.display="block" ;
		//document.getElementById('divloginform').innerHTML="	<table border=0 cellspacing=6 cellpadding=0>        <tr>              <td>用户名</td>              <td><input  name=username type=text id=username size=15 /></td> </tr>   <tr>  <td>密 &nbsp;码</td>        <td><input  name=password type=password id=password size=15 /></td>        </tr>            <tr>          <td></td>          <td><table border=0 cellspacing=0 cellpadding=0>                <tr>                  <td><img src=images/index-icon_21.gif onclick=\"javascript:ajaxLogin_submit('login')\" style=\"cursor:hand;\"/></td>                  <td>&nbsp;&nbsp;注 册</td>                </tr>              </table></td>            </tr>          </table>";
		ShowLogin();
	}
}




 