var BiznetCork = {
	MyAccount : {
		Logo : {
			Validate:function() {
				if( $("newLogo").value.blank() ) {
					alert("Please click 'Browse...' to choose a file from your computer to upload");
					return false;
				}
				if( $("newLogo").value.indexOf(".jpg")==-1) {
					alert("Only JPG files are allowed");
					return false;
				}
				return true;
			}
		}
	},
	Newsletter : {
		Validate:function() {
			if( $("email").value.blank() ) {
				new Effect.Highlight($("email"));
				$("email").focus();
				return false;
			}
			return true;
		}
	},
	PaymentForm : {
		NonMembers : {
			Validate:function() {
				if( $("numberOfPlaces").value==-1 ) {
					alert("Please choose the number of places you would like to register");
					return false;
				}
				if( $("firstname").value.blank() ) {
					new Effect.Highlight($("firstname"));
					return false;
				}
				if( $("lastname").value.blank() ) {
					new Effect.Highlight($("lastname"));
					return false;
				}
				if( $("emailAddress").value.blank() ) {
					new Effect.Highlight($("emailAddress"));
					return false;
				}
				if( $("telephone").value.blank() ) {
					new Effect.Highlight($("telephone"));
					return false;
				}
				if( $("address1").value.blank() ) {
					new Effect.Highlight($("address1"));
					return false;
				}
				if( $("city").value.blank() ) {
					new Effect.Highlight($("city"));
					return false;
				}
				if( $("country").value.blank() ) {
					new Effect.Highlight($("country"));
					return false;
				}
				return true;
			}
		}
	},
	ToggleAddCompany:function(type) {
		if(type==1) {
			$("companyNameHolder").show();
			$("addCompanyLink").hide();
			$("companySelectHolder").hide();
			new Effect.Highlight($("companyName"));
			$("companyName").focus();
		} else {
			$("companyNameHolder").hide();
			$("addCompanyLink").show();
			$("companySelectHolder").show();
		}
	},
	Calendar : {
		NextMonth:function(year,month) {
			var url = "index.cfm?action=Calendar_NextMonth&year="+year+"&month="+month;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:BiznetCork.Calendar.NextMonth_onSuccess, onFailure:errFunc});
			return false;
		},
		NextMonth_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("calendarPlaceholder").update(z.calendarHTML);
		},
		PreviousMonth:function(year,month) {
			var url = "index.cfm?action=Calendar_PreviousMonth&year="+year+"&month="+month;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:BiznetCork.Calendar.PreviousMonth_onSuccess, onFailure:errFunc});
			return false;
		},
		PreviousMonth_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			$("calendarPlaceholder").update(z.calendarHTML);
		}
	},	
	Events : {		
		ShowLoginDialog:function(eventId) {
			var url = "index.cfm?action=Dialog_Event_Login&id="+eventId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:BiznetCork.Events.ShowLoginDialog_onSuccess, onFailure:errFunc});			
		},
		ShowLoginDialog_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			BiznetCork.InitialiseLightboxHolder();
			$("LightBoxHolder").update(z.dialogHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 400, 400, false );
			if($("emailAddress")) {
				$("emailAddress").focus();
			}
		},
		ShowCreateAccountDialog:function(eventId) {
			var url = "index.cfm?action=Dialog_Event_Signup&id="+eventId;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:BiznetCork.Events.ShowCreateAccountDialog_onSuccess, onFailure:errFunc});			
		},
		ShowCreateAccountDialog_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			BiznetCork.InitialiseLightboxHolder();
			$("LightBoxHolder").update(z.dialogHTML);	
			MFLightbox.showBoxByID( "LightBoxHolder", 500, 500, false );
			if($("firstname")) {
				$("firstname").focus();
			}
		},
		RegisterForEvent:function(eventId,numberOfPlacesAlreadyBooked) {
			if(numberOfPlacesAlreadyBooked==null) {
				var numberOfPlacesAlreadyBooked = 0;
			}
			var cNumberOfPlaces = $("numberOfPeople").value;
			var url = "index.cfm?action=Event_Register&id="+eventId+"&places=" + cNumberOfPlaces + "&npab=" + numberOfPlacesAlreadyBooked;
			new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:BiznetCork.Events.RegisterForEvent_onSuccess, onFailure:errFunc});
		},		
		RegisterForEvent_onSuccess:function(t) {
			z = parseJSON(t.responseText);
			if(z.complete) {
				if($("eventCount"+z.id)) {
					$("eventCount"+z.id).replace(z.badgeHTML);
					new Effect.Highlight("eventCount"+z.id);
				}
				$("eventBookingWidget").replace(z.widgetHTML);
			} else {
				alert(z.message);
			}			
		},
		CancelRegistration:function(eventId) {
			var confirmation = confirm("Are you sure you want to un-register from this event?");
			if(confirmation) {
				var url = "index.cfm?action=Event_Cancel&id="+eventId;
				new Ajax.Request( url, {asynchronous:true, evalScripts:false, onSuccess:BiznetCork.Events.RegisterForEvent_onSuccess, onFailure:errFunc});
			}
		},
		ChangeRegistration:function() {
			$("changeRequest").hide();
			$("changeBooking").show();
		}				
	},
	InitialiseLightboxHolder:function() {
		if(!$("LightBoxHolder")) {
			$$("body")[0].insert("<div id='LightBoxHolder' style='display:none'>Dan</div>");
		}
	},
	CloseLightBox:function() {
		Element.hide('box');
		Element.hide('overlay');		
	}
}


var CreditCard = {
	IsVisa:function(cc) {
		if(((cc.length == 16) || (cc.length == 13)) && (cc.substring(0,1) == 4)) {
			return CreditCard.IsCreditCard(cc);
		} else {
			 return false;
		}
	},
	IsMasterCard:function(cc) {
		var firstdig = cc.substring(0,1);
		var seconddig = cc.substring(1,2);
		if((cc.length == 16) && (firstdig == 5) && ((seconddig >= 1) && (seconddig <= 5))) {
			return CreditCard.IsCreditCard(cc);
		} else {
			return false;
		}
	},
	IsAmericanExpress:function(cc) {
		var firstdig = cc.substring(0,1);
		var seconddig = cc.substring(1,2);
		if((cc.length == 15) && (firstdig == 3) && ((seconddig == 4) || (seconddig == 7))) {
			return CreditCard.IsCreditCard(cc);
		} else {
			return false;
		}
	},
	IsLaser:function(cc) {
		if((cc.length == 19) && isNumeric(cc)) {
			return true;
		} else {
			return false;
		}
	},
	IsCreditCard:function(st) {
		if(st.length > 19) {
			return false;
		}		
		var sum = 0; 
		var mul = 1; 
		var l = st.length;
		for(i = 0;i<l;i++) {
			digit = st.substring(l-i-1,l-i);
		    tproduct = parseInt(digit ,10)*mul;
		    if (tproduct >= 10) {
		      sum += (tproduct % 10) + 1;
		    } else {
		      sum += tproduct;
			}
		    if(mul == 1) {
		      mul++;
		    } else {
		      mul--;
			}
		}
		
		if((sum % 10) == 0) {
			return true;
		} else {
			return false;
		}
	}
}

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(x) { 
	var y=parseInt(x); 
	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();
}