///////////////////////////////////////////////////////////
// chinesesavvy.com membership common util
// author : shixin
// createTime : 2007-04-01
// using : nothing
///////////////////////////////////////////////////////////
function chinesesavvy_doPost(_postUrl) {
	var _utilScript = null;
	var _number = Math.random();
	_utilScript = document.createElement("script");
	_utilScript.setAttribute("type", "text/javascript");
	_utilScript.setAttribute("language", "javascript");
	_utilScript.setAttribute("src", _postUrl);
	_utilScript.setAttribute("id", "chinesesavvy_comment_" + _number);
	document.getElementsByTagName("head").item(0).appendChild(_utilScript);
	return false;
}
function chinesesavvy_appendDiv(_divId, _html) {
	var _utilDiv = null;
	var _number = Math.random();
	_utilDiv = document.createElement("div");
	_utilDiv.setAttribute("id", _divId);
	_utilDiv.setAttribute("style", "display:none");
	_utilDiv.innerHTML = _html;
	document.getElementsByTagName("body").item(0).appendChild(_utilDiv);
	return false;
}
function chinesesavvy_appendBackGroudForm(_formId, _formAction, _html) {
	var _utilForm = null;
	var _number = Math.random();
	_utilForm = document.createElement("form");
	_utilForm.setAttribute("id", _formId);
	_utilForm.setAttribute("style", "display:none");
	_utilForm.setAttribute("action", _formAction);
	_utilForm.setAttribute("method", "post");
	_utilForm.innerHTML = _html;
	document.getElementsByTagName("body").item(0).appendChild(_utilForm);
	return _utilForm;
}
function chinesesavvy_appendBackGroudFormJson(_formId, _formAction, _values) {
	var _utilForm = null;
	var _number = Math.random();
	_utilForm = document.createElement("form");
	_utilForm.setAttribute("id", _formId);
	_utilForm.setAttribute("style", "display:none");
	_utilForm.setAttribute("action", _formAction);
	_utilForm.setAttribute("method", "post");
	for (var itm in _values) {
		var _name = itm;
		var _val = _values[itm];
		var _input = document.createElement("input");
		_input.setAttribute("name", _name);
		_input.setAttribute("value", _val);
		_input.setAttribute("type", "hidden");
		_utilForm.appendChild(_input);
	}
	document.getElementsByTagName("body").item(0).appendChild(_utilForm);
	return _utilForm;
}
function $debug(_obj) {
	if (_obj) {
		var s = "";
		for (var itm in _obj) {
			s += "obj." + itm + "=" + _obj[itm] + ";";
		}
		alert(s);
	}
}

function $debugstr(_obj) {
	if (_obj) {
		var s = "";
		for (var itm in _obj) {
			s += "obj." + itm + "=" + _obj[itm] + ";";
		}
		return s;
	}
	return "";
}

/////////////////////////////////////////////////////////////
// cookie
/////////////////////////////////////////////////////////////
function chinesesavvy_getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) {
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function chinesesavvy_setCookie(name, value) {
	var expdate = new Date();
	var argv = chinesesavvy_setCookie.arguments;
	var argc = chinesesavvy_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;
	if (expires != null) {
		expdate.setTime(expdate.getTime() + (expires * 1000));
	}
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expdate.toGMTString())) + ((path == null) ? "" : ("; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) + ((secure == true) ? "; secure" : "");
}

function chinesesavvy_delCookie(name) {
	var exp = new Date();
	exp.setTime(exp.getTime() - 1);
	var cval = chinesesavvy_getCookie(name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

function chinesesavvy_getCookie(name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) {
			return chinesesavvy_getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) {
			break;
		}
	}
	return null;
}

/////////////////////////////////////////////////////////////
// base64
/////////////////////////////////////////////////////////////

var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var base64DecodeChars = new Array(
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
    -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);

function base64encode(str) {
    var out, i, len;
    var c1, c2, c3;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	c1 = str.charCodeAt(i++) & 0xff;
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt((c1 & 0x3) << 4);
	    out += "==";
	    break;
	}
	c2 = str.charCodeAt(i++);
	if(i == len)
	{
	    out += base64EncodeChars.charAt(c1 >> 2);
	    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	    out += base64EncodeChars.charAt((c2 & 0xF) << 2);
	    out += "=";
	    break;
	}
	c3 = str.charCodeAt(i++);
	out += base64EncodeChars.charAt(c1 >> 2);
	out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
	out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
	out += base64EncodeChars.charAt(c3 & 0x3F);
    }
    return out;
}

