jQuery(document).ready(function() { 
  jQuery('.ShowSignIn').click(function(e) {
	 
	e.preventDefault();
	jQuery('#popup_signin').modal( { containerCss:{width:'370px',height:'300px',top:'22px',left:'350px',padding:'20px 23px 10px 25px',border:'#cccccc 1px solid' }, overlayClose:true});

  });
});

//document.onkeypress=keyPr; //function if press key anywhere in the window

/*
if (navigator.appName == 'Netscape') {
	window.captureEvents(Event.KEYPRESS);
	window.onKeyPress = keyPr;
}
*/

function html_remind_pass( ){
  html1='<u>Забыли пароль?</u> Пожалуйста, введите Ваш логин или адрес электронной почты:<input type="text" name="login_to_remind_password" value="" onKeyPress ="if (event.keyCode == 13) remind_pass();"/><input type="submit" value="OK" onclick="javascript:remind_pass();" />';
  jQuery.modal( html1 , { closeHTML:"<a title='Close' class='modalCloseX simplemodal-close' style='cursor:pointer;'><img src='"+HTTP_HOST+"images/x.png'></a>", containerCss:{ backgroundColor:"#fff", borderColor:"#fff", height:200, padding:0, width:250 }, overlayClose:true}); 
}

function remind_pass( ){
  varPOST = {};
  varPOST['remind_password'] = jQuery('input[name=login_to_remind_password]').attr("value");
  varPOST['sess_id'] = sess_id;
  
  jQuery.ajax({
//    async:false,
	type: 'post',
	dataType: 'json',
	url: HTTP_HOST_S+'mine/api_smarty.php?json=yes&callback=?',
	data: varPOST,
	success: function(AllData){
	  jQuery.modal.close();
	  jQuery.modal( AllData['remind_password'] , { closeHTML:"<a title='Close' class='modalCloseImg' style='cursor:pointer;'><img src='"+HTTP_HOST+"images/x.png'></a>", containerCss:{ backgroundColor:"#fff", borderColor:"#fff", height:200, padding:0, width:250 }, overlayClose:true}); 
	}
  });
}

function login( type ) {

  varPOST = {};
  varPOST['enter_login'] = 1;
  varPOST['productID'] = jQuery('input[name=productID]').attr("value");
  varPOST['categoryID'] = jQuery('input[name=categoryID]').attr("value");
  varPOST['login'] = jQuery('input[name=login_'+type+']').attr("value");
  varPOST['pass'] = jQuery('input[name=pass_'+type+']').attr("value");
  varPOST['sess_id'] = sess_id;
  if( jQuery('input[name=cook_save_'+type+']').length!=0 ){
	if(jQuery('input[name=cook_save_'+type+']').attr('checked')==false) varPOST['cook_save'] = 1; // если input type=checkbox не выделен, значит сохраняем куки
	else varPOST['cook_save'] = 0; // если input type=checkbox выделен, значит НЕ сохраняем куки
  }else varPOST['cook_save'] = 0; // если нет input type=checkbox, значит не сохраняем куки
  //  $cook_save=(isset($_POST['cook_save']))?true:false; // если есть - сохраняем куки
  
  jQuery.ajax({
//   async:false,
	type: 'post',
	dataType: 'json',
	url: HTTP_HOST_S+'mine/api_smarty.php?json=yes&callback=?',
	data: varPOST,
	beforeSend: function(a){
	  showModal('Вход на сайт под Вашим логином...', 130, 100, 5000);
	},
	success: function(AllData){
	  //alert(AllData['enter_login']);
	  if( AllData['enter_login']=='no') m_text='Неверный логин или пароль!';
	  else if( AllData['enter_login'] == 'ok' ) m_text='Пара логин-пароль верна.';
	  else if( AllData['enter_login'] == 'ok_cookies_saved' ) m_text='Пара логин-пароль верна. Пароль сохранен.';
	  else if( AllData['enter_login'] == 'not_cookies_saved' ){
		m_text='Пара логин-пароль верна. Пароль сохранен.';
		jQuery.cookie("login", varPOST['login'], { expires: 30, path: "/", secure: false });
		pass_cook = Base64.encode(varPOST['pass']);
		jQuery.cookie("password", pass_cook, { expires: 30, path: "/", secure: false });
	  } else if( AllData['enter_login'] == 'REGCONFIRMATION' ) m_text='Учетная запись не активированна.';
	  else m_text='Ответ сарвера не определен. Обратитесь к разработчику портала.';
	  
	  showModal(m_text, 130, 100, 5000);
	  
	  setTimeout( function() {
		if( AllData['enter_login'] == 'ok' || AllData['enter_login'] == 'ok_cookies_saved' || AllData['enter_login'] == 'not_cookies_saved' || AllData['enter_login'] == 'no' )
		  redirectPage();
		if( AllData['enter_login'] == 'REGCONFIRMATION' ){
		  redirectPage( HTTP_HOST_S+'activation/'+jQuery('input[name=login_'+type+']').attr("value") );
		} else {
		  jQuery.modal.close();
		}
	  },5000); // half seconds
	}
  });

  if(location.host == 'fege.it' || location.host == 'glamstyle.biz' || location.host == 'www.fege.it' || location.host == 'www.glamstyle.biz'){  // http:// проверка fege.com.ua
	redirectPage( 'http://fege.com.ua/?sess_id='+sess_id );
  }
}

