﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="Main.js" />
var g_iCurDirectoryID = 0;
var subcategoryIds = [];
var selectedRefiningValuesIds = [];
var currentSubcategoryId = null;
var loadedRefiningIds = [];
var g_UploadDefaultImage_DirectoryPhotos = '';
var g_Upload_DirectoryLogo = '';
var g_iDirectorySelCityID;
var g_iDirectorySelSubCategoryID;

var dirEditMode = false;

var currentCityComboText = 0;
var currentCountryComboText = 0;

function changeDirectoryStatus(iDirectoryToChangeStatusID) {
	g_iDirectoryToChangeStatusID = iDirectoryToChangeStatusID;
	Directory.ChangeDirectoryStatus(g_iDirectoryToChangeStatusID, onChangeDirectoryStatusComplete, onAjaxError, onAjaxTimeOut);
}

function onChangeDirectoryStatusComplete(result) {
	if ($get('divDirectoryName_' + g_iDirectoryToChangeStatusID).className == "NameItemRow") {
		replaceStyle('divDirectoryName_' + g_iDirectoryToChangeStatusID, 'NameItemRowUnSelected');
		$get('aDirectoryStatus_' + g_iDirectoryToChangeStatusID).innerHTML = 'Activate';
	}
	else {
		replaceStyle('divDirectoryName_' + g_iDirectoryToChangeStatusID, 'NameItemRow');
		$get('aDirectoryStatus_' + g_iDirectoryToChangeStatusID).innerHTML = 'Deactivate';
	}
}

function LoadDirectoryData(iCurDirectoryID) {
	g_iCurDirectoryID = iCurDirectoryID;
	Directory.GetDirectoryData(g_iCurDirectoryID, onGetDirectoryDataComplete, onAjaxError, onAjaxTimeOut);
	Directory.GetItemPhotosForEdit(g_iCurDirectoryID, onGetDirectoryPhotosForEditComplete, onAjaxError, onAjaxTimeOut);
}

function onGetDirectoryDataComplete(result) {
	$get('txtDirectory_Name').value = result[1];
	ComboSetSelectedValue($get(g_DirectoryCategoryComboID), result[14]);
	g_iDirectorySelSubCategoryID = result[2];

	//added by hassan
	dirEditMode = true;
	


	Directory.GetDirectorySubCategories(result[14], onDirectorySubCategoriesComplete, onAjaxError, onAjaxTimeOut);
	$get('txtDirectory_URL').value = result[3];
	$get('txtDirectory_Description').value = result[4];
	$get('txtDirectory_ContactEmail').value = result[5];
	if (result[6].length > 0) {
		$get('divDirectoryLogo').style.display = 'block';
		$get('imgDirectoryLogo').src = 'Images/Directory_Logo/' + result[6];
		g_Upload_DirectoryLogo = result[6];
	}
	else {
		$get('divDirectoryLogo').style.display = 'none';
		g_Upload_DirectoryLogo = '';
	}
	$get('txtDirectory_Address1').value = result[7];
	$get('txtDirectory_Address2').value = result[8];
	ComboSetSelectedValue($get(g_DirectoryCountryComboID), result[10]);
	g_iDirectorySelCityID = result[9];
	Classified.GetAdCities(result[10], onGetCitiesComplete, onAjaxError, onAjaxTimeOut);
	$get('txtDirectory_PostalCode').value = result[11];
	$get('txtDirectory_PhoneNumber').value = result[12];
	$get('txtDirectory_FaxNumber').value = result[13];

	getRefiningGroups(g_iDirectorySelSubCategoryID);
}

