//////////////// PUBLIC
function AddCMA(){
   var sql="", sd="", sp="" , pd="", lb="", statusClosd = "", statusPend  = "";
      

   if (typeof(document.FormOne.pStatus    ) != "undefined") lb=document.FormOne.pStatus       ;  // status
   if (typeof(document.FormOne.cSoldDatMin) != "undefined") sd=AddDate(   "", "cSoldDat", "|");  // Sold Date
   if (typeof(document.FormOne.cSalePrMin ) != "undefined") sp=AddMinMax(sql, "cSalePr" , "|");  // Sale Price
   if (typeof(document.FormOne.cPndDateMin) != "undefined") pd=AddDate(   "", "cPndDate", "|");  // Pending Date

   // calculate sql for status, Excludes closed and pending statuses if closed/pending dates are entered.
   for(i=0; i< lb.length; i++) {
      if (lb[i].selected && lb[i].value=="CLOSD") statusClosd    = "pStatus='CLOSD' and";
      if (lb[i].selected && lb[i].value=="PEND")  statusPend     = "pStatus='PEND'  and";

      if (lb[i].selected && lb[i].value!="-ALL-" &&
         (lb[i].value!="CLOSD" || (lb[i].value=="CLOSD" && sd.length==0 && sp.length==0)) &&
         (lb[i].value!="PEND"  || (lb[i].value=="PEND"  && pd.length==0)) ) {

         if ( 0 < sql.length) sql += ",";
         sql += "'" + lb[i].value +"'";
         }
      }

   if (0 < sql.length) sql = "pStatus IN (" +sql+ ")";
 
   // add Sold Date, Pendding Date, Sales Price code if these variable are definded
   if      (0< sd.length && 0< sp.length) {
      sql = AndOr(sql, "|") + "(" + statusClosd + AndOr(sd, "&") + sp + ")";
      }
   else if (0< sd.length || 0< sp.length){
      sql = AndOr(sql, "|") + "(" + statusClosd + sd + sp + ")";
      }
   if (0< pd.length) sql = AndOr(sql, "|")  + "(" + statusPend + pd + ")";

   return AddPar(sql);
   }


function AddBool(sql, fieldName) {
   if      (eval("document.forms[0]." + fieldName +"[0].checked")) {
      // They do not care what the value is, so skip it
      return sql;
      }
   else if (eval("document.forms[0]." + fieldName +"[1].checked")){
      // Yes, must have
      return AndOr(sql, "&") +fieldName + "='Y'";
      }
   else if (eval("document.forms[0]." + fieldName +"[2].checked")){
      // No, must not have
      return AndOr(sql, "&") +fieldName + "='N'";
      }
   }

function AddDate(sql, fieldName, AndOrV) {
   var sqlN = new String();
   sqlN = AddOpDate(sqlN, fieldName+"Min", fieldName, ">=", "&");
   sqlN = AddOpDate(sqlN, fieldName+"Max", fieldName, "<=", "&");
   if (0<sqlN.length) {
      return AndOr(sql, AndOrV) + AddPar(sqlN);
      }
   else {
      return sql;
      }
   }

function AddEqual(sql, fieldName) {
   var formName  = "document.forms[0]." + fieldName +".value";
   var formValue = trimAll(eval(formName));

   if (formValue.length==0) {
      return sql;
      }

   return AndOr(sql, "&") + OrCommasEq(fieldName, formValue);
   }

function AddEqual_v1(sql, fieldName, AndOrV) {
   var sqlN = new String();
   sqlN = AddOp(sqlN, fieldName, fieldName, ">=", "&");
   if (0<sqlN.length) {
      return AndOr(sql, AndOrV) + AddPar(sqlN);
      }
   else {
      return sql;
      }
   }

