window.onload = function() {
	var cart = new Cart();
	cart.refresh();
}

function Cart()	{
	
	this.label = document.getElementById( 'topcartLabel' );
	this.userEmail = document.getElementById( 'iEmail' );
	this.userPhone = document.getElementById( 'iPhone' );
	this.userFullName = document.getElementById( 'iFullName' );
	this.userCity = document.getElementById( 'sLocation' );
	this.deliveryAdress = document.getElementById( 'iDelivery' );
	
	this.expireTime = function ()	{
		var dt = new Date();
		dt.setTime ( dt.getTime() + (14 * 24 * 60 * 60 * 1000) ); // 2 weeks from now 
		return dt;
	}
	
	this.setUser = function( email, fullName, city, phone, delivery )	{ //debugger;
		var params;
		var cookuser = getCookie( "USER" );
		if( cookuser ) {
			params = cookuser.split(';');
			if( email )		params[0] = email;
			if( fullName )	params[1] = fullName;
			if( city )		params[2] = city;
			if( phone )		params[3] = phone;
			if( delivery )	params[4] = delivery;
		}
		else
			params = new Array( email, fullName, city, phone, delivery )
		var cookuser = params.join(';');
		setCookie( "USER", cookuser, this.expireTime(), "/" );
		/*var params = new Array( email, fullName, city );
		var cookuser = params.join(';');
		setCookie( "USER", cookuser, this.expireTime(), "/" );*/
	}
	
	this.add = function ( productId )	{
		var cookart = getCookie( "CART" );
		var allprod = cookart.split(';');
		var couple = [], q = 0, miss = true;
		for( var i = 0; i < allprod.length; i++ )	{
			couple = allprod[i].split('>');
			if( couple[0] == productId )	{
				couple[1]++;
				allprod[i] = couple.join( '>' );
				miss = false;
				break;
			}
		}
		cookart = allprod.join(';');		
		if( miss )	{
			if( cookart && cookart.length > 0 )
				cookart += ';';
			cookart += productId.toString() + '>1';
		}
		setCookie( "CART", cookart, this.expireTime(), "/" );
		this.refresh( cookart );
	}
	
	this.remove = function ( productId )	{
		var cookart = getCookie( "CART" );
		var allprod = cookart.split(';');
		var couple = [], q = 0, miss = true;
		for( var i = 0; i < allprod.length; i++ )	{
			couple = allprod[i].split('>');
			if( couple[0] == productId )	{
				couple[1] = couple[1] <= 1 ? 0 : couple[1]--;
				allprod[i] = couple.join( '>' );
				miss = false;
				break;
			}
		}
		cookart = allprod.join(';');		
		
		setCookie( "CART", cookart, this.expireTime(), "/" );
		this.refresh( cookart );
	}
	
	this.removeAll = function ( productId )	{
		var cookart = getCookie( "CART" );
		var allprod = cookart.split(';');
		var couple = [], q = 0, miss = true;
		for( var i = 0; i < allprod.length; i++ )	{
			couple = allprod[i].split('>');
			if( couple[0] == productId )	{
				couple[1] = 0;
				allprod[i] = couple.join( '>' );
				miss = false;
				break;
			}
		}
		cookart = allprod.join(';');		
		
		setCookie( "CART", cookart, this.expireTime(), "/" );
		this.refresh( cookart );
	}
	
	this.set = function ( productId, quantity )	{
		var cookart = getCookie( "CART" );
		var allprod = cookart.split(';');
		var couple = [], q = 0, miss = true;
		for( var i = 0; i < allprod.length; i++ )	{
			couple = allprod[i].split('>');
			if( couple[0] == productId )	{
				couple[1] = quantity;
				allprod[i] = couple.join( '>' );
				miss = false;
				break;
			}
		}
		cookart = allprod.join(';');		
		if( miss )	{
			if( cookart && cookart.length > 0 )
				cookart += ';';
			cookart += productId.toString() + '>' + quantity.toString();
		}
		
		setCookie( "CART", cookart, this.expireTime(), "/" );
		this.refresh( cookart );
	}
	
	this.readUserFromCookie = function () {
		var cookuser = getCookie( "USER" );
		if( cookuser )
			return cookuser.split(';');
		return null;
	}
	

	this.refresh = function ( cookart )	{
		var params;
		if( this.userEmail && this.userCity ) {//full
			params = this.readUserFromCookie();
			if( params ) {
//			var cookuser = getCookie( "USER" );
//			if( cookuser ) {
//				var params = cookuser.split(';');
//				if( params )	{
					this.userEmail.value = params[0];
					this.userFullName.value = params[1];
					if( params[2] != '' ){
						this.userCity.value = params[2];
						if( this.userCity.fireEvent )
							this.userCity.fireEvent("onchange");
						else
							this.userCity.onchange();
					}
					else //if null =>> add default value to coockie
						this.setUser( null, null, this.userCity.value, null, null );
					this.userPhone.value = params[3] ? params[3] : '';
					this.deliveryAdress.value = params[4] ? params[4] : '';
//				}
			}
			else //if cookuser doesn't exist =>> create it & add default city-value there
				this.setUser( null, null, this.userCity.value, null, null );
		}
		else {//debugger;
			params = this.readUserFromCookie();
			if( params ) {
				if( this.userEmail ) //user email
					this.userEmail.value = params[0];
				if( this.userFullName ) //user name
					this.userFullName.value = params[1];
			}
		}
		if( this.label ) {//cartinfo
			if( !cookart )
				cookart = getCookie( "CART" );
			//var cookart = getCookie( "CART" );
			var allprod = cookart.split(';');
			var q = 0;
			for( var i = 0; i < allprod.length; i++ )	{
				q += parseInt( allprod[i].split('>')[1] );
			}
			if( q > 0 )	{
				var prod;
				if( q == 1 )		prod = "товар";
				else if( q <= 4 )	prod = "товара";
				else				prod = "товаров";
				this.label.innerHTML = "Вы <a href='Cart/'>выбрали</a> " + q.toString() + "&nbsp;" + prod;
			}
			else
				this.label.innerHTML = "Вы&nbsp;пока&nbsp;ничего не&nbsp;заказали";
		}
	}
	
}