function onGetDirectoryPhotosForEditComplete(result) {
	$get('Upload_Thumbnails_DirectoryPhotos').innerHTML = '<img id="Upload_LoadingImage_DirectoryPhotos" src="/img/ajax-loader.gif" style="margin-left: 410px; display:none;"/>'; ;
	g_UploadDefaultImage_DirectoryPhotos = result[result.length - 1];
	for (var i = 0; i < result.length - 1; i++) {
		var src = "/Images/directory/" + result[i];
		var imgDiv = document.createElement('div');
		imgDiv.innerHTML = $get('UploadImageTemplate_DirectoryPhotos').innerHTML;
		imgDiv.id = 'UploadImageTemplate_DirectoryPhotos_' + src.replace('/Images/directory/', '');
		imgDiv.className = 'PhotoUpLoadDiv';
		var new_img = $get('UploadImage', imgDiv);
		new_img.name = 'UploadPhotos_DirectoryPhotos';
		new_img.id = src.replace('/Images/directory/', '');
		if (new_img.id == g_UploadDefaultImage_DirectoryPhotos)
			$get('Upload_MakeDefaultLink', imgDiv).innerHTML = '';
		else
			$get('Upload_MakeDefaultLink', imgDiv).innerHTML = 'Make it default';
		var new_SetDefaultLink = $get('Upload_MakeDefaultLink', imgDiv);
		new_SetDefaultLink.href = 'javascript:SetAsDefaultImage("' + src.replace('/Images/directory/', '') + '", "DirectoryPhotos");';
		new_SetDefaultLink.name = 'setDefaultImageLink_DirectoryPhotos';
		new_SetDefaultLink.id = 'set_' + src.replace('/Images/directory/', '');

		$get('Upload_DeleteLink', imgDiv).href = 'javascript:DeleteImage("' + src.replace('/Images/directory/', '') + '", "DirectoryPhotos");';
		$get('Upload_Thumbnails_DirectoryPhotos').appendChild(imgDiv);
		imgDiv.style.display = '';
		new_img.src = src;
	}
}

function onDirectorySubCategoriesComplete(result) {
	$get(g_DirectorySubCategoryComboID).innerHTML = '';
	$get(g_DirectorySubCategoryComboID).options.length = result.length;
	for (var i = 0; i < result.length; i++) {
		var item = result[i].split(",");
		$get(g_DirectorySubCategoryComboID).options[i] = new Option(item[1], item[0]);
	}
	ComboSetSelectedValue($get(g_DirectorySubCategoryComboID), g_iDirectorySelSubCategoryID);
}

function onGetCitiesComplete(result) {
	$get(g_DirectoryCityComboID).innerHTML = '';
	$get(g_DirectoryCityComboID).options.length = result.length;
	for (var i = 0; i < result.length; i++) {
		var item = result[i].split(",");
		$get(g_DirectoryCityComboID).options[i] = new Option(item[1], item[0]);
	}
	ComboSetSelectedValue($get(g_DirectoryCityComboID), g_iDirectorySelCityID);
}

function DeleteDirectory(iDirectoryIDToDelete) {
	g_iDirectoryIDToDelete = iDirectoryIDToDelete;
	Directory.DeleteDirectory(g_iDirectoryIDToDelete, onDeleteDirectoryComplete, onAjaxError, onAjaxTimeOut);
}

function onDeleteDirectoryComplete(result) {
	LoadMyDirectories();
}

function Directory_Validate() {
	if (!validateEmpty('txtDirectory_Name')) {
		showMessage('please insert name', 1);
		return false;
	}
	if (ComboGetSelectedValue($get(g_DirectorySubCategoryComboID)) == -1) {
		showMessage('please select sub category', 1);
		return false;
	}
	if (!validateEmpty('txtDirectory_Address1')) {
		showMessage('please insert address', 1);
		return false;
	}
	if (ComboGetSelectedValue($get(g_DirectoryCountryComboID)) == -1) {
		showMessage('please select country', 1);
		return false;
	}
	if (!validateEmpty('txtDirectory_PhoneNumber')) {
		showMessage('please insert phone number', 1);
		return false;
	}
	return true;
}
function SaveDirectory() {
	if (!Directory_Validate())
		return;

	var strName = $get('txtDirectory_Name').value;
	var iSubCategoryID = ComboGetSelectedValue($get(g_DirectorySubCategoryComboID));
	var strURL = $get('txtDirectory_URL').value;
	var strDescription = $get('txtDirectory_Description').value;
	var strContactEmail = $get('txtDirectory_ContactEmail').value;
	var strAddress1 = $get('txtDirectory_Address1').value;
	var strAddress2 = $get('txtDirectory_Address2').value;
	var iCityID = ComboGetSelectedValue($get(g_DirectoryCityComboID));
	var iCountryID = ComboGetSelectedValue($get(g_DirectoryCountryComboID));
	var strPostalCode = $get('txtDirectory_PostalCode').value;
	var strPhoneNumber = $get('txtDirectory_PhoneNumber').value;
	var strFaxNumber = $get('txtDirectory_FaxNumber').value;

	var arrstrPhotosIDs = new Array();
	var arrPhotos = document.getElementsByName('UploadPhotos_DirectoryPhotos');
	for (var i = 0; i < arrPhotos.length - 1; i++) {
		arrstrPhotosIDs.push(arrPhotos[i].id);
	}

	openProfileTab(4);

	//added by hassan
	var selectedRefiningValues = new Array();
	var refiningCombo;
	var selectedValue;
	for (var i = 0; i < currentRefiningGroupsIds.length; i++) {
		refiningCombo = $get("refiningGroup" + currentRefiningGroupsIds[i]);

		if (refiningCombo.selectedIndex > -1) {
			selectedValue = refiningCombo.options[refiningCombo.selectedIndex].value;
			if (selectedValue != undefined && selectedValue.length > 0)
				selectedRefiningValues.push(selectedValue);
		}
	}
	Directory.SaveDirectory(g_iCurDirectoryID, ID, strName, iSubCategoryID, strURL, strDescription, strContactEmail, strAddress1, strAddress2, iCityID, iCountryID, strPostalCode, strPhoneNumber, strFaxNumber, arrstrPhotosIDs, g_UploadDefaultImage_DirectoryPhotos, g_Upload_DirectoryLogo, selectedRefiningValues, onSaveDirectoryComplete, onAjaxError, onAjaxTimeOut);
}