function AddIn(sql, fieldName) {
   var menu = eval("document.forms[0]." + fieldName);
   var value = new String("");

    for (var i=0; i<menu.length; i++) {
      if (menu.options[i].selected) {
         if (menu.options[i].value =="-ALL-") {
             // ignore this field in the selection process
             return sql;
             }
         if (value.length!=0) {
            value = value + ", ";
            }
         value = value + "'"+ menu.options[i].value +"'";
         }
      }

   if (value.length==0) {
      // ignore this field in the selection process
      return sql;
      }

   return AndOr(sql, "&") + fieldName + " in (" + value + ")";
   }

function AddInTxt(sql, fieldName){
	var menu = eval("document.forms[0]." + fieldName);
    var value = new Array();
    var sqlStr = "";
    
    value = menu.value.split(',');
    
		for(q = 0; q < value.length; q++){
				
			if(sqlStr != ''){
				sqlStr += ',';
			}
				sqlStr += " '" + value[q] + "' ";
				
		}
	if(menu.value != ""){	
    	return AndOr(sql, "&") + fieldName + " in (" + sqlStr + ")";
	}
	else{
		return sql;
	}
	
	
}

function AddInNum(sql, fieldName) {
   var menu = eval("document.forms[0]." + fieldName);
   var value = new String("");

    for (var i=0; i<menu.length; i++) {
      if (menu.options[i].selected) {
         if (menu.options[i].value =="-ALL-") {
             // ignore this field in the selection process
             return sql;
             }
         if (value.length!=0) {
            value = value + ", ";
            }
         value = value + menu.options[i].value;
         }
      }


   if (value.length==0) {
      // ignore this field in the selection process
      return sql;
      }

   return AndOr(sql, "&") + fieldName + " in (" + value + ")";
   }


function AddLike(sql, fieldName) {
   var formName  = "document.forms[0]." + fieldName +".value";
   var formValue = trimAll(eval(formName));

   // Just return if there is nothing new to add.
   if (formValue.length==0) {
      return sql;
      }

   var Result = new String();
   var Positive = new String(); Positive = GetPositiveValues(formValue); 
   var Negative = new String(); Negative = GetNegativeValues(formValue); 

   Result = sql;
   if(Positive.length>0) Result = AndOr(Result, "&") + OrCommas(fieldName, Positive);
   if(Negative.length>0) Result = AndOr(Result, "!") + OrCommas(fieldName, Negative);

   return(Result);
   }

// This function differs from AddLike in that it DOES NOT treat
// commas as value delimiters. A comma is treated as any other
// character for the search.
function AddLikeLiteral(sql, fieldName) {
   var formName  = "document.forms[0]." + fieldName +".value";
   var formValue = trimAll(eval(formName));

   // Just return if there is nothing new to add.
   if (formValue.length==0) {
      return sql;
      }

   Result = sql;
   Result = AndOr(Result, "&") + fieldName + " LIKE '" + formValue + "%' ";

   return(Result);
   }

function AddMinMax(sql, fieldName, AndOrV) {
   var sqlN = new String();
   sqlN = AddOp(sqlN, fieldName+"Min", fieldName, ">=", "&");
   sqlN = AddOp(sqlN, fieldName+"Max", fieldName, "<=", "&");
   // This hack to allow pmlsnum to be a search field in CSS. Otherwise
   // we get an ambigous column error.
   sqlN = sqlN.toLowerCase();
   sqlN = sqlN.replace(/pmlsnum/g, "Tbl.pMlsNum");
   if (0<sqlN.length) {
      return AndOr(sql, AndOrV) + AddPar(sqlN);
      }
   else {
      return sql;
      }
   }

function AddMulti(sql, fieldName) {
   var menu = eval("document.forms[0]." + fieldName);
   var value = new String("");

   for (var i=0; i<menu.length; i++) {
      if (menu.options[i].selected) {
         if (menu.options[i].value =="-ALL-") {
             // ignore this field in the selection process
             return sql;
             }
         value = AndOr(value, "|") + fieldName + " like '%"+ menu.options[i].value +"%'";
         }
      }

   if (value.length==0) {
      // ignore this field in the selection process
      return sql;
      }

   return AndOr(sql, "&") + "(" + value + ")";
   }

