// JavaScript Document
function checkExplorer()
{
   var agent = navigator.userAgent;
   if(agent.indexOf('Mac') != - 1)
   {
      if(agent.indexOf('MSIE') != - 1) alert('Please use the Firefox web browser on the Mac (download it from www.getfirefox.com).');
   }
   else if(agent.indexOf('Win') != - 1)
   {
      if(agent.indexOf('MSIE') != - 1)
      {
         var IE = agent.indexOf('MSIE');
         var IEBlank = agent.substring(IE).indexOf(' ');
         var Position = agent.substring(IE).indexOf(';');
         var version = agent.substring(IE).substring(IEBlank + 1, Position);
         if(parseFloat(version) < 7) alert('You\'d better to upgrade your version of Internet Explorer to version 7.0 or above, or use Firefox (www.getfirefox.com)');
      }
   }
   return null;
}

function getPageElementByID(element, id)
{
   //  alert();
   var coord =
   {
      x : 0, y : 0
   }
   ;
   while (element)
   {
      if (typeof id == "string")
      {
         if(element.id.indexOf(id) == 0)
         {
            coord.x = element.offsetLeft + element.offsetWidth;
            coord.y = element.offsetTop;
            break;
         }
      }
      else if(typeof id == "object")
      {
         if(element = id)
         {
            coord.x = element.offsetLeft + element.offsetWidth;
            coord.y = element.offsetTop;
            break;
         }
      }
      element = element.offsetParent;
   }
   return coord;
}

//  ---------------------------------------

function inmergobject(mobj, sobj, pre)
{
   pre = (typeof pre == 'undefined') ? "" : pre;
   for(var sna in sobj)
   {
      if(sna != 'toJSONString')
      eval("mobj." + pre + sna + " ='"+ sobj[sna]+"'");
   }
   return mobj;
}

//  ---------------------------------------

function changecss(theClass, element, value)
{
   var cssRules;
   if (document.all)
   {
      cssRules = 'rules';
   }
   else if (document.getElementById)
   {
      cssRules = 'cssRules';
   }

   var sl = document.styleSheets.length;
   for (var S = 0; S < sl; S ++ )
   {
      try
      {
         var ssl = document.styleSheets[S][cssRules].length;
         ;
         for (var R = 0; R < ssl; R ++ )
         {
            if (isIE && document.styleSheets[S][cssRules][R].selectorText.toUpperCase() == theClass.toUpperCase())
            {
               document.styleSheets[S][cssRules][R].style[element] = value;
            }
            else if(document.styleSheets[S][cssRules][R].selectorText == theClass)
            {
               document.styleSheets[S][cssRules][R].style[element] = value;
            }
         }
      }
      catch(e)
      {

      }
   }

}

//  ---------------------------------------

function getParams()
{
   var o = new Object()
   var a = document.location.search.substr(1).split('&');
   for (i = 0; i < a.length; i ++ )
   {
      try
      {
         var aa = a[i].split('=');
         var n  = aa[0];
         var v  = aa[1];
         o[n]   = v.Trim();
      }
      catch(e)
      {
      }
   }
   return o;
}

//  ---------------------------------------

function testNumber(e)
{

   var key = window.event ? window.event.keyCode : e.which;
   var keychar = String.fromCharCode(key);
   reg = /\d/;
   if(key == 8)
   {
      return true;
   }
   else
   {
      return reg.test(keychar);
   }
}

//  ---------------------------------------

function getCookie(name)
{
   //   alert(document.cookie);
   var search = name + "="
   if(document.cookie.length > 0)
   {
      offset = document.cookie.indexOf(search)
      if(offset != - 1)
      {
         offset += search.length
         end = document.cookie.indexOf(";", offset)
         if(end == - 1) end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      }
      else return ""
   }
}

//  ---------------------------------------

function getCookieVal (offset)
{
   // ȡΪoffsetcookieֵ
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == - 1)
   endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

//  ---------------------------------------

String.prototype.Trim = function()
{
   return this.replace(/(^\s*)|(\s*$)/g, "");
}

//  ---------------------------------------

String.prototype.LTrim = function()
{
   return this.replace(/(^\s*)/g, "");
}

//  ---------------------------------------

String.prototype.RTrim = function()
{
   return this.replace(/(\s*$)/g, "");
}

//  ---------------------------------------

function getPageSize()
{

   var xScroll, yScroll;
   var scroll_xy = getScroll();
   xScroll = scroll_xy[0];
   yScroll = scroll_xy[1];
   var windowWidth, windowHeight;
   if (self.innerHeight)
   {
      // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
   }
   else if (document.documentElement && document.documentElement.clientHeight)
   {
      // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
   }
   else if (document.body)
   {
      // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
   }

   // for small pages with total height less then height of the viewport
   if(yScroll < windowHeight)
   {
      pageHeight = windowHeight;
   }
   else
   {
      pageHeight = yScroll;
   }

   // for small pages with total width less then width of the viewport
   
   if(xScroll < windowWidth)
   {
      pageWidth = windowWidth;
   }
   else
   {
      pageWidth = xScroll;
   }

   arrayPageSize = new Array(pageWidth, pageHeight, windowWidth, windowHeight)
   return arrayPageSize;
}

//  ---------------------------------------