function onSaveDirectoryComplete(result) {
	g_iCurDirectoryID = result;
	Directory_ClearFormValues();
	$get('Upload_Thumbnails_DirectoryPhotos').innerHTML = '';

	LoadMyDirectories();
	showMessage("Your directory was  saved successfully", 2);
}

function PrepareForAddNew_Directory() {
	g_iCurDirectoryID = 0;
	Directory_ClearFormValues();
	$get('Upload_Thumbnails_DirectoryPhotos').innerHTML = '<img id="Upload_LoadingImage_DirectoryPhotos" src="/img/ajax-loader.gif" style="margin-left: 410px; display:none;"/>';
}
function Directory_ClearFormValues() {
	$get('txtDirectory_Name').value = '';
	ComboSetSelectedValue($get(g_DirectoryCategoryComboID), -1);
	ComboSetSelectedValue($get(g_DirectorySubCategoryComboID), -1);
	$get('txtDirectory_URL').value = '';
	$get('txtDirectory_Description').value = '';
	$get('txtDirectory_ContactEmail').value = '';
	$get('txtDirectory_Address1').value = '';
	$get('txtDirectory_Address2').value = '';
	ComboSetSelectedValue($get(g_DirectoryCountryComboID), -1);
	ComboSetSelectedValue($get(g_DirectoryCityComboID), -1);
	$get('txtDirectory_PostalCode').value = '';
	$get('txtDirectory_PhoneNumber').value = '';
	$get('txtDirectory_FaxNumber').value = '';
	$get('divDirectoryLogo').style.display = 'none';
	g_Upload_DirectoryLogo = '';
	//add by hassan 
	removeRefiningGroups();

}


function LoadMyDirectories() {
	g_UploadDefaultImage_DirectoryPhotos = '';
	Directory.LoadMyDirectories(ID, onLoadMyDirectoriesComplete, onAjaxError, onAjaxTimeOut);
}

