var $j = jQuery.noConflict();
 /* CR 277 STARTS */   
/*$j(document).ready(function(){
$j("#autoCompleteCreateConsumerZip").autocomplete(HOST_URL + BASE_PATH + "/Users/autoCompleteCreateConsumerZip",
{
minChars: 1,
cacheLength: 10,
onItemSelect: selectItem,
onFindValue: findValue,
formatItem: formatItem,
 autoFill: false
 });
}); */
$j(document).ready(function(){
	$j("#autoCompleteCreateCountryList").autocomplete(HOST_URL + BASE_PATH + "/Users/autoCompleteCreateCountryList",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
	 	autoFill: false
	 });
	
	$j("#autoCompleteProductBusinessName").autocomplete(HOST_URL + BASE_PATH + "/Products/autoCompleteProductBusinessName",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
	    autoFill: false
	});
	
	$j("#autoCompleteCreateCityList").autocomplete(HOST_URL + BASE_PATH + "/Users/autoCompleteCreateCityList",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
	    autoFill: false
	 });
	
	$j("#autoCompleteCreateProvinceList").autocomplete(HOST_URL + BASE_PATH + "/Users/autoCompleteCreateProvinceList",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	 });
	
	$j("#autoCompleteSearch").autocomplete(HOST_URL + BASE_PATH + "/Users/autoCompleteSearch",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	 });
	
	$j("#autoCompleteCity").autocomplete(HOST_URL + BASE_PATH + "/Announcements/autoCompleteCity",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteState").autocomplete(HOST_URL + BASE_PATH + "/Announcements/autoCompleteState",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteProductSearch").autocomplete(HOST_URL + BASE_PATH + "/Products/autoCompleteProductSearch",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteProductSearchMy").autocomplete(HOST_URL + BASE_PATH + "/Products/autoCompleteProductSearchMy",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteProductCategorySearch").autocomplete(HOST_URL + BASE_PATH + "/Products/autoCompleteProductCategorySearch",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteClassifiedCategory").autocomplete(HOST_URL + BASE_PATH + "/classifieds/autoCompletecategory",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteCategory").autocomplete(HOST_URL + BASE_PATH + "/products/autoCompletecategory",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteHobbie").autocomplete(HOST_URL + BASE_PATH + "/consumer_profiles/autoComplete",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoComplete").autocomplete(HOST_URL + BASE_PATH + "/consumer_educations/autoComplete",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteEdit").autocomplete(HOST_URL + BASE_PATH + "/consumer_educations/autoComplete",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteEventCity").autocomplete(HOST_URL + BASE_PATH + "/users/autoCompleteGuestViewCity/",
	{
		 minChars: 1,
		 cacheLength: 10,
		 onItemSelect: selectItem,
		 onFindValue: findValue,
		 formatItem: formatItem,
		 autoFill: false,
		 countryField: 'EventCountry'
	 });
	 
	$j("#autoCompleteEventViewCity").autocomplete(HOST_URL + BASE_PATH + "/users/autoCompleteViewCityForEvent/",
	{
		 minChars: 1,
		 cacheLength: 10,
		 onItemSelect: selectItem,
		 onFindValue: findValue,
		 formatItem: formatItem,
		 autoFill: false,
		 countryField: 'autoCompleteEventViewCountry'
		
	 });
	 
	
	/*$j("#autoCompleteBusinessname").autocomplete(HOST_URL + BASE_PATH + "/events/autoCompleteBusinessnameSearch/",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});*/
	$j("#autoCompleteBusinessname").autocomplete(HOST_URL + BASE_PATH + "/events/autoCompleteBusinessnameSearchaccordingtoprovince/",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteBusinessCategory").autocomplete(HOST_URL + BASE_PATH + "/business_profiles/autoCompleteCategory",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteQuestionCategory").autocomplete(HOST_URL + BASE_PATH + "/questions/autoCompleteQuestionCategory",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,	
		autoFill: false
	});

	$j("#ProductBrand").autocomplete(HOST_URL + BASE_PATH + "/products/autoCompleteBrand",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#product_brand").autocomplete(HOST_URL + BASE_PATH + "/products/autoCompleteBrand",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoConsumerExperienceCompanyname").autocomplete(HOST_URL + BASE_PATH + "/consumer_experiences/autoCompleteCompanyName",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
	$j("#autoCompleteGuestViewCity").autocomplete(HOST_URL + BASE_PATH + "users/autoCompleteGuestViewCity",
	{
		minChars: 1,
		cacheLength: 10,
		cache:true,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false,
		maxItemsToShow:10
	});
	
	$j("#autoCompleteGuestViewCountry").autocomplete(HOST_URL + BASE_PATH + "users/autoCompleteGuestViewCountry",
	{
		minChars: 1,
		cacheLength: 10,
		cache:true,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false,
		maxItemsToShow:10
	});
	
	$j("#ProductProductName").autocomplete(HOST_URL + BASE_PATH + "/Products/autoCompleteProductSearch",
	{
		minChars: 1,
		cacheLength: 10,
		onItemSelect: selectItem,
		onFindValue: findValue,
		formatItem: formatItem,
		autoFill: false
	});
	
});


