$(document).ready(function() {
//Lucky CODE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Lucky CODE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Lucky CODE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$('.callMe').click(function(){
							
							$.post('php/buttons_model.php',{q:'callMe',name:$("input[name='txt_name']").val(), tel:$("input[name='txt_telephone']").val()},function(data)
																																									  {
																																										 
																																									  });
							});
$('.send').click(function(){
						  if($("input[name='email']").val() == '' && $("input[name='phone']").val() == '')
						  	alert('Please enter email or phone.');
						else
						{
									   $.post('php/buttons_model.php',{q:'contact',name:$("input[name='name']").val(), email:$("input[name='email']").val(),phone:$("input[name='phone']").val(),type:$("select[name='sel_type']").val(),msg:$("textarea[name='message']").val()},function(data)
																											{
									   
																											   $('#email_middle').html('<div id="message" style=" height:468px;"><h2 class="font_arial_bold font_blueish font_26">Thank you!</h2><h3 class="font_sansation_bold font_blueish font_20">Your message has been sent. We aim to reply to all messages the same or next business day.</h3>.</div>');
																											});
						}
									 });
	$('.when').change(function(){
							   var to = $("select[name='sel_ship_to']").val();
							   var via = $("select[name='sel_ship_via']").val();
							   
							   if(to != 0)
							   {
									$.post('php/buttons_model.php',{q:'delivery',to:to,via:via},function(data)
																										 {
																											 
																											$('#whenResult').text(data);
																										 });
							   }
							   });
	
	$('.ship').change(function(){
							   		var qty = $("input[name='quantity']").val();
									qty = qty.replace(",","");
									var ship_to = $("select[name='ship_to']").val();
									
									
									if(ship_to != 0)
									{
									if(qty > 24)
									{
									$.post('php/buttons_model.php',{q:'quoteTotal',
										   							qty:qty,
																	ship_to : $("select[name='ship_to']").val(),
																	ship_via : $("select[name='ship_via']").val(),
																	shape: $("select[name='shape']").val()
																	},function(data)
																				{
																					
																					obj = JSON.parse(data);
																					$('#total').text('$'+obj.total);
																					$('#tax').text('$'+obj.tax);
																					$('#subTotal').text('$'+obj.price);
																					$('#shipCost').text('$'+obj.shipCost);
																					$('#price_each').val('$'+obj.price_each);
																					
																
																				});
									}
									else
										alert('Enter minimum quantity of 25.');
									}
							   });
		$('.ship').keyup(function(){
							   		var qty = $("input[name='quantity']").val();
									var ship_to = $("select[name='ship_to']").val();
									qty = qty.replace(",","");
									if(ship_to != 0)
									{
									if(qty > 24)
									{
									$.post('php/buttons_model.php',{q:'quoteTotal',
										   							qty:qty,
																	ship_to : $("select[name='ship_to']").val(),
																	ship_via : $("select[name='ship_via']").val(),
																	shape: $("select[name='shape']").val()
																	},function(data)
																				{
																					
																					obj = JSON.parse(data);
																					$('#total').text('$'+obj.total);
																					$('#tax').text('$'+obj.tax);
																					$('#subTotal').text('$'+obj.price);
																					$('#shipCost').text('$'+obj.shipCost);
																					$('#price_each').val('$'+obj.price_each);
																					
																
																				});
									}
									}
									
							   });
	$('.sendQuote').click(function(){
								   var quoteID;
								   if(validateEmail($("input[name='txt_email']").val()))
									{
										$.post('php/buttons_model.php',{q:'recordQuote',
															qty: $("input[name='quantity']").val(),
														   shape: $("select[name='shape']").val(),
														   to: $("select[name='ship_to']").val(),
															via: $("select[name='ship_via']").val(),
															total: $('#total').text(),
															email_to: $("input[name='txt_email']").val()
															},function(data)
															{	
																
																obj = JSON.parse(data);
																quoteID=obj.quoteID;
																
																$.post('php/quote.php',{q:'sendQuote',
										  				 							  qty: $("input[name='quantity']").val(),
														 							  shape: $("select[name='shape']").val(),
														  							  to: $("select[name='ship_to']").val(),
																					  via: $("select[name='ship_via']").val(),
																					  subTotal: $('#subTotal').text(),
																					  shipCost: $('#shipCost').text(),
																					  tax: $('#tax').text(),
																					  total: $('#total').text(),
																					  price_each: $('#price_each').val(),
																					  quoteID: quoteID,
																					  email_to: $("input[name='txt_email']").val()
										  											 },function(data)
																  								{																			
																								});
															});
															
								   		
																   
									}
									else
										alert('Invalid Email.');
								   });
	
	$('#survey').click(function(){
								
								var ans1 = $('textarea[name="ans1"]').val();
								var ans2 = $('textarea[name="ans2"]').val();
								var ans3 = $('textarea[name="ans3"]').val();
								var ans4 = $('textarea[name="ans4"]').val();

								$.post('php/buttons_model.php',{q:'survey', ans1 : ans1, ans2: ans2, ans3: ans3, ans4: ans4},function(data)
																																	  {
																																		  
																																	  });

		
								});

