﻿/* ==============================================================
javascript library
================================================================*/
var util = new Object();
util.xmlHttpRequest = xmlHttpRequest;
util.xmlDocument = xmlDocument;
util.setCookie =setCookie;
util.getCookie = getCookie;
util.deleteCookie = deleteCookie;
util.getQueryString = getQueryString;
util.getHost = getHost;
util.changeStyle = changeStyle;
util.clearTextbox = clearTextbox;
util.htmlFormat = htmlFormat;
util.trim = trim;
util.isInteger = isInteger;
util.isTime = isTime;
util.isDate = isDate;
util.isEditInteger = isEditInteger;
util.isIP = isIP;
util.isInt = isInt;
util.ChangeTitleColor = ChangeTitleColor;
util.changeCode = changeCode;
util.reportError = reportError;
util.StringBuffer = StringBuffer;
util.reloadFramePage = reloadFramePage;
util.setLanguage = setLanguage; //set langauge environment
util.changeLanguage = changeLanguage; //switch language environment

/* ==============================================================
Form Utility Library
================================================================*/
var FormUtil = new Object();
/***********************************************************/
// set focus on the first element.
/***********************************************************/
FormUtil.focusOnFirst = function() {
  var formLen = document.forms.length;
  if (formLen > 0) {
    var oFirstForm = document.forms[0];
    var oElementLen = oFirstForm.elements.length;
    for (var i=0; i<oElementLen; i++) {
      var oField = oFirstForm.elements[i];
      if (oField.type != "hidden") {
        if (oField.value.length == 0) {//
        oField.focus();
        }//
        return;
      }
    }
  }
}

/***********************************************************/
// load the login page
/***********************************************************/
function loadLogin()
{
  setLanguage();
  FormUtil.focusOnFirst();
}

/***********************************************************/
// set language cookie on the page
/***********************************************************/
function setLanguage()
{
	if (getCookie("language") == "")
	{
		var ver = "cn";
		var verName = ver=="cn"?"中文版":"English";
		var oDate = new Date();
		oDate.setTime(oDate.getTime()+86400 * 1000 );
		setCookie("language",ver, oDate);
		setCookie("languagecn",verName, oDate);
	}
}

/***********************************************************/
// reload frame page
/***********************************************************/
function changeLanguage(arg)
{
	var verName = arg=="cn"?"中文版":"English";
	var oDate = new Date();
	oDate.setTime(oDate.getTime()+86400 * 1000 );
	setCookie("language",arg, oDate);
	setCookie("languagecn",verName, oDate);
	parent.frames('mainFrame').location.reload();
}

/***********************************************************/
// reload frame page
/***********************************************************/
function reloadFramePage()
{
  parent['mainFrame'].location.reload();
}

/***********************************************************/
// 字符串连接
// e.g. var str = new StringBuffer();
// str.append("Kenny,"); str.toString();
/***********************************************************/
function StringBuffer()
{
  this.__strings__ = new Array;
}
StringBuffer.prototype.append = function(str)
{
  this.__strings__.push(str);
};
StringBuffer.prototype.toString = function()
{
  return this.__strings__.join("");
};

/***********************************************************/
// 错误报告
// e.g. <script type="text/javascript">window.onerror = reportError;</script>
/***********************************************************/
function reportError(msg,url,line)
{
  var str = new StringBuffer();
  str.append("系统发现如下的的错误:\n\n");
  str.append("位置: 第" + line + "行\n\n");
  str.append("错误: " + msg + "\n\n");
  str.append("网址: " + url);
  alert(str.toString());
  return true;
}

/***********************************************************/
// 局部更新验证码(支持IE,FF)
/***********************************************************/
function changeCode()
{
  var vcode = $("getcode");
  vcode.src = 'verifycode.aspx?randNum='+Math.random();
}

/***********************************************************/
// 改变文章标题颜色
/***********************************************************/
function ChangeTitleColor(txb,color) {
  $(txb).style.color = color;
}

