﻿/* 
公共客户端操作js库 
*/
var picSiteUrl = 'http://itv.ifeng.com/vip';
var domain = 'ifeng.com';
var onloadActions = new Array();
window.onloadActions = onloadActions;
function onWindowLoad()
{
    for(var i=0;i<window.onloadActions.length;i++)
    {
        window.onloadActions[i].callback.call(window.onloadActions[i]);
    }
}


//公共ajax通信类库
var net = new Object();
net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;
net.ContentLoader=function(url,method,requestParams,onload,onerror,extendParams)
{
    this.url = url;
    this.req = null;
    this.onload = onload;
    this.onerror = (onerror) ? onerror : this.defaultError;
    this.extendParams = extendParams;
    this.requestParams = requestParams;
    this.method = method;   
}
net.ContentLoader.prototype={
    loadXMLDoc:function(notAppendRandom)
    {
        if( window.XMLHttpRequest)
        {
            this.req = new XMLHttpRequest();
        }
        else if( window.ActiveXObject)        
        {
            this.req = new ActiveXObject("Microsoft.XMLHTTP");  
        }    
        if( this.req )
        {
            try
            {
                var loader = this;
                this.req.onreadystatechange = function(){
                    loader.onReadyState.call(loader);
                }
                if( this.method == 'GET' )
                {
                    if(!notAppendRandom)
                    {
                      if( this.requestParams.length != 0 )
                          this.requestParams += "&a="+Math.random();
                      else
                          this.requestParams = "a="+Math.random();
                    }
                    
                    this.req.open(this.method,this.url+"?"+this.requestParams,true);    
                    this.req.send(null);                
                }
                else
                {
                   this.req.open(this.method,this.url,true); 
                   this.req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                   this.req.send(this.requestParams);                
                }
            }
            catch(err)
            {
                this.onerror.call(this);
            }
        }
    },
    onReadyState:function()
    {
        var req = this.req;
        var ready = req.readyState;
        if( ready ==net.READY_STATE_COMPLETE)
        {
            var httpStatus = req.status;
            if( httpStatus == 200 || httpStatus == 0 )
            {
                this.onload.call(this);
            }
            else            
            {
                this.onerror.call(this);
            }
        }
    },
    defaultError:function()
    {
        alert("error occured");      
    }
    
}


function $(obj)
{
    if (typeof(obj) == "string")
        return document.getElementById(obj);
    else
        return obj;
} 

function isEmptyStr(str) 
{
    return "" == trimStr(str);
}
function trimStr(str)
{
    if(str == undefined || str == null) 
        return "";
    return str.replace(/(^(\s|\u0000)*|(\s|\u0000)*$)/g, "");
}
function isNumber(str)
{
    var str1=trimStr(str);
    return str1.replace( /[0-9]/g,'') ;
}
function isMobileNumber(str)
{	
	var reg=/^13\d{9}$/ ;
	return reg.test(str);
}
function isIDCardNumber(str)
{
  if(str == undefined || str == null) 
	return "";	
	var reg=/^\d{15}$/;
	if(str.length==15)
	{		
	
	 return reg.test(str);
	}	
	var regs=/^\d{17}(?:\d|x)$/;
	if(str.length==18)
	{				
	return regs.test(str);
	}
	
	return false;   
}

function setStrInStr(str1, str2, ch, clear)
{
    if(clear)
    {//从str1中清除清除str2，以ch分隔
       str1 = clearStrInStr(str1, str2, ch);
    }
    else
    {//将str2追加到str1末尾，以ch分隔
       str1 = clearStrInStr(str1, str2, ch);
       if( str1.length > 0)
       {    
            if(str1.charAt(str1.length-1) == ch)
                str1 += str2;
            else
                str1 += ch+str2;            
       }
       else
            str1 = str2;
    } 
    return str1;   
}

