// JavaScript Document
/*This file collects and verifies the information entered in the fields*/
$(document).ready(function() {
		/*when the proceed button is clicked the function below is called*/
		$('#proceed_btn').click(function(){
										 $('#first_step').hide();
										 var cert_amount = $.trim($('#cert_amount').val());
										 var shipping = $("input[@name=shipping]:checked").val();
										 var opening_msg = $.trim($('#opening_msg').val());
										 var msg_body = $.trim($('#msg_body').val());
										 var closing_msg = $.trim($('#closing_msg').val());
										 var location_opt = $("#location_opt").val();
										 if(cert_amount.length<1 || opening_msg.length<2 || msg_body.length<3 || closing_msg.length<1 || location_opt==""){
											 /*if the fields have less that 1 character alert*/
											 //
											 $('#first_step').html('Please make sure you have enter all details !');
											 //$('#first_step').html(location_opt);
											 $('#first_step').fadeIn();
											 return false;											 
											 }else{
												 /*all fields have been filled so trigger the link to show pop up*/
												 $("#second_form_pop_link").delay(300).queue(function(){ 
																							$(this).trigger("click"); 
																							$(this).dequeue(); 
																							});
												 $('#first_step').html('');
												 }
										 });
		
		/*mailing same as billing info ... if the user clicks on the check box we check if the check box is enable or disabled*/
		$('#same_info').click(function(){
			if($('#same_info').is(':checked')){
				/*checkbox is checked so we copy the values in the mailing fields to the billing fields*/
				$('#bill_f_name').val($('#f_name').val()); 
				$('#bill_l_name').val($('#l_name').val());
				$('#bill_street_address').val($('#street_address').val());
				$('#bill_city').val($('#city').val());
				$('#bill_state').val($('#state').val());
				$('#bill_zip').val($('#zip').val());
				} else{
					/*Check box unchecked to will empty the billing fields*/
					$('#bill_f_name').val('');
					$('#bill_l_name').val('');
					$('#bill_street_address').val('');
					$('#bill_city').val('');
					$('#bill_state').val('');
					$('#bill_zip').val('');
					}
			});
		
/*when the submit info button is clicked the function below is called*/
$('#submit_info').click(function(){
	$('#sending_email_status').hide(); /*hide the notification text below the button at the inital click*/
	/*we capture all the values from the fields in the pop up form and the previous form*/				
	var cert_amount = $.trim($('#cert_amount').val());
	var shipping = $("input[@name=shipping]:checked").val();
	var opening_msg = $.trim($('#opening_msg').val());
	var msg_body = $.trim($('#msg_body').val());
	var closing_msg = $.trim($('#closing_msg').val());
	
	var f_name = $.trim($('#f_name').val());
	var l_name = $.trim($('#l_name').val());
	var street_address = $.trim($('#street_address').val());
	var city = $.trim($('#city').val());
	var state = $.trim($('#state').val());
	var zip = $.trim($('#zip').val());
	var location_opt = $("#location_opt").val();
	
	var bill_f_name = $.trim($('#bill_f_name').val());
	var bill_l_name = $.trim($('#bill_l_name').val());
	var bill_street_address = $.trim($('#bill_street_address').val());
	var bill_city = $.trim($('#bill_city').val());
	var bill_state = $.trim($('#bill_state').val());
	var bill_zip = $.trim($('#bill_zip').val());
	var bill_work_phone = $.trim($('#bill_work_phone').val());
	var bill_home_phone = $.trim($('#bill_home_phone').val());
	var bill_email = $.trim($('#bill_email').val());
	var credit_card = $.trim($('#credit_card').val());
	var cardholder_name = $.trim($('#cardholder_name').val());
	var card_number = $.trim($('#card_number').val());
	var card_expiration = $.trim($('#card_expiration').val());
	
	/*Check if the fields have more than one value....if any has less than one meaning 0 we alert*/
	if(f_name.length<1 || l_name.length<1 || street_address.length<1 || city.length<1 || state.length<1 || zip.length<1 ||  bill_f_name.length<1 || bill_l_name.length<1 ||  bill_street_address.length<1 || bill_city.length<1 || bill_state.length<1 || bill_zip.length<1 || bill_email.length<3 || cardholder_name.length<1 || card_number.length<1 ||  card_expiration.length<1 ){
		/*One of the fields has contains nothing*/
		$('#sending_email_status').html('Please fill in all the important details !');
		$('#sending_email_status').fadeIn();
		}else{
			/*All details are ready to be sent. So we post to the send_info.php script*/
			$('#sending_email_status').html('Sending your info...please wait !');
			$('#sending_email_status').fadeIn();
			$.post(
				   'send_info.php',
				   {cert_amount:cert_amount,shipping:shipping,opening_msg:opening_msg,msg_body:msg_body,closing_msg:closing_msg,f_name:f_name,l_name:l_name,street_address:street_address,city:city,state:state,zip:zip,bill_f_name:bill_f_name, location_opt:location_opt, bill_l_name:bill_l_name, bill_street_address:bill_street_address, bill_city:bill_city,bill_state:bill_state, bill_zip:bill_zip, bill_work_phone:bill_work_phone, bill_home_phone:bill_home_phone, bill_email:bill_email, credit_card:credit_card,  cardholder_name:cardholder_name, card_number:card_number, card_expiration:card_expiration},
				   function(send_email){/*this function waits for the reply from the send_info.php file*/
					   if(send_email == 'Message sent!'){
						   /*send_info.php posted back notifying that the mail was sent*/
						   $('#sending_email_status').html('Your Info was Sent successfully !');
						   alert ('Thank you for the purchasing a Gift Certificate from us !');
						   window.location.reload(true);
						   }else{
							   /*Reply was not Message sent so we put it on the notification*/
							   $('#sending_email_status').html(send_email);
							   }
					   }
				   );
			}
	
	
	
	});
		
		
		
		
		$(".already").delay(2000).fancybox({
							   /*THis function is the one that is triggered to make the pop up window show*/
							   'titlePosition': 'inside',
							   'speedIn': '200',
							   'transitionIn': 'fade',
							   'transitionOut': 'none'
							   });

		
		
		
						   
						   });
