//Javascript
function check_outlet_choice(){
	$("input[name='all_outlets']").each(function(){											 
		if($(this).is(":checked")){		
			if($(this).val() == "0"){
				$("#outlets_box").removeClass("box_notselected");
				$("#outlets_box").find("input").each(function(){
					$(this).removeAttr("disabled");					
				});
			} else {				
				$("#outlets_box").addClass("box_notselected");
				$("#outlets_box").find("input").each(function(){
					$(this).attr("disabled","disabled");
				});
			}
		}
	});	
	$("input[name='all_outlets']").click(function(){											 
		if($(this).is(":checked")){		
			if($(this).val() == "0"){
				$("#outlets_box").removeClass("box_notselected");
				$("#outlets_box").find("input").each(function(){
					$(this).removeAttr("disabled");					
				});
			} else {				
				$("#outlets_box").addClass("box_notselected");
				$("#outlets_box").find("input").each(function(){
					$(this).attr("disabled","disabled");
				});
			}
		}
	});	
	
	$("#outlets_recording").submit(function(){
		$("input[name='all_outlets']:checked").each(function(){			
			if($(this).val() == "0"){
				if($("#outlets_box input:checked").length == 0){
					alert("Please select at least one of your outlets");
					return false;
				} else {
					$("#outlets_recording").submit();			
				}
			} else {
				$("#outlets_recording").submit();	
			}												 
		});		
		return false;
	});
}

function check_country(){
	$("#business_country").change(function(){		
		$("#business_county").val("other");
	});
}


function carboncopy(){	
	var original_fields = new Array("applicant_name","applicant_phone_prefix","applicant_phone_local","applicant_phone_number","applicant_email");
	var dest_fields = new Array("marketing_name","marketing_phone_prefix","marketing_phone_local","marketing_phone_number","marketing_email");	
	$("#same_above").click(function(){
		if($(this).is(":checked")){			
			//check if one of the fields listed above are empty
			for($i = 0; $i < original_fields.length; $i++){
				if($("#"+original_fields[$i]).val() != ""){
					$("#"+dest_fields[$i]).val($("#"+original_fields[$i]).val());
				}//end if
			}//end for $i			
		} else {
			for($i = 0; $i < dest_fields.length; $i++){				
				$("#"+dest_fields[$i]).val("");			
			}//end for $i			
		}//end if
	});	//end click
}

function check_delete(){
	$("a[rel=delete]").click(function(){
		var href = $(this).attr("href");
		
		$.prompt('Are you sure to delete this element?',{ top: "40%",buttons: { Ok: true, Cancel: false }, submit:function(v,m){ 
			//location.href = href; 
				if(v){
					location.href = href; 
				}
			} 
		});

		/*if(confirm("Are you sure to delete this element?")){
			location.href = href;	
		}//end if*/
		return false;
	});	
}

function check_map_selection(){
	$("#setmap").submit(function(){
		if($("#field_lat").val() != ""){
			return true;
		} else {
			$.prompt('Please specify the position of your outlet from the map',{ top: "40%"});
			return false;	
		}//end if
	});
};

function check_passwords(){	
	$("#changepwd_form").submit(function(){
		//check if the password is empty
		if($("#password_change").val() == ""){
			$.prompt('Please enter your new password',{ top: "40%"});
			return false;
		}
		//check if the passwords are the same		
		if($("#password_change").val() != $("#conf_password_change").val()){
			$.prompt('The two passwords don\t match',{ top: "40%"});
			return false;
		}//end if
		//check if the passwords are too short
		if($("#password_change").val().length < 6){
			$.prompt('The password must be at least 6 characters long',{ top: "40%"});
			return false;
		}//end if
		return true;
	});
}

function check_copy_function(offer_id){
	$("#copy_from_default").click(function(){										  
		if($(this).is(":checked")){
			$.getJSON('inc/ajax/json.process.php?id='+offer_id+'&a=get_post_data', function(data) {
				//$('.result').html('<p>' + data.foo + '</p>'	+ '<p>' + data.baz[1] + '</p>');			
				$("#business_descr").val(data.post_business_descr);
				$("#terms").val(data.post_terms);
				$("#contact_details").val(data.post_contact_details);
				/*$("#opening_hours_start").val(data.post_opening_hours_start);
				$("#opening_hours_end").val(data.post_opening_hours_end);*/			
				$("#opening_hours_text").val(data.post_opening_hours_text);
				if(data.post_benefit != ""){
					$("#benefits").val(data.post_benefit);
				}//end if
				$("#other_benefits").val(data.post_other_benefits);
			});
		} else {	
			$("#business_descr").val("");
			$("#terms").val("");
			$("#contact_details").val("");
			//$("#opening_hours_start").val("");
			//$("#opening_hours_end").val("");
			$("#opening_hours_text").val("");
			$("#other_benefits").val("");
		}///end if	
	});
}

function count_words(element_class,maxwords){
	$('#'+element_class).each(function(){ 
								
		// get current number of characters  
		//var length = $(this).val().length;  
		// get current number of words  
		var length = $(this).val().split(/\b[\s,\.-:;]*/).length; 
		if( $(this).val() == ""){
			length = 0;	
		}
		// update characters  
		$(this).parent().find('.counter').html( length + ' words');  
		// bind on key up event  
		$(this).keyup(function(){  
		 // get new length of characters  
		 //var new_length = $(this).val().length;  
		 // get new length of words  
		 var new_length = $(this).val().split(/\b[\s,\.-:;]*/).length;  
		 if(maxwords < new_length){
			var value = $(this).val();
			$(this).val(value.substr(0,$(this).val().length - 1)); 
		 }
		 // update  
		 $(this).parent().find('.counter').html( new_length + ' words');  
		});  
	}); 
}

$().ready(function(){
	$('a[title]').qtip({ 
		style: { 
			name: 'dark', 
			tip: true,
			background: '#616161',
			opacity : 0.8,
			border: {
				 width: 7,
				 radius: 5,
				 color: '#616161'
			  }
		},
		position: {
			corner: {
				target: 'rightMiddle',
				tooltip: 'leftMiddle'
			}
		}

	});
	
	$('input[title]').qtip({ 
		style: { 
			name: 'dark', 
			tip: true,
			background: '#616161',
			width: 200,
			border: {
				 width: 7,
				 radius: 5,
				 color: '#616161'
			  }
		},
		position: {
			corner: {
				target: 'rightMiddle',
				tooltip: 'leftMiddle'
			}
		}

	});

});
