function URLencode(sStr) {
	sStr = escape(sStr)
    sStr.replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/\//g,'%2F');
		return sStr
  }

function emailPage(sURL, sQueryString) {
	sURL = sURL + URLencode(sQueryString);
	window.open(sURL,"emailPage","width=655px,height=450px,toolbars=yes,scrollbars=yes,resizable=yes,menubar=yes");
}

function isBlank(testStr) 
{
	if (testStr.length == 0) 
		return true
	for (var i = 0; i <= testStr.length-1; i++) // all spaces?
		if (testStr.charAt(i) != " ")
			return false
	return true
}

function isDate(str)
{
	if(isNaN(new Date(Date.parse(str)))) {
	   return false;
	  }
	else {
	   return true;
	}
}

function checkEmail(strEmail, bRequired, strName) {
	var msg = '';
	if (isBlank(strEmail)) {
		if (bRequired) {
			msg += '\n - ' + strName + ' is Required  - You must enter a valid email address (username@domain.com)';
		}
	} else {
		if (! /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail)){
			msg += '\n - ' + strName + ' - You must enter a valid email address (username@domain.com) ';
		}	
	}
	return msg;
}

function getSelectValue(oSelect) {
 retval = oSelect.options[oSelect.selectedIndex].value;
 return retval;
}

function getRadioValue(oRadio) { 
  for(var i=0; i<oRadio.length; i++) {
 if (oRadio[i].checked) {
  return oRadio[i].value; 
 }
  }
  return "";
} 

function setUSTCalculatorUstScholarshipAmount(value) {
	//if ((window.name=='calcPopup') && (window.opener.setUstScholarship)) {
	if (window.name=='calcPopup') {
		window.opener.setUstScholarship(value);
		close();
	} else {
		location.href='http://stthom.edu/Admissions_Financial_Aid/Financial_AidScholarships/Calculators.aqf?ustScholarship=' + value;
	}
}


function trim(value) {
	return value.replace(/^\s+|\s+$/g,"");
}

function checkIsBlank(value, label) {
	if (isBlank(value)) {
		return '\n - ' + label + ' is required';
	}
	return '';
}

function validateFormGeneric(oForm) {
	var msg ='';
	var fieldsToValidate = '';
	var hasFields = false;
	if (oForm.Required.value) {
		fieldsToValidate = oForm.Required.value;
		hasFields = true;
	} else {
		//try lowercase
		if (oForm.required.value) {
			fieldsToValidate = oForm.Required.value;
			hasFields = true;
		}
	}
	
	if ((hasFields) && (!isBlank(fieldsToValidate))) {
		var validationArray = fieldsToValidate.split(',');
		var label = '';
		var fieldname = '';
		var objField = '';
		for (i=0;i<validationArray.length;i++) {
			fieldname = trim(validationArray[i].split(':')[1]);
			label = trim(validationArray[i].split(':')[0]);
			objField = eval('oForm.'+fieldname);
			if (objField) {
				switch(objField.type) {
					case "text":
					case "textarea":
							msg += checkIsBlank(objField.value, label);
						break;
					case "select":
					case "select-one":
					case "select-multiple":
						msg += checkIsBlank(objField.options[objField.selectedIndex].value, label);
						break;
					default:
						break;
				}
			}
		}
	}
	if (!isBlank(msg)) {
		alert(msg);
		return false;
	}
	return true;
}
