function disableEnterKey(e)
{
 var key;

 if(window.event)
 key = window.event.keyCode; //IE
 else
 key = e.which; //firefox

 if(key == 13)
 return false;
 else
 return true;
}


function function_exists( function_name ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Steve Clay
	// +   improved by: Legaev Andrey
	// *     example 1: function_exists('isFinite');
	// *     returns 1: true


	if (typeof function_name == 'string'){
		return (typeof window[function_name] == 'function');
	} else{
		return (function_name instanceof Function);
	}
}

if (!function_exists('btoa')) {
	function btoa( data ) {
		var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

		do { // pack three octets into four hexets
			o1 = data.charCodeAt(i++);
			o2 = data.charCodeAt(i++);
			o3 = data.charCodeAt(i++);

			bits = o1<<16 | o2<<8 | o3;

			h1 = bits>>18 & 0x3f;
			h2 = bits>>12 & 0x3f;
			h3 = bits>>6 & 0x3f;
			h4 = bits & 0x3f;

			// use hexets to index into b64, and append result to encoded string
			enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
		} while (i < data.length);

		switch( data.length % 3 ){
			case 1:
			enc = enc.slice(0, -2) + '==';
			break;
			case 2:
			enc = enc.slice(0, -1) + '=';
			break;
		}

		return enc;
	}
}

if (!function_exists('atob')) {
	function atob( data ) {
		var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
		var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';

		do {  // unpack four hexets into three octets using index points in b64
			h1 = b64.indexOf(data.charAt(i++));
			h2 = b64.indexOf(data.charAt(i++));
			h3 = b64.indexOf(data.charAt(i++));
			h4 = b64.indexOf(data.charAt(i++));

			bits = h1<<18 | h2<<12 | h3<<6 | h4;

			o1 = bits>>16 & 0xff;
			o2 = bits>>8 & 0xff;
			o3 = bits & 0xff;

			if (h3 == 64)      enc += String.fromCharCode(o1);
			else if (h4 == 64) enc += String.fromCharCode(o1, o2);
			else               enc += String.fromCharCode(o1, o2, o3);
		} while (i < data.length);

		return enc;
	}
}

//////


function addToCart()
{
	$('#result').text('...');
	$('#add_to_cart').ajaxSubmit(
	 {
		 success: function(t)
		 {
		 	$('#result').text(t);		 	
		 }
	 });
	 $('#short_cart_info').load('menu_cart_info.php');
	 return false;
}

function addSceneToCart(path, id, place)
{
	$('#result-'+id).load(path + 'cart_deamon.php?submit=ADD_TO_CART&type=Scene&id='+id+'&place='+place, function(){
	$('#cb_to_cart_'+id).attr('checked', false);
	$('#cb_to_cart_'+id).attr('disabled', 'disabled');
	$('#short_cart_info').load(path + 'cart_deamon.php?submit=MENU_CART_INFO');
	});
	return false;
}

function addDvdToCart(path, id, place)
{
	$('#result-'+id).load(path + 'cart_deamon.php?submit=ADD_TO_CART&type=DVD&id='+id+'&place='+place, function(){
	$('#cb_to_cart_'+id).attr('checked', false);
	$('#cb_to_cart_'+id).attr('disabled', 'disabled');
	$('#short_cart_info').load(path + 'cart_deamon.php?submit=MENU_CART_INFO');
	});
	return false;
}

function addPackageToCart(path, id, place)
{
	$('#result-'+id).load(path + 'cart_deamon.php?submit=ADD_TO_CART&type=Package&id='+id+'&place=list', function(){
	$('#short_cart_info').load(path + 'cart_deamon.php?submit=MENU_CART_INFO');
	});
	return false;
}

function checkAllItems()
{
	$(".cb_to_cart:enabled").attr('checked', true);
	return false;
}

function uncheckAllItems()
{
	$(".cb_to_cart:enabled").attr('checked', false);
	return false;
}

