$j = jQuery.noConflict();

$j("#cost").val("");
$j("#budget").val("");
$j(".qq").val("");
var pwait = 1;

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

$j(function () {

	$j("#clamp-sel").val("S");
	$j("#dcid").focus();

	$j("#dcid").focus(function(){
		$j("#dcid-err").hide();
		$j("#dcid-help").show();
	});
	
	$j("#dcid-but").click(function(){
		$j("#dcid-err").hide();
		$j("#dcid-but").hide();
		$j("#dcid-load").show();
		var foo = new Date;
		var unixtime_ms = foo.getTime();
		var unixtime = parseInt(unixtime_ms / 1000);
		$data = "iHateIE=" + unixtime + "&hwid=" + $j("#dcid").val();
		$j.ajax({
			url: '/data/check_hware_id.json',
			type: 'POST',
			dataType: 'json',
			data: $data,
		    timeout: 25000,
			error: function(){
				alert("Unable to check the ID at this time.");
			},
			success: function(json){
				if (json.ok == 0) {
					$j("#dcid-err").html(json.reason);
					$j("#dcid-help").hide();
					$j("#dcid-err").show();
					$j("#dcid-but").show();
					$j("#dcid-load").hide();
				} else {
					$j("#dcid-but").hide();
					$j("#dcid-fin").html($j("#dcid").val());
					$j("#dcid").hide();
					$j("#dcid-help").hide();
					$j("#dcid-fin").show();
					$j("#clamp-type").show();
					$j("#clamp-help").show();
					$j("#dcid-load").hide();
				}
			}
		});
		return false;
	});

	$j("#clamp-sel").change(function(){
		$j("#clamp-type-fin").html($j('#clamp-sel :selected').text());
		$j("#clamp-sel").hide();
		$j("#clamp-type-fin").show();
		$j("#clamp-help").hide();
                $j("#account-setup").show();
		$j("#acc-user").focus();
	});

	// Account set up stuff.
	$j("#acc-user").focus(function(){
		$j("#acc-user-err").hide();
		$j("#acc-user-help").show();
	});

	$j("#acc-user-but").click(function(){
		$j("#acc-user-err").hide();
		$j("#acc-user-but").hide();
		$j("#acc-user-load").show();
		var foo = new Date;
		var unixtime_ms = foo.getTime();
		var unixtime = parseInt(unixtime_ms / 1000);
		$data = "iHateIE=" + unixtime + "&uname=" + $j("#acc-user").val();
		$j.ajax({
			url: '/data/check_user.json',
			type: 'POST',
			dataType: 'json',
			data: $data,
		        timeout: 25000,
			error: function(){
				alert("Unable to check the username at this time.");
			},
			success: function(json){
				if (json.ok == 0) {
					$j("#acc-user-err").html(json.reason);
					$j("#acc-user-help").hide();
					$j("#acc-user-err").show();
					$j("#acc-user-but").show();
					$j("#acc-user-load").hide();
				} else {
					$j("#acc-user-but").hide();
					$j("#acc-user-help").hide();
					$j("#acc-user-fin").html($j("#acc-user").val());
					$j("#acc-user").hide();
					$j("#acc-user-fin").show();
					$j("#acc_pass").focus();
					$j("#password-wrapper").show();
					$j("#acc-user-load").hide();
				}
			}
		});
		return false;
	});

	$j("#acc-pass")
		.focus(function(){
			if (pwait == 1) {
				$j("#acc-pass-err").hide();
				$j("#acc-pass-help").show();
				$j('#acc-conf').val("");
				$j("#acc-conf-err").hide();
			}
		})
		.blur(function(){
			if (pwait == 1) {
				if (this.value.length < 6) {
					$j("#acc-pass-err").html("Password is too short. Must be 6 characters or longer.");
					$j("#acc-pass-err").show();
				}
				$j("#acc-pass-help").hide();
				$j('#acc-conf').focus();
				$j('#pass-but').show();
			}
		});

	$j("#acc-conf").focus(function(){
		if (pwait == 1) {
			$j("#acc-conf-err").hide();
			$j("#acc-conf-help").show();
		}
	});
		
	$j('#pass-but').click(function(){
			if ( $j('#acc-pass').val() == $j('#acc-conf').val() ) {
				$j('#acc-pass').attr("readOnly", true);
				$j('#acc-pass').addClass("rs-set");
				$j('#acc-conf').attr("readOnly", true);
				$j('#acc-conf').addClass("rs-set");
				pwait = 0;
				$j("#acc-pass-help").hide();
				$j("#acc-conf-help").show();
				$j('#pass-but').hide();
				$j('#email-wrapper').show();
				$j("#acc-but").show();
			} else {
				$j("#acc-conf-err").html("Passwords don't match.");
				$j("#acc-conf-err").show();
			}
			$j("#acc-conf-help").hide();
	});

	$j("#acc-email").focus(function(){
		$j("#acc-email-err").hide();
		$j("#acc-email-help").show();
	});

	$j("#acc-but").click(function() {
		// check the email and move on
		$j("#acc-email-help").hide();
		if ($j("#acc-email").val().match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/)) {
			$j('#acc-email').attr("readOnly", true);
			$j('#acc-email').addClass("rs-set");
			$j("#acc-but").hide();
			$j("#tariff-data").show();
			$j("#supplier").focus();
		} else {
			$j("#acc-email-err").html("That doesn't look like a real Email address. Please try again.");
			$j("#acc-email-err").show();
		}
	});
		
	// Supplier stuff
		
	$j("#supplier")
		.focus(function(){$j("#supplier-help").show();	})
		.blur(function(){$j("#supplier-help").hide();})
		.change(function(){
			$j("#supplier-fin").html($j('#supplier :selected').text());
			$j("#supplier").hide();
			$j("#supplier-fin").show();
			$j("#supplier-help").hide();
			$j("#tariff").focus();
			$j('#tariff1-wrapper').show();
		});
			
	$j("#cost")
		.focus(function(){
			$j("#cost-err").hide();
			$j("#cost-help").show();
		})
		.blur(function(){
			$j("#cost-help").hide();
			
			if ( parseFloat($j("#cost").val()) > 100 ) {
				$j("#cost-err").html("That seems too expensive for a kWh.");
				$j("#cost-err").show();
			} else if( !isNumber(  $j("#cost").val() ) ) {
				$j("#cost-err").html("Please enter a number");
				$j("#cost-err").show();
			} else {
				$j("#cost-fin").html($j("#cost").val());
				$j("#cost").hide();
				$j("#cost-fin").show();
				$j('#tariff2-wrapper').show();
			}
		});

	$j("#budget").focus(function(){
		$j("#budget-err").hide();
		$j("#budget-help").show();
		$j("#supp-but").show();
	});
		
		
	$j("#supp-but").click(function(){
		$j("#budget-help").hide();
		if( !isNumber(  $j("#budget").val() ) ){
			$j("#budget-err").html("Please enter a number");
			$j("#budget-err").show();
		} else if ( !isNumber(  $j("#cost").val() ) ){
			$j("#cost-err").html("Please enter a number");
			$j("#cost-err").show();
		} else if ( parseFloat($j("#cost").val()) > 100 ) {
			$j("#cost-err").html("That seems too expensive for a kWh.");
			$j("#cost-err").show();
		} else {
			$j("#budget-fin").html($j("#budget").val());
			$j("#budget").hide();
			$j("#budget-fin").show();
			$j("#cost").hide();
			$j("#cost-fin").show();
			if ( $j("#tariff").val() == "" ) { $j("#tariff").val("unknown"); }
			$j("#tariff-fin").html($j("#tariff").val());
			$j("#tariff").hide();
			$j("#tariff-fin").show();
			$j("#supp-but").hide();
			$j("#demographics").show();
			$j("#pcode").focus();
		}
	});
	
	$j("#pcode").focus(function(){
		$j("#pcode-err").hide();
		$j("#pcode-help").show();
	});
		
	$j('#pcode-but').click(function(){
			if ($j("#pcode").val().length < 1) {
				$j("#pcode-err").html("You must enter the first section of a valid uk postcode.");
				$j("#pcode-err").show();
			} else {
				$data = "callback=?&pcode=" + $j("#pcode").val();
				$j.ajax({
					url: '/data/check_pcode.json',
					type: 'POST',
					dataType: 'json',
					data: $data,
				    timeout: 5000,
					success: function(json){
						if (json.ok == 0) {
							$j("#pcode-help").hide();
							$j("#pcode-err").html(json.reason);
							$j("#pcode-err").show();
							$j('#pcode').val(json.pcode);
						} else {
							$j("#pcode-help").hide();
							$j('#pcode').val(json.pcode);
							$j('#pcode-but').hide();
							$j("#pcode-fin").html($j("#pcode").val());
							$j("#pcode").hide();
							$j("#pcode-fin").show();
							$j('#demo-wrapper').show();
						}
					}
				});
			}
		});
		
	var q_count = $j('.qq').size();		
	$j(".qq").change(function(){
		$j(this).next().html( $j(this).find(':selected').text() );
		$j(this).next().show();
		$j(this).hide();
		q_count --;
		if (q_count == 0) { $j("#rs-submit").show(); }
	});
	
	$j("#rs-submit").click(function(){
		$j("rs-submit").hide();
		$j("rs-submit-wait").show();
		$data = "callback=?&pcode=" + $j("#pcode").val() + "&dcid=" + $j("#dcid").val() + "&clamp=" + $j('#clamp-sel :selected').val();
		$data = $data + "&username=" + $j("#acc-user").val() + "&password=" + $j('#acc-pass').val() + "&email=" + $j("#acc-email").val() + "&supplier=" + $j('#supplier :selected').val();
		$data = $data + "&tariff=" + $j("#tariff").val() + "&cost=" + $j("#cost").val() + "&budget=" + $j("#budget").val();
		$j('.qq').each(function(i,el) { $data = $data + "&" + el.name + "=" + $j(el).val(); 	});
		
		$j.ajax({
			url: '/data/do_reg.json',
			type: 'POST',
			dataType: 'json',
			data: $data,
		    timeout: 5000,
			success: function(json){
				if (json.ok == 0) {
					alert("A problem has occurred and we are unable to create your account at this time. Please try again later." + json.reason);
				} else {
					if (json.ok == 0) {
							alert("A problem has occurred and we are unable to create your account at this time. " + json.reason);
					}
					window.location = "http://www.energyhive.co.uk/account_created.html"
					
				}
			}
		});
	});
	
});
