function CheckLength(length) {
	if (window.event.srcElement.value.length >= length) {
		alert('The maximum number of characters for a short abstract is' + length);
		return false;
	}
}

function PutDate(iMonth, iDay, iYear, sMonthCtrlName, sDayCtrlName, sYearCtrlName) {
	var sMonth, sDay, sYear;
	var iStart

	sMonth 	= 'opener.' + sMonthCtrlName;
	sDay 	= 'opener.' + sDayCtrlName;
	sYear 	= 'opener.' + sYearCtrlName;

	iStart  = eval(sYear + '[1].value');

	if (typeof(opener.document) != 'object') { window.close(); return; }
	if (opener.document == null) { window.close(); return; }

    // SET THE VALUE OF THE FIELD THAT WAS PASSED TO THE CALENDAR
	if (typeof(eval(sMonth)) == "object") eval(sMonth + '.selectedIndex = ' + iMonth);
	if (typeof(eval(sDay)) == "object") eval(sDay + '.value = ' + iDay);
	if (typeof(eval(sYear)) == "object") eval(sYear + '.selectedIndex = ' + (iYear - iStart + 1));

    // GIVE FOCUS BACK TO THE DATE FIELD
	if (typeof(eval(sMonth)) == "object") eval(sMonth + '.focus()');

	window.close();
}

function PickDate(iMonth, iDay, iYear, sMonthCtrlName, sDayCtrlName, sYearCtrlName) {
	var sURL;

	sURL = '/includes/datepicker.asp?MONTH=' + iMonth + '&DAY=' + iDay + '&YEAR=' + iYear + '&M_NAME=' + sMonthCtrlName + '&D_NAME=' + sDayCtrlName + '&Y_NAME=' + sYearCtrlName;
	var newwin = window.open(sURL, 'calendar', 'TOP=150,LEFT=300,WIDTH=170,HEIGHT=220,RESIZABLE=yes,SCROLLBARS=no,STATUS=0');
	if (newwin != null)
	{
		if (newwin.opener == null)
			newwin.opener = self;
	}
}

function getIndex(arrayName, value) {
	for (var i=0; i < arrayName.length; i++) if(value == arrayName[i]) return i;
	return -1;
}

function getValue(Field) {
	fieldType = Field.type;

	if (fieldType == "text") {
		return getTextValue(Field);
	} else if (fieldType == "hidden") {
		return getTextValue(Field);
	} else if (fieldType == "select-one") {
		return getListValue(Field);
	} else if (fieldType == "textarea") {
		return getTextValue(Field);
	} else if (fieldType == "file") {
		return getTextValue(Field);
	} else if (fieldType == "password") {
		return getTextValue(Field);
	} else if (fieldType == "checkbox") {
		return getCheckboxValue(Field);
	} else if (isNaN(fieldType)) {
		return getRadioValue(Field);
	} else {
		return getTextValue(Field);
	}
}

function isEmptyField(Field) {
	fieldType = Field.type;

	if (fieldType == "text") {
		return isEmptyText(Field);
	} else if (fieldType == "hidden") {
		return isEmptyText(Field);
	} else if (fieldType == "file") {
		return isEmptyText(Field);
	} else if (fieldType == "select-one") {
		return isEmptyList(Field);
	} else if (fieldType == "textarea") {
		return isEmptyText(Field);
	} else if (fieldType == "password") {
		return isEmptyText(Field);
	} else if (fieldType == "checkbox") {
		return isEmptyCheckbox(Field)
	} else if (isNaN(fieldType)) {
		return isEmptyRadio(Field)
	} else {
		return isEmptyText(Field);
	}
}

function isCurrency(Field) {
	strValue = Field.value;

	regexp = /^(([0-9]{1,3}(\,[0-9]{3})*)|([0-9]{0,3}))(\.[0-9]{2})?$/

	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}


