ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
function setType(frm,which){
	ctrl_field = eval("frm.miscField"+which);
	ctrl_type = eval("frm.miscType"+which);
	ctrl_oper = eval("frm.miscOper"+which);
	ctrl_combo = eval("frm.miscValueCbo"+which);
	ctrl_text = eval("frm.miscValueTxt"+which);

	fldval = ctrl_field.options[ctrl_field.selectedIndex].value;
	fldtyp = ctrl_field.options[ctrl_field.selectedIndex].text;
	fldtyp = fldtyp.substr(fldtyp.length-2,1);
	ctrl_type.value = fldtyp;
	if(ctrl_oper.selectedIndex <= 0){
		opr = "";
	}
	else {
		opr = ctrl_oper.options[ctrl_oper.selectedIndex].value;
	}
	if((fldtyp=="E" || fldtyp=="S") && opr != "in"){
		if(ie4 || navigator.appVersion > "6"){
			document.getElementById("tdiv"+which).style.visibility = "hidden";
			ctrl_text.value = "";
			document.getElementById("cdiv"+which).style.visibility = "visible";
		}
		resetCbo(frm,fldval,which);
	}
	else if(fldtyp=="Y"){
		if(ie4 || navigator.appVersion > "6"){
			document.getElementById("tdiv"+which).style.visibility = "hidden";
			ctrl_combo.selectedIndex = -1;
			document.getElementById("cdiv"+which).style.visibility = "visible";
		}
		x = ctrl_combo.options.length
		for (var i = 0; i <= x; i++) {
			ctrl_combo.options[i] = null
		}
		ctrl_combo.options[0] = new Option("","")
		ctrl_combo.options[1] = new Option("Yes","1");
		ctrl_combo.options[2] = new Option("No","0")
		ctrl_combo.options.length = 3;
	}
	else {
		if(ie4 || navigator.appVersion > "6"){
			document.getElementById("cdiv"+which).style.visibility = "hidden";
			ctrl_combo.selectedIndex = -1;
			document.getElementById("tdiv"+which).style.visibility = "visible";
			document.getElementById("tdiv"+which).value = "";
		}
		resetCbo(frm,fldval,which);
	}
}
function copyvalue(frm,which){
	ctrl_combo = eval("frm.miscValueCbo"+which);
	ctrl_text = eval("frm.miscValueTxt"+which);
	ctrl_text.value = ctrl_combo.options[ctrl_combo.selectedIndex].value;
}
function validate(obj,which){
	var frm = obj.form;
	ctrl_type = eval("frm.miscType"+which);
	ctrl_text = eval("frm.miscValueTxt"+which);
	if(ctrl_type.value == "T" || ctrl_type.value == "E"){
		// plain text or dropdown - no validation needed
		return true;
	}
	else {
		return validatethis(ctrl_type.value, ctrl_text);
	}
}
function validatethis(type, field) {
	oktosubmit = true;
	msg1 = "";
	msg2 = "";
	if(type == "phone") {
		mask = "'999-999-9999 ??????'";
		msg1 = validatemask("999-999-9999 ??????", field.value);
		msg2 = validatemask("999-9999 ??????", field.value);
	}
	else if(type == "N"){
		if(checknumber(field.value) == false){
			mask = "of a number, without words or punctuation.";
			msg1 = "Invalid format";
			msg2 = "x";
		}
	}
	else if(type == "D"){
		if (checkdate(field) == false) {
		alert("Please enter a valid date");
		field.select();
		field.focus();
		oktosubmit = false;
		}
	}
	else if(type == "I"){
		if (checkinteger(field.value) == false) {
		alert("Please enter a valid integer.")
		field.select();
		field.focus();
		oktosubmit = false;
		}
	}
	else if(type == "mm/yyyy or yyyy"){
		mask = "MM/YYYY or YYYY";
		if(field.value.length == 0){
			oktosubmit = true;
		}
		else if(field.value.length <= 4){
			if(field.value >= 1950 && field.value <= 2050){
				oktosubmit = true;
			}
			else {
				msg1 = "Please enter a valid date";
				msg2 = "x";
			}
		}
		else {
			if(date_mmyyyy(field)){
				oktosubmit = true;
			}
			else {
				msg1 = "Please enter a valid date";
				msg2 = "x";
			}
		}
	}
	else if(type == "yyyy"){
		if (field.value >= 1950 && field.value <= 2050){
			oktosubmit = true;
			//ok
		}
		else {
			mask = "YYYY";
			msg1 = "Please enter a valid date";
			msg2 = "x";
		}
	}
	else if(type == "mm/yyyy"){
		if(date_mmyyyy(field) == false){
			msg1 = "Please enter a valid date"
			msg2 = "x"
		}
	}
	else if(type == "email"){
		var r = /^[\w\-%_\.]+@([\w\-]+\.)+\w{2,4}$/;
		if(field.value && !r.test( field.value )){
			alert("Please enter a valid email address.")
			field.select();
			field.focus();
			oktosubmit = false;
		}
	}
	else if(type == "zip"){
		var r = /^(\d{5}|\d{5}-\d{4})$/;
		if(field.value && !r.test( field.value )){
			alert("Please enter a valid zip code.");
			field.select();
			field.focus();
			oktosubmit = false;
		}
	}
	if(msg1 != "" && msg2 != ""){
		alert(msg1 +": Expecting entry in the form " + mask);
		field.select();
		field.focus();
		oktosubmit = false;
	}
	return oktosubmit;
}
function rerun_validation(obj){
	for(i=0; i < obj.elements.length; i++){
		fld = obj.elements[i];
		if(fld.onchange != null){
			validationtype = fld.onchange.toString();
			if(validationtype.indexOf("validatethis") >= 0){
				if(document.all){
					first_quote = validationtype.indexOf("'");
					validationtype = validationtype.substring(first_quote+1,validationtype.indexOf("'",first_quote+1));
				}
				else {
					first_quote = validationtype.indexOf('"');
					validationtype = validationtype.substring(first_quote+1,validationtype.indexOf('"',first_quote+1));
				}
				if(!validatethis(validationtype,fld)){
					return false;
				}
			}
		}
		else if(fld.onblur != null){
			validationtype = fld.onblur.toString();
			if(validationtype.indexOf("validatethis") >= 0){
				if(document.all){
					validationtype = validationtype.substring(validationtype.indexOf("'")+1,validationtype.lastIndexOf("'"));
				}
				else {
					validationtype = validationtype.substring(validationtype.indexOf('"')+1,validationtype.lastIndexOf('"'));
				}
				if(!validatethis(validationtype,fld)){
					return false;
				}
			}
		}
	}
	return true;
}
function date_mmyyyy(field){
   	mmyyyy = field.value;
 	pos = mmyyyy.indexOf('/');
 	if(pos == 0 || pos != mmyyyy.lastIndexOf("/")){
		return false;
 	}
 	else {
		if(mmyyyy.substr(0,1) == "0"){
			m = mmyyyy.substr(1,1)
		}
		else if(pos == 2) {
			m = mmyyyy.substr(0,2)
		}
		else {
			m = mmyyyy.substr(0,1)
		}
		if(parseInt(mmyyyy.substr(pos+1)) == 0){
  			y = parseInt(mmyyyy.substr(pos+2));
		}
		else {
  			y = parseInt(mmyyyy.substr(pos+1));
		}
		if (mmyyyy == ""){
			//ok
		}
	 	else if (m > 0 && m < 13 && y >= 1950 && y <= 2050){
			//ok
	 	}
	 	else if (m > 0 && m < 13 && y >= 1 && y <= 9){
	 		field.value = m.toString() + "/200" + y.toString();
	 	}
	 	else if (m > 0 && m < 13 && y >= 10 && y <= 20){
	 		field.value = m.toString() + "/20" + y.toString();
	 	}
	 	else {
			return false;
	 	}
	}
	return true;
}
function fieldChecked(field){
	x = 0;
	for (i=0; i<field.length; i++){
		if (field[i].checked){
			x = x + 1;
		}
	}
	return x;
}
function fieldClear(field){
	for (i=0; i<field.length; i++){
		field[i].checked = false;
	}
}
function mutexcl(field,othervalue,opt1,opt2){
	exclchecked = 0;
	mainchecked = 0;
	if(othervalue) mainchecked++;
	for (i=0; i<field.length; i++){
		if (field[i].checked){
			if(field[i].value == opt1 || field[i].value == opt2){
				exclchecked++;
			}
			if(field[i].value != opt1 && field[i].value != opt2){
				mainchecked++;
			}
		}
	}
	return mainchecked > 0 && exclchecked > 0 || exclchecked >= 2;
}
function validatemask(mask, value) {
		if(value.length == 0) {
			return ""
		}
		for (var i=0; i <= Math.max(value.length,mask.length); i++) {
			m = mask.substring(i,i+1);
			v = value.substring(i,i+1);
			if(m == "9") {
				if(outside(v, "0", "9")) return "Invalid format"
			}
			else if(m == "A") {
				if(outside(v.toUpperCase()), "A", "Z") return "Invalid format"
			}
			else if(m == "?") {
			}
			else if(m == " " && v == "") {
			}
			else {
				if(m != v) return "Invalid format"
			}
		}
		return "";
}

