
function QueryOperator(displayName, value, inputValueCount, express) {
	this.displayName = displayName;
	this.value = value;
	this.inputValueCount = inputValueCount;
	this.express = express;
}

function compareFunc(param1,param2){
	 if(parseInt(param1) > parseInt(param2)) return 1;
	 if(parseInt(param1) == parseInt(param2)) return 0;
	 if(parseInt(param1) < parseInt(param2)) return -1;
}

function getQueryOperators(propertyTpye) {
	var Operators = new OperatorConstannt();
	var rets;
	switch (propertyTpye) {
	  case "0":
	  case "3":
	  case "4":
	  case "5":
	  case "10":
	  //string type
		rets = new Array(Operators.IS_NULL, Operators.IS_NOT_NULL, Operators.STRING_EQUAL, Operators.INCLUDE, Operators.NOT_INCLUDE, Operators.START_WITH, Operators.END_WITH);
		break;
	  case "1":
	  //num type
		rets = new Array(Operators.IS_NULL, Operators.IS_NOT_NULL, Operators.LESSTHAN, Operators.GREATERTHAN, Operators.BETWEEN, Operators.EQUAL, Operators.NOT_EQUAL, Operators.EQUAL_OR_GREATERTHAN, Operators.EQUAL_OR_LESSTHAN);
		break;
	  case "2":
	  //date type
		rets = new Array(Operators.IS_NULL, Operators.IS_NOT_NULL, Operators.BETWEEN);
		break;
	 //enum type
	  case "6":
	  	rets = new Array(Operators.IS_NULL, Operators.IS_NOT_NULL, Operators.EQUAL);
		break;
	//davey add
	  case "9":
	    rets = new Array(Operators.IN);
	    break;

	}
	return rets;
}

