var AngelinaBallerina = {
	Studios : {
		Login : {
			Validate:function() {
				if( $("studioEmailAddress").value.blank() ) {
					AngelinaBallerina.HighlightAndFocus("studioEmailAddress");
					return false;
				}
				if( $("studioPassword").value.blank() ) {
					AngelinaBallerina.HighlightAndFocus("studioPassword");
					return false;
				}
				return true;
			}
		}
	},
	ShoppingCart : {
		EmptyCart:function(argUrl) {
			var confirmation = confirm("Are you sure you want to empty your shopping cart?");
			if(confirmation) {
				document.location=argUrl + "index.cfm?action=ShoppingCart_EmptyCart";
			}
		},
		RemoveItem:function(key) {
			var confirmation = confirm("Are you sure you wish to remove one of these items?");
			if(confirmation) {
				var url = "index.cfm?action=ShoppingCart_RemoveItem&id="+key;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.ShoppingCart.RemoveItem_onSuccess, onFailure:errFunc});
			}
		},
		RemoveItem_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("shoppingCartHolder").update(z.shoppingCartHTML);
		},
		Checkout : {
			Validate:function() {
				if($("orderFirstName").value.blank()) {
					AngelinaBallerina.HighlightAndFocus("orderFirstName");
					return false;
				}
				if($("orderLastName").value.blank()) {
					AngelinaBallerina.HighlightAndFocus("orderLastName");
					return false;
				}
				if($("orderEmailAddress").value.blank() || !$("orderEmailAddress").value.isEmail()) {
					AngelinaBallerina.HighlightAndFocus("orderEmailAddress");
					return false;
				}
				if($("orderAddress1").value.blank()) {
					AngelinaBallerina.HighlightAndFocus("orderAddress1");
					return false;
				}
				if($("orderCountyId").value==-1) {
					alert("Please choose the county to post to");
					return false;
				}
				return true;
			}
		}
	},	
	Registration : {
		Cancel:function(termId,childId) {
			var confirmation = confirm("Are you sure you wish to cancel registration for these classes? This can not be undone and your place may be taken by another student.");
			if(confirmation) {
				var url = "index.cfm?action=Registration_Cancel&tId="+termId+"&cId="+childId;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.Registration.Cancel_onSuccess, onFailure:errFunc});
			}
			return false;
		},
		Cancel_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			document.location = z.targetURL;
		},
		ValidateLogin:function() {
			if($("regParentMobileNo").value.blank() || !isNumeric($("regParentMobileNo").value)) {
				AngelinaBallerina.HighlightAndFocus("regParentMobileNo");
				return false;
			}
			if($("regPINNumber").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regPINNumber");
				return false;
			}
			$("btnLogin").hide();
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.Registration.ValidateLogin_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "registrationForm" ) )});
			return false;
		},
		ValidateLogin_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.result) {
				document.location=z.targetURL;
			} else {
				alert(z.message);
				$("btnLogin").show();
			}
		},
		ValidateStep1:function() {			
						
			if($("regParentName").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regParentName");
				return false;
			}
			if($("regSurname").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regSurname");
				return false;
			}
			if($("regAddress1").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regAddress1");
				return false;
			}
			if($("regCounty").value==-1) {
				alert("Please choose the county you reside in");
				return false;
			}
			if($("regParentHomeNo").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regParentHomeNo");
				return false;
			}
			if($("regParentMobileNo").value.blank() || !isNumeric( $("regParentMobileNo").value )) {
				AngelinaBallerina.HighlightAndFocus("regParentMobileNo");
				return false;
			}
			if($("regParentEmailAddress").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regParentEmailAddress");
				return false;
			}
			if($("regEmergencyName").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regEmergencyName");
				return false;
			}
			if($("regEmergencyNo").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regEmergencyNo");
				return false;
			}
			
			var numChecked = 0;
			var radios = $$("input[type=radio]");
			
			for( i=0;i<radios.length;i++) {
				if( radios[i].checked ) {
					numChecked++;
				}
			}

			if( numChecked == 0 ) {
				alert("Please tell us how you found out about us");				
				return false;
			}
			
			if( radios[ radios.length-1 ].checked && $("other").value.blank() ) {
				AngelinaBallerina.HighlightAndFocus("other");
				return false;
			}
			
			$("newUserBut").hide();
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.Registration.ValidateStep1_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "registrationForm" ) )});
			return false;	
		},
		ValidateStep1_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.result) {
				alert(z.message);
				document.location=z.targetURL;
			} else {
				alert(z.message);
				if( z.HAL ) {
					AngelinaBallerina.HighlightAndFocus( z.HAL );
				}
				$("newUserBut").show();
			}
		},
		ForgotPIN:function() {
			new Effect.SlideUp("existingUsersHolder");
			$("forgotPINHolder").show();
		},
		CancelForgotPIN:function() {						
			$("forgotPINHolder").hide();
			$("existingUsersHolder").show();
			$("forgotPINPhoneDetailsHolder").show();
			$("forgotPINBut").show();
		},
		SendForgotPIN:function() {
			var cNumber = $("forgotMobileNo").value;
			var cPrefix = $("forgotMobilePrefix").value;
			if(cNumber.blank()) {
				AngelinaBallerina.HighlightAndFocus("forgotMobileNo");
			} else {
				$("forgotPINPhoneDetailsHolder").hide();
				$("forgotPINBut").hide();
				var url = "index.cfm?action=Registration_SendForgotPIN&n="+cNumber+"&p="+cPrefix;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.Registration.SendForgotPIN_onSuccess, onFailure:errFunc});
			}
		},
		SendForgotPIN_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.result) {
				AngelinaBallerina.Registration.CancelForgotPIN();
				$("regParentMobileNo").value=z.n;
				$("regParentMobilePrefix").value=z.p;
				alert(z.message);
				AngelinaBallerina.HighlightAndFocus("regPINNumber");
			} else {
				$("forgotPINPhoneDetailsHolder").show();
				$("forgotPINBut").show();
				alert(z.message);				
			}
		}
	}
	,MyAccount : {
		ShopOrders : {
			CancelOrder:function(id,baseHref) {
				var confirmation = confirm("Are you sure you wish to cancel this order?");
				if(confirmation) {
					document.location = baseHref + "index.cfm?action=MyAccount_DeleteOrder&id="+id;
				}
			}
		},
		ValidateEditChild:function() {
			if($("regChildsName").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regChildsName");				
				return false;
			}
			$("butAddChild").hide();
			return true;
		},
		ValidateDetails:function() {
			if($("regSurname").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regSurname");
				return false;
			}
			if($("regParentName").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regParentName");
				return false;
			}
			if($("regAddress1").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regAddress1");
				return false;
			}
			if($("regCounty").value==-1) {
				alert("Please choose the county you reside in");
				return false;
			}
			if($("regParentHomeNo").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regParentHomeNo");
				return false;
			}
			if($("regParentMobileNo").value.blank() || !isNumeric($("regParentMobileNo").value)) {
				AngelinaBallerina.HighlightAndFocus("regParentMobileNo");
				return false;
			}
			if(!$("regParentEmailAddress").value.blank() && !$("regParentEmailAddress").value.isEmail()) {
				AngelinaBallerina.HighlightAndFocus("regParentEmailAddress");
				return false;
			}
			if($("regEmergencyName").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regEmergencyName");
				return false;
			}
			if($("regEmergencyNo").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regEmergencyNo");
				return false;
			}
			$("newUserBut").hide();
			return true;
		},
		ValidateAddChild:function() {
			if($("regChildsName").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("regChildsName");				
				return false;
			}
			new Ajax.Request('index.cfm', {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.MyAccount.ValidateAddChild_onSuccess, onFailure:errFunc, parameters:Form.serialize( $( "registrationForm" ) )});
			$("butAddChild").hide();
			return false;
		},
		ValidateAddChild_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.result) {
				document.location = z.targetUrl;
			} else {
				alert(z.message);
				$("butAddChild").show();
			}
		}		
	}
	,ClassFinder : {
		ShowDays:function(tId,dNum) {
			var url = "index.cfm?action=ClassFinder_ShowDays&tId="+tId+"&dNum="+dNum;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.ClassFinder.ShowDays_onSuccess, onFailure:errFunc});
			return false;
		},
		ShowDays_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("LightBoxHolder").update(z.lightboxHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 400, 400, false );
		},
		ValidateRequestForm:function() {
			if($("name").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("name");
				return false;
			}
			if($("telephone").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("telephone");
				return false;
			}
			if(!$("email").value.blank() && !$("email").value.isEmail()) {
				AngelinaBallerina.HighlightAndFocus("email");
				return false;
			}
			if($("message").value.blank()) {
				AngelinaBallerina.HighlightAndFocus("message");
				return false;
			}
			return true;
		},
		ChangeTerm:function(cId) {
			var termId = $("termId").value;
			$("dataHolder").hide();
			var url = "index.cfm?action=ClassFinder_ChangeTerm&tId="+termId+"&cId="+cId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.ClassFinder.ChangeTerm_onSuccess, onFailure:errFunc});			
		},		
		ChangeTerm_onSuccess:function(t) {
			z = parseJSON( t.responseText );
			$("termDates").update( z.termDatesHTML );
			$("venueId").replace( z.availableVenuesHTML );
			$("dataHolder").show();
		},
		EnroleChild:function(childName,childId,termId,venueId,timeslotId) {
			var confirmation = confirm("Are you sure you wish to enrole " + childName + " in this class?");
			if(confirmation) {
				var url = "index.cfm?action=ClassFinder_EnroleChild&cId="+childId+"&tId="+termId+"&vId="+venueId+"&tsId="+timeslotId;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.ClassFinder.EnroleChild_onSuccess, onFailure:errFunc});
			}
		},
		EnroleChild_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.result) {
				$("classesHolder").update(z.classesHolderHTML);
				if(z.redirect) {
					document.location = z.redirect;
				}
			} else {
				alert(z.message);
			}
		}
	}
	,DateChooser : {
		ChangeDate:function(d,m,y) {
			var cDay = $(d).value;
			var cMonth = $(m).value;
			var cYear = $(y).value;
			var url = "index.cfm?action=DateChooser_ChangeDate&d="+cDay+"&m="+cMonth+"&y="+cYear;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:AngelinaBallerina.DateChooser.ChangeDate_onSuccess, onFailure:errFunc});
		},
		ChangeDate_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("dobDay").replace(z.newSelectHTML);
		}
	}
	,HighlightAndFocus:function(id) {
		new Effect.Highlight($(id));
		$(id).focus();
	}
	,CloseLightBox:function() {
		Element.hide('box');
		Element.hide('overlay');
	}
}