/*============================================================
 cookie 
 var last = getCookie("lastvisit");  
 var exp = new Date(); 
 exp.setTime(exp.getTime() + (86400 * 1000 * 30)); 
 setCookie("lastvisit", last, exp); 
*/
function setCookie(name, value, expires)
{
	 document.cookie = name + "=" + escape(value) + "; expires=" + expires.toLocaleTimeString() + "; path=/"; 
} 
function getCookie(name) 
{ 
	var search = name + "="; 
	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 deleteCookie(name) 
{ 
	var expdate = new Date(); 
	expdate.setTime(expdate.getTime() - (86400 * 1000 * 1)); 
	setCookie(name, "", expdate); 
}

/* =============================================================
 QueryString getquerystring(document.location.search,'id')
*/
function getQueryString(search,name) 
{ 
	var vStr="";	
	search = search.toLowerCase();	
	search = search.substring(1,search.length);
	//alert(search);
	var arr=search.split("&");
	for(i=0;i<arr.length;i++)
	{
	    var temp=arr[i].split("="); 
	    if (temp[0]==name)		
	    {
	        vStr  = temp[1]; 
	        break;
	    }
	}	
	//alert(vStr);	 
	return vStr;
	
}

/* =============================================================
 host : getHost()
*/
function getHost() 
{ 
  return document.location.hostname;
}

/*=============================================================
style:changeStyle("h2","font-size","14px")
*/
function changeStyle(styleSheetIndex, selector, key, value)
{
  var rules = null;
  if (document.styleSheets[styleSheetIndex].cssRules) //standard browser
  {
    rules = document.styleSheets[styleSheetIndex].cssRules; //different
    for (var i = 0; i < rules.length; i++)
    {
      if (rules[i].selectorText === selector)
      {
        rules[i].style[key] = value;
      }
    }
  }
  else //nonstandard browser
  {
    rules = document.styleSheets[styleSheetIndex].rules; //different
    for (var j = 0; j < rules.length; j++)
    {
      if (rules[j].selectorText === selector)
      {
        rules[j].style[key] = value;
      }
    }
  }
}

/*===========================================================
  XmlHttp | XmlDocument
*/
function xmlHttpRequest()
{   
    var xmlhttps=["MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
    for(j=0;j<xmlhttps.length;j++)
    {
        try{ return new ActiveXObject(xmlhttps[j]);}
        catch(e){ alert("not found MSXML2.XMLHTTP"); return null;}
    }  
}
function xmlDocument()
{
    var xmldoms = [ "MSXML2.DOMDocument.5.0", "MSXML2.DOMDocument.4.0","MSXML2.DOMDocument.3.0","MSXML2.DOMDocument.2.0", "MSXML2.DOMDocument", "Microsoft.XmlDom"];
	for(var i=0; i <xmldoms.length ; i++)
	{
	    try{ return  new ActiveXObject(xmldoms[i]);	} 
		catch(e){alert("not found  MSXML2.DOMDocument");return null;}		
	}	
}

/*=========================================================
 clear input 
*/
function clearTextbox()
{
    var inputs = document.getElementsByTagName("input");
    var count = inputs.length;
    for(var i=0;i<count;i++)
    {	
        if(inputs[i].type=="text" )
        {
            inputs[i].value="";
        }
    }
    var textareas = document.getElementsByTagName("textarea");
    count = textareas.length;	
    for(var i=0;i<count;i++)
    {
       textareas[i].value="";      
    }	
}

/*=========================================================
 script wipe 
*/

function htmlFormat (strVal)
{
    reVal = /</g;
    strTmp = strVal.replace (reVal, "&lt;");
    reVal = />/g;
    strTmp = strTmp.replace (reVal, "&gt;");
    reVal = /"/g;
    strTmp = strTmp.replace (reVal, "&quot;");
     reVal = /'/g;
    strTmp = strTmp.replace (reVal, "&apos;");
    return (strTmp);
}

function trim(strVal)
{
    strTmp = strVal + "";
    if (strTmp.length == 0)
        return (strTmp);
    reVal = /^(\s| )*/;
    strTmp = strTmp.replace (reVal, '');
    reVal = /(\s| )*$/;
    return (strTmp.replace (reVal, ''));
}

/*=========================================================
 check input 
*/

function isInteger (strVal)
{
    strVal = TrimString (strVal);
    if (strVal.length == "")
        return (false);
    reVal = /^[\-\+]?([1-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)$/;
    return (reVal.test (strVal));
}

function isTime (strTime)
{
    strTime = (TrimString (strTime));
    if (strTime.length == 0)
        return (false);
    reVal = /^(([0-9]|[01][0-9]|2[0-3])(:([0-9]|[0-5][0-9])){0,2}|(0?[0-9]|1[0-1])(:([0-9]|[0-5][0-9])){0,2}\s?[aApP][mM])?$/;
    return (reVal.test (strTime));
}
function isDate (strDate)
{
    var nStart;
    var nEnd;
    var nYear;
    var nMonth;
    var nDay;
    var nFact;
    var arrDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    strDate = (TrimString (strDate));
    if (strDate.length == 0)
        return (false);
    reVal = /^([1-2]\d{3})[\/|\-](0?[1-9]|10|11|12)[\/|\-]([1-2]?[0-9]|0[1-9]|30|31)$/;
    if (!reVal.test (strDate))
        return (false);
    nStart = strDate.indexOf ("/", 0);
    nEnd = strDate.indexOf ("/", nStart + 1);
    nYear = eval (strDate.substring (0, nStart));
    nMonth = eval (strDate.substring (nStart + 1, nEnd));
    nDay = eval (strDate.substring (nEnd + 1, strDate.length));
    nFact = arrDay[nMonth - 1];
    if (nMonth == 2)
    {
        if ((nYear % 4 == 0 && nYear %100 != 0) || (nYear % 400 == 0))
            nFact ++;
    };
    if (nDay > nFact)
        return (false);
    return (true);
}
function isEditInteger (strVal)
{
    strVal = TrimString (strVal);
    if (strVal.length == "")
        return (false);
    reVal = /^[\-\+]?([0-9]\d*|0|[1-9]\d{0,2}(,\d{3})*)$/;
    return (reVal.test (strVal));
}
function isIP(strVal)
{
    strVal = (TrimString (strVal));
    if (strVal.length == 0)
        return (false);
    reVal = /^(\d{1}|\d{2}|[0-1]\d{2}|2[0-4]\d|25[0-5])\.(\d{1}|\d{2}|[0-1]\d{2}|2[0-4]\d|25[0-5])\.(\d{1}|\d{2}|[0-1]\d{2}|2[0-4]\d|25[0-5])\.(\d{1}|\d{2}|[0-1]\d{2}|2[0-4]\d|25[0-5])$/;
    return (reVal.test (strVal));
}
function isInt(strVal)
{ 
    var reVal = /^\d+$/g;
    return (reVal.test(strVal));
}

function $() {
  return document.getElementById(arguments[0]);
}
function $n() {
  return document.getElementsByName(arguments[0]);
}