function base64decode(str) {
    var c1, c2, c3, c4;
    var i, len, out;

    len = str.length;
    i = 0;
    out = "";
    while(i < len) {
	/* c1 */
	do {
	    c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c1 == -1);
	if(c1 == -1)
	    break;

	/* c2 */
	do {
	    c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
	} while(i < len && c2 == -1);
	if(c2 == -1)
	    break;

	out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

	/* c3 */
	do {
	    c3 = str.charCodeAt(i++) & 0xff;
	    if(c3 == 61)
		return out;
	    c3 = base64DecodeChars[c3];
	} while(i < len && c3 == -1);
	if(c3 == -1)
	    break;

	out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

	/* c4 */
	do {
	    c4 = str.charCodeAt(i++) & 0xff;
	    if(c4 == 61)
		return out;
	    c4 = base64DecodeChars[c4];
	} while(i < len && c4 == -1);
	if(c4 == -1)
	    break;
	out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
    }
    return out;
}

function utf16to8(str) {
    var out, i, len, c;

    out = "";
    len = str.length;
    for(i = 0; i < len; i++) {
	c = str.charCodeAt(i);
	if ((c >= 0x0001) && (c <= 0x007F)) {
	    out += str.charAt(i);
	} else if (c > 0x07FF) {
	    out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
	    out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
	    out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
	} else {
	    out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
	    out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
	}
    }
    return out;
}

function utf8to16(str) {
    var out, i, len, c;
    var char2, char3;

    out = "";
    len = str.length;
    i = 0;
    while(i < len) {
	c = str.charCodeAt(i++);
	switch(c >> 4)
	{ 
	  case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
	    // 0xxxxxxx
	    out += str.charAt(i-1);
	    break;
	  case 12: case 13:
	    // 110x xxxx   10xx xxxx
	    char2 = str.charCodeAt(i++);
	    out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
	    break;
	  case 14:
	    // 1110 xxxx  10xx xxxx  10xx xxxx
	    char2 = str.charCodeAt(i++);
	    char3 = str.charCodeAt(i++);
	    out += String.fromCharCode(((c & 0x0F) << 12) |
					   ((char2 & 0x3F) << 6) |
					   ((char3 & 0x3F) << 0));
	    break;
	}
    }

    return out;
}

function base64_encode_safe(_input){
	return base64encode(utf16to8(_input))
}

function base64_decode_safe(_input){
	return utf8to16(base64decode(_input));
}


/////////////////////////////////////////////////////////////
function cs_member_getLoginServiceUrl(_hostname) {
	var _host = _hostname ? _hostname : document.location.host;
	var _url = "http://" + _host + "/services/member/login.ot";
	return _url;
}
function cs_member_getLogoutServiceUrl(_hostname) {
	var _host = _hostname ? _hostname : document.location.host;
	var _url = "http://" + _host + "/services/member/logout.ot";
	return _url;
}
function cs_member_getRegisterServiceUrl(_hostname){
	var _host = _hostname ? _hostname : document.location.host;
	var _url = "http://" + _host + "/services/member/register.ot";
	return _url;
}

//////////////////////////////////////////////////////
// register
//////////////////////////////////////////////////////

// submit the register form
function submitRegisterForm(_form, _callbackZone) {
	return false;
}


//////////////////////////////////////////////////////
// login
//////////////////////////////////////////////////////
/**
* parameters:
*	_hostname	:	login service host
*	_username	:
*	_password	:
*	callbackFunction	:	login result function
*/
function cs_member_loginByScript(_hostname, _username, _password, _callbackFunction) {
	var _url = cs_member_getLoginServiceUrl(_hostname) + "?event=Byscript&callback=" + _callbackFunction + "&loginName=" + _username + "&loginPwd=" + _password;
	chinesesavvy_doPost(_url);
	return false;
}

