/*
 * Basic validation & misc. usefull functions script file
 * Created by Mark Frederik - kuvra@yahoo.com
 */

function hasValue(fieldName,alertMsg) {
	if ( fieldName.value.length == 0 ) {
		alert(alertMsg);
		fieldName.focus();
		return false;
	} else return true;
}

function setValue(fieldName,theValue) {
	fieldName.value = theValue;
}

function setEmpty(fieldName) {
	fieldName.value = "";
}

function isEmail(fieldName, alertMsg) {
	var idx1 = fieldName.value.indexOf("@");
	var status = 1;
	if ( idx1 == -1 ) status = 0;	
	else {
		var idx2 = fieldName.value.lastIndexOf("@");
		if (fieldName.value.substring(0,idx1).length == 0) status = 0;
		if (idx1 != idx2) status = 0;
		else {
			idx2 = fieldName.value.indexOf(".");
			if (idx2 < idx1 ) status = 0;
			else {
				if (fieldName.value.substring(idx1+1,idx2).length == 0 ) status = 0;
				if (fieldName.value.substring(idx2+1,fieldName.value.length).length == 0 ) status = 0;
			}
		}
	}
	if (!status) {
		alert(alertMsg);
		fieldName.focus();
	}
	return status;
}

function minLength(fieldName, minLen, alertMsg) {
	if (fieldName.value.length < minLen){
		alert(alertMsg);
		fieldName.focus();
		return false;
	} else return true;
}

function maxLength(fieldName, maxLen, alertMsg) {
	if (fieldName.value.length > maxLen){
		alert(alertMsg);
		fieldName.focus();
		return false;
	} else return true;
}

function isDate(day, month, year) {
	var status=1;
	if (( month == 2 ) && ( day > 27 )) {
		if (( year % 4 == 0 ) && ( day > 29 )) {
			status = 0;
			alert("There is only 29 days for February " + year);
		}
		if (( year % 4 != 0 ) && ( day > 28 )) {
			status = 0;
			alert("There is only 28 days for February " + year);
		}
	}

	if ((( month == 4  ) || ( month  == 6 ) || ( month == 9 ) || ( month == 11 )) && ( day > 30 )) {
		status = 0;
		alert("There is only 30 days for the current month");
	}
	return status;
}

function isAlphabet(fieldName) {
	var i=0;
	var status = 1;
	var valStr = fieldName.value.toUpperCase();
	for (i=0 ; i<fieldName.value.length ; i++) {
		if ((valStr.substring(0,i+1) < "A") || (valStr.substring(0,i+1) > "Z")) {
			status = 0;
			break;
		}
	}
	return status;
}

function isNumeric(fieldName) {
	var i = 0;
	var status = 1;
	for (i=0 ; i<fieldName.value.length ; i++) {
		if ((fieldName.value.substring(i,i+1) < "0") || (fieldName.value.substring(i,i+1) > "9")) {
			status = 0;
			break;
		}
	}
	return status;
}

function leftTrim(fieldNameValue) {
	var tmpStr = fieldNameValue;
	while (tmpStr.substring(0,1) == " ") {
		tmpStr = tmpStr.substring(1,tmpStr.length);	
	}
	return tmpStr;
}

function rightTrim(fieldNameValue) {
	var tmpStr = fieldNameValue;
	while (tmpStr.substring(tmpStr.length-1,tmpStr.length) == " ")	{
		tmpStr = tmpStr.substring(0,tmpStr.length-1);	
	}
	return tmpStr;
}

function trim(fieldNameValue) {
	var tmpStr = leftTrim(fieldNameValue);
	tmpStr = rightTrim(tmpStr);
	return tmpStr;
}

function thereIsNo(fieldName,charList) {
	var i = 0;
	var j = 0;
	var tmpChr = "A"
	var status = 1;
	for (i=0 ; i<fieldName.value.length ; i++) {
		tmpChr = fieldName.value.substring(i,i+1);
		for (j=0 ; j<charList.length ; j++) {
			if (tmpChr == charList.substring(j,j+1)) {
				status = 0;
				alert("This input cannot contain \"" + charList.substring(j,j+1) + "\" characater(s).\n Please erase it");
				fieldName.focus();
				break;
			}
		}
		if (!status)  break;
	}
	return status;
}

function noSpace(fieldName,alertMsg) {
	var i = 0;
	var status = 1;
	for (i=0 ; i<fieldName.value.length ; i++ ) {
		if (fieldName.value.substring(i,i+1) == " ") {
			alert("This input cannot contain space(s)");
			fieldName.focus();
			status = 0;
			break;
		}
	}
	return status;
}