function logout() {

  jQuery.cookie('login', null, { path: '/', expires: -1 }); // удалить cookie
  jQuery.cookie('password', null, { path: '/', expires: -1 }); // удалить cookie

  varPOST = {};
  varPOST['logout'] = 'yes';
  varPOST['sess_id'] = sess_id;
  
  jQuery.ajax({
//	async:false,
	type: 'post',
	dataType: 'json',
	url: HTTP_HOST_S+'mine/api_smarty.php?json=yes&callback=?',
	data: varPOST,
	beforeSend: function(a){
	  showModal('Выход из Вашей учетной записи...', 130, 100, 0);
	},
	success: function(data){
	  redirectPage();
	}
  });
}

function shop_cart_info_upd_html(data){ // обновляет shopping_cart_info data=json

  if(data.shopping_cart_items==0){ // сменить на - корзина пустая
	if( jQuery("#shop_cart_filled").css("display")=="block") jQuery("#shop_cart_filled").css("display","none");
	if( jQuery("#shop_cart_empty").css("display")=="none") jQuery("#shop_cart_empty").css("display","block");
  }else{
	if( jQuery("#shop_cart_filled").css("display")=="none") jQuery("#shop_cart_filled").css("display","block");
	if( jQuery("#shop_cart_empty").css("display")=="block") jQuery("#shop_cart_empty").css("display","none");
	
	jQuery("#shopping_cart_items").html( data.shopping_cart_items );
	jQuery("#shopping_cart_value_shown").html( data.shopping_cart_value_shown );
	
	blinkit( 'shopping_cart_items', 3, 200 );
	blinkit( 'shopping_cart_value_shown', 3, 200 );
  }
}

function blinkit(id, count, intr){
  intrvl=0;
  for(nTimes=0;nTimes<count;nTimes++){
	intrvl += intr;
	setTimeout("document.getElementById('"+id+"').style.color = 'red';",intrvl);
	intrvl += intr;
	setTimeout("document.getElementById('"+id+"').style.color = 'black';",intrvl);
  }
}

/*cookies*/
/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

  // private property
  _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

  // public method for encoding
  encode : function (input) {
	var output = "";
	var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
	var i = 0;

	input = Base64._utf8_encode(input);

	while (i < input.length) {

	  chr1 = input.charCodeAt(i++);
	  chr2 = input.charCodeAt(i++);
	  chr3 = input.charCodeAt(i++);

	  enc1 = chr1 >> 2;
	  enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	  enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	  enc4 = chr3 & 63;

	  if (isNaN(chr2)) {
		enc3 = enc4 = 64;
	  } else if (isNaN(chr3)) {
		enc4 = 64;
	  }

	  output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

	}

	return output;
  },

  // public method for decoding
  decode : function (input) {
	var output = "";
	var chr1, chr2, chr3;
	var enc1, enc2, enc3, enc4;
	var i = 0;

	input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

	while (i < input.length) {

	  enc1 = this._keyStr.indexOf(input.charAt(i++));
	  enc2 = this._keyStr.indexOf(input.charAt(i++));
	  enc3 = this._keyStr.indexOf(input.charAt(i++));
	  enc4 = this._keyStr.indexOf(input.charAt(i++));

	  chr1 = (enc1 << 2) | (enc2 >> 4);
	  chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
	  chr3 = ((enc3 & 3) << 6) | enc4;

	  output = output + String.fromCharCode(chr1);

	  if (enc3 != 64) {
		output = output + String.fromCharCode(chr2);
	  }
	  if (enc4 != 64) {
		output = output + String.fromCharCode(chr3);
	  }

	}

	output = Base64._utf8_decode(output);

	return output;

  },

  // private method for UTF-8 encoding
  _utf8_encode : function (string) {
	string = string.replace(/\r\n/g,"\n");
	var utftext = "";

	for (var n = 0; n < string.length; n++) {

	  var c = string.charCodeAt(n);

	  if (c < 128) {
		utftext += String.fromCharCode(c);
	  }
	  else if((c > 127) && (c < 2048)) {
		  utftext += String.fromCharCode((c >> 6) | 192);
		utftext += String.fromCharCode((c & 63) | 128);
	  }
	  else {
		utftext += String.fromCharCode((c >> 12) | 224);
		utftext += String.fromCharCode(((c >> 6) & 63) | 128);
		utftext += String.fromCharCode((c & 63) | 128);
	  }

	}

	return utftext;
  },

  // private method for UTF-8 decoding
  _utf8_decode : function (utftext) {
	var string = "";
	var i = 0;
	var c = c1 = c2 = 0;

	while ( i < utftext.length ) {

	  c = utftext.charCodeAt(i);

	  if (c < 128) {
		string += String.fromCharCode(c);
		i++;
	  }
	  else if((c > 191) && (c < 224)) {
		c2 = utftext.charCodeAt(i+1);
		string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
		i += 2;
	  }
	  else {
		c2 = utftext.charCodeAt(i+1);
		c3 = utftext.charCodeAt(i+2);
		string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
		i += 3;
	  }

	}

	return string;
  }

}
/*cookies*/