QueryObject = function(className,locale,properties,operatorSelect,inputValue,paramName){
	
	this.getOperatorValue = function(){
		if(operatorSelect.value!=''){
			var operator= operatorSelect.value.split(":");
			return operator[0];
		}
	};
	this.getInputValueCount = function(){
		if(operatorSelect.value!=''){
			var operator= operatorSelect.value.split(":");
			return operator[1];
		}
	};
	this.getExpress = function(){
		if(operatorSelect.value!=''){
			var operator= operatorSelect.value.split(":");
			return operator[2];
		}
	};

	this.getPropertyName= function(){
		if(properties.value!=""){
			var property= properties.value.split(':');
			return property[0];
		}
		return '';
	};
	this.getPropertyType = function(){
		if(properties.value!=""){
			var property= properties.value.split(':');
			return property[1];
		}
		return '';
	};
	this.getExtendInfoId = function(){
		if(properties.value!=""){
			var property= properties.value.split(':');
			return property[2];
		}
		return '';
	}

	this.setOperator = function (){
			inputValue.innerHTML="";
			operatorSelect.options.length=0;
					
			var operators = getQueryOperators(this.getPropertyType());
			for(var i=0;i<operators.length;i++){
				var op = operators[i];
				operatorSelect.options.add(new Option(op.displayName,op.value+":"+op.inputValueCount+":"+op.express));
			}
	};
	
	this.setInputValue = function (){
		
		//clear
		inputValue.innerHTML="";
		
		switch (this.getPropertyType()){
			//type is string
			case "0":
			case "3":
			case "4":
			case "5":
			case "10":
				if(this.getInputValueCount()=="1"){
					inputValue.innerHTML="<input id='userInputValue"+paramName+"'/>";
				}
				break;
			//type is num
			case "1":
				if(this.getInputValueCount()=="2"){
					inputValue.innerHTML="<input id='userInputValue1"+paramName+"'/>&nbsp;<input id='userInputValue2"+paramName+"'/>";
				}else if(this.getInputValueCount()=="1"){
					inputValue.innerHTML="<input id='userInputValue"+paramName+"'/>";
				}
				break;
			case "2":
				if(this.getInputValueCount()=="2"){
					inputValue.innerHTML="<input id='userInputValue1"+paramName+"' type='text' class='input_calendar' readonly='readonly' onclick=\"displayCalendar('',$('userInputValue1"+paramName+"'),'yyyy-mm-dd',this)\"/>&nbsp;<input id='userInputValue2"+paramName+"' type='text' readonly='readonly' class='input_calendar' onclick=\"displayCalendar('',$('userInputValue2"+paramName+"'),'yyyy-mm-dd',this)\"/>";
				}else if(this.getInputValueCount()=="1"){
					inputValue.innerHTML="<input id='userInputValue"+paramName+"' type='text' class='input_calendar' readonly='readonly' onclick=\"displayCalendar('',$('userInputValue"+paramName+"'),'yyyy-mm-dd',this)\" />";
				}
				inputValue.innerHTML += "<style>#calendarDiv{z-index:999999!important;}</style>";
				break;
			case "6":
				if(isNaN(parseInt(this.getExtendInfoId()))){
				// alert("propertyName:"+propertyName+";extendInfoId:"+extendInfoId);
				 ajaxInvokeForDWRFacade.getEnumValues(this.getExtendInfoId(),this.getPropertyName(),locale,this.setEnumValues);
				}else {
					extendInfoService.getEnumValues(this.getExtendInfoId(),locale,this.setEnumValues);
				}	
				break;	
			case "9":
			    eval( this.getExtendInfoId()+"();" );
			    break;
		}
	};
	
	this.setEnumValues = function (enumValues){
		var operator= operatorSelect.value.split(":");
		var inputValueCount = operator[1];
		if(inputValueCount>0){
			inputValue.innerHTML="<select id='userInputValue"+paramName+"'>"
			var opSelect = $('userInputValue'+paramName).options;
			var keyArray = new Array();
			var i=0;
			for(idKey in enumValues){
				keyArray[i]=idKey;
				i++;
			}
			keyArray.sort(compareFunc);
			for(j = 0 ; j<i ; j++ ){
				//alert("j is "+ j+";enumValues[keyArray[j]] is "+enumValues[keyArray[j]]+";keyArray[j] is "+keyArray[j]);
				tmpOption = new Option(enumValues[keyArray[j]],keyArray[j]);
				if(j==0){
					tmpOption.selected=true;
				}
				opSelect.add(tmpOption);
			}
		}
	};
	
	this.hasInputValue = function(){
		try{
			if(this.getInputValueCount()=="1"){
				 var value = $('userInputValue'+paramName).value.trim();
				 if( value == ""||(this.getPropertyType()==6&&value==0) ){
				 	return false;
				 }
			} else if(this.getInputValueCount()=="2"){
				var value1 = $('userInputValue1'+paramName).value.trim();
				var value2 = $('userInputValue2'+paramName).value.trim();
				if( value1 == "" || value2 == ""){
					return false;
				}
			}
		}catch(e){
			return false;
		}
		return true;
	};
	
	this.clearInputValue = function(){
		try{
			if(this.getInputValueCount()=="1"){
				$('userInputValue'+paramName).value = "";
			} else if(this.getInputValueCount()=="2"){
				$('userInputValue1'+paramName).value = "";
				$('userInputValue2'+paramName).value = "";
			}
		}catch(e){
			return false;
		}
	}
	
	this.getExpression = function( aliasName ){
		if (aliasName != undefined ){
	    className = aliasName;
	    }
	    var exp = this.getExpress();
	    if(exp==undefined)
	    {
	    	exp="";
	    }
		exp=exp.replace("$property",className+'.'+this.getPropertyName());
		switch (this.getPropertyType()){
			//type is string enum
			case "0":
			case "3":
			case "4":
			case "5":
			case "6":
			case "9":
			case "10":
				if(this.getInputValueCount()=="1"){
					exp=exp.replace("$paramter",trim($('userInputValue'+paramName).value.replace(/\\/g, "\\\\\\\\").replace(/\'/g, "").replace(/%/g, "\\%").replace(/_/g, "\\_"))) ;
				}
				break;
			//type is num date
			case "1":
			case "2":
				
				if(this.getInputValueCount()=="2"){
				var value1,value2;
					if(this.getPropertyType()=="2"){
						value1 ="'"+trim($('userInputValue1'+paramName).value.replace(/\\/g, "\\\\\\\\").replace(/\'/g, "").replace(/%/g, "\\%").replace(/_/g, "\\_"))+" 00:00:00'";
						value2 ="'"+trim($('userInputValue2'+paramName).value.replace(/\\/g, "\\\\\\\\").replace(/\'/g, "").replace(/%/g, "\\%").replace(/_/g, "\\_"))+" 23:59:59'";
					}else{
						value1 =trim($('userInputValue1'+paramName).value);
						value2 =trim($('userInputValue2'+paramName).value);
					}
					exp=exp.replace("$paramter1",value1) ;
					exp=exp.replace("$paramter2",value2) ;
				}else if(this.getInputValueCount()=="1"){
					var value;
					if(this.getPropertyType()=="2"){
						value ="'"+trim($('userInputValue'+paramName).value.replace(/\\/g, "\\\\\\\\").replace(/\'/g, "").replace(/%/g, "\\%").replace(/_/g, "\\_"))+"'";
					}else{
						value =trim($('userInputValue'+paramName).value.replace(/\\/g, "\\\\\\\\").replace(/\'/g, "").replace(/%/g, "\\%").replace(/_/g, "\\_"));
					}
					exp=exp.replace("$paramter",value) ;
				}
				break;
		}
		return exp;
	};
	
	this.addExpression = function (expressTextarea, aliasName){

		if(this.hasInputValue() === false){
			alertMessage();
			return;
		}
	    var exp = this.getExpression(aliasName);
		if(expressTextarea.value.length==0){
			expressTextarea.value = exp;
		}else if(exp==""){
			expressTextarea.value += "";
		}
		else{
			expressTextarea.value += " and "+exp;
		}
	};
}


