var W3CDOM =(document.createElement && document.getElementsByTagName);
var ID = function(id){ return (typeof id === "string" ? document.getElementById(id) : id); };
var Tag = function(tag){ return (typeof tag === "string" ? document.getElementsByTagName(tag) : tag); };
var addTableRowCount = 1;
function oc(a){ var o = {}; for(var i=0;i<a.length;i++){ o[a[i]]=''; } return o;}
function addListener(element, type, expression, bubbling){ bubbling = bubbling || false; if(window.addEventListener){ element.addEventListener(type, expression, bubbling); return true; }else if(window.attachEvent){ element.attachEvent("on" + type, expression); return true; }else{ return false; } }

String.prototype.trim = function () { return this.replace(/^\s*/, "").replace(/\s*$/, ""); }

function init(){
	if(!W3CDOM) return;
	kom.initiate();
}

var kom = {
	encode : function(string){
		return encodeURIComponent(string);
	},
	live : {
		layers : {
			create : {
				initiate : function(name){
					if(!ID(name)){
						var layer = document.createElement("div");
						layer.setAttribute("id",name);
						layer.setAttribute("class","hide");
						Tag("body")[0].appendChild(layer);
					}
				}
			}
		},
		/* kom.live.goto();
		 */
		goto : function(object){
			if(ID(object)){
				window.location = object.value;
				return false;
			}
		},
		/* kom.live.stripToInteger();
		 */
		stripToInteger : function(object){
			if(object){
				object.value = object.value.replace(/[^0-9]/g, '');
			}
		},
		/* kom.live.nl2br();
		 */
		nl2br : function(text){
			var re_nlchar;
			text = escape(text);
			if(text.indexOf('%0D%0A') > -1){
				re_nlchar = /%0D%0A/g ;
			}else if(text.indexOf('%0A') > -1){
				re_nlchar = /%0A/g ;
			}else if(text.indexOf('%0D') > -1){
				re_nlchar = /%0D/g ;
			}else{
				re_nlchar = "";
			}
			if(text.indexOf('%0A') > -1 || text.indexOf('%0D') > -1){
				return unescape( text.replace(re_nlchar,'<br>') );
			}else{
				return unescape(text);
			}
		},
		/* kom.live.confirm();
		 */
		confirm : function(object, text){
			text = typeof(text) != 'undefined' ? text : "Are you sure?";
			return confirm(text);
		},
		creditCards : {
			checkType : function(object){
				var visaCard = new Array(40,41,42,43,44,45,46,47,48,49);
				var masterCard = new Array(51,52,53,54,55);
				var americanExpressCard = new Array(34,37);
				var discoverCard = new Array(60,61);
				if(object && object.value.length >= 1){
					if(kom.live.creditCards.validate(object.value) && object.value.length >= 14){
						if(object.value.substring(0,2) in oc(visaCard)){
							ID("credit_card_image").setAttribute("src","/images/visa.png");
						}else if(object.value.substring(0,2) in oc(masterCard)){
							ID("credit_card_image").setAttribute("src","/images/mastercard.png");
						}else if(object.value.substring(0,2) in oc(americanExpressCard)){
							ID("credit_card_image").setAttribute("src","/images/americanexpress.png");
						}else if(object.value.substring(0,2) in oc(discoverCard)){
							ID("credit_card_image").setAttribute("src","/images/discover.png");
						}else{
							ID("credit_card_image").setAttribute("src","/images/blankcard.png");
						}
						/* checking card type #credit_card_image */
						ID("check_credit_card_response").innerHTML = "The credit card number that you entered is valid.";
						ID("check_credit_card_response").setAttribute("class","good");
					}else{
						ID("check_credit_card_response").innerHTML = "The credit card number that you entered is not valid.";
						ID("check_credit_card_response").setAttribute("class","bad");
						ID("credit_card_image").setAttribute("src","/images/blankcard.png");
					}
				}else{
					ID("check_credit_card_response").innerHTML="";
					ID("credit_card_image").setAttribute("src","/images/blankcard.png");
				}
			},
			checkDate : function(object){
				var now = new Date;
				var currentYear = now.getFullYear();
				var currentMonth = now.getMonth();
				currentMonth = currentMonth + 1;
				if(ID("dss_credit_card_expiration_month") && ID("dss_credit_card_expiration_year")){
					if(currentYear > ID("dss_credit_card_expiration_year").value){
						ID("check_credit_card_response").innerHTML = "Your credit card has expired!";
						ID("check_credit_card_response").setAttribute("class","bad");
					}else if(currentYear == ID("dss_credit_card_expiration_year").value && currentMonth > ID("dss_credit_card_expiration_month").value){
						ID("check_credit_card_response").innerHTML = "Your credit card has expired!";
						ID("check_credit_card_response").setAttribute("class","bad");
					}
				}
			},
			validate : function(number){
				var number=number.replace(/\D/g, '');
				var number_length=number.length;
				var parity=number_length % 2;
				var total=0;
				for(i=0; i < number_length; i++){
					var digit=number.charAt(i);
					if(i % 2 == parity){
						digit=digit * 2;
						if(digit > 9){
							digit=digit - 9;
						}
					}
					total = total + parseInt(digit);
				}
				if(total % 10 == 0){
					return true;
				}else{
					return false;
				}
			},
			close : function(object){
				if(ID(object)){
					ID(object).setAttribute("class","hide");
					ID(object).innerHTML = "";
				}
				return false;
			}
		},
		time : {
			get : function(){
				/* kom.live.time.get() */
				var current_date = new Date;
				var unixtime_ms = current_date.getTime();
				var unixtime = parseInt(unixtime_ms / 1000);
				return unixtime;
			}
		},
		forms2tables : {
			initiate : function (){
				var linkElements = Tag("a"), n = linkElements.length;


				if(ID("students_receipts_current_count")){
					addTableRowCount = ID("students_receipts_current_count").value;
				}else{
					addTableRowCount = 1;
				}

				for(var i=0;i<n;i++){
					if(linkElements[i].className.indexOf("student_receipts_form_add_item") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.forms2tables.addStudentReceiptsFields('student_receipts_form');");
					}
				}
			},
			addStudentReceiptsFields : function(object){
				if(ID(object)){
					var columnpre, columnpreHR, rowpre;

					addTableRowCount++;

					if(addTableRowCount <= 10){
						kom.live.forms2tables.addTableRowWithTwoColumns(object, "Receipt", "receipt"+addTableRowCount, "#");
						kom.live.forms2tables.addTableRowWithTwoColumns(object, "Amount Paid", "amount_paid"+addTableRowCount, "$");

						columnpreHR = document.createElement("hr");
						columnpre = document.createElement("td");
						columnpre.setAttribute("colspan","2");
						columnpre.appendChild(columnpreHR);

						rowpre = document.createElement("tr");
						rowpre.appendChild(columnpre);

						ID(object).appendChild(rowpre);

						if(ID("receipt"+addTableRowCount)){
							ID("receipt"+addTableRowCount).focus();
						}
					}else{
						ID("student_receipts_form_add_item").setAttribute("class","hide");
					}
				}
				return false;
			},
			addTableRowWithTwoColumns : function(object, labelText, inputName, preInputText){
				if(ID(object)){
					var column1a, column1aLabel, column1aLabelText,
							column1b, column1bInput, column1bPreInputText,
							row;

					column1aLabelText = document.createTextNode(labelText + ": ");
					column1aLabel = document.createElement("label");
					column1aLabel.setAttribute("for", inputName);
					column1aLabel.appendChild(column1aLabelText);
					column1a = document.createElement("td");
					column1a.setAttribute("class","right");
					column1a.appendChild(column1aLabel);

					column1bPreInputText = document.createTextNode(preInputText);
					column1bInput = document.createElement("input");
					column1bInput.setAttribute("id",   inputName);
					column1bInput.setAttribute("name", inputName);
					column1b = document.createElement("td");
					column1b.setAttribute("class","left");
					column1b.appendChild(column1bPreInputText);
					column1b.appendChild(column1bInput);

					row = document.createElement("tr");
					row.appendChild(column1a);
					row.appendChild(column1b);

					ID(object).appendChild(row);
				}
				return false;
			}
		},
		/* kom.live.toggleDisplay.element(object); */
		toggleDisplay : {
			element : function(object){
				object = ID(object);
				if(object){
					if(object.className.indexOf("showing") !=-1){
						object.setAttribute("class","hiding");
					}else{
						object.setAttribute("class","showing");
					}
				}
				return false;
			},
			siblingRowWithData : function(object){
				/*
					Imagine a structure similar to:
					--------------------------------------
					table(0) > tr(0) > td(0) > a(0)
					table(0) > tr(1)
					--------------------------------------
					We would want to have this method be able to switch
					from a(0) to disable/enable display for tr(1)
				*/
				object = ID(object).parentNode.parentNode.nextSibling.nextSibling;
				if(object){
					if(object.className.indexOf("showing") !=-1){
						object.setAttribute("class","hiding");
					}else{
						object.setAttribute("class","showing");
					}
				}
				return false;
			}
		},
		forms : {
			initiate : function(){
				var formElements = Tag("form"), n = formElements.length;
				for(var i=0;i<n;i++){
					if(formElements[i].className.indexOf("bug_report_form") !=-1){
						formElements[i].setAttribute("onsubmit","return kom.live.forms.sendForm(this);");
					}
					if(formElements[i].className.indexOf("validate_login") !=-1){
						formElements[i].setAttribute("onsubmit","return kom.live.forms.validate(this);");
					}
					if(formElements[i].className.indexOf("newwin") !=-1){
						formElements[i].setAttribute("target","_blank");
					}
				}
				var inputsElements = Tag("input"), n = inputsElements.length;
				for(var i=0;i<n;i++){
					if(inputsElements[i].className.indexOf("signup_same_as_contact_address") !=-1){
						inputsElements[i].setAttribute("onclick","return kom.live.forms.copyContactAddressToBillingAddress(true);");
					}
					if(inputsElements[i].className.indexOf("strip_to_integer") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.stripToInteger(this);");
					}
					if(inputsElements[i].className.indexOf("check_credit_card") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.creditCards.checkType(this);");
					}
					if(inputsElements[i].className.indexOf("check_credit_card_expiration") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.creditCards.checkDate(this);");
					}
					if(inputsElements[i].className.indexOf("check_if_student_username_is_available") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.forms.checkUsernames(this);");
						inputsElements[i].setAttribute("onkeyup","return kom.live.forms.checkUsernames(this);");
						inputsElements[i].setAttribute("autocomplete","off");
					}
					if(inputsElements[i].className.indexOf("clone_value") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.forms.cloneValue(this, this);");
					}
					if(inputsElements[i].className.indexOf("clone_value2pickup_address") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.forms.cloneValue(this, ID('pickup_address'));");
					}
					if(inputsElements[i].className.indexOf("clone_value2pickup_city") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.forms.cloneValue(this, ID('pickup_city'));");
					}
					if(inputsElements[i].className.indexOf("clone_value2pickup_state") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.forms.cloneValue(this, ID('pickup_state'));");
					}
					if(inputsElements[i].className.indexOf("clone_value2pickup_zip") !=-1){
						inputsElements[i].setAttribute("onblur","return kom.live.forms.cloneValue(this, ID('pickup_zip'));");
					}
				}
				var selectElements = Tag("select"), n = selectElements.length;
				for(var i=0;i<n;i++){
					if(selectElements[i].className.indexOf("goto") !=-1){
						selectElements[i].onchange = function(){ return kom.live.goto(this); };
					}
					if(selectElements[i].className.indexOf("set_spanning_dates_to_null") !=-1){
						selectElements[i].onchange = function(){ return kom.live.forms.selects.setSpanningDatesToNull(); };
					}
					if(selectElements[i].className.indexOf("set_this_date_to_null") !=-1){
						selectElements[i].onchange = function(){ return kom.live.forms.selects.setThisDateToNull(); };
					}
					if(selectElements[i].className.indexOf("set_this_date_to_null_and_match_to_dates") !=-1){
						selectElements[i].onchange = function(){ return kom.live.forms.selects.setThisDateToNullAndMatchToDates(this); };
					}
					if(selectElements[i].className.indexOf("copy_this_start_time_to_end_time") !=-1){
						selectElements[i].onchange = function(){ return kom.live.forms.selects.setThisStartTimeToEndTime(this); };
					}
				}
				var optionElements = Tag("option"), n = optionElements.length;
				for(var i=0;i<n;i++){
					if(optionElements[i].className.indexOf("lastclass") !=-1){
						optionElements[i].setAttribute("onclick","return kom.live.forms.setIsLastClass(true);");
					}
					if(optionElements[i].className.indexOf("notlastclass") !=-1){
						optionElements[i].setAttribute("onclick","return kom.live.forms.setIsLastClass(false);");
					}
					if(optionElements[i].className.indexOf("admin_classroom_schedule_jump") !=-1){
						optionElements[i].setAttribute("onclick","return kom.live.forms.optionsList2ClassroomScheduleJump(this, 'admin');");
					}
					if(optionElements[i].className.indexOf("madmin_classroom_schedule_jump") !=-1){
						optionElements[i].setAttribute("onclick","return kom.live.forms.optionsList2ClassroomScheduleJump(this, 'madmin');");
					}
				}
			},
			validate : function(object){
				var uid, pwd;
				if(ID("uid") && ID("pwd")){
					uid = ID("uid");
					pwd = ID("pwd");
					if(uid.value == ""){
						uid.focus();
						alert("Please enter the user id");
						return false;
					}else if(pwd.value == ""){
						pwd.focus();
						alert("Please enter the password");
						return false;
					}
				}
			},
			cloneValue : function(object, newobject){
				if(ID(object) && ID(newobject)){
					ID(newobject).value = ID(object).value;
				}
			},
			loadForm : function(object, url){
				if(ID(object)){
					ID(object).setAttribute("class","show");
					if(ID("ratpack")) url = (ID("ratpack").innerHTML.length > 0 ? url + "?customer="+ID("ratpack").value : "" );
					xhr.get(ID(object), url, false);
					kom.initiate();
				}
				return false;
			},
			sendForm : function(object){
				var url, mkurl;
				if(object.nodeName.toLowerCase() == "form"){
					if(ID("bug_title").value.length == 0){
						alert("Please, enter in a title for this form!");
						ID("bug_title").focus();
						return false;
					}
					if(ID("bug_description").value.length == 0){
						alert("Please, enter in a description for this form!");
						ID("bug_description").focus();
						return false;
					}
					url = object.getAttribute("action");
					for(var i=0;i<object.elements.length;i++){mkurl+="&"+object.elements[i].getAttribute("name")+"="+kom.encode(object.elements[i].value);}
					var unixtime = kom.live.time.get();
					url = (url.indexOf('?') !=-1? url + "&": url + "?") + "xhr=TRUE&modtime="+unixtime+mkurl;
					window.setTimeout(function(){ xhr.get(object,url,'true'); },0);
				}
				return false;
			},
			close : function(object){
				if(ID(object)){
					ID(object).setAttribute("class","hide");
					ID(object).innerHTML = "";
				}
				return false;
			},
			checkUsernames : function(object){
				var url, mkurl, unixtime;
				if(ID(object)){
					ID(object).value = ID(object).value.trim();
					if(ID(object).value.length > 0){
						if(ID("signup_form") && ID("check_if_student_username_is_available")){
							url = ID("signup_form").getAttribute("action");
							url = url.replace("insert","check_if_student_username_is_available");
							mkurl ="&"+ID(object).getAttribute("name")+"="+kom.encode(ID(object).value);
							unixtime = kom.live.time.get();
							url = (url.indexOf('?') !=-1? url + "&": url + "?") + "xhr=TRUE&modtime="+unixtime+mkurl;
							window.setTimeout(function(){ xhr.get(ID("check_if_student_username_is_available"),url,'false'); },0);
						}
					}
				}
				return false;
			},
			copyContactAddressToBillingAddress : function(bool){
				var dss_address1 = ID("dss_address1");
				var dss_address2 = ID("dss_address2");
				var dss_city = ID("dss_city");
				var dss_state = ID("dss_state");
				var dss_zip = ID("dss_zip");
				var dss_phone1 = ID("dss_phone1");
				var dss_credit_card_holder_address1 = ID("dss_credit_card_holder_address1");
				var dss_credit_card_holder_address2 = ID("dss_credit_card_holder_address2");
				var dss_credit_card_holder_city = ID("dss_credit_card_holder_city");
				var dss_credit_card_holder_state = ID("dss_credit_card_holder_state");
				var dss_credit_card_holder_zip = ID("dss_credit_card_holder_zip");
				var dss_credit_card_holder_phone = ID("dss_credit_card_holder_phone");
				if(bool){
					dss_credit_card_holder_address1.value = dss_address1.value;
					dss_credit_card_holder_address2.value = dss_address2.value;
					dss_credit_card_holder_city.value = dss_city.value;
					dss_credit_card_holder_state.value = dss_state.value;
					dss_credit_card_holder_zip.value = dss_zip.value;
					dss_credit_card_holder_phone.value = dss_phone1.value;
				}
			},
			optionsList2ClassroomScheduleJump : function(object, user_level){
				if(ID(object)){
					switch(user_level){
						case "admin":
							window.location = "/admin/classroom/add.php?location=" + ID(object).value;
						break;
						case "madmin":
							window.location = "/madmin/schedule/classroom/add.php?location=" + ID(object).value;
						break;
					}
				}
			},
			setIsLastClass : function(bool){
				if(ID("last_class")){ if(bool){ ID("last_class").value = 1; }else{ ID("last_class").value = 0; } }
				if(ID("last_class_item1")){ if(bool){ ID("last_class_item1").value = 1; }else{ ID("last_class_item1").value = 0; } }
				if(ID("last_class_item2")){ if(bool){ ID("last_class_item2").value = 1; }else{ ID("last_class_item2").value = 0; } }
			},
			selects : {
				/* kom.live.forms.selects.setSpanningDatesToNull()
				 * WAS clickdate()
				 */
				setSpanningDatesToNull : function(){
					if(ID("from_day")) ID("from_day").value="";
					if(ID("from_month")) ID("from_month").value="";
					if(ID("from_year")) ID("from_year").value="";
					if(ID("to_day")) ID("to_day").value="";
					if(ID("to_month")) ID("to_month").value="";
					if(ID("to_year")) ID("to_year").value="";
					return false;
				},
				/* kom.live.forms.selects.setThisDateToNull()
				 * WAS clicksdate()
				 */
				setThisDateToNull : function(){
					if(ID("this_day")) ID("this_day").value="";
					if(ID("this_year")) ID("this_year").value="";
					if(ID("this_month")) ID("this_month").value="";

					return false;
				},
				/* kom.live.forms.selects.setThisDateToNullAndMatchToDates()
				 * WAS clicksdate()
				 */
				setThisDateToNullAndMatchToDates : function(object){
					var toObject;
					if(ID("this_day")) ID("this_day").value="";
					if(ID("this_year")) ID("this_year").value="";
					if(ID("this_month")) ID("this_month").value="";
					switch(object.getAttribute("id")){
						case "from_month":
							toObject = ID("to_month");
						break;
						case "from_day":
							toObject = ID("to_day");
						break;
						case "from_year":
							toObject = ID("to_year");
						break;
					}
					var toObjectOptions = toObject.options, toLength= toObjectOptions.length;
					if(toObject.value == ""){
						while(toLength){
							if(toObjectOptions[--toLength].value == object.value){
								toObjectOptions.selectedIndex = toLength;
								toLength = 0;
							}
						}
					}
					return false;
				},
				/* kom.live.forms.selects.setThisStartTimeToEndTime()
				 */
				setThisStartTimeToEndTime : function(object){
					var endObject, locationTimeLength = 0, tempObject,
							tempObjectValue = 0, tmp, splicedEndObjectOptionsValue = 0,
							endObjectOptionsValue = 0, endObjectOptionsValueFirstNode = 0,
							endLengthAndLocationTimeLength = 0,
							filteredEndObjectOptionsValue = 0,
							isCopied = false,
							forHours = false,
							forMinutes = false,
							fooBar = 1;
					if(ID(object) && ID("location_time_length")){
						locationTimeLength = parseInt(ID("location_time_length").value);
						if(locationTimeLength.length == 0){
							locationTimeLength = 2;
						}
						switch(object.getAttribute("id")){
							case "start_time_hours":
								endObject = ID("end_time_hours");
							break;
							case "start_time_minutes":
								endObject = ID("end_time_minutes");
							break;
						}
						tempObjectValue = parseInt(object.value);

						tmp = endObject.value;

						var endObjectOptions = endObject.options, endLength = endObjectOptions.length;

						var testTMP1 = parseInt(endLength) + parseInt(locationTimeLength);
						while(endLength){
							filteredEndObjectOptionsValue = parseInt(endObjectOptions[--endLength].value);
							if(filteredEndObjectOptionsValue == tempObjectValue){
								if(object.getAttribute("id") == "start_time_hours"){
									endLengthAndLocationTimeLength = endLength + locationTimeLength;
									if(endLengthAndLocationTimeLength >= 24){
										endLengthAndLocationTimeLength = endLengthAndLocationTimeLength - 24;
									}
									endObjectOptions.selectedIndex = endLengthAndLocationTimeLength;
									isCopied = true;
									forHours = true;
								}else{
									endObjectOptions.selectedIndex = endLength;
									isCopied = true;
									forMinutes = true;
								}

								endLength = 0;
							}
						}
					}
					return false;
				},
				/* kom.live.forms.selects.packageNoteDisplay()
				 * WAS PackageNoteDisplay()
				 */
				packageNoteDisplay : function(object, type){
					if(object && document.getElementById("birthdate_labels") && document.getElementById("birthdate_fields")){
						if(object.nodeName.toLowerCase() == 'select'){
							document.getElementById("package_note").innerHTML = object.options[object.selectedIndex].title;
							if(object.options[object.selectedIndex].innerHTML.search(/adult/i) != -1){
								type = "adult";
							}else{
								type = "teenager";
							}
						}else{
							document.getElementById("package_note").innerHTML = object.getAttribute("title");
						}
						if(type == "adult"){
							document.getElementById("birthdate_labels").style.display="none";
							document.getElementById("birthdate_fields").style.display="none";
						}else if(type == "teenager"){
							document.getElementById("birthdate_labels").style.display="block";
							document.getElementById("birthdate_fields").style.display="block";
						}
					}
					return false;
				}
			}
		},
		links : {
			initiate : function(){
				var text = '';
				var linkElements = Tag("a"), n = linkElements.length;
				for(var i=0;i<n;i++){
					if(linkElements[i].className.indexOf("newwin") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.links.newwin(this);");
					}
					if(linkElements[i].className.indexOf("close_bug_report") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.forms.close('bug_report_form');");
					}
					if(linkElements[i].className.indexOf("close_bug_window") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.forms.close('bug_report_form');");
					}
					if(linkElements[i].className.indexOf("load_url_in_xhr_layer") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.page.loadInXHR(this, 'xhr_layer');");
					}
					if(linkElements[i].className.indexOf("bug_report") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.forms.loadForm('bug_report_form', this.href);");
					}
					if(linkElements[i].className.indexOf("toggledisplay") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.toggleDisplay.element(this);");
					}
					if(linkElements[i].className.indexOf("toggledisplay_forsiblingrowwithdata") !=-1){
						linkElements[i].setAttribute("onclick","return kom.live.toggleDisplay.siblingRowWithData(this);");
					}
					if(linkElements[i].className.indexOf("confirm_action") !=-1){
						text = "Are you sure about this?";
						linkElements[i].setAttribute("onclick","return kom.live.confirm(this, '" + text + "');");
					}
					if(linkElements[i].className.indexOf("confirm_delete_appointment") !=-1){
						text = "Are you sure that you want to cancel this appointment?";
						linkElements[i].setAttribute("onclick","return kom.live.confirm(this, '" + text + "');");
					}
					if(linkElements[i].title.length >= 1){
						linkElements[i].setAttribute("onmouseover","return tooltip.show(kom.live.nl2br(this.title));");
						linkElements[i].setAttribute("onmouseout","return tooltip.hide();");
					}
				}
			},
			newwin : function(object){
				window.open(object.href);
				return false;
			}
		},
		page : {
			closeLayer : function(loadInLayer){
				if(ID(loadInLayer)){
					ID(loadInLayer).setAttribute("class","hide");
					ID(loadInLayer).innerHTML="";
				}
				return false;
			},
			loadCloseInXHR : function(loadInLayer){
				var hasBreadcrumb, breadcrumb, divs, i, linkText, link, linkSpan, linkSeparator;
				if(ID(loadInLayer)){
					divs = ID(loadInLayer).getElementsByTagName("div");
					for(i in divs){
						if(/breadcrumb/.test(divs[i].className)){
							breadcrumb = divs[i];
							hasBreadcrumb = true;
							break;
						}else{
							hasBreadcrumb = false;
						}
					}
					linkText = document.createTextNode("Close");
					link = document.createElement("a");
					link.setAttribute("id","close_layer");
					link.setAttribute("class","close_layer");
					link.setAttribute("href","#");
					link.setAttribute("onclick","return kom.live.page.closeLayer('"+loadInLayer+"');");
					link.appendChild(linkText);
					if(hasBreadcrumb == false){
						ID(loadInLayer).appendChild(link);
					}else{
						link.setAttribute("class","null");
						linkSpan = document.createElement("span");
						linkSeparator = document.createTextNode(" | ");
						linkSpan.appendChild(linkSeparator);
						linkSpan.appendChild(link);
						breadcrumb.appendChild(linkSpan);
					}
				}
				return false;
			},
			loadInXHR : function(linkObject, loadInLayer){
				if(ID(loadInLayer)){
					var unixtime = kom.live.time.get();
					url = linkObject.href;
					url = (url.indexOf('?') !=-1? url + "&": url + "?") + "xhr=TRUE&modtime="+unixtime;
					window.setTimeout(function(){ xhr.get(ID(loadInLayer),url,'true'); },0);
					window.setTimeout(function(){ ID(loadInLayer).setAttribute("class","show_small"); },400);
					window.setTimeout(function(){ ID(loadInLayer).setAttribute("style","top:10%; left:10%;bottom:10%; right:10%; width:80%; height:80%;"); },450);
				}
				return false;
			}
		},
		images : {
			initiate : function(){
				var imagesElements = Tag("img"), n = imagesElements.length;
				for(var i=0;i<n;i++){
					if(imagesElements[i].className.indexOf("bug_report") !=-1){
						imagesElements[i].setAttribute("onclick","return kom.live.forms.loadForm('bug_report_form', '/bug_report.php');");
						imagesElements[i].setAttribute("onmouseover","return kom.live.images.changePicture(this, '/images/bug_report_hover.gif');");
						imagesElements[i].setAttribute("onmouseout","return kom.live.images.changePicture(this, '/images/bug_report.gif');");
					}
				}
			},
			changePicture : function(object, src){
				if(object){
					object.setAttribute("src",src);
				}
				return false;
			}
		},
		print : {
			initiate : function(){
				var bodyElements = Tag("body"), n = bodyElements.length;
				for(var i=0;i<n;i++){
					if(bodyElements[i].className.indexOf("print") !=-1){
						window.print();
					}
				}
			}
		},
		form : {
			validate : function(object){
				var uid, pwd;
				if(ID("uid") && ID("pwd")){
					uid = ID("uid");
					pwd = ID("pwd");
					if(uid.value == ""){
						uid.focus();
						alert("Please enter the user id");
						return false;
					}else if(pwd.value == ""){
						pwd.focus();
						alert("Please enter the password");
						return false;
					}
				}
			},
			demovalidate : function(object,formID){
				var uid, pwd;
				if(ID("uid"+formID) && ID("pwd"+formID)){
					uid = ID("uid"+formID);
					pwd = ID("pwd"+formID);
					if(uid.value == ""){
						uid.focus();
						alert("Please enter the user id");
						return false;
					}else if(pwd.value == ""){
						pwd.focus();
						alert("Please enter the password");
						return false;
					}
				}
			}
		},
		elements : {
			initiate : function(){
				var spanElements = Tag("span"), n = spanElements.length;
				for(var i=0;i<n;i++){
					if(spanElements[i].title.length >= 1){
						spanElements[i].setAttribute("onmouseover","return tooltip.show(kom.live.nl2br(this.title));");
						spanElements[i].setAttribute("onmouseout","return tooltip.hide();");
					}
				}
			}
		}
	},
	initiate : function(){
		kom.live.elements.initiate();
		kom.live.forms.initiate();
		kom.live.images.initiate();
		kom.live.links.initiate();
		kom.live.print.initiate();
		kom.live.forms2tables.initiate();
		kom.live.layers.create.initiate("xhr_layer");
		if(ID("uid")) ID("uid").setAttribute("autocomplete","off");
	}
};

