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]);}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)   {   
	number = number.fix(2);
	return 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 ShareDialog(html,callback){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();

	$('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)\';"></div><div class="dialog"><div class="dialog_title">推荐</div><div class="block5"></div>'+html+'<div class="mf"><div class="mleft">评论：</div><div class="mright"><textarea id="share_description" style="width:300px;height:50px;"></textarea></div></div><div class="dialog_bottom"><input type="button" class="btn" id="dialog_ok" value="确定" /> <input type="button" class="btn" value="取消" onclick="UnDialog();" /></div></div>');

	$('#dialog_ok').click(callback);
}

function Dialog(title, html, callback,init){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();

	$('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 Win(title, html, callback,init){
	$('body').find('.dialog').remove();

	$('body').append('<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 Loading(html){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();

	$('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">Loading...</div></div>');
}

function UnDialog(){
	$('body').find('#mask').remove();
	$('body').find('.dialog').remove();
}

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 red;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.width());
	div.css('height',obj.height());
	div.css('line-height',obj.height() + '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 false; 
    } 

    // 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 validLength = parseInt(obj.attr('valid-length'));
		var value = obj.val();

		var pass = true;
		var text = '';

		do{
			if (valid=='required'){
				if ($.trim(value)==''){
					text = validName + ' is required.';
					pass = false;
					break;
				}
			}

			if (valid=='email'){
				if ($.trim(value)=='' || !isEmail(value)){
					text = 'Are you sure '+ validName + ' is correct.';
					pass = false;
					break;
				}
			}

			if (valid=='selected'){
				if (parseInt(value)<1){
					text = 'Please select ' + validName + '.';
					pass = false;
					break;
				}
			}

			if (validLength>0){
				if ($.trim(value).length<validLength){
					text = validName + ' must contain a minimum of '+validLength+' characters.';
					pass = false;
					break;
				}
			}
		}while(false);


		var content = text;

		obj.parent().find('.valid-img').remove();
		obj.parent().find('.valid-msg').remove();

		if (pass){
			obj.after('<img class="valid-img" src="/images/valid-yes.gif"/>');
		}else{
			obj.after('<div class="valid-msg" style="font-size:11px;color:red;">'+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;
		}
	});
}



  String.prototype.fn   =   function(n)   
      {   s=""   
          for(i=0;i<n;i++)s+=this   
          return   s   
      }   
  Number.prototype.fix   =   function(num)   
      {with(Math)return   (round(this.valueOf()*pow(10,num))/pow(10,num)).toString().search(/\./i)==-1?(round(this.valueOf()*pow(10,num))/pow(10,num)).toString()+"."+"0".fn(num):(round(this.valueOf()*pow(10,num))/pow(10,num));   
      }   