var userAgent = navigator.userAgent.toLowerCase();
var is_opera = userAgent.indexOf('opera') != -1 && opera.version();
var is_moz = (navigator.product == 'Gecko') && userAgent.substr(userAgent.indexOf('firefox') + 8, 3);
var is_ie = (userAgent.indexOf('msie') != -1 && !is_opera) && userAgent.substr(userAgent.indexOf('msie') + 5, 3);

function DateCheck(y,m,d){
	var  date =  new  Date(y, m-1, d);

	if (date.getFullYear() == y && ((date.getMonth()+1) == m) && date.getDate() == d){
		return true;
	}else{
		return false;
	}
}

function GetFileExt(name){
	return String(name.match(/\.\w{1,4}$/gi)).replace('.', '').toLowerCase();
}

function GetFilename(path){
	return path.match(/[^\/\\]+$/gi)[0];
}

String.prototype.sub = function(n){var r = /[^\x00-\xff]/g;if(this.replace(r, "mm").length <= n) return this;n = n - 3;var m = Math.floor(n/2);for(var i=m; i<this.length; i++){if(this.substr(0, i).replace(r, "mm").length>=n){return this.substr(0, i) +"...";}}return this;}

String.prototype.format = function(){
	var str = this;
	for(var i=0;i<arguments.length;i++){
	var re = new RegExp('\\[' + (i) + '\\]','gm');str = str.replace(re, arguments[i]);
	var re = new RegExp('\\{' + (i) + '\\}','gm');str = str.replace(re, arguments[i]);
	}
return str;
}


function fetchOffset(obj) {
var left_offset = obj.offsetLeft;
var top_offset = obj.offsetTop;
while((obj = obj.offsetParent) != null) {
left_offset += obj.offsetLeft;
top_offset += obj.offsetTop;
}
return { 'left' : left_offset, 'top' : top_offset };
}

var SubStr=function(str,length){
var a=str.match(/[^\x00-\xff]|\w{1,2}/g);
return a.length<length?str:a.slice(0,length).join("")+"……";
}


function   FormatMoney(number)   {   
if(number<0)   
var m =   '-'+FormatDollars(Math.floor(Math.abs(number)-0)   +   '')   +   FormatCents(Math.abs(number)   -   0);   
else   
var m =   FormatDollars(Math.floor(number-0)   +   '')   +   FormatCents(number   -   0); 



return m;

}   

function   FormatDollars(number)   {   
if   (number.length<=   3)   
return   (number   ==   ''   ?   '0'   :   number);   
else{   
var   mod   =   number.length%3;   
var   output   =   (mod   ==   0   ?   ''   :   (number.substring(0,mod)));
for   (i=0   ;   i<   Math.floor(number.length/3)   ;   i++)   {
if   ((mod   ==0)   &&   (i   ==0))
output+=   number.substring(mod+3*i,mod+3*i+3);   
else
output+=  number.substring(mod+3*i,mod+3*i+3);   
}
return   (output);   
}
}

function   FormatCents(amount)   {   
amount   =   Math.round(   (   (amount)   -   Math.floor(amount)   )   *100);   
return   (amount<10   ?   '.0'   +   amount   :   '.'   +   amount);   
}