var xhr_reqs = new Array();
var xhr = {
	initiate: function(type, xhrobj){
		this.type = type;
		this.xhrobj = xhrobj;
	},
	post: function(object,url,parameters,focus){
		/* xhr.post(object, url, parameters, focus); */
		var xhrobj = false;
		if(window.XMLHttpRequest){
			xhrobj=new XMLHttpRequest();
			if(xhrobj.overrideMimeType){
				/* xhrobj.overrideMimeType('text/xml'); */
				xhrobj.overrideMimeType('text/html');
			}
		}else if(window.ActiveXObject){
			try{
				xhrobj = new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e){
				try{
					xhrobj = new ActiveXObject("Microsoft.XMLHTTP");
				}catch (e) {}
			}
		}
		if(!xhrobj){
			alert('Cannot create XMLHTTP instance'); return false;
		}
		if(xhrobj){
			xhrobj.onreadystatechange = function(){
				xhr.set(object,focus);
			};
			xhrobj.open('POST', url, true);
			xhrobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xhrobj.setRequestHeader("Content-length", parameters.length);
			xhrobj.setRequestHeader("Connection", "close");
			xhrobj.send(parameters);
		}
		var xmlreq = new xhr.initiate('', xhrobj);
		xhr_reqs.push(xmlreq);
	},
	get: function(object,url,focus){
		/* xhr.get(object,url,focus) */
		var xhrobj=false;
		if(window.XMLHttpRequest){
			xhrobj=new XMLHttpRequest();
			xhrobj.onreadystatechange = function(){
				xhr.set(object,focus);
			};
			xhrobj.open("GET",url,true);
			xhrobj.send(null);
		}else if(window.ActiveXObject){
			try{
				xhrobj = new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e){
				try{
					xhrobj = new ActiveXObject("Microsoft.XMLHTTP");
				}catch (e){}
			}
			if(xhrobj){
				xhrobj.onreadystatechange = function(){
					xhr.set(object,focus);
				};
				xhrobj.open("GET",url,true);
				xhrobj.send();
			}
		}
		var xmlreq = new xhr.initiate('', xhrobj);
		xhr_reqs.push(xmlreq);
	},
	set: function(object,focus){
		if(typeof(window['xhr_reqs']) == "undefined") return;
		var xhr_doc = null;
		for(var i=0; i<xhr_reqs.length; i++){
			if(xhr_reqs[i].xhrobj.readyState == 4){
				if(xhr_reqs[i].xhrobj.status == 200 || xhr_reqs[i].xhrobj.status == 304){
					if(document.implementation && document.implementation.createDocument){
						xhr_doc = document.implementation.createDocument("", "", null);
					}else if(window.ActiveXObject){
						xhr_doc = new ActiveXObject("Microsoft.XMLDOM");
					}
					xhr_doc = xhr_reqs[i].xhrobj.responseText;
					xhr_reqs.splice(i,1);
					i--;
					var responses=xhr_doc;
					this.response(object, responses, focus);
				} else {
					xhr_reqs.splice(i,1);
					i--;
				}
			}
		}
	},
	response: function(object, responses, focus){
		var msg = '',url='', closeLink='';
		if(object.nodeName.toLowerCase() == "input"){
			object.value=responses.replace(/&quot;/g,"\"");
		}else{
			object.innerHTML=responses;
			if(object.getAttribute("id") == "xhr_layer"){
				kom.live.page.loadCloseInXHR("xhr_layer");
			}
		}
		kom.initiate();
	}
};

