//funcões de trim
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g, '');
}

String.prototype.ltrim = function() {
	return this.replace(/^\s+/, '');
}

String.prototype.rtrim = function() {
	return this.replace(/\s+$/, '');
}

//funções de calculo de latitude e longitude
var input1;
var input2;

function calcPoint(response) {
    try
    {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1],
                            place.Point.coordinates[0]);
	    input1.value = point.lat();
	    input2.value = point.lng();	
    }
    catch(ex)
    {
        input1.value = '';
        input2.value = '';
        alert('Localidade não encontrada');
    }			
}

function showLatLng(inputLat, inputLong, endereco) {
  input1 = inputLat;
  input2 = inputLong;
  var geocoder = new GClientGeocoder();
  geocoder.getLocations(endereco, calcPoint);
}

function checkMail(email) {
	if (typeof(email) == 'undefined') {
		return false;
	}
	var rgx1 = /(\@.*\@)|(.*\.\..*)|(.*\@\..*)|(^\.)|(\.$)|(\@\/)|(.*\@\-.*)|(.*\.$)/gi;
	var rgx2 = /^[_\w\d][\w\d\_\/\-\.]*\@[\d\w\-\.]+[0-9A-z]$/gi;
	var rgx3 = /.*\@.*\.+.*/gi;
	return !email.match(rgx1) && email.match(rgx2) && email.match(rgx3);
}

function counterUpdate(opt_countedTextBox, opt_countBody, opt_maxSize) {
    var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "countedTextBox";
    var countBody = opt_countBody ? opt_countBody : "countBody";
    var maxSize = opt_maxSize ? opt_maxSize : 1024;

    var field = document.getElementById(countedTextBox);
    if (field && field.value.length >= maxSize) {
        field.value = field.value.substring(0, maxSize);
    }
    var txtField = document.getElementById(countBody);
    if (txtField) { 
        txtField.innerHTML = opt_maxSize - field.value.length;
    }
}

function getCookie( name ){
    	var start = document.cookie.indexOf( name + "=" );
    	var len = start + name.length + 1;
    	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		    return null;
	    }
    	if ( start == -1 ) return null;
	    var end = document.cookie.indexOf( ';', len );
	    if ( end == -1 ) end = document.cookie.length;
	    return unescape( document.cookie.substring( len, end ) );
    }

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
    	( ( path ) ? ';path=' + path : '' ) +
	    ( ( domain ) ? ';domain=' + domain : '' ) +
	    ( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
		( ( path ) ? ';path=' + path : '') +
		( ( domain ) ? ';domain=' + domain : '' ) +
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


