
ns4 = (document.layers)? true:false;
ie4 = (document.all)? true:false;
function setType(obj,which){
	ctrl_fld = eval("obj.miscField"+which);
	ctrl_typ = eval("obj.miscType"+which);
	ctrl_opr = eval("obj.miscOper"+which);
	ctrl_txt = eval("obj.miscValueTxt"+which);
	ctrl_cbo = eval("obj.miscValueCbo"+which);
	fldval = ctrl_fld.options[ctrl_fld.selectedIndex].value;
	fldtyp = ctrl_fld.options[ctrl_fld.selectedIndex].text;
	fldtyp = fldtyp.substr(fldtyp.length-2,1);
	ctrl_typ.value = fldtyp;
	if(fldtyp=="Y"){
		document.getElementById("tdiv" + which).style.visibility = "hidden";
		ctrl_txt.value = "";
		document.getElementById("cdiv" + which).style.visibility = "visible";
		x = ctrl_cbo.options.length
		for (var i = 0; i <= x; i++){
			ctrl_cbo.options[i] = null
		}
		ctrl_cbo.options[0] = new Option("","")
		ctrl_cbo.options[1] = new Option("Yes","1");
		ctrl_cbo.options[2] = new Option("No","0")
		ctrl_cbo.options.length = 3;
	}
	else if((fldtyp=="C" || fldtyp=="L") && ctrl_opr.options[ctrl_opr.selectedIndex].value != "in"){
		document.getElementById("tdiv" + which).style.visibility = "hidden";
		ctrl_txt.value = "";
		document.getElementById("cdiv" + which).style.visibility = "visible";
		reset_cbo(which);
		document.getElementById("tdiv" + which).style.width = 1;
	}
	else {
		document.getElementById("cdiv" + which).style.visibility = "hidden";
		ctrl_cbo.selectedIndex = -1;
		document.getElementById("tdiv" + which).style.visibility = "visible";
	}
}
function validate(obj,which){
	ctrl_typ = document.forms[0].elements["miscType" + which];
	ctrl_txt = document.forms[0].elements["miscValueTxt" + which];
	if(ctrl_typ.value == "D" && "x,dateadd,getdate".indexOf(left(ctrl_txt.value,7)) > 0){
		//looks like a calculated expresssion - leave as is
	}
	else if(ctrl_typ.value != "T" && ctrl_typ.value != "E"){
		return validatethis(ctrl_typ.value, ctrl_txt);
	}
}
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){
				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 == "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 == "C"){
		if(!_checkcreditcard(field.value)){
			msg = "Please enter a valid card number.";
		}
	}
	else if(type == "zip"){
		country_value = get_country(field);
		if(country_value == "" || country_value == "United States"){
			if(country_value <= 0){
				var r = /^(\d{5}|\d{5}-\d{4})$/;
				if(field.value && !r.test( field.value )){
					msg = "Please enter a valid zip code.";
				}
			}
		}
		else if(country_value == "Canada"){
			var r_cdn = /[A-Z]\d[A-Z][ ]\d[A-Z]\d/;
			if(field.value && !r_cdn.test( field.value)){
				msg = "Please enter a valid 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 == "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 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 >= 1870 && 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 _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 checkdate(obj){
//Returns true if value is a date format or is NULL
//otherwise returns false	
	today = new Date();
	
    if(obj.value.length == 0)
        return true;
	if(obj.value.indexOf('-') > 0)
		obj.value = obj.value.replace(/-/gi,"/");
	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, 99) && !checkrange(sYear, 1900, today.getFullYear()+5)) //check year
		return false;
	else
	if(!checkinteger(sDay) || sDay.length == 0 || sMonth.length == 0) //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.indexOf(" ") > 0) return false;
	if(left(object_value,1) == "0" && object_value.length >= 3) return 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 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 add_option(obj, tx, vl){
	l = obj.options.length;
	newOpt = new Option(tx, vl, false, false);
	obj.options[l]=newOpt;
	l++;
	obj.length = l;
}
function set_dropdown(obj,code){
	for(i=0;i<obj.length;i++){
		if(obj.options[i].value == code){
			obj.selectedIndex = i;
			break;
		}
	}
}
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;
}
function disable(obj){
	obj.disabled = true;
	obj.style.backgroundColor = "silver";

}
function enable(obj){
	obj.disabled = false;
	obj.style.backgroundColor = "";
}
function newwindow(url){
	newWindow = window.open(url, 'newWindow', 'width=600,height=500,toolbar=1,location=0,directories=0,status=0,menuBar=0, scrollBars=2,resizable=1');
	newWindow.focus();
}

