﻿
function getCookie( field ){
	var strCookie=document.cookie;
	var arrCookie=strCookie.split("; ");
	var value;
	for(var i=0;i<arrCookie.length;i++){
		 var arr=arrCookie[i].split("=");
		 if(field==arr[0]){
			value=arr[1];
			break;
		 }
	}
	return value;
}

function msg( obj, info ){
	jQuery('#'+obj).show().html(info);
}

function _msg( obj, info, time ){
	jQuery('#'+obj).show().html(info);
	window.setTimeout("jQuery('#"+obj+"').hide()",time);
}

function tips( obj, src ){
	jQuery('#'+obj).show().html('<img src="'+src+'">');
}

/* add to cart */
/*
id: product id
qyt: quantity
fresh: false 不刷新， true 刷新 ,sub 
*/
function addCart( id , qyt , fresh ){
	if( id == '' ){return ;}
	var t;
	if( fresh == true ){	//cart page
		if( qyt > 0 ){	// +
			t	= 'Add success ';
		}else if( qyt < 1 ) {
			t	= 'Updata success ';
		}
	}else{	//other page
		if( qyt > 0 ){	// +
			t	= 'Add to cart success ';
		}
		t	+= ' <a href="javascript:;" onclick="window.location.href=\'/Cart/\'" >View Cart</a>'
	}
	
	jQuery.ajax({
		url:'/Cart/add/',
		data:{id:id,qyt:qyt},
		type:'post',
		beforeSend:function(){},
		success:function(data){
			if(1==data){
				artDialog({
					id:'cartMsg'+id, 
					title:'Tips',
					content:t,
					left:'right',
					width:'15em',
					top:'bottom',
					time:'3',
					fixed:true
				});
				if(fresh == true)setTimeout("window.location.href=location.href;",1000);
				else{ setTimeout("window.location.href='/Cart/';",1000); }
			}else{
				artDialog({	title:'Tips',content:data,time:2});return false;
			}
		},
		error:function(){artDialog({title:'Error',content:'System is busy now, please try later',time:3});return false;}
	});
}

//add to db
function saveToCart( id ){
	artDialog({content:'Save your cart for next time you can see ?',  yesText:'Sure',noText:'Cancel', style:'confirm'}, function(){
		jQuery.ajax({
			url:'/Cart/addToDb/',
			data:{id:id},
			type:'post',
			beforeSend:function(){},
			success:function(data){
				if(0==data){
					artDialog({	id:'loginCfm',title:'Tips',content:'Sorry, Only web menber can save cart, do you want to login now ?', fixed:true,yesText:'Yes', style:'confirm'}, function(){window.location.href='/User/';});
				}else if(1==data){
					artDialog({	id:'cartMsg'+id, title:'Tips',content:'Save success! You can ^&$^%^*&*(&*#$....',left:'right',width:'15em',top:'bottom',time:'3',fixed:true});
				}else{
					artDialog({	title:'Tips',content:data,time:3});return false;
				}
			},
			error:function(){}
		});
	}, function(){});
}



