var NS = (navigator.appName == "Netscape") ? true : false;

function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function isEmpty(o){
	if (isObject(o)){
		for (var i in o){
			return false;
		}
	}
return true;
}

function isIEObject(a){
	return isObject(a) && typeof a.constructor != 'function';
}

function print_r(theObj){
  if(theObj.constructor == Array ||
     theObj.constructor == Object){
    document.write("<ul>")
    for(var p in theObj){
      if(theObj[p].constructor == Array||
         theObj[p].constructor == Object){
document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
        document.write("<ul>")
        print_r(theObj[p]);
        document.write("</ul>")
      } else {
document.write("<li>["+p+"] => "+theObj[p]+"</li>");
      }
    }
    document.write("</ul>")
  }
}

function base64_encode(decStr){
var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
decStr=escape(decStr);		//line add for chinese char
  var bits, dual, i = 0, encOut = '';
  while(decStr.length >= i + 3){
    bits =
    (decStr.charCodeAt(i++) & 0xff) <<16 |
    (decStr.charCodeAt(i++) & 0xff) <<8  |
     decStr.charCodeAt(i++) & 0xff;
    encOut +=
     base64s.charAt((bits & 0x00fc0000) >>18) +
     base64s.charAt((bits & 0x0003f000) >>12) +
     base64s.charAt((bits & 0x00000fc0) >> 6) +
     base64s.charAt((bits & 0x0000003f));
    }
  if(decStr.length -i > 0 && decStr.length -i < 3){
    dual = Boolean(decStr.length -i -1);
    bits =
     ((decStr.charCodeAt(i++) & 0xff) <<16) |
     (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
    encOut +=
      base64s.charAt((bits & 0x00fc0000) >>18) +
      base64s.charAt((bits & 0x0003f000) >>12) +
      (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') +
      '=';
    }
  return encOut
}

function base64_decode(encStr){
var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  var bits, decOut = '', i = 0;
  for(; i<encStr.length; i += 4){
    bits =
     (base64s.indexOf(encStr.charAt(i))    & 0xff) <<18 |
     (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | 
     (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 |
      base64s.indexOf(encStr.charAt(i +3)) & 0xff;
    decOut += String.fromCharCode(
     (bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
    }
  if(encStr.charCodeAt(i -2) == 61)
    undecOut=decOut.substring(0, decOut.length -2);
  else if(encStr.charCodeAt(i -1) == 61)
    undecOut=decOut.substring(0, decOut.length -1);
  else undecOut=decOut;
  
  return unescape(undecOut);		//line add for chinese char
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

var setCheckedValue = function(radioObj, newValue){
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

/* BEGIN: EFFECT PRESET */
function showHide(elem1,elem2){
	if(!Element.visible(elem1)){
		Effect.BlindDown(elem1);
	}
	if(Element.visible(elem2)){
		Effect.BlindUp(elem2);
	}
}
function show(elem){
	if(!Element.visible(elem)){
		Effect.BlindDown(elem);
	}
}

function hide(elem){
	if(Element.visible(elem)){
		Effect.BlindUp(elem);
	}
}

var msgInterval;
var msgShowing = false;
var msgTimer;
var msgString;
function showMessage(str){
	elem = 'thisMessage';
	msgString = str;
	
	window.clearInterval(msgInterval);
	window.clearTimeout(msgTimer);
	if(msgShowing == false){
		finalString  = '<div style="text-align:right;background:#5B77A7;margin:0;cursor:pointer;padding:2px">x</div>';
		finalString += '<div style="margin:0;padding:8px;background:#E5E5E5">'+str+'</div>';
		
		$(elem).innerHTML = finalString;
		Element.show(elem);
		
		msgInterval = window.setInterval("centerElem('"+elem+"','200')",10);
		msgTimer = window.setTimeout("hideMessage()",10000);
		
		msgShowing = true;
	} else {
		window.setTimeout("showMessage(msgString)",500);
		Element.hide(elem);
		msgShowing = false;
	}
}

function hideMessage(){
	window.clearTimeout(msgTimer);
	window.clearInterval(msgInterval);
	new Effect.Fade('thisMessage',{queue: {position:'end',scope:'message'}});
	msgShowing = false;
}

// Immediately close message without transition
function closeMessage(){
	window.clearTimeout(msgTimer);
	window.clearInterval(msgInterval);
	Element.hide('thisMessage');
	msgShowing = false;
}

function constantScroll(elem){
	if(NS){
		thisScroll = window.pageYOffset;
		$(elem).style.top = thisScroll+'px';
	} else {
		thisScroll = document.documentElement.scrollTop;
		$(elem).style.pixelTop = thisScroll;
	}
}

var processing = false;
function showProcess(){
	if(!processing){
		elem = 'thisProcess';
		thisWidth = 140;
		thisHeight = 20;

		if(NS){
			thisScroll = window.pageYOffset;
		} else {
			thisScroll = document.documentElement.scrollTop;
		}
		Element.show(elem);
		

		thisLeft = 0;
		thisTop = 0;
		
		if(NS){
			$(elem).style.left = thisLeft+'px';
			$(elem).style.top = (thisTop+thisScroll)+'px';
		}
		else{
			$(elem).style.pixelLeft = thisLeft;
			$(elem).style.pixelTop = thisTop+thisScroll;
		}
		processing = true;
	}
}

function hideProcess(){
	if(processing){
		//new Effect.Fade('thisProcess',{queue: {position:'end',scope:'process'}});
		Element.hide('thisProcess');
		processing = false;
	}
}

function centerElem(elem){
  var dimensions = Element.getDimensions(elem);
  var thisWidth = dimensions.width;
  var thisHeight = dimensions.height;

	if(NS){
		thisScroll = window.pageYOffset;
	} else {
		thisScroll = document.documentElement.scrollTop;
	}
	offset = arguments.length > 1 ? arguments[1] : 30;
	thisScroll = thisScroll - offset;

	thisLeft = screen.availWidth  / 2 - thisWidth / 2;
	thisTop = screen.availHeight / 2 - thisHeight / 2;
	
	if(NS){
		$(elem).style.left = thisLeft+'px';
		$(elem).style.top = (thisTop+thisScroll)+'px';
	}
	else{
		$(elem).style.pixelLeft = thisLeft;
		$(elem).style.pixelTop = thisTop+thisScroll;
	}
}

/* END: EFFECT PRESET */

/* BEGIN: AJAX FUNCTIONS */
function ajaxThis(elem,url,pars){
	new Ajax.Updater(elem,url,{method: 'post', parameters: pars, evalScripts: true});
}

function reqThis(page,pars){
	showProcess();
	new Ajax.Request('rpc/'+page,{method: 'post', parameters: pars, evalScripts: true, onComplete: responseContent});
}

function logout(sid){
	new Ajax.Request('logout.php',{method: 'get', onComplete: secondaryLogout(sid)});
}
function secondaryLogout(sid){
	pars = '?logout=true&sid='+sid;
	new Ajax.Request('forum/login.php'+pars,{method: 'get', 
		onComplete: function(originalRequest){
			window.location="index.php";
		}
	});
}

function adminLogin(){
	var url = 'rpc-login.php';
	var pars = Form.serialize('login');
	reqThis(url,pars);
return false;
}

function responseContent(originalRequest){
	hideProcess();
	var xmldoc = originalRequest.responseText;
	var xotree = new XML.ObjTree();
	var tree = xotree.parseXML(xmldoc);
	var node = tree.result;

	function processNode(thisNode,func){
		if(isArray(thisNode)){
			for(i=0;i<thisNode.length;i++){
				processNode(thisNode[i],func);
			}
		} else {
			eval("node_"+func+"(thisNode);");
		}
	}

	function node_output(thisNode){
		theID = thisNode["-id"];
		theContents = thisNode["#cdata-section"];
		if(thisNode["-option"]){
			thisOption = thisNode["-option"];
		} else {
			thisOption = false;
		}

		if(theContents == undefined){
			theContents = thisNode["#text"];
		}
		if(theContents == undefined){
			theContents = '';
			if(Element.visible(theID)){
				Element.hide(theID);
			}
		}
		else if(!Element.visible(theID) && thisOption != 'hide'){
			Element.show(theID);
		}
		$(theID).innerHTML = theContents;

	}

	function node_formInput(thisNode){
		var theID = thisNode["-name"];
		var theVal = thisNode["#text"];
		if(theVal == undefined){
			theVal = '';
		}
		$(theID).value = theVal;
	}
	function node_formSelect(thisNode){
		var theID = thisNode["-name"];
		var theVal = thisNode["#text"];

		c=0;
		for(s=0;s<$(theID).childNodes.length;s++){
			if($(theID).childNodes[s].tagName == 'OPTION'){
				if($(theID).childNodes[s].value == theVal){
					$(theID).selectedIndex = c;
				}
			c++;
			}
		}
	}

	if(node.message){
		showMessage(node.message);
	}
	if(node.permsg){
		showPerMessage(node.permsg);
	}
	if(node.script){
		eval(node.script);
	}
	if(node.delayscript){
		delayTime = node.delayscript['-set'];
		theScript = node.delayscript['#text'];
		window.setTimeout("eval(theScript)",delayTime);
	}
	if(node.redirect){
		window.location=node.redirect;
	}
	if(node.output){
		processNode(node.output.element,'output');
	}

	try{
		if(node.form){
			if(isArray(node.form.input)){
				for(i=0;i<node.form.input.length;i++){
					processNode(node.form.input[i],'formInput');
				}
			}
			else{
				processNode(node.form.input,'formInput');
			}
			
			// SELECT
			if(isArray(node.form.select)){
				for(i=0;i<node.form.select.length;i++){
					processNode(node.form.select[i],'formSelect');
				}
			}
			else{
				processNode(node.form.select,'formSelect');
			}
		}
	} catch(e){
		alert(e);
	}

	try{
		if(node.checkbox){
			theForm = node.checkbox["-form"];
			theID = node.checkbox["-name"];
			objFormElements = document[theForm].elements;

			if(isArray(node.checkbox.value)){
				for(f=0;f < objFormElements.length; f++){
					if(objFormElements[f].name == theID){
						for(i=0;i<node.checkbox.value.length;i++){
							if(objFormElements[f].value == node.checkbox.value[i]){
								objFormElements[f].checked = true;
							}
						}
					}
				}
			}
			else{
				for(f=0;f < objFormElements.length; f++){
					if(objFormElements[f].name == theID && objFormElements[f].value == node.checkbox.value){
						objFormElements[f].checked = true;
						break;
					}
				}
			}
		}
	} catch(e){
		//alert(e);
	}
}

function debug(){
	new Ajax.Updater('debug','rpc-debug.php');
}

function rectData(){
	pars = Form.serialize('rectdata');
	reqThis('rectdata.php',pars);
return false;
}

function centerLeft(width){
  var thisWidth = width;
	thisLeft = screen.availWidth  / 2 - thisWidth / 2;
return thisLeft;
}

function centerTop(height){
  var thisHeight = height;
	thisTop = screen.availHeight  / 2 - thisHeight / 2;
return thisTop;
}

function openWindow(winName,winURL,width,height){
	thisLeft = centerLeft(width);
	thisTop = centerTop(height);

	thisWindow = window.open(winURL, winName, 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width='+width+',height='+height+',left = '+thisLeft+',top = '+thisTop);
	thisWindow.focus();

	//Event.observe(thisWindow,'blur',windowCleanup(),true);
	window.setTimeout("windowCleanup('"+winURL+"');",5000);
}
