﻿// JScript File

function $(elem)
{
   return document.getElementById(elem);
}

function show(ids)
{
   var idarr=ids.split("|");
   for(var i=0;i<idarr.length;i++)
   {
      if($(idarr[i])!=null)
         $(idarr[i]).style.display="";
   }
}

function hide(ids)
{
   var idarr=ids.split("|");
   for(var i=0;i<idarr.length;i++)
   {
      if($(idarr[i])!=null)
         $(idarr[i]).style.display="none";
   }
}

function alertError(msg)
{
   var _msg="sorry,error occurs, please try again";
   if(msg!=null&&msg!="")
      _msg=msg;
   alert(_msg);
}

function alertOK(msg)
{
   var _msg="Success!";
   if(msg!=null&&msg!="")
      _msg=msg;
   alert(_msg);
}

function checkAll(str)
{
 var a = document.getElementsByName(str);
 var n = a.length;
 for (var i=0; i<n; i++)
 a[i].checked = window.event.srcElement.checked;
}

function getCookieVal (offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie(name)
{
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for(var i=0;i<arrCookie.length;i++)
    {
    	var index=arrCookie[i].indexOf("=");
    	var l=arrCookie[i].length;
    	var n=arrCookie[i].substring(0,index);
    	var v="";
    	if(l>index+1)
    	  v=arrCookie[i].substring(index+1,l);
	    if (n==name) 
	    {
		    return unescape(v);
	    }
    }
    return "";
}

function SetCookie (name, value)
{
    var argv = SetCookie.arguments;
    var argc = SetCookie.arguments.length;
    var expires = (argc > 2) ? argv[2] : null;
    var path = (argc > 3) ? argv[3] : null;
    var domain = (argc > 4) ? argv[4] : null;
    var secure = (argc > 5) ? argv[5] : false;
    document.cookie = name + "=" + escape (value) +
    ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function User(id,name,pic)
{
   this.ID=id;
   this.Name=name;
   this.Pic=pic;
}

function getCurrentUser()
{
   var cookie=GetCookie("UserProfile");
   if(cookie=="")
      return null;
   else
   {
      var profile=cookie.split("&");
      var id=parseInt(profile[0].split("=")[1]);
      var name=unescape(profile[1].split("=")[1]);
      var pic=profile[2].split("=")[1];
      return new User(id,name,pic);
   }
}

function writeUserProfile(hidecount)
{
   var user=getCurrentUser();
   if(user==null)
   {
       document.write("<a href='http://talk.v.wenguo.com/login.aspx?returnurl="+escape(location.href)+"'>"+__Msg['login']+"</a>&nbsp; | &nbsp;<a href='http://talk.v.wenguo.com/reg.aspx'>"+__Msg['reg']+"</a>");
   }
   else
   {
      document.write("<a href='http://talk.v.wenguo.com/User/View.aspx'>"+user.Name+"</a>&nbsp;| &nbsp;<a id='ausermsg' href='http://talk.v.wenguo.com/my/message/list.aspx?type=2&InboxOrOut=1'>"+__Msg['inbox']+"<span id='msgcount' style='color:red;'></span></a>&nbsp; | &nbsp;<a href='http://talk.v.wenguo.com/logout.aspx'>"+__Msg['logout']+"</a>");
      if(hidecount==null)  
        getMsgCount();
    }
//        document.write(__Msg["logout"]);
}

function getMsgCount()
{
    Veduchina.Components.UserManager.GetMessageUnreadCount(getMsgCount_cb);
}

function getMsgCount_cb(res)
{
   //if(res.value!=null&&res.value>0)
     //$("msgcount").innerHTML="("+res.value+")";
   if(res.value!=null&&res.value.length>0)
     var msgcount=parseInt(res.value.split("|")[0]);
     var msgtype=res.value.split("|")[1];
     if(msgcount>0)
     {
        $("msgcount").innerHTML="("+msgcount+")";
        $("ausermsg").href="http://talk.v.wenguo.com/my/message/list.aspx?type=2&InboxOrOut=1";
     }
}

function convertToHtml(str)
{
   var pattern=new RegExp("(\r\n|\n)( |　){0,}","ig");
   return "　　"+str.replace(pattern,"<br/>　　");
}

function convertToText(str)
{
   var pattern=new RegExp("(<br/>)|(<br>)","ig");
   return str.replace(pattern,"\r\n");
}

function checkInput(reg)
{
	var srcElem = event.srcElement
	var oSel = document.selection.createRange()
	var srcRange = srcElem.createTextRange()
	oSel.setEndPoint("StartToStart", srcRange)
	var num = oSel.text + String.fromCharCode(event.keyCode) + srcRange.text.substr(oSel.text.length)
	event.returnValue = reg.test(num);
}



function ShowDetails(id)
{
    if($(id).style.display="none")
    {
        $(id).style.display="";
    }
    else
    {
        $(id).style.display="none";
    }
    
}

function searchUsers()
{
    var nativeLanguage=$("ctl00_selNativeLanguage").value;
    var studyLanguage=$("ctl00_selStudyLanguage").value;
    var fromCountry=$("ctl00_selFromCountry").value;
    var isOnline=$("ctl00_chkIsOnline").checked;
    var hasPhoto=$("ctl00_chkHasPhoto").checked;
    //alert(nativeLanguage+"|"+studyLanguage+"|"+fromCountry+"|"+isOnline+"|"+hasPhoto);
    //window.open("/share/user/searchlist.aspx?NativeLanguage="+nativeLanguage+"&StudyLanguage="+studyLanguage+"&FromCountry="+fromCountry+"&IsOnline="+isOnline+"&HasPhoto="+hasPhoto);
    location.href="http://talk.v.wenguo.com/user/searchlist.aspx?NativeLanguage="+nativeLanguage+"&StudyLanguage="+studyLanguage+"&FromCountry="+fromCountry+"&IsOnline="+isOnline+"&HasPhoto="+hasPhoto;
}


function delLeaveword(ID)
{
   if(!confirm("确定要删除此留言吗？"))
      return;   
   Veduchina.Components.UserManager.DeleteLeaveword(ID,delLeaveword_callback,ID);
}

function delLeaveword_callback(res)
{
    var ID=res.context;
    $("tr_"+ID).parentNode.removeChild($("tr_"+ID));
    resize();
}

//function addFriend(id)
//{
//   var user=getCurrentUser();
//   if(user==null)
//   {
//       alertError("请先登录或注册!");
//       return;
//   }
//   Veduchina.Components.UserManager.AddFriend('',id,addFriend_callback);
//}
//function addFriend_callback(res)
//{
//   if(res!=null&&res.value!=null)
//   {
//      if(res.value==true)
//      {
//         alertOK("添加好友成功！");
//         return;
//      }
//   }
//   alertError("对不起，请先登录！");
//}