function confirmStudentActivationForBTW(object, name, username){
	var a = confirm("Are you sure that you want to activate this student:\n\nStudent Name:\n			 "+name+"\nUser ID:\n			 "+username+"\n\nAre you sure?");
	if(a){
		return true;
	}else{
		return false;
	}
}

function confirmStudentDeletion(object, name, username){
	var a = confirm("Are you sure that you want to delete this student:\n\nStudent Name:\n			 "+name+"\nUser ID:\n			 "+username+"\n\nAre you sure?");
	if(a){
		return true;
	}else{
		return false;
	}
}

function confirmStudentBookingForClassroomBooking(object, title, date, time){
	var a = confirm("Are you sure that you want to book:\n\nTitle:\n			 "+title+":\nDate:\n			 "+date+"\nTime:\n			 "+time+"\n\nAre you sure?");
	if(a){
		return true;
	}else{
		return false;
	}
}

function confirmStudentBookingForRemedialBooking(object, title, date, time){
	var a = confirm("Are you sure that you want to book:\n\nTitle:\n			 "+title+":\nDate:\n			 "+date+"\nTime:\n			 "+time+"\n\nAre you sure?");
	if(a){
		return true;
	}else{
		return false;
	}
}

function confirmStudentBookingForBTWBooking(object, date, time){
	var a = confirm("Are you sure that you want to book:\n\nDate:\n			 "+date+"\nTime:\n			 "+time+"\n\nAre you sure?");
	if(a){
		return true;
	}else{
		return false;
	}
}
function solveBirthDate(){
	var index=document.getElementById('bmonth').selectedIndex;
	var month=document.getElementById('bmonth').options[index].value;

	var index=document.getElementById('bday').selectedIndex;
	var date=document.getElementById('bday').options[index].value;

	var index=document.getElementById('byear').selectedIndex;
	var year=document.getElementById('byear').options[index].value;

	if(month!='0'&&date!='0'&&year!='0'){
		if(checkdate(month, date, year)==false){
			alert("Invalid Day, Month, or Year range detected. Please correct and click ok.")
			return false;
		}
	}else{
		alert('Please select valid date of birth.');
	}
}

