// JavaScript Document
// include :
// 1.cookie
// 2.client
// 3.basic
// 4.FFpatch

// 5. show page info of ok / hint / error

// 6. set SubNav to Current

// 7. table alter color

// 0.allow cross - domain
document.domain = "100e.com";
function E(objID)
{
   return document.getElementById(objID);
}


// 1.cookie
function getCookie(name)
{
   var i = 0;
   // 查找的变量开始位置
   var n = 0;
   // 记录name的个数
   var nameIndex ;
   // 记录name的位置
   var prefix = name + "=";
   while( (nameIndex = document.cookie.indexOf(prefix, i)) != - 1 )
   {
      i = nameIndex + 1;
      if (( nameIndex != 0 ) && (document.cookie.substring(nameIndex - 1, nameIndex) != " "))
      {
         continue;
      }
      else
      {
         var cookieendindex = document.cookie.indexOf(";", nameIndex + prefix.length);
         if( cookieendindex == - 1 )
         cookieendindex = document.cookie.length;
         return unescape(document.cookie.substring(nameIndex + prefix.length, cookieendindex));
         break;
      }
   }
   return null;
}

// name, value, expire seconds
function setCookie(name, value)
// 设定Cookie值
{
   var expdate = new Date();
   var argv = arguments;
   var argc = arguments.length;
   var expires = (argc > 2) ? argv[2] : null;
   // var path = (argc > 3) ? argv[3] : null;
   var path = "/";
   // var domain = (argc > 4) ? argv[4] : null;
   var domain = ".100e.com";
   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 delCookie(name)
// 删除Cookie
{
   // var exp = new Date();
   // exp.setTime (exp.getTime() - 100000);
   // alert(exp);
   // var cval = getCookie (name);
   setCookie(name, "", - 1000);
   // document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


function getUserName()
{

   var UserName = null;
   if(getCookie("UserName") != null)
   UserName = getCookie("UserName");
   else if (getCookie("username") != null)
   UserName = getCookie("username");
   if (UserName != null)
   return UserName;
   else
   return null;
}
function decodeCookie(str)
{
   var strArr;
   var strRtn = '';
   if(str.indexOf('a') == - 1)
   {
      return null;
   }
   strArr = str.split('a');
   for(var i = strArr.length - 1 ; i >= 0 ; i -- )
   {
      strRtn += String.fromCharCode(eval(strArr[i]));
   }
   return strRtn;
}
function getUserNickName()
{
   var UserNickName = null;
   if(getCookie("UserNickName") != null)
   UserNickName = decodeCookie(getCookie("UserNickName"));
   else if (getCookie("usernickname") != null)
   UserNickName = decodeCookie(getCookie("usernickname"));

   if(UserNickName != null)
   return UserNickName;
   else
   return null;
}

// 2. client
// chat to User
function Chat(UserID)
{
   var url = "100e://210.51.18.41/100e_ForIMClient/UserChat.asp?Userid=" + UserID;
   if ( ! checkClient())
   {
      var cookieName = "100eClient_ListLink";
      var cookieValue = url;
      var expireSecond = "3600";
      setCookie(cookieName, cookieValue, cookieValue);
      return;
   }

   window.location = url;
}

// open book
function OpenBook(BookID)
{
   var url;
   if(parseInt(BookID) == 0)
   {
      url = "100e://";
   }
   else
   {
      var fn = "000" + BookID + ".100e";
      var n = fn.length;
      var xfn  = fn.substring(n - 9);
      url = "100e://bookfile.100e.com/100e_ForProduct/100eListFile/List" + xfn;

   }

   if ( ! checkClient())
   {
      var cookieName = "100eClient_ListLink";
      var cookieValue = url;
      var expireSecond = "3600";
      setCookie(cookieName, cookieValue, cookieValue);
      return;
   }
   window.location = url;
}
//open book with special client 2009-06-17
function OpenBookS(bookid)
{
   var url;
   if(parseInt(bookid) == 0)
   {
      url = "100e://";
   }
   else
   {
      var fn = "000" + bookid + ".100e";
      var n = fn.length;
      var xfn  = fn.substring(n - 9);
      url = "100e://bookfile.100e.com/100e_ForProduct/100eListFile/List" + xfn;
   }

   if (!checkClientS())
   {
      var cookieName = "100eClient_ListLink";
      var cookieValue = url;
      var expireSecond = "3600";
      setCookie(cookieName, cookieValue, cookieValue);
      return;
   }
   window.location = url;   
}

function checkClientS()
{
   var nameClient = "100eClient";
   var curClient = getCookie(nameClient);

   if (curClient != null && curClient.substr(0, 3) == "7.0")
   {
      return true;
   }
   else
   {
      if (confirm("您需要安装最新的 一百易7.0 。现在就下载并安装吗？"))
      {
         var url = "http://img.100e.com/Download/test/100els_7_Group_1.exe ";
         var   oIFrame   =   document.createElement("IFRAME");
         oIFrame.src = url;
         oIFrame.style.display = "none";
         document.body.appendChild(oIFrame); 
         CountDownload();
      }
      return false;
   }
}
// enter room
// use dynamic script.src instead of ajax, so can cross domain
function EnterRoom(RoomID)
{
   // ensure client ready
   if ( ! checkClient())
   {
      return;
   }

   var oScriptID = "enterRoomScript";
   var oScript = document.getElementById(oScriptID);
   if (oScript == null)
   {
      // create element
      oScript = document.createElement("script");
      oScript.setAttribute("type", "text/javascript");

      document.appendChild(oScript);

   }
   var queryString = "?RoomID=" + RoomID + "&date=" + (new Date()).toString();
   // alert(queryString);

   oScript.src = "http://client.100e.com/tool/EnterRoom/" + queryString;

}

function PlayVideo(VideoID)
{
   // ensure client ready
   if ( ! checkClient())
   {
      return;
   }
   var Url = "100e://chat1.100e.com/100e_ForIMClient/Record_GroupInfo.asp?RecordID=" + VideoID;
   window.location = Url;
}



// true : 7.0
// false : < 7.0
function checkClient()
{
   var nameClient = "100eClient";
   var curClient = getCookie(nameClient);

   if (curClient != null && curClient.substr(0, 3) == "7.0")
   {
      return true;
   }
   else
   {
      if (confirm("您需要安装最新的 一百易7.0 。现在就下载并安装吗？"))
      {
         // check system version windows or vista
         var url = check_sys();
         var   oIFrame   =   document.createElement("IFRAME");
         // oIFrame.src = "http://file.100e.com/download/client/100els_7_0.exe";
         oIFrame.src = url;
         oIFrame.style.display = "none";
         document.body.appendChild(oIFrame);
         
         CountDownload();
      }
      return false;
   }

}

function CountDownload()
{
    var FN="100els_7_0.exe";
    var oIFrame = document.createElement("IFRAME");
    oIFrame.style.display="none";
    document.body.appendChild(oIFrame);
    oIFrame.src="http://sta.100e.com/page/download_sub_www.asp?FN="+FN;
}

function check_sys()
{
   var browser = navigator.userAgent.toLowerCase();
   if(browser.indexOf('windows nt 6.0') != - 1)
       url = "http://download.100e.com/download/client/100els_7_0_Vista.exe";
   else
       url = "http://download.100e.com/download/client/100els_7_0.exe";
   return url;
}

// 3.basic
// use for iframe form submit.

// default formID is 'formID'
var iFrameFormID;
// = "iFrameFormID";

function iFrameSubmit_Error(info)
{
   alert(info);
   enableForm(iFrameFormID);

}

// for submit done , hide button and set submit button vale
function iFrameSubmit_Ok(info)
{
   if (info != "undefined")
   {
      alert(info);
   }
   var e;
   for (i = 0; i < document.forms[iFrameFormID].elements.length ; i ++ )
   {
      e = document.forms[iFrameFormID].elements[i];
      if (e.type == "submit" )
      {
         e.value = e.value + "  已完成！";
      }
      if (e.type == "button")
      {
         e.style.display = "none";
      }
   }

}


function IFrameSubmit(formID)
{
   var oForm = document.getElementById(formID);
   iFrameFormID = formID;
   var iFrameName = "iFrameSubmitName";


   // create new iframe if not exist
   var htmlStr = '<iframe name="'+iFrameName+'" style="display:none;"></iframe>';
   insertHtml('afterbegin', document.body, htmlStr);

   // set form attribute
   oForm.target = iFrameName;
   // oForm.id = iFrameFormID;


   if (oForm.attachEvent)// ie
   {
      oForm.attachEvent("onsubmit", disableForm);

   }
   else// ff
   {
      oForm.addEventListener("submit", disableForm, false);

   }


}



function enableForm()
{
   // check every submit and button
   setFormDisabled(iFrameFormID, false);

}

function disableForm()
{

   setFormDisabled(iFrameFormID, true);

}



function setFormDisabled(formID, trueOrFalse)
{

   var oForm = document.getElementById(formID);

   var e;
   for (i = 0; i < oForm.elements.length ;   i ++ )
   {
      e = oForm.elements[i];
      if (e.type == "submit" || e.type == "button")
      {
         e.disabled = trueOrFalse;
      }
   }
}


// select and radio auto. useful for edit mode.
function AutoSelect(SelectID, OptionValue)
{
   var obj = document.getElementById(SelectID);
   if (obj)
   {
      for   (i = 0;   i <   obj.options.length;
      i ++ )
      {
         if   (obj.options[i].value == OptionValue)
         {
            obj.options[i].selected = true;

            return;
         }
      }
   }

}

function AutoCheck(RadioOrCheckBoxName, CheckValue)
{
   var obj = document.getElementsByName(RadioOrCheckBoxName);
   if (obj)
   {
      for   (i = 0;   i <   obj.length; i ++ )
      {
         if   (obj[i].value == CheckValue)
         {
            obj[i].checked = true;

            return;
         }
      }
   }

}



// visibility functions


function HideAndShow()
// hide html elements id1, id2..., and show last IDN
{
   for( var i = 0; i < arguments.length - 1; i ++ )
   {
      var obj = document.getElementById(arguments[i]);
      if (obj != null)
      {
         obj.style.display = "none";
      }
   }


   var objLast = document.getElementById(arguments[arguments.length - 1]);
   if (objLast != null)
   {
      objLast.style.display = "";
   }


}

function Display(objID)
// toggle objID 's display
{
   var obj = document.getElementById(objID);
   if (obj != null)
   {
      if(obj.style.display == "none")

      {
         obj.style.display = "";
      }
      else
      {
         obj.style.display = "none";
      }


   }


}

function HideAndShow_bak(ID1, ID2)
// hide html elements id1, id2, and show id2
{
   var obj1 = document.getElementById(ID1);
   if (obj1 != null)
   {
      obj1.style.display = "none";
   }

   var obj2 = document.getElementById(ID2);
   if (obj2 != null)
   {
      obj2.style.display = "";
   }


}


function tog()
{
   // tog : toggle the visibility of html elements (arguments[1..]) from none to
   // arguments[0].  Return what should be returned in a javascript onevent().
   display = arguments[0];
   for( var i = 1; i < arguments.length; i ++ )
   {
      var x = document.getElementById(arguments[i]);
      if ( ! x) continue;
      if (x.style.display == "none" || x.style.display == "")
      {
         x.style.display = display;
      }
      else
      {
         x.style.display = "none";
      }
   }

   var e = is_ie ? window.event : this;
   if (e)
   {
      if (is_ie)
      {
         e.cancelBubble = true;
         e.returnValue = false;
         return false;
      }
      else
      {
         return false;
      }
   }
}

function Go(url, confirmMsg)
{
   if (confirmMsg)
   {
      if (confirm(confirmMsg))
      {
         window.location = url;
      }

   }
   else
   {
      window.location = url;
   }


}


function Nav_Goto(url)
{
   window.location = url;
}

function Back()
{
   window.history.back( - 1);
}
function Nav_Back()
{
   window.history.back( - 1);
}
function Reload()
{
   window.location.reload();

}




// 4.ffPatch
// where：插入位置。包括beforeBegin, beforeEnd, afterBegin, afterEnd。
// el：用于参照插入位置的html元素对象
// html：要插入的html代码
function insertHtml(where, el, html)
{
   where = where.toLowerCase();
   if(el.insertAdjacentHTML)
   {
      switch(where)
      {
         case "beforebegin" :
            el.insertAdjacentHTML('BeforeBegin', html);
            return el.previousSibling;
         case "afterbegin" :
            el.insertAdjacentHTML('AfterBegin', html);
            return el.firstChild;
         case "beforeend" :
            el.insertAdjacentHTML('BeforeEnd', html);
            return el.lastChild;
         case "afterend" :
            el.insertAdjacentHTML('AfterEnd', html);
            return el.nextSibling;
      }
      throw 'Illegal insertion point -> "' + where + '"';
   }
   var range = el.ownerDocument.createRange();
   var frag;
   switch(where)
   {
      case "beforebegin" :
      range.setStartBefore(el);
      frag = range.createContextualFragment(html);
      el.parentNode.insertBefore(frag, el);
      return el.previousSibling;
      case "afterbegin" :
      if(el.firstChild)
      {
         range.setStartBefore(el.firstChild);
         frag = range.createContextualFragment(html);
         el.insertBefore(frag, el.firstChild);
         return el.firstChild;
      }
      else
      {
         el.innerHTML = html;
         return el.firstChild;
      }
      case "beforeend" :
      if(el.lastChild)
      {
         range.setStartAfter(el.lastChild);
         frag = range.createContextualFragment(html);
         el.appendChild(frag);
         return el.lastChild;
      }
      else
      {
         el.innerHTML = html;
         return el.lastChild;
      }
      case "afterend" :
      range.setStartAfter(el);
      frag = range.createContextualFragment(html);
      el.parentNode.insertBefore(frag, el.nextSibling);
      return el.nextSibling;
   }
   throw 'Illegal insertion point -> "' + where + '"';
}

// 5. show page info of ok / hint / error
function showPageInfo()
{

   var eName = "layout_info";
   var divInfo = document.getElementById(eName);
   if (divInfo != null)
   {
      var cookieName_InfoType = "InfoType";
      var cookieValue_InfoType = getCookie(cookieName_InfoType);

      var cookieName_InfoText = "InfoText";
      var cookieValue_InfoText = getCookie(cookieName_InfoText);

      // alert(cookieValue_InfoText);
      if (cookieValue_InfoText != null && cookieValue_InfoText != "")
      {
         switch (cookieValue_InfoType)
         {
            case "0" :
            divInfo.className = "info_error";
            break;
            case "1" :
            divInfo.className = "info_ok";
            break;
            case "2" :
            divInfo.className = "info_hint";
            break;
            default :
            return;

         }

         divInfo.innerHTML = cookieValue_InfoText.replace(/\+/g, " ");
         delCookie(cookieName_InfoType);
         delCookie(cookieName_InfoText);

      }

   }


}

// 6. set Nav to Current
function SetCurrent(objID)
{
   var e = E(objID);
   e.className = e.className + " current";

}

// 7. table alter color
function AlterRow(objID)
{

   var el = E(objID).getElementsByTagName('tr');

   var length = el.length;

   for(var i = 0, l = el.length; i < l; i ++ )
   {

      el[i].className = ((i + 1) % 2) ? "odd" : "even";

   }

}
