

function reportRetreat(id, title){
	showModal({
		width: 575,
		height: 395,
		btnOk: 'Save',
		isWindow: true,
		title: 'Serious or simply wrong?',
		fileName: 'SendReport',
		listeners: {
			'ok': {
				fn: function (){
					$('#reportForm').ajaxSubmit({
						success: function (msg)  {
							var msg = eval('(' + msg + ')');
							$('#titleRetreat').html('');
							if(msg['err']){
								$('#msgArea').html(msg['msg']);		
							}else{
								closeModal();
								showModal({
									modal: true,
									title: 'Info',
									msg: msg['msg']
								});	
							}
						} 
					});
				}
			},
			'afterLoad': {
				fn: function (){
					$('#idRetreat').val(id);
					$('#titleRetreat').html(title);
					$('#retreatTitle').val(title);
				}
			}
		}
	}); 
}


function sendRetreat(id){
	showModal({
		width: 575,
		height: 395,
		btnOk: 'Save',
		isWindow: true,
		title: 'Share this retreat via e-mail.',
		fileName: 'inviteFriend',
		listeners: {
			'ok': {
				fn: function (){
					$('#inviteForm').ajaxSubmit({
						success: function (msg)  {
							var msg = eval('(' + msg + ')');
							$('#msgArea').html('');
							if(msg['err']){
								$('#msgArea').html(msg['msg']);		
							}else{
								closeModal();
								showModal({
									modal: true,
									title: 'Info',
									msg: msg['msg']
								});	
							}
						} 
					});
				}
			},
			'afterLoad': {
				fn: function (){
					$('#message').val("Hello, \n this retreat might interest you: \n \n " + window.location);
				}
			}
		}
	}); 
}


function sw2aInit(){
	var eq = document.getElementById('event_query');
	eq.onclick = function(){
		if(eq.value==searchStr){
			eq.value='';
		}
	}
	eq.onblur = function(){
		if(eq.value==''){
			eq.value=searchStr;
		}
	}
	eq.onkeypress = function(e){
		if(e.keyCode==13){
			sa.click();
		}
	}
	
	var sa = document.getElementById('search_action');
	sa.onclick = function(){
		if(eq.value!=searchStr && eq.value!=''){
			window.location = web_path + "result.php?q=" + eq.value;
		}
	}
	
}

window.onload=sw2aInit;


