var W3CDOM =(document.createElement && document.getElementsByTagName);
var ID = function(id){ return document.getElementById(id); };
var Tag = function(tag){ return document.getElementsByTagName(tag); };
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; } }

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

var kom = {
	live : {
		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();
					}
				}
			}
		}
	},
	initiate : function(){
		kom.live.print.initiate();
	}
};

function validate(){
	f=document.frm;
	if(f.uid.value=="")
	{
		f.uid.focus();
		alert("Please enter the user id");
		return false;
	}
	else if(f.pwd.value=="")
	{
		f.pwd.focus();
		alert("Please enter the password");
		return false;
	}
}

function PopWin(url_link,wwidth,wheight){
	newWin=open(url_link, "InfoWindow","toolbar=no,location=no,directories=no,status=no,menubar=no,width="+wwidth+",height="+wheight+",top=100,left=100,screenX=0,screenY=0,resizable=yes,scrollbars=auto");
	newWin.focus();
}

function newwin(object){
	window.open(object.href);
	return false;
}

function callDelete(ID){
	 a=confirm("Are u sure that you want to cancel this appointment");
	if(a)
	{
		window.location=ID;
	 }
}

function AgreeWin(url_link,wwidth,wheight){
	newWin=window.open(url_link, "AgreeWindow","toolbar=no,location=no,directories=no,status=no,menubar=yes,width="+wwidth+",height="+wheight+",top=10,left=10,screenX=0,screenY=0,resizable=yes,scrollbars=yes");
	newWin.focus();
}

function MM_findObj(n, d) {
	var p,i,x;	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() {
	var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
	for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
		if (val) { nm=val.name; if ((val=val.value)!="") {
			if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
				if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
			} else if (test!='R') { num = parseFloat(val);
				if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
				if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
					min=test.substring(8,p); max=test.substring(p+1);
					if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
		} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
	} if (errors) alert('The following error(s) occurred:\n'+errors);
	document.MM_returnValue = (errors == '');
}
function clickdate(){
var f=document.frm;
document.getElementById("from_day").value="";
document.getElementById("from_month").value="";
document.getElementById("from_year").value="";
document.getElementById("to_day").value="";
document.getElementById("to_month").value="";
document.getElementById("to_year").value="";

}

function clicksdate(){
document.getElementById("this_day").value="";
document.getElementById("this_year").value="";
document.getElementById("this_month").value="";
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
	eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
	if (restore) selObj.selectedIndex=0;
}
function admin_classroom_schedule_jump(object){
	parent.location = "/admin/classroom/add.php?location=" + object.value;
}
function madmin_classroom_schedule_jump(object){
	parent.location = "/madmin/schedule/classroom/add.php?location=" + object.value;
}

function confirmAction(object){
	var a=confirm("Are you sure?");
	if(a){
		window.location=object.href;
		return false;
	}else{
		return false;
	}
}
function callDeleteAppointment(object){
	var a=confirm("Are you sure that you want to cancel this appointment?");
	if(a){
		window.location=object.href;
		return false;
	}else{
		return false;
	}
}

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 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 stripPhone(object){
	if(object){
		object.value = object.value.replace(/[^0-9]/g, '');
	}
}

function cloneValue(object,newobject){
	if(object && document.getElementById(newobject)){
		document.getElementById(newobject).value = object.value;
	}
}
function popUpprint(URL,stat){
	if(stat=="F"){
		alert("Please select the starting date and ending date to print the appointments.\n(or)\nSelect a particular date");
		return false;
	}else{
		// URL=URL + "&result="+document.bk.result.value;
		day = new Date();id = day.getTime();eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=1000,height=600,left=10,top=50');");
	}
}
function displayTitle(object){
	if(object){
		alert(object.title);
		return false;
	}
}

function displayLocalTime(utc_object, date, localtime_object_id){
	var returnValue;
	var time_value;
	var hour_time_value;
	var minutes_time_value;
	if(utc_object && document.getElementById(localtime_object_id) && document.getElementById(date)){
		time_value = utc_object.value;
		hour_time_value = time_value.substring(0,2);
		minutes_time_value = time_value.substring(3,5);
		if(hour_time_value > 12){
			returnValue = (hour_time_value - 12)	+ ":" + minutes_time_value + " PM";
		}else{
			if(hour_time_value > 9){
				returnValue = hour_time_value + ":" + minutes_time_value + " AM";
			}else{
				returnValue = hour_time_value.substring(1,2) + ":" + minutes_time_value + " AM";
			}
		}
		document.getElementById(localtime_object_id).innerHTML = returnValue;
	}
}

function isLastClass(object){
	if(object){
		switch(object.value){
			case "LESSON 6":
			case "LESSON 6 Part B":
				document.getElementById("last_class").checked="checked";
			break;
			default:
				document.getElementById("last_class").checked="";
			break;
		}
	}
}
function checkdate(month, date, year){
	var dayobj = new Date(year, month-1, date);
	if((dayobj.getMonth()+1!=month)||(dayobj.getDate()!=date)||(dayobj.getFullYear()!=year)){
		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;
		}
	}
}
function PackageNoteDisplay(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";
		}
	}
}

addListener(window, "load", init);