﻿
j = jQuery.noConflict(); 
skidka_tekushchaja=0;

j().ready(function() {
	var statistic={};
	var current_date = new Date;
	//alert(document.location.href);
	
	if(document.location.href=='http://www.tricotage.s47.org.ua/index.php/component/jshopping/checkout/finish?Itemid='||document.location.href=='www.tricotage.s47.org.ua/index.php/component/jshopping/checkout/finish?Itemid='||document.location.href=='http://www.tricotage.com.ua/index.php/component/jshopping/checkout/finish?Itemid='||document.location.href=='www.tricotage.com.ua/index.php/component/jshopping/checkout/finish?Itemid='){finish();}
	
	//alert(get_cookie ( "activate_code"));
	//alert(get_cookie("login_sm")+'-'+get_cookie("pass_sm"))
load_sm(get_cookie("login_sm"), get_cookie("pass_sm"), '1');


 
    j('.head_stat_block_main').click(function(){
		if(j('.statistic_head_sm').is(":hidden")){j('.statistic_head_sm').slideDown('normal');}else{j('.statistic_head_sm').slideUp('normal')}
	}); 
	
	
    j('.about_title').click(function(){
		if(j('.status_about').is(":hidden")){j('.status_about').slideDown('normal');}else{j('.status_about').slideUp('normal')}
	});
 
    j('#period_button').click(function(){
		month = j('#choice_month').val();
		year = j('#choice_year').val();
		load_statistic(month, year);
	}); 
 
    j('#generation_cod').click(function(){
		new_code=getRandomInt(100000000000, 999999999999);	
		
		
		var current_date = new Date;
					var cookie_year = current_date.getFullYear ( );
					var cookie_month = current_date.getMonth ( )+1;
					var cookie_day = current_date.getDate ( );
					var hours = current_date.getHours()
					var minutes = current_date.getMinutes()
					var seconds = current_date.getSeconds()
					
					//set_cookie ( "activate_code", j('#activate_input').val(), cookie_year, cookie_month, cookie_day, '/');
		
		
			j.get('/set_marketing/php/insert_code.php',
		{
			id_user_cookie:get_cookie( "id_user_cookie" ),
			new_code:new_code,
			coupon_value:skidka_tekushchaja
		},	
		function(data){
			//alert(data);
			j('#generation_cod_input').val(new_code);
			
			j('#statistic_block_sm').prepend(statistic.tab_statistic_obj('', new_code, skidka_tekushchaja, '', '', cookie_year+'-'+cookie_month+'-'+cookie_day, '0', '', '', cookie_year+'-'+cookie_month+'-'+cookie_day+' '+hours+':'+minutes+':'+seconds, '', 'Пока нет заказа', ''));
				if(j('.stat_block_main').is(":hidden")){
					j('.stat_block_main').slideDown('normal');
			j('.status_empty').slideUp('normal');}
			
			
		});
	}); 


	statistic.tab_statistic_obj=function(coupon_id, coupon_code, coupon_value, used, coupon_start_date, coupon_expire_date, order, names, email, order_date, currency_code_iso, order_status_text, order_number){
		str='<div class="tab_statistic"><div class="td_statistic" id="order_date">'+order_date+'</div><div class="td_statistic" id="coupon_code">'+coupon_code+'</div><div class="td_statistic" id="coupon_value">'+tround(coupon_value,0)+'%'+'</div><div class="td_statistic" id="coupon_expire_date">'+coupon_expire_date+'</div><div class="td_statistic" id="order_number">'+order_number+'</div><div class="td_statistic" id="order">'+order+' '+currency_code_iso+'</div><div class="td_statistic" id="names">'+names+'</div><div class="td_statistic" id="email">'+email+'</div><div class="td_statistic" id="order_status_text">'+order_status_text+'</div></div>'
		return str;
}


	statistic.tab_little_statistic_obj=function(key, val){
		str='<div class="statistic_head_title_sm" id="st_tit_'+key+'">'+val+'</div><div class="statistic_head_val_sm"  id="st_here_n_'+key+'">0</div><div class="statistic_head_val_sm"  id="st_here_m_'+key+'">0</div>'
		return str;
}
	
	

	statistic.tab_skidki_obj=function(key, summa_ot, summa_do, skidka){
		str='<div class="statistic_skidki"  id="diapazon_'+key+'">от '+summa_ot+' до '+summa_do+' грн.</div><div class="statistic_head_val_sm_pr"  id="skidka_'+key+'"> '+skidka+' %</div>'
		return str;
	}


	statistic.tab_last_oborot_obj=function(key, last_oborot, skidka, vytorg_tek_m, zarobotok_tek_mes ){
		str='<div class="vytorg_title"  id="diapazon_'+key+'">Ваш выторг за последние 30 дней: 	</div><div class="vytorg">'+tround(last_oborot,2)+' грн.</div><div class="vytorg_title"  id="diapazon_'+key+'">Ваш процент скидок и заработка на сейчас: 	</div><div class="vytorg">'+skidka+' %.</div><div class="vytorg_title"  id="diapazon_'+key+'">Ваш выторг за указанный месяц: 	</div><div class="vytorg">'+tround(vytorg_tek_m,2)+' грн.</div><div class="vytorg_title"  id="diapazon_'+key+'">Ваш заработок за указанный месяц: 	</div><div class="vytorg">'+tround(zarobotok_tek_mes,2)+' грн.</div>'
		
		return str;
	}


	function load_procent_analiz(vytorg_tek_m){
 	j.get('/set_marketing/php/load_procent_analiz.php',
		{id_user_cookie:get_cookie( "id_user_cookie" )},	
		function(data){
			//alert(data);
			json_obj = jsonParse(data);
			last_oborot=0;
			j('#statistic_head_top_bottom').html('');
			for (var key in json_obj) {
				val = json_obj [key];
			} 
			
			if(val.skidka!=null){skidka_v=val.skidka}else{skidka_v=0;}
			zarobotok_tek_mes=vytorg_tek_m*val.skidka/100;
			skidka_tekushchaja=val.skidka;
		j('#statistic_head_top_bottom').append(statistic.tab_last_oborot_obj(key, val.order_last_month , skidka_v, vytorg_tek_m, zarobotok_tek_mes ));
		
		}); 
	}
	
	
	function load_skidki(){
	
	
	j.get('/set_marketing/php/load_skidki.php',
		{},	
		function(data){
			json_obj = jsonParse(data);
					
			for (var key in json_obj) {
				val = json_obj [key];
				j('#statistic_head_block_sm_skidki').append(statistic.tab_skidki_obj(key, val.summa_ot, val.summa_do, val.skidka ));
			} 
		});
	}
	
	
	
	
	function load_little_statistic(){
	j.get('/set_marketing/php/load_little_statistic.php',
		{},	
		function(data){
			json_obj = jsonParse(data);
			for (var key in json_obj) {
				val = json_obj [key];
				j('#statistic_head_block_sm_left').prepend(statistic.tab_little_statistic_obj(key, val));
			} 
					var current_date = new Date;
					var cookie_year = current_date.getFullYear ( );
					var cookie_month = current_date.getMonth ( )+1;
					
					j('#choice_month').val(cookie_month);
					j('#choice_year').val(cookie_year);
		
			load_statistic(cookie_month, cookie_year);
			
		});
	}

	
	function load_statistic(month, year){
	j.get('/set_marketing/php/load_statistic.php',
		{
			id_user_cookie:get_cookie( "id_user_cookie" ),
			month:month,
			year:year
		},	
		function(data){
		
			json_obj = jsonParse(data);
			
			j('#statistic_block_sm').html('');
			j('.statistic_head_val_sm').html('0');
			
			if(json_obj!=''){
			
			j('.stat_block_main').show();
			j('.status_empty').hide();
			vytorg_tek_m=0;
			for (var key in json_obj) {
				val = json_obj [key];
					
				
					
				order=val.order_total-val.order_shipping-val.order_payment;
				names=val.f_name+' '+val.l_name;
				
				
				ord=j('#st_here_m_'+val.order_status).html()*1+order;
				j('#st_here_m_'+val.order_status).html(ord);
				num=j('#st_here_n_'+val.order_status).html()*1+1;
				j('#st_here_n_'+val.order_status).html(num);
				
				if(val.order_status='7'){vytorg_tek_m=ord;}
				
				if(val.used==0){order_status_text='Пока нет заказа'}else {order_status_text=val.order_status_text}
				
				j('#statistic_block_sm').prepend(statistic.tab_statistic_obj(val.coupon_id, val.coupon_code, val.coupon_value, val.used, val.coupon_start_date, val.coupon_start_date, order, names, val.email, val.order_date, val.currency_code_iso, order_status_text, val.order_number));
			}
			
			load_procent_analiz(vytorg_tek_m);
		}else{load_procent_analiz(0);j('.stat_block_main').hide();j('.status_empty').show();}
		});
	}

	
	
/*     j('#finish_ord').mousedown(function(){
		
	get_activate_code=get_cookie ( "activate_code");
	
		if(j('#agb').is(":checked")){
			j.get('/set_marketing/php/insert_activation.php',
				{	coupon_code:get_activate_code},	
				function(data){
				//alert(data);
					delete_cookie ( 'activate_code','/')
				}); 
				//delete_cookie ( 'activate_code','/')
		}						
	});	 */
	
	
	function finish(){
	
	get_activate_code=get_cookie ( "activate_code");
	//alert(get_activate_code);
		//if(j('#agb').is(":checked")){
			j.get('/set_marketing/php/insert_activation.php',
				{	coupon_code:get_activate_code},	
				function(data){
				//alert(data);
					delete_cookie ( 'activate_code','/')
				}); 
				//delete_cookie ( 'activate_code','/')
		//}		
	
	
	
	}
	
	
	
	
	
    j('#activate_button').mousedown(function(){
		
		
		var current_date = new Date;
					var cookie_year = current_date.getFullYear ( ) + 1;
					var cookie_month = current_date.getMonth ( );
					var cookie_day = current_date.getDate ( );

					set_cookie ( "activate_code", j('#activate_input').val(), cookie_year, cookie_month, cookie_day, '/');
		
		
		j.get('/set_marketing/php/insert_activation_details.php',
			{	coupon_code:j('#activate_input').val()
			},	
			function(data){
		//alert(get_cookie ( "activate_code"));
			});
										
	}); 
	
		
    j('#logout_sm').mousedown(function(){
	

					delete_cookie('login_sm','/');
					delete_cookie('pass_sm','/');
					delete_cookie('id_user_cookie','/');
			//alert();			
										
	}); 
	
	
	
	function getRandomInt(min, max){
		return Math.floor(Math.random() * (max - min + 1)) + min;
	}
	
    j('#login_b').mousedown(function(){
					
					//alert();
					
					
					var current_date = new Date;
					var cookie_year = current_date.getFullYear ( ) + 1;
					var cookie_month = current_date.getMonth ( );
					var cookie_day = current_date.getDate ( );

					set_cookie ( "login_sm", j('#modlgn_username').val(), cookie_year, cookie_month, cookie_day, '/' );
					set_cookie ( "pass_sm", j('#modlgn_passwd').val(), cookie_year, cookie_month, cookie_day, '/'); 
					
					
	});

	
	
/* 		$('#modlgn_username').live('keydown', function(e) 
{
     if(e.keyCode==13)
     {
          var current_date = new Date;
					var cookie_year = current_date.getFullYear ( ) + 1;
					var cookie_month = current_date.getMonth ( );
					var cookie_day = current_date.getDate ( );

					set_cookie ( "login_sm", j('#modlgn_username').val(), cookie_year, cookie_month, cookie_day, '/' );
					set_cookie ( "pass_sm", j('#modlgn_passwd').val(), cookie_year, cookie_month, cookie_day, '/'); 
					
     }
}); */
	
	
	
	    j('#login_second').mousedown(function(){
					
					var current_date = new Date;
					var cookie_year = current_date.getFullYear ( ) + 1;
					var cookie_month = current_date.getMonth ( );
					var cookie_day = current_date.getDate ( );

					set_cookie ( "login_sm", j('#username').val(), cookie_year, cookie_month, cookie_day, '/' );
					set_cookie ( "pass_sm", j('#password').val(), cookie_year, cookie_month, cookie_day, '/'); 
	});

		    j('#login_second_2').mousedown(function(){
					//alert();
					var current_date = new Date;
					var cookie_year = current_date.getFullYear ( ) + 1;
					var cookie_month = current_date.getMonth ( );
					var cookie_day = current_date.getDate ( );
					//alert(j('#username_s').val()+'-'+j('#passwd_s').val());
					set_cookie ( "login_sm", j('#username_s').val(), cookie_year, cookie_month, cookie_day, '/' );
					set_cookie ( "pass_sm", j('#passwd_s').val(), cookie_year, cookie_month, cookie_day, '/'); 
	});

	
	
	//j('.jshop .button').click(function(){
	j('#button_ins').mousedown(function(){
		//alert();
		user_pol=j('#title').val();
		user_name=j('#f_name').val();
		user_lname=j('#l_name').val();
		user_firma=j('#firma_name').val();
		user_ulica_dom=j('#street').val();
		user_index=j('#zip').val();
		user_gorod=j('#city').val();
		user_oblast=j('#state').val();
		user_strana=j('#country').val();
		user_tel=j('#phone').val();
		user_fax=j('#fax').val();
		user_pass=j('#password').val();
		user_pass2=j('#password_2').val();
		email=j('#email').val();
		username=j('#u_name').val();
		user_firma=j('#firma_name').val();
	
		if((user_pol!=''&&user_name!=''&&user_lname!=''&&user_ulica_dom!=''&&user_index!=''&&user_gorod!=''&&user_strana!=''&&user_tel!=''&&user_pass!=''&&email!='')&&user_pass==user_pass2){
		
			j.get('/set_marketing/php/insert_user_details.php',
		{
			user_pol:user_pol,
			user_name:user_name,
			user_lname:user_lname,
			user_ulica_dom:user_ulica_dom,
			user_index:user_index,
			user_gorod:user_gorod,
			user_oblast:user_oblast,
			user_strana:user_strana,
			user_tel:user_tel,
			user_pass:user_pass,
			email:email,
			username:username,
			user_firma:user_firma

		},	
		function(data){
		//alert(data);
		});
		}
  });

   j('#button_log_sm').click(function(){	
		username=j('#login_log_sm').val();
		user_pass=j('#pass_log_sm').val();
	
		load_sm(username, user_pass, '2');
   
   
   });
  
  

  function load_sm(log, pass, load){
  //alert(log+'-'+pass)

		j.get('/set_marketing/php/login_sm.php',
		{	user_name:log,
			user_pass:pass
		},	
		function(data){
			//alert(data);
				d=data.slice(2);
			
				if(d=='no1'){if(load!='1'){alert('Вы ввели не всю информацию, вернитесь назад и заполните все поля!');}}else{
		
				if(d!='no'){
							j('.statistic_head_date').show();
							j('.content_sm').show();
							j('.status_about').slideUp('normal');
					var current_date = new Date;
					var cookie_year = current_date.getFullYear ( ) + 1;
					var cookie_month = current_date.getMonth ( );
					var cookie_day = current_date.getDate ( );
					
						json_obj = jsonParse(d);
					
						for (var key in json_obj) {
							val = json_obj [key];
						}
					
					set_cookie ( "login_sm", val.user_name, cookie_year, cookie_month, cookie_day, '/');
					set_cookie ( "pass_sm", val.user_pass, cookie_year, cookie_month, cookie_day, '/'); 
					set_cookie ( "id_user_cookie", key, cookie_year, cookie_month, cookie_day, '/');
				
					login_sm = get_cookie ( "login_sm" );
					pass_sm = get_cookie ( "pass_sm" );
					id_user_cookie = get_cookie ( "id_user_cookie" );
					
					j('#congratulation_sm').html('Здравствуйте, '+val.f_name+' '+val.l_name+', Вы удачно вошли в систему.');
					j('.head_sm').slideUp('normal');
					j('.head_hello_sm').slideDown('normal');
					j('.code_sm').slideDown('normal');
					j('.statistic_sm').slideDown('normal');
					
					
					
					
					
					load_skidki();
					load_little_statistic();
					
					
					
					
				}else{if(load!='1'){alert('Извините, введённый вами логин или пароль неверный!');}
						else{
							j('.status_about').slideDown('normal');

							
						}
					}
			}
		
		});
	
  
  }
  
  
  
  
  
  
  //////////////////////////////////cookies f-iji///////////////////////////////////////////////

//zapis cookies f-ija

function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{	 var cookie_string = name + "=" + escape ( value );
 
  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }
 
  if ( path )
        cookie_string += "; path=" + escape ( path );
 
  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}


////poluchenije znach cookies f-ija

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );
 
  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}

///////udalenie
 
function delete_cookie( name, path, domain ) {
if ( get_cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}




function tround(num, dig) {
    if (!isNaN(num) && num != "") {
        if (!isNaN(dig) && dig >= 1) {
            return parseFloat(num).toFixed(dig);
        }
        else {
            return parseFloat(num).toFixed();
        }
    }
    else {
        return "0";
    }
}
  


  
});



