/*!
 * jQuery JavaScript Library v1.4.2
 * http://jquery.com/
 *
 * Copyright 2010, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://jquery.org/license
 *
 * Includes Sizzle.js
 * http://sizzlejs.com/
 * Copyright 2010, The Dojo Foundation
 * Released under the MIT, BSD, and GPL Licenses.
 *
 * Date: Sat Feb 13 22:33:48 2010 -0500
 */


/**
 *  登录注销js接口
 *  产生登录注销信息，也就是url
 *  使用说明:
 *    	1. 登录
 *		    var aclogon = new ACLogon();
 *			var url = aclogon.logon("https://200.200.65.145/cgi-bin/caauth.cgi?ui=web&opr=logon&chk_cookie=0&info=","用户名"，"显示名"，"组路径");
 *			将产生的url放入浏览器地址栏，或者使用ajax提交均可
 *			如：使用jquery ajax提交
 *				$.get(url,[option],[callback])；
 *      2. 注销
 *		   将urlprefix改为"https://200.200.65.145/cgi-bin/caauth.cgi?ui=web&opr=logout&chk_cookie=0&info=""即可
 *
 *  @file 	ACLogon.js
 *  @author asongnic@gmail.com
 *  @date   2010/09/30
 */


function ACLogon() {
	var encoding_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";// 64个

	var base64_encode = function(plain) {
		var cipher,i,len;
		var c1,c2,c3;
		i = 0;
		cipher="";
		len = plain.length;
		while (i<len) {
			c1 = plain.charCodeAt(i++) & 0xff;
			if (i == len) {
				cipher += encoding_table.charAt(c1>>2);
				cipher += encoding_table.charAt((c1&0x3) << 4);
				cipher += "==";
				break;
			}
			c2 = plain.charCodeAt(i++);
			if (i == len) {
				cipher += encoding_table.charAt(c1>>2);
				cipher += encoding_table.charAt(((c1&0x3) << 4)|((c2&0xF0)>>4));
				cipher += encoding_table.charAt((c2 & 0xF) << 2);
				break;
			}
			c3 = plain.charCodeAt(i++);
			cipher += encoding_table.charAt(c1 >> 2);
			cipher += encoding_table.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
			cipher += encoding_table.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
			cipher += encoding_table.charAt(c3 & 0x3F);
		}
		return cipher;
	};
	// 
	var toUTF8 = function(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;
	
	};
	/**
	 *	@description, 产生发送到AC认证的信息
	 *  @param {String} urlprefix，如 https://200.200.65.145/cgi-bin/caauth.cgi?ui=web&opr=logon&chk_cookie=0&info=
	 *       
	 *  	登录发送urlprefix可以为:https://200.200.65.145/cgi-bin/caauth.cgi?ui=web&opr=logon&chk_cookie=0&info=
	 *  	注销urlprefix可以为:https://200.200.65.145/cgi-bin/caauth.cgi?ui=web&opr=logout&chk_cookie=0&info=
	 *
	 *  @param {String} ip ，认证用户的ip地址
	 *  @param {String} username，认证用户的用户名
	 *  @param {String} group  认证用户的组路径
	 *  @return {String} 发送的url
	 *  @notes 不检查参数的合法性
	
	 */
	this.logon = function(urlprefix,ip,username,group){
		var plain = toUTF8(ip+"/"+username+"/"+""+"/"+(group?group:""));
		var info   = encodeURIComponent(base64_encode(plain));
		return urlprefix+info;
	};
}
/**
	 *	@description, 产生发送到AC认证的信息
	 *  @param {String} acip ，AC的ip
	 *  @param {String} opr ，"logon"或者"logout"
	 *  @param {String} ip ，认证用户的ip地址
	 *  @param {String} username，认证用户的用户名
	 *  @param {String} group  认证用户的组路径
	 *  @return {String} 发送的url
	 *  @notes 不检查参数的合法性
	
	 */
function sendToAc(acip,opr,ip,usr,group ){
	var lo = new ACLogon();
	var url = "http://"+acip+":85/cgi-bin/caauth.cgi?ui=web&opr="+opr+"&chk_cookie=0&info=";
		url = lo.logon(url,ip,usr,group);
	var ifr = $('<iframe id="ifr_hide_send_to_ac" style="display:none;"></iframe>');
		ifr.attr("src",url);
		if (!!$('#ifr_hide_send_to_ac')[0]){
			$('#ifr_hide_send_to_ac').remove();	
		}
		$('body').append(ifr);
		
}
