jQuery(document).ready(function(){
	jQuery('#select_type').change(function(){
		var ad_type = jQuery('#select_type').val();
		
		switch(ad_type){
			case 'text':
					jQuery('.aloader').fadeIn(1000);
					jQuery('.monetize-fields').fadeOut(200);
					jQuery('.banner-form').css({'display':'none'});
					setTimeout(function(){
						jQuery('.aloader').fadeOut(200);
						jQuery('.monetize-fields').fadeIn(200);
						jQuery('.contextual-form').fadeIn(200);
					}, 1000);
					break;
					
			case 'banner':
					jQuery('.aloader').fadeIn(1000);
					jQuery('.monetize-fields').fadeOut(200);
					jQuery('.contextual-form').css({'display':'none'});
					setTimeout(function(){
						jQuery('.aloader').fadeOut(200);
						jQuery('.monetize-fields').fadeIn(200);
						jQuery('.banner-form').fadeIn(200);
					}, 1000);
					
					break;
			default:
					jQuery('.monetize-fields').fadeOut(200);
					jQuery('.contextual-form').fadeOut(200);
					jQuery('.banner-form').fadeOut(200);
					break;
		}
	});
	
	jQuery('#ads_appearance').change(function(){
		var ads_appearance = jQuery('#ads_appearance').val();
		
		switch(ads_appearance){
			case 'spec_cat':
						jQuery('#spec_page').css({'display':'none'});
						jQuery('#spec_post').css({'display':'none'});
						jQuery('#spec_cat').fadeIn(200);
					break;
			
			case 'spec_post':
						jQuery('#spec_page').css({'display':'none'});
						jQuery('#spec_cat').css({'display':'none'});
						jQuery('#spec_post').fadeIn(200);
					break;
			
			case 'spec_page':
						jQuery('#spec_cat').css({'display':'none'});
						jQuery('#spec_post').css({'display':'none'});
						jQuery('#spec_page').fadeIn(200);
					break;
			
			default:
						jQuery('#spec_cat').fadeOut(200);
						jQuery('#spec_post').fadeOut(200);
						jQuery('#spec_page').fadeOut(200);
					break;
		}
	});
	
	jQuery('#monetze-form').submit(function(){
		var ad_type = jQuery('#select_type').val();
		var affiliate_link = jQuery.trim(jQuery('#affiliate_link').val());
		var appearance = jQuery('#ads_appearance').val();
		
		switch(ad_type){
			case 'text':
						var keyword = jQuery.trim(jQuery('#target_keyword').val());
						var occurrence = jQuery('#occurrence').val();
						
							
						if(affiliate_link.length > 0){
							jQuery('#affiliate_error').fadeOut(100);
							
							if(appearance == 'spec_cat' || appearance == 'spec_post' || appearance == 'all_post' || appearance == 'spec_page' || appearance == 'all_page'){
								jQuery('#appearance_error').fadeOut(100);
								var appearance_error = 0;
								
								switch(appearance){
									case 'spec_cat':
											var spec_cat = jQuery('#spec_cat').val();
											if(spec_cat == 0)
												appearance_error = 1;
												
											break;
									
									case 'spec_post':
											var spec_post = jQuery.trim(jQuery('#spec_post').val());

											if(spec_post.length == 0)
												appearance_error = 2;
											
											break;
									
									case 'spec_page':
											var spec_page = jQuery.trim(jQuery('#spec_page').val());
											
											if(spec_page.length == 0)
												appearance_error = 3;
												
											break;
								}
								
								if(appearance_error == 0){
									jQuery('#appearance_error').fadeOut(100);
									
									if(keyword.length > 0){
										jQuery('#keyword_error').fadeOut(100);
										
										if(occurrence > 0){
											jQuery('#occurrence_error').fadeOut(100);
											
											return true;
										}else{
											jQuery('#occurrence_error').fadeIn(200).html('Please choose the no. of occurrence');
										}
									}else{
										jQuery('#keyword_error').fadeIn(200).html('Please enter your keyword');
									}
									
								}else{
									if(appearance_error == 1){
										jQuery('#appearance_error').fadeIn(200).html('Please select a specific category');
									}
									
									if(appearance_error == 2){
										jQuery('#appearance_error').fadeIn(200).html('Please enter the Post URL');
									}
									if(appearance_error == 3){
										jQuery('#appearance_error').fadeIn(200).html('Please enter the Page URL');
									}
									
								}
								
							}else{
								jQuery('#appearance_error').fadeIn(200).html('Please select the Ad\'s Appearance');
							}
						}else{
							jQuery('#affiliate_error').fadeIn(200).html('Please enter your affiliate link');
						}
						
					break;
			
			case 'banner':				
					if(affiliate_link.length > 0){
						jQuery('#affiliate_error').fadeOut(100);
						
						if(appearance == 'spec_cat' || appearance == 'spec_post' || appearance == 'all_post' || appearance == 'spec_page' || appearance == 'all_page'){
							jQuery('#appearance_error').fadeOut(100);
							var appearance_error = 0;
							
							switch(appearance){
								case 'spec_cat':
										var spec_cat = jQuery('#spec_cat').val();
										if(spec_cat == 0)
											appearance_error = 1;
											
										break;
								
								case 'spec_post':
										var spec_post = jQuery.trim(jQuery('#spec_post').val());

										if(spec_post.length == 0)
											appearance_error = 2;
										
										break;
										
								case 'spec_page':
										var spec_page = jQuery.trim(jQuery('#spec_page').val());
										
										if(spec_page.length == 0)
											appearance_error = 3;
										break;
							}
							
							if(appearance_error == 0){
								jQuery('#appearance_error').fadeOut(100);
								var banner = jQuery('#banner').val();
								var extension = banner.substring(banner.lastIndexOf('.') + 1);
								
								if(extension == 'jpg' || extension == 'png' || extension == 'gif' || extension == 'jpeg'){
									jQuery('#banner_error').fadeOut(100);
									var position = jQuery('#banner_pos').val();
									
									if(position == 'top' || position == 'bottom' || position == 'middle'){
										jQuery('#position_error').fadeOut(100);
										var align = jQuery('#banner_align').val();
										
										if(align == 'left' || align == 'center' || align == 'right'){
											jQuery('#align_error').fadeOut(100);
											
											var arrow = jQuery('#banner_arrow').val();
											if(arrow == 'yes' || arrow == 'no'){
												jQuery('#arrow_error').fadeOut(100);
												return true;
											}else{
												jQuery('#arrow_error').fadeIn(200).html('Please select if you want to add an arrow for the image.');
											}
										}else{
											jQuery('#align_error').fadeIn(200).html('Please select a banner alignment');
										}
									}else{
										jQuery('#position_error').fadeIn(200).html('Please select a banner position');
									}
								}else{
									jQuery('#banner_error').fadeIn(200).html('Please upload a valid file');
								}
							}else{
								if(appearance_error == 1){
									jQuery('#appearance_error').fadeIn(200).html('Please select a specific category');
								}
								
								if(appearance_error == 2){
									jQuery('#appearance_error').fadeIn(200).html('Please enter the Post URL');
								}
								
								if(appearance_error == 3){
									jQuery('#appearance_error').fadeIn(200).html('Please enter the Page URL');
								}
							}
							
						}else{
							jQuery('#appearance_error').fadeIn(200).html('Please select the Ad\'s Appearance');
						}
					}else{
						jQuery('#affiliate_error').fadeIn(200).html('Please enter your affiliate link');
					}
					break;
		}
		
		return false;
	});
	
	jQuery('#euform1').submit(function(){
		var uop = jQuery.trim(jQuery('#uop').val());
		
		if(uop.length > 0){
			return true;
		}else{
			jQuery('#uop_error').fadeIn(200).html('Please enter the POST URL');
		}
		
		return false;
	});
	
	jQuery('#euform2').submit(function(){
		var banner = jQuery('#url_file').val();
		var extension = banner.substring(banner.lastIndexOf('.') + 1);
		if(extension == 'txt'){
			jQuery('#bulk_error').fadeOut(100);
			
			return true;
		}else{
			jQuery('#bulk_error').fadeIn(200).html('Please select a valid file');
		}
		return false;
	});
});
