var dragstart;
var dragend;

var referencepoint;

var selectmode;

var isselecting;

var selectstart;
var selectend;

var selectday;

var duration = 0;

var calendarsearchterm = "";
var cajax;

calmonth = new Date();


calyear = calmonth.getFullYear();
calmonth = calmonth.getMonth();
calmonth++;

Date.prototype.getWeek = function (dowOffset) {

	dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
	var newYear = new Date(this.getFullYear(),0,1);
	var day = newYear.getDay() - dowOffset; //the day of week the year begins on
	day = (day >= 0 ? day : day + 7);
	var daynum = Math.floor((this.getTime() - newYear.getTime() - (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
	var weeknum;
	if(day < 4) {
		weeknum = Math.floor((daynum+day-1)/7) + 1;
		if(weeknum > 52) {
			nYear = new Date(this.getFullYear() + 1,0,1);
			nday = nYear.getDay() - dowOffset;
			nday = nday >= 0 ? nday : nday + 7;
			weeknum = nday < 4 ? 1 : 53;
		}
	}
	else {
		weeknum = Math.floor((daynum+day-1)/7);
	}
	return weeknum;
};


function process_findevent(start, end, m, y, fesearch, fetype, fecity){


	dragstart = start;
	referencepoint = dragstart;
	dragend = end;



	if(fesearch){

		document.getElementById('cscheck').checked=1;
		document.getElementById('csterm').value=fesearch;

	}
	else{
		document.getElementById('csterm').value='';
	}

	if(fetype || fecity){

		document.getElementById('feclear').style.display='block';
		document.getElementById('feclear2').innerHTML = (fetype ? "Type: "+fetype : "") + (fecity && fetype ? "<br />":"") + (fecity ? "City: "+fecity:"");

	}

	document.getElementById('fe_type').value = fetype;
	document.getElementById('fe_city').value = fecity;

	smallcalendar(m, y);

	dyncalsearch('');

}

function thecalsearch(){

	thecsterm = noHTML(document.getElementById('csterm').value);

	if(typeof(cajax) == 'object'){
		cajax.abort();
	}


	try{
		cajax=new XMLHttpRequest();
	}

	catch (e){
		try{
			cajax=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				cajax=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}



	cajax.onreadystatechange=function(){
		if(cajax.readyState==4){

			var cajaxresponse;
			cajaxresponse = cajax.responseText;




			if(cajaxresponse !== ""){

				document.getElementById('daterow').innerHTML = "<div style='padding:5px;margin:auto;text-align:center;'><b>Search Results: " + thecsterm + "</b></div>";
				document.getElementById('fullcalendar').innerHTML = cajaxresponse;
				unittab();



			}


		}
		else{

			document.getElementById('daterow').innerHTML = "<div style='padding:5px;margin:auto;text-align:center;'>Searching...<img src='/img/searching.gif' style='display:inline;' /></div>";

		}

	}


	cajax.open("GET", "http://www.thywordnetwork.org/calendar/index.php?do=search&search=" + thecsterm, true);
	cajax.send('');

}

function dyncalsearch(e){

if(document.getElementById('cscheck').checked == 1){
	var code;
	if (!e){
		var e = window.event;
	}
	if(e){
		if (e.keyCode){
			code = e.keyCode;
		}
		else if (e.charCode){
			code = e.charCode;
		}
		else if (e.which){
			code = e.which;
		}
	}
	
	if(code != 13){
		calendarsearchterm = noHTML(document.getElementById('csterm').value);
		updateselect('');
	}	
}

}

function clearcalsearch(){

	if(document.getElementById('cscheck').checked == 1){
		dyncalsearch('');
	}
	else{
		calendarsearchterm = "";
		updateselect('');
	}
}


function smallcalendar(month,year){



	month--;

	tempd = new Date();
	tempd.setFullYear(year,month,1);

	month=tempd.getMonth();
	year=tempd.getFullYear();

	calmonth = month-0+1;
	calyear = year;

	if(month<10){
		month = "0" + "" + month;
	}

	var theHTML = "<table id='smallfullcal' border='0' cellpadding='2' cellspacing='0' width='100%'>";

	today = new Date();
		tp2 = today.getMonth();
		tp2 = tp2-0+1;
		if(tp2<10){
			tp2 = "0" + "" + tp2;
		}
		tp3 = today.getDate();
		tp3 = tp3-0;
		if(tp3<10){
			tp3 = "0" + "" + tp3;
		}

	today = today.getFullYear() + "" + tp2 + "" + tp3;

	tempidate = new Date();
	tempidate.setFullYear(year,month,1);

	cday = tempidate.getDay();
	
	cday = 1-cday;


		for(var i=0;i<42;i++){


			if(i == 0 || i == 7 || i == 14 || i == 21 || i == 28 || i == 35 || i == 42){
				theHTML += "<tr valign='top'>";
			}


			tempidate.setFullYear(year, month, (cday-0+i));

				tp2 = tempidate.getMonth();
				tp2 = tp2+1;
				if(tp2<10){
					tp2 = "0" + "" + tp2;
				}
				tp3 = tempidate.getDate();
				tp3 = tp3-0;
				if(tp3<10){
					tp3 = "0" + "" + tp3;
				}

			tempid = tempidate.getFullYear() + "" + tp2 + "" + tp3;


			if(tempid == today){
				theHTML += "<td align='center' onmousedown=\"beginselect('" + tempid + "');\" onmouseover=\"if(isselecting == 'yes'){ updateselect('" + tempid + "');}\" id='s" + tempid + "' class='caltoday' classy='caltoday'>";
			}
			else if(i == 0 || i == 7 || i == 14 || i == 21 || i == 28 || i == 35 || i == 42 || i == 6 || i == 13 || i == 20 || i == 27 || i == 34 || i == 41 || i == 48){
				theHTML += "<td align='center' onmousedown=\"beginselect('" + tempid + "');\" onmouseover=\"if(isselecting == 'yes'){ updateselect('" + tempid + "');}\" id='s" + tempid + "' class='weekend' classy='weekend'>";
			}
			else{
				theHTML += "<td align='center' onmousedown=\"beginselect('" + tempid + "');\" onmouseover=\"if(isselecting == 'yes'){ updateselect('" + tempid + "');}\" id='s" + tempid + "'>";
			}


			tp2 = month-0+1;
			tp2 = tp2-0;
			if(tp2<10){
				tp2 = "0" + "" + tp2;
			}

			tp2 = year + "" + tp2;


			if(tempid.substring(0,6) != tp2){
				theHTML += "<span style='color:rgb(200,200,200);'>";

			}


			theHTML += "" + tempidate.getDate();

			if(tempid.substr(0,6) != today.substr(0,6)){
				theHTML += "</span>";
			}

			theHTML += "</td>";


			if(i == 6 || i == 13 || i == 20 || i == 27 || i == 34 || i == 41 || i == 48){
				theHTML += "</tr>";
			}

		}


	theHTML += "</table>";

	document.getElementById('smallcal').innerHTML = theHTML;

	months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	var mno = month-0;

	var tzmonth = calmonth-0;

	if(tzmonth<10){
		tzmonth = "0" + tzmonth;
	}

	document.getElementById('calmonth2').innerHTML = "<a href='#' onclick=\"selectstart = '" + calyear + "" + tzmonth + "01'; referencepoint=selectstart; selectend = '" + calyear + "" + tzmonth + "01'; selectatab('month'); return false;\">" + months[mno] + " " + calyear + "</a>";

	updateselect('');

}


function updatecalendar(smonth, sday, syear, emonth, eday, eyear){



	if(typeof(cajax) == 'object'){
		cajax.abort();
	}


	try{
		cajax=new XMLHttpRequest();
	}

	catch (e){
		try{
			cajax=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
				cajax=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}



	cajax.onreadystatechange=function(){
		if(cajax.readyState==4){

			var cajaxresponse;
			cajaxresponse = cajax.responseText;




			if(cajaxresponse !== ""){


				eval(cajaxresponse);				

			}


		}
		else{

			document.getElementById('daterow').innerHTML = "<div style='padding:5px;margin:auto;text-align:center;'>Loading...<img src='/img/searching.gif' style='display:inline;' /></div>";

		}

	}

	calendarsearchterm = noHTML(calendarsearchterm);

	cajax.open("GET", "http://www.thywordnetwork.org/calendar/index.php?fe_type=" + document.getElementById('fe_type').value + "&fe_city=" + document.getElementById('fe_city').value + "&search=" + calendarsearchterm + "&do=events&sy=" + syear + "&sm=" + smonth + "&sd=" + sday + "&ey=" + eyear + "&em=" + emonth + "&ed=" + eday, true);

	cajax.send('');

}

function findmonth(){



		var endperiod = stamptodate(selectend);
		var startperiod = stamptodate(selectstart);

		lastday = stamptodate(selectstart);
		lastday.setFullYear(lastday.getFullYear(),(lastday.getMonth()-0+1),0);


		if(startperiod.getFullYear() != endperiod.getFullYear() || startperiod.getMonth() != endperiod.getMonth()){

			var modiff = Math.abs(startperiod.getMonth()-endperiod.getMonth());


			if(modiff > 1 && modiff < 11){

				var bsperiod = datetostamp(endperiod);
				bsperiod = stamptodate(bsperiod);
				bsperiod.setMonth(bsperiod.getMonth()-1);

			}
			else if( (lastday.getDate()-startperiod.getDate()) > ( endperiod.getDate() - 1 ) ){

				var bsperiod = datetostamp(startperiod);
				bsperiod = stamptodate(bsperiod);
			}
			else{

				var bsperiod = datetostamp(endperiod);
				bsperiod = stamptodate(bsperiod);
			}
		}
		else{

				var bsperiod = datetostamp(startperiod);
				bsperiod = stamptodate(bsperiod);
		}


	return bsperiod;

}

function stamptodate(ymd){

	ymd = ymd+"";

	d01 = new Date();
	d01.setFullYear((ymd.substring(0,4)-0),(ymd.substring(4,6)-1),(ymd.substring(6,8)-0));
	return d01;

}
function datetostamp(ymd){


	var m1 = ymd.getMonth();


	m1 = m1-0+1;

	if(m1<10){
		m1 = "0" + m1;
	}

	var d1 = ymd.getDate();

	d1 = d1-0;

	if(d1<10){
		d1 = "0" + d1;
	}

	return ymd.getFullYear() + "" + m1 + "" + d1;

	
}

function selectatab(what){

	if(what == "day"){
		dragstart = referencepoint;
		dragend = referencepoint;
		selectmode = "";
	}
	if(what == "month"){

		dragstart = findmonth();
		dragend = findmonth();
		dragstart.setDate(1);
		dragend.setFullYear(dragend.getFullYear(), (dragend.getMonth()-0+1), 0);

		smallcalendar((dragstart.getMonth()+1), dragstart.getFullYear());

		calmonth = (dragstart.getMonth()+1);
		calyear = dragstart.getFullYear();

		dragstart = datetostamp(dragstart);	
		dragend = datetostamp(dragend);

		selectmode = "";

	}
	if(what == "week"){
		dragstart = stamptodate(referencepoint);
		dragstart.setDate(dragstart.getDate()-dragstart.getDay());
		dragstart = datetostamp(dragstart);

		dragend = stamptodate(referencepoint);
		dragend.setDate(dragend.getDate()-dragend.getDay()+6);
		dragend = datetostamp(dragend);

		selectmode = "week";
	}

	updateselect('');

}



function beginselect(sid){

	dragstart = sid;	

	referencepoint = dragstart;

	isselecting = 'yes';

	if(duration > 20){ duration = 0;}

	document.onmouseup = endselect;
	document.onmousemove = selectmove;

	if(dragstart<=selectend && dragstart>=selectstart){
		dragend = dragstart;
		selectmode = "";
	}
	else{
		dragend = stamptodate(dragstart);
		dragend.setDate(dragend.getDate()+duration);
		dragend = datetostamp(dragend);
	}

	updateselect('');

	return false;

}

function selectmove(){

	document.body.focus();
	return false;

}

function endselect(){

	isselecting = 'no';
	document.onmouseup = "";
	document.onmousemove = "";

}


function updateselect(sid){


	if(sid != ""){
		dragend = sid;
		selectmode = "";
	}

	if(dragstart > dragend){
		selectstart = dragend + "";
		selectend = dragstart + "";
	}
	else{
		selectstart = dragstart + "";
		selectend = dragend + "";
	}


	// Select Mode Functions

	if(selectmode == "week"){

		selectstart = stamptodate(selectstart);

		selectstart.setDate(selectstart.getDate()-selectstart.getDay());
		selectstart = datetostamp(selectstart);

		selectend = stamptodate(selectstart);
		selectend.setDate(selectend.getDate()-0+6);
		selectend = datetostamp(selectend);

	}



	//Set to Full Weeks if Not in One Week...



		var startweek = stamptodate(selectstart);
		var endweek = stamptodate(selectend);

		startcompare1 = stamptodate(selectstart);
		startcompare1.setDate(startcompare1.getDate()+7);


		if(startcompare1 <= endweek){
			var startoffset = startweek.getDay();
			var endoffset = endweek.getDay();

			startweek.setDate(startweek.getDate()-startoffset);
			endweek.setDate(endweek.getDate()+(6-endoffset));


			selectstart = datetostamp(startweek);
			selectend = datetostamp(endweek);

		}



// COLOR CELLS

	var resettds = document.getElementById('smallfullcal').getElementsByTagName('TD');


	for(i=0;i<resettds.length;i++){

		if(resettds[i].getAttribute("classy") == "weekend"){
			resettds[i].style.background = 'rgb(230,255,230)';
		}
		else if(resettds[i].getAttribute("classy") == "caltoday"){
			resettds[i].style.background = 'rgb(100,100,200)';
		}
		else{
			resettds[i].style.background = 'white';
		}
		

	}


	var i = selectstart;

	while(parseInt(i) <= parseInt(selectend)){

		if(document.getElementById('s'+i)){

			if(document.getElementById('s'+i).getAttribute("classy") == "weekend"){
				document.getElementById('s'+i).style.background='rgb(125,200,225)';
			}
			else if(document.getElementById('s'+i).getAttribute("classy") == "caltoday"){
				document.getElementById('s'+i).style.background='navy';
			}
			else{
				document.getElementById('s'+i).style.background='rgb(150,200,250)';
			}
		}

		ii = i + "";

		var smallcaldate = stamptodate(ii);

		smallcaldate.setDate(smallcaldate.getDate()+1);

		ii = datetostamp(smallcaldate);
		i = parseInt(ii);


	}



// SET TABS

	var startd = stamptodate(selectstart);
	var endd = stamptodate(selectend);


	var startm = new Date();
	startm.setFullYear(selectstart.substring(0,4),(selectstart.substring(4,6)-1), 1);
	var endm = new Date();
	endm.setFullYear(selectend.substring(0,4),(selectend.substring(4,6)-0),0);

	unittab();

	if(selectstart == selectend){
		selecttab('day'); 
	}


	if( Math.round((endd.getTime()-startd.getTime())/1000/60/60/24) == 6 && startd.getDay() == 0){
		selecttab('week');
		selectmode = 'week';
	}

	var startc = findmonth();
	var endc = findmonth();

	startc.setDate(1);
	endc.setFullYear(endc.getFullYear(), (endc.getMonth()-0+1), -1);


	if(startd.getTime() <= startc.getTime() && endd.getTime() > endc.getTime() && Math.round(Math.abs(startd.getTime()-startc.getTime())/1000/60/60/24) < 7 && Math.round(Math.abs(endd.getTime()-endc.getTime())/1000/60/60/24) < 8){
		selecttab('month');
	}


	var startc2 = new Date();

	startc2 = datetostamp(startc2);

	if(startc2 == selectstart && selectstart == selectend){
		unittab();
		selecttab('today');
	}

	var startd = stamptodate(dragstart);
	var endd = stamptodate(dragend);
	

	duration = Math.abs(Math.round( (endd.getTime()-startd.getTime())/1000/60/60/24 ));

	updatecalendar(selectstart.substring(4,6),selectstart.substring(6,8),selectstart.substring(0,4),selectend.substring(4,6),selectend.substring(6,8),selectend.substring(0,4));


	return false;

}

function unittab(){

	document.getElementById('unitmonth').style.background = "white url('/img/tab_bg.jpg') top repeat-x";
	document.getElementById('unitmonth').style.borderBottomColor = "rgb(150,150,150)";
	document.getElementById('unitday').style.background = "white url('/img/tab_bg.jpg') top repeat-x";
	document.getElementById('unitday').style.borderBottomColor = "rgb(150,150,150)";
	document.getElementById('unitweek').style.background = "white url('/img/tab_bg.jpg') top repeat-x";
	document.getElementById('unitweek').style.borderBottomColor = "rgb(150,150,150)";
	document.getElementById('unittoday').style.background = "white url('/img/tab_bg.jpg') top repeat-x";
	document.getElementById('unittoday').style.borderBottomColor = "rgb(150,150,150)";

}


function selecttab(obj){
	document.getElementById('unit' + obj).style.background = "rgb(181,209,255) url('/img/tab_bg_ud.jpg') top repeat-x";
	document.getElementById('unit' + obj).style.borderBottomColor = "rgb(181,209,255)";
}

referencepoint = new Date();
referencepoint = datetostamp(referencepoint);