function checkAge(method){
	var fields_empty = "Please, enter a birth date";
	var if_not_old_enough = "Sorry, you have to be 15 years and 5 months old to start driving school";
	var notice_to_user = document.getElementById('notice_to_user');
	var year = parseInt(document.getElementById('byear').value);
	var month = parseInt(document.getElementById('bmonth').value);
	var day = parseInt(document.getElementById('bday').value);

	var hyear = parseInt(document.getElementById('hbyear').value);
	var hmonth = parseInt(document.getElementById('hbmonth').value);
	var hday = parseInt(document.getElementById('hbday').value);

	if(document.getElementById("birthdate_fields") && document.getElementById("birthdate_fields").style.display == "none"){
		notice_to_user.innerHTML = "";
		return true;
	}else{
		if(year != "" && month != "0" && day != "0"){
			if(year > hyear){
				notice_to_user.innerHTML = "";
				notice_to_user.innerHTML = if_not_old_enough;
				if(method == "form") alert(if_not_old_enough);
				notice_to_user.focus();
				return false;
			}else if(year == hyear && month > hmonth){
				notice_to_user.innerHTML = "";
				notice_to_user.innerHTML = if_not_old_enough;
				if(method == "form") alert(if_not_old_enough);
				notice_to_user.focus();
				return false;
			}else if(year == hyear && month == hmonth && day > hday){
				notice_to_user.innerHTML = "";
				notice_to_user.innerHTML = if_not_old_enough;
				if(method == "form") alert(if_not_old_enough);
				notice_to_user.focus();
				return false;
			}else{
				notice_to_user.innerHTML = "";
				return true;
			}
		}else{
			notice_to_user.innerHTML = "";
			notice_to_user.innerHTML = fields_empty;
			notice_to_user.focus();
			return false;
		}
	}
}

