function cookie(){
this.setcookie=function(name, value, hours){
var expire = "";
if(hours != null){
expire = new Date((new Date()).getTime() + hours * 3600000);
expire = "; expires=" + expire.toGMTString();
}
document.cookie = escape(name) + "=" + escape(value) + expire;
}
this.getcookie=function(name){
var cookievalue = "";
var search = escape(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;
cookievalue = unescape(document.cookie.substring(offset, end))
}
}
return cookievalue;
}
}
function Car(name){
if( !window.clientInformation.cookieEnabled ) {
alert('你的浏览器不支持cookie无法使用此 购物车 系统');
return false;
}
//##内部变量#############################################################
this.carName = name;
this.expire = 24*30; //购物车的有效时间(30天)
this.carDatas = new Array();
this.cookie = new cookie();
//##内部对象#############################################################
this.typeObj=function(name,value){ //自带的 类别 对象
this.name =name;
this.value="/value;
}
this.proObj=function(name,value){ //自带的" 商品 对象
this.name =name;
this.value=value;
}
//##私有方法列表##########################################################
//
// getTypePoint(typeName); //得到购物车里类别数组里的下标
// getProPoint(typeName,proName); //得到购物车里类别下的产品下标
// savecookie() //以特定的形式存储此购物车的cookie
//
//########################################################################
this.getTypePoint=function(typeName){
var isok=false;
var i=0;
for(;i
isok=true; //找到位置
break;
}
}
if(isok) return i;
else return -1;
}
this.getProPoint=function(typeId,proName){
var isok=false;
var j = 0;
var tempProObj=this.carDatas[typeId].value;
for(;j
isok=true;
break;
}
}
if(isok) return j;
else return -1;
}
this.savecookie=function(){
var outStr='';
for( i=0; i
var typevalue=this.carDatas[i].value;
var proOutStr='';
for( j=0; j
else proOutStr += '|' + typevalue[j].name + ':' + typevalue[j].value;
}
if ( i==0 ) outStr = typeName + '#' + proOutStr;
else outStr += ',' + typeName + '#' + proOutStr;
}
this.cookie.setcookie(this.carName,outStr,this.expire); //存入 cookie
}
//##构造语句############################################################
if(this.cookie.getcookie(name)==''){
this.cookie.setcookie(name,'',this.expire);
}else{
var tempTypes=this.cookie.getcookie(name).split(',');
for(i=0;i
var type_pro=new Array();
if(tempTypeObj[1]){
var tempProObj=tempTypeObj[1].split('|');
for(j=0;j
type_pro.push(new this.proObj(proDesc[0],proDesc[1]));
}
}
this.carDatas.push(new this.typeObj(tempTypeObj[0],type_pro));
}
}
//##公共方法列表#########################################################
//
// addType(typeName); //增加一个类别
// addPro(typeName,proName,value); //增加一个产品
// editPro(typeName,proName,value); //修改产品的值
// delPro(typeName,proName); //删除购物车内的一个类别下的产品
// delType(typeName); //删除购物车内的一个类别,包括类别下的产品
// delCar(); //删除购物车
//
// getCar(); //得到整个购物车的数据
// getType(); //得到购物车内的所有类别列表
// getPro(typeName); //得到购物车内指定类别下的产品列表
// getProVal(typeName,proName); //得到购物车内指定类别下的产品属性
//
//########################################################################
this.addType=function(typeName){
if(this.getTypePoint(typeName)!=-1) return false; //如果已经有此类别了,返回假
this.carDatas.push(new this.typeObj(typeName,new Array())); //push进 自身数组
this.savecookie(); //存入 cookie
return true;
}
this.addPro=function(typeName,proName,value){
var typePoint=this.getTypePoint(typeName); if ( typePoint ==-1 ) return false; //没有此类别,无法增加,返回假
var proPoint =this.getProPoint(typePoint,proName); if ( proPoint != -1 ) return false; //有此产品了,无法增加重复,返回假
this.carDatas[typePoint].value.push(new this.proObj(proName,value)); //push到自身数组
this.savecookie(); //存入 cookie
return true;
}
this.editPro=function(typeName,proName,value){
var typePoint=this.getTypePoint(typeName); if ( typePoint == -1 ) return false; //没有此类别,无法修改,返回假
var proPoint =this.getProPoint(typePoint,proName); if ( proPoint == -1 ) return false; //没有此产品,无法修改,返回假
this.carDatas[typePoint].value[proPoint].value=value; //更新自身
this.savecookie(); //存入 cookie
return true;
}
this.delPro=function(typeName,proName){
var typePoint=this.getTypePoint(typeName); if ( typePoint == -1 ) return false; //没有此类别,无法删除,返回假
var proPoint =this.getProPoint(typePoint,proName); if ( proPoint == -1 ) return false; //没有此产品,无法删除,返回假
var pros=this.carDatas[typePoint].value.length;
this.carDatas[typePoint].value[proPoint] = this.carDatas[typePoint].value[pros-1]; //最后一个产品放置要删除的产品上
this.carDatas[typePoint].value.pop();
this.savecookie(); //存入 cookie
return true;
}
this.delType=function(typeName){
var typePoint=this.getTypePoint(typeName); if ( typePoint == -1 ) return false; //没有此类别,无法删除,返回假
var types=this.carDatas.length;
this.carDatas[typePoint] = this.carDatas[types-1]; //删除类别
this.carDatas.pop();
this.savecookie(); //存入 cookie
return true;
}
this.delCar=function(){
this.cookie.setcookie(this.carName,'',0);
this.carDatas=new Array();
this.savecookie(); //存入 cookie
}
this.getCar=function(){
return this.carDatas;
}
this.getType=function(){
var returnarr=new Array();
for ( i=0; i
}
this.getPro=function(typeName){
var typePoint=this.getTypePoint(typeName); if ( typePoint == -1 ) return false; //没有此类别,返回假
return this.carDatas[typePoint].value;
}
this.getProVal=function(typeName,proName){
var typePoint=this.getTypePoint(typeName); if ( typePoint == -1 ) return false; //没有此类别,返回假
var proPoint =this.getProPoint(typePoint,proName); if ( proPoint == -1 ) return false; //没有此产品,返回假
return this.carDatas[typePoint].value[proPoint].value;
}
}



