
var CALLBACK_MSG = '';

function getXMLHttpRequestObject() {
	return window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
}

function loadEstados(url, pais, estado, cidade, loadCities) {
	setInnerHTML('estados', '');
	setInnerHTML('cidades', '');

	hide('estados_table');
	hide('cidades_table');

	//Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				show('estados_table');
				setInnerHTML('estados', xmlHttp.responseText);

				if (loadCities)
					loadCidades(url, estado, cidade);
			} else {
				if (loadCities) 
					loadPaisCidades(url, pais, cidade);
			}
			//Modal.unblock();
		}
	};
	xmlHttp.open('GET', url + '/states/' + pais + '/' + estado, true);
	xmlHttp.send(null);
}

function loadCidades(url, estado, cidade) {
	setInnerHTML('cidades', '');
	hide('cidades_table');

	//Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				show('cidades_table');
				setInnerHTML('cidades', xmlHttp.responseText);
			}
			//Modal.unblock();
		}
	};
	xmlHttp.open('GET', url + '/cities/' + estado + '/' + cidade, true);
	xmlHttp.send(null);
}

function loadPaisCidades(url, pais, cidade) {
	setInnerHTML('cidades', '');
	hide('cidades_table');

	//Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				show('cidades_table');
				setInnerHTML('cidades', xmlHttp.responseText);
			}
			//Modal.unblock();
		}
	};
	xmlHttp.open('GET', url + '/countryCities/' + pais + '/' + cidade, true);
	xmlHttp.send(null);
}

function loadCaptcha(url) {
	document.getElementById('captcha').src = url + '?' + Math.random();
}

function hide(field){
	if (document.getElementById(field))
		document.getElementById(field).style.visibility = 'hidden';
		// element.style.display = 'none';
}

function hide2(field){
	if (document.getElementById(field)) {
		document.getElementById(field).innerHTML = '';
		document.getElementById(field).style.display = 'none';
	}
}

function show(field){
	if (document.getElementById(field))
		document.getElementById(field).style.visibility = 'visible';
		// element.style.display = 'block';
}

function setInnerHTML(element, html) {
	if (document.getElementById(element))
		document.getElementById(element).innerHTML = html;
}

function manageCheckBoxes(form, name, checked) {
	var el = eval('document.' + form + '.' + name);
	if (el) {
		if (el.length) { // checkboxes
			for (i = 1; i < el.length; i++) { // pula o primeiro elemento...
				el[i].checked = checked;
			}
		} else { // selects
			var loop = 1;
			while (el = eval('document.' + form + '.' + name + '_' + loop)) {
				el.selectedIndex = 0;
				loop++;
			}
		}
	}
}

function uncheckFirst(form, name) {
	var el = eval('document.' + form + '.' + name);
	if (el) {
		if (el.length) {
			el[0].checked = false;
		} else {
			el.checked = false;
		}
	}
}

function uncheckFirstAnimals(form, name, value) {
	var el = eval('document.' + form + '.' + name);
	if (value == 2) {
		manageCheckBoxes(form, name, false);
		el[0].checked = true;
	} else {
		el[0].checked = false;
	}
}

function textCounter(field, cntfield, maxlimit) {
	field = document.getElementById(field);
	cntfield = document.getElementById(cntfield);

	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	} else {
		cntfield.innerHTML = (maxlimit - field.value.length);
	}
}

function checkIntencao(form, name) {
	var el = eval('document.' + form + '.' + name);
	if (el && el.length) {
		var checkeds = 0;
		for (i = 0; i < el.length; i++) {
			if (el[i].checked)
				checkeds++;
		}
		if (checkeds > 2) {
			return false;
		}
	}
	return true;
}

function nextEmailSendStep(url, id) {
	var dest = document.getElementById(id);
	if (dest.value != null && dest.value != '') {
		location.href = url + dest.value;
	} else {
		Modal.alert('Digite o apelido de quem irá receber sua mensagem.');
		dest.focus();
	}
}

//Centered Pop-Up Window (v1.0)
//(C) 2002 www.smileycat.com
//Free for all users, but leave in this header

/*
 * usage:
 * <a href="#" onClick="pop('http://www.java-scripts.net','','750','450','resizable,scrollbars,status,toolbar')">Click Here</a>
 */
var win = null;
function pop(mypage, myname, w, h, features) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	if (winl < 0) winl = 0;
	if (wint < 0) wint = 0;
	var settings  = 'height=' + h    + ',';
		settings += 'width='  + w    + ',';
		settings += 'top='    + wint + ',';
		settings += 'left='   + winl + ',';
		settings += features;
	win = window.open(mypage, myname, settings);
	if (win)
		win.window.focus();
	return win;
}

function popEmoticons(url) {
	pop(url, 'emoticons', '470', '200', 'resizable=0,scrollbars=0,status=0,toolbar=0');
}

