/**
 * lost.js
 * javascript functions for LOST
 * @author Peter Edwards <tech@e-2.org>
 * @version 1.0
 */
 
function loadMaps() {
    if (GBrowserIsCompatible()) {
        loadFoundMap();
		    loadLostMap();
		}
}

/* function to change the background colour of an input box when focussed */
function focusInput(obj)
{
		obj.style.backgroundColor = '#fff';
}
/* function to change the background colour of an input box when blurred */
function blurInput(obj)
{
    obj.style.backgroundColor = '#eee';
}
/* function to open a popup with a calendar in it */
function openCalendar(form_field)
{
    var page_url = 'calendar.php?lf=' + form_field;
    var calWin = window.open(page_url, 'calendar', 'width=200,height=200,scrollbars');
    if (calWin.focus) {
        calWin.focus();
    }
} 
/* function to set the date in the original window from the calendar popup */
function set_day(field, day, month, year)
{
    if (window.opener && window.opener.document && window.opener.document.forms['lostform']) {
        var f = window.opener.document.forms['lostform'];
        for (i = 0; i < f.elements.length; i++) {
            if (f.elements[i].name == field + '_day') {
                setSelectValue(f.elements[i], day);
            }
            if (f.elements[i].name == field + '_month') {
                setSelectValue(f.elements[i], month);
            }
            if (f.elements[i].name == field + '_year') {
                setSelectValue(f.elements[i], year);
            }
        }
    }
    self.close();
}
/* function to set a select dropdown's selectedIndex based on the value given */
function setSelectValue(selObj, value)
{
    for (s = 0; s < selObj.options.length; s++) {
		    if (selObj.options[s].value == value) {
				    selObj.selectedIndex = s;
						return;
				}
		}
}
/**
 * form checking functions
 */

/* function used to check the format of a book number */
function checkBookNumber()
{
    var book_id = document.getElementById('lostform').book_id.value;
		if (document.getElementById('lostform').book_id.value.match(/^[Ll][0-5][0-9][0-9]$/) == null) {
		    alert('Please enter the book number as it appears on the cover');
    		return false;
		}
		return true;
}

/* function used to check the format of an email address*/
function checkEmail(email)
{
    if (email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/)) {
        return true;
    } else {
        return false;
    }
}

/* function used to check required fields on map page */
function checkRequired()
{
    var f = document.getElementById('lostform');
		var errorMsg = '';
		var foundDay = false;
		var lostDay = false;
		if (f.condition.value == '') {
		    errorMsg += '  - Please describe the condition of the book when you found it\n';
		}
		if (f.found_text.value == '') {
		    errorMsg += '  - Please describe where you found your book\n';
		}
		if (f.found_day.options[f.found_day.selectedIndex].value == "null" || f.found_month.options[f.found_month.selectedIndex].value == "null" || f.found_year.options[f.found_year.selectedIndex].value == "null") {
		    errorMsg += '  - Please select the date of your discovery\n';
		} else {
    		foundDay = new Date();
    		foundDay.setDate(parseInt(f.found_day.options[f.found_day.selectedIndex].value));
    		foundDay.setMonth(parseInt(f.found_month.options[f.found_month.selectedIndex].value));
    		foundDay.setYear(parseInt(f.found_year.options[f.found_year.selectedIndex].value));
    }	
		if (f.found_lat.value == '' || f.found_long.value == '') {
		    errorMsg += '  - Please record the location of where you found your copy on the map\n';
		}
		if (f.lost_text.value == '') {
		    errorMsg += '  - Please describe where you intend to release your copy of LOST back into the world\n';
		}
		if (f.lost_lat.value == '' || f.lost_long.value == '') {
		    errorMsg += '  - Please indicate the location of where you where you will release your copy of lost on the map\n';
		}
		if (f.lost_day.options[f.lost_day.selectedIndex].value == "null" || f.lost_month.options[f.lost_month.selectedIndex].value == "null" || f.lost_year.options[f.lost_year.selectedIndex].value == "null") {
		    errorMsg += '  - Please select a date for the release of the book\n';
		} else {
    		lostDay = new Date();
    		lostDay.setDate(parseInt(f.lost_day.options[f.lost_day.selectedIndex].value));
    		lostDay.setMonth(parseInt(f.lost_month.options[f.lost_month.selectedIndex].value));
    		lostDay.setYear(parseInt(f.lost_year.options[f.lost_year.selectedIndex].value));
		}
		if (f.email.value != '' && !checkEmail(f.email.value)) {
		    errorMsg += '  - Your email address appears to be invalid. Please check it and try again\n';
		}
		if (errorMsg) {
		    alert('The form could not be sent because some\nrequired fields were not filled in:\n\n' + errorMsg);
				return false;
		} else {
		    return true;
		}    
}
