// JavaScript Document
var xmlHttp=createXmlHttpRequestObject();

function createXmlHttpRequestObject(){	
		var xmlHttp;		
		try{
				xmlHttp=new XMLHttpRequest();
		}catch(e){
			var xmlHttpVersions=new Array('Microsoft.XMLHTTP','MSXML2.XMLHTTP.6.0','MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP');
			for(var i=0;i<xmlHttpVersions.length&&!xmlHttp;i++){
				try{
					xmlHttp=new ActiveXObject(xmlHttpVersions[i]);
				}
				catch(e) {}
			}
		}
		if(!xmlHttp)
			alert('Error! Cant not create object xmlHttp!');
			else return xmlHttp;
	}
	
function open_login(){
	if(xmlHttp.readyState==4||xmlHttp.readyState==0){
		xmlHttp.open("GET","../ajax/show_login_info.php",true);
		xmlHttp.send(null);
		xmlHttp.onreadystatechange=handleServerResponse_show_info;
	}else
		setTimeout('open_login()',1000);
}

function close_login(){
	var a=document.getElementById('login_information');
	if(a){
		var newNode = document.createElement("span");
    	var newText = document.createTextNode("　");
    	newNode.appendChild(newText);
    	a.parentNode.replaceChild(newNode, a);
	}
}

function handleServerResponse_show_info(){
	if(xmlHttp.readyState==4){
		if(xmlHttp.status==200){	
			xmlResponse=xmlHttp.responseXML;
			xmlDoc=xmlResponse.documentElement;	
			
	if(xmlDoc.hasChildNodes()){	//if the returned xml has childnodes not an empty xml
		if(xmlDoc.firstChild.nodeName=='error'){//if error tage exists then user does not login
				show_login_box();
		}else{
						if(xmlDoc.getElementsByTagName("id"))
						var id=xmlDoc.getElementsByTagName("id")[0].firstChild.data;
						if(xmlDoc.getElementsByTagName("type"))
						var type=xmlDoc.getElementsByTagName("type")[0].firstChild.data;
						if(xmlDoc.getElementsByTagName("points"))
						var points=xmlDoc.getElementsByTagName("points")[0].firstChild.data;
						show_login_info(id,type,points)
						}
				}
		}else
			alert('There was a problem accessing server'+xmlHttp.statusText);
	}
}

function show_login_info(id,type,points){
//	alert('type='+type+' id='+id+' points='+points);
	if(document.getElementById("login_info")){
		var info=document.getElementById("login_info");
			var html='<div id=\'login_information\'><table width=\'850\'><tr align=left><td>';
			if(type=='f'){
					html+='会員情報'+id+'様<font color=#ff6600>ログイン中</font><br>';
					html+='保有ポイント数　<font color=#FF0000>'+points+'</font><br>';
					html+='<a href=http://www.freelancer.jp/profile.php?id=2>プロフィール</a>･･情報公開が可能<br>';
					html+='<a href=http://www.freelancer.jp/resume.php>履歴書作成</a>･･PDFで保存、印刷';
					html+='<a href=http://www.freelancer.jp/ssl/order.php>ポイント購入</a>&nbsp;&nbsp;';
					html+='<a href=http://www.freelancer.jp/ssl/trade_record.php>購入履歴</a><br>';
					html+='<a href=http://www.freelancer.jp/favourite.php?type=f>ブックマーク</a><br>';
					html+='<span onclick=\"log_out()\" style=\"text-decoration:underline; cursor:pointer; color:#0000FF\">ログアウト</span></div>';
					info.innerHTML=html;
			}else{
				html+='会員情報'+id+'様<font color=#ff6600>ログイン中</font><br>';
					html+='保有ポイント数　<font color=#FF0000>'+points+'</font><br>';
					html+='<a href=http://www.freelancer.jp/profilec.php?id=2>プロフィール</a>･･情報公開が可能<br>';
					html+='<a href=http://www.freelancer.jp/free-resume.php>履歴書作成</a>･･PDFで保存、印刷';
					html+='<a href=http://www.freelancer.jp/ssl/order.php>ポイント購入</a>&nbsp;&nbsp;';
					html+='<a href=http://www.freelancer.jp/ssl/trade_record.php>購入履歴</a><br>';
					html+='<a href=http://www.freelancer.jp/favourite.php?type=f>ブックマーク</a><br>';
					html+='<span onclick=\"log_out()\" style=\"text-decoration:underline; cursor:pointer; color:#0000FF\">ログアウト</span></div>';
					info.innerHTML=html;
			}
	}
}

function login_check(){
	if(xmlHttp.readyState==4||xmlHttp.readyState==0){
		//get the user information
		var str='?type=';
		
		if(document.getElementsByName('user_type')){
		if(document.getElementsByName('user_type')[0].checked)
		str+=document.getElementsByName('user_type')[0].value;
		else
			str+=document.getElementsByName('user_type')[1].value;
	}
	
	if(document.getElementById('user_id'))
		str+='&user_id='+document.getElementById('user_id').value;
	if(document.getElementById('pwd'))
		str+='&pwd='+document.getElementById('pwd').value;
	//end
	
	//this part of code is used to show a dynamic pic
	//var a=document.getElementById('login_info');
//	var b=document.getElementById('login_information');
//	if(a){
//			if(b){
//				a.innerHTML='<img src=\'\'';
//			}
//	}
	//end
		xmlHttp.open("GET","../ajax/show_login_info.php"+str,true);
		xmlHttp.send(null);
		xmlHttp.onreadystatechange=handleServerResponse_show_info;
	}else
		setTimeout('login_check()',1000);
}



function show_login_box(){
		if(document.getElementById("login_info")){
			var info=document.getElementById("login_info");
			var html='<div id=\'login_information\'><table width=\'850\'><tr align=left><td>';
			html+='<img src=\'../images/spacer.gif\' width=19 height=1 />会員ID: <input type=text id=\'user_id\'><br>';
			html+='パスワード: <input type=password id=\'pwd\'><br>';
			html+='選択 :<input type=radio name=\'user_type\' value=\'f\' checked=\'checked\'>フリーランサー&nbsp;&nbsp;&nbsp;<input type=radio name=\'user_type\' value=\'c\'>企業の方<br>';
			html+='<button onclick=\"login_check()\">ロゲイン</button>';
			html+='</div>';
			info.innerHTML=html;
	}
}

function log_out(){
	if(xmlHttp.readyState==4||xmlHttp.readyState==0){
		xmlHttp.open("GET","../exit.php",true);//log_out user and destory session
		xmlHttp.send(null);
		show_login_box();
//		xmlHttp.onreadystatechange=handleServerResponse_show_info;
	}else
		setTimeout('log_out',1000);
}