﻿/*****字符串扩展*********/
String.prototype.Trim=function()
{
	return this.replace(/(^\s*)|(\s*$)/g, '');
}
String.prototype.isHtmlEmpty=function()
{
   return (this.Trim().replace(/<P>&nbsp;<\/P>/ig,'').replace(/(<br>)| /ig,'').Trim()=='');
}
String.prototype.isInt=function(){
   if (this.match(/^\d+$/))
   {return true;}
   else
   {return false;}
}
String.prototype.isMail=function(){
   var reg=/^[_a-zA-Z0-9\-]+(\.[_a-zA-Z0-9\-]*)*@[a-zA-Z0-9\-]+([\.][a-zA-Z0-9\-]+)+$/g;
   if (this.match(reg))
   {return true;}
   else
   {return false;}
}
String.prototype.isPInteger=function(){
   var reg=/^[1-9]+[0-9]*$/;
   if (this.match(reg))
   {return true;}
   else
   {return false;}
}
String.prototype.isEndWidth=function(str){
   if (str==''||this.length==0){return false;}
   return (this.lastIndexOf(str)+str.length==this.length);
}

String.prototype.checkByteLength=function(){
	if (this == null) return 0;
	var l = this.length;
	var blen = 0;
	for(i=0; i<l; i++) 
	{
		if ((this.charCodeAt(i) & 0xff00) != 0) 
		{blen ++;		}
		blen ++;
	}
	return blen;
}
/*****数组扩展*********/
Array.prototype.search = function(value)  
{
  for(var i=0;i<this.length;i++)
  {
     if (this[i]===value)
     {return i;}
  }
  return -1;
}
Array.prototype.has = function(value)  
{
  for(var i=0;i<this.length;i++)
  {
     if (this[i]===value)
     {return true;}
  }
  return false;
}
Array.prototype.del=function(n) 
{ 
  if(n<0)
  {return this;  }
  else
  {return this.slice(0,n).concat(this.slice(n+1,this.length));  }
}
//************************Cookie*****************//
function setCookie(name, value) //domain=localhost;domain=NULL;
{document.cookie = name + '=' + escape(value) + (';path=/company/;expires='+  (new Date(2099,12,31)).toGMTString());}
function getCookie(name) {
   var search = name + '=';
   if(document.cookie.length > 0) 
   {
      offset = document.cookie.indexOf(search)
      if(offset != -1) 
      {
         offset += search.length;
         end = document.cookie.indexOf(";", offset);
         if(end == -1) end = document.cookie.length;
         return unescape(document.cookie.substring(offset, end));
      }      
      else
      {return '';}
   }
   else
   {return '';}
}
//****************************购物车系统*************************************//
//购物车类
var Cart=function(_cid,_pid,_sid,_number,_price,_price2,_exp01,_hasp2){
  this.Cid=_cid;//商家ID
  this.Id=_pid+'_'+_sid+'_'+_exp01;
  this.Pid=_pid;//产品ID//唯一//重复重计数量
  this.Sid=_sid;//备(颜色)
  this.Num=Number(_number);  
  this.HasVipPrice=(_hasp2=='Yes');
  this.Price=Number(_price);//单价
  if (!this.HasVipPrice)
  {this.VipPrice=this.Price;}
  else
  {this.VipPrice=Number(_price2);}
  this.Total=this.Price*this.Num;//该项总价
  this.VipTotal=this.VipPrice*this.Num;//会员总价
  this.Exp01=_exp01;//备(尺寸)
  this.getValue=function(){return _cid+'*'+_pid+'*'+_sid+'*'+this.Num+'*'+this.Price+'*'+this.VipPrice+'*'+this.Exp01;}
}


var Trade=new Object();
Trade.Item=new Array();//购物车实体类
//获取总记录数（条数）
Trade.count=function(){
   return this.Item.length;
}
//获取总金额数(不含运费)
Trade.total=function(){
   var t={p1:0,p2:0};
   for(var i=0;i<this.Item.length;i++)
   {
      t.p1 +=this.Item[i].Total;      
      t.p2 +=this.Item[i].VipTotal;
   }
   t.p1=t.p1.toFixed(2);
   t.p2=t.p2.toFixed(2);   
   return t;
}

//检测是否存在项，无:返回null 有：返回对象
Trade.hasOne=function(_id){
  for(var i=0;i<this.Item.length;i++)
  {
      if (this.Item[i].Id==_id)
      {return this.Item[i];}
  }
  return null;
}