$(document).ready(function() { 

	
	if($('#myAccountForm')){
		$('#myAccountForm').ajaxForm({
			dataType: 'json',
        	success: function (res, statusText, xhr, $form)  {
				$('#resMsg')[0].innerHTML = res.msg;
    		} 
    	}); 
	}	
    
	if($('#loginForm')){
		$('#loginForm').ajaxForm({
			dataType: 'json',
        	success: function (res, statusText, xhr, $form)  {
				var remember = document.getElementById('remember');
				if(remember.checked){
					var tempData = res.tempData;
					setCookie('retreatTmpInfo', tempData);
				}else{
					deleteCookie('retreatTmpInfo');
				}
				$('#resMsg')[0].innerHTML = res.msg;
				if(!res.err){
					window.location.href=res.url;
				}
    		} 
    	}); 
	}
	
	if($('#registerForm')){
		$('#registerForm').ajaxForm({
			dataType: 'json',
        	success: function (res, statusText, xhr, $form)  {
				$('#resMsg')[0].innerHTML = res.msg;
				if(!res.err){
					setTimeout('window.location.href=web_path;', 1500);
				}
				if(res.errFields){
					$('#resMsg')[0].innerHTML;	// += ' (';
					var lbls = document.getElementsByTagName('label');
					for(var a=0; a<res.errFields.length; a++){
						var t = '';
						for(var x=0; x<lbls.length;x++){
							if(lbls[x].getAttribute('for')==res.errFields[a]){
						//		$('#resMsg')[0].innerHTML += lbls[x].innerHTML +', ';
								break;
							}
						}
						
					}
				//	var s = $('#resMsg')[0].innerHTML;
				//	$('#resMsg')[0].innerHTML = s.substring(0, s.length-2);
				//	$('#resMsg')[0].innerHTML += ')';
				}
    		} 
    	}); 
	}
	
	if($('#recoverForm')){
		$('#recoverForm').ajaxForm({
			dataType: 'json',
        	success: function (res, statusText, xhr, $form)  {
				$('#resMsg')[0].innerHTML = res.msg;
				if(!res.err){
					window.location.href=web_path;
				}
    		} 
    	}); 
	}
	
	if($('#chPassForm')){
		$('#chPassForm').ajaxForm({
			dataType: 'json',
        	success: function (res, statusText, xhr, $form)  {
				$('#resMsg')[0].innerHTML = res.msg;
				if(!res.err){
					setTimeout('window.location.reload();', 1000);					
				}
    		} 
    	}); 
	}
	
	
	$("#dialog-report").dialog({
		autoOpen: false,
		height: 300,
		width: 350,
		modal: true,
		buttons: {
			'Cancel': function() {
				$(this).dialog('close');
			},
			'Send Report': function() {
				var bValid = true;
				allFields.removeClass('ui-state-error');

				bValid = bValid && checkLength(name,"username",3,16);
				bValid = bValid && checkLength(email,"email",6,80);
				bValid = bValid && checkLength(password,"password",5,16);

				bValid = bValid && checkRegexp(name,/^[a-z]([0-9a-z_])+$/i,"Username may consist of a-z, 0-9, underscores, begin with a letter.");
				// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
				bValid = bValid && checkRegexp(email,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. ui@jquery.com");
				bValid = bValid && checkRegexp(password,/^([0-9a-zA-Z])+$/,"Password field only allow : a-z 0-9");
				
				if (bValid) {
					$('#users tbody').append('<tr>' +
						'<td>' + name.val() + '</td>' + 
						'<td>' + email.val() + '</td>' + 
						'<td>' + password.val() + '</td>' +
						'</tr>'); 
					$(this).dialog('close');
				}
			}
		}, 
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	
	
	
	$('#report').click(function() {
		$('#dialog-form').dialog('open');
	});
	
}); 

function updateCountry(cbo){
	var cID = getDDValue(cbo);
	$.ajax({ 
		url: web_path + "ajax.php?l=getCountry",
		dataType: 'json',
		type: 'POST',
		data: {CID : cID},
		success: function(res){
			var s = '';
        	if(res.data.length<1){
        		s += '<option value="0">No country.</option>';
        	}else{
        		for(var i=0; i<res.data.length; i++){
        			s += '<option value="'+res.data[i]['CountryID']+'">'+res.data[i]['Name']+'</option>';
        		}
        	}
        	document.getElementById('Country').innerHTML = s;
      	}
	});
}

function eL(id, val, el){
	document.getElementById(el+'ID').value = id;
	document.getElementById(el).value = val; 
}

function dL(id, val, el){
	document.getElementById(el+'ID').value = 0;
	document.getElementById(el).value = '';
}

function removeFilter(){
	var filterCt = document.getElementById('filterCt');
	var urlTo = web_path + 'show-event.php?m=' + gup('m');
	filterCt.style.display = 'none';
	window.location.href = urlTo;
}

function filterEvent(sel){
//	init	
	var filterCt = document.getElementById('filterCt');
	var optRetreat = document.getElementById('optRetreat');
	var optCountry = document.getElementById('optCountry');
	var optContinent = document.getElementById('optContinent');
	var urlTo = web_path + 'show-event.php?m=' + gup('m');
	
//	settings
	filterCt.style.display = '';
	urlTo += '&s1=' + getSelected(optRetreat);
	urlTo += '&s2=' + getSelected(optCountry);
	urlTo += '&s3=' + getSelected(optContinent);

//	final result	
	window.location.href = urlTo;
}

function getSelected(sel){
	return sel.options[sel.selectedIndex].value;
}


function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}


function showTerms(k){
//	var showTermsCh = document.getElementById('showTermsCh');
	var showTermsCt = document.getElementById('showTermsCt');
//	if(k==1){
		showTermsCt.style.display = '';
//	}
}

function setCookie(name, value, expires, path, domain, secure){
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function getCookie(check_name){
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	for ( i = 0; i < a_all_cookies.length; i++ ){
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name ){
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ){
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ){
		return null;
	}
}

/**
 * Common functions
 */
function getDDValue(dd){
	return dd.options[dd.selectedIndex].value;
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



function showOverlay() {
	var h = getDocHeight(); 
	var d = document;
	var overlay = d.createElement('div');
	overlay.id = "overlay";
	overlay.className = 'overlay';
	overlay.style.width = "100%";
	overlay.style.height = h+'px';
	d.body.insertBefore(overlay, d.body.firstChild) // not what I want
}

function noFunctYes(){alert('neimplementat');}

function showModal(config) {
	if(!config) { config = {}; }
	var modal = config.modal ? config.modal : true;
	var title = config.title ? config.title : 'Retreatline';
	var msg = config.msg ? config.msg : 'lorem ipsum';
	var width = config.width ? config.width : 350;
	var height = config.height ? config.height : 150;
	var btnOk = config.btnOk ? config.btnOk : 'Ok';
	var isConfirm = config.isConfirm ? config.isConfirm : false;
	var d = document;
	if(modal){
		showOverlay();
	}
	
	var buttons = '<a href="javascript:;" class="button" onfocus="this.blur();" id="ModalOkAction"' +
		'<span class="l">&nbsp;</span' +
		'><input type="button" id="ModalOkActionBtn" value="'+ btnOk +'" ' +
		' /><span class="r">&nbsp;</span>' +
	'</a>';
	if(isConfirm){
		buttons = '<a href="javascript:;" class="button" onfocus="this.blur();" id="ModalYesAction">' +
			'<span class="l">&nbsp;</span' +
			'><input type="button" value="Da" ' + 
			'/><span class="r">&nbsp;</span>' +
		'</a> ' + '<a href="javascript:;" class="button" onfocus="this.blur();" id="ModalNoAction"' +
			'<span class="l">&nbsp;</span' +
			'><input type="button" value="Nu" ' + 
			'/><span class="r">&nbsp;</span>' +
		'</a>';
	}
	
	var htmlContent = '<div class="border-box-dre" style="width:'+ width +'px">'+
			'<div class="top-left">'+
				'<div class="top-right" style="height:'+ height +'px">'+
					'<div class="popup_title">' + '<h5>'+title+'</h5>' + 
						'<a href="javascript:;" onfocus="this.blur();" id="closeModalDlg">&nbsp;</a>' +
						'<div class="clear"></div>' +
					'</div>' +
					'<div class="popup_content" id="ctDialog">' + msg+ '</div>' +
				//	buttons +
					'<div class="popup_actions">' + buttons+ '</div>' +
				'</div>'+
			'</div>'+
			'<div class="bottom-left">'+
				'<div class="bottom-right">'+
				'</div>'+
			'</div>'+
		'</div>';
	
	var yScroll = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	 
	var modalWin = d.createElement('div');
	modalWin.id = 'modalBox';
	modalWin.className = 'modal-box';
	modalWin.style.top = "50%";
	modalWin.style.left = "50%";
	modalWin.style.marginLeft = "-" + width/2 + "px";
	modalWin.style.marginTop = (yScroll - height/2) + "px";
	modalWin.innerHTML = htmlContent;
	d.body.insertBefore(modalWin, d.body.firstChild); // not what I want
	
	$(window).scroll(function () {
		var yScroll = this.pageYOffset || this.document.body.scrollTop || this.document.documentElement.scrollTop;
		modalWin.style.marginTop = (yScroll - height/2) + "px";
	});

//	events	
	var listeners = config.listeners ? config.listeners : null;
	$("#closeModalDlg").click(function (){
		if(listeners && listeners['close']){
			listeners['close']['fn']();
		}
		closeModal();
	});
	$("#ModalOkAction").click(function(){
		var rez = true;
		if(listeners && listeners['ok']){
			var rez = listeners['ok']['fn']();
		}
		if(rez){
			closeModal();
		}
    });
	$("#ModalNoAction").click(function(){
		closeModal();
		if(listeners && listeners['no']){
			listeners['no']['fn']();
		}
    });
	$("#ModalYesAction").click(function(){
		var rez = true;
		if(listeners && listeners['yes']){
			rez = listeners['yes']['fn']();
		}
		if(rez){
			closeModal();
		}
    });
	
	//	document.body.style.overflowY = 'hidden';
	//	document.getElementById("closeModalDlg").focus();
	if(config.isWindow && config.fileName){
		var url = web_path + 'ajax/ajax.php?l=' + config.fileName;
		$.ajax({
			type: "POST",
			url: url,
			data: {
				fileName: config.fileName,
				method: 'getWindowTemplateFile'
			},
			beforeSend: function(){
				document.getElementById('ctDialog').innerHTML = "please wait...";
			},
			success: function(msg){
				var msg = eval('(' + msg + ')');
				document.getElementById('ctDialog').innerHTML = msg['data'];
				if(listeners && listeners['afterLoad']){
					listeners['afterLoad']['fn']();
				}
			}
		});
	}
}


function closeModal(){
	if(document.getElementById('overlay')){
		window.document.body.removeChild(document.getElementById('overlay'));
	}
	window.document.body.removeChild(document.getElementById('modalBox'));
}

function getDocHeight() {
	var D = document;
	return Math.max(
		Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
		Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
		Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}









