// JavaScript Document

$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};

$(document).ready(function() {
	
	// bind to the form's submit event 
    $('.send_cc_form').click(function() {

		function showResponse() {
			$("#edit_cc_box").slideUp("normal");
		};
    	
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        
        var options = { 
	        target:        '#lastfour',   // target element(s) to be updated with server response 
	       // beforeSubmit:  showRequest(message_id),  // pre-submit callback 
	       success:       showResponse,  // post-submit callback 
	 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        type:      'post'        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
	        //clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    };
        
        $('#update_cc_form').ajaxSubmit(options); 
 
        // !!! Important, always return false to prevent standard browser submit and page navigation 
        return false; 
    });

	$(".auto_submit").change(function() {
		this.form.submit();
	});
	
	$("#country_go_button").click(function() {
		this.form.submit();
	});
	
	$("#coupon_code_button").click(function() {
		var coupon = $("#coupon_code").val();
		coupon = encodeURI(coupon);
		$("#coupon_box").load("https://globalcellularrental.com/index.php/order/validate_coupon/"+coupon);
	});
	
	$("#edit_cc_toggle").click(function() {
		$("#edit_cc_box").slideToggle("fast");
		//return false;
	});
	
	
	var timer;

	function OnMouseHover0(event)
	{
	     $("#main_navigation li ul").css("visibility","hidden");
	     $("#subNav0").css("visibility","visible");
	}
	
	function OnMouseOut0(event)
	{
	     $("#subNav0").css("visibility","hidden");
	}
	
	$('#trigger0').bind('mouseover', OnMouseHover0);
	
	$('#trigger0').bind('mouseout', OnMouseOut0);
	
	$(".change_plan").change(function() {
		var confirmblock = confirm("Are you sure you want to change to this plan? Any form data will be lost.");
		if(confirmblock)
		{
			this.form.submit();	
		}
		else
		{
			return false;
		}		
	});	
	
	$(".change_country").change(function() {		
		var confirmblock = confirm("Are you sure you want to change the primary country that you are visiting?  Any form data will be lost.");
		if(confirmblock)
		{
			window.location.href = site_url+this.value;
		}
		else
		{
			return false;
		}		
	});
	
	$(".ship_to_address_trig").click(function() {
		$("#ship_to_address").slideToggle("normal");
		$("#ship_trigger").slideUp("normal");
	});

	
	$(".dismiss").click(function() {
		$(this.parentNode).slideUp("normal");
		return false;
	});
	
	$(".up_photo_trigger").click(function() {
		$("#up_photo").slideToggle("normal");
		return false;
	});
	
	$("#form_upload_button").click(function() {
		$("#form_upload_button").hide();
		$(this.parentNode).append('<div class="form_working">Loading ...</div>');
		
	});
	
	$(".delete_check_fix_inventory").click(function() {
		var confirmblock = confirm("DELETE PHONE FROM ORDER AND RETURN PHONE TO IN STATUS. Are you sure you want to do this?");
		if(confirmblock) {} else { return false; }
	});
	
	$(".delete_check_ignore_inventory").click(function() {
		var confirmblock = confirm("DELETE PHONE FROM ORDER AND LEAVE PHONE STATUS AS IT IS. Are you sure you want to do this?");
		if(confirmblock) {} else { return false; }
	});
	
	$(".delete_check").click(function() {
		var confirmblock = confirm("Are you sure you want to delete this?");
		if(confirmblock) {} else { return false; }
	});
	
	$(".logout_check").click(function() {
		var confirmblock = confirm("Are you sure you want to logout?");
		if(confirmblock) {} else { return false; }
	});
	
	$("#quick_login").click(function() {
		$("#login_form").fadeIn("fast");
		return false;
	});
	
	$("#login_win_close").click(function() {
		$("#login_form").fadeOut("fast");
	});
	

});