function outside(string, x, y) {
	if (string < x || string > y) return true
	return false
}

function checkdate(obj){
//Returns true if value is a date format or is NULL
//otherwise returns false	

    if (obj.value.length == 0)
        return true;

	if (obj.value.length <= 5){
		today = new Date();
		yr = today.getYear();
		if(ns4) yr = yr + 1900;
		obj.value = obj.value +"/"+ yr;
	}
	object_value = obj.value

    //Returns true if value is a date in the mm/dd/yyyy format
	isplit = object_value.indexOf('/');

	if (isplit == -1 || isplit == object_value.length)
		return false;

    sMonth = object_value.substring(0, isplit);
	isplit = object_value.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;

    sDay = object_value.substring((sMonth.length + 1), isplit);

	sYear = object_value.substring(isplit + 1);

	if (!checkinteger(sMonth)) //check month
		return false;
	else
	if (!checkrange(sMonth, 1, 12)) //check month
		return false;
	else
	if (!checkinteger(sYear)) //check year
		return false;
	else
	if (!checkrange(sYear, 0, 9999)) //check year
		return false;
	else
	if (!checkinteger(sDay)) //check day
		return false;
	else
	if (!checkday(sYear, Number(sMonth), sDay)) // check day
		return false;
	else
		return true;
}
function checkday(checkYear, checkMonth, checkDay){
	maxDay = 31;
	if(checkMonth == 4 || checkMonth == 6 || checkMonth == 9 || checkMonth == 11){
		maxDay = 30;
	}
	else
	if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else
		if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}
	return checkrange(checkDay, 1, maxDay); //check day
}
function checkinteger(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 an integer defined as
    //   having an optional leading + or -.
    //   otherwise containing only the characters 0-9.
	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 checknumber(object_value);
    else
	return false;
}

