﻿function UserProfileControls()
{
	this.RESTResponse = "";
	
	var appID = new String();
	appID = "cd4dff22bfbf495aa69b207f1f7f6782";

	// class methods
	this.isUserLoggedIn = function()
	{
		result = getNamedCookieValue("loginstatus");
		if(result == "TRUE")
			return true;
		else
			return false;
	}

	this.userPUID = function()
	{
		result = getNamedCookieValue("puid");
		return result;
	}	
	
	this.signinurl = function()
	{
		result = getNamedCookieValue("signinurl");
		return result;	
	}
	
	this.signouturl = function()
	{
		result = getNamedCookieValue("signouturl");
		return result;	
	}	
	
	this.userFirstName = function()
	{
		result = getNamedCookieValue("firstname");
		return result;	
	}	

	this.userLastName = function()
	{
		result = getNamedCookieValue("lastname");
		return result;	
	}	
	
	this.userMemberName = function()
	{
		result = getNamedCookieValue("membername");
		return result;	
	}		
	
	
	this.emailIt = function(tomail,bodytext,fr_name,vurl)
	{
			popupEmailClient(tomail,bodytext,fr_name,vurl);
			return;
	}	
	
/*
	this.AddToSpace = function()
	{

		var msnSpacesApi="http://spaces.live.com/BlogIt.aspx?mkt=zh-hk&";

		if(this.isUserLoggedIn)
		{
			title = escape(rplayer.vidGetTitle());
			txt = "<embed src=\"http://images.video.msn.com/flash/soapbox1_1.swf\" width=\"432\" height=\"364\" id=\"celoik2u\" type=\"application/x-shockwave-flash\" allowFullScreen=\"true\" allowScriptAccess=\"always\" pluginspage=\"http://macromedia.com/go/getflashplayer\" flashvars=\"c=v&v=" + getVideoID() + "&ifs=true&fr=msnvideo&mkt=zh-HK\"></embed><noembed><a href=\"http://video.msn.com/video.aspx?vid=" + getVideoID() + "\" target=\"_new\" title=\"Water bird\">Video: " + rplayer.vidGetTitle() + "</a></noembed>";
			bnew = true;

			window.open(msnSpacesApi + "Title=" + title + 
		              "&SourceURL=" + escape(window.location.href.replace(window.location.hash, "")) + 
		              "&description=" + escape(txt), (bnew) ? "_toolbar" : "_self", "", bnew)
		}
		else
		{
			ans = confirm("您必須先登入，才能將這個影片上傳到分享空間。");
		}
	}	
*/
	
	function sendRESTRequest(requesturl)
	{
		var proxy = "http://video.hk.msn.com/wlid/rest_request.asp?q=";
		var xmlhttp=false; 
        try {
                xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); 
        } catch (e) {
                try {
                        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP'); 
				} catch (E) {
					xmlhttp = false;
               }
        }
        if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
                xmlhttp = new XMLHttpRequest(); 
        }
//        var file = requesturl; 
		xmlhttp.open('GET', proxy + escape(requesturl), true); 
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) { 
                var content = xmlhttp.responseText; 
                if( content ){ 
                      this.RESTResponse = content;
                }
			}
        }
        xmlhttp.send(null);
		return;
	}
	
	// method use inside the class
	function get_cookie ( cookie_name )
	{
	  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

	  if ( results )
	    return ( unescape ( results[2] ) );
	  else
	    return "";
	}

	function getNamedCookieValue(fieldname)
	{
		var userstatus = get_cookie("UserStatus");
		arr_cookie = userstatus.split("&");
		for(i=0;i<arr_cookie.length;i++)
		{
			sTmp = arr_cookie[i];
			arrTmp = sTmp.split("=");
			if(arrTmp[0] == fieldname)
				return arrTmp[1];
		}
		return "";
	}
	
	function popupEmailClient(tomail,bodytext,fr_name,vurl) 
	{
		var a=checkString(fr_name)?fr_name+" sent you a video!":"You received a video!",b = checkString(bodytext)?"Message:%0A" + bodytext.replace(/\n/g,"%0A"):"";
		window.location.href="mailto:"+tomail.replace(/,/g,";")+"?Subject="+a+"&body=Here's what I picked out for you. %0A%0A Video URL: " + escape(vurl) + "%0A           (If you are unable to click the link, please copy and paste the URL in the browser to watch the video.)%0A%0A"+ b; //最尾加 &#x3113; 係用黎出中文 US_Ansii 可以唔駛加
	};			
	
	function checkString(a){return typeof a=="string"&&a.length>0}
	
} // end class user profile