// JavaScript Document
//-----------------------------------------------//
//         JavaScript FUNCTIONS                  //
//        BabyFace Software, Inc.                //
//          www.babyfaceinc.com                  //
//-----------------------------------------------//
//       for Tim Wood Design, LLC                //
//-----------------------------------------------//
//~~~~~~~~~~~~~~~~~~~~~~~~Form Validation~~~~~~~~~~~~~~~~~~~~~~~~//

function msg_valid(myform){
	//second date validation is broken into another script for the sake of mac ie
	if (myform.date.value == '' || !date_validate(myform.date.value)){
		alert("Please enter a date in the format mm/dd/yyyy.");
		myform.date.focus();
		return false;
	}
	
	if (myform.db.value != 'director' && myform.title.value == ''){
		alert("Please enter a title.");
		myform.title.focus();
		return false;
	}
	
	if (myform.detail.value == ''){
		alert("Please enter the details.");
		myform.detail.focus();
		return false;
	}
	return true;
}
//~~~~~~~~~~~~~~~~~~~~~~Deletion Confirmation~~~~~~~~~~~~~~~~~~~~~~//
function confirm_del(pg, db, id){
	var msg = 'You are about to permanently delete this record from the database.\n\nClick "Ok" to continue';
	if (confirm(msg))
		location.replace('edit_delete.php?pg='+pg+'&db='+db+'&id='+id);
}
//~~~~~~~~~~~~~~~~~~~~~~~~Platform Handlers~~~~~~~~~~~~~~~~~~~~~~~~//

function macie_sniff(){
	//sniffs for the browser from hell
	var agt=navigator.userAgent.toLowerCase();
	var is_mac    = (agt.indexOf("mac")!=-1);
	var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));	
	if (is_mac && is_ie)
		return 1;
	return 0;
}

