﻿var modalOnLoad = Lightbox.prototype.modalOnLoad;
Lightbox.prototype.modalOnLoad = function(modal) {
	var doc = $('lightboxPage').contentDocument || $('lightboxPage').contentWindow.document;

	// remove default event handler from form's submit event
	if(doc.forms[0]) { Event.observe(doc.forms[0],'submit',function(){ return; }); }

	var frameElems = doc.getElementsByTagName('*');
	for(var i=0; i<frameElems.length; i++) {
		// ASP.NET Button/ImageButton
		if(frameElems[i].nodeName.toLowerCase() == 'input') {
			if(frameElems[i].getAttribute('onclick')) {
				if(frameElems[i].getAttribute('onclick').toString().toLowerCase().indexOf('dopostback') != -1) {
					Event.observe(frameElems[i],'click',function(){
						var isPageValid = true;
						if(typeof($('lightboxPage').contentWindow.Page_ClientValidate) == 'function') {
							isPageValid = $('lightboxPage').contentWindow.Page_ClientValidate();
						}
						if(isPageValid) { myLightbox.changePage(); }
					});
				}
			}
		}
		// ASP.NET LinkButton
		if(frameElems[i].nodeName.toLowerCase() == 'a') {
			if(frameElems[i].getAttribute('href') && frameElems[i].getAttribute('href').toLowerCase().indexOf('dopostback') != -1) {
				Event.observe(frameElems[i],'click',function(){ myLightbox.changePage(); });
			}
		}
	}
	modalOnLoad(modal);
}
