var ajax = new ClsAjax, ubb = new ClsUBBCode;
//======= class function ====================
function ClsAjax() {
  this.defaultURL = "", this.xmlHTTP = testSupport();
  //-------------------------------------------------
  this.query = function(strReq, innerTag, objForm, boolBar) {
  	var xh = this.xmlHTTP, oMethod = "POST";
  	strReq = !strReq ? this.defaultURL : this.defaultURL + strReq;
    if(!xh) { if(objForm) { objForm.method = "POST"; objForm.action = strReq; objForm.submit(); } else { document.location.href = strReq; } return; }
    if(!objForm || objForm.length<1) { oMethod = "GET"; objForm = null; }
    if(!innerTag || typeof(innerTag)!="object") innerTag = document.getElementById(innerTag) || document.createElement("div");
    try {
      xh.open(oMethod, strReq + "&random=" + Math.random(), true);
      if(oMethod == "POST") xh.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  	  xh.send(formFormat(objForm));
  	  xh.onreadystatechange = function() {
  	    if(xh.readyState==4 || xh.readyState=="complete") {
  	      if(xh.status==200 || xh.status==0) { showRes(xh.responseText, innerTag); return; }
  	    }
  	  }
  	  if(boolBar==true) showProgressBar(innerTag);
    } catch(e) { showRes("Request fail!", innerTag); }
  }
  //---------------tools function----------------------------------
  function testSupport() { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { try { return new XMLHttpRequest(); } catch(e) { return false; } } } }
  function showRes(strRes, innerObj) {
    if(!strRes) return;
  	var re = /<script.*?>([\s\S]*?)<\/script>/igm;
  	var arr = strRes.match(re);
    innerObj.innerHTML = strRes.replace(re, "");
    if(arr) { for(var i=0; i<arr.length; i++) eval(arr[i].replace(re, "$1")); }
  }
  function formFormat(objForm) {
    if(!objForm) return null;
    var cows = objForm.length, queryString = [], tmp;
    for(var i=0; i<cows; i++) { if(objForm[i].name) { tmp = caseForm(objForm[i]); if(tmp) queryString[queryString.length] = tmp; } }
    tmp = null;
    return queryString.join("&");
  }
  function caseForm(e) {
    var a, s;
    switch (e.type.toLowerCase()) {
      case "select-one"      : s = e.selectedIndex; a = e.options[s].value || e.options[s].text; if(s>=0 && a) return rsForm(e.name, a); else return null; break;
      case "select-mulitple" : s = e.length; if(s>0) { a = []; var o, v; for(var i=0; i<s; i++) { o = e.options[i]; v = o.value || o.text; if(o.selected && v) a.push(v); } if(a.length>0) return rsForm(e.name, a.join(",")); else return null; }; break;
      case "checkbox"        :
      case "radio"           : if(e.checked && e.value) return rsForm(e.name, e.value); else return null; break;
      case "submit"          :
      case "reset"           : return null; break;
      default                : return rsForm(e.name, e.value);
    }
  }
  function rsForm(strName, strValue) { return strName + "=" + encodeURIComponent(strValue); }
  function showProgressBar(innerTag) { innerTag.innerHTML = '<marquee direction="right" scrollamount="10"><table style="font-size:1px;width:50px;height:10px;"><tr><td bgcolor="#EEEEEE"></td><td bgcolor="#CCCCCC"></td><td bgcolor="#AAAAAA"></td></tr></table></marquee>'; }
}
//===========================================
function ClsUBBCode() {
  var setFocus = false, caretPos = false, range = false, rangeText = "", setText = "";
  this.getFocus = function(objThis) { setFocus = objThis; setFocus.onselect = storeCaret; setFocus.onclick = storeCaret; setFocus.onkeyup = storeCaret; }
  //-----------range tools function--------------------
  function storeCaret() { if(setFocus.createTextRange) caretPos = setFocus.document.selection.createRange().duplicate(); }
  function getRange() {
    range = false; rangeText = ""; setText = "";
    if(!setFocus) return;
    if(document.all) {
      var sel = setFocus.document.selection;
      if(sel && sel.type == "Text") { range = sel.createRange(); rangeText = range.text; }
    } else rangeText = "";
  }
  function addText() {
  	if(!setFocus) return;
  	if(range) { range.text = setText; return; }
    if(setFocus.createTextRange && caretPos) {
    	var caretText = caretPos.text;
      caretPos.text += (caretText.charAt(caretText.length - 2) == ' ') ?  setText + ' ' : setText;
    } else { setFocus.value += setText; }
    setFocus.focus();
  }
  //-----------select tools function-----
  function getValue(obj) {
  	obj.onfocus = function() { this.selectedIndex = 0; }
  	obj = obj.options[obj.selectedIndex];
  	obj = obj.value || obj.text;
  	if(obj) return obj; else return "";
  }
  //-----------select--------------------
  this.select = function(obj, str) {
  	obj = getValue(obj);
    getRange(); setText = "[" + str + "=" + obj + "]" + rangeText + "[/" + str + "]"; addText();
  }
  //-----------button---------------------
  this.button = function(str) {
    getRange(); setText = "[" + str + "]" + rangeText + "[/" + str + "]"; addText();
  }
  //-----------insert---------------------
  this.insert = function(strCode) {
    getRange(); setText = strCode; addText();
  }
  //-----------object---------------------
  this.list = function(str) {
  	switch(str) {
  	  case "ol" : str = "ol"; break;
  	  default   : str = "ul";
  	}
    getRange();
    if(rangeText != "") {
      rangeText = rangeText.split("\n");
      for(var i=0; i<rangeText.length; i++) rangeText[i] = "[*]" + rangeText[i];
      setText = "[" + str + "]\n" + rangeText.join("\n") + "\n[/" + str + "]";
    } else { setText = "[" + str + "]\n[*]" + rangeText + "\n[*]\n[*]\n[/" + str + "]"; }
    addText();
  }
  this.align = function(strAlign) {
  	switch(strAlign) {
  	  case "left"  : strAlign = "left"; break;
  	  case "right" : strAlign = "right"; break;
  	  default      : strAlign = "center";
  	}
    getRange(); setText = "[align=" + strAlign + "]" + rangeText + "[/align]"; addText();
  }
  this.image = function() {
    getRange();
    if(rangeText != "") { setText = "[img]" + rangeText + "[/img]"; }
    else {
      var text1 = prompt("[input the Image Link]", "");
      if(text1) {
        var text2 = prompt("[input the WIDTH,HEIGHT]\nlike: 400,300 OR 400%,300%", "");
        if(text2) setText = "[img=" + text2 + "]" + text1 + "[/img]";
        else setText = "[img]" + text1 + "[/img]";
      }
    }
    addText();
  }
  this.hyperlink = function() {
    getRange();
    if(rangeText != "") { setText = "[url]" + rangeText + "[/url]"; }
    else {
      var text1 = prompt("[input the URL]", "http://");
      if(text1) {
        var text2 = prompt("[input the display text]", "");
        if(text2) setText = "[url=" + text1 + "]" + text2 + "[/url]";
        else setText = "[url]" + text1 + "[/url]";
      }
    }
    addText();
  }
  this.mail = function() {
    getRange();
    if(rangeText != "") { setText = "[mail]" + rangeText + "[/mail]"; }
    else {
      var text1 = prompt("[input the Email Link]\nlike: name@domain.com", "");
      if(text1) {
        var text2 = prompt("[input the display text]", "");
        if(text2) setText = "[mail=" + text1 + "]" + text2 + "[/mail]";
        else setText = "[mail]" + text1 + "[/mail]";
      }
    }
    addText();
  }
  this.quote = function() {
    getRange();
    if(rangeText != "") { setText = "[quote]" + rangeText + "[/quote]"; }
    else {
      var text1 = prompt("[input the Quote Title]", "");
      if(text1) {
        var text2 = prompt("[input the Quote content]", "");
        if(text2) setText = "[quote=" + text1 + "]" + text2 + "[/quote]";
        else setText = "[quote][/quote]";
      } 
    }
    addText();
  }
  this.media = function() {
    getRange();
    if(rangeText != "") { setText = "[media]" + rangeText + "[/media]"; }
    else {
      var text1 = prompt("[input the Media Link]", "");
      if(text1) {
        var text2 = prompt("[input the WIDTH,HEIGHT]\nlike: 400,300", "");
        if(text2) setText = "[media=" + text2 + "]" + text1 + "[/media]";
        else setText = "[media]" + text1 + "[/media]";
      }
    }
    addText();
  }
  this.moreless = function() {
    getRange();
    if(rangeText != "") { setText = "[more=more ...]" + rangeText + "[/less=... less]"; }
    else {
      var text1 = prompt("[input the More Title]", "More ...");
      if(text1) {
        var text2 = prompt("[input the Less Title", "... Less");
        if(text2) {
          var text3 = prompt("[input the Content", "");
          if(text3) {
            setText = "[more=" + text1 + "]" + text3 + "[/less=" + text2 + "]";
          } else setText = "[more=" + text1 + "][/less=" + text2 + "]";
        }
      }
    }
    addText();
  }
}
//----------- object tools function --------------
ClsUBBCode.prototype.runCode = function(objThis) {
	var win = window.open("", "", "");
  objThis = objThis.parentNode.childNodes[0].value;
  win.opener = null;
  win.document.write(objThis);
  win.document.close();
}
ClsUBBCode.prototype.getObjStrByType = function(strType, strURL, intWidth, intHeight) {
  switch(strType) {
    case "exe" : return 'COM'; break;
    case "zip" : return 'ZIP'; break;
  	case "doc" : return 'TEXT'; break;
    case "img" : return '<img border="0" onload="ubb.resizeImage(this, true)" src="' + strURL + '" width="' + intWidth + '" height="' + intHeight + '" alt="' + strURL + '" />'; break;
		case "swf" : return '<object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + intWidth + '" height="' + intHeight + '"><param name="movie" value="' + strURL + '" /><param name="quality" value="high" /><param name="AllowScriptAccess" value="never" /><embed src="' + strURL + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + intWidth + '" height="' + intHeight + '" /></object>'; break;
	  case "wmp" : return '<object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" type="application/x-oleobject" standby="Loading..." width="' + intWidth + '" height="' + intHeight + '"><param name="FileName" VALUE="' + strURL + '" /><param name="ShowStatusBar" value="-1" /><param name="AutoStart" value="true" /><embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" src="' + strURL + '" autostart="true" width="' + intWidth + '" height="' + intHeight + '" /></object>'; break;
		case "rm"  : return '<object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="' + intWidth + '" height="' + intHeight + '"><param name="SRC" value="' + strURL + '" /><param name="CONTROLS" VALUE="ImageWindow" /><param name="CONSOLE" value="one" /><param name="AUTOSTART" value="true" /><embed src="' + strURL + '" nojava="true" controls="ImageWindow" console="one" width="' + intWidth + '" height="' + intHeight + '"></object><br /><object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="' + intWidth + '" height="32" /><param name="CONTROLS" value="StatusBar" /><param name="AUTOSTART" value="true" /><param name="CONSOLE" value="one" /><embed src="' + strURL + '" nojava="true" controls="StatusBar" console="one" width="' + intWidth + '" height="24" /></object><br /><object classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="' + intWidth + '" height="32" /><param name="CONTROLS" value="ControlPanel" /><param name="AUTOSTART" value="true" /><param name="CONSOLE" value="one" /><embed src="' + strURL + '" nojava="true" controls="ControlPanel" console="one" width="' + intWidth + '" height="24" autostart="true" loop="false" /></object>'; break;
		case "qt"  : return '<embed src="' + strURL + '" autoplay="true" loop="false" controller="true" playeveryframe="false" cache="false" scale="TOFIT" bgcolor="#000000" kioskmode="false" targetcache="false" pluginspage="http://www.apple.com/quicktime/" />'; break;
		default    : return '?';
  }
}
ClsUBBCode.prototype.getFileTypeByName = function(str) {
	if(!str) return "";
  str = new RegExp(str, "ig");
  switch(true) {
    case str.test("|exe|com|bat|dll|") : return "exe"; break;
  	case str.test("|ace|arj|rar|cab|lzh|jar|zip|") : return "zip"; break;
    case str.test("|doc|txt|rtf|wri|pdf|") : return "doc"; break;
  	case str.test("|bmp|gif|jpg|jpeg|png|tif|") : return "img"; break;
    case str.test("|swf|") : return "swf"; break;
    case str.test("|aac|asf|asx|avi|aiff|wmv|wmp|wm|wvx|wax|wmx|wma|mp4|mpg|mpeg|m3u|mid|wav|mp3|mpa|mp2|m1a|m2a|pls|") : return "wmp"; break;
    case str.test("|amr|3gp|3gpp|3g2|3gp2|ra|rm|ram|rmvb|rpm|rt|rp|smi|smil|") : return "rm"; break;
    case str.test("|mov|vod|qt|") : return "qt"; break;
    default : return "";
  }
}
//----------- html function --------------
ClsUBBCode.prototype.showSmilies = function(objThis) {
  objThis = objThis.parentNode.parentNode.getElementsByTagName("a")[0];
  if(!objThis) return;
  objThis.style.display = (objThis.style.display=="none") ? "" : "none";
}

