jQuery(document).ready(function() {
    
    //Hide login form
	$(".login-form").hide();
	$(".login-reset").hide();
	$(".login-link a").click(function(){
		$(".login-form").show();
		$(".login-link").hide();
		$(".login-reset").show();
		return false;
	});
	
	//Show/hide payment
	$(".how-worldpay").click(function(){
		$("#cardinfo").hide();
		//$('input#myCoupon').trigger('blur');
		calculateDiscount();
	});
	
	$(".how-card").click(function(){
		$("#cardinfo").show();
		//$('input#myCoupon').trigger('blur');
		calculateDiscount();
	});
	
	
	
	$('div.boxes').hide();
	
	// try to capture pre loaded coupons
	calculateDiscount();

  	$("input#myCoupon").blur(function(event) {
		event.preventDefault();
		
		$('a[name=modal]').trigger('click');
	});
  
  
  	 //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        
        calculateDiscount();
        	
        if ($('input#myCoupon').val() != '') {
		
			 //Get the A tag
	        var id = $(this).attr('href');
	     
	        //Get the screen height and width
	        var maskHeight = $(document).height();
	        var maskWidth = $(window).width();
	     
	        //Set height and width to mask to fill up the whole screen
	        $('#mask').css({'width':maskWidth,'height':maskHeight});
	         
	        //transition effect     
	        $('#mask').fadeIn(1000);    
	        $('#mask').fadeTo("slow",0.8);  
	     
	        //Get the window height and width
	        var winH = $(window).height();
	        var winW = $(window).width();
	               
	        //Set the popup window to center
	        $(id).css('top',  winH/2-$(id).height()/2);
	        $(id).css('left', winW/2-$(id).width()/2);
	        
	        //transition effect
        	$(id).fadeIn(2000); 
        
        }
    });
     
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
        
        //$('input#myCoupon').trigger('blur');

    });     
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
        
        //$('input#myCoupon').trigger('blur');

    });  
    
		
	function calculateDiscount() {
    
    	// send request
		$.post("/ajax/is_valid_coupon.php", { coupon: $("input#myCoupon").val(), 
									email: $("input#email").val(), 
									myplan: $("input#planid").val(),
									deal: $("input#deal").val(), 
									paytype: $("input[@name=pay_by]:checked").val()}, function(xml) {
		    
		    // set price
		    $('span.couponRate').html($("amount", xml).text());
	     	
			// format and output result
			if ($("error", xml).text()  == 1) {
						
				$("#couponInfo").html("<span style='color:red;'>" + $("msg", xml).text() + "</span>");
				$('input#myCoupon').val('');
				return false;
			}
			else {
				$("#couponInfo").html("<span style='color:green;'>" + $("msg", xml).text() + "</span>");
				return true;
			}
			
			
		});
    }  
	
});


