$(document).ready(function() {
	
	if ($("p.Total").size()>0) {
	
		$("#previewDevice").attr("src", BASE_FOLDER + "/images/preview_grey.gif").attr("alt", "Preview").attr("value", "previewDevice").attr("name", "previewDevice");	
	} else {
		$("#previewDevice").attr("src", BASE_FOLDER + "/images/preview-grey.gif").attr("alt", "Preview").attr("value", "previewDevice").attr("name", "previewDevice");	
	}
	/**
	 * Select Type
	 */
	$("#GetRecycling_type").change(function () {
		var type = $(this).selectedValues()[0];
		if(type !== ""){
			$("#GetRecycling_make").removeAttr('disabled');
			$.post(
				BASE_PATH + '/site/ajaxPopulateMake', { 
					type: type 
				},
				function(response) {
					var options = '';
					var makes = $.evalJSON(response);
					for(i = 0; i < makes.length; i++) {
						options += '<option value="' + makes[i].make  + '">' + makes[i].name + '</option>';
					}
					$("#GetRecycling_make").html(options);
				}
			);
		}
		else {
			$("#GetRecycling_make").attr('disabled', 'disabled')
			$("#GetRecycling_device").attr('disabled', 'disabled');
		}
	});
	
	/**
	 * Select Make
	 */
	$("#GetRecycling_make").change(function () {
		var make = $(this).selectedValues()[0];
		var type = $("#GetRecycling_type").selectedValues()[0];
		if(make !== "" && type !== ""){
			$("#GetRecycling_device").removeAttr('disabled');
			$.post(
				BASE_PATH + '/site/ajaxPopulateDevice', {
					type: type,
					make: make
				},
				function(response) {
					var options = '';
					var devices = $.evalJSON(response);
					for(i = 0; i < devices.length; i++) {
						options += '<option value="' + devices[i].id  + '">' + devices[i].name + '</option>';
					}
					$("#GetRecycling_device").html(options);
				}
			);
		}
		else {
			$("#GetRecycling_device").attr('disabled', 'disabled');
		}
	});

	//:selected
	$("#Pay-Box").find("input:radio").click(function() {										  
			var value = $(this).attr("value");
			step2RadiosClick(value);
	})
	$("#Pay-Box").find("input:radio:checked").each(function() {
		var value = $(this).attr("value");
		step2RadiosClick(value)													  
															  
	})
	
	
	
	$("#GetRecycling_pay_email_confirm").focus(function(){ 
		if ($(this).attr('value')=="Enter PayPal email address") {
			$(this).attr('value','');
		}							   								   
	}).blur(function() {
		if ($(this).attr('value')=="") {
			$(this).attr('value','Enter PayPal email address');
		}		
	})
	$("#GetRecycling_pay_email").focus(function(){ 
		if ($(this).attr('value')=="Enter PayPal email address") {
			$(this).attr('value','');
		}							   								   
	}).blur(function() {
		if ($(this).attr('value')=="") {
			$(this).attr('value','Enter PayPal email address');
		}		
	})
	
	$("#GetRecycling_pay_telephone").focus(function(){ 
		if ($(this).attr('value')=="Enter mobile number") {
			$(this).attr('value','');
		}							   								   
	}).blur(function() {
		if ($(this).attr('value')=="") {
			$(this).attr('value','Enter mobile number');
		}		
	})
	
	$("#GetRecycling_pay_telephone_confirm").focus(function(){ 
			if ($(this).attr('value')=="Enter mobile number") {
				$(this).attr('value','');
			}							   								   
		}).blur(function() {
			if ($(this).attr('value')=="") {
				$(this).attr('value','Enter mobile number');
			}		
	})
	
	
	$("#PayDetailsForm").submit(function() {
		
		
		if ($("#GetRecycling_pay_telephone").attr('value')=="Enter mobile number") {
			$("#GetRecycling_pay_telephone").attr('value','');
		}
		if ($("#GetRecycling_pay_telephone_confirm").attr('value')=="Enter mobile number") {
			$("#GetRecycling_pay_telephone_confirm").attr('value','');
		}
		
		if ($("#GetRecycling_pay_email").attr('value')=="Enter PayPal email address") {
			$("#GetRecycling_pay_email").attr('value','');
		}
		if ($("#GetRecycling_pay_email_confirm").attr('value')=="Enter PayPal email address") {
			$("#GetRecycling_pay_email_confirm").attr('value','');
		}	
		
	});
	
	
	if ($("#GetRecycling_pay_email").attr('value')=="") {
		$("#GetRecycling_pay_email").attr('value','Enter PayPal email address');			
	}
	if ($("#GetRecycling_pay_email_confirm").attr('value')=="") {
		$("#GetRecycling_pay_email_confirm").attr('value','Enter PayPal email address');
	}
	if ($("#GetRecycling_pay_telephone").attr('value')=="") {
		$("#GetRecycling_pay_telephone").attr('value','Enter mobile number');
	}
	if ($("#GetRecycling_pay_telephone_confirm").attr('value')=="") {
		$("#GetRecycling_pay_telephone_confirm").attr('value','Enter mobile number');
	}	
	
	
	
});

function step2RadiosClick(value) {
	if (value=="postal-order") {
		$("#GetRecycling_pay_email").attr('disabled', 'disabled').attr("value", "Enter PayPal email address");
		$("#GetRecycling_pay_email_confirm").attr('disabled', 'disabled').attr("value", "Enter PayPal email address");
		$("#GetRecycling_pay_telephone").attr('disabled', 'disabled').attr("value", "Enter mobile number");
		$("#GetRecycling_pay_telephone_confirm").attr('disabled', 'disabled').attr("value", "Enter mobile number");
		$("#GetRecycling_pay_charity").attr('disabled', 'disabled');
	} else if (value=="paypal") {
		$("#GetRecycling_pay_email").removeAttr('disabled');
		$("#GetRecycling_pay_email_confirm").removeAttr('disabled');
		
		$("#GetRecycling_pay_telephone").attr('disabled', 'disabled').attr("value", "Enter mobile number");
		$("#GetRecycling_pay_telephone_confirm").attr('disabled', 'disabled').attr("value", "Enter mobile number");
		$("#GetRecycling_pay_charity").attr('disabled', 'disabled');			
	} else if (value=="sms") {
		$("#GetRecycling_pay_email").attr('disabled', 'disabled').attr("value", "Enter PayPal email address");
		$("#GetRecycling_pay_email_confirm").attr('disabled', 'disabled').attr("value", "Enter PayPal email address");
		$("#GetRecycling_pay_telephone").removeAttr('disabled');
		$("#GetRecycling_pay_telephone_confirm").removeAttr('disabled');
		$("#GetRecycling_pay_charity").attr('disabled', 'disabled');			
	} else if (value=="donate") {
		$("#GetRecycling_pay_email").attr('disabled', 'disabled').attr("value", "Enter PayPal email address");
		$("#GetRecycling_pay_email_confirm").attr('disabled', 'disabled').attr("value", "Enter PayPal email address");
		$("#GetRecycling_pay_telephone").attr('disabled', 'disabled').attr("value", "Enter mobile number");
		$("#GetRecycling_pay_telephone_confirm").attr('disabled', 'disabled').attr("value", "Enter mobile number");
		$("#GetRecycling_pay_charity").removeAttr('disabled');
	}	
}