function fillUpBusinessCataegory() {
	
	$j("#businessProductCategorySelection").show();
	temp_id = document.getElementById("business_profile_id").value;	
	
	if(temp_id) {
		$j.get(HOST_URL+site_root+"/Products/autoCompleteProductCategorySearch?q=&selSearch=business&bProfileId="+temp_id, function(data) {
			$j("#autoCompleteProductCategorySearch").hide();
			$j("#categoryLinkSpan").hide();
			
		//	var select=document.getElementById("businessProductCategorySelection");
			var x=document.getElementById("businessProductCategorySelection");
			var options=x.getElementsByTagName("option");
			optionLength = options.length;
//				console.log(optionLength);
			for (i= optionLength -1 ; i >= 0; i--) {
//					console.log(i);
				x.removeChild(options[i]);
			}
			
			if (!data) return null;
			
			
			var option=document.createElement("option");
			option.innerHTML='All categories';
			option.value='0';	
			try {					 
				  x.add(option,x.options[null]);  // for IE earlier than version 8
			  } catch (e) {
				  x.add(option,null);
			 }
			  
			 var rows = data.split("\n"); 
			for (var i=0; i < rows.length; i++) {
				var row = $j.trim(rows[i]);
				if (row) {
					var option=document.createElement("option");
					tempData = row.split("|");					
					if (!tempData) continue;
						option.innerHTML=tempData[0];
						var valArr = tempData[1].split('_');		
						option.value=valArr[1];						
					try {					 
						  x.add(option,x.options[null]);  // for IE earlier than version 8
					  } catch (e) {
						  x.add(option,null);
					  }
											
				}
			}

			
		});
	}
}

/* For Guest home page City & Country Search */

function selectItem(li) {	
	findValue(li);
}

