
/**********************************************************************************/
/* Form Validation: Begin													      */
/**********************************************************************************/

function IsEmpty(obj, obj_type)
{
	if (obj_type == "text" || obj_type == "password" || obj_type == "textarea" || obj_type == "file")	{
		if (obj.value.length == 0) {
			obj.focus();
			return true;
		} else {
			return false;
		}
	} else if (obj_type == "select") {
		for (i=0; i < obj.length; i++) {
			if (obj.options[i].selected) {
				if(obj.options[i].value == "") {
					obj.focus();
					return true;
				} else {
					return false;
				}
			}
			
		}
		return true;	
	} else if (obj_type == "radio" || obj_type == "checkbox") {
		if (!obj[0] && obj) {
			if (obj.checked) {
				return false;
			} else {
				obj.focus();
				return true;	
			}
		} else {
			for (i=0; i < obj.length; i++) {
				if (obj[i].checked) {
					return false;
				}
			}
			obj[0].focus();
			return true;
		}
	} else {
		return false;
	}
}

function InLengthRange(object_value, min_value, max_value)
{
	
	if (object_value == null) {
		return true;
	}

	if (min_value == null) {
		min_value = 0;
	}
	
	if (max_value == null) {
		if (object_value.length >= min_value) {
			return true;
		} else {
			return false;
		}
	}
	
	if (object_value.length >= min_value && object_value.length <= max_value) {
		return true;
	} else {
		return false;
	}

}

function InValueRange(object_value, min_value, max_value)
{
	
	if (object_value == null) {
		return true;
	}
	else {
		if (object_value == '')
			return true;
	}

	if (min_value == null) {
		min_value = 0;
	}
	
	

	if (max_value == null) {
		if (object_value >= min_value) {
			return true;
		} else {
			return false;
		}
	}
	
	if (object_value >= min_value && object_value <= max_value) {
		return true;
	} else {
		return false;
	}

}


function TestNumberRange(object_value, min_value, max_value)
{
	// check minimum
	if (min_value != null) {
		if (object_value < min_value) {
			return false;
		}
	}
		
	// check maximum
	if (max_value != null) {
		if (object_value > max_value) {
			return false;
		}
	}
	//All tests passed, so...
	return true;
}

function IsValidDate(month, day, year) {
	var blnRet;

	if (month ==  "" || day == "" || year == "") {
		return true;
	}

	day = parseInt(day)
	month = parseInt(month)
	year = parseInt(year)
	

	if( (day >= 1 && day <= 31) && 
		(month >=1 && month <= 12) && 
		(year > 0) ) {
		blnRet = true;
		switch(month) {
			case 2:
				
				if(day > 28) {
					// check for leap years
					if( ((year % 4 == 0) && (year % 100 != 0)) ||
						(year % 400 == 0) ) {
						if(day > 29) {
							blnRet =  false;
						}
					} else {
						blnRet = false;
					}
				}
				break;
			case 4:
			case 6:
			case 9:
			case 11:
				if(day > 30) {
					blnRet = false;
				}
				break;
			default:
				break;
		}
	} else {
		blnRet = false;
	}
		
	return blnRet;
}

function IsInteger(object_value)
{
    if (object_value.length == 0){
		return true;
	}

	var decimal_format = ".";
	var check_char;

    //The first character can be + -  blank or a digit.
	check_char = object_value.indexOf(decimal_format)

    //Was it a decimal?
	if (check_char < 1) {
		return IsNumeric(object_value);
	} else {
		return false;
	}
}


function IsNumeric(object_value)
{
	//Returns true if value is a number or is NULL
	//otherwise returns false	
	
	if (object_value.length == 0) {
		return true;
	}
	
	//	Returns true if value is a number defined as
	//	having an optional leading + or -.
	// 	having at most 1 decimal point.
	//	otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
	
	//The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
	//Was it a decimal?
	if (check_char == 1) {
		decimal = true;
	} else if (check_char < 1) {
		return false;
	}
	        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++)
	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0) {
			return false;
		} else if (check_char == 1) {
			if (decimal) {		// Second decimal.
				return false;
			} else {
				decimal = true;
			}
		} else if (check_char == 0)	{
			if (decimal || digits) {
				trailing_blank = true;
			}
			// ignore leading blanks
		} else if (trailing_blank) {
			return false;
		} else {
			digits = true;
		}
	}	
	//All tests passed, so...
	return true
}

function IsNumberChar(ch)
{
	return (ch >= '0' && ch <= '9');
}

function IsAlphaChar(ch)
{
	return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
}

function IsAlphaNumeric(object_value)
{
	var i;
	var intLength, ch;
	intLength = object_value.length;
	
	for(i = 0; i < intLength; i++) 
	{
		ch = object_value.charAt(i);
		if(!(IsAlphaChar(ch) || IsNumberChar(ch))) {
			return false;
		}
	}
	return true;
}