var tooltip=function(){
	var id = 'tt';
	var top = 3;
	var left = 3;
	var maxw = 300;
	var speed = 10;
	var timer = 20;
	var endalpha = 95;
	var alpha = 0;
	var tt,t,c,b,h;
	var ie = document.all ? true : false;
	return{
		show:function(v,w){
			if(tt == null){
				tt = document.createElement('div');
				tt.setAttribute('id',id);
				t = document.createElement('div');
				t.setAttribute('id',id + 'top');
				c = document.createElement('div');
				c.setAttribute('id',id + 'cont');
				b = document.createElement('div');
				b.setAttribute('id',id + 'bot');
				tt.appendChild(t);
				tt.appendChild(c);
				tt.appendChild(b);
				document.body.appendChild(tt);
				tt.style.opacity = 0;
				tt.style.filter = 'alpha(opacity=0)';
				document.onmousemove = this.pos;
			}
			tt.style.display = 'block';
			c.innerHTML = v;
			tt.style.width = w ? w + 'px' : 'auto';
			if(!w && ie){
				t.style.display = 'none';
				b.style.display = 'none';
				tt.style.width = tt.offsetWidth;
				t.style.display = 'block';
				b.style.display = 'block';
			}
			if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
			h = parseInt(tt.offsetHeight) + top;
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(1)},timer);
		},
		pos:function(e){
			var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
			var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
			tt.style.top = (u - h) + 'px';
			tt.style.left = (l + left) + 'px';
		},
		fade:function(d){
			var a = alpha;
			if((a != endalpha && d == 1) || (a != 0 && d == -1)){
				var i = speed;
				if(endalpha - a < speed && d == 1){
					i = endalpha - a;
				}else if(alpha < speed && d == -1){
					i = a;
				}
				alpha = a + (i * d);
				tt.style.opacity = alpha * .01;
				tt.style.filter = 'alpha(opacity=' + alpha + ')';
			}else{
				clearInterval(tt.timer);
				if(d == -1){tt.style.display = 'none'}
			}
		},
		hide:function(){
			clearInterval(tt.timer);
			tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
		}
	};
}();


addListener(window, "load", init);