function getScroll()
{
   if (window.innerHeight && window.scrollMaxY)
   {
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
   }
   else if (document.body.scrollHeight > document.body.offsetHeight)
   {
      // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
   }
   else
   {
      // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
   }
   
   return [xScroll, yScroll];
}

//  ---------------------------------------

function doResize (obj)
{
   var width  = parseInt(obj.width);   var height = parseInt(obj.height);
	
   if (width > 300)
   {
		width = 300 + "px";
		obj.style.width = width;
   }
   if (height > 230)
   {
      height = 230 + "px";
      obj.style.height = height;
   }
}

//  ---------------------------------------

function InitAjax()
{
   var ajax = false;
   try
   {
      ajax = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e)
   {
      try
      {
         ajax = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (E)
      {
         ajax = false;
      }
   }
   if ( ! ajax && typeof XMLHttpRequest != 'undefined')
   {
      ajax = new XMLHttpRequest();
   }
   return ajax;
}

//  ---------------------------------------

function checkEmail(el)
{
   var regu = "^(([0-9a-zA-Z]+)|([0-9a-zA-Z]+[_.0-9a-zA-Z-]*[0-9a-zA-Z]+))@([a-zA-Z0-9-]+[.])+([a-zA-Z]{2}|net|NET|com|COM|gov|GOV|mil|MIL|org|ORG|edu|EDU|int|INT)$"
   var re = new RegExp(regu);
   if (el.search(re) == - 1)
   {
      return false;
      // 非法
   }
   return true;
   // 正确
}

//  ---------------------------------------

function coder(str)
{
   var s = "";
   if (str.length == 0) return "";
   for (var i = 0; i < str.length; i ++ )
   {
      switch (str.substr(i, 1))
      {
         case "<" :
            s += "&lt;";
            break;
         case ">" :
            s += "&gt;";
            break;
         case "&" :
            s += "&amp;";
            break;
         case " " :
            s += "&nbsp;";
            break;
         case "\"" :
            s += "&quot;";
            break;
         case "\n" :
            s += "<br>";
            break;
         default :
            s += str.substr(i, 1);
            break;
      }
   }
   return s;
}

//  ---------------------------------------

function doFavorite(title)
{
   var url = window.location.href;
   var title = "Petizens - " + title;
   if (document.all)
   {
      window.external.addFavorite(url, title);
   }
   else if (window.sidebar)
   {
      window.sidebar.addPanel(title, url, "");
   }
}

//  ---------------------------------------

function viewBox(boxid, checkboxid)
{
   if ($(checkboxid).checked == false)
   {
      $(boxid).style.display = "";
      $(checkboxid).checked = "checked";
   }
   else
   {
      $(boxid).style.display = "none";
      $(checkboxid).checked = "";
   }
}

//  ---------------------------------------

function replacePic(str, length)
{
   var strnew = str.replace(/<img( ||.*?)(>)/, "");
   return strnew.substring(0, length) + "...";
}

//  ---------------------------------------

function chkAll(ob, id, name)
{
   //  var b = document.getElementById(id).getElementsByTagName("input");
   var b = document.getElementsByName(name);
   var check = ob.checked;
   for(var j = 0; j < b.length; j ++ )
   {
      var it = b[j];
      if(it.id != 'chekcAll' && it.type.toLowerCase() == 'checkbox' && it.name == name)
      {
         it.checked = check;
      }
   }
}

//  ---------------------------------------

function getCheckBoxValue( checkboxname )
{
   var s = document.getElementsByName( checkboxname );
   var s2 = "";
   for( var i = 0; i < s.length; i ++ )
   {
      if ( s[i].checked )
      {
         s2 += s[i].value + ',';
      }
   }
   s2 = s2.substr( 0, s2.length - 1 );
   return s2;
}

//  ---------------------------------------

function ajaxRequestByObj(obj, url, paras, func)
{
   var myAjax = new Ajax.Request(
   url,
   {
      method : 'post',
      parameters : paras,
      onLoading : function()
      {
         var loadImg = document.createElement("div");
         loadImg.id = "loadimg";
         loadImg.style.textAlign = "center";
         loadImg.innerHTML += "<img src='Template/skinDefault/images/loading_grey.gif'";
         obj.appendChild(loadImg);
      }
      ,
      onComplete : func
   }
   );
}

//  ---------------------------------------
function ajaxRequest(id, url, paras, func)
{
   var myAjax = new Ajax.Request(
   url,
   {
      method : 'post',
      parameters : paras,
      onLoading : function()
      {
         try
         {
            if(id != "")
            {
               $(id).innerHTML = "Loading...";
            }
         }
         catch(e)
         {
         }
      }
      ,
      onComplete : func
   }
   );
}

function ajaxGet(id, url, paras, func)
{
   var myAjax = new Ajax.Request(
   url,
   {
      method : 'get',
      parameters : paras,
      onLoading : function()
      {
         try
         {
            if(id != "")
            {
               $(id).innerHTML = "Loading...";
            }
         }
         catch(e)
         {
         }
      }
      ,
      onComplete : func
   }
   );
}

//  ---------------------------------------

function ajaxRequestOnLoad( url, paras, func, onloadfunc)
{
   var myAjax = new Ajax.Request(
   url,
   {
      method : 'post',
      parameters : paras,
      onLoading : onloadfunc,
      onComplete : func
   }
   );
}