function findValue(li) {		
	if( li == null ) return alert("No match!");
	
	if( !!li.extra ) {
		var sValue = li.extra[0];		
		//alert(sValue);
		var valArr = sValue.split('_');		
		//alert(valArr[0]);
		if (valArr[0] == 'CARTRULE') {
			$j('#ProductBrand').val('');	
			$j('#autoCompleteProductCategorySearch').val('');	
			
			$j('#business_profile_id').val(valArr[1]);
			$j('#criteria_str').val(sValue);
			fillUpBusinessCataegory();			
			
			if ( valArr[2] && valArr[2] != '' ) {
				if (parseInt(valArr[2]))
					$j('#is_online_payment').attr('checked',true);	
				else	
					$j('#is_online_payment').attr('checked',false);	
				$j('#is_online_payment').attr('disabled',true);		
					
				if (parseInt(valArr[3])) 
					$j('#is_onsite_payment').attr('checked',true);	
				else	
					$j('#is_onsite_payment').attr('checked',false);						
				$j('#is_onsite_payment').attr('disabled',true);		
				
				if (parseInt(valArr[4]))
					$j('#allow_pickup').attr('checked',true);	
				else	
					$j('#allow_pickup').attr('checked',false);			
				$j('#allow_pickup').attr('disabled',true);
					
				if (parseInt(valArr[5]))
					$j('#offer_shipping').attr('checked',true);	
				else	
					$j('#offer_shipping').attr('checked',false);				
				$j('#offer_shipping').attr('disabled',true);	
					
				if (valArr[5] == 'MMO')
					$j('#offer_by_mmo').attr('checked',true);	
				else	
					$j('#offer_by_mmo').attr('checked',false);
				$j('#offer_by_mmo').attr('disabled',true);	
				$j('#all_shipping').attr('disabled',true);	
				$j('#all_payment').attr('disabled',true);	
				
			} else {
				$j('#is_online_payment').attr('disabled',false);
				$j('#is_onsite_payment').attr('disabled',false);
				$j('#allow_pickup').attr('disabled',false);
				$j('#offer_shipping').attr('disabled',false);
				$j('#offer_by_mmo').attr('disabled',false);		
				
				$j('#is_online_payment').attr('checked',false);
				$j('#is_onsite_payment').attr('checked',false);
				$j('#allow_pickup').attr('checked',false);
				$j('#offer_shipping').attr('checked',false);
				$j('#offer_by_mmo').attr('checked',false);		
				
				$j('#offer_by_mmo').attr('disabled',false);	
				$j('#all_shipping').attr('disabled',false);	
				$j('#all_payment').attr('disabled',false);	
			}
			
			$j("#all_item_is").attr('disabled',true);
			var new_old_val = li.extra[1];
			
			$j('#item_is_new').attr('disabled',true);					
			if(new_old_val.indexOf('New') != '-1') {
				$j('#item_is_new').attr('checked',true);
			}else {
				$j('#item_is_new').attr('checked',false);
			}
		
			$j('#item_is_used').attr('disabled',true);	
			if(new_old_val.indexOf('Used') != '-1') {
				$j('#item_is_used').attr('checked',true);
			}else {
				$j('#item_is_used').attr('checked',false);
			}
			
			var is_free = li.extra[2];
			$j('#is_free').attr('disabled',true);	
			if(new_old_val.indexOf('1') != '-1') {
				$j('#is_free').attr('checked',true);
			}else {
				$j('#is_free').attr('checked',false);
			}
					
		} else if (valArr[0] == 'PRODCAT')   {
			if ($j('#prod_cat_id')) {
				$j('#prod_cat_id').val(valArr[1]);
			}
		}
		//otherwise, let's just display the value in the text box
	} else 	{
	   var sValue = li.selectValue;
	   //alert("The value you selected was: " + sValue);
	   
	   // Events calendar version 2.2
	   if(document.getElementById('autoCompleteBusinessname'))
		  document.getElementById('autoCompleteBusinessname')['onblur']();
	   // End of version 2.2
	}
}

function formatItem(row) {
	//alert('d');
	if(row[1] == undefined) {
		return row[0];
	}
	else {		
		//return row[0] + " (id: " + row[1] + ")";
		return row[0];
	}
}

function fillEmailContactsValue(li,id_no){
		
	if( li == null ) return alert("No match!");
	
	if( !!li.extra ) {
		
	email = $j("#email_"+id_no).val();	
	
	if(email != '') {	
		if(echeck(email)) {
			$j("#msgDiv_"+id_no).hide();		
			$j("#msgDiv_"+id_no).html('');
		}
	}
			
			
		var sValue = li.extra[0];		
		
		var valArr = sValue.split('##');		
		
		var contactId= valArr[0];
		var contactUserId= valArr[1];
		var contactName = $j.trim(valArr[2]);
		var contactPhone = $j.trim(valArr[3]);
		var contactEmail = $j.trim(valArr[4]);
		
		$j('#email_'+id_no).val(contactEmail);
		
		if (contactName != '') {			
			var nameArr = contactName.split(' ');
		
			var firstname;
			var lastname;

			if(nameArr.length>1){
				firstname = nameArr[0];
				lastname = $j.trim(contactName.substring(firstname.length));
			} else {
				var firstname = contactName;
			}
			
			$j('#firstname_'+id_no).val(firstname);
			$j('#firstname_'+id_no).removeClass('text-default-label');
			
			if(lastname != '') {
				$j('#lastname_'+id_no).val(lastname);
				$j('#lastname_'+id_no).removeClass('text-default-label');
			} else {
				$j('#lastname_'+id_no).val('');
			}
		}
		if(contactPhone != '') {
			$j('#phone_'+id_no).val(contactPhone);
			$j('#phone_'+id_no).removeClass('text-default-label');
		} else {
			$j('#phone_'+id_no).val('');
		}
		//otherwise, let's just display the value in the text box
	} else 	{
	   var sValue = li.selectValue;
	   if(sValue == ''  || sValue == 'No results') {
		   $j('#email_'+id_no).val('');
		   $j('#firstname_'+id_no).val('');
		   $j('#lastname_'+id_no).val('');
		   $j('#phone_'+id_no).val('');
	   }
	}
}