function gotoLink(val)
{
	if(val.value!='')
		window.location = val.value;
}
function gotoLinkVal(val)
{
	if(val.value!='')
		window.location = val;
}
function searchBox(x, y)
{
	$('#'+y).css("display", "none");
	$('#'+x).css("display", "block");
	return false;
}

function submitForm(formId, noticeId, noticeColor, colorOk, colorNoOk, errorMsg)
{
	if(noticeColor) {$('#'+noticeId).css('color', noticeColor);}
	$('#'+noticeId).text('');
	$('#'+formId).ajaxSubmit(
	 {
		 success: function(t)
		 {
		 	var res = Array();
		 	res = t.split('|');
		 	switch (res[0])
		 	{
		 		case 'OK':
		 			if(colorOk) {$('#'+noticeId).css('color', colorOk);}
		 			$('#'+noticeId).html(res[1]);
		 			break;
		 		case 'NOOK':
		 			if(colorNoOk) {$('#'+noticeId).css('color', colorNoOk);}
		 			$('#'+noticeId).html(res[1]);
		 			break;
		 		case 'DEFAULT':
		 			$('#'+noticeId).html(res[1]);
		 			break;
		 		case 'GOTO':
		 			window.location = res[1];
		 			break;
		 		default:
		 			if(colorNoOk) {$('#'+noticeId).css('color', colorNoOk);}
		 			$('#'+noticeId).html(errorMsg);
		 			break;		 		
		 	}	 	
		 }
	 });
}

function removeItem(path, id, uid, url_wo_sort, sort, direction)
{
	var uws = btoa(url_wo_sort);
	$('#cart_content').load(path + 'cart_deamon.php?submit=REMOVE_ITEM&uid='+uid+'&id='+id+'&uws='+uws+'&sort='+sort+'&sort_direction='+direction);
	$('#short_cart_info').load(path + 'cart_deamon.php?submit=MENU_CART_INFO');
	return false;
}

function clearCart(path, uid)
{
	$('#cart_content').load(path + 'cart_deamon.php?submit=CLEAR_CART&uid='+uid, function() {
	$('#short_cart_info').load(path + 'cart_deamon.php?submit=MENU_CART_INFO');
	});
	return false;
}

function setPP(path, typeOfContent, thumbsCount, referer)
{
	window.location=path+'deamon.php?show=SET_PP&type='+typeOfContent+'&pp='+thumbsCount+'&ref='+btoa(referer);
}

function setDisplayType(path, typeOfContent, dt, referer)
{
	window.location=path+'deamon.php?show=SET_DISPLAY_TYPE&type='+typeOfContent+'&dt='+dt+'&ref='+btoa(referer);
}

function showDvdImage(img)
{
	IM = new Image();
	IM.src = img;
	$('#dvd_image').attr('src', IM.src);
}

function emailOK(e)
{
	var at = e.indexOf('@');
	var dot = e.lastIndexOf('.');
	var space = e.indexOf(' ');
	var len = e.length - 1;
	
	return ((at > 1) && (dot >= at + 1) && (dot < len) && (space == -1)) ? true : false;
}


function checkRegisterForm()
{
	$('#email_error_notice').text('');
	$('#pass_error_notice').text('');
	$('#cpass_error_notice').text('');
	$('#notice-register-form').text('');
	
	if(!emailOK($('#email').attr('value')))
	{
		$('#email_error_notice').text('Incorect email address');
		$('#email').focus();
		return false;
	}
	
	
	if($('#pass').attr('value') == '')
	{
		$('#pass_error_notice').text('Please fill password field');
		$('#pass').focus();
		return false;
	}
	
	
	if($('#pass').attr('value').length < 6 || $('#pass').attr('value').length>16)
	{
		$('#pass_error_notice').text('Wrong password length');
		$('#pass').focus();
		return false;
	}
	
	
	if($('#pass').attr('value') != $('#cpass').attr('value'))
	{
		$('#cpass_error_notice').text('Password and confirmed password are different');
		$('#cpass').focus();
		return false;
	}
	
	return true;
}