	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
	var mPath = sPath.substr(0, (sPath.length - sPage.length));

/////////////////////////////////////////ON PRESS ENTER
$(document).ready(function() {
	$('.inp').keypress(function (e) {
 			if (e.which == 13){	  sendForm(); }
	    });
});

////////////////////////////////////////ADD CRITERIA
function addCriteria(select) {
	$('td#criteriatd').show();	
	$('em#criteria').show();
	
	if (select.value != 'ALL'){
		
		var $ul = $('ul#'+select.name);
//alert(select.name);
	
		if ($ul.find('input[value='+select.value+']').length == 0){
			$ul.append('<li>' + 
							'<input onclick="remCriteria(this);" class="rem" type="button" name="'+select.name+'" value="'+select.value+'" /> ' +
						'</li>'); 
			
			changeSelect(select, '+');
			select.value = 'ALL';
		
		}
	}
}

////////////////////////////////////////CHANGE SELECT OPTION
function changeSelect(select, act){
		
		switch (select.name){
		
			case 'MAINCAT':				

			$('select#sCAT').children().remove().end();
			$('ul#CAT').children().remove().end();
			$('ul#SUBCAT').children().remove().end();
				
			var maincat = '';
			
			$('ul#MAINCAT').contents().find('input').each(function (i, item) {
				maincat += this.value+'-';
			  });
//alert(maincat);				
				
			$('select#sCAT').append('<option selected="selected" value="ALL">CATEGORY:</option>');
			
			$.getJSON(mPath+'ajaxrequest.php', {REQ: 'CAT', VAL: maincat}, function(data){
						$.each(data, function(i,item){
							$('select#sCAT').append('<option value="'+item+'">'+item+'</option>');

						});
				});

			if (act == '+'){
				$('select#sMAINCAT').find('option[value='+select.value+']').attr("disabled","disabled");
			}else {
				$('select#sMAINCAT').find('option[value='+select.value+']').removeAttr("disabled");
			}
			

			break;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////


			case 'CAT':
			
			$('select#sSUBCAT').children().remove().end();
			$('ul#SUBCAT').children().remove().end();		
		
			var cat = '';
			
			$('ul#CAT').contents().find('input').each(function (i, item) {
				cat += this.value+'-';
			  });
//alert(cat);				
				
			$('select#sSUBCAT').append('<option selected="selected" value="ALL">SUBCATEGORY:</option>');
			
			$.getJSON(mPath+'ajaxrequest.php', {REQ: 'SUBCAT', VAL: cat}, function(data){
						$.each(data, function(i,item){
							$('select#sSUBCAT').append('<option value="'+item+'">'+item+'</option>');
						});
				});
			
			if (act == '+'){
				$('select#sCAT').find('option[value='+select.value+']').attr("disabled","disabled");
			}else {
				$('select#sCAT').find('option[value='+select.value+']').removeAttr("disabled");
			}

			break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


			case 'SUBCAT':
			
			var subcat = '';
				
			$('ul#SUBCAT').contents().find('input').each(function (i, item) {
				subcat += this.value+'-';
			  });
//alert(subcat);

			$('input#iSUBCAT').attr("value", subcat);

			if (act == '+'){
				$('select#sSUBCAT').find('option[value='+select.value+']').attr("disabled","disabled");
			}else {
				$('select#sSUBCAT').find('option[value='+select.value+']').removeAttr("disabled");
			}
			
			break;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////			
			
			case 'PROV':
			
			var prov = '';
				
			$('ul#PROV').contents().find('input').each(function (i, item) {
				prov += this.value+'-';
			  });
//alert(prov);
			$('input#iPROV').attr("value", prov);

			if (act == '+'){
				$('select#sPROV').find('option[value='+select.value+']').attr("disabled","disabled");
			}else {
				$('select#sPROV').find('option[value='+select.value+']').removeAttr("disabled");
			}

			break;	
		}

		
	

}

////////////////////////////////////////SEND FORM
function sendForm(){
	
		var maincat = '';

		$('ul#MAINCAT').contents().find('input').each(function (i, item) {
			maincat += this.value+'-';
		  });
		$('input#iMAINCAT').attr("value", maincat);

		var cat = '';

		$('ul#CAT').contents().find('input').each(function (i, item) {
			cat += this.value+'-';
		  });
		$('input#iCAT').attr("value", cat);

		var subcat = '';

		$('ul#SUBCAT').contents().find('input').each(function (i, item) {
			subcat += this.value+'-';
		  });
		$('input#iSUBCAT').attr("value", subcat);

		var prov = '';

		$('ul#PROV').contents().find('input').each(function (i, item) {
			prov += this.value+'-';
		  });
		$('input#iPROV').attr("value", prov);
		
		$('input#iSt').attr("value", 0);
		
		if ($('input#sNAME').val() != 'NAME'){		$('input#iNAME').attr("value", $('input#sNAME').val());		}
		$('form#SEARCH').submit();
	
}

////////////////////////////////////////REMOVE CRITERIA
function remCriteria(select){

	$('select#s'+select.name).find('option[value='+select.value+']').removeAttr("disabled").end();
	$(select).parent().remove();
	changeSelect(select, '-');
}


////////////////////////////////////////CLEAR FORM
function resetForm() {
	
	$('td#criteria').hide();
	$('input#iNAME').attr("value","");
	$('input#iPROV').attr("value","");	
	$('input#iMAINCAT').attr("value","");
	$('input#iCAT').attr("value","");
	$('input#iSUBCAT').attr("value","");
	
	$('select#sNAME').attr("value","");


	$('select#sPROV').children().remove().end();
	$('select#sPROV').append('<option selected="selected" value="ALL">CITY:</option>');
	$.getJSON(mPath+'ajaxrequest.php', {REQ: 'PROV'}, function(data){
			$.each(data, function(i,item){
					$('select#sPROV').append('<option value="'+item+'">'+item+'</option>');
				});
		});
	$('select#sPROV').attr("value","ALL");


	$('select#sMAINCAT').children().remove().end();
	$('select#sMAINCAT').append('<option selected="selected" value="ALL">CLASS:</option>');
	$.getJSON(mPath+'ajaxrequest.php', {REQ: 'MAINCAT'}, function(data){
			$.each(data, function(i,item){
					$('select#sMAINCAT').append('<option value="'+item+'">'+item+'</option>');
				});
		});
	$('select#sMAINCAT').attr("value","ALL");


	$('select#sCAT').children().remove().end();
	$('select#sCAT').append('<option selected="selected" value="ALL">CATEGORY:</option>');
	$.getJSON(mPath+'ajaxrequest.php', {REQ: 'CAT'}, function(data){
			$.each(data, function(i,item){
					$('select#sCAT').append('<option value="'+item+'">'+item+'</option>');
				});
		});
	$('select#sCAT').attr("value","ALL");


	$('select#sSUBCAT').children().remove().end();
	$('select#sSUBCAT').append('<option selected="selected" value="ALL">SUBCATEGORY</option>');	
	$.getJSON(mPath+'ajaxrequest.php', {REQ: 'SUBCAT'}, function(data){
			$.each(data, function(i,item){
					$('select#sSUBCAT').append('<option value="'+item+'">'+item+'</option>');
				});
		});
	$('select#sSUBCAT').attr("value","ALL");


	$('ul#PROV').children().remove().end();		
	$('ul#MAINCAT').children().remove().end();		
	$('ul#CAT').children().remove().end();		
	$('ul#SUBCAT').children().remove().end();	
	$('ul#NAME').children().remove().end();		
}