function isFloat(Field) {
	strValue = Field.value;

	regexp = /^(\+|\-)?([0-9]+)(((\.|\,)?([0-9]+))?)$/

	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isInteger(Field) {
	strValue = getValue(Field);

	regexp = /^(\+|\-)?([0-9]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isMemberUserName(Field) {
	strValue = getValue(Field);

	regexp = /^[^*]([^$@\\ ]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isUserName(Field) {
	strValue = getValue(Field);

	regexp = /^([^$@\\ ]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}


function getCCNumber(sInput) {
	var sResult='';

	// make sure input is a string
	sInput+='';

	for (iLoop=0; iLoop<sInput.length; iLoop++) {
		if (sInput.substring(iLoop,iLoop+1) >= '0' && sInput.substring(iLoop, iLoop+1) <= '9')
			sResult += sInput.substring(iLoop, iLoop+1);
	}
	return sResult;
}

function isCreditCardNumber(Field) {
	var iChkSum=0;
	var ccnum = getCCNumber(getValue(Field));

	// check for correct card number length
	if (ccnum.length<13) return false;

    // make an array and fill it with the individual digits of the cc number
    ccnumchk=new Array;
	for (iLoop=0; iLoop < ccnum.length; iLoop++) {
		ccnumchk[ccnum.length-1-iLoop] = ccnum.substring(iLoop, iLoop+1);
	}

    // perform the weird mathematical method (some base 10 stuff) to
    // convert the number to a two digit number
    // for those of you who aren't as familiar with the js operators
    // i'll comment some of the math lines...well, really just one

    var skemp=0;
	for (iLoop=0; iLoop < ccnum.length; iLoop++) {
		// if splits is an even number...
        if (iLoop %2 != 0) {
			ccnumchk[iLoop]=ccnumchk[iLoop]*2;
			if (ccnumchk[iLoop] >= 10) ccnumchk[iLoop]=ccnumchk[iLoop]-9;
		}

        // switch ccnumchk[splits] to a number
		ccnumchk[iLoop]++;
		ccnumchk[iLoop]--;

		iChkSum = iChkSum + ccnumchk[iLoop].valueOf();
	}
	if (iChkSum%10 != 0) { return false; } // The result isn't base 10

	return true;
}

function isEmail(Field) {
	strValue = getValue(Field);

	regexp = /^([^$@\\ ]+)@((([^$@\\ \.]+)\.)+)([A-Za-z0-9]+)$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isURL(Field) {
	strValue = getValue(Field);

	regexp = /^http(s?):\/\/([^$@\\ ]+)$/i
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isEmailList(Field) {
	strValue = getValue(Field);

	// delete all spaces near comma
	rexp = /, /gi;
	strValue = strValue.replace(rexp, ',');
	rexp = / ,/gi;
	strValue = strValue.replace(rexp, ',');
	strArray = strValue.split(",");

	regexp = /^([^$@\\ ]+)@((([^$@\\ \.]+)\.)+)([A-Za-z0-9]+)$/

	for (i=0; i < strArray.length; i++) {
		if (isEmpty(strArray[i])) return false;
		if (!regexp.test(strArray[i])) return false;
	}
	// set new field value (with removed spaces between comma and addresses)
	Field.value = strValue;
	return true;
}

function isEmptyCheckbox(Field) {
	return !Field.checked;
}

function isEmptyRadio(Field) {
	found = false;

	if(isNaN(Field.length)) {
		return !Field.checked;
	}

	for(i=0; i< Field.length; i++) {
		if ( Field[i].checked ) {
			found = true;
			break;
		}
	}
	return !found;
}

function getListValue(Field) {
	return Field[Field.selectedIndex].value;
}

function getTextValue(Field) {
	return Field.value;
}

function getCheckboxValue(Field) {

	if (Field.checked) return Field.value;
	return '';
}

function getRadioValue(Field) {
	found = false;

	if(isNaN(Field.length)) {
		return Field.value;
	}

	for(i=0; i< Field.length; i++) {
		if ( Field[i].checked ) {
			return Field[i].value;
			break;
		}
	}
	return !found;
}

function isZip(Field) {
	strValue = getValue(Field);

	if (isEmpty(strValue)) {
		return false;
	}

	if (strValue.indexOf('-') >=0 ) {
		regexp = /^\d{5}-\d{4}$/
	} else {
		regexp = /^\d{5}$/
	}

	return regexp.test(strValue);
}

function isPhone(Field) {
	strValue = getValue(Field);

	regexp = /^\d{3}-\d{3}-\d{4}$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isFax(Field) {
	strValue = getValue(Field);

	regexp = /^\d{3}-\d{3}-\d{4}$/
	if (isEmpty(strValue)) {
		return false;
	}
	return regexp.test(strValue);
}

function isEmptyList(Field) {
	return isEmpty(Field[Field.selectedIndex].value);
}

function isEmptyText(Field) {
	return isEmpty(Field.value)
}

function isEmpty(s) {
	if (s == null || trim(s) == '') {
		return true;
	}
	else {
		return false;
	}
}

function convertYear(y) {
	var borderYEAR = 40;

	yearvalue = parseInt(y,10);
	if (isNaN(yearvalue)) return y;

	if (yearvalue - borderYEAR <= 0) {
		yearvalue = yearvalue + 2000
	} else if (yearvalue - 100 < 0) {
		yearvalue = yearvalue + 1900
	}

	return yearvalue;
}

function CheckTime(hh,mm,ampm) {
	if (isNaN(parseInt(hh,10))) return false;
	if (isNaN(parseInt(mm,10))) return false;
	if (isEmpty(ampm)) return false;

	hh = parseInt(hh,10);
	mm = parseInt(mm,10);

	if (ampm == 'PM' && hh > 12) return false;
	return true;
}

function CheckDate(m,d,y) {
	Months = "31/!/28/!/31/!/30/!/31/!/30/!/31/!/31/!/30/!/31/!/30/!/31";
	MonthArray  = Months.split("/!/");

	if (isNaN(parseInt(m,10))) return false;
	if (isNaN(parseInt(d,10))) return false;
	if (isNaN(parseInt(y,10))) return false;

	d = parseInt(d,10);
	y = parseInt(y,10);
	m = parseInt(m,10);

	y = convertYear(y);

	if (y <= 1900 ) return false;
	if (m < 1 || m > 12 ) return false;
	if ( isLeapYear(y)) MonthArray[1] = eval(eval(MonthArray[1]) + 1);

	if (d<1 || MonthArray[m-1] < d ) return false;
	return true;
}

function isLeapYear(Year)
{
	if(Math.round(Year/4) == Year/4){
		if(Math.round(Year/100) == Year/100){
			if(Math.round(Year/400) == Year/400)
				return true;
			else return false;
		}else return true;
	}
	return false;
}

function trim(str) {
	while (str.substring(0,1) == " ") {
		str = str.substring(1,str.length);
	}
	while (str.substring(str.length-1,str.length) == " ") {
		str = str.substring(0,str.length-1);
	}
	return str;
}

function openfilepopup(sUrl){
	var popurl=sUrl;
	winpops=window.open(popurl,"","width=600,height=475,status,scrollbars,resizable,")
}

function openimagepopup(sUrl) {
	var popurl=sUrl;
	winpops=window.open(popurl,"","width=500,height=338,resizable,")
}

function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

