$(window).load(function () {
	$("#EEContent").show();
});
jQuery(function($){
	$("#Phone,#Fax").mask("99 999 9999",{placeholder:" "});
	$("#Extn").mask("9999",{placeholder:" "});
	$("#OBOOPhone").mask("9999 999 999",{placeholder:" "});

});
$(document).ready(function(){
	$("#tabs > ul").tabs();
	//$('.titleHintBox').inputHintBox({div:$('#shiny_box'),div_sub:'.shiny_box_body',source:'attr',attr:'title',incrementLeft:5});
	$("#isCoordinator").click(function(){
		isRegForm(true);
	});
	$("#isOkRequirements").click(function(){
		isRegForm(true);
	});
	// start new reg script
	$("#frmRegForm").validate({
		rules: {
			Name:{ required: true, rangelength: [2,50] },
			Position:{ required: true, rangelength: [2,50] },
			Organisation:{ required: true, rangelength: [2,50] },
			Street:{ required: true, rangelength: [7,50] },
			City:{ required: true, rangelength: [4,50] },
			PostalCode:{ required: true, number: true },
			Phone:{ required: function(element){
					var $OBOOPhone = jQuery.trim($("#OBOOPhone").val());
					var $Mobile = jQuery.trim($("#Mobile").val());
					if('' == $OBOOPhone && '' == $Mobile){ return true; }
					else{ return false; }
				}
			},
			Email:{ required: true, email: true },
			Password:{ required: true, rangelength: [7,20] },
			Password2:{ required: true, rangelength: [7,20], equalTo: "#Password" }
		}
		,debug: false
	});
	$("#frmRegForm").submit(function(){
		if(false == $("#frmRegForm").valid()){ return false; }
		return true;
	});

	$("#Email").blur(function(){
		if(false == $("#frmRegForm").validate().element("#Email")){ return false; }
		$("#Email").addClass('isEmail');
		$.getJSON("/reg/index.html",{
				Email: $("#Email").val(),
				btnSearch: "Search"
			},
			function(data, status){
				$("#Email").removeClass('isEmail');
				if('success' == status){
					if('true' == data.isEmail){
						callDialog('Bike Wise registration form',data.response);
					}
				}
		});
		return false;

	});
	// end new reg script

	// start login script
	$("#frmLogin").validate({
		rules: {
			Email: { required: true, email: true },
			Password: { required: true, rangelength: [7,20] }
		}
		,debug: false
	});
	$("#frmLogin").submit(function(){
		if(false == $("#frmLogin").valid()){ return false; }
		return true;
	});
	// end login script

	// forgot email
	$("#frmForgotEmail").validate({
		rules: {
			Email: {
				required: true,
				email: true
			}
		}
		,debug: false
	});
	$("#frmForgotEmail").submit(function(){
		if(false == $("#frmForgotEmail").valid()){ return false; }
		$("#pForgotEmail").css("color","#008000").html( $("#spnAjax").html() );
		$("#frmForgotEmail").find("input").attr("disabled",true);
		$.getJSON("/reg/index.html",
			{	Email: $("#MyEmail").val(),
				btnForgotEmail: $("#btnForgotEmail").val()
			},
			function(data, status){
				if('success' == status){ onResponse(data,'pForgotEmail'); $("#frmForgotEmail").find("input").attr("disabled",false); }
				else{ callDialog('Bike Wise error','System error ...<br/>Please contact the Bike wise team'); }
		});
		return false;
	});
	// end forgot email

	// start last year registration script
	$("#frmSearchEmail").validate({
		rules: { Email: { required: true, email: true } }
		,debug: false
	});
	$("#frmSearchEmail").submit(function(){
		if(false == $("#frmSearchEmail").valid()){ return false; }
		$("#spnSearchEmail").css("color","#008000").html( $("#spnAjax").html() );
		$("#frmSearchEmail").find("input").attr("disabled",true);
		$.getJSON("/reg/index.html",
			{	Email: $("#txtSearchEmail").val(),
				btnSearch: "Search"
			},
			function(data, status){
				if('success' == status){ onResponse(data,'spnSearchEmail'); $("#frmSearchEmail").find("input").attr("disabled",false);/*onSuccess(data);*/ }
				else{ callDialog('Bike Wise error','System error ...<br/>Please contact the Bike wise team'); }
		});
		return false;
	});
	// end last year registration script
});

function switchTab(tabId){
	$("#tabs > ul").tabs("select", tabId); // switch to 4 tab

	$("#dialog").dialog('close'); // TG-reg-form-update-2009 code added by Trevor 19-10-2009 to automatically close the dialog box whent he user clicks the forgotten pasword link
	return false;
}

function isRegForm(isDialog){
	/*
	TG-reg-form-update-2009
	code removed by Trevor 19-10-2009 to enable the registration form by default
	
	if( $("#isCoordinator").is(":checked") && $("#isOkRequirements").is(":checked") ){
		$("#tblRegForm").find("input,select").attr("disabled",false);
	}
	else if( $("#isCoordinator").is(":checked") && !$("#isOkRequirements").is(":checked") ){
		$("#tblRegForm").find("input,select").attr("disabled",false);
	}
	else if( !$("#isCoordinator").is(":checked") && $("#isOkRequirements").is(":checked") ) {
		$("#tblRegForm").find("input,select").attr("disabled",true);
		if(isDialog){
			$.getJSON("/reg/index.html",
				{ Terms: "OkRequirementsNotCoordinator" },
				function(data, status){
					if('success' == status){ callDialog('Bike Wise registration form',data.response); }
					else{ callDialog('Bike Wise error','System error ...<br/>Please contact the Bike wise team'); }
				}
			);
		}
	}
	else{
		$("#tblRegForm").find("input,select").attr("disabled",true);
		if(isDialog){
			$.getJSON("/reg/index.html",
				{ Terms: "NullRequirementsNotCoordinator" },
				function(data, status){
					if('success' == status){ callDialog('Bike Wise registration form',data.response); }
					else{ callDialog('Bike Wise error','System error ...<br/>Please contact the Bike wise team'); }
				}
			);
		}
	}*/
}

function onResponse(data, Id){
	if('false' == data.error){ $('#'+Id).css("color","#008000"); }
	else{ $('#'+Id).css("color","#FF0000"); }
	$('#'+Id).html(data.response);
}

function callDialog($title, $body){
	$("#dialog").dialog({
			modal: true,
			overlay:{ opacity: 0.5,
				background: "black"
			},
			height: 250,
			width: 400,
			title: $title,
			resizable: false,
			draggable: false,
			//autoOpen: false,
			buttons: { "Close": function() {
		            $(this).dialog("close");
		        }
		    }
	});
	$("#dialog").html($body);
}

function ErrFieldNull($field){
	return "The '" + $field + "' field is required.";
}