function Dialog(title, html, callback,init){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();
	$('embed').hide();

	$('body').append('<div id="mask"><iframe src="javascript:false" style="position:absolute; visibility:inherit;top:0px;left:0px;width:100%;height:100%;z-index:-1;   filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';"></iframe></div><div class="dialog"><div class="dialog_title">'+title+'</div><div class="block20"></div>'+html+'<div class="dialog_bottom"><input type="button" class="button" id="dialog_ok" value="确定" /> <input type="button" class="button" value="取消" onclick="UnDialog();" /></div>');

	if (init){
		init.call();
	}

	$('#dialog_ok').click(callback);
}

function DialogMin(title, html, callback,init){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();
	$('embed').hide();

	var dialogHtml = '';
	dialogHtml += '<div id="mask"><iframe src="javascript:false" style="position:absolute; visibility:inherit;top:0px;left:0px;width:100%;height:100%;z-index:-1;filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';"></iframe></div><div class="dialog">';
	dialogHtml += '<div class="title-box"><div class="title-box-top"><b></b><h4></h4><i></i></div><div class="title-box-main">';
	dialogHtml += '</div></div>';
	dialogHtml += '</div>';

	dialogHtml = $(dialogHtml);
	dialogHtml.find('h4').html(title);

	if (typeof(html) == 'object'){
		dialogHtml.find('.title-box-main').append(html);
	}else{
		dialogHtml.find('.title-box-main').append($(html));
	}

	$('body').append(dialogHtml);

	if (init){
		init.call();
	}

	$('#dialog_ok').click(callback);
}


function DialogAlert(html,callback,init){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();
	$('embed').hide();

	var dialogHtml = '';
	dialogHtml += '<div id="mask"><iframe src="javascript:false" style="position:absolute; visibility:inherit;top:0px;left:0px;width:100%;height:100%;z-index:-1;filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';"></iframe></div><div class="dialog">';
	dialogHtml += '<div class="title-box"><div class="title-box-top"><b></b><h4>消息</h4><i></i></div><div class="title-box-main">';
	dialogHtml += '<div id="dialog-alert-main" style="margin:15px 0px 15px;text-align:center;font-size:14px;"></div>';
	dialogHtml += '<div class="normal-form clearfix" style="text-align:center;padding-left:;"><input type="button" value="" class="yes" id="dialog-ok" /></div>';
	dialogHtml += '</div></div>';
	dialogHtml += '</div>';

	dialogHtml = $(dialogHtml);


	dialogHtml.find('#dialog-alert-main').append(html);
	

	$('body').append(dialogHtml);

	if (init){
		init.call();
	}

	$('#dialog-ok').click(callback);
	$('#dialog-ok').click(function(){
		UnDialog();
	});
}

function DialogWin(title, html, callback,init){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();
	$('embed').hide();

	var dialogHtml = '';
	dialogHtml += '<div id="mask"><iframe src="javascript:false" style="position:absolute; visibility:inherit;top:0px;left:0px;width:100%;height:100%;z-index:-1;filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';"></iframe></div><div class="dialog dialog-large">';
	dialogHtml += '<div class="hbox"><div class="hbox-head dialog-title"><div class="hbox-head-left"><div class="hbox-head-right"><span class="float-l">'+title+'</span><span class="dialog-close"><img src="/images/icon-close.png" /></span></div></div></div><div class="hbox-main pad-a10">';
	dialogHtml += '</div></div>';
	dialogHtml += '</div>';

	dialogHtml = $(dialogHtml);

	if (typeof(html) == 'object'){
		dialogHtml.find('.hbox-main').append(html);
	}else{
		dialogHtml.find('.hbox-main').append($(html));
	}

	$('body').append(dialogHtml);

	if (init){
		init.call();
	}

	$('#dialog_ok').click(callback);
	$('.dialog-close').click(function(){
		UnDialog();
	});
	//$('.dialog').draggable({handle:'.dialog-title'});
}

function Loading(title){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();
	$('embed').hide();

	if(!title){
		title = '正在处理，请稍等...';
	}

	var dialogHtml = '';
	dialogHtml += '<div id="mask"><iframe src="javascript:false" style="position:absolute; visibility:inherit;top:0px;left:0px;width:100%;height:100%;z-index:-1;filter=\'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)\';"></iframe></div><div class="dialog">';
	dialogHtml += '<div class="title-box"><div class="title-box-top"><b></b><h4>Loading...</h4><i></i></div><div class="title-box-main">';
	dialogHtml += '<div class="loading" style="margin-left:140px;">正在处理，请稍等...</div>';
	dialogHtml += '</div></div>';
	dialogHtml += '</div>';

	dialogHtml = $(dialogHtml);

	$('body').append(dialogHtml);
}

function UnDialog(){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();
	$('embed').show();
}

function PointOut(obj,time,txt){
	if (obj.length<1){
		return false;
	}
	if (!txt){
		var txt = '';
	}


	var offset = obj.offset();
	var div = $('<div style="position:absolute;border:3px dashed #FF6600;text-align:center;background-color:#eee;" id="__point_out">'+txt+'</div>');
	div.css('left',offset.left-4);
	div.css('top',offset.top-4);
	div.css('width',obj.outerWidth());
	div.css('height',obj.outerHeight());
	div.css('line-height',obj.outerHeight() + 'px');
	$('body').append(div);

	var htime = 800;
	if (time){
		var htime = time;
	}

	div.animate({opacity: 0.1}, htime, false, function(){div.remove();});
}

function isFloat(val){
var re = /^[0-9\.]+$/ig;
if (!re.test(val))
{
return true;
}else{
return false; 
} 
}


function htmlspecialchars(txt){
txt = txt.replace(/&/g, '&amp;');
txt = txt.replace(/"/g, '&quot;');
txt = txt.replace(/'/g, '&#039;');
txt = txt.replace(/</g, '&lt;');
txt = txt.replace(/>/g, '&gt;');
return txt;
}


function isEmail(email) { 
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;"; 

    // Check for null 
    if (email == "") { 
        return true; 
    } 

    // Check for invalid characters as defined above 
    for (i=0; i<invalidChars.length; i++) { 
        badChar = invalidChars.charAt(i); 
        if (email.indexOf(badChar,0) > -1) { 
            return false; 
        } 
    } 
    lengthOfEmail = email.length; 
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) { 
        return false; 
    } 
    Pos = email.indexOf("@",1); 
    if (email.charAt(Pos + 1) == ".") { 
        return false; 
    } 
    while ((Pos < lengthOfEmail) && ( Pos != -1)) { 
        Pos = email.indexOf(".",Pos); 
        if (email.charAt(Pos + 1) == ".") { 
            return false; 
        } 
        if (Pos != -1) { 
            Pos++; 
        } 
    } 

    // There must be at least one @ symbol 
    atPos = email.indexOf("@",1); 
    if (atPos == -1) { 
        return false; 
    } 

    // But only ONE @ symbol 
    if (email.indexOf("@",atPos+1) != -1) { 
        return false; 
    } 

    // Also check for at least one period after the @ symbol 
    periodPos = email.indexOf(".",atPos); 
    if (periodPos == -1) { 
        return false; 
    } 
    if (periodPos+3 > email.length) { 
        return false; 
    } 
    return true; 
} 




function StarValid(){
	function Valid(obj){
		obj = $(obj);
		var valid = obj.attr('valid');
		var validName = obj.attr('valid-name');
		var validWith = obj.attr('valid-with');
		var validLength = parseInt(obj.attr('valid-length'));
		var value = obj.val();

		var errorId = obj.attr('error-id');
		var yesId = obj.attr('yes-id');
		var hideId = obj.attr('hide-id');

		var pass = true;
		var text = '';

		do{
			if (valid=='required'){
				if ($.trim(value)==''){
					text = '请输入' + validName + '';
					pass = false;
					break;
				}
			}

			if (valid=='email'){
				if ($.trim(value)=='' || !isEmail(value)){
					text = '请输入正确的'+ validName + '';
					pass = false;
					break;
				}
			}

			if (valid=='selected'){
				if (parseInt(value)<1){
					text = '请选择' + validName + '';
					pass = false;
					break;
				}
			}

			if (valid=='checked'){
				if (!obj.attr('checked')){
					text = '请选择' + validName + '';
					pass = false;
					break;
				}
			}

			if (validWith){
				if (value!=$('#'+validWith).val()){
					text = validName + '必须等于'+$('#'+validWith).attr('valid-name')+'';
					pass = false;
					break;
				}
			}

			if (validLength>0){
				if ($.trim(value).length<validLength){
					text = validName + '长度不能小于'+validLength+'个字符';
					pass = false;
					break;
				}
			}
		}while(false);


		var content = text;

		obj.parent().find('.valid-img').remove();
		obj.parent().find('.valid-msg').remove();

		if (pass){
			if (yesId){
				$('#'+yesId).html('<img class="valid-img" src="/images/valid-yes.gif"/>').show();
				$('#'+errorId).hide();
				$('#'+hideId).show();
			}else{
				obj.after('<img class="valid-img" src="/images/valid-yes.gif"/>');
			}
		}else{
			if (yesId){
				$('#'+errorId).html('<img class="valid-img" src="/images/valid-error.gif" align="absmiddle"/> <span class="valid-msg">'+content+"</span>").show();
				$('#'+yesId).hide();
				$('#'+hideId).hide();
			}else{
				obj.after('<div class="valid-msg">'+content+'</div>');
				obj.after('<img class="valid-img" src="/images/valid-error.gif" align="absmiddle"/>');
			}
		}

		return pass;
	}

	$('input[valid], textarea[valid]').blur(function(){Valid(this)});
	$('select[valid]').change(function(){Valid(this)});

	$('form').submit(function(){
		var pass = true;

		$(this).find('*[valid]').each(function(){
			if (!Valid(this)){
				pass = false;
			}
		});

		if (!pass){
			return false;
		}else{
			return true;
		}
	});
}


function clone(myObj)   
{   
    if(typeof(myObj) != 'object') return myObj;   
    if(myObj == null) return myObj;   
   
    var myNewObj = new Object();   
   
    for(var i in myObj)   
        myNewObj[i] = clone(myObj[i]);   
   
    return myNewObj;   
}

function PageBar(page, total, onePage, offset, onclick){
	var totalPage = Math.ceil( total / onePage );
	
	if ( !totalPage ) {
			totalPage = 1;
	}

	if ( page > totalPage || !page ){
			page = 1;
	}

	var mid = Math.floor( offset / 2 );
	var last = offset - 1;
	var minPage = ( page - mid ) < 1 ? 1 : page - mid;
	var maxPage = minPage + last;

	if ( maxPage > totalPage ){
		maxPage = totalPage;
		minPage = maxPage - last;
		minPage = minPage < 1 ? 1 : minPage;
	}

	var numPageBar = "";

	if (minPage != 1){
		//numPageBar += "<a href=\"javascript:void(0);\">1</a>...";
	}

	for ( var i = minPage; i <= maxPage; i++ ){
		if ( i == page ){
			numPageBar += " <small>" + i + "</small> ";
		}else{
			numPageBar += ' <a href="javascript:void(0);" rel="'+i+'"><small>' + i + '</small></a> ';
		}
	}

	if (maxPage != totalPage){
		numPageBar += '... <a href="javascript:void(0);" rel="'+totalPage+'"><small>' + totalPage + '</small></a> ';
	}

	var nextPageBar = "";
	if ( page < totalPage ){
		var nextPage = page + 1;
		nextPageBar = '<a href=\"javascript:void(0);\" rel="'+nextPage+'">下页</a>';
	}

	return numPageBar + nextPageBar;
}


function Str2Json(str){   
	try{
		var json = (new Function("return " + str))();
	}catch(err){
		var json = false;
	}
	return json;
}