function onLoadMyDirectoriesComplete(result)
{
	if (result.length == 0)
		$j("#divNoMyDirectories_Inner").show();
	else
		$j("#divNoMyDirectories_Inner").hide();
		
	var divMyDirectoriesTemp = document.createElement('div');
	var divLine = null;
	for (var i = 0; i < result.length; i++) {
		divLine = null;
		var arrDataFields = result[i].split('#,#');
		if (arrDataFields.length == 2) {//this item is a category
			divLine = document.createElement('div');
			divLine.id = 'divSubcategoryContainer';
			divLine.className = 'LineClassified';
			divLine.innerHTML = $get('divSubcategoryContainer', $get('divMyDirectoriesTemplate')).innerHTML;
			$get('divSubcategory_Name', divLine).innerHTML = getHTMLEncode(arrDataFields[0]);
		}
		else if (arrDataFields.length == 5) {//this item is a directory
			var arrDirectoryFields = result[i].split('#,#');
			divLine = document.createElement('div');
			divLine.id = 'divDirectoriesContainer';
			divLine.className = 'ContainerItem';
			var strDirectoriesContainerHTML = $get('divDirectoriesContainer', $get('divMyDirectoriesTemplate')).innerHTML;
			strDirectoriesContainerHTML = strDirectoriesContainerHTML.replace(/\$\$DirectoryId\$\$/g, arrDataFields[0]);
			strDirectoriesContainerHTML = strDirectoriesContainerHTML.replace(/\$\$Title\$\$/g, getHTMLEncode(arrDataFields[1]));
			strDirectoriesContainerHTML = strDirectoriesContainerHTML.replace(/\$\$NumOfViews\$\$/g, arrDataFields[2]);
			strDirectoriesContainerHTML = strDirectoriesContainerHTML.replace(/\$\$NumOfResponses\$\$/g, arrDataFields[3]);
			var strStatus = 'Deactivate';
			var strStatusClass = 'NameItemRow';
			if (arrDataFields[4] == 'False') {
				strStatus = 'Activate';
				strStatusClass = 'NameItemRowUnSelected';
			}
			strDirectoriesContainerHTML = strDirectoriesContainerHTML.replace(/\$\$Status\$\$/g, strStatus);
			strDirectoriesContainerHTML = strDirectoriesContainerHTML.replace(/\$\$StatusClass\$\$/g, strStatusClass);

			divLine.innerHTML = strDirectoriesContainerHTML;
		}
		divMyDirectoriesTemp.appendChild(divLine);
	}
	$get('divMyDirectories').innerHTML = divMyDirectoriesTemp.innerHTML;
}



//---------------------hassan

