﻿/* Ajax ---------------------------------------------------------------------------------------------*/
//var Ajax=new Object();
function Ajax(method,url,callBack,content)
{
	url=urlRandom(url);
	var XmlHttp=createXmlHttp();
	if(callBack)
	{
		XmlHttp.onreadystatechange=function(){
			callBack(XmlHttp);
			//delete XmlHttp when complete;
			if(XmlHttp.readyState==4){
				if(XmlHttp!=null) XmlHttp.abort();
			}
		}
	}
	try {
		XmlHttp.open(method,url,callBack?true:false);
	}
	catch(e){
		throw new Error(e);
	}
	if(method.toLowerCase()=="post"){
		XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	}
	XmlHttp.send(content);
}
/* create XMLHttpRequest */
function createXmlHttp()
{
	var xmlHttp=null;
	if(window.ActiveXObject)
	{
		try{
			xmlHttp=new ActiveXObject("Microsoft.XmlHttp");
		}catch(e){
			xmlHttp=new ActiveXObject("MSXML2.XmlHttp");			
		}
	}
	else if(window.XMLHttpRequest)
	{
		xmlHttp=new XMLHttpRequest();
	}
	return xmlHttp;
}
function urlRandom(url){
	if(url.indexOf("?")==-1) url=url+"?rid="+Math.random();
	else url=url+"&rid="+Math.random();
	return url;
}
/* Function -----------------------------------------------------------------------------------------*/
/* get Xml Object */
function getXmlDoc()   
{   
    if(document.implementation && document.implementation.createDocument)   
    {   
        var doc=document.implementation.createDocument("","",null);   
        doc.addEventListener("load",function(e){this.readyState=4;},false);   
        doc.readyState=4;
        return doc;   
    }   
    else   
    {   
        var msxmls=["MSXML2","Microsoft","MSXML","MSXML3"];   
        for(var i=0;i<msxmls.length;i++)
        {
            try
            {
				return new ActiveXObject(msxmls[i]+".DomDocument")
            }catch(e){}
        }
        throw new Error("Could not find an installed XML parser!");
    }   
}
function judgeNum(Num)
{
	var strTemp="0123456789";
	if(Num.length!=0)
	{
		for(var i=0;i<Num.length;i++)
		{
			if(strTemp.indexOf(Num.charAt(i))==-1)
			{
				return false;
				break;
			}
		}
		return true;
	}
}
function cutStr(str,max)
{
	if(str.length>max)
	{
		str=str.substring(0,max)+"...";
	}
	return str;
}
/* select key str */
function findKey(str,key)
{
	if(str.indexOf(key)==-1)
		return false;
	else
		return true;
}
/* $ <=> document.getElementById */
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);
    if (arguments.length == 1)
      return element;
    elements.push(element);
  }
  return elements;
}

/* operate cookie */
//read cookie
/*function getCookie(cookieName)
{
	var cookieStr=document.cookie;
	var cookieArr=cookieStr.split("; ");
	if(cookieArr.length>0)
	{
		for(var i=0;i<cookieArr.length;i++)
		{
			var arr=cookieArr[i].split("=");
			if(cookieName==arr[0])
			{
				return arr[1];
				break;
			}
		}
	}
	return "";
}*/
//delete Cookie  
function delCookie(name)
{
	var exp=new Date();    
	exp.setTime (exp.getTime() - 1);    
	var cval=getCookie(name);    
	document.cookie = name+"="+cval+"; expires="+exp.toGMTString()+"; path='/'";  
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


//write by miracolo
function writeCookie(name, value) 
{ 
	var expire = ""; 
	var hours = 365;
	expire = new Date((new Date()).getTime() + hours * 3600000); 
	expire = ";path=/;expires=" + expire.toGMTString(); 
	document.cookie = name + "=" + escape(value) + expire; 
}

/////构造新的COOKIE删除方法
function DeletecookieN (name) {  //删除名称为name的Cookie  
    var exp = new Date();    
    exp.setTime (exp.getTime() - 1);    
    var cval = getCookie(name);    
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString(); "; path='/'";  
}  

function Clearcookie()   //清除COOKIE  
{  
	var temp=document.cookie.split(";");  
    var loop3;  
    var ts;  
    for (loop3=0;loop3<temp.length;loop3++)        
	 {  
	        ts=temp[loop3].split("=")[0];  
			DeletecookieN(ts);   //如果ts含“mycat”则执行清除  
     }  
}  
