/**
 * function TextCounter( field, maxlimit )
 * Prints the number of remaining characters allowed to given field, based on given limit
 *
 * @param string field name of field to count characters from
 * @param string span name of span field to show remaining characters in
 * @param integer maxlimit number of characters allowed in given field
 * @return boolean false if max exceeded, true otherwise
 */
function TextCounter(form, field, model, maxlimit)
{
    if (field.value.length > maxlimit) // if too long...trim it!
    {
    	field.value = field.value.substring(0, maxlimit);
    }
    // otherwise, update 'characters left' counter
    else
    {
        if (null != form.elements['data['+model+'][textcounter]']) {
    	   form.elements['data['+model+'][textcounter]'].value = maxlimit - field.value.length;
        }
    }
}
/**
 * function DisableField(form, fieldToDisable, checkBox)
 * Prints the number of remaining characters allowed to given field, based on given limit
 *
 * @param string fieldToDisable, name of date field to disable each part of, in form [model][field_name]
 * @param boolean true to enable date field, false to disable
 */
function DisableDateField(fieldToDisable, checkboxTicked)
{
	var fieldSub = fieldToDisable.substr(0, fieldToDisable.length-1);
	datepart = 'data' + fieldSub + '_day]';
	monthpart = 'data' + fieldSub + '_month]';
	yearpart = 'data' + fieldSub + '_year]';

    if (checkboxTicked) // enable field
    {
    	document.getElementById(datepart).disabled = false;
    	document.getElementById(monthpart).disabled = false;
    	document.getElementById(yearpart).disabled = false;
    }
    else	// disable field
    {
    	document.getElementById(datepart).disabled = true;
    	document.getElementById(monthpart).disabled = true;
    	document.getElementById(yearpart).disabled = true;
    }
}
/**
 * function MakeOption(name, i, num)
 * Adds an option element to an existing select element
 *
 * @param string name of select element
 * @param integer i, index of option element to be created
 * @param integer num, total number of options in select element
 * @return boolean
 */
function MakeOption(name, i, num) {
	document.mainform.selectFile.length = num;
	document.mainform.selectFile.options[i] = null;
	document.mainform.selectFile.options[i] = new Option(name, name);

	document.mainform.fileList.value = document.mainform.selectFile.options[0].text;
	return true;
}

/**
 * function SelectAll( pForm )
 * Selects all checkboxes on form
 *
 * @param string name of form that contains checkboxes
 */
function SelectAll(pForm) {
    for (var i=0;i<pForm.elements.length;i++)
	{
		var e = pForm.elements[i];
		if ((e.name != 'selectall') && (e.type=='checkbox'))
		{
			e.checked = pForm.selectall.checked;
		}
	}
}

/**
 * function SendCheckboxes(pForm)
 * Retrieves all checked checkboxes on form and returns url GET string
 *
 * @param string name of form that contains checkboxes
 * @return string to be attached to url, containing checkboxes which are ticked
 */
function SendCheckboxes(pForm) {
    var params = '';

    for (var i=0;i<pForm.select.length;i++)
	{
		var e = pForm.select[i];
		if ((e.name != 'selectall') && (e.type=='checkbox'))
		{
			params = params + (pForm.selectall.checked ? (e.value+'=on&amp;') : '');

		}
	}
	return params;
}

/**
 * function ChangeCounty(pAddress)
 * Zooms in on Google Map to the selected county
 *
 * @param pAddress string address
 * @param pMap reference to Google Map
 * @param pGeocoder reference to Google Map Geocoder
 */
function ChangeCounty(pAddress, pMap, pGeocoder, pZoom) {
	if (!pZoom) {
		pZoom = 11;
	}

	if ('' != pAddress && 'UNKNOWN' != pAddress && 'UNKOWN' != pAddress) {
		pGeocoder.getLatLng(
				(pAddress),
				function(point) {
					if (!point) {
						alert(pAddress + " was not found. Please narrow down your search.");

						//If there are commas, reduce the search by address element
						if (-1 < pAddress.indexOf(',')) {
							var addressElements = pAddress.split(',');
							pAddress = '';

							//Exclude the first part of the address
							for (var i=1; i<addressElements.length; i++) {
								pAddress = pAddress + addressElements[i] + ', ';
							}
							//Trim the trailing comma
							pAddress = pAddress.substr(1, pAddress.length - 3);
							ChangeCounty(pAddress, pMap, pGeocoder, pZoom);
							return false;
						}
					}
					else {
						pMap.setCenter(point, pZoom);
						pMap.addControl(new GLargeMapControl());
						return false;
					}
				}
		);
	}
}

function CheckEnterPressed(e) {
	var keynum;
	var keychar;

	if(window.event) // IE
	{
		keynum = e.keyCode;
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which;
	}

	//If Enter is pressed
	if (13 == keynum) {
		return true;
	}
	else {
		return false;
	}
}