function confirm_delete() 
{
	if (confirm('Are you sure?')) 
	{
			return true;
	} else {
			return false;
	}
}

function changeLocation(newLocation)
{
   document.location.href = newLocation;
   return false;
}

function goToAddToBundle()
{ 
   if ( isCheckboxSelected() )
   {
      if ( confirm_delete() )
      {
         document.data.a.value=17.03;
         document.forms['data'].submit();
      }
   }
   else
   {
      alert("Select items to add to bundle.");
   }

   return false;
}


function isSelectedForDiscount()
{ 
   if ( isCheckboxSelected() )
   {
   popup('15.00');
   }
   else
   {
      alert("Select items to add discount.");
   }
   return false;
}



function goToAddDiscount()
{ 
   if ( isCheckboxSelected() )
   {
         document.data.a.value='15.02';
         document.forms['data'].submit();
   }
   else
   {
      alert("Select items to add discount.");
   }

   return false;
}


function isSelectedForGPC()
{   
   popup('18.00');
}



function goToAddGPC()
{ 
   document.data.a.value='18.02';
   document.forms['data'].submit();

   return true;
}


function goToHideAction(state, strValue)
{
   if ( isCheckboxSelected() )
   {
      document.data.a.value=50;
      document.data.state.value=state;
      document.data.submit();
   }
   else
   {
      alert("Select items for " + strValue + ".");
   }

   return false;
}

function changeLocation(newLocation)
{
   document.location.href = newLocation;
   return false;
}



function goToCopy() {
   if ( isCheckboxSelected() )
   {
      document.data.a.value=101;
      document.data.submit();
   }
   else
   {
      alert("Select items for copying.");
   }

   return false;
}

function goToPaste() { 
   document.data.a.value=102;
   document.data.submit();

   return false;
}

function checkSignUpForm()
{
	var theForm = document.forms.sign_up_form;

	if (!theForm.user_email.value)
	{
		alert("Please, enter your e-mail.");
		theForm.user_email.focus();
		return false;
	}

	if( !checkEmailAddress(theForm.user_email.value) )
	{
		alert("Please, enter valid email address.");
		theForm.user_email.focus();
		return false;
	}

	if (!theForm.user_password.value)
	{
		alert("Please, enter your password.");
		theForm.user_password.focus();
		return false;
	}
	
	if (theForm.user_password.value.length < 4 || theForm.user_password.value.length > 12)
	{
		alert("Your password must be 4 to 12 characters long with no spaces.");
		theForm.user_password.focus();
		return false;
	}

	if (theForm.user_password.value != theForm.user_password2.value)
	{
		alert("Please verify your password - the password you entered in both fields does not match.");
		theForm.user_password2.focus();
		return false;
	}

	if (!theForm.user_con_code.value)
	{
		alert("Please, enter confirmation code.");
		theForm.user_con_code.focus();
		return false;
	}

	return true;
}

function checkIsIntNumberCorrect(sNumber)
{
 var re = new RegExp("^(\\d)+$");
 var ares = re.exec(sNumber); 
 if ( null == ares )
 {
  return false;
 }

 return true;
}

function checkIsDoubleNumberCorrect(sNumber)
{
 var re = new RegExp("^(\\d)+(\\.(\\d)+)?$");
 var ares = re.exec(sNumber); 
 if ( null == ares )
 {
  return false;
 }

 return true;
}


function validateFormCertificate()
{

	if (document.getElementById('amount').value == "")
	{
	  alert("Please, enter amount.");
	  document.getElementById('amount').focus();
	  return (false);
	}

	if (isNaN(document.getElementById('amount').value))
	{
	  alert("The number you entered is invalid, check the number and try again.");
	  document.getElementById('amount').focus();
	  return (false);
	}

	if (document.getElementById('email').value == "")
	{
	  alert("Please, enter email.");
	  document.getElementById('email').focus();
	  return (false);
	}
	
	var re_check_email = new RegExp("[A-Za-z0-9_]+([-+.][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\\.[A-Za-z0-9_]{2,}([-.][A-Za-z0-9_]+)*");
	var ares = re_check_email.exec(document.getElementById('email').value);
	if( ares == null )
	{
	  alert("Please, enter valid email address.");
	  document.getElementById('email').focus();
	  return (false);
	}

	return true;
}

