function Trim(text) {
	ndx = 0;
    lgth = text.length;
    while ((ndx < lgth) && (text.charAt(ndx) == ' ')) ++ndx;
    while ((lgth > ndx) && (text.charAt(lgth - 1) == ' ')) --lgth;
    return text.substring(ndx, lgth);
}
function IsNumeric(num) {
	ndx = 0;
    lgth = num.length;
	while (ndx < lgth) {
        if ((num.charAt(ndx) < '0') || (num.charAt(ndx) > '9'))
			return false;
		++ndx;
	}
	return true;
}
function FormError(obj, message) {
	alert(message);
	obj.focus();
	return false;
}
function EditRequired(obj, msg) {
	obj.value = Trim(obj.value);
	if (obj.value.length == 0) {
		return FormError(obj, msg);
	}
	return true;
}
function IsDollar(obj, msg) {
	obj.value = Trim(obj.value);
	if (obj.value.length == 0) {
		return FormError(obj, msg);
	}
	numdecs = 0; 
	for (i = 0; i < obj.value.length; i++) {
		mychar = obj.value.charAt(i);
		if ((mychar >= "0" && mychar <= "9") || mychar == ".") { 
			if (mychar == ".")
			numdecs++;
		}
		else {
			return FormError(obj, msg);
		}
		if (numdecs > 1) {
			return FormError(obj, msg);
		}
		return true; 
	}
	return true; 
}
function IsTwo(obj, msg) {
	obj.value = Trim(obj.value);
	if (obj.value < 2.00) {
		return FormError(obj, msg);
	}
	return true;
}

function FormatUpper(obj) {
	obj.value = Trim(obj.value.toUpperCase());
}
function FormatPostal(obj) {
	FormatUpper(obj);
	if ((obj.value.length == 6) && (obj.value.charAt(0) >= 'A') && (obj.value.charAt(0) <="Z"))
		obj.value = obj.value.substring(0, 3) + ' ' + obj.value.substring(3, 6);
}
function PhoneError(obj) {
	return FormError(obj, 'Please include your number and area code only.');
}
function FormatPhone(obj) {
	obj.value = Trim(obj.value);
	if (obj.value.length == 0)
		return true;

	ndx = 0;
	num = obj.value;

	if (num.charAt(0) == '+') {
		ndx = 1;
		while (ndx < obj.value.length) {
			if ( ((num.charAt(ndx) < '0') || (num.charAt(ndx) > '9')) &&
				(num.charAt(ndx) != ' ') &&
				(num.charAt(ndx) != '-') &&
				(num.charAt(ndx) != '.') )
				return PhoneError(obj);
			++ndx;
		}
		return true;
	}

	if ((num.charAt(0) == '(') && (num.charAt(4) == ')')) {
		area = num.substring(1, 4);
		num = Trim(num.substring(5));
	} else {
		area = num.substring(0, 3);
		if ((num.charAt(3) == '-') || (num.charAt(3) == '.'))
			num = Trim(num.substring(4));
		else
			num = Trim(num.substring(3));
	}

	xchg = num.substring(0,3);
	if ((num.charAt(3) == '-') || (num.charAt(3) == '.'))
		num = Trim(num.substring(4));
	else
		num = Trim(num.substring(3));

	if (num.length > 4) {
		if ((num.charAt(4) == '-') || (num.charAt(4) == '.'))
			ext = Trim(num.substring(5));
		else
			ext = Trim(num.substring(4));

		num = Trim(num.substring(0, 4));
	}
	else
		ext = '';

	for (ndx = 0; ndx < ext.length; ndx++) {
		if ((ext.charAt(ndx) >= '0') && (ext.charAt(ndx) <= '9'))
			break;
	}

	ext = Trim(ext.substring(ndx));

	if (ext.length > 0)
		ext = 'Ext. ' + ext;

	if ((area.length != 3) || !IsNumeric(area) ||
		(xchg.length != 3) || !IsNumeric(xchg) ||
		(num.length  != 4) || !IsNumeric(num) ||
		(area.charAt(0) == '0') || (area.charAt(0) == '1') ||
		(xchg.charAt(0) == '0') || (xchg.charAt(0) == '1')) {
		return PhoneError(obj);
	}
	obj.value = Trim(area + ' ' + xchg + '-' + num + ' ' + ext);
	return true;
}
function EmailError(obj) {
	return FormError(obj, '"' + obj.value + '" is not a valid email address.');
}
function FormatEmail(obj) {
	obj.value = Trim(obj.value);
	if (obj.value.length == 0)
		return true;

	ndx = 0;
	atsign = false;
	dot = false;

	while (ndx < obj.value.length) {
		if (obj.value.charAt(ndx) == ' ')
			return EmailError(obj);

		if (obj.value.charAt(ndx) == '@') {
			atsign = true;
			++ndx;
			break;
		}
		++ndx;
	}

	if (!atsign) return EmailError(obj);
	while (ndx < obj.value.length) {
		if (obj.value.charAt(ndx) == ' ')
			return EmailError(obj);

		if (obj.value.charAt(ndx) == '.') {
			dot = true;
		}
		++ndx;
	}
	if (!dot) return EmailError(obj);
	return true;
}
function PopUp(url, name, windowWidth, windowHeight, scroll) {
	myleft=(screen.width)?(screen.width-windowWidth)/2:100;
	mytop=(screen.height)?(screen.height-windowHeight)/2:100;
	properties = "width="+windowWidth+", height="+windowHeight+", scrollbars="+scroll+", top="+mytop+", left="+myleft;
	window.open(url, name, properties)
}
function ClearCoordinates() {
	var  message = confirm("Are you sure you want to clear your coordinates?");
 	if (message == true) {
   		document.getElementById('company_lat').value='';
		document.getElementById('company_lon').value='';
		return true;
 	}
 	else {
  		return false;
  	}
}

