/* Last Modified: 10/27/99 9:30am - mft */

/* ============================================================= */

var blnDebugURL = false;

/* ============================================================= */

//Purpose: Check if a string is a valid URL format
//Input: vstrTest: Any string
//Output: false if not valid URL format, true if it is

function isValidURLFormat(vstrTest) {
	if ((vstrTest != "http://") && (vstrTest != "https://") && (vstrTest != "ftp://")) {
		return false;
	} else {
		return true;
	}
}

/* ============================================================= */

//Purpose: Check if a string is a valid URL domain name
//Input: vstrTest: Any string
//Output: false if not valid URL domain name, true if it is

function isValidURLDomain(vstrTest) {
	var re = new RegExp("^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$");
	if (!vstrTest.match(re))
		return false;
	else
		return true;
/*	
	var blnValid = true;
	var strThisChar;

	if (vstrTest.charAt(0) == '/') {
		return false;
	}

	for (var i = 0; i < vstrTest.length; i++) {
		strThisChar = vstrTest.charAt(i);
		if ((strThisChar >= 'a') && (strThisChar <= 'z')) {
//			blnValid = true;
		} else if ((strThisChar >= 'A') && (strThisChar <= 'Z')) {
//			blnValid = true;
		} else if ((strThisChar >= '0') && (strThisChar <= '9')) {
//			blnValid = true;
		} else if ((strThisChar == '_') || (strThisChar == '/') || (strThisChar == '-')) {
//			blnValid = true;
		} else if (strThisChar == '.') {
//			blnValid = true;
			if ((i < 3) || (i > vstrTest.length-3)) {
				blnValid = false;
			}
		} else {
			blnValid = false;
		}
	}


	return blnValid;
*/
}

/* ============================================================= */

//Purpose: Check if a string is all alpha-numeric (or has "." or "_")
//Input: vstrTest: Any string
//Output: false if not alpha-numeric-URL, true if it is alpha-numeric-URL

function isAlphaNumURL(vstrTest) {
	var blnValid = true;
	var strThisChar;

	for (i = 0; i < vstrTest.length; i++) {
		strThisChar = vstrTest.charAt(i);
		if (blnDebugURL) {
			alert(strThisChar);
		}

		if ((strThisChar >= 'a') && (strThisChar <= 'z')) {
			blnValid = true;
		} else if ((strThisChar >= 'A') && (strThisChar <= 'Z')) {
			blnValid = true;
		} else if ((strThisChar >= '0') && (strThisChar <= '9')) {
			blnValid = true;
		} else if ((strThisChar == '.') || (strThisChar == '_')
			|| (strThisChar == '/') || (strThisChar == '?')
			|| (strThisChar == '=') || (strThisChar == '%')
			|| (strThisChar == '&') || (strThisChar == '-')
			|| (strThisChar == '+') || (strThisChar == '~')
			|| (strThisChar == '"') || (strThisChar == "'")) {
			blnValid = true;
		} else {
			blnValid = false;
		}
	}

	if (blnDebugURL) {
		if (blnValid) { alert("It is valid."); }
		else { alert("It is NOT valid."); }
	}
	return blnValid;
}

/* ============================================================= */

//Purpose: Check if a string is a valid URL
//Input: robjField: Any string
//Output: false if not a valid URL, true if it is a valid URL

function isValidURL(robjField) {
	var blnValid = true;
	var intPos, strFormat, strDomain, strPathAndFile;
	strDomain = robjField.value + '';
	strFormat = '';
	strPathAndFile = '';

	intPos = strDomain.indexOf('//');
	if (intPos != -1) {
		strFormat = strDomain.substring(0,intPos+2);
		strDomain = strDomain.substring(intPos+2,strDomain.length);
	} else {
		strFormat = 'http://';
	}

	intPos = strDomain.indexOf('/');
	if (intPos != -1) {
		strPathAndFile = strDomain.substring(intPos,strDomain.length);
		strDomain = strDomain.substring(0,intPos);
	}

	if (blnDebugURL) {
		alert('Format: ' + strFormat);
		alert('Domain: ' + strDomain);
		alert('Path and File: ' + strPathAndFile);
	}

	/* Check if the email address has all 3 parts */
	if ((strFormat.length < 1) || (strDomain.length < 1))
		{ blnValid = false; }

	if (!blnValid) {
		alert("Not a valid URL");
		robjField.focus();
	   	return blnValid;
	}

	/* Check if the 3 parts are all valid */
	if (!isValidURLFormat(strFormat))
		{ blnValid = false; }
	if (!isValidURLDomain(strDomain))
		{ blnValid = false; }
	if (!isAlphaNumURL(strPathAndFile))
		{ blnValid = false; }

	if (blnValid) {
		robjField.value = strFormat + strDomain + strPathAndFile;
	   	return blnValid;
	} else {
		alert("Not a valid URL");
		robjField.focus();
	   	return blnValid;
	}
}

/* ============================================================= */