function updateCertificateAjax(){
	from_text = encodeURI(document.getElementById('from_text').value);
	to_text = encodeURI(document.getElementById('to_text').value);
	message = encodeURI(document.getElementById('message').value);
	email = encodeURI(document.getElementById('email').value);
	amount = document.getElementById('amount').value;
	id  = document.getElementById('id').value;

	ajaxRequestByURL("index.php?a=6.04&id="+id+"&from_text="+from_text+"&to_text="+to_text+"&message="+message+"&email="+email+"&amount="+amount, "certificate");

}

function validateFormBillTo()
{
	if (document.getElementById('bill_name').value == "")
	{	 
	  alert("Please, enter name.");
	  document.getElementById('bill_name').focus();
	  return (false);
	}
	if (document.getElementById('bill_firstname').value == "")
	{	 
	  alert("Please, enter first name.");
	  document.getElementById('bill_firstname').focus();
	  return (false);
	}
	
	if (document.getElementById('bill_lastname').value == "")
	{
	  alert("Please, enter last name.");
	  document.getElementById('bill_lastname').focus();
	  return (false);
	}
	
	if (document.getElementById('bill_address1').value == "")
	{
	  alert("Please, enter address.");
	  document.getElementById('bill_address1').focus();
	  return (false);
	}
	
	if (document.getElementById('bill_city').value == "")
	{
	  alert("Please, enter city.");
	  document.getElementById('bill_city').focus();
	  return (false);
	}
	
	if (document.getElementById('bill_zip').value == "")
	{	
	  alert("Please, enter zip code.");
	  document.getElementById('bill_zip').focus();
	  return (false);
	}
	
	var bill_zip_lenght = document.getElementById('bill_zip').value.length;
	if ( bill_zip_lenght > 0 && bill_zip_lenght != 5 )
	{
		alert("Please, enter correct BILL TO zip.");
		document.getElementById('bill_zip').focus();
		return (false);
	}
	
	if (document.getElementById('bill_state').value == "N/A")
	{
	  alert("Please, select state.");
	  document.getElementById('bill_state').focus();
	  return (false);
	}
	
	if (document.getElementById('bill_phone').value == "")
	{
	  alert("Please, enter phone.");
	  document.getElementById('bill_phone').focus();
	  return (false);
	}

	if (document.getElementById('bill_email').value == "")
	{
	  alert("Please, enter email.");
	  document.getElementById('bill_email').focus();
	  return (false);
	}
	
	var re_check_email = new RegExp("[A-Za-z0-9_]+([-+.][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\\.[A-Za-z0-9_]{2,}([-.][A-Za-z0-9_]+)*");
	var ares = re_check_email.exec(document.getElementById('bill_email').value);
	if( ares == null )
	{
	  alert("Please, enter valid email address.");
	  document.getElementById('bill_email').focus();
	  return (false);
	}

	return true;

}

function validateFormShipTo()
{
	if (document.getElementById('ship_name').value == "")
	{	 
	  alert("Please, enter name.");
	  document.getElementById('ship_name').focus();
	  return (false);
	}
	return true;
}

function redeemMyCertificates(parameters)
{
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "a=50.4&total_sum=" + $('#total_sum').val() + ((parameters == '')? '' : '&'+parameters),
		dataType: "html",
		complete: function(){},
		success: function(data){ 
			obj = $('#certificate_redeem');
			if (obj)
				obj.html(data);
			//document.getElementById('certificate_redeem').innerHTML = data;
		},
		error: function()
		{
			//alert('Export operation failed!');
		}
	});
}

function applyCertificate(total_amount, selected_amount, total_sum, number)
{
	selected_amount = parseFloat(selected_amount);
	total_amount = parseFloat(total_amount);
	total_sum = parseFloat(total_sum);

	if (selected_amount <= total_amount && selected_amount <= total_sum) 
	{
		 redeemMyCertificates('apply_number='+number+'&apply_amount='+selected_amount);
	}
	else if (selected_amount <= total_amount && selected_amount > total_sum) 
	{
		 redeemMyCertificates('apply_number='+number+'&apply_amount='+total_sum);
	}
	else if (selected_amount > total_amount) 
	{
		alert('Entered amount must be less then available amount!')
	}
	return false;
}



function addToCart(id, quantity)
{
    $("#cartIcon_"+id).effect(
			'spectrum', 
			{	dest: $("#shoppingCart") },
			1000,
			function(){
				addToCartActive(id, quantity);
			}
	);
}