function popMess(url) {
	pop(url, 'messenger', '670', '465', 'resizable=0,scrollbars=0,status=0,toolbar=0');
}

function popEmailDireto(url) {
	pop(url, 'emaildireto', '900', '380', 'resizable=0,scrollbars=1, status=0,toolbar=0');
}

function popEmail(url) {
	pop(url, 'email', '900', '390', 'resizable=0,scrollbars=1, status=0,toolbar=0');
}

function popParceiros(url) {
	pop(url, 'parceiros', '750', '450', 'resizable=0,scrollbars=1, status=0,toolbar=0');
}

function popDenounce(url) {
	pop(url, 'email', '900', '390', 'resizable=0,scrollbars=1, status=0,toolbar=0');
}

function popInviter(url) {
	pop(url, 'inviter', '900', '530', 'resizable=0,scrollbars=1, status=0,toolbar=0');
}

function checkAll(field) {
	if (field)
		if (field.length)
			for (i = 0; i < field.length; i++)
				field[i].checked = true;
		else
			field.checked = true;
}

function uncheckAll(field) {
	if (field)
		if (field.length)
			for (i = 0; i < field.length; i++)
				field[i].checked = false;
		else
			field.checked = false;
}

function hasChecked(field) {
	if (field)
		if (field.length) {
			for (i = 0; i < field.length; i++)
				if (field[i].checked == true) {
					return true;
				}
		} else {
			return field.checked;
		}
}

function getAllCheckboxesValues(field) {
	var qs = '';
	if (field)
		if (field.length)
			for (i = 0; i < field.length; i++)
				qs += 'id[]=' + field[i].value + '&';
		else
			qs = 'id[]=' + field.value;
	return '?' + qs;
}

function getAllSelectedCheckboxesValues(field) {
	var qs = '';
	if (field)
		if (field.length) {
			for (i = 0; i < field.length; i++)
				if (field[i].checked)
					qs += 'id[]=' + field[i].value + '&';
		} else {
			if (field.checked)
				qs = 'id[]=' + field.value;
		}
	return '?' + qs;
}

function sendAllEmailDireto(url, field) {
	var xmlHttp = getXMLHttpRequestObject();
	Modal.block();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url + getAllCheckboxesValues(field), true);
	xmlHttp.send(null);	
}

function alertOnline(url, id, checkbox) {
	var xmlHttp = getXMLHttpRequestObject();
	Modal.block();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url + (checkbox.checked ? 'save' : 'delete') + '/' + id, true);
	xmlHttp.send(null);	
}

function sendQuiz(url, id, text, alertt, anonymous) {
	if (text.value == '') {
		// alert('Informe a pergunta que será feita neste Quiz!');
		text.focus();
		return;
	}
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
				text.value = '';
				if (alertt) alertt.checked = true;
				if (anonymous) anonymous.checked = false;
			}
			Modal.unblock();
		}
	};
	var params = 'id=' + id + 
				 '&quiz=' + escape(text.value) + 
				 '&alert=' + (alertt && alertt.checked ? 1 : 2) + 
				 '&anonymous=' + (anonymous && anonymous.checked ? 1 : 2);

	xmlHttp.open('POST', url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.send(params);
}

