var jQuery = $;
var isLogin = null;

function checkemail(str)
{
	//在JavaScript中，正则表达式只能使用"/"开头和结束，不能使用双引号
 	var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 
 	var objExp=new RegExp(Expression);
 	if(objExp.test(str)==true){
  		return true;
 	}
 	else
 	{
  		return false;
 	}
}

function checkMail(myform)
{
//	var myMail = myform.myMail.value;
	var frMail = myform.friendMail.value;
	if(frMail=="")
	{
		alert("请输入Email地址!");
		myform.friendMail.focus();
		return;
	}
//	var flag1 = checkemail(myMail);
	var flag2 = checkemail(frMail);
	if(flag2)
	{
 		myform.submit();
		alert("Email已经发送。");
		return;
	}
	else
	{
		alert("您输入Email地址不正确!");
		myform.friendMail.focus();
		return;
	}
}

function doTopSearch()
  {
	  if(top_searchForm.searchText.value=="")
	  {
		  alert('请输入您要搜索的内容');
		  top_searchForm.searchText.focus();
		  return;
	  }
	  top_searchForm.caction.value = "doSearch";
	  top_searchForm.submit();
  }
  
  function doAdvancedSearch()
  {
	  top_searchForm.caction.value = "doAdvancedSearch";
	  top_searchForm.submit();
  }
  var xmlHttp;
  function checkSmsLogin()
  {
  	//createXMLHttpRequest();
  	//var url="/loginhelp";
	  //xmlHttp.onreadystatechange=handleStateChange2;
	  //xmlHttp.open("GET",url,true);
	  //xmlHttp.send(null);
	 
	  	var url = "/loginhelp?callback=?";
		  jQuery.getJSON(url, null, function call(result) {
				loginRes2(result);
		  });
  }
  
  function chooseDisplay()
  {
	  //createXMLHttpRequest();
	  //var url="/loginhelp";
	  var url = "/loginhelp?callback=?";
	  jQuery.getJSON(url, null, function call(result) {
			loginRes(result);
	  });
	  //showChooseDiv();
	  //xmlHttp.onreadystatechange=handleStateChange;
	  //xmlHttp.open("GET",url,true);
	  //xmlHttp.send(null);
  }
  
  function loginRes2(result) {
  		jQuery.each(result.data, function(i, user) {
				isLogin = user.isLogin;
				//userName = user.userName;
				checkLogin();
			});
  }
  
  function loginRes(result) {
  		jQuery.each(result.data, function(i, user) {
				isLogin = user.isLogin;
				//userName = user.userName;
				showChooseDiv();
			});
  }
  
  function createXMLHttpRequest()
  {
	  if(window.ActiveXObject)
	  {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	  else if(window.XMLHttpRequest)
	  {
		  xmlHttp=new XMLHttpRequest();
	  }
  }
  function handleStateChange2()
{
  if(xmlHttp.readyState==4)
  {
	  if(xmlHttp.status==200)
	  { 		
		  checkLogin();
	  }
  }
}
  function handleStateChange()
{
  if(xmlHttp.readyState==4)
  {
	  if(xmlHttp.status==200)
	  { 		
		  showChooseDiv();
	  }
  }
}
 function checkLogin()
 {
  	//var results = xmlHttp.responseXML.getElementsByTagName("isLogin");
  	//for(var i = 0; i < results.length; i++)
  	//{
  	//	isLogin = results[i].firstChild.nodeValue;
  	//}
  	if(isLogin==true)
  	{
  		 	var sendMobile = sendsmsfrm.sendMobile.value;
 // 	var sendContent = sendsmsfrm.sendContent.value;
	  	if (!trimAll(sendMobile))
		{
			alert("请填写您的手机号码");
			sendsmsfrm.sendMobile.focus();
			return false;
		}
		if (sendMobile.length != 11)
		{
			alert("您输入的手机号码长度不对,请重新输入");
			sendsmsfrm.sendMobile.focus();
			return false;
		}
		var smsClientUrl = document.URL;
		document.sendsmsfrm.smsClientUrl.value=smsClientUrl;
		sendsmsfrm.submit();		
		alert("推荐短信已经发送!");
		return;
	}
  	else
	{
		window.showModalDialog("/city/sendMail_login.jsp",window,"dialogHeight:300px;dialogWidth:400px;edge:Sunken;center:yes;help:no;resizable:no;Status:no;scroll:no;");
    }
  }
 
  
  function showChooseDiv()
  {
	  //var results = xmlHttp.responseXML.getElementsByTagName("isLogin");
	  //for(var i = 0; i < results.length; i++)
	   //{
		 // isLogin = results[i].firstChild.nodeValue;
	   //}
	   if(isLogin==true)
	   {
		  var clientUrl = document.URL;
		  document.sendmailfrm.clientUrl.value=clientUrl;
		  checkMail(sendmailfrm);
		  return false;
	   }
	   else
	   {
		  window.showModalDialog("/city/sendMail_login.jsp",window,"dialogHeight:300px;dialogWidth:400px;edge:Sunken;center:yes;help:no;resizable:no;Status:no;scroll:no;");
//	  	 	window.open("/city/sendMail_login.jsp");
	   }
  }