function addToCartActive(id, quantity)
{
	$.ajax({
		type: "POST",
		url: "index.php",
		data: "a=2.001&action=add&id="+id+"&quantity="+quantity,
		dataType: "html",
		complete: function(){},
		success: function(data){
			if (data == "No ship to")
			{
				var url_ship = "index.php?a=2.17&id=" + id + "&quantity=" + quantity;
				ShowDialog(url_ship, 300, 'Ship To State', true);
			}
			else if (data == "Need New York")
			{
				var url_ship = "index.php?a=2.19";
				ShowDialog(url_ship, 300, 'Ship To State', false);
			}
			else if (data == "Gift Sets")
			{
				var url_ship = "index.php?a=2.20&id=" + id + "&quantity=" + quantity;
				ShowDialog(url_ship, 300, 'Ship To State', true);
			}
			else
			{
				document.getElementById('table_loaded_cart').rows[0].cells[0].innerHTML = data;
			}
		},
		error: function()
		{
			//alert('Export operation failed!');
		}
	});
}

function SendShippingState()
{
	var isSpirit = document.forms.shipping_state.isSpirits.value;
	if ( '-1' != isSpirit) 
	{
		var shippingState = document.forms.shipping_state.shipto_state.value;
		if ( 'N/A' == shippingState ) 
		{
			alert('Please select your Ship To state to calculate your shipping price!'); 
			document.getElementById('shipto_state').focus();
			return false;
		}
		
		if ( '1' == isSpirit && 'NY' != shippingState ) 
		{
			alert('We can only ship spirits to the state of New York!'); 
			document.forms.shipping_state.shipto_state.value = 'NY';
			document.getElementById('shipto_state').focus();
			return false;
		}
	}
	
	var params = $("#shipping_state").serialize();

	$.ajax({
		type: "POST",
		url: "index.php",
		data: params,
		dataType: "html",
		complete: function(){},
		success: function(data){
			document.getElementById('table_loaded_cart').rows[0].cells[0].innerHTML = data;
		},
		error: function()
		{
			//alert('Export operation failed!');
		}
	});
	return true;
}

function updateCartFromCart(id)
{
    var quantity = $("#updateCartFromCart_" + id).val();
	$.ajax({
		type: "POST",
		url: "index.php?a=2.001&action=update&id="+id+"&quantity="+quantity,
		dataType: "html",
		complete: function(){},
		success: function(data){ 
			document.getElementById('table_loaded_cart').rows[0].cells[0].innerHTML = data;
		},
		error: function()
		{
			//alert('Export operation failed!');
		}
	});
}

function removeFromCart(id)
{
	if (confirm_delete())
	{
		$.ajax({
			type: "POST",
			url: "index.php?a=2.011&id="+id,
			dataType: "html",
			complete: function(){},
			success: function(data){ 
				document.getElementById('table_loaded_cart').rows[0].cells[0].innerHTML = data;
			},
			error: function()
			{
				//alert('Export operation failed!');
			}
		});
	}
}

// #################################################################################

// Dialog window	
function ShowDialog(url, width, title, buttons_view)
{
	$('#modalContent').html("");
	$.ajax({
			type: "POST",
			url: url,
			dataType: "html",
			async: false,
			complete: function(){},
			success: function(data){ 
				$('#modalContent').html(data);
				
				var dlgParams = {
					autoOpen: false,
					bgiframe: true,
					draggable: false,
					title: title,
					modal: true,
					resizable: false,
					width: width
				};
				$('#modalContent').dialog(dlgParams);
				
				var dlgButtons = {};
				if (buttons_view) 
				{
					dlgButtons = { "Ok" : function() { 
							if (SendShippingState()) 
								$(this).dialog("close"); 
						}};
				}
				$('#modalContent').dialog('option', 'buttons', dlgButtons);
				
				$('#modalContent').dialog('open');
			},
			error: function()
			{
				//alert('Export operation failed!');
			}
	});
}


function joinMailing()
{
	var email = document.forms['joinMail'].email_address.value;
	if( !checkEmailAddress(email) ) {
		alert('Please, enter valid e-mail address');
	} else {
		alert('Thank you for your subscription to a newsletter!');
		document.forms['joinMail'].submit();
	}
}
			
function checkEmailAddress(emailStr)
{
	var re_check_email = new RegExp("[A-Za-z0-9_]+([-+.][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\\.[A-Za-z0-9_]{2,}([-.][A-Za-z0-9_]+)*");
	var ares = re_check_email.exec(emailStr);
	if( ares == null )
	{
	  return false;
	}
	return true;
}
				
