// Preload Images
img1 = new Image(16, 16);  
img1.src="../../images/spinner.gif";

img2 = new Image(220, 19);  
img2.src="../../images/ajax-loader.gif";

// When DOM is ready
$(document).ready(function(){
	
	var default_keywords = $('#inputString').val();
	
	$('#topSearch > form').submit(function() {
		var search_keywords = $('#inputString').val();
		
		// Don't let users search for blank strings or default homepage search keywords
		if (search_keywords == '' || search_keywords.toLowerCase().indexOf('search by keyword or brand') != -1) {
			alert("Please enter some search keywords!");
			$('#search_keywords').val(default_keywords);
			return false;
		}


	});
	
	$('#inputString').click(function() {
		search_keywords = $(this).val();
		if (search_keywords == default_keywords) {
			$(this).val('');
		} 
	});

	$('#inputString').blur(function() {
		search_keywords = $(this).val();
		if (search_keywords == '') {
			$(this).val(default_keywords);
		}
	});
	
	$('#mainSearch > form').submit(function() {
		var search_keywords = $('#searchText').val();
		
		// Don't let users search for blank strings or default homepage search keywords
		if (search_keywords == '' || search_keywords.toLowerCase().indexOf('search by keyword or brand') != -1) {
			alert("Please enter some search keywords!");
			$('#search_keywords').val(default_keywords);
			return false;
		}


	});
	
	$('#searchText').click(function() {
		search_keywords = $(this).val();
		if (search_keywords == default_keywords) {
			$(this).val('');
		} 
	});

	$('#searchText').blur(function() {
		search_keywords = $(this).val();
		if (search_keywords == '') {
			$(this).val(default_keywords);
		}
	});
	
	// Launch Discount Code Panel if the link is clicked
	$("#discount_link").click(function(){
		$('#discount_link').hide();		
		$('#discount_panel').show();
	});
	
	// When the discount form is submitted
	$("#discount_panel > form").submit(function(){
		
		// 'this' refers to the current submitted form  
		var str = $(this).serialize(); 
		
		// -- Start AJAX Call --
		$.ajax({  
		    type: "POST",
		    url: "process_discount.php",  // Send the discount info to this page
		    data: str,  
		    success: function(msg){  
			
			$("#discount_panel").ajaxComplete(function(event, request, settings){  
				 var obj = JSON.parse(msg);
				 if(obj.Error == '-1') // Invalid Discount Code
				 {  
					 var err_response = "<span class='sorryMsg'>Sorry - The discount you entered is not valid!</span>";	 
					 $('#discountTable').html(err_response); // Refers to 'status'
					 $('#discountTable').show();
					 $('#discount_panel').hide();
				 }  
				 else // Valid Discount Code?
				 {  
					 var err_response = "<span class='successMsg'>" + obj.Discount + "% Discount applied - " + obj.Description + "</span>";
					 $('#totalEuroDiscount').html("- &euro;" + obj.Amt);
					 $('#totalEuro').html("&euro;" + obj.OrderValue);
					 $("#paymentPanel input#pay_amount").val(obj.OrderValue);
					 $('#discountTable').html(err_response); // Refers to 'status'
					 $('#discountTable').show();
					 $('#discount_panel').hide();
				 }  
				      
				 });  
				   
				 }  
				   
				  });  
				  
				// -- End AJAX Call --
	
		return false;
	});
	
	//terms and conditions popup window
	$("#termsLink").click(function(){	
		$('#terms_and_conditions').modal();
	});	
	
	// Launch MODAL BOX if the Login Link is clicked
	$("#feedback_link").click(function(){
		$('#ajax_loading').hide();		
		$('#info_form').modal();
	});
	
	// When the form is submitted
	$("#status > form").submit(function(){  
	
		// Hide 'Submit' Button
		//$('#feedback-details').hide();
	
		// Show Gif Spinning Rotator
		$('#ajax_loading').show();
		
		// 'this' refers to the current submitted form  
		var str = $(this).serialize(); 
		
		// -- Start AJAX Call --

		$.ajax({  
		    type: "POST",
		    url: "send_feedback.php",  // Send the login info to this page
		    data: str,  
		    success: function(msg){  
			
			$("#status").ajaxComplete(function(event, request, settings){  
				 
				 // Show 'Submit' Button
				$('#feedback-details').show();

				// Hide Gif Spinning Rotator
				$('#ajax_loading').hide();  

				 if(msg == 'OK') // LOGIN OK?
				 {  
				 var login_response = '<div id="notification_success">' +
					 "Thank You <br /> We have received your comments.</div>";  

				$('a.modalCloseImg').hide();  

				$('#simplemodal-container').css("width","500px");
				$('#simplemodal-container').css("height","110px");
				 
				$(this).html(login_response); // Refers to 'status'
				 
				// After 3 seconds redirect the
				setTimeout('$(\'#simplemodal-container\').remove();$(\'#simplemodal-overlay\').remove();', 3000);

				 }  
				 else // ERROR?
				 {  
				 var login_response = msg;
				 $('#login_response').html(login_response);
				 }  
				      
				 });  
				   
				 }  
				   
				  });  
				  
				// -- End AJAX Call --

	
		return false;
	
	}); // end submit event

});

