// pop the event reminder
function remindEvents(frm) {
    var theTxt = "?id=";
    var theUrl;
	var popWidth = 600;
	var popHeight = 400;
	var popLeft = self.screen.availWidth/2 - popWidth/2;
	var popTop = self.screen.availHeight/2 - popHeight/2;

    for (var i=0; i<document.form1.elements.length; i++) {
        if(document.form1.elements[i].name.indexOf("event_num") >= 0 &&
           document.form1.elements[i].checked == true) {
            theTxt+= document.form1.elements[i].value + '|';
        }
    }
    theTxt = theTxt.substr(0, theTxt.length-1);

    if(theTxt != "?id") {
        theUrl = 'remind_pop.php' + theTxt;
        window.open(theUrl,'reminder','left='+popLeft+',top='+popTop+',width='+popWidth+',height='+popHeight+',scrollbars=yes,resizable=no,status=no');
    } else {
        alert('Please select at least one event.');
    }
}

// validate the form
function form_validate(frm)
{
	formElementIndex = 0;
	form_objs = new Array();
	var year, month, day, eyear, emonth, eday;

	// form object properties: id, name, contents, required
	newFormElement('name', 'Event Name', frm.name.value, 1);
	newFormElement('description', 'Event Description', frm.description.value, 0);
	newFormElement('month', 'Starting Month', frm.month.value, 1);
	newFormElement('day', 'Starting Day', frm.day.value, 1);
	newFormElement('year', 'Starting Year', frm.year.value, 1);
	newFormElement('emonth', 'Ending Month', frm.emonth.value, 0);
	newFormElement('eday', 'Ending Day', frm.eday.value, 0);
	newFormElement('eyear', 'Ending Year', frm.eyear.value, 0);
	newFormElement('starttime', 'Start Time', frm.starttime.value, 0);
	newFormElement('endtime', 'End Time', frm.endtime.value, 0);
	newFormElement('location', 'Location', frm.location.value, 0);
	newFormElement('address', 'Address', frm.address.value, 0);
	newFormElement('city', 'City', frm.city.value, 1);
	newFormElement('state', 'State', frm.state.value, 1);
	newFormElement('zipcode', 'Zip code', frm.zip.value, 0);

	// general validation
	for(var a in form_objs)
		if(form_objs[a].required)
			if(form_objs[a].contents == '') {
				alert(form_objs[a].name + " cannot be blank.");
				var theField = eval("frm."+form_objs[a].id);
				theField.focus();
				return false;	
			}

	// locate the date members
	for(var a in form_objs) {
		if(form_objs[a].id == 'year')
			year = form_objs[a].contents;
		if(form_objs[a].id == 'month')
			month = form_objs[a].contents;
		if(form_objs[a].id == 'day')
			day = form_objs[a].contents;
		if(form_objs[a].id == 'eyear')
			eyear = form_objs[a].contents;
		if(form_objs[a].id == 'emonth')
			emonth = form_objs[a].contents;
		if(form_objs[a].id == 'eday')
			eday = form_objs[a].contents;
}

	// validate the starting date
	if(!validate_date(year, month, day)) {
		alert("Invalid Start Date.");
		frm.day.focus();
		return false;
	}
	
	// validate the ending date
	if(eyear || emonth || eday)
		if(!validate_date(eyear, emonth, eday)) {
			alert("Invalid End Date.");
			frm.eday.focus();
			return false;
		}
	
	// if we're here, everything is kosher..
	return true;
}

// validate the date
function validate_date(yr, mo, da)
{
	// make sure we have all components..
	if(!yr || !mo || !da)
		return false;

	now = new Date();
	days = new Array();
	days[0] = new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	days[1] = new Array(0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	leap = yr%4 == 0 && yr%100 != 0 || yr%400 == 0;
	if(leap) leap = 1;
	else leap = 0;

	if(da > days[leap][mo])
		return false;
	else if(yr < now.getFullYear() || (yr == now.getFullYear() && mo < now.getMonth()+1) || 
			(yr == now.getFullYear() && mo == now.getMonth()+1 && da < now.getDate()))
		return false;
	else
		return true;
}


// create the form element objects
function newFormElement(id, name, contents, required)
{
	form_objs[formElementIndex] = new FormElement(id, name, contents, required);
	formElementIndex++;
}

// form element constructor
function FormElement(id, name, contents, required) {
	this.id = id;
	this.name = name;
	
	this.contents = contents;
	this.required = required;
}

// bold selected text in the textarea
function textareaBold(obj)
{
	selTxt = getSelected(obj);
	if(selTxt) {
		writeHTML(obj, preText + '<b>' + selTxt + '</b>' + postText);
	}
	return;
}

// maka a link from selected text in the textarea
function textareaLink(obj)
{
	selTxt = getSelected(obj);
	linkTxt = prompt('Enter URL for link:', '');
	if(linkTxt)
	{
		if(!linkTxt.match("^http://"))
			linkTxt = "http://" + linkTxt;
		writeHTML(obj, preText + '<a target="_blank" href="' + linkTxt + '">' + selTxt + '</a>' + postText);
	}
	return;
}	

function removeWhiteSpace(myTxt)
{
	// move leading or trailing whitespace outside the selection
	var selLength = myTxt.length;
	while(myTxt.charAt(0) == " ")
	{
		preText+= " ";
		myTxt = myTxt.substr(1, selLength-1);
	}
	while(myTxt.charAt(selLength-1) == " ")
	{
		selLength--;
		postText+= " ";
		myTxt = myTxt.substr(0, selLength);
	}
	//alert(':'+myTxt+':');
	return myTxt;
}

function getSelected(obj)
{
	// make sure the text area has focus
	obj.focus();
	
	txt = '';
	preText = '';
	postText = '';

	if(window.getSelection) {
		txt = obj.value.substring(obj.selectionStart, obj.selectionEnd);	
	} else if (document.selection) {
		theSel = document.selection.createRange();
		txt = theSel.text;
	}

	txt = removeWhiteSpace(txt);
	return txt;
}

function writeHTML(obj, theData)
{
	if(window.getSelection) {
		obj.value = obj.value.substring(0, obj.selectionStart) + theData + obj.value.substring(obj.selectionEnd, obj.value.length);
		return;
	} else if (document.selection) {
		theSel.text = (theData); // replace the text
		document.selection.empty(); // deselect
		return;
	}
}

