
	// AlyShop javascript functions
	
	var current_numeric_value = 0;
	
	$(document).ready(function() {
	
		$('input.numeric_field').focus( function() { current_numeric_value = $(this).val(); } );
		$('input.numeric_field').keyup( function() { if (!valid_number(this.id)) { $(this).val(current_numeric_value); } } );
		$('input.numeric_field').change( function() { if (!valid_number(this.id)) $(this).val(current_numeric_value); } );
		
		$('#ps_facebook').html('<a name="fb_share" type="button_count" href="http://www.facebook.com/sharer.php&u='+escape(PAGE_DATA.url)+'&t='+escape(PAGE_DATA.title)+'">Share</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script>');
		$('#ps_facebook_like').html('<iframe src="http://www.facebook.com/plugins/like.php?href='+escape(PAGE_DATA.url)+'&amp;layout=standard&amp;show_faces=false&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:35px;" allowTransparency="true"></iframe>');
		$('#sb_likebox').html('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpages%2FBacau%2FAlyShop%2F473688945396&amp;width=210&amp;colorscheme=light&amp;connections=9&amp;stream=false&amp;header=true&amp;height=370" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:210px; height:370px;" allowTransparency="true"></iframe>');
		
		//$('#ps_twitter').html('<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="AlyShop">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>');
		//$('#fb_recom_area').html('<iframe src="http://www.facebook.com/plugins/like.php?href='+PAGE_DATA.url+'&amp;layout=standard&amp;show_faces=true&amp;width=450&amp;action=recommend&amp;font=arial&amp;colorscheme=light&amp;height=70" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:450px; height:70px;"></iframe>');
		
		$(window).scroll( function () {
			$('#gallery_order_window').animate({top: $(window).scrollTop() + 100}, 'fast');
		});
		
		$('a.gallery_photo').click(function () {
			//$('a.gallery_photo img').css({'borderColor': '#ddd'});
			var large_picture = $(this).attr('href');
			$('#current_gallery_picture').fadeOut('medium',
				function() { 
					$('#current_gallery_picture').attr('src', large_picture).fadeIn('medium'); 
					//$(this.id+' > img').css({'borderColor': '#F36'});
				}
			);
			//document.location.hash = '#picture_top';
			return false;
		});
		
		$('div.contest_vote_item a').hover(
			function() {
				var vote_id = this.id.replace('vote_note_', '');
				for (var vc = 0; vc <= vote_id; vc++) {
					vi = 'vote_note_'+vc;
					$('#'+vi).css({'backgroundImage': 'url(/img/icons/v/on.gif)', 'backgroundRepeat': 'no-repeat', 'backgroundPosition': 'center center'});
				}
			},
			function() {
				var vote_id = this.id.replace('vote_note_', '');
				for (var vc = 0; vc <= vote_id; vc++) {
					vi = 'vote_note_'+vc;
					$('#'+vi).css({'backgroundImage': 'url(/img/icons/v/off.gif)', 'backgroundRepeat': 'no-repeat', 'backgroundPosition': 'center center'});
				}
			}
		);
		
		$('.product_available_stock').each(function () {
			$(this).prepend('<div style="position: absolute; z-index: 1;" class="product_tooltip_stock_stock opacity75">In stoc</div>');
		});
		
		$('.product_available_order').each(function () {
			$(this).prepend('<div style="position: absolute; z-index: 1;" class="product_tooltip_stock_order opacity75">La comanda</div>');
		});
		
		$('.product_available_no').each(function () {
			$(this).prepend('<div style="position: absolute; z-index: 1;" class="product_tooltip_stock_no opacity75">Stoc epuizat</div>');
		});
	});
	
	function jump_to(url) {
		document.location.href = url;
		return false;
	}

	function popup_alert(id, item, message) {
		$('#'+id).html(message);
		$('#'+id).slideDown("fast");
		//slideDown('slow')
		
		if (item != '') {
			$('#'+item).addClass('alert_item');
			$('#'+item).focus();
		}
		
		return false;
	}
	
	function valid_email(email) {
		if ($('#'+email).val() == '') {
			$('#'+email).addClass('alert_item');
			$('#'+email).focus();
			
			return false;
		}
		
		var email_pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+$/;
		return email_pattern.test($('#'+email).val());
	}
	
	function valid_number(number) {
		if ($('#'+number).val() == '') {
			$('#'+number).addClass('alert_item');
			$('#'+number).focus();
			
			return false;
		}
		
		if (isNaN($('#'+number).val())) {
			$('#'+number).addClass('alert_item');
			$('#'+number).focus();
			
			return false;
		}
		
		return true;
	}
	
	function valid_text(text) {
		if ($('#'+text).val() == '') {
			$('#'+text).addClass('alert_item');
			$('#'+text).focus();
			
			return false;
		}
		
		return true;
	}
	
	function valid_size(text, size) {
		if ($('#'+text).val().length < size) {
			$('#'+text).addClass('alert_item');
			$('#'+text).focus();
			
			return false;
		}
		
		return true;
	}
	
	
	function valid_select(text) {
		if ($('#'+text+' option:selected').val() == 0) {
			$('#'+text).addClass('alert_item');
			$('#'+text).focus();
			
			return false;
		}
		
		return true;
	}

	function validate_contact_form() {
		var contact_error = '';
		
		if ($('#name').val() == '') {
			return popup_alert('contact_alert_area', 'name', 'Completeaza-ti numele!');
		}
		
		if (($('#email').val() == '') || !valid_email('email')) {
			return popup_alert('contact_alert_area', 'email', 'Completeaza-ti adresa de email!');
		}
		
		if ($('#message').val() == '') {
			return popup_alert('contact_alert_area', 'message', 'Introdu mesajul tau!');
		}
		
		return true;
	}

	function validate_register() {
		if (!valid_email('email')) {
			return popup_alert('form_message', 'email', 'Completeaza o adresa de e-mail valida!');
		}
		
		return true;
	}
	
	function validate_login() {
		if (!valid_email('username')) {
			return popup_alert('form_message', 'username', 'Adresa de e-mail si/sau parola sunt incorecte!');
		}
		
		if (!valid_text('password')) {
			return popup_alert('form_message', 'password', 'Adresa de e-mail si/sau parola sunt incorecte!');
		}
		
		return true;
	}
	
	function validate_password() {
		if (!valid_text('password')) {
			return popup_alert('form_message', 'password', 'Parola veche nu este completata!');
		}
		
		if (!valid_text('request_password') || !valid_size('request_password', 6)) {
			return popup_alert('form_message', 'request_password', 'Parola noua trebuie sa contina minim 6 caractere!');
		}
		
		if (!valid_text('confirm_password') || !valid_size('confirm_password', 6)) {
			return popup_alert('form_message', 'confirm_password', 'Confirmarea parolei nu este corecta!');
		}
		
		if (('#request_password').val() != ('#confirm_password').val()) {
			return popup_alert('form_message', 'confirm_password', 'Parola noua nu a fost confirmata corect!');
		}
		
		return true;
	}
	
	function validate_profile() {
		if (
			!valid_text('fname') 		|| 	!valid_text('lname') 		||
			!valid_number('cnp') 		|| 	!valid_size('cnp', 13) 		||
			!valid_number('phone') 		||
			!valid_number('poffice')	|| 	!valid_size('poffice', 6) 	||
			
			!valid_text('address_street') ||
			!valid_number('address_number') ||
			
			false
			) 
			return popup_alert('form_message', 'form_message', 'Campurile marcate cu rosu sunt invalide! Va rugam verificati corectitudinea lor.');
		
		return true;
	}
	
	function add_error_span(item_id, message) {
		$('#'+item_id).after('<span id="'+item_id+'_error" class="order_contact_error"><br />'+message+'</span>');
		$('#'+item_id).addClass('alert_item');
		$('tr > td > #'+item_id).addClass('alert_item');
	}
	
	function validate_order_basket_form() {
		var form_completed = true;
		var order_total = $('#order_total').html().replace('.', '').replace(',', '.');
		var total_order_value = parseInt(order_total);
		
		if (total_order_value < MIN_ORDER) 
			form_completed = false;
		
		if (!form_completed) {
			alert('Comanda minima este de '+MIN_ORDER+' RON.\nVa multumim pentru intelegere!');
			document.location.hash = '#form_top';
			popup_alert('contact_alert_area', '', 'Comanda minima este de '+MIN_ORDER+' RON. Vizitati <a href="'+SITE_URL+'/produse/">catalogul de produse</a> pentru a adauga mai multe produse in cosul de cumparaturi. Va multumim pentru intelegere!');
		}
		
		return form_completed;
	}
	
	function validate_order_contact_form() {
		var form_completed = true;
		
		$('.order_contact_error').remove();
		$('.alert_item').removeClass('alert_item');
		
		if (!valid_text('contact_fname') && !valid_size('contact_fname', 3)) {
			add_error_span('contact_fname', 'Prenumele nu a fost completat.');
			form_completed = false;
		}
		
		if (!valid_text('contact_lname') && !valid_size('contact_lname', 3)) {
			add_error_span('contact_lname', 'Numele nu a fost completat.');
			form_completed = false;
		}
		
		if (!valid_email('contact_email')) {
			add_error_span('contact_email', 'Adresa de email nu este valida.');
			form_completed = false;
		}
		
		if (!valid_number('contact_phone')) {
			add_error_span('contact_phone', 'Numarul de telefon trebuie sa contina doar cifre.');
			form_completed = false;
		}
		
		if (!valid_text('deliver_address')) {
			add_error_span('deliver_address', 'Adresa de livrare nu a fost completata.');
			form_completed = false;
		}
		
		if (!valid_number('deliver_poffice') && !valid_size('deliver_poffice', 4)) {
			add_error_span('deliver_poffice', 'Codul postal nu a fost completat corect.');
			form_completed = false;
		}
		
		if (!valid_text('deliver_city') && !valid_size('deliver_city', 4)) {
			add_error_span('deliver_city', 'Localitatea nu a fost completata.');
			form_completed = false;
		}
		
		if (!valid_select('deliver_county')) {
			add_error_span('deliver_county', 'Judetul nu a fost selectat.');
			form_completed = false;
		}
		
		if (!form_completed) {
			document.location.hash = '#form_top';
			popup_alert('contact_alert_area', '', 'Informatiile solicitate nu au fost completate corect. Va rugam verificati datele introduse.');
		}
		
		return form_completed;
	}
	
	function product_add_to_basket(product_id) {
		$.post(ACTIONS_URL, { action: 'add_product', product_id: product_id },
			function(response){
				if (!response.error) {
					$('#buy_product').html(response.result);
					update_basket_stats();
				}
				else {
					$('#buy_product').html(response.status);
				}
			}, 'json');
			
		return false;
	}
	
	function product_remove_from_basket(product_id) {
		if (confirm('Stergi produsul din cosul de cumparaturi?')) {
			$.post(ACTIONS_URL, { action: 'remove_product', product_id: product_id },
				function(response){
					//$('#product_row_' + product_id).slideUp('fast');
					$('#product_row_' + product_id).remove();
					update_basket_stats();
				}, 'json');
		}
			
		return false;
	}
	
	function update_basket_stats() {
		$.post(ACTIONS_URL, { action: 'basket_stats' },
			function(response){
				$('#shop_header_count').html(response.result.products);
				$('#shop_sidebar_count').html(response.result.products);
				$('#shop_sidebar_value').html(number_format(response.result.total));
				$('#order_total').html(number_format(response.result.total));
			}, 'json');
		
		return false;
	}
	
	function updown_change(item_prefix, change) {
		var quantity_id = item_prefix + '_quantity';
		var price_id = item_prefix + '_price';
		var total_id = item_prefix + '_total';
		
		var value = parseInt($('#'+quantity_id).val());
		var price = parseFloat($('#'+price_id).val());
		var total = parseFloat($('#'+total_id).val());
		
		value += change;
		if (value < 1 || value > 20) return false;
		
		total = value * price;
		
		$('#'+quantity_id).val(value);
		$('#'+total_id).val(total);
		
		return false;
	}
	
	function number_convert(number) {
		return number.toString().replace(',', '|').replace('.', ',').replace('|', '.');
	}
	
	function number_format(number) {
		var strnum = number_convert(number);
		if (strnum.indexOf(',') == -1) strnum = strnum + ',00';
		else {
			parts = strnum.split(',');
			if (parts[1].length == 1) strnum = strnum + '0';
		}
		return strnum;
	}
	
	function order_updown_change(item_prefix, change) {
		var quantity_id = item_prefix + '_quantity';
		var price_id = item_prefix + '_price';
		var total_id = item_prefix + '_total';
		var variation = 0;
		
		var value = parseInt($('#'+quantity_id).val());
		var product_id = parseInt($('#'+item_prefix + '_id').html());
		var price = parseFloat(number_convert($('#'+price_id).html()));
		var total = parseFloat(number_convert($('#'+total_id).html()));
		var order = parseFloat(number_convert($('#order_total').html()));
		
		variation = value * price;
		value += change;
		if (value < 1 || value > 20) return false;
		
		$.post(ACTIONS_URL, { action: 'update_product', product_id: product_id, quantity: value },
			function(response){
				total = value * price;
				variation = total - variation;
				order += variation;
				
				$('#'+quantity_id).val(value);
				$('#'+total_id).html(number_format(total));
				$('#order_total').html(number_format(order));
				
				update_basket_stats();
			}, 'json');
		
		return false;
	}
	
	function order_quantity_change(item_prefix, change) {
		var quantity_id = item_prefix + '_quantity';
		var price_id = item_prefix + '_price';
		var total_id = item_prefix + '_total';
		var variation = 0;
		
		var value = parseInt($('#'+quantity_id).val());
		var product_id = parseInt($('#'+item_prefix + '_id').val());
		var price = parseFloat(number_convert($('#'+price_id).html()));
		var total = parseFloat(number_convert($('#'+total_id).html()));
		var order = parseFloat(number_convert($('#order_total').html()));
		
		variation = value * price;
		//value += change;
		if (value < 1 || value > 20) return false;
		
		$.post(ACTIONS_URL, { action: 'update_product', product_id: product_id, quantity: value },
			function(response){
				total = value * price;
				variation = total - variation;
				order += variation;
				
				$('#'+quantity_id).val(value);
				$('#'+total_id).html(number_format(total));
				//$('#order_total').html(number_format(order));
				
				update_basket_stats();
			}, 'json');
		
		return false;
	}
	
	
	function order_toggle_company(select) {
		if (select.selectedIndex == 0) {
			$('#cnp_label').html('Serie si numar CI:');
		}
		else {
			$('#cnp_label').html('Cod fiscal:');
		}
		
		return false;
	}
	
	function validate_product_order_form() {
		var items = 0;
		var check = 0;
		
		while(document.getElementById('product_quantity_'+items)) {
			if ($('#product_quantity_'+items).val() != 0) {
				check++;
			}
			items++;
		}
		
		if (!check) {
			document.location.hash = 'comanda';
		
			alert('Pentru a putea adauga produse in cosul de cumparaturi selectati cantitatea dorita.');
			$('#product_quantity_0').focus();
			return false;
		}
		
		return true;
	}
	
	function show_comment_form() {
		$('#comments_form_container').slideToggle();
		//document.location.hash = 'comments';
		return false;
	}

	function validate_comment_form() {
		var comment_error = '';
		
		if ($('#comment_name').val() == '') {
			return popup_alert('comment_alert_area', 'name', 'Completeaza-ti numele!');
		}
		
		if (($('#comment_email').val() == '') || !valid_email('comment_email')) {
			return popup_alert('comment_alert_area', 'email', 'Completeaza-ti adresa de e-mail!');
		}
		
		if ($('#comment_text').val() == '') {
			return popup_alert('comment_alert_area', 'message', 'Introdu comentariul tau!');
		}
		
		return true;
	}
	
	function popup_order_form() {
		/*
		$('#overlayer').css('opacity', 0);
		$('#overlayer').css('height', $(document).height());
		$('#overlayer').css('background', '#fff');
		$('#overlayer').css('visibility', 'visible');
		
		
		$('#overlayer').animate({opacity: .75}, 'slow',
			function () {
				$('#window_wrapper').fadeIn('slow');
				$('#email').focus();
			}
		);
		*/
		
		if (window.popup_order) {
			return close_order_form();
		}
		
		$('#gallery_order_window').animate({top: $(window).scrollTop() + 100}, 'fast',
			function() { 
				$('#gallery_order_window').fadeIn('medium',
					function () {
						window.popup_order = true;
					}
				); 
			}
		);
		
		return false;
	}
	
	function close_order_form() {
		$('#gallery_order_window').fadeOut('fast', 
			function() {
				window.popup_order = false;
				//$('#overlayer').animate({opacity: 0}, 'fast')
			}
		);
		
		/*
		$('#overlayer').css('opacity', 0);
		$('#overlayer').css('visibility', 'hidden');
		*/
		
		return false;
	}

	function contest_vote(id, vote) {
		$.post(
			'/actions.html',
			{action: 'contest_vote', apply_id: id, vote_id: vote},
			function(response) {
				if (!response.error) {
					$('#vote_message').fadeOut('fast',
						function () {
							$('#vote_description').html('<strong>Multumim pentru vot!</strong>');
							$('#vote_status').html('Sunt inregistrate '+response.result.votes+' voturi. Nota medie acordata <strong class="pinkish">'+response.result.rank+'</strong>');
							$('#vote_message').fadeIn('medium');
						}
					);
				}
				else {
					$('#vote_description').html('<strong>Ati votat deja!</strong>');
					$('#vote_message').fadeIn('fast');
				}
			},
			'json'
		);
		
		return false;
	}
	