/**
* logout by script
*/
function cs_member_logoutByScript(_hostname,_callbackFunction){
	var _url = cs_member_getLogoutServiceUrl(_hostname) + "?event=Byscript&callback=" + _callbackFunction;
	chinesesavvy_doPost(_url);
	return false;
}

/**
* login by redirect
* parameters:
* 	_hostname	:	login service host
*	_username	:
*	_password	:
*	_callbackUrl:	redirect url
*	_isAll		:	is either successful or faild redirect
*/
function cs_member_loginByRedirect(_hostname, _username, _password, _callbackUrl, _isAll) {
	var _type = _isAll ? "Byredirectall" : "Byredirectok";
	var _url = cs_member_getLoginServiceUrl(_hostname);
	var _tmpId = "_cs_member_utilform_" + Math.random();
	var _form = chinesesavvy_appendBackGroudFormJson(_tmpId, _url, {loginName:_username, loginPwd:_password, callback:_callbackUrl, event:_type});
	_form.submit();
	return false;
}


function cs_member_logoutByRedirect(_hostname, _callbackUrl, _isAll) {
	var _type = "Byredirect";
	var _url = cs_member_getLogoutServiceUrl(_hostname);
	var _tmpId = "_cs_member_utilform_" + Math.random();
	var _form = chinesesavvy_appendBackGroudFormJson(_tmpId, _url, {callback:_callbackUrl, event:_type});
	_form.submit();
	return false;
}


function cs_member_loginByRedirectThisPage(_hostname, _username, _password, _isAll) {
	var _type = _isAll ? "Byredirectall" : "Byredirectok";
	var _url = cs_member_getLoginServiceUrl(_hostname);
	var _tmpId = "_cs_member_utilform_" + Math.random();
	var _callback = document.location.href;
	var _form = chinesesavvy_appendBackGroudFormJson(_tmpId, _url, {loginName:_username, loginPwd:_password, callback:_callback, event:_type});
	_form.submit();
	return false;
}

function cs_member_registerByRedirectThisPage(_hostname){
	var _callback = document.location.href;
	var _url = cs_member_getRegisterServiceUrl(_hostname);
	var _tmpId = "_cs_member_utilform_" + Math.random();
	var _form = chinesesavvy_appendBackGroudFormJson(_tmpId, _url, {callback:_callback});
	_form.submit();	
	return false;
}

function cs_member_logoutByRedirectThisPage(_hostname, _isAll) {
	var _type = "Byredirect"; 
	var _url = cs_member_getLogoutServiceUrl(_hostname);
	var _tmpId = "_cs_member_utilform_" + Math.random();
	var _callback = document.location.href;
	var _form = chinesesavvy_appendBackGroudFormJson(_tmpId, _url, {callback:_callback, event:_type});
	_form.submit();
	return false;
}

///////////////////////////////////////////////////////
// user info access
///////////////////////////////////////////////////////

/**
* test is login
*/
function cs_member_isLogin() {
	return chinesesavvy_getCookie("CS_U_LOGINNAME")!=null;
}

/**
* get login user info
* 	userInfo:{
*		userName:"user name",
*       userEmail:"user email",
*       userLanguage:"user default language",
*       userCountry:"user default country",
*       userGroup:"user groups"
*   }
*/
function cs_member_getLoginUserInfo(){
	if(cs_member_isLogin()){
		return {
			userName:base64_decode_safe(chinesesavvy_getCookie("CS_U_NAME")),
			userEmail:chinesesavvy_getCookie("CS_U_LOGINNAME"),
			userLanguage:chinesesavvy_getCookie("CS_U_LANGUAGE"),
			userCountry:chinesesavvy_getCookie("CS_U_COUNTRY"),
			userGroup:chinesesavvy_getCookie("CS_U_GROUP"),
			userAvatar:chinesesavvy_getCookie("CS_U_AVATAR"),
			userMemberId:chinesesavvy_getCookie("CS_U_MEMBERID")
			};
	}else{
		return null;
	}
}

/**
* reload user status
* parameters :
*   fun	: callback function whicth should recieve 2 arguments(isLogin,userInfo)
*/
function cs_member_reloadUserStatus(_fun){
	if(cs_member_isLogin()){
		_fun(true,cs_member_getLoginUserInfo());
	}else{
		_fun(false,null);
	}
}

//////////////////////////////////////////////////////


