﻿var Utils = {};
/*=======================================================================================
固有类扩展 删除两边空格
=======================================================================================*/
String.prototype.trim = function(trimChar){
  if(null == trimChar){
	  return this.replace(/(^\s*)|(\s*$)/g, "");
	}else{
	  return this.ltrim(trimChar).rtrim(rtrim);
	}
}
/*=======================================================================================
删除左边空格
=======================================================================================*/
String.prototype.ltrim = function(trimChar){ 
    if(null == trimChar){
      return this.replace(/(^\s*)/g, ""); 
    }else{
      return this.replace(new RegExp("^"+trimChar+"*", "g"), ""); 
    }
} 
/*=======================================================================================
删除右边空格
=======================================================================================*/
String.prototype.rtrim = function(trimChar){ 
    if(null == trimChar){
      return this.replace(/(\s*$)/g, ""); 
    }else{
      return this.replace(new RegExp(trimChar+"*$", "g"), ""); 
    }
}
/*** 检查是否由数字组成 ***/ 
String.prototype.isDigit = function() { 
	var s = this.trim(); 
	return (s.replace(/\d/g, "").length == 0); 
}
/*** 检查是否为数 ***/ 
String.prototype.isNumber = function() { 
	var s = this.trim(); 
	return (s.search(/^[+-]?[0-9.]*$/) >= 0); 
}
/*** 返回字节数 ***/ 
String.prototype.lenb = function() { 
	return this.replace(/[^\x00-\xff]/g,"**").length; 
}
/*** 检查是否包含汉字 ***/ 
String.prototype.isInChinese = function() { 
	return (this.length != this.replace(/[^\x00-\xff]/g,"**").length); 
}
/*=======================================================================================
字符串转int option 10 10进制 8 8进制...
=======================================================================================*/
String.prototype.toInt = function(option){
	if(null == option) option = 10;
	return parseInt(this, option);
};
/*=======================================================================================
字符串转Float
=======================================================================================*/
String.prototype.toFloat = function(){
	return parseFloat(this);
}
/*=======================================================================================
四舍五入n为要转换的位数
=======================================================================================*/
String.prototype.round = function(n){
	return Utils.numberRound(this.toFloat(), n);
}
/*email检测*/
String.prototype.isEmail = function() {
	var ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.+@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(var i=0; i<this.length; i++){
		if (ok.indexOf(this.charAt(i))<0) {
			return false;
		}
	}
	if (document.images) {
		var re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		var re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,8}|[0-9]{1,3})(\]?)$/;
		if (!this.match(re) && this.match(re_two)) {
			return -1;
		}
	}
}
/*=======================================================================================
type 0 加前边 1 加后边 str 要加的字符串 len 长度
=======================================================================================*/
String.prototype.formatString = function(type, str, len){
	if(0 != type && 1 != type) return "";
	if(undefined == str || 0 == str.length) return "";
	var text = this;
	while(text.length<len){ 
		if(0 == type){ text = str + text; }
		else if(1 == type){ text = text + str; }
	}
	return text;
}
/*=======================================================================================
字符串转uri
=======================================================================================*/
String.prototype.toUri = function(){
	return new Uri(this);
}
Utils.getTimeSpan = function(){
  var date = new Date();
  return date.getFullYear() + date.getMonth() + date.getDate() + date.getHours() + date.getMinutes() + date.getSeconds() + date.getMilliseconds();
}
/*=======================================================================================
uri对象
=======================================================================================*/
function Uri(uri){
	this.uri = uri;
	this.baseUri = "";
	this.AnchorName = "";
	this.paramers = [];
	this._init();
}
Uri.prototype._init = function(){
	var start = this.uri.indexOf("?");
	if(-1 == start) {
		this.baseUri = this.uri;
		return;
	}else{
		this.baseUri = this.uri.substring(0, start);
	}
	var end = this.uri.lastIndexOf("#");
	if(-1 == end){
	  end = this.uri.length;
	}else{
	  this.AnchorName = this.uri.substring(end+1, this.uri.length);
	}
	var pstr = this.uri.substring(start+1, end);
	var pstrs = pstr.split("&");
	if(0 == pstrs.length) return;
	for(var i=0; i<pstrs.length; i++){
		var paramer = pstrs[i];
		if("" == paramer) continue;
		var params = paramer.split("=");
		if(1 == params.length) params.push("");
		this.paramers.push(params);
	}
}
Uri.prototype.addTimeSpan = function(){
	this.addParamer("timespan", Utils.getTimeSpan());
	return this;
}
Uri.prototype.toString = function(){
	if(this.paramers.length > 0){
		var pstrs = [];
		for(var i=0; i<this.paramers.length; i++){
			pstrs.push(this.paramers[i].join("="));
		}
		var anchor = ((this.AnchorName == "") ? "" : "#" + this.AnchorName);
		return this.baseUri + "?" + pstrs.join("&") + anchor;
	}else{ return this.baseUri; }
}
Uri.prototype.toString2 = function(){
	if(this.paramers.length > 0){
		var pstrs = [];
		for(var i=0; i<this.paramers.length; i++){
			pstrs.push(this.paramers[i].join("="));
		}
		return this.baseUri + "?" + pstrs.join("&");
	}else{ return this.baseUri; }
}
Uri.prototype._isExists = function(name){
	var exists = false;
	for(var i=0; i<this.paramers.length; i++){
		if(name == this.paramers[i][0]){
			exists = true; break;
		}
	}
	return exists;
}
Uri.prototype.addParamer = function(name, value){
	if(undefined == name || null == name || "" == name) return;
	var paramer = this._getParamer(name);
	if(null == paramer){
		paramer = [];
		paramer.push(name);
		paramer.push(value);
		this.paramers.push(paramer);
	}else{
		paramer[1] = value;
	}
	return this;
}
Uri.prototype.removeParamer = function(name){
	if(undefined == name || null == name || "" == name) return;
	for(var i=0; i<this.paramers.length; i++){
		if(name == this.paramers[i][0]){
			this.paramers.splice(i,1);
			return;
		}
	}
}
Uri.prototype._getParamer = function(name){
	if(0 == this.paramers.length) return null;
	for(var i=0; i<this.paramers.length; i++){
		if(name == this.paramers[i][0])
			return this.paramers[i];
	}
	return null;
}
Uri.prototype.getParamer = function(name){
	if(0 == this.paramers.length) return "";
	for(var i=0; i<this.paramers.length; i++){
		if(name == this.paramers[i][0])
			return this.paramers[i][1];
	}
	return "";
}
/*=======================================================================================
uri对象
=======================================================================================*/
/*=======================================================================================
path对象
=======================================================================================*/
var Path = function(path){
  if(null == path) path = "";
  path = path.trim();
  if("" == path) {
    this.basePath = "";
    this.subs = [];
    this.fileName = "";
    this.fileExtName = "";
    return;
  }
	this.basePath = path;
	this.subs = path.split("\\");
	this.fileName = this.subs[this.subs.length-1];
	var subFileNames = this.fileName.split(".");
	if(1 == subFileNames.length) {
	  this.fileExtName = "";
	}else{
	  this.fileExtName = subFileNames[subFileNames.length-1];
	}
}
Path.instance = function(filePath){
  return new Path(filePath);
}
/*=======================================================================================
path对象
=======================================================================================*/
/*=======================================================================================
连接
=======================================================================================*/
var IsBusy = false;
function Connection(){
	this.RequestType = "POST";
	this.XmlHttp = null;
	try { this.XmlHttp =new ActiveXObject("Msxml2.XMLHTTP");} catch (e) {
		try { this.XmlHttp =new ActiveXObject("Microsoft.XMLHTTP");	} catch (oc) {
			try{ this.XmlHttp = new XMLHttpRequest(); }catch(ee){this.XmlHttp = null;}
		}
	};
	Connection.prototype.Send = function(uri,post_data,CallBack){
		try{
//			if(IsBusy){ 
//				return;
//			}
//			IsBusy = true;
			var xmlHttp = this.XmlHttp;
			xmlHttp.open(this.RequestType, uri, true);
			xmlHttp.onreadystatechange = function() {
				try{
					if(xmlHttp.readyState != 4){
						return; 
					}
					if(xmlHttp.status != 200) {
					    return;
					}
					CallBack(xmlHttp);
//					IsBusy = false;
					//delete this;
				}catch(e){ xmlHttp.abort(); }
			}
			xmlHttp.send(post_data);
		}catch(e){}
	};
}
Connection.instance = function(){
  return new Connection();
}
/*=======================================================================================
连接
=======================================================================================*/
/*=======================================================================================
DomReader
=======================================================================================*/
/*=======================================================================================
DomReader
=======================================================================================*/
/*=======================================================================================
Menus对象
=======================================================================================*/
Utils.Menus = [];
Utils.RegisterMenu = function(obj){
    var isExists = false;
    for(var index=0; index<this.Menus.length; index++){
        if(this.Menus[index] == obj){ isExists = true;}
    }
    if(!isExists){ this.Menus.push(obj);}
}
Utils.StopBubbleAndCloseOther = function (e,menu){
    try{
        if(!e){e = event;}
        if(e.stopPropagation) e.stopPropagation();
        else e.cancelBubble = true;
        for(var index=0; index<this.Menus.length; index++){
            try{
                if(menu != this.Menus[index]){
                this.Menus[index].style.display = "none"; }
            }catch(e){}
        }
    }catch(e){}
}
document.onclick = function(){
    for(var index=0; index<Utils.Menus.length; index++){
        try{
            Utils.Menus[index].style.display = "none";
        }catch(e){}
    }
}
Utils.gridMenuClick = function(id){
    if(id && id.tagName && "DIV"==id.tagName){
        if(id.nextSibling && id.nextSibling.tagName && "UL" == id.nextSibling.tagName){
            var menu = id.nextSibling;
            if(!menu.hasChildNodes()){
                alert("对不起!你可能没有权限访问这个上下文菜单");
                return;
            }
            Utils.RegisterMenu(menu);
            menu.style.left = event.x + "px";
            menu.style.top =  event.y + "px";
            menu.style.display = "block";
            Utils.StopBubbleAndCloseOther(event,menu);
        }
    }
}
Utils.gridMenuOut = function(obj){
    obj.style.backgroundImage = "url(/img/gridmenumouseout.gif)";
    //obj.style.backgroundColor = "";
}
Utils.gridMenuOver = function(obj){
    obj.style.backgroundImage = "url(/img/gridmenumouseover.gif)";
    //obj.style.backgroundColor = "#B5BED6";
}
/*=======================================================================================
Menus对象
=======================================================================================*/
Utils.findPosX = function(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else if (obj.x) curleft += obj.x;
	return curleft;
}
Utils.findPosY = function(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;
}
Utils.findPosX = function(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
    } else if (obj.x) curleft += obj.x;
    return curleft;
}