function macie_msg(){
	if (macie_sniff())
		document.write('<p style="color: #FFC20D; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 12px;"><strong>Notice</strong>: this website is not fully compatible with Macintosh versions of Microsoft Internet Explorer. For best results please use Safari or <a href="http://www.mozilla.com/firefox/" target="_blank" style="color: #FFC20D;">Firefox</a>.</p>');
}
///////////////////////// AJAX Functions /////////////////////////
/* The following function creates an XMLHttpRequest object... */
function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}
/* The variable http will hold our new XMLHttpRequest object. */
//var http = createRequestObject(); 
function callScript(myScript, myDivId){
	http.open('get', myScript);
	//http.onreadystatechange = handleText; 
	http.onreadystatechange = function(){
		if(http.readyState == 4){ //Finished loading the response
			var response = http.responseText;
			document.getElementById(myDivId).innerHTML = response;
		}
	};
	http.send(null);
}
//for events page - builds WHERE clause
function refreshEventTbl(myForm, myScript){
	var myDivId = 'event_cage';
	var myTxtDivId = 'pagetitle';
	var myMonth = myForm.month.value;
	var myType = myForm.type.value;
	var myTWhere = '';
	var myMWhere = '';
	var myWhere = '';
	var myCall = '';
	if (myMonth != '0' && myMonth != '-1')
		myMWhere = "DATE_FORMAT(`date`,'%c')="+myMonth;
	if (myType != '0' && myType != '-1')
		myTWhere = '`type` = "'+myType+'"';
	myWhere = myMWhere;
	if (myWhere != '' && myTWhere != '')
		myWhere += ' AND ';
	myWhere += myTWhere;
	
	
	myCall = myScript+'?where='+escape(myWhere);
	//alert(unescape(myCall));
	//AJAX
	callScript(myCall, myDivId);
	//set document title
	var typeTxt = '';
	if (myTWhere != ''){
		var typeAr = Array('', 'General', 'Kids', 'Story Time', 'Special' , 'Teen' , 'Adult' , 'Holidays' , 'Book Sales' , 'Computer');
		typeTxt = typeAr[myType];
	}
	var monthTxt = '';
	if (myMWhere != ''){
		var monthAr = Array('', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
		monthTxt = monthAr[myMonth];
	}		
	var defEvTxt = 'Programs';
	var altEvTxt = '';
	if ( monthTxt != '')
		altEvTxt += monthTxt;
	if (typeTxt != '' &&  monthTxt != '')
		altEvTxt += ' ';
	if (typeTxt != '')
		altEvTxt += typeTxt;
	if (altEvTxt != '')
		defEvTxt = altEvTxt + ' Programs';
	
	document.getElementById(myTxtDivId).innerHTML = defEvTxt;
}










//------------------------------------------------------//
//------------------------------------------------------//
//------------------------------------------------------//
//Calendar Scripts Licensed to BabyFace Software, Inc.
function KW_cal_class(m,d,y,d1,d2,mn,c) { // v1.3.0
	this.o="";this.dsp="";this.m=m;this.d=d;this.y=y;this.d1=d1;this.d2=d2;
	this.mn=mn,this.sy=((y%100)<10)?"0"+(y%100):(y%100);this.mm=(m<10)?"0"+m:m;this.c=c
	this.dd=(d<10)?"0"+d:d;	iD=new Date(y,(m-1),d);eD=new Date();eD.setMilliseconds(0)
	eD.setHours(0);eD.setMinutes(0);eD.setSeconds(0);this.ofs=parseInt((eD-iD)/86400000)
	this.gC=function(){iD=new Date(this.y,(this.m-1),this.d);sD=this.dsp.toString()
	retVal="on";if (sD.indexOf("<a")!=0)retVal="off";if (iD.getDay()==0||iD.getDay()==6)
	if (sD.indexOf("<a")!=0)retVal="wkendoff";else retVal="wkendon";if (this.c==0)
	if (sD.indexOf("<a")!=0)retVal="ntmoff";else retVal="ntmon";if (this.ofs==0) 
	retVal="today";	if (this.spc()==true)retVal="special";return "kw_cal_"+retVal;}
	this.spc=function(){var retVal=false;dc=document;if (dc.kw_sp){ for (var i=0;i<dc.kw_sp.length;i++){
	if (this.m==dc.kw_sp[i].m&&(this.y==dc.kw_sp[i].y||dc.kw_sp[i].y=="*")&&this.d==dc.kw_sp[i].d)
	retVal=true;}} return retVal;}
}

function KW_setThisDisplay(f,n,d,o,a1,a2,a3,a4,w,l,sd){ // v1.4.0
	var rV="<a href=\"javascript:window.opener.KW_setCalendar('"+f+"',"+n+",'"+l+"'); window.close();\">"+d+"</a>";
	if ((a1==1&&o>0)||(a1==-1&&o<1)||(a1==2&&o>=0)) rV=d;if (a2>0) {if (o<=-a2) rV=d;} 
	else if (a2<0) {if (o>=-a2) rV=d;}if (a3!=-1) {ss=a3.split("|");if (ss[w]==1) rV=d} 
	if (a4>0) {if (o>=-a4 && o<0) rV=d;} if (a4<0) {if (o<=-a4 && o>0) rV=d; } if (sd) {
	rV=d} return rV;
}

function KW_date_class(m,d,y) {this.m=m;this.d=d;this.y=y;}

function KW_setCalendar(obj,n,l) { // v1.3.0
	dc=document;dO=dc.tMh[n];if (obj.indexOf("|")==-1)MM_findObj(obj).value=dO.o;
	else {oS=obj.split("|");tV= new Array(dO.m,dO.d,dO.y);for(var i=0;i<3;i++) {
	tO=MM_findObj(oS[i]);dc.KW_calYear=dO.y;for (var j=0;j<tO.options.length;j++)
	if (tO.options[j].value==tV[i]) tO.selectedIndex=j;}}dc.KW_calMonth=dO.m-1;
	if (l!=''){o=MM_findObj(l);v="hide";if (o.style){o=o.style; v='hidden'} o.visibility=v}
}

function MM_findObj(n, d) { //v4.01
  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 KW_setDel(val) { // v1.3.1
	return (val.match(/\.gif$|\.png$|\.jpg$|\.jpeg$/i))?"<img src=\""+val+"\" class=\"kw_img\">":val
}

function KW_expertCalendar(){ // v1.4.1 
	var dc=document,a=KW_expertCalendar.arguments,d=new Date();d.setDate(1); if (a[29]!=-1) 
	d.setMonth(a[29]);else if (dc.KW_calMonth) d.setMonth(dc.KW_calMonth);cMn= d.getMonth(); 
	pMt=((cMn-1)<0)?11:cMn-1;nMh=((cMn+1)>11)?0:cMn+1;if (a[30]!=-1) d.setFullYear(a[30]);
	else if (dc.KW_calYear) d.setYear(dc.KW_calYear);cYr=d.getFullYear();pYr=(pMt==11)?cYr-1:cYr;
	nYr=(nMh==0)?cYr+1:cYr;wdy=d.getDay();dc.tMh=new Array();if (a[25]==1) wdy=((--wdy)<0)?6:wdy;
	tpM=new Date(d.getFullYear(),d.getMonth(),d.getDate()-wdy);for (var i=0;i<wdy;i++) {
	n=dc.tMh.length;dc.tMh[n]= new KW_cal_class((tpM.getMonth()+1),tpM.getDate(),tpM.getFullYear(),
	a[21],a[23],a[tpM.getMonth()],0);dc.tMh[n].o=eval("dc.tMh["+n+"]."+a[20])+dc.tMh[n].d1+
	eval("dc.tMh["+n+"]."+a[22])+dc.tMh[n].d2+eval("dc.tMh["+n+"]."+a[24]);	dc.tMh[n].dsp=
	(a[31]==1)?"&nbsp;":KW_setThisDisplay(a[19],n,tpM.getDate(),dc.tMh[n].ofs,a[26],a[27],
	a[28],a[42],tpM.getDay(),a[41],(dc.tMh[n].spc() && a[45]==1));tpM.setDate(tpM.getDate()+1)}EOM=false;for (var i=1;!EOM;i++) {
	n=dc.tMh.length;dc.tMh[n]=new KW_cal_class((d.getMonth()+1),d.getDate(),d.getFullYear(),
	a[21],a[23],a[d.getMonth()],1);;dc.tMh[n].o=eval("dc.tMh["+n+"]."+a[20])+dc.tMh[n].d1+
	eval("dc.tMh["+n+"]."+a[22])+dc.tMh[n].d2+eval("dc.tMh["+n+"]."+a[24]);
	dc.tMh[n].dsp=KW_setThisDisplay(a[19],n,d.getDate(),dc.tMh[n].ofs,a[26],a[27],a[28],a[42],
	d.getDay(),a[41],(dc.tMh[n].spc() && a[45]==1));d.setDate(d.getDate()+1);if (d.getDate()==1) EOM=true;} wdy=d.getDay();
	if (a[25]==1) wdy=((--wdy)<0)?6:wdy;for (var i=wdy;i<7;i++) {n=dc.tMh.length;
	dc.tMh[n]= new KW_cal_class((d.getMonth()+1),d.getDate(),d.getFullYear(),a[21],a[23],
	a[d.getMonth()],0);dc.tMh[n].o=eval("dc.tMh["+n+"]."+a[20])+dc.tMh[n].d1+
	eval("dc.tMh["+n+"]."+a[22])+dc.tMh[n].d2+eval("dc.tMh["+n+"]."+a[24]);
	dc.tMh[n].dsp=(a[31]==1)?"&nbsp;":KW_setThisDisplay(a[19],n,d.getDate(),dc.tMh[n].ofs,
	a[26],a[27],a[28],a[42],d.getDay(),a[41],(dc.tMh[n].spc() && a[45]==1));d.setDate(d.getDate()+1);} 
	ns4 =(dc.layers)?true:false;px=(ns4||window.opera)?'':'px';s=a[32];if (a[33]==-1) 
	{ww=window;d="document.getElementById('"+a[32].name+"')";k=1;x=0;y=0;xpos=0;ypos=0;
	iM=(navigator.appVersion.indexOf("Mac")==-1);p=".offsetParent";if (dc.getElementById) {
	if (!eval(d)) {d=d.replace(/.getElementById/,".getElementsByName")}
	while (eval(d+p)) {	x+=parseInt(eval(d+p+".offsetLeft"));
	y+=parseInt(eval(d+p+".offsetTop"));p+=".offsetParent";}ox=parseInt(s.offsetLeft);
	oy=parseInt(s.offsetTop);} else {x=parseInt(screen.height/2-75);ox=0;oy=0;
	y=parseInt(screen.width/2-75);}xp=parseInt(x+ox),yp=parseInt(y+oy+20);
	winW=(ns4)?window.innerWidth-16:dc.body.offsetWidth-20;
	winH=(ns4)?window.innerHeight:dc.body.offsetHeight;if (dc.all && iM) winH+=dc.body.scrollTop
	if ((xp+a[35])>winW) xp=winW-a[35]; if ((yp+a[36])>winH) yp=winH-a[36];	
	if (a[41]=='') {if (isNaN(ww.screenX)) {xp=xp-dc.body.scrollLeft+ww.screenLeft;
	yp=yp-dc.body.scrollTop+ww.screenTop;}else {xp=xp+ww.screenX+
	(ww.outerWidth-ww.innerWidth)-ww.pageXOffset;yp=yp+ww.screenY+
	(ww.outerHeight-24-ww.innerHeight)-ww.pageYOffset;	}if (!iM && dc.all)
	{ xp=xp-dc.body.scrollLeft;yp=yp-dc.body.scrollTop}}a[33]=xp;a[34]=yp}
	str="<html><head><title>"+a[cMn]+" "+cYr; accs="<a class=\"kw_cal_a\" ";
	str+="</title></head><link href=\""+a[43]+"\" rel=\"stylesheet\" type=\"text/css\" />"
	str+="<body style=\"margin:0; background-color:"+a[44]+";\">";if (a[41]!='' && !ns4) str="";
	str+="<table class=\"kw_cal_tbl2\"><tr><td colspan=\"7\">"
	str+="<table width=\""+(a[35]-10)+"\" border=0 cellspacing=0 cellpadding=0 class=\"kw_cal_tbl2\">";
	endStr="','"+a[31]+"',0";for(ix=33;ix<37;ix++)endStr+=","+a[ix];for(ix=37;ix<46;ix++)
	endStr+=",'"+a[ix]+"'";bStr="";for (var j=0;j<29;j++) bStr+="'"+a[j]+"',";
	aStr1=bStr+"'"+pMt+"','"+pYr+endStr;aStr2=bStr+"'"+nMh+"','"+nYr+endStr;str+="<tr><td "
	str+="class=\"kw_cal_mnth\">"+accs+"href=\"javascript:window.opener.KW_expertCalendar("+aStr1;
	str+=")\">"+KW_setDel(a[37])+"</a>"+a[cMn]+accs+"href=\"javascript:window.opener.KW_expertCalendar("
	str+=aStr2+")\">"+KW_setDel(a[38])+"</a></td>";aStr1=bStr+"'"+cMn+"','"+(cYr-1)+endStr;
	aStr2=bStr+"'"+cMn+"','"+(cYr+1)+endStr;str+="<td class=\"kw_cal_yr\">"+accs
	str+="href=\"javascript:window.opener.KW_expertCalendar("+aStr1+")\">"+KW_setDel(a[39])+"</a>"
	str+=cYr+accs+"href=\"javascript:window.opener.KW_expertCalendar("+aStr2+")\">"
	str+=KW_setDel(a[40])+"</a></td></tr></table></td></tr><tr>"
	for (var i=(a[25]==1)?13:12;i<19;i++) str+="<td class=\"kw_cal_wktitle\">"+a[i]+"</td>";
	if (a[25]==1) str+="<td class=\"kw_cal_wktitle\">"+a[12]+"</td>";str+="</tr>";
	for (var i=0;i<dc.tMh.length;i++) {if ((i)/7==parseInt((i)/7)) str+="<tr>"; 
	tdClass=dc.tMh[i].gC();temp=dc.tMh[i].dsp.toString();tdActive=(temp.indexOf("<a")!=-1)?"id='kwon'":"";
	str+="<td class=\""+tdClass+"\" "+tdActive+">"+dc.tMh[i].dsp+"</td>"
	if ((i+1)/7==parseInt((i+1)/7))str+="</tr>";}str+=(a[41]==''&&!ns4)?"</table></body></html>":"</table>";
	if (a[41]!='' && !ns4) {t=MM_findObj(a[41]);v="show";if (t.style) { t=t.style; v='visible'}
	t.visibility=v;	t.left=xp+px;t.top=yp+px;while (str.indexOf('window.opener.')!=-1)
	str=str.replace('window.opener.','');p1=str.indexOf(" window.close();"); while (p1!=-1) {
	str=str.substring(0,p1)+str.substring(p1+16);p1=str.indexOf(" window.close();")}
	MM_findObj(a[41]).innerHTML=str;} else {var nnx=(ns4)?1.25:1;var look='width='+(a[35]*nnx)
	look+=',height='+(a[36]*nnx)+',left='+a[33]+',top='+a[34];
	popwin=window.open('','calendar',look);	with(popwin.document){open();write(str);close();}}
}
//------------------------------------------------------//
//------------------------------------------------------//
//------------------------------------------------------//