ClsUBBCode.prototype.showMedia = function(objThis, strURL, intWidth, intHeight) {
  var obj = objThis.nextSibling.nextSibling;
  if(obj.innerHTML) { obj.innerHTML  = ""; return; }
  var str = strURL.split(/\./g);
  str = this.getFileTypeByName(str[str.length - 1]);
  if(!str || !str.match(/swf|wmp|rm|qt/ig)) return;
  obj.innerHTML = this.getObjStrByType(str, strURL, intWidth || 400, intHeight || 300);
}
ClsUBBCode.prototype.showMoreLess = function(objThis, boolLess) {
  var obj1, obj2;
  if(boolLess==true) {
    obj1 = objThis.previousSibling; obj2 = objThis.nextSibling;
    obj1.style.display = ""; obj2.style.display = "none";
  } else {
    obj1 = objThis.nextSibling; obj2 = obj1.nextSibling;
    obj1.style.display = ""; obj2.style.display = "";
  }
  objThis.style.display = "none";
}
ClsUBBCode.prototype.resizeImage = function(objThis, boolClick) {
  var w1 = objThis.offsetWidth || 0, h1 = objThis.offsetHeight || 0;
  if(w1==0 && h1==0) return;
  if(!this.i) this.i = new Image();
  this.i.src = objThis.src;
  var w2 = this.i.width, h2 = this.i.height;
  if((h1<h2 || w1<w2) && boolClick==true) {
    objThis.onclick = function() {
      this.win = window.open('about:blank', '', 'scrollbars=yes, width=10, height=10');
      this.win.opener = null;
      this.win.document.write('<html><body style="margin:0px"><img src="' + objThis.src + '" style="cursor: pointer" onclick="window.opener=null;window.close()" onload="window.resizeTo(this.width, this.height)" /></body></html>'); 
    }
    objThis.style.cursor = "pointer";
  }
  if(w1 > w2) w1 = w2;
  if(h1 > h2) h1 = h2;
  w1 = w1 / w2; h1 = h1 / h2;
  if(w1 < h1) { w2 *= w1; h2 *= w1; } else { w2 *= h1; h2 *= h1; }
  w2 += "px", h2 += "px";
  objThis.width = w2; objThis.height = h2;
  objThis.style.width = w2; objThis.style.height = h2;
}