function checkrange(object_value, min_value, max_value){
    //if value is in range then return true else return false

    if (object_value.length == 0)
        return true;

    if (!checknumber(object_value))
	{
	return false;
	}
    else
	{
	return (numberrange((eval(object_value)), min_value, max_value));
	}
	
    //All tests passed, so...
    return true;
}

function numberrange(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 checknumber(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 hasvalue(obj){
	if(obj == null){
		x = false;
	}
	else if(obj == 0){
		x = false;
	}
	else if(obj == "0"){
		x = false;
	}
	else if(obj == ""){
		x = false;
	}
	else {
		x = true;
	}
	return x;
}                     
function setcheck(obj, field){
	fld = obj.form.elements[field];
	if(obj.value != ""){
		fld[fld.length-1].checked = true;
	}
	else {
		fld[fld.length-1].checked = false;
	}
}

function setcheckfield(obj, field){
	fld = obj.form.elements[field];
	if(obj.value != ""){
		fld.checked = true;
	}
	else {
		fld.checked = false;
	}
}
function minimum(minnum,type,field){
	r = true
	if(type == 'D'){
		if(new Date(field.value) < new Date(minnum)){
			alert("The minimum entry is "+minnum)
			field.select();
			field.focus();
			r = false;
		}
	}
	else if(field.value < minnum){
		alert("The minimum entry is "+minnum)
		field.select();
		field.focus();
		r = false;
	}
	return r;
}
function maximum(maxnum,type,field){
	if(type == 'D'){
		if(new Date(field.value) > new Date(maxnum)){
			alert("The maximum entry is "+maxnum)
			field.select();
			field.focus();
			return false;
		}
	}
	else if(field.value > maxnum){
		alert("The maximum entry is "+maxnum)
		field.select();
		field.focus();
		return false;
	}
}
function minlength(obj,num){
	if(obj.value.length < num){
		alert("The " + obj.name + " must be at least " + num + " characters.");
		obj.select();
		obj.focus();
		return false;
	}
}
function get_checked_value(obj){
	if(typeof(obj) == "undefined"){
		return 0;
	}
	else if(typeof(obj.length) == "undefined"){
		if(typeof(obj.value) == "string" && obj.checked){
			return obj.value;
		}
		else {
			return 0;
		}
	}
	else {
		for(i=0;i<obj.length;i++){
			if(obj[i].checked){
				return obj[i].value;
			}
		}
	}
	return 0;
}
