$(document).ready(function(){
	$("#contact").validate({
		rules:{
			email:{
				required:true,
				email:true
			},
			subject: {
				required:true
			},
			os:{
				required:true
			},
			username:{
				required:true
			},
			secure_code:{
				required:true
			},
			my_question:{
				required:true
			}
		},
		messages:{
			username:{
				required:"Name is required!"
			},
			email:{
				required:"Email is required!",
				email:"Invalid email address"
			},
			subject: {
				required: "Subject is required"
			},
			os: {
				required: "Operating System is required"
			},
			secure_code: {
				required: "Verification code is required"
			},
			my_question: {
				required: "Question content is required"
			}
		}
	});
		
	$("#affirmation").validate({
		rules:{
			name:{
				required:true
			},
			from:{
				required:true,
				email:true
			}
		}
	});
	
	$("select[@name='select']").change(function(){
		window.location.href="index.html"+$(this).val();
	});
	
	$("#submit").click(function(){
		$("#affirmation").submit();
	});
	
	
	$("#affirmation").submit(function(){
		var name = $("input[name='name']").val();
		var email = $("input[name='from']").val();
		if(name != '' && email != ''){
			$.ajax({
				type: "POST",
				url: "saveToBackend.php",
				data: "name="+name+"&email="+email
				//success: function(msg){
					//alert(msg);
					//if(msg.length < 60){
						//window.location = "readme.html";
					//}
				//}
			}); 
		}
	});
	
	$("#referral").validate({
		rules:{
			name:{
				required:true
			},
			email:{
				required:true,
				email:true
			},
			email1:{
				required:true,
				email:true
			}
		},
		messages:{
			name:{
				required:"Your name is required!"
			},
			email:{
				required:"Your email is required!",
				email:"Invalid email address"
			},
			email1:{
				required:"Email is required!",
				email:"Invalid email address"
			}
		}								 
										 
	});
	
	$("#login input[name='name']").click(function(){
		$(this).attr("value","");
	});
	
	//$("#login input").attr("class","email");
	//$("#login input:first").attr("class","required email");
	
	$("img[name='submit']").click(function(){
		$("form[name='referral']").submit();
	});

	$("#login").validate({
		rules:{
			password:{
				required:true
			}
		}
	});
	
	$("#change").validate({
		rules:{
			old:{
				required:true,
				minlength:5,
				remote:"checkpass.php"
			},
			newpass:{
				required:true,
				minlength:5	
			},
			confirm:{
				required:true,
				minlength:5,
				equalTo: "#newpass"
			}
		},
		messages:{
			old:{
				remote:jQuery.format("Old Password Error!")
			},
			confirm:{
				equalTo:"Please enter same value with New Password"
			}
		}
	});
	
	$("#trial").validate({
		rules:{
			name:{
				required:true
			},
			from:{
				required:true,
				email:true,
				remote:"checkemail.php"
			},
			pass:{
				required:true
			},
			pass_repeat:{
				required:true,
				equalTo:"#pass"
			}
		},
		messages:{
			from:{
				remote:jQuery.format("{0} have already in use")
			}
		}
	});

	var rotate_img_interval_counter = 0;
	setInterval(function(nav){
		if(rotate_img_interval_counter == 3){
			rotate_img_interval_counter = 0;	
		}
						 
/*		var img = new Array("DrGaryCasaccio2.jpg","nikki_stone_small.jpg", "JohnDaly.jpg"); */		var img = new Array("DrGaryCasaccio2.jpg","cropped.jpg", "JohnDaly.jpg");
/*		var href = new Array("Dr. Gary Casaccio","Olympic Gold Medalist---Nikki Stone", "John Daly"); */		var href = new Array("Dr. Gary Casaccio","Shae Stover", "John Daly");
		var desp = new Array("Being a psychiatrist for the past 16 years, I have to say that MindMaster is a real breakthrough in the area of personal development.",/*
							 "MindMaster is something that I highly recommend to all those who are trying to achieve goals, build confidence and self esteem, or create success in any area of their life.  This program will help you harness the powers of your own mind and achieve goals you never thought possible",*/
							 "MindMaster has helped me overcome so many personal issues and insecurities many of which began during my childhood. I have been able to let go of self destructive patterns of behavior that had shadowed my life for more than 30 years.",							 "I feel very strongly that MindMaster has helped me with my anger and depression. Because of MindMaster I have become a calmer person. I have not had a bout of anger in 6 months. My depression is no longer a burden.");
/*		var readmoreLink = new Array("success.html#index_1","success.html#nikki_stone", "success.html#index_2");*/		/*var readmoreLink = new Array("success.html#index_1","success.html#index_3", "success.html#index_2");*/
var readmoreLink = new Array("success.html","success.html", "success.html");
		
		$("#repeatImg").attr("src","images/table/"+img[rotate_img_interval_counter]);
		$("#repeatPos").text(href[rotate_img_interval_counter]);
		$("#repeatDesp").text(desp[rotate_img_interval_counter]);
		$("#repeatReadmoreLink").attr("href", readmoreLink[rotate_img_interval_counter]);
		
		rotate_img_interval_counter++;
		
	},3000);
	rotate_img_interval_counter = 0;
	
});
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function redirect(){
    var id;
    id = document.getElementById('affirmation_cat').value;
    window.location.href = "affirmation-"+id+".html";
}

function jump_to_location_on_page(){
	var id = $("#jump_id").val();
	if(id > 0){
		window.location.href="index.html#"+id;
	}
}

//for hide and shoe shopping cart elements
function cart_proceed_action(){
	$("#cart_title_div").toggle();
	$("#cart_table").toggle();
	$("#billing_form").toggle();
	$("#cart_buttons_table").toggle();	
    $("#direct_paypal_button").toggle();
}

//function to update busket item to remove
function update_basket(){
	if($("#mm_product_quantity").val() != $("#mm_product_quantity_original").val()){	
		//to update the mm product's quantity
		var new_quantity = $("#mm_product_quantity").val();
		$.post('checkout_add_process.php', {task:'update_mm_quantity', q:new_quantity}, function(data) {
			if(data == 'success'){
				//alert("Quantity updated successfully");
				window.location.href="checkout.php?change";
			}
		});		
	}
	
	//remove mp3 items
	$(".remove_cart_item").each(function(i){
		var status = $(this).attr("checked");
		if(status){
			var id_name = $(this).attr('id');
			var id_name_arr = id_name.split('_');
			var id = id_name_arr[1];
			$.post('checkout_add_process.php', {task:'remove_item', item_id:id}, function(data) {
				if(data == 'success'){
					window.location.href="checkout.php?remove_item="+id;
				}
			});		
		}
	});
	
}

//if selected us as the country show the us state selection box
function toggleStateDD() {
	var country = $("#country").val();
	if(country == 'US'){
		$("#stateUS").show();
		$("#state").hide();	
		$("#state_required_id").show();
		$("#zip_required_id").show();
	}else{
		$("#state").val('');
		$("#zip").val('');
		
		$("#stateUS").hide();
		$("#state").show();	
		
		if(country == 'CA' || country == 'GB'){
			$("#state_required_id").show();
		}else{
			$("#state_required_id").hide();
		}
		
		$("#zip_required_id").hide();
	}
}

//function used for temporarily hide the remove checkboxes
/*function check_removal_options(original_quantity){
	if($("#mm_product_quantity").val() != original_quantity){
		$(".remove_cart_item").hide();
	}else{
		$(".remove_cart_item").show();	
	}
}*/