function ListBox2Str(fieldName) {
   var menu = eval("document.forms[0]." + fieldName);
   var value = new String("");

    for (var i=0; i<menu.length; i++) {
      if (menu.options[i].selected) {
         if (menu.options[i].value =="-ALL-") {
             // ignore this field in the selection process
             return value;
             }
         if (value.length!=0) {
            value = value + ", ";
            }
         value = value + "'"+ menu.options[i].value +"'";
         }
      }

   return value
   }


////////////////////////////////// PRIVATE
function AndOr(sql, V) {
   if  (sql.length==0) {
      // do not change
      }
   else if (V=="&") {
      sql = sql + " AND ";
      }
   else if (V=="|") {
      sql = sql + " OR ";
      }
   else if (V=="!") {
      sql = sql + " AND NOT ";
      }
   else {
      alert("Error in AndOr v=*" + V + "*"); 
      }

   return sql;
   }

function AddPar(sql) {
   if (sql.length==0) {
      return sql;
      }
   return "(" + sql + ")";
   }

function GetPositiveValues(value) {
   sql = new String();
   L   = new List(value);

   while (0 < L.tail.length) {
      L.Parse();

      if(L.head.substring(0, 1)!="-") {
         if(sql.length!=0) {
            sql = sql + ", ";
            }
         sql =  sql + L.head;
         }      
      } // end of while 

   return(sql);
   }

function GetNegativeValues(value) {
   sql = new String();
   L   = new List(value);

   while (0 < L.tail.length) {
      L.Parse();

      if(L.head.substring(0, 1)=="-") {
         if(sql.length!=0) {
            sql = sql + ", ";
            }
         sql =  sql + L.head.substring(1); // Get the data without the negative sign
         }      
      } // end of while 

   return(sql);
   }

function AddExact(sql, fieldName, AndOrV) {
   var sqlN = new String();
   sqlN = AddOp(sqlN, fieldName, fieldName, "=", "&");
   if (0<sqlN.length) {
      return AndOr(sql, AndOrV) + AddPar(sqlN);
      }
   else {
      return sql;
      }
   }

function List(tail) {
   this.tail  = tail;
   this.head  = new String();
   this.Parse = Parse;
   }

function OrCommas(fieldName, value) {
   sql = new String();
   L   = new List(value);

   while (0 < L.tail.length) {
      L.Parse();
      sql = AndOr(sql, "|") + fieldName + " Like '" + L.head + "%' "
      }      

   // AddPar places parens around a statement
   return AddPar(sql);
   }

function OrCommasEq(fieldName, value) {
   sql = new String();
   L   = new List(value);

   while (0 < L.tail.length) {
      L.Parse();
      sql = AndOr(sql, "|") + fieldName + "='" + L.head + "' "
      }      

   return AddPar(sql);
   }

function trimL(str) {
   for (i=0; i<str.length && str.charAt(i)==' '; i++);
   return str.substring(i, str.length);
   }
function trimR(str) {
   for (i=str.length; 0<i && str.charAt(i-1)==' '; i--);
   return str.substring(0, i);
   }
function trimAll(str) {
    return trimL(trimR(str));
   }

function AddOp(sql, formName, fieldName, OP, V) {
   var value =  eval("document.forms[0]." +formName+ ".value" );

   if (value.length==0) {
      return sql;
      }

   return AndOr(sql, V) + fieldName+ OP + value;
   }

function AddOpDate(sql, formName, fieldName, OP, V) {
   var value =  eval("document.forms[0]." +formName+ ".value" );

   if (value.length==0) {
      return sql;
      }

//   return AndOr(sql, V) + fieldName+ OP + "convert(datetime,'" + value + "')";
   return AndOr(sql, V) + fieldName+ OP + "'" + value + "'";
   }

function Parse() {
   for (i=0; i< this.tail.length; i++) {
      if (this.tail.charAt(i)==",") {
         break;
         }
      }
   this.head =       this.tail.substring(0,i);
   this.tail = trimL(this.tail.substring(i+1, this.tail.length));
   return this.tail;
   }