function IsAlpha(object_value)
{
	var i;
	var intLength, ch;
	intLength = object_value.length;
	
	for(i = 0; i < intLength; i++) 
	{
		ch = object_value.charAt(i);
		if(!IsAlphaChar(ch)) {
			return false;
		}
	}
	return true;
}


function IsEmail(object_value, min_value)
{
	var i, ch, intAtCount, intDotCount;
	
	var strEmailChars = "_-@.~!#$%&*+\"'"

	if (object_value.length == 0) {
		return true;
	}
	var len = object_value.length;
	
	if (len == 0 && min_value == 0) {
		return true;
	}
	
	if (len < min_value) {
		return false;
	}
	
	
	intAtCount = 0;
	intDotCount = 0;
	for(i = 0; i < len; i++) {
		ch = object_value.charAt(i);
		
		if(!(	IsAlphaChar(ch) || 
				IsNumberChar(ch) ||
				strEmailChars.indexOf(ch) != -1) 
			) {
				return false;
		} else {
			if(ch == '@') {
				intAtCount++;
				if(intAtCount > 1) {
					return false;
				}
			} else if (ch == '.') {
				intDotCount++;
			}
		}
	}
	
	return (intDotCount > 0 && intAtCount > 0);
	
}

function IsURL(object_value)
{
	var i, ch, intDotCount;
	
	var strURLChars = "~!@#$%*_+-:/.?&="
	
	var len = object_value.length;
	
	if (object_value.length == 0) {
		return true;
	}
	
	if (object_value.length < 10) {
		return false;
	}
	
	if	((object_value.indexOf("http://") == -1) &&
			(object_value.indexOf("https://") == -1) &&
			(object_value.indexOf("ftp://") == -1)) {
		return false;
	}
	
	intDotCount = 0;
	for(i = 0; i < len; i++) {
		ch = object_value.charAt(i);
		
		if(!(	IsAlphaChar(ch) || 
				IsNumberChar(ch) ||
				strURLChars.indexOf(ch) != -1) 
			) {
				return false;
		} else if(ch == '.') {
			intDotCount++;
		} 
	}
	
	return (intDotCount > 0);
	
}

function IsValidExtension(object_value, extensions)
{
	// ---------------Examples----------------
	// extensions -->   "jpeg, gif, zip"
	// object_value --> "chaka.zip"
	// ---------------------------------------

	var i, ch, intDotCount;
	var test_extension = "";
	var obj_extension = "";
	var len = object_value.length;

	if ((object_value == "") || (extensions == ""))
		return true;
		 
	// --------------------------------------------------
	// First get the extension of the object value
	// --------------------------------------------------
	for (i = len; i > 0; i--) {
		ch = object_value.charAt(i);
		if (ch == '.') {
			intDotCount = i;
			break;
		}
	}	
	
	for (i = (intDotCount + 1); i < len; i++) {
		obj_extension = obj_extension + object_value.charAt(i);
	}

	if (obj_extension.length == 0)
		return false;

	// --------------------------------------------------
	// Then, we compare that string to every extension we passed in
	// --------------------------------------------------

	extensions = extensions.toLowerCase();
	obj_extension = obj_extension.toLowerCase();

	len = extensions.length;
	test_extension = "";

	for (i = 0; i <= len; i++) {

		ch = extensions.charAt(i);

		if ((ch == ',') || (ch == ' ') || (i == len)) {
			if (test_extension.length == 0)
				continue;			
			if (test_extension == obj_extension)
				return true;
			test_extension = "";
		}
		else {
			test_extension = test_extension + ch;
		}
	}
	return false;
}

function LimitInput(field, countfield, maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
	{
		field.value = field.value.substring(0, maxlimit);
	}
	// otherwise, update 'characters left' counter
	else
	{
		if (countfield != null) 
			countfield.value = maxlimit - field.value.length;
	}
}
	
function IsCreditCard(object_value) {
  // Encoding only works on cards with less than 19 digits
  if (object_value.length > 19)
    return (false);

  sum = 0; mul = 1; l = object_value.length;
  for (i = 0; i < l; i++) {
    digit = object_value.substring(l - i - 1, l - i);
    tproduct = parseInt(digit ,10) * mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()


function ModalBox(blnInterNational, strText)
{
	if (blnInterNational && window.showModalDialog)
	{
		window.showModalDialog("/lib/ModalBox.asp?strText=" + escape(strText),null,"edge:raised;status:no;unadorned:yes;font-size:14px;dialogWidth:20em;dialogHeight:10em");
	}
	else
	{
		alert(strText);
	}
}

/**********************************************************************************/
/* Form Validation: End															  */
/**********************************************************************************/