//添加进入购物车
Trade.addItem=function(_cid,_pid,_sid,_number,_price,_price2,_exp01,_hasp2){
   var item=this.hasOne(_pid+'_'+_sid+'_'+_exp01);
   if (item==null)
   {this.Item.push(new Cart(_cid,_pid,_sid,_number,_price,_price2,_exp01,_hasp2));}
   else
   {
       item.Num+=_number;
       item.Price=_price;
       item.Total=item.Price*item.Num;  
       item.VipTotal=item.VipPrice*item.Num;    
   }    
   Trade.saveData();
}
//移除项
Trade.deleItem=function(_id){
   var d=-1;
   for(var i=0;i<this.Item.length;i++)
   {
      if (this.Item[i].Id==_id)
      {
        this.Item = this.Item.slice(0,i).concat(this.Item.slice(i+1,this.Item.length));
      }
   }
}
//保存Cookies
Trade.saveData=function(){
  var jg=new Array();  
  for(var i=0;i<this.Item.length;i++)
  {
     jg.push(Trade.Item[i].getValue());  
  }
  setCookie('CartList',jg.join('|'));  
}
//读取Cookies
Trade.readData=function(){
  var v=getCookie('CartList');
  if (v==''){return;}
  var list=v.split('|');
  this.Item=new Array();
  for(var i=0;i<list.length;i++)
  {
     var s=list[i].split('*');
     if (s.length!=7){continue;}
     this.Item.push(new Cart(s[0],s[1],s[2],s[3],s[4],s[5],s[6]));
  }  
}
Trade.readData();//初始化


//****************************功能类*************************************//
var JqLinCOM=new Object();
var Point = function (_x,_y){this.x = _x;this.y = _y;}
var JqLinCOM=new Object();
JqLinCOM.RandomCode=String((new Date()).getTime())+'_';
JqLinCOM.isIE=function(){if(document.all){return true;}else{return false;}}
JqLinCOM.getXY=function(obj){
    var a /*返回对象*/ = new Array();
    var t = obj.offsetTop;
    var l = obj.offsetLeft;
    var h = obj.offsetHeight;
    var w = obj.offsetWidth;
    while(obj=obj.offsetParent){
    t+=obj.offsetTop;
    l+=obj.offsetLeft;
    }
    a[0] = t;
    a[1] = l;
    a[2] = h;
    a[3] = w;
    return a;
}
//建立特定对象偏移位置的对象
JqLinCOM.creatDivBox = function(divid,obj,fixX,fixY,classname,divInnerHTML)
{
    var tempEditDiv=document.createElement('div');    
    tempEditDiv.id=divid;
    var x=this.getXY(obj);    
    var tempStyleStr='position:absolute;z-index:10000;top:'+(x[0]+fixY)+'px;left:'+(x[1]+fixX)+'px;';
    tempEditDiv.style.cssText=tempStyleStr;
    tempEditDiv.className=classname;
    if(divInnerHTML)
    {  tempEditDiv.innerHTML=divInnerHTML;    }
    document.body.appendChild(tempEditDiv);
}




JqLinCOM.appendListener=function(obj,handler,func)
{
    if (this.isIE())
    {obj.attachEvent('on'+handler,func);}
    else
    {obj.addEventListener(handler,func,false);} 
}

//clickToHide
JqLinCOM.clickToHide=new function()
{
    this.Name=new Array();  //[属性集,1属性集表示一事件]  
}
JqLinCOM.clickToHide.clickToHideId=function(attr,subobj,e)
{
    var currentnode=((window.event)? event.srcElement : e.target);
    var hidesubobj=true
    while (currentnode.tagName!="BODY" && currentnode.tagName!="HTML")
    {
      if (currentnode.getAttribute('jqcom.attributes')==attr)
      {hidesubobj=false;break;}
      currentnode=currentnode.parentNode;
    }
    if (hidesubobj&&$(subobj))
    {$(subobj).style.display='none';}
}

JqLinCOM.clickToHide.bind=function(id,attr)
{
   if (!this.Name.has(attr))
   {     
        JqLinCOM.appendListener(document,'click',function(event){JqLinCOM.clickToHide.clickToHideId(attr,id,event)});
        this.Name.push(attr);   
   }
}


JqLinCOM.addSelect=function(obj,cText,cValue){
    var oOption=document.createElement("OPTION");   
    oOption.text = cText;
    oOption.value = cValue;
    obj.options.add(oOption);
}
JqLinCOM.clearSelect=function(obj){
  for (var i=obj.options.length-1;i>=0;i--)
  {
    if (this.isIE())
    {obj.options.remove(i);  }
    else
    {$(obj.id).remove(i);}
  }
}





JqLinCOM.CI=new Object();
JqLinCOM.CI.PInteger=function(obj){
   var v=obj.value.Trim();
   if (!v.isPInteger())
   {obj.value=obj.getAttribute('oldvalue');}
   else
   {obj.setAttribute('oldvalue',v);}
} 