String.prototype.isEmail = function () { 
	var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	var matches = rx.exec(this); 
	return (matches != null && this == matches[0]); 
}

function isNumeric(str) { 
	var y=parseInt(str); 
	if (isNaN(y)) {
	   return false; 
	} else {
		return true;
	}
} 

function LoadingAjax(txt) {
	if(typeof(txt)=="undefined") {
		txt = "Loading...";
	}
	return "<div class='ajaxMsg' id='widgetAjaxMsg'> "+txt+"</div>";
}

function parseJSON( json ){
	var o = eval('(' + json + ')');
	if( o.redirectURL )
	{
		document.location = o.redirectURL;
		return;
	}
	if( $("message") != null ) new Element.remove( "message" );
	if( o.errMsg )
	{
		if( o.errMsgTitle == null ) o.errMsgTitle = "Error";
		if( o.errMsgTimer == null ) o.errMsgTimer = 5000;
		tw.ShowMessage( o.errMsgTitle, o.errMsg, "error", o.errMsgTimer, o.errMsgPosition );
		return o;
	}
	if( o.msgTitle || o.msg )
	{
		if( o.msgTimer == null ) o.msgTimer = 5000;
		tw.ShowMessage( o.msgTitle, o.msg, o.msgClass, o.msgTimer, o.msgPosition );
	}
	return o;
}

var errFunc = function(t) {
	var win = window.open("", "win", "width=1024,height=700,resizable=yes,scrollbars=yes,status=no"); // a window object
	win.document.open("text/html", "replace");
	win.document.write( "<html><body style='margin:0'><div style='border-bottom:1px solid #222;background:#666;padding:10px;'><h1 style='color:#FFF;margin:0;padding:0;'>Digital Crew Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}