function clearStrInStr(str1, str2, ch)
{
    var re = new RegExp("^"+str2+"("+ch+"|$|\ +)","ig");
    str1 = str1.replace(re,'');
    re = new RegExp(ch+str2+ch,"ig");
    str1 = str1.replace(re,ch);
    re = new RegExp(ch+str2+"($|\ +)","ig");
    str1 = str1.replace(re,'');        
    return str1;
}
function isInteger(strInteger) 
{
    return strInteger.match(/^\d+$/);
    // return strInteger.match(/^(-|\+)?\d+$/);
} 

function GetPosition(o)
{
    var e = $(o);
    var t=e.offsetTop;
    var l=e.offsetLeft;
    while(e=e.offsetParent){
        t+=e.offsetTop;
        l+=e.offsetLeft;
    }
    var result = new Array();   
    result[0] = t;
    result[1] = l;
    return result;
}

function visiObjs(tagName, displayFlag)
{
    var objs = new Array();
    objs = document.getElementsByTagName(tagName);
    for (var i=0; i < objs.length; ++i)
    {
        objs[i].style.display = displayFlag;
    }
}
//图片缩放
function imgResize(img,width,height) 
{
	if (width == 0 && height == 0) return;
	var obj = new Image();
	obj.src = img.src;
	if (width == 0)
	{
		if(obj.height>=height) 
		{
			img.removeAttribute('width');
			img.height=height;
		}
		obj = null;
		return;
	}
	if (height == 0)
	{
		if(obj.width>=width)
		{
			img.removeAttribute('height');
			img.width=width;
		}
		obj = null;
		return;
	}
	if ((obj.width/obj.height) > (width/height)) 
	{
		if(obj.width>=width)
		{
			img.removeAttribute('height');
			img.width=width;
		}
	} 
	else 
	{
		if(obj.height>=height) 
		{
			img.removeAttribute('width');
			img.height=height;
		}
	}
	obj = null;
}
function imgWheel(o)
{
	var zoom = parseInt(o.style.zoom, 10)||100;
	zoom += event.wheelDelta / 12;
	if (zoom>0) o.style.zoom = zoom + '%';
	return false;
}
function imgResizeAll(obj,width,height) 
{
	for (var i = 0; i < obj.children.length; i ++) 
	{
		var img = obj.children[i];
		if (img.tagName.toLowerCase() == 'img')
		{
			imgResize(img,width,height)
		}
		imgResize(obj.children[i]);
	}
}



// ------- cookie -------
function getExpTime(time,type){

    var expTime = new Date();

	switch(type) {

		case 'year' :

			expTime.setFullYear(expTime.getFullYear() + time );

			break ;

		case 'month' :

			expTime.setMonth( expTime.getMonth() + time );

			break;

		case 'day' :

			expTime.setDate( expTime.getDate() + time );

			break;

		case 'hour' :

			expTime.setHours( expTime.getHours() + time );

			break;

	}

    return expTime.toGMTString();

}



function getCookieVal(offset) {

	var endstr = document.cookie.indexOf(";", offset);

	if (endstr == -1)

	endstr = document.cookie.length;

	return decodeURIComponent(document.cookie.substring(offset, endstr));

}



function getCookie(name) {

	var arg = name + "=";

	var alen = arg.length;

	var clen = document.cookie.length;

	var i = 0;

	while (i < clen){

		var j = i + alen;

		if (document.cookie.substring(i, j) == arg)

		return getCookieVal(j);

		i = document.cookie.indexOf(" ", i) + 1;

		if (i == 0) break;

	}

	return "";

}



function setCookie(name,value,expires,path,domain,secure) {

	document.cookie = name + "=" + encodeURIComponent(value) +

	((expires) ? "; expires=" + expires : "") +

	((path) ? "; path=" + path : "") +

	((domain) ? "; domain=" + domain : "") +

	((secure) ? "; secure" : "");
	

}

function delCookie(name){

	var guoqu = new Date();

	expiresTime = guoqu.setTime(guoqu.getTime() - 100);

	setCookie(name,'',expiresTime,'/','.'+domain) ;

}
