var d = document;

function lwPopupWindow(url, width, height, options) {
	
	var xMax = 800;
	var yMax = 600;
	
	if (screen) {
		
		xMax = screen.width;
		yMax = screen.height;
		
	} else if (document.layers) {
		
		// NN 4 support
		xMax = window.outerWidth;
		yMax = window.outerHeight;
		
	}
	
	var xOffset = (xMax - width)/2;
	var yOffset = (yMax - height)/2;
	
	var str = 'width='+width+',height='+height+',screenX='+xOffset+',screenY='+yOffset+',top='+(yOffset-30)+',left='+xOffset+''
	
	if (options) {
		str += ','+options;  
	}
	
	// close popup window if already exists
	if (window && window._popupWindow) {
		window._popupWindow.close();
	}
	
	window._popupWindow = window.open(url, '_popupWindow', str); 
	
	if (window._popupWindow) {
		window._popupWindow.focus();
	}
	
	return window._popupWindow ? false : true;
}

function swapImage(imageId, newUrl) {
	
	var imageObject = document.getElementById(imageId);
	
	if (!imageObject || !newUrl) {
		return false;
	}
	
	imageObject.src = newUrl;
	
	return true;
}

function swapProfileImage(imageId, newUrl) {
	
	var imageObject = document.getElementById(imageId);
	
	if (!imageObject || !newUrl) {
		return false;
	}
	imageObject.style.backgroundImage = "url("+newUrl+")";
	return true;
}

function lwCheckUncheckAll(senderObject, senderForm) {
		
	if (!senderObject.form && senderForm) {
		senderObject.form = senderForm;
	}
	
	isAllChecked = true;
	
	if (senderObject.form && senderObject.form.elements) {
		
		for (i = 0; i < senderObject.form.elements.length; i++) {
			if (senderObject.form.elements[i].type == 'checkbox' && !senderObject.form.elements[i].checked) {
				isAllChecked = false;
			}
		}
		
		for (i = 0; i < senderObject.form.elements.length; i++) {
			if (senderObject.form.elements[i].type == 'checkbox' && !senderObject.form.elements[i].disabled) {
				senderObject.form.elements[i].checked = !isAllChecked;
			}
		}
	}
}


function qsSpSwapCountryList() {
	
	if (document.getElementById('qsGenderFemale').checked) {
	
		document.getElementById('qsCountries4Men').style.visibility = 'hidden';
		document.getElementById('qsCountries4Men').style.display = 'none';
		document.getElementById('qsCountries4Men').name = 'none';
		
		document.getElementById('qsCountries4Ladies').style.visibility = 'visible';
		document.getElementById('qsCountries4Ladies').style.display = 'inline';
		document.getElementById('qsCountries4Ladies').name = 'search[countries]';
		
	} else {
	
		
		document.getElementById('qsCountries4Ladies').style.visibility = 'hidden';
		document.getElementById('qsCountries4Ladies').style.display = 'none'
		document.getElementById('qsCountries4Ladies').name = 'none'
		
		document.getElementById('qsCountries4Men').style.visibility = 'visible';
		document.getElementById('qsCountries4Men').style.display = 'inline';
		document.getElementById('qsCountries4Men').name = 'search[countries]';
	}
	
}


function acountSpSwapCountryList(senderObject) {

	
	if (senderObject.options[senderObject.selectedIndex].value == 'female') {
	
		document.getElementById('qsCountries4Men').style.visibility = 'hidden';
		document.getElementById('qsCountries4Men').style.display = 'none';
		
		document.getElementById('qsCountries4Ladies').style.visibility = 'visible';
		document.getElementById('qsCountries4Ladies').style.display = 'inline';
		
	} else {
	
		
		document.getElementById('qsCountries4Ladies').style.visibility = 'hidden';
		document.getElementById('qsCountries4Ladies').style.display = 'none'
		document.getElementById('qsCountries4Men').style.visibility = 'visible';
		document.getElementById('qsCountries4Men').style.display = 'inline';
	}
	swapCities();
}

function swapCities(senderObject) {
	
	var citySelect = d.getElementById('citySelect'); 
	var otherCityInput = d.getElementById('otherCityInput');
	var otherCity = d.getElementById('otherCity');
	
	if (!senderObject || !citySelect) {
		return;
	}
	
	var countryId = senderObject.options[senderObject.selectedIndex].value;

	
	citySelect.options.length = 0;
	
	citySelect.options[0] = new Option('Select city or specify below');
	citySelect.options[0].value = '';
	
	for (i = 0; i < ac.length; i++) {
		
		if (ac[i].cn != countryId) {
			continue;
		}
		
		citySelect.options[citySelect.options.length] = new Option(ac[i].n);
		citySelect.options[citySelect.options.length-1].value = ac[i].ct;
	}
	
	citySelect.disabled = countryId ? false : true;
	otherCityInput.disabled = true;
	otherCity.checked = false;
}