Utils.findPosY = function(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    } else if (obj.y) curtop += obj.y;
    return curtop;
}
//====           common mouseover mouseout         ====================================
Utils.MouseOver = function(obj){
    if("#DEE" == obj.style.backgroundColor) return;
    obj.style.backgroundColor = "#8080C0";
    //obj.style.backgroundImage = "url(/img/menuover.gif)";
    obj.style.lineHeight = "20px";
    obj.style.color = "#FFF";
}
Utils.MouseOut = function(obj){
    if("#FFE" == obj.style.backgroundColor) return;
    obj.style.backgroundColor = "#FFE";
    //obj.style.backgroundImage = "";
    obj.style.lineHeight = "22px";
    obj.style.color = "#000";
}
Utils.getdate = function (ipt){
    showModalDialog('/js/calendar.html',ipt, "dialogWidth:252px;dialogHeight:219px;status:no;scroll:no");
}
Utils.ResetImg = function(obj,width, height){
    //if(obj.width == 0){
      obj.width = width;
      obj.height = height;
      return;
    //}
    var w = (obj.width+"").toFloat()/width;
    var h = (obj.height + "").toFloat()/height;
    if(w>1 && h>1){
      if(w>h){
        obj.width = width;
      }else{
        obj.height = height;
      }
    }else if(w>1 || h>1){
      if(w>h){
        obj.width = width;
      }else{
        obj.height = height;
      }
    }
}
Utils.pagerCheck = function(pageIndex, pageCount){
  var val = document.getElementById(pageIndex).value; 
  var result = (val != "" && val.isDigit() && val.toInt()>0 && val.toInt()<=pageCount);
  if(!result){
    alert("请输入正确的页码");
  }
  return result;
}
Utils.GoToPage = function(pageIndex, pageCount,name){
  var val = document.getElementById(pageIndex).value; 
  var result = (val != "" && val.isDigit() && val.toInt()>0 && val.toInt()<=pageCount);
  if(!result){
    alert("请输入正确的页码");
  }else{
    var url = (location+"").toUri().addParamer(name, val).toString();
    document.forms[0].action = url;
    document.forms[0].submit();
  }
  return false;
}
Utils.CutString = function(){
/*  for(var i=0;i<document.links.length;i++){
    var link = document.links[i];
    alert(link.innerText);
    if(link.className == "cut"){
      alert(link.innerText);
    }
  }
*/
}
setTimeout(dontknow, 1000);
function dontknow(){
  var linklist = document.links;
  for(var i=0; i<linklist.length; i++){
    var lindex = linklist[i].href.indexOf("#")
    if(-1 != lindex){
      if((lindex + 1) != linklist[i].href.length){
        continue;
      }
      linklist[i].target = "_blank";
      //linklist[i].href="/dontknow.htm";
    }
  }
}
function mainmover(mindex, obj){
    for(var i=1; i<6; i++){
        document.getElementById("MainM"+i).src = "../images/cen_menu_a"+i+".jpg";
    }
    obj.src = "../images/cen_menu_a"+mindex+"on.jpg";
}
function mainmout(mindex, obj){
    for(var i=1; i<6; i++){
        var cur = document.getElementById("MainM"+i);
        if(i==curmenumindex){
            cur.src = "../images/cen_menu_a"+i+"on.jpg";
        }else{
            cur.src = "../images/cen_menu_a"+i+".jpg";
        }
    }
}
var ddd = false;
function HiddenLoadImg(imgindex, obj){
    /*var img = document.getElementById("Loading_"+imgindex);
    if(img && img != null){
        img.style.display = "none";
    }
    */
    //obj.style.display = "block";
}
function WriteComment(proCode)
{
    $.blockUI({ 
            message: $('#addComment'), css: { 
                top:  ($(window).height() - 300) /2 + 'px', 
                left: ($(window).width() - 400) /2 + 'px',
                width: '400px',height: '240px' 
            }
      }); 
    $('#yes').click(function() {             
            var tempName = escape($("#UserName").val());
            var tempTitle = escape($("#Title").val());
            var tempText = escape($("#Text").html());
            if(tempName.length<2||tempName.length>150)
            {
                alert("姓名长度在2个到150个字符之间");
                return false;
            }
            if(tempTitle.length<2||tempTitle.length>150)
            {
                alert("标题长度在2个到50个字符之间");
                return false;
            }
            if(tempText.length>1000)
            {
                alert("内容字符在1000个以内");
                return false;
            }
            this.disabled="disabled";
            this.value = '您已经评论过了';
            var UpStr = "GoodsCode="+escape(proCode);            
            UpStr += "&UserName="+tempName;
            UpStr += "&Title="+tempTitle;
            UpStr += "&Content="+tempText;
            // update the block message 
            $.ajax({ 
                url: '/Goods/AJGoodsCommentAdd.aspx?'+UpStr, 
                cache: false, 
                success: function(data, textStatus) {
                    $("#UserName").val("");
                    $("#Title").val("");
                    $("#Text").html("");
                     $.blockUI({ message: "<h3>您的评论已经发布成功,<br />需要我们管理员审核后才能看到！</h3><div style=\"width:100%; text-align:center;\"><input type=\"button\" value=\" 确定 \" onclick=\"$.unblockUI();\" ></div>",css:{ width: '400px',height: '120px', textAlign: 'left' } }); 
                     
                },
                error: function()
                {
                     $.blockUI({ message: "<h3>出现服务器错误,管理员会解决这个问题的！</h3><div style=\"width:100%; text-align:center;\"><input type=\"button\" value=\" 确定 \" onclick=\"$.unblockUI();\" ></div>",css:{ width: '400px',height: '120px', textAlign: 'left' } }); 
                }
            }); 
        }); 
 
        $('#no').click(function() { 
            $.unblockUI(); 
            return false; 
        }); 
} 