// JavaScript Document - Providore

jQuery(document).ready(function($) {
	//Function for slideshow on the homepage
	if($('#homeRotatorWrap').length >= 0){
		$('.homeIndvRotate').cycle({
			fx: 'scrollHorz',
			next: '#homeRotatorNext',
			prev: '#homeRotatorPrev',
			timeout: 7000
		});
	}
	
	//Function for clearing the text on email form and checking fields for submit
	var isValid = false;
	if($('#platomailName').length >= 0){
		//Clears the name field
		$('#platomailName').focus(function() {
			if($(this).val() == 'Name'){
				$(this).val('');
			}
		});
		// Fills the name field if nothing is entered
		$('#platomailName').blur(function() {
			if($(this).val() == ''){
				$(this).val('Name');
			}
			checkValidInputs();
		});
		
		//Clears the email field
		$('#platomailEmail').focus(function() {
			if($(this).val() == 'Email'){
				$(this).val('');
			}
		});
		// Fills the email field if nothing is entered
		$('#platomailEmail').blur(function() {
			if($(this).val() == ''){
				$(this).val('Email');
			}
			checkValidInputs();
		});
		
		// Checks if the fields are valid		
		function checkValidInputs(){
			if($('#platomailName').val() != '' && $('#platomailName').val() != 'Name'){
				if($('#platomailEmail').val() != '' && $('#platomailEmail').val() != 'Email'){
					isValid = true;
				} else {
					isValid = false;
				}
			}	
		}
		
		//Check when form submitted
		$("#subForm").submit(function() {
			checkValidInputs();
			return isValid;
		});
	}
	
	//Function for checking if fields are filled out on product page
	if($('#text_fields textarea').length > 0){
		var attributesValid = false;
		var required = false;
		
		function checkProductAttributes(){
			// Checks if the function has already run			
			if($("#text_fields").attr('class') != "checkedAlready"){
				attributesValid = true;
				//checks if they have been filled out already
				if(!$("#text_fields li textarea").val()){
					attributesValid = false;
				}
				$("#text_fields").addClass("checkedAlready");	
			} else {
				attributesValid = true;
			}
			
			//Checks if the field is required or not
			if($("#text_fields li.required").length > 0){
				if(!$("#text_fields li.required").children("textarea").val()){
					attributesValid = false;
					required = true;
				}
			} else {
				required = false;		
			}
		}
		
		$("#buy_block").submit(function() {
			checkProductAttributes();
			if(attributesValid == false){
				if(required == false){
					alert("Please enter your personal message and delivery address that you would like the product to be delivered to. \n\nIf you do not require a personal message or a seperate delivery address please just leave these blank and add the product to your cart again.");
				} else {
					alert("Please enter your personal message and delivery address that you would like the product to be delivered to.");
				}
			}
			return attributesValid;
		});
	}
});
