/*
*扩展jQuery utility functions
*
*by liumangqing@hotmail.com
*/	
(function($){
//start
  /**
   *  站点常用信息
   */
$.WEBDOMAIN={
	        HOST:'http://www.flog.com',
		 LOADGIF:'http://www.flog.com/images/load/loading_2.gif'
  	}
  /**
  *  常用正则表达式和错误提示文字
  */					 
$.REGEXP={
	   EMAIL:'^([a-zA-Z0-9_-]+\\.)*[a-zA-Z0-9_-]+@([a-zA-Z0-9_-]+\\.)+[a-zA-Z]+$',
        NAME:'^((\\w{3,32})|([\\u4e00-\\u9fa5]{2,12}))$',
	 NAME_CN:'^[\\u4e00-\\u9fa5]{2,12}$',
	  MOBILE:'^0?1[358]\\d{9}$',
	POSTCODE:'^\\d{6}$',
	     TEL:'^(0\\d{2,3}[\\s-])?\\d{7,8}([\\s-]\\d{4,5})?$'
    }
  /**
  *  字符串连接操作数组。
  *  字符输出连接操作函数
  */	
$.StringBuffer=new Array();
$.getStrBuffer=function(){
	var str=$.StringBuffer.join('');
	$.StringBuffer.length=0;
	return str;
	}
  /**
   *  插入loading Gif 动画
   *
   * @param String id-----id名            
   * @return void
   */   
$.innerLoadGif=function(id){
	   $(id).html('<img src="'+$.WEBDOMAIN.LOADGIF+'" style="position:absolute;margin-left:'+($(id).width()/2-8)+'px; margin-top:'+($(id).height()/2-8)+'px;"/>');
	}			 
  /**
   *  截取URL尾巴参数值
   *
   * @param String argname-----参数名称            
   * @return String
   */
$.getParam=function(argname){
        var url = document.location.href;
        var arrStr = url.substring(url.indexOf("?")+1).split("&");
        for(var i =0;i<arrStr.length;i++){
            var loc = arrStr[i].indexOf(argname+"=");
            if(loc!=-1){
              return arrStr[i].replace(argname+"=","").replace("?","");
            }  
         }
       return "";
    }
  /**
   *  正则验证函数
   *
   * @param String argname-----参数名称            
   * @return String
   */	
$.regExp=function(str,reStr){
	var regExp=new RegExp(reStr);
	return regExp.test($.trim(str));
	}
  /**
   *  使用$.getJSON创建select option
   *  json格式为{result:'y/n',data:['数据']}
   * @param string id    SELECT选框id
   * @param string url   查询地址
   * @param string parm{first:'请选择城市',_key:'option value',_value:'选项值'}
   * @return String
   */
$.getChildren=function(id,url,parm){
        $(id).html('<option>查询中...</option>').attr('disabled','disabled');		
		$.getJSON(url,function(json){
              if(json.result=='y'){
				    if(parm.firstOption){
			            $.StringBuffer.push('<option value="-1">'+parm.firstOption+'</option>');
					}
			  	    $.each(json.data,function(i,value){ $.StringBuffer.push('<option '+'value='+value[parm._key]+' >'+value[parm._value]+'</option>');});
		            $(id).html($.getStrBuffer()).attr('disabled','');
			  }else{
			        $(id).html('<option value="-1">查询失败</option>').attr('disabled','disabled');
			  }
			  
		});		
}
  /**
   *  刷新验证码图片
   *
   * @param String id-----图片id
   * @param String url----图片地址
   * @return String
   */	
$.refreshYZM=function(id,url){
	$(id).attr('src',url+'&rand='+Math.random());
	}
  /**
   * 获取radio或checkbox值,以逗号隔开选项值形式返回
   *
   * @param String name-----单复选的值
   * @return String
   */	
$.getBoxRadioValue=function(name){
	   var str="";
	       $('input[name="'+name+'"]').each(function(){if(this.checked){ if(str){str+=','+this.value;}else{str=this.value;}}});
	    return str;
	}
/**
*获取字符串长度，双字节算两个
*@param String s 
*@return Int  
*/
$.bytes=function(str){
	var str=$.trim(str);
	return str.replace(/[^\x00-\xff]/g, "**").length;
	}
/**
*通用验证函数
*@param String val-------------需要验证的值         
*@param regexp expStr----------正则表达式
*@param function callback------回调函数
*@return boolean
*@example
*       $.checkVal('liumangqing','/^\\w{3,15}$/',function(b){
*														  if(b){
*															     alert('ok');
*															  }else{
*																  alert('error');
*																  }
*														  });
*/
$.checkVal=function(val,expStr,callback){
		var b=$.regExp(val,expStr);
		if(callback){
			    callback(b);
			}
		return b;
	}
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', {expires: 7, path: '/', domain: 'jquery.com', secure: true});
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
$.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
//end
})(jQuery)