var numberOfDirectory = 0;
function showHideSubcategory(id) {
	if ($get("SubcategoryArrow_" + id).className == 'open' || $get("SubcategoryArrow_" + id).className == 'close') {
		if ($get("SubcategoryArrow_" + id).className == 'open') {
			replaceStyle("SubcategoryArrow_" + id, 'close');
			currentSubcategoryId = null;
		}
		else {
			replaceStyle("SubcategoryArrow_" + id, 'open');
			if (currentSubcategoryId != null) {
				if ($get("SubcategoryArrow_" + currentSubcategoryId).className == 'emptyActive' || $get("SubcategoryArrow_" + currentSubcategoryId).className == 'empty') {
					replaceStyle("SubcategoryArrow_" + currentSubcategoryId, 'empty');
				}
				else if ($get("SubcategoryArrow_" + currentSubcategoryId).className == 'open' || $get("SubcategoryArrow_" + currentSubcategoryId).className == 'close') {
					replaceStyle("SubcategoryArrow_" + currentSubcategoryId, 'close');
					Effect.toggle($get("SubcategoryRefining_" + currentSubcategoryId), 'BLIND');
				}
			}
			currentSubcategoryId = id;

			//Load subcaregory Directory Items
			//alert("subcat = " + id);
			fillRefiningFilters();
			pageNumber = 0;
			showLoadProgress();
			Directory.GetNumberOfLabels([currentSubcategoryId], selectedRefiningValuesIds, searchKeyWord, currentCountryComboText, currentCityComboText, onGetNumberOfDirectoryLabelsComplete, onGetNumberOfDirectoryLabelsTimeOut, onGetNumberOfDirectoryLabelsError);
			Directory.GetLabelsData([currentSubcategoryId], selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
		}
		Effect.toggle($get("SubcategoryRefining_" + id), 'BLIND');
	}
	else if ($get("SubcategoryArrow_" + id).className == 'empty' || $get("SubcategoryArrow_" + id).className == 'emptyActive') {
		if ($get("SubcategoryArrow_" + id).className == 'emptyActive') {
			replaceStyle("SubcategoryArrow_" + id, 'empty');
			currentSubcategoryId = null;
		}
		else {
			replaceStyle("SubcategoryArrow_" + id, 'emptyActive');
			if (currentSubcategoryId != null) {
				if ($get("SubcategoryArrow_" + currentSubcategoryId).className == 'open' || $get("SubcategoryArrow_" + currentSubcategoryId).className == 'close') {
					Effect.toggle($get("SubcategoryRefining_" + currentSubcategoryId), 'BLIND');
					replaceStyle("SubcategoryArrow_" + currentSubcategoryId, 'close');
				}
				else if ($get("SubcategoryArrow_" + currentSubcategoryId).className == 'emptyActive' || $get("SubcategoryArrow_" + currentSubcategoryId).className == 'empty') {
					replaceStyle("SubcategoryArrow_" + currentSubcategoryId, 'empty');
				}

			}
			currentSubcategoryId = id;

			//Load subcaregory Directory Items
			//alert("subcat = " + id);
			fillRefiningFilters();
			pageNumber = 0;
			showLoadProgress();
			Directory.GetNumberOfLabels([currentSubcategoryId], selectedRefiningValuesIds, searchKeyWord, currentCountryComboText, currentCityComboText, onGetNumberOfDirectoryLabelsComplete, onGetNumberOfDirectoryLabelsTimeOut, onGetNumberOfDirectoryLabelsError);
			Directory.GetLabelsData([currentSubcategoryId], selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
		}

	}

	if (currentSubcategoryId == null) //load all data
	{
		showLoadProgress();
		Directory.GetNumberOfLabels(null, null, searchKeyWord, currentCountryComboText, currentCityComboText, onGetNumberOfDirectoryLabelsComplete, onGetNumberOfDirectoryLabelsTimeOut, onGetNumberOfDirectoryLabelsError);
		Directory.GetLabelsData(null, null, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
		$get('selectedSubcategory').innerHTML = "<span>" + searchKeyWord + "</span> within all categories";
	}
	else
		$get('selectedSubcategory').innerHTML = "<span>" + searchKeyWord + "</span> within " + $get("SubcategoryArrow_" + id).title;
}


function loadDirectoryPage() {

	$get("show10Label").className = "";
	$get("show30Label").className = "";
	$get("show50Label").className = "";
	$get("show" + numberOfLabelsPerPage + "Label").className = "select";

	// Sorting
	$get("SortAToZ").className = '';
	//$get("SortPopularity").className = '';
	//$get("SortPrice").className = '';
	//$get("SortMostRecent").className = '';

	var className = '';
	if (sortDirection == 0)
		className = 'Sort-AS';
	else
		className = 'Sort-DS';

	if (sortBy == 0)
		replaceStyle('SortMostRecent', className);
	if (sortBy == 1)
		replaceStyle('SortAToZ', className);
	if (sortBy == 2)
		replaceStyle('SortPrice', className);
	if (sortBy == 3)
		replaceStyle('SortPopularity', className);

	//showLoadProgress();
	if (pageNumber > numberOfLabelsPerPage) {
		pagingStartPageNumber = Math.floor(pageNumber % numberOfLabelsPerPage) + (numberOfLabelsPerPage - 1);
	}
	else {
		pagingStartPageNumber = 1;
	}
}


var pagingStartPageNumber = 1;
function loadDirectoryLabelsPage(currentPageNumber) {
	pageNumber = currentPageNumber;
	var max = 10;
	if (numberOfPagingButtons < max)
		max = numberOfPagingButtons;
	for (var i = pagingStartPageNumber; i < max + pagingStartPageNumber - 1; i++) {
		try {
			$get("pageButton" + i).className = '';
		}
		catch (ex) { break; }
	}
	$get("pageButton" + pageNumber).className = 'selected';
	showLoadProgress();

	Directory.GetLabelsData(subcategoryIds, selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
}



function onGetDirectoryLabelsComplete(result) {
	//alert(result);
	$get("LabelsContainer").innerHTML = "";
	if (result.length > 0) {
		for (var i = 0; i < result.length; i++) {
			var item = result[i].split('{,}');
			var newLabel = document.createElement("li");
			newLabel.className = "LableSubjectsNew";
			newLabel.innerHTML = $get('dummyLabel').innerHTML;
			newLabel.id = "label_" + item[0];
			var labelLink = $get("CurrentLabel", newLabel);
			labelLink.href = item[8];
			//var labelImgDiv = $get("CurrentLabelImageDiv", newLabel);
			$get("CurrentLabelImg", labelLink).src = "Images\\directory_sml\\" + item[5];
			//var labelContent = $get("CurrentLabelContent", labelLink);
			$get("LabelTitle", labelLink).innerHTML = getHTMLEncode(item[6]);
			//$get("LabelPrice", labelLink).innerHTML = item[1];
			$get("LabelDescription", labelLink).innerHTML = item[1];
//			$get("LabelDateCreated", labelLink).innerHTML = item[1];
			$get("LabelNumOfViews", labelLink).innerHTML = item[3] + ' Review(s)'; //NumberOfResponses
//			$get("LabelPopularity", labelLink).innerHTML = item[9];
			//$get("LabelIcon", labelLink).src = item[12];
			newLabel.style.display = "block";
			$get("LabelsContainer").appendChild(newLabel);
		}
	}
	else
		showNoResult();

	//saveClassifiedsPageVariables();

}

function onGetDirectoryLabelsError(result) {
	alert(result);
}

function onGetDirectoryLabelsTimeOut(result) {
	alert(result);
}

function sortDirectoryLabels(itemName) {

	var item = $get(itemName);
	var currentClass = item.className;
	$get("SortAToZ").className = '';
	$get("SortPopularity").className = '';
	$get("SortAutomatic").className = '';


	if (itemName != "SortAutomatic") {
		switch (currentClass) {
			case 'Sort-AS':
				replaceStyle(itemName, 'Sort-DS');
				sortDirection = 1;
				break;
			case 'Sort-DS':
				replaceStyle(itemName, 'Sort-AS');
				sortDirection = 0;
				break;
			default:
				if (itemName == 'SortPopularity') {
					replaceStyle(itemName, 'Sort-DS');
					sortDirection = 1;
				}
				else {
					replaceStyle(itemName, 'Sort-AS');
					sortDirection = 0;
				}
				break;
		}
	}
	else {
		sortDirection = 0;
		replaceStyle(itemName, 'Sort-Auto');
	}

	if (itemName == 'SortAToZ')
		sortBy = 0;
	else if (itemName == 'SortPopularity')
		sortBy = 1;
	else if (itemName == 'SortAutomatic')
		sortBy = 2;

	showLoadProgress();
	pagingStartPageNumber = 1;
	pageNumber = 1;
	drawPaging(numberOfDirectory);

	if (currentSubcategoryId != null)
		Directory.GetLabelsData([currentSubcategoryId], selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
	else
		Directory.GetLabelsData(null, selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);

}




function onGetNumberOfDirectoryLabelsComplete(result) {
	//alert("count = " + result);
	if (result == 0)
		showNoResult();

	numberOfDirectory = result;
	drawPaging(result);
}


function onGetNumberOfDirectoryLabelsTimeOut(result) {
}

function onGetNumberOfDirectoryLabelsError(result) {
}

function changeNumberOfDirectoryLabelsPerPage(numberOfLabels) {
	pagingStartPageNumber = 1;
	numberOfLabelsPerPage = numberOfLabels;
	$get("show10Label").className = "";
	$get("show30Label").className = "";
	$get("show50Label").className = "";

	$get("show" + numberOfLabels + "Label").className = "select";

	showLoadProgress();
	drawPaging(numberOfDirectory);

	pageNumber = 0;
	if (currentSubcategoryId != null)
		Directory.GetLabelsData([currentSubcategoryId], selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
	else
		Directory.GetLabelsData(null, selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
}


function fillRefiningFilters() {
	var ctrl;
	selectedRefiningValuesIds = new Array();
	for (var i = 0; i < loadedRefiningIds.length; i++) {
		ctrl = $get("refiningGroup" + loadedRefiningIds[i], $get("SubcategoryRefining_" + currentSubcategoryId));
		if (ctrl != null && ctrl.options.length > 0) {
			if (ctrl.selectedIndex != -1 && ctrl.selectedIndex != 0)
				selectedRefiningValuesIds.push(ctrl.options[ctrl.selectedIndex].value);
		}
	}
}

function refineDirectoryData() {
	showLoadProgress();

	fillRefiningFilters();
	Directory.GetNumberOfLabels([currentSubcategoryId], selectedRefiningValuesIds, searchKeyWord, currentCountryComboText, currentCityComboText, onGetNumberOfDirectoryLabelsComplete, onGetNumberOfDirectoryLabelsTimeOut, onGetNumberOfDirectoryLabelsError);
	Directory.GetLabelsData([currentSubcategoryId], selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);

}

function showHideSubCategoriesContainer() {
	if ($get('showHideSubCategoriesLink').className == 'show') {
		replaceStyle('showHideSubCategoriesLink', '');
		$get('showHideSubCategoriesLink').innerHTML = 'show all';

		elements = document.getElementsByName('subcategories');
		if (elements.length > 10) {
			for (i = 10; i < elements.length; i++) {
				elements[i].style.display = "none";
			}
		}
	}
	else {
		replaceStyle('showHideSubCategoriesLink', 'show');
		$get('showHideSubCategoriesLink').innerHTML = 'show less';

		elements = document.getElementsByName('subcategories');
		for (i = 0; i < elements.length; i++) {
			elements[i].style.display = "";
		}
	}

}

function ComboSetSelectedValue(cmbControl, iValue) {
	if (iValue == -1) {
		cmbControl.selectedIndex = -1;
	}
	else {
		for (var i = 0; i < cmbControl.options.length; i++) {
			if (cmbControl.options[i].value == iValue)
				cmbControl.selectedIndex = i;
		}
	}
}

function ComboGetSelectedValue(cmbControl) {
	if (cmbControl.selectedIndex == -1)
		return -1;
	var iValue = parseInt(cmbControl.options[cmbControl.selectedIndex].value);
	if (isNaN(iValue))
		return -1;
	return iValue;
}



function loadItemPhotosPage(pageNumber) {
	for (var k = 1; k <= numberOfPagingButtons; k++) {
		$get("pageButton" + k).className = '';
	}
	$get("pageButton" + pageNumber).className = 'selected';
	//showLoadProgress();

	Directory.GetItemPhotos(itemId, pageNumber, 5, GetItemPhotosComplete, GetItemPhotosTimeOut, GetItemPhotosError);
}

function GetItemPhotosError(result) {
	//alert(result);
}

function GetItemPhotosTimeOut(result) {
	//alert(result);
}

function GetItemPhotosComplete(result) {
	$get("AdsPhotos").innerHTML = '';
	if (result.length > 0) {
		for (var i = 0; i < result.length; i++) {
			var photo = result[i].split(',');
			var newPhoto = document.createElement("div");
			newPhoto.id = "PicBox_" + i;
			newPhoto.className = "pict-box";
			newPhoto.innerHTML = $get("dummyPicBox").innerHTML;
			$get("dummyPic", newPhoto).src = "/Images/directory/" + photo[0];
			$get("dummyPicDesc", newPhoto).innerHTML = photo[1];
			newPhoto.style.display = "block";
			$get("AdsPhotos").appendChild(newPhoto);
		}
	}
}



var currentGroupId;
var currentRefiningGroupsIds = new Array();
function getRefiningGroups(subcategoryId) {
	//var subcategoryId = control.options[control.selectedIndex].value; //1002; //
	Directory.GetRefiningGroups(subcategoryId, onGetRefiningGroupsComplete, onGetRefiningGroupsTimeOut, onGetRefiningGroupsError);
}
function onGetRefiningGroupsComplete(result) {
	$get("dvRefiningGroups").innerHTML = "";
	if (result.length == 0)
		return;
	var data = result.toString().split(',');
	if (data.length > 0) {
		currentRefiningGroupsIds = new Array();
		for (var i = 0; i < data.length; i += 2) {
			var refineGroupHtml = "<div class='AddClassifiedForm'><label>";
			refineGroupHtml += data[i + 1] + ":" + "<select style='width:178px;' id='refiningGroup" + data[i] + "' title='" + data[i + 1] + "' onclick='getRefiningValues(" + data[i] + ")' > </select><br />";
			refineGroupHtml += "</label></div>";
			$get("dvRefiningGroups").innerHTML += refineGroupHtml;
			currentRefiningGroupsIds.push(data[i]);
		}
	}
	if (dirEditMode) {
		Directory.GetItemRefiningValues(g_iCurDirectoryID, onGetItemRefiningValuesComplete, onAjaxError, onAjaxTimeOut);
	}
	dirEditMode = false;
}


function onGetRefiningGroupsTimeOut(result) {
}

function onGetRefiningGroupsError(result) {
}

function removeRefiningGroups() {
	$get("dvRefiningGroups").innerHTML = "<p>no refining groups for this subcategory</p>";
	groupsIdsHaveOldvalues = "";
}


function getRefiningValues(groupId) {
	if ($get("refiningGroup" + groupId).options.length == 0 || groupsIdsHaveOldvalues.indexOf("|" + groupId + "|") > -1) {
		currentGroupId = groupId;
		Directory.GetRefiningValues(groupId, onGetRefiningValuesComplete);
	}
}

function onGetRefiningValuesComplete(result) {
	var data = result.toString().split(',');
	if (data.length > 0) {
		var refiningGroup = $get("refiningGroup" + currentGroupId);
		var selectedValue = "";
		if (groupsIdsHaveOldvalues.indexOf("|" + currentGroupId + "|") > -1) {
			selectedValue = refiningGroup.options[0].value;
			refiningGroup.options.length = 0;
			groupsIdsHaveOldvalues = groupsIdsHaveOldvalues.replace("|" + currentGroupId, "");
		}
		var counter = 0;
		for (var i = 0; i < data.length; i += 2) {
			refiningGroup.options[counter] = new Option(data[i + 1], data[i]);
			if (data[i] == selectedValue)
				refiningGroup.selectedIndex = counter;
			counter++;
		}
	}
	currentGroupId = "";
}


var groupsIdsHaveOldvalues = "";
function onGetItemRefiningValuesComplete(result) {
	if (result != null && result.length > 0) {
		var optionData;
		var refiningGroup;
		for (var i = 0; i < result.length; i++) {
			optionData = result[i].split('|');
			refiningGroup = $get("refiningGroup" + optionData[0]);
			refiningGroup.options[0] = new Option(optionData[2], optionData[1]);
			groupsIdsHaveOldvalues += "|" + optionData[0];
		}
		groupsIdsHaveOldvalues += "|";
	}
}



function SelectDirSubCategory(ctrlName) {
	if ($get(ctrlName).selectedIndex > -1) {
		if ($get(ctrlName).options[$get(ctrlName).selectedIndex].text) {
			getRefiningGroups($get(ctrlName).options[$get(ctrlName).selectedIndex].value);
			 //$get(ctrl.id + '_myp').innerHTML = ctlSelect.options[ctlSelect.selectedIndex].text;
		}
	}
}


function onGetCitiesByCountryComplete(result) {
	$get('cityCombobox').innerHTML='';
	var data = result.toString().split(',');
	var counter = 0;
	for (var i = 0; i < data.length; i += 2) {
		$get('cityCombobox').options[counter] = new Option(data[i + 1], data[i]);
		counter++;
	}
	$get('cityCombobox_myp').innerHTML = $get('cityCombobox_myp').title;
}


function FilterCitiesByCountry() {
	if ($get('locationCombobox').options[$get('locationCombobox').selectedIndex].text)
		currentCountryComboText = $get('locationCombobox').options[$get('locationCombobox').selectedIndex].value;
	else
		currentCountryComboText = 0;
	Members.GetAllCities($get('locationCombobox').options[$get('locationCombobox').selectedIndex].value, true, onGetCitiesByCountryComplete, onGetDataTimeOut, onGetDataError);
	showLoadProgress();
	Directory.GetNumberOfLabels(subcategoryIds, selectedRefiningValuesIds, searchKeyWord, currentCountryComboText, currentCityComboText, onGetNumberOfDirectoryLabelsComplete, onGetNumberOfDirectoryLabelsTimeOut, onGetNumberOfDirectoryLabelsError);
	Directory.GetLabelsData(subcategoryIds, selectedRefiningValuesIds, 0, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
}


function SelectLocationCity() {
	if ($get('cityCombobox').options[$get('cityCombobox').selectedIndex].text)
		currentCityComboText = $get('cityCombobox').options[$get('cityCombobox').selectedIndex].value;
	else
		currentCityComboText = 0;
	showLoadProgress();
	Directory.GetNumberOfLabels(subcategoryIds, selectedRefiningValuesIds, searchKeyWord, currentCountryComboText, currentCityComboText, onGetNumberOfDirectoryLabelsComplete, onGetNumberOfDirectoryLabelsTimeOut, onGetNumberOfDirectoryLabelsError);
	Directory.GetLabelsData(subcategoryIds, selectedRefiningValuesIds, pageNumber, numberOfLabelsPerPage, sortBy, sortDirection, searchKeyWord, currentCountryComboText, currentCityComboText, onGetDirectoryLabelsComplete, onGetDirectoryLabelsTimeOut, onGetDirectoryLabelsError);
}