var Modal = {
	block : function () {
	    $.blockUI({
	    	message: '<div class="font12 bold dark_red">Por favor, aguarde...</div>',
	        centerY: 0,
	        overlayCSS: { backgroundColor: '#eeeeee' },
	        css: {
//	    		top: '10px',
	    		padding: '20px',
	            backgroundColor: '#FFFF80',
	            '-webkit-border-radius': '10px', 
	            '-moz-border-radius': '10px'
//	    		left: '',
	//    		right: '10px'
	    	} 
	    });
	},

	unblock : function () {
	    setTimeout($.unblockUI, 100);
	    if (CALLBACK_MSG != '')
	    	setTimeout('Modal.showCallbackMessage();', 200);
	}, 

	showCallbackMessage : function () {
        $.blockUI({ 
        	message: '<div class="font12 bold dark_red"><table border="0" style="background-color: #FF8040; padding: 3px;" width="100%"><tr><td align="left" class="bold white font12">Resultado</td><td align="right"><a href="javascript:void(0);" onclick="Modal.unblock();"><img src="' + Constants.CONTEXT + '/img/close.png" alt="Fechar" title="Fechar" border="0" /></a></td></tr></table><br />' + CALLBACK_MSG + '</div>',
            fadeIn: 1000,
            timeout: 5000,
            overlayCSS: { backgroundColor: '#eeeeee' },
	        css: {
	            backgroundColor: '#FFFF80',
	            padding: '10px',
	            '-webkit-border-radius': '10px', 
	            '-moz-border-radius': '10px' 
    		}
        });
		CALLBACK_MSG = '';
	},

	alert : function (msg) {
        $.blockUI({ 
        	message: '<div class="font12 bold dark_red">' + msg + '</div>',
            fadeIn: 1000,
            timeout: 3000,
            overlayCSS: { backgroundColor: '#eeeeee' },
	        css: {
	            backgroundColor: '#FFFF80',
	            padding: '20px',
	            '-webkit-border-radius': '10px', 
	            '-moz-border-radius': '10px' 
    		}
        });
	},

	alertRemocao : function () {
        $.blockUI({ 
        	message: '<div class="font12 bold dark_red" align="left">Importante:<br /><br />1. MESMO <u>APÓS REMOVER</u> SEU PERFIL, BASTARÁ EFETUAR UM LOGIN NA SUA CONTA PARA <u>REATIVÁ-LA A QUALQUER MOMENTO</u>.<br /><br />2. Para não haver dúvidas, por favor, leia todos os avisos desta página!<br /><br />Esta mensagem fechará em 45 segundos.</div>',
            fadeIn: 1000,
            timeout: 45000,
            overlayCSS: { backgroundColor: '#eeeeee' },
	        css: {
	            backgroundColor: '#FFFF80',
	            padding: '20px',
	            '-webkit-border-radius': '10px', 
	            '-moz-border-radius': '10px' 
    		}
        });
	},

	alertRemovido : function () {
        $.blockUI({ 
        	message: '<div class="font12 bold dark_red" align="left">Importante:<br /><br />1. Seu perfil foi <u>REMOVIDO</u> e ninguém mais acessará seus dados.<br /><br />2. Para <u>REATIVAR SEU PERFIL</u>, basta efetuar um login na sua conta para  qualquer momento.<br /><br />Agradecemos pelo tempo em que esteve conosco!<br /><br />Esta mensagem fechará em 45 segundos.</div>',
            fadeIn: 1000,
            timeout: 45000,
            overlayCSS: { backgroundColor: '#eeeeee' },
	        css: {
	            backgroundColor: '#FFFF80',
	            padding: '20px',
	            '-webkit-border-radius': '10px', 
	            '-moz-border-radius': '10px' 
    		}
        });
	}
}

function setQuiz(url, bool) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
		}
		Modal.unblock();
	};
	var busy = bool ? Constants.YES : Constants.NO;
	xmlHttp.open('GET', url + busy, true);
	xmlHttp.send(null);
}

function sendSelectedEmailDireto(url, field) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url + getAllSelectedCheckboxesValues(field), true);
	xmlHttp.send(null);	
}

function sendEmailDireto(url, id) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url + '?id[]=' + id, true);
	xmlHttp.send(null);	
}

function sendKiss(url) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);	
}

function askPhoto(url) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);	
}

function manageFavorite(url) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);	
}

function manageVisits(url) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);	
}

function manageContacts(url) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);	
}

function manageBlocked(url) {
	var changeText = true;
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);

				if (changeText) {
					var blockLink = document.getElementById('blockLink');
					if (blockLink)
						blockLink.innerHTML = blockLink.innerHTML == 'Bloquear' ? 'Desbloquear' : 'Bloquear';
				}
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);	
}

function denounce(url) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);	
}
/*
function fetchRightProfiles(url) {
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
		}
	};
	xmlHttp.open('GET', url, true);
	xmlHttp.send(null);
}
*/
function setBusy(url, bool) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	var busy = bool ? Constants.YES : Constants.NO;
	xmlHttp.open('GET', url + busy, true);
	xmlHttp.send(null);
}

function setProfileNotifier(url, bool) {
	Modal.block();
	var xmlHttp = getXMLHttpRequestObject();
	xmlHttp.onreadystatechange = function () {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			if (xmlHttp.responseText != '') {
				eval(xmlHttp.responseText);
			}
			Modal.unblock();
		}
	};
	var busy = bool ? Constants.YES : Constants.NO;
	xmlHttp.open('GET', url + busy, true);
	xmlHttp.send(null);
}

function checkAgeRange(min, max) {
	var recomendado = 3;
	if (min != '' && min != 0 && max != '' && max != 0 && (max - min) >= 0)
		if (max - min < recomendado) {
			var msg = 'Você informou uma faixa de idade com diferença de ' + (max - min) + ' ano' + ((max - min) > 1 ? 's' : '' ) + '.';
			alert(msg + '\n\nPara que você possa encontrar mais perfis compatíveis, recomendamos uma diferença de pelo menos ' + recomendado + ' anos.');
		}
}

/*
$(function () {
  
  var msie6 = $.browser == 'msie' && $.browser.version < 7;
  
  if (!msie6) {
    var top = $('#comment').offset().top - parseFloat($('#comment').css('margin-top').replace(/auto/, 0));
    $(window).scroll(function (event) {
      // what the y position of the scroll is
      var y = $(this).scrollTop();
      
      // whether that's below the form
      if (y >= top) {
        // if so, ad the fixed class
        $('#comment').addClass('fixed');
      } else {
        // otherwise remove it
        $('#comment').removeClass('fixed');
      }
    });
  }  
});
*/