//End Lucky CODE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//End Lucky CODE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//End Lucky CODE//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$(function(){ 
			    // find all the input elements with title attributes
				$('input[title!=""]').hint();
			});

	//Set Default State of each portfolio piece
	$(".paging").show();
	$(".paging a:first").addClass("active");
	//Get size of images, how many there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	//Paging + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$(".paging a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$(".image_reel").animate({ 
			left: -image_reelPosition
		}, 500 );
		
	}; 
	
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('.paging a.active').next();
			if ( $active.length === 0) { //If paging reaches the end...
				$active = $('.paging a:first'); //go back to first
			}
			rotate(); //Trigger the paging and slider function
		}, 7000); //Timer speed in milliseconds (7 seconds)
	};
	
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$(".image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	
	
	//On Click
	$(".paging a").click(function() {	
		$active = $(this); //Activate the clicked paging
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		rotateSwitch(); // Resume rotation
		return false; //Prevent browser jump to link anchor
	});	
	
/////////////////////////////////////////////////////////////////////////////////////////////
/*									Code Change Starts here.								*/
	
/////////////////////////////////////////////////////////////////////////////////////////////	
	
	//When you click on a link with class of poplight and the href starts with a # 
	$('a.poplight[href^=#]').click(function() {
		var aID = $(this).attr('id'); //Get Popup Name
		//alert(aID);
		var flag=0;
		if(aID=='email')
		{
			$('.error').hide();
			var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/; 
			var emailaddressVal = $("#txt_email").val();
			//alert(emailaddressVal);
			if(emailaddressVal == '') {
				$("#email").after('<span class="error">Please enter your email address.</span>');
				hasError = true;
				flag=1;
			}
			else if(!emailReg.test(emailaddressVal)) {
				$("#email").after('<span class="error">Enter a valid email address.</span>');
				hasError = true;
				flag=1;
			}
		}
		if(aID=='survey')
		{
			$('.error').hide();
			if($('textarea[name="ans1"]').val() == '' || $('textarea[name="ans2"]').val() == '' || $('textarea[name="ans3"]').val() == '' || $('textarea[name="ans4"]').val() == ''	)
			{
			$("#survey").after('<span class="error">Please answer all questions.</span>');
				hasError = true;
				flag=1;	
			}
		}
		
		if(aID=='quote')
		{
			$('.error').hide();
			if($('input[name="quantity"]').val() == '' || $('select[name="ship_to"]').val() == 0 	)
			{
			$("#quote").after('<span class="error">Please enter Quantity & Ship To.</span>').css('margin-bottom','-2px');
				hasError = true;
				flag=1;	
			}
		}
		
		if(aID=='contact')
		{
			$('.error').hide();
			if($('input[name="txt_name"]').val() == '' || $('input[name="txt_telephone"]').val() == '' 	)
			{
			$("#contact").after('<span class="error">Please enter Name & Telephone.</span>');
				hasError = true;
				flag=1;	
			}
		}
		
		if(flag==0)
		{
		
		$('#fade , .popup_block').fadeOut(function() {
		});
		var popID = $(this).attr('rel'); //Get Popup Name
		var popURL = $(this).attr('href'); //Get Popup href to define size
				
		//Pull Query & Variables from href URL
		var query= popURL.split('?');
		var dim= query[1].split('&');
		var popWidth = dim[0].split('=')[1]; //Gets the first query string value

		//Fade in the Popup and add close button
		$('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
		
		//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		var popMargTop = ($('#' + popID).height() + 80) / 2;
		var popMargLeft = ($('#' + popID).width() + 80) / 2;
		
		//Apply Margin to Popup
		$('#' + popID).css({ 
			'margin-top' : -popMargTop,
			'margin-left' : -popMargLeft
		});
		
		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
		$('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
		
		return false;
		}
	});
	
/////////////////////////////////////////////////////////////////////////////////////////////
/*									Code Changed upto Here									*/
	
/////////////////////////////////////////////////////////////////////////////////////////////	
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	  	$('#fade , .popup_block').fadeOut(function() {
			$('#fade').remove();  
	}); //fade them both out
		
		return false;
	});
	
	
});

function validateEmail($email) {

    var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;

    if( !emailReg.test( $email ) ) {

        return false;

    } else {

        return true;

    }

}

