var ocontainer;
var ocontent;
var spaceToScroll;

function handleSliderChange(e, ui){
	var maxScroll = $("#cart_container").attr("scrollHeight") -
					$("#cart_container").height();
	$("#cart_container").animate({scrollTop: ui.value *
	(maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui){
	var maxScroll = $("#cart_container").attr("scrollHeight") -
					$("#cart_container").height();
	$("#cart_container").attr({scrollTop: ui.value * (maxScroll / 100) });
}


function init_slider(){
	var hh = $("#cart_container").height();
	var ch = $("div.cartt").height();
	if (ch>hh){
		$("#cart_slider").show();
		$("#cart_slider").slider({
			animate: true,
			handle: ".slider-handle",
			change: handleSliderChange,
			slide: handleSliderSlide
		});
	}
}

$(document).ready(function(){

	$("#prod_enlarge").hover(function(){ $("#prod_enlarge strong").show();},function(){ $("#prod_enlarge strong").hide();});
	$("#prod_close").hover(function(){ $("#prod_close strong").show();},function(){ $("#prod_close strong").hide();});
	$("#prod_enlarge1").hover(function(){ $("#prod_enlarge1 strong").show();},function(){ $("#prod_enlarge1 strong").hide();});
	$("#prod_close1").hover(function(){ $("#prod_close1 strong").show();},function(){ $("#prod_close1 strong").hide();});
	$("#prod_enlarge2").hover(function(){ $("#prod_enlarge2 strong").show();},function(){ $("#prod_enlarge2 strong").hide();});
	$("#prod_close2").hover(function(){ $("#prod_close2 strong").show();},function(){ $("#prod_close2 strong").hide();});
	$("#prod_enlarge3").hover(function(){ $("#prod_enlarge3 strong").show();},function(){ $("#prod_enlarge3 strong").hide();});
	$("#prod_close3").hover(function(){ $("#prod_close3 strong").show();},function(){ $("#prod_close3 strong").hide();});
	$("#prod_enlarge4").hover(function(){ $("#prod_enlarge4 strong").show();},function(){ $("#prod_enlarge4 strong").hide();});
	$("#prod_close4").hover(function(){ $("#prod_close4 strong").show();},function(){ $("#prod_close4 strong").hide();});
	$("#prod_enlarge5").hover(function(){ $("#prod_enlarge5 strong").show();},function(){ $("#prod_enlarge5 strong").hide();});
	$("#prod_close5").hover(function(){ $("#prod_close5 strong").show();},function(){ $("#prod_close5 strong").hide();});
	$("#prod_enlarge6").hover(function(){ $("#prod_enlarge6 strong").show();},function(){ $("#prod_enlarge6 strong").hide();});
	$("#prod_close6").hover(function(){ $("#prod_close6 strong").show();},function(){ $("#prod_close6 strong").hide();});

	$('a.fab_mat').click(function(){
		var my_id = $(this).attr('class').replace('fab_mat ','');
		$('#'+my_id).fadeIn();	
		return false;
	});

	$('#login_form').submit(function() {
		var lang = $('body').attr('class').replace('lang_','');
		var lang_msg = '';
		var my_form = document.login_form;
		var error = 0;
		my_form.account.style.background= c_no_error;
		my_form.password.style.background= c_no_error;
		if (my_form.account.value==''){
			error = 1;
			my_form.account.style.background= c_error;
		}
		if (my_form.password.value==''){
			error = 1;
			my_form.password.style.background= c_error;
		}
		if (error == 1){
			lang_msg = 'Please enter your account and password!';
			if (lang=='fr') lang_msg = 'Veuillez introduire votre nom d\'utilisateur et votre mot de passe!';
			alert(lang_msg);
			return false;
		}
		else{
			var inputs = [];
			$(':input', this).each(function() {
				inputs.push(this.name + '=' + escape(this.value));
			})  
			jQuery.ajax({
				data: inputs.join('&'),
				url: this.action,
				timeout: 2000,
				error: function() {
					lang_msg = 'Failed to submit.';
					if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
					alert(lang_msg);
				},
				success: function(r) { 
					if (r==1){
						window.location.reload();
					}
					else{
						$('p.msg').remove();
						$('#login').append('<p class="msg">'+r+'</p>');
					}
				}
				})
				
			return false;
		}
	});

	$('#gpassword_form').submit(function() {
		var lang = $('body').attr('class').replace('lang_','');
		var lang_msg = '';
		var my_form = document.gpassword_form;
		var error = 0;
		my_form.email.style.background= c_no_error;
		if (my_form.email.value==''){
			error = 1;
			my_form.email.style.background= c_error;
		}
		if (error == 1){
			lang_msg = 'Please enter your email address!';
			if (lang=='fr') lang_msg = 'Veuillez introduire votre adresse email.';
			alert(lang_msg);
			return false;
		}
		else{
			var inputs = [];
			$(':input', this).each(function() {
				inputs.push(this.name + '=' + escape(this.value));
			})  
			jQuery.ajax({
				data: inputs.join('&'),
				url: this.action,
				timeout: 2000,
				error: function() {
					lang_msg = 'Failed to submit.';
					if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
					alert(lang_msg);
				},
				success: function(r) { 
					$('p.msg').remove();
					if (r==1){
						$('#gpassword').fadeOut(function(){
							lang_msg = '<b>Retrieve Password</b><br />Your password was reset.<br />Please check your email.';
							if (lang=='fr') lang_msg = '<b>Rétablissement de mot de passe</b><br />Votre mot de passe a été annulé. Veuillez vérifier votre adresse email!';
							show_message(1,lang_msg);
						});
					}
					else{
						$('#gpassword').append('<p class="msg">'+r+'</p>');
					}
				}
				})
				
			return false;
		}
	});

	$('#register_form').submit(function() {
		var lang = $('body').attr('class').replace('lang_','');
		var lang_msg = '';
		var my_form = document.register_form;
		var error = 0;
		my_form.first_name.style.background= c_no_error;
		my_form.last_name.style.background= c_no_error;
		my_form.type.style.background= c_no_error;
		my_form.address1.style.background= c_no_error;
		my_form.city.style.background= c_no_error;
		my_form.zip.style.background= c_no_error;
		my_form.country.style.background= c_no_error;
		my_form.phone.style.background= c_no_error;
		my_form.email.style.background= c_no_error;
		if (my_form.type.value=='0'){
			my_form.company.style.background= c_no_error;
			if (my_form.company.value==''){
				error = 1;
				my_form.company.style.background= c_error;
			}
		}
		my_form.password.style.background= c_no_error;
		my_form.password1.style.background= c_no_error;
		if (my_form.password.value==''){
			error = 1;
			my_form.password.style.background= c_error;
		}
		if (my_form.password1.value==''){
			error = 1;
			my_form.password1.style.background= c_error;
		}
		if (my_form.first_name.value==''){
			error = 1;
			my_form.first_name.style.background= c_error;
		}
		if (my_form.last_name.value==''){
			error = 1;
			my_form.last_name.style.background= c_error;
		}
		if (my_form.type.value==''){
			error = 1;
			my_form.type.style.background= c_error;
		}
		if (my_form.address1.value==''){
			error = 1;
			my_form.address1.style.background= c_error;
		}
		if (my_form.city.value==''){
			error = 1;
			my_form.city.style.background= c_error;
		}
		if (my_form.zip.value==''){
			error = 1;
			my_form.zip.style.background= c_error;
		}
		if (my_form.country.value==''){
			error = 1;
			my_form.country.style.background= c_error;
		}
		if (my_form.phone.value==''){
			error = 1;
			my_form.phone.style.background= c_error;
		}
		if (my_form.email.value==''){
			error = 1;
			my_form.email.style.background= c_error;
		}
		if (error == 1){
			lang_msg = 'Please fill in the highlighted fields!';
			if (lang=='fr') lang_msg = 'Veuillez compléter les champs indiqués!';
			alert(lang_msg);
			return false;
		}
		else{
			if (my_form.password.value!=my_form.password1.value){
				my_form.password.style.background= c_error;
				my_form.password1.style.background= c_error;
				lang_msg = 'Your passwords don\'t match.';
				if (lang=='fr') lang_msg = 'Vos mots de passe ne correspondent pas.';
				alert(lang_msg);
				return false;	
			}
			else{
				var account_type = my_form.type.value;
				var inputs = [];
				$(':input', this).each(function() {
					inputs.push(this.name + '=' + escape(this.value));
				})  
				jQuery.ajax({
					data: inputs.join('&'),
					url: this.action,
					timeout: 2000,
					error: function() {
						$('p.msg').remove();
						lang_msg = 'Failed to submit.';
						if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
						$('#register').append('<p class="msg"><br />'+lang_msg+'</p>');
					},
					success: function(r) { 
						if (r==1){
							$('#register').fadeOut(function(){
								document.location.href='index.php?lm=1';							
/*
								if (account_type==1){
								}
								else{
									show_message(1,'<b>Account Registration</b><br />Your request was succesfully saved<br />and will be processed as soon as possible.<br />Thank you.');
								}
*/
							});
						}
						else{
							$('p.msg').remove();
							$('#register').append('<p class="msg"><br />'+r+'</p>');
						}
					}
				})
			}
				
			return false;
		}
	});

});

var logins = 0;
var registers = 0;
var gpasswords = 0;
var aw = '';

function show_password(elem){
	if (elem.val()==1){
		$('#register_form p.reg_pass').fadeIn();
	}
	else{
		$('#register_form p.reg_pass').fadeOut();	
	}
}

function sh(e){ // show/hide 
	var a = eval(e+'s');
	if (aw!=''){
		$('#'+aw).fadeOut(
			function(){
				eval(aw + 's = 0');	
			}
		);
	}
	if (a==0){
		clearForm(eval('document.'+e+'_form'));
		$('p.msg').remove();
		$('#'+e).fadeIn(
			function(){
				eval(e + 's = 1');
				aw = e;
			}
		);
	}
	else{
		$('#'+e).fadeOut(
			function(){
				eval(e + 's = 0');
				aw = '';
			}
		);
	}
}

var c_error = '#733636';
var c_no_error = '#787978';

function clearForm(form) {
  $(':input', form).each(function() {
	var type = this.type;
	var tag = this.tagName.toLowerCase(); // normalize case
	if (type == 'text' || type == 'password' || tag == 'textarea')
	  this.value = "";
	else if (type == 'checkbox' || type == 'radio')
	  this.checked = false;
	else if (tag == 'select')
	  this.selectedIndex = 0;
  });
};

function buy(id){
	if (id!=''){
		var lang = $('body').attr('class').replace('lang_','');
		var lang_msg = '';
		jQuery.ajax({
			data: '&id='+id+'&r='+Math.random(),
			url: 'scripts/get_prod.php',
			timeout: 2000,
			error: function() {
				lang_msg = 'Failed to submit.';
				if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
				alert(lang_msg);
			},
			success: function(r) { 
				if (aw!=''){
					$('#'+aw).fadeOut(
						function(){
							$('#buy').html(r);
							$('#buy').fadeIn();	
							aw = 'buy';
						}
					);
				}
				else{
					$('#buy').html(r);
					$('#buy').fadeIn();	
					aw = 'buy';
				}
			}
		})
	}
}

function account(){
	var lang = $('body').attr('class').replace('lang_','');
	var lang_msg = '';
	jQuery.ajax({
		data: '&r='+Math.random(),
		url: 'scripts/get_account.php',
		timeout: 2000,
		error: function() {
			lang_msg = 'Failed to submit.';
			if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
			alert(lang_msg);
		},
		success: function(r) { 
			if (aw!=''){
				$('#'+aw).fadeOut(
					function(){
						$('#account').html(r);
						$('#account').fadeIn();	
						aw = 'account';
					}
				);
			}
			else{
				$('#account').html(r);
				$('#account').fadeIn();	
				aw = 'account';
			}
		}
	})
}

function cart(){
	var lang = $('body').attr('class').replace('lang_','');
	var lang_msg = '';
	jQuery.ajax({
		data: '&r='+Math.random(),
		url: 'scripts/get_cart.php',
		timeout: 2000,
		error: function() {
			lang_msg = 'Failed to submit.';
			if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
			alert(lang_msg);
		},
		success: function(r) { 
			if (aw!=''){
				$('#'+aw).fadeOut(
					function(){
						$('#cart').html(r);
						$('#cart').fadeIn(function(){
							init_slider();
							aw = 'cart';
						});	
					}
				);
			}
			else{
				$('#cart').html(r);
				$('#cart').fadeIn(function(){
					init_slider();
					aw = 'cart';
				});	
			}
		}
	})
}

function delete_cart(pos){
	var lang = $('body').attr('class').replace('lang_','');
	var lang_msg = '';
	if (confirm('Are you sure?')){
		jQuery.ajax({
			data: '&pos='+pos+'&r='+Math.random(),
			url: 'scripts/scr_cart_del.php',
			timeout: 2000,
			error: function() {
				lang_msg = 'Failed to submit.';
				if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
				alert(lang_msg);
			},
			success: function(r) { 
				jQuery.ajax({
					data: '&r='+Math.random(),
					url: 'scripts/get_cart.php',
					timeout: 2000,
					error: function() {
						lang_msg = 'Failed to submit.';
						if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
						alert(lang_msg);
					},
					success: function(r) { 
						$('#cart').html(r);
						init_slider();
					}
				})		
			}
		})
	}
}

function checkout(){
	var lang = $('body').attr('class').replace('lang_','');
	var lang_msg = '';
	jQuery.ajax({
		data: '&r='+Math.random(),
		url: 'scripts/get_checkout.php',
		timeout: 2000,
		error: function() {
			lang_msg = 'Failed to submit.';
			if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
			alert(lang_msg);
		},
		success: function(r) { 
			if (aw!=''){
				$('#'+aw).fadeOut(
					function(){
						$('#checkout').html(r);
						$('#checkout').fadeIn();	
						aw = 'checkout';
					}
				);
			}
			else{
				$('#checkout').html(r);
				$('#checkout').fadeIn();	
				aw = 'checkout';
			}
		}
	});
}

function cw(e){
	$('#'+e).fadeOut();
	aw = '';
}

function sel_mat(type,id,value){
	var sv = eval('document.buy_form.'+type+'.value');
	if (sv!=''){
		if (value!=''){
			$('li.opt_'+sv+' a').removeClass('msel');
		}
		else{
			$('#'+type+sv+' a').removeClass('msel');
		}
	}
	$('#'+type+id+' a').addClass('msel');
	$('#'+type+'s').html($('#'+type+id+' a').html());
	if (value!=''){
		eval('document.buy_form.'+type+'.value="'+value+'"');
	}
	else{
		eval('document.buy_form.'+type+'.value="'+id+'"');
	}
}


function validate_checkout_form(){
		var lang = $('body').attr('class').replace('lang_','');
		var lang_msg = '';
		var my_form = document.checkout_form;
		var error = 0;
		my_form.bill_name.style.background= c_no_error;
/*
		my_form.bill_company.style.background= c_no_error;
		my_form.bill_tax_id.style.background= c_no_error;
*/
		my_form.bill_address1.style.background= c_no_error;
		my_form.bill_city.style.background= c_no_error;
		my_form.bill_zip.style.background= c_no_error;
		my_form.bill_country.style.background= c_no_error;
		my_form.bill_phone.style.background= c_no_error;
		my_form.bill_email.style.background= c_no_error;
		my_form.ship_name.style.background= c_no_error;
		my_form.ship_company.style.background= c_no_error;
		my_form.ship_address1.style.background= c_no_error;
		my_form.ship_city.style.background= c_no_error;
		my_form.ship_state.style.background= c_no_error;
		my_form.ship_zip.style.background= c_no_error;
		my_form.ship_country.style.background= c_no_error;
		my_form.payment.style.background= c_no_error;
		if (my_form.payment.value==''){
			error = 1;
			my_form.payment.style.background= c_error;
		}
		if (my_form.bill_name.value==''){
			error = 1;
			my_form.bill_name.style.background= c_error;
		}
/*
		if (my_form.bill_company.value==''){
			error = 1;
			my_form.bill_company.style.background= c_error;
		}
		if (my_form.bill_tax_id.value==''){
			error = 1;
			my_form.bill_tax_id.style.background= c_error;
		}
*/
		if (my_form.bill_address1.value==''){
			error = 1;
			my_form.bill_address1.style.background= c_error;
		}
		if (my_form.bill_city.value==''){
			error = 1;
			my_form.bill_city.style.background= c_error;
		}
/*
		if (my_form.bill_state.value==''){
			error = 1;
			my_form.bill_state.style.background= c_error;
		}
*/
		if (my_form.bill_zip.value==''){
			error = 1;
			my_form.bill_zip.style.background= c_error;
		}
		if (my_form.bill_country.value==''){
			error = 1;
			my_form.bill_country.style.background= c_error;
		}
		if (my_form.bill_phone.value==''){
			error = 1;
			my_form.bill_phone.style.background= c_error;
		}
		if (my_form.bill_email.value==''){
			error = 1;
			my_form.bill_email.style.background= c_error;
		}
		if (my_form.ship_name.value==''){
			error = 1;
			my_form.ship_name.style.background= c_error;
		}
/*
		if (my_form.ship_company.value==''){
			error = 1;
			my_form.ship_company.style.background= c_error;
		}
*/
		if (my_form.ship_address1.value==''){
			error = 1;
			my_form.ship_address1.style.background= c_error;
		}
		if (my_form.ship_city.value==''){
			error = 1;
			my_form.ship_city.style.background= c_error;
		}
/*
		if (my_form.ship_state.value==''){
			error = 1;
			my_form.ship_state.style.background= c_error;
		}
*/
		if (my_form.ship_zip.value==''){
			error = 1;
			my_form.ship_zip.style.background= c_error;
		}
		if (my_form.ship_country.value==''){
			error = 1;
			my_form.ship_country.style.background= c_error;
		}
		if (error == 1){
			lang_msg = 'Please fill in the highlighted fields!';
			if (lang=='fr') lang_msg = 'Veuillez compléter les champs indiqués!';
			alert(lang_msg);
			return false;
		}
		else{
			var lang = $('body').attr('class').replace('lang_','');
			var lang_msg = '';
			var inputs = [];
			$(':input', my_form).each(function() {
				inputs.push(this.name + '=' + encodeURIComponent(this.value));
			})  
			jQuery.ajax({
				data: inputs.join('&')+'&r='+Math.random(),
				url: 'scripts/get_checkoutp.php',
				timeout: 2000,
				error: function() {
					lang_msg = 'Failed to submit.';
					if (lang=='fr') lang_msg = 'Une erreur s\'est produite lors de l\'enregistrement.';
					alert(lang_msg);
				},
				success: function(r) { 
					if (aw!=''){
						$('#'+aw).fadeOut(
							function(){
								$('#cart').html(r);
								$('#cart').fadeIn(function(){
									aw = 'cart';
									init_slider();				
								});	
							}
						);
					}
					else{
						$('#cart').html(r);
						$('#cart').fadeIn(function(){
							aw = 'cart';
							init_slider();				
						});	
					}
				}
			});
			return false;
		}
		return false;
}

function check_ship(){
	var my_form = document.checkout_form;
	var error = 0;
	my_form.bill_name.style.background= c_no_error;
	my_form.bill_company.style.background= c_no_error;
	my_form.bill_address1.style.background= c_no_error;
	my_form.bill_city.style.background= c_no_error;
	my_form.bill_state.style.background= c_no_error;
	my_form.bill_zip.style.background= c_no_error;
	my_form.bill_country.style.background= c_no_error;
	my_form.bill_phone.style.background= c_no_error;
	my_form.bill_email.style.background= c_no_error;
	if (my_form.bill_name.value==''){
		my_form.bill_name.style.background=c_error;
		error = 1;
	}
/*
	if (my_form.bill_company.value==''){
		my_form.bill_company.style.background=c_error;
		error = 1;
	}
*/
	if (my_form.bill_address1.value==''){
		my_form.bill_address1.style.background=c_error;
		error = 1;
	}
	if (my_form.bill_city.value==''){
		my_form.bill_city.style.background=c_error;
		error = 1;
	}
/*
	if (my_form.bill_state.value==''){
		my_form.bill_state.style.background=c_error;
		error = 1;
	}
*/
	if (my_form.bill_zip.value==''){
		my_form.bill_zip.style.background=c_error;
		error = 1;
	}
	if (my_form.bill_country.value==''){
		my_form.bill_country.style.background=c_error;
		error = 1;
	}
	if(my_form.same_as_billing.checked==true){
		if (error == 0){
			my_form.ship_name.readOnly=true;
			my_form.ship_company.readOnly=true;
			my_form.ship_address1.readOnly=true;
			my_form.ship_address2.readOnly=true;
			my_form.ship_city.readOnly=true;
			my_form.ship_state.readOnly=true;
			my_form.ship_zip.readOnly=true;
			my_form.ship_country.readOnly=true;

			my_form.ship_name.value=my_form.bill_name.value;
			my_form.ship_company.value=my_form.bill_company.value;
			my_form.ship_address1.value=my_form.bill_address1.value;
			my_form.ship_address2.value=my_form.bill_address2.value;
			my_form.ship_city.value=my_form.bill_city.value;
			my_form.ship_state.value=my_form.bill_state.value;
			my_form.ship_zip.value=my_form.bill_zip.value;
//			my_form.ship_country.value=my_form.bill_country.value;
		}
		else{
			my_form.same_as_billing.checked = false;
			lang_msg = 'Please fill in correctly the billing information.';
			if (lang=='fr') lang_msg = 'Veuillez compléter correctement les coordonées facturation!';
			alert(lang_msg);
		}
	}
	else{
		my_form.ship_name.value='';
		my_form.ship_company.value='';
		my_form.ship_address1.value='';
		my_form.ship_address2.value='';
		my_form.ship_city.value='';
		my_form.ship_state.value='';
		my_form.ship_zip.value='';
		my_form.ship_country.value='';

		my_form.ship_name.readOnly=false;
		my_form.ship_company.readOnly=false;
		my_form.ship_address1.readOnly=false;
		my_form.ship_address2.readOnly=false;
		my_form.ship_city.readOnly=false;
		my_form.ship_state.readOnly=false;
		my_form.ship_zip.readOnly=false;
		my_form.ship_country.readOnly=false;
	}
}

function calculate_shipping(country){
	$("#shipping_cost").load("scripts/scr_get_shipping.php",{'country':encodeURIComponent(country.value)},function(){

	});
}