ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
function xxx_setType(obj){
	var frm = obj.form;
	fldval = frm.miscField.options[frm.miscField.selectedIndex].value;
	fldtyp = frm.miscField.options[frm.miscField.selectedIndex].text;
	fldtyp = fldtyp.substr(fldtyp.length-2,1);
	frm.miscType.value = fldtyp;
	if(fldtyp=="E" || fldtyp=="S"){
		if(ie4){
			document.all["tdiv"].style.visibility = "hidden";
			frm.miscValueTxt.value = "";
			document.all["cdiv"].style.visibility = "visible";
		}
		resetCbo(frm,fldval);
	}
	else if(fldtyp=="Y"){
		if(ie4){
			document.all["tdiv"].style.visibility = "hidden";
			frm.miscValueTxt.value = "";
			document.all["cdiv"].style.visibility = "visible";
		}
		x = frm.miscValueCbo.options.length
		for (var i = 0; i <= x; i++) {
			frm.miscValueCbo.options[i] = null
		}
		frm.miscValueCbo.options[0] = new Option("","")
		frm.miscValueCbo.options[1] = new Option("Yes","1");
		frm.miscValueCbo.options[2] = new Option("No","0")
		frm.miscValueCbo.options.length = 3;
	}
	else {
		if(ie4){
			document.all["cdiv"].style.visibility = "hidden";
			frm.miscValueCbo.value = "";
			document.all["tdiv"].style.visibility = "visible";
		}
	}
}
function validate(obj,which){
	ctrl_fld = document.getElementById("miscField"+which);
	ctrl_type = document.getElementById("miscType"+which);
	ctrl_text = document.getElementById("miscValueTxt"+which);
	if(ctrl_type.value == "D" && "x,dateadd,getdate,current".indexOf(left(ctrl_txt.value,7)) > 0){
		//looks like a calculated expresssion - leave as is
	}
	else if(ctrl_fld.selectedIndex > 0 && ctrl_type.value != "T" && ctrl_type.value != "E" && ctrl_type.value != "S"){
		return validatethis(ctrl_type.value, ctrl_text);
	}
	else {
		return true;
	}
}
function validate_again(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){
				oktosubmit = false;
				fld.onchange();
				if(!oktosubmit) return false;
			}
		}
		else if(fld.onblur != null){
			validationtype = fld.onblur.toString();
			if(validationtype.indexOf("validatethis") >= 0){
				oktosubmit = false;
				fld.onblur();
				if(!oktosubmit) return false;
			}
		}
	}
	return true;
}
function validatethis(type, field){
	msg = ""
	if(left(field.value,2) == "{{"){
	}
	else if(type == "s"){
		if(field.value.length <= 0){
			msg = field.name + " cannot be empty.";
		}
	}
	else if(type == "phone"){
		country_value = get_country(field);
		if(field.value.length == 0)
        msg = '';
		else if(country_value == "" || country_value == "United States" || country_value == "Canada"){
			msg = validatemask("999-999-9999 ??????????", field.value);
		}
	}
	else if(type == "ssn"){
		msg = validatemask("999-99-9999", field.value);
	}
	else if(type == "N"){
		if(checknumber(field.value) == false){
			msg = "Invalid format, expecting a number, without words or punctuation.";
		}
	}
	else if(type == "D"){
		if(!checkdate(field)){
			msg = "Please enter a valid date";
		}
	}
	else if(type == "time"){
		if(!checktime(field.value)){
			msg = "Please enter a valid time (hh:mm am/pm)";
		}
	}
	else if(type == "future"){
		now = new Date();
		today = new Date(now.getYear(),now.getMonth(),now.getDate());
		if(!checkdate(field) || new Date(field.value) < today){
			msg = "Please enter a valid date";
		}
	}
	else if(type == "DOB"){
		isvalid = checkdate(field);
		if(left(right(field.value,3),1) == "/"){
			msg = "Please enter a 4-digit year";
		}
		else if(!isvalid){
			msg = "Please enter a valid date";
		}
		else if(new Date(field.value) > new Date()){
			msg = "Please enter a date before today";
		}
	}
	else if(type == "mm/dd/yyyy"){
		if(right(field.value,4).indexOf("/") > 0){
			msg = "Please enter a valid date, 4 digit year";
		}
		else if(!checkdate(field)){
			msg = "Please enter a valid date";
		}
	}
	else if(type == "mm/yyyy"){
		if(date_mmyyyy(field) == false){
			msg = "Please enter a valid date in the form MM/YYYY";
		}
	}
	else if(type == "I"){
		if(checkinteger(field.value) == false){
			msg = "Please enter a valid integer."
		}
	}
	else if(left(type,1) == "I"){
		//Integer types I-P (percent), I-T (tinyint), I-S (smallint), I-M (mediumint)
		fieldtype = right(type,1);
		//SQLServer 
		if(fieldtype == "P") fieldmax = 100;
		else if(fieldtype == "T") fieldmax = 255;
		else if(fieldtype == "S") fieldmax = 32767;
		else if(fieldtype == "I") fieldmax = 2147483647;
		//MYSQL 
		// if(fieldtype == "P") fieldmax = 100; 
		//else if(fieldtype == "T") fieldmax = 255;
		//else if(fieldtype == "S") fieldmax = 65535;
		//else if(fieldtype == "M") fieldmax = 16777215;
		//else if(fieldtype == "I") fieldmax = 4294967295;
		if(checkinteger(field.value) == false){
			msg = "Please enter a valid integer.";
		}
		if(checkrange(field.value,0,fieldmax) == false){
			msg = "Please enter a number smaller than " + fieldmax;
		}
	}
	else if(type == "cc"){
		if(!_checkcreditcard(field.value)){
			msg = "Please enter a valid card number.";
		}
	}
	else if(type == "zip"){
		var r1 = /^(\d{5}|\d{5}-\d{4})$/;
		var r2 = /[A-Z]\d[A-Z][ ]\d[A-Z]\d/;
		if(field.value && !r1.test(field.value) && !r2.test(field.value)){
			msg = "Please enter a valid zip or postal code.";
		}
	}
	else if(type == "money"){
		var r = /^([\$\d]*)$/;
		if(field.value && !r.test( field.value )){
			msg = "Please enter a valid dollar amount.";
		}
	}
	else if(type == "login-password"){
		if(field.value.length < 6){
			msg = "Please enter a " + field.name + " of least 6 letters or numbers.";
		}
	}
	else if(type == "multiple email"){
		var r = /^(\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+(,\s*)*)+$/;
		if(field.value && !r.test( field.value )){
			alert("Please enter one or more valid e-mail addresses separated by commas.")
			field.select();
			field.focus();
			return false;
		}
	}
	else if(type == "email"){
		var r = /^[\w\-%_\.]+@([\w\-]+\.)+\w{2,4}$/;
		if(field.value && !r.test( field.value )){
			msg = "Please enter a valid e-mail address.";
		}
	}
	if(msg != ""){
		alert(msg);
		field.select();
		field.focus();
		oktosubmit = false;
		return false;
	}
	oktosubmit = true;
	return true;
}
function get_country(field){
	pos = field.name.indexOf("_");
	if(pos > 0){
		if(typeof(field.form.elements[left(field.name,pos) + "_country"]) == "object"){
			fld = field.form.elements[left(field.name,pos) + "_country"];
			return fld.options[fld.selectedIndex].text;
		}
		else {
			return "";
		}
	}
	else {
		return "";
	}
}
function validatemask(mask, value){
	msg = "";
	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")) msg = "Invalid format";
		}
		else if(m == "A"){
			if(outside(v.toUpperCase()), "A", "Z") msg = "Invalid format";
		}
		else if(m == "?"){
		}
		else if(m == " " && v == ""){
		}
		else {
			if(m != v) msg = "Invalid format";
		}
	}
	if(msg == ""){
		return "";
	}
	else {
		return msg +": Expecting entry in the form '" + mask + "'";
	}
}
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	

	today = new Date()
	if(obj.value == "t" || obj.value == "T"){
		obj.value = (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getYear();
	}
	if (obj.value.length >= 1 && obj.value.length <= 5){
		obj.value = obj.value +"/"+ today.getYear()
	}

	object_value = obj.value
    if (object_value.length == 0)
        return true;

    //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, 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 checktime(object_value){
	if(object_value.length == 0){
		return true;
	}
	else if(validatemask("99:99 PM", object_value) == ""){
		object_value = object_value.substring(0,object_value.indexOf(' '));
		
	    //Returns true if value is in time format or is NULL otherwise returns false	
	    if (object_value.length == 0)
	        return true;
	
	    //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;
	
	    sHour = object_value.substring(0, isplit);
		iminute = object_value.indexOf(':', isplit + 1);
	
		if (iminute == -1 || iminute == object_value.length)
			sMin = object_value.substring((sHour.length + 1));
		else
			sMin = object_value.substring((sHour.length + 1), iminute);
	
	    if (!checkinteger(sHour)) //check hour
			return false;
	    else
	    if (!checkrange(sHour, 1, 12)) //check hour
			return false;
	
		if (!checkinteger(sMin)) //check minutes
			return false;
		else
		if (!checkrange(sMin, 0, 59)) // check minutes
			return false;
	
		// did they specify seconds
	    if (iminute != -1){
			sSec = object_value.substring(iminute + 1);
	
			if (!checkinteger(sSec)) //check seconds
				return false;
			else
			if (!checkrange(sSec, 0, 59)) //check seconds
				return false;	
		}
	    return true;
	}
	else {
		return false;
	}
}
function _checkcreditcard(object_value){
	var white_space = " -";
	var creditcard_string="";
	var check_char;

    if (object_value.length == 0)
        return true;

	// squish out the white space
	for (var i = 0; i < object_value.length; i++){
		check_char = white_space.indexOf(object_value.charAt(i))
		if (check_char < 0)
			creditcard_string += object_value.substring(i, (i + 1));
	}	

	// if all white space return error
    if (creditcard_string.length == 0)
        return false;
	 	
	// make sure number is a valid integer
	if (creditcard_string.charAt(0) == "+")
        return false;

	if (!checkinteger(creditcard_string))
		return false;

    // now check mod10

	var doubledigit = creditcard_string.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcard_string.length; i++){
		tempdigit = eval(creditcard_string.charAt(i))
		if (doubledigit){
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);
			if ((tempdigit / 10) >= 1.0){
				checkdigit++;
			}

			doubledigit = false;
		}
		else{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
}
function fieldChecked(field){
	x = 0;
	for (i=0; i<field.length; i++){
		if (field[i].checked){
			x = x + 1;
		}
	}
	return x;
}
function left(txt,num){
	return txt.substr(0,num);
}
function right(txt,num){
	len = txt.length;
	return txt.substr(len-num,num);
}
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){
				if(obj[i].value == "on" || obj[i].value == ""){
					return i+1;
				}
				else {
					return obj[i].value;
				}
			}
		}
	}
	return 0;
}
function set_dropdown(obj,code){
	for(i=0;i<obj.length;i++){
		if(obj.options[i].value == code){
			obj.selectedIndex = i;
			break;
		}
	}
}
function left(txt,num){
	return txt.substr(0,num);
}
function right(txt,num){
	len = txt.length;
	return txt.substr(len-num,num);
}
function enable(obj,on,isselect){
	if(isselect){
		obj.readOnly = !on;
	}
	else {
		obj.readOnly = !on;
	}
	if(on){
		obj.style.backgroundColor = "white";
		obj.className = "input";
		obj.style.borderColor = "99bfd2";
	}
	else {
		obj.style.backgroundColor = "ECE7D8";
		obj.style.borderColor = "d1b192";
	}
}

