/**
 * @author Mayank Rastogi
 */

// global query object
var query = new Query();
	
function Query(){
	
	this.searchKeyword = "";
	this.sortColumn = ACADEMY_NAME;
	this.sortDir = SORT_ASC;
	this.pageNumber;
	this.recordsPerPage = numberOfRecodsPerPage;
	this.params = new Array();
	this.term="";
	this.offset = null;

	this.filters = new Array();

	this.addModifier = function (key, value){
		
		var modifier = new Modifier();
		modifier.name = key;
		modifier.key = key;
		modifier.value =  value; 

		index = this.filters.length;
		this.filters [index] = modifier;
	
	}	
					
	this.removeAllModifiers = function()
	{
		this.filters = new Array();
		this.params = new Array();
	}	
	
	this.removeModifier = 	function(key, value){
	
		originalValue = value;
		value = '\"' + hexcode((unescape(value)).replace(/\"/g,"\\\""))+ '\"' ;

		var modifiers = this.filters;	
		
	for(index=0; index<modifiers.length;index++){
			if(modifiers[index].key == key && modifiers[index].value == value){
				this.filters.splice(index,1);	// remove the element and renumbered the array
			return;
			}else if(modifiers[index].key == key && modifiers[index].value == originalValue){
				this.filters.splice(index,1);	// remove the element and renumbered the array
				return;
		}
	}
}

	this.toJSONString = function()
	{
		var tmpQueryString=this.searchKeyword;
		this.searchKeyword = (this.searchKeyword).replace(/\"/g,"\\\"");
		this.searchKeyword=hexcode(this.searchKeyword);
		
		var originalModifiers = new Array();// this.filters;
		if((this.filters) && (this.filters.length > 0)){
			for(index = 0; index < this.filters.length; index++){
				 var modifier = new Modifier();
		                modifier.name = this.filters[index].name;
                		modifier.key = this.filters[index].key;
		                modifier.value =  this.filters[index].value;

				originalModifiers[index] = modifier;
				this.filters[index].value =  '\"' + hexcode((unescape(this.filters[index].value)).replace(/\"/g,"\\\""))+ '\"' ;
				
			}
		}
		
		var stringifyString=YAHOO.lang.JSON.stringify(this);
						
		this.searchKeyword=tmpQueryString;
		this.filters=originalModifiers;
		return ("{\"query\":" + stringifyString + "}");
	}
	
	this.toJSONStringNonEncoded = function()
	{
		var stringifyString=YAHOO.lang.JSON.stringify(this);
		return ("{\"query\":" + stringifyString + "}");
	}
	
}

function initializeQuery(){
	query = new Query();
	if(LOCATOR_TYPE=="membershipdirectory"){
		query.sortColumn = MD_LAST_NAME;
		mdLastNameDescSort = true;	    
		return;
	}	
	if(LOCATOR_TYPE=="sitewideSearch"){
		query.addModifier('cnamsswactiveflag','Y');
		return;
	}
	query.addModifier('cdclocatortype',LOCATOR_TYPE);
	
	//
	//Internal Locator Bharat
	if(LOCATOR_TYPE == "class" ){
		if(!isUserLoggedIn){	
			query.addModifier('cdclmcfirstname','N');
			query.addModifier('cdchidden','NO');
		}
	}

	academyDescSort = true;
}


function Modifier() {
	this.name="";
	this.key = "";
	this.value = "";
}

function AndModifier() {
	this.andModifiers = new Array();
}

function OrModifier() {
	this.orModifiers = new Array();
}
function NotModifier() {
	this.notModifiers = new Array();
}


