﻿
var loaded = new Array();
var time;
var timer = 0;
var stop = true;
var ajax=nuevoAjax();

var timeout = false;

function showPopIn(id){
	loaded[id] = true;
	document.getElementById('popin_'+id).style.display = 'block';
}
function hidePopIn(id){
	loaded[id] = false;
	document.getElementById('popin_'+id).style.display = 'none';
}
var ajaxGlb;
var numGlb = new Array(false,false,false,false,false,false);
var ocu = false;
function canviCuadros(){
	var ids_actius3 = '';
	var ids_actius4 = '';
	var error = 0;
	timer ++;
	//throw new Error('timer: '+timer);
	if (time == 50)	clearTimeout(time);
	
	
	timeout = true;
	ajax.open("POST", "http://www.eroticbibletoeurope.com/ajax_home.php",true);
	
	ids_actius2 = document.getElementsByName('hn');
	
	for (i=1;i<=6;i++){
		if (document.getElementById('h_'+i))
			ids_actius4 += document.getElementById('h_'+i).value;
		else
			error = 1;
		if (i<6)
			ids_actius4 += ',';
			
	}
	//ids_actius4 = document.getElementById('h_1').value+','+document.getElementById('h_2').value+','+document.getElementById('h_3').value+','+document.getElementById('h_4').value+','+document.getElementById('h_5').value+','+document.getElementById('h_6').value;
	if (!error){	
		for (i=0;i<=ids_actius2.length-1;i++){
			ids_actius3 += ','+ids_actius2.item(i).value;
		}
		
		num = Math.floor(Math.random()*6);
		num++;
		if (loaded[num]){
			num++;
			num = num % 5;
		}
		ajax.onreadystatechange=function() {
			if (ajax.readyState==4) {
				if(ajax.responseText != "0"){
					for (f=0;f<6;f++){
						if (!numGlb[f]){
							numGlb[f] = num;
							break;
						}
					}
					ajaxGlb = ajax;
					if (document.getElementById("div_"+num) && !ocu){
						loaded[num] = true;
						ocu = true;
						document.getElementById("div_"+num).id = "old_div_"+num;
						$("#old_div_"+num).hide( "fade", {}, 2000);
						
						
						time = setTimeout("wait("+f+")",1500);
						//if 
						
						//$("#div_"+num).show( "fade", {}, 2000);
						//document.getElementById("old_div_"+numGlb).removeElement;
						/*
						$("#div_"+num).hide( "fade", {}, 2000, wait()); 
						
						
						//while (document.getElementById("div_"+num).visible == '')
						//$("#div_"+num).show( "fade", {}, 2000);
						
						document.getElementById(numGlb).innerHTML += ajaxGlb.responseText;
						
						//while (document.getElementById("div_"+num).opacity != '0'){
						//}
						document.getElementById(num).innerHTML = ajax.responseText;
						$("#div_"+num).show( "fade", {}, 2000, wait());
						*/ 
											
						star_status[num] = 0;
						if (stop)	time = setTimeout("canviCuadros()", 3000);//amb 3000 va perfecte
					}
				}
			}
		}
		ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		
		if (document.getElementById('search')){
			if (document.getElementById('search').value == "search")
				ajax.send("ids="+ids_actius3+"&div_i="+num+"&search=1");
		}else{
			ajax.send("ids="+ids_actius3+"&div_i="+num);
		}
		//if (stop)	time = setTimeout("canviCuadros()", 3000);//amb 3000 va perfecte
		//throw new Error('timer: '+timer);
		
	}else{
		time = setTimeout("canviCuadros()", 3000);//amb 3000 va perfecte
	}
}
function wait(ff){
	if (document.getElementById(numGlb[ff]) && stop){
		document.getElementById(numGlb[ff]).innerHTML = ajaxGlb.responseText;
		$("#div_"+numGlb[ff]).show( "fade", {}, 2000);
		loaded[numGlb[ff]] = false;
		ocu = false;
		
		if (document.getElementById('h_'+numGlb[ff])){
			num_cokie = document.getElementById('h_'+numGlb[ff]).value;
			var cooqui = getCookie('ebe_'+num_cokie);
			
			if (!cooqui){
				document.getElementById('stars_wrapper_si_'+numGlb[ff]).style.display = 'none';
				document.getElementById('stars_wrapper_si_'+numGlb[ff]).innerHTML = "";
			}else{
				document.getElementById('stars_wrapper_no_'+numGlb[ff]).style.display = 'none';
				document.getElementById('stars_wrapper_no_'+numGlb[ff]).innerHTML = "";
			}	
		}
		numGlb[ff] = false;
		//document.getElementById("old_div_"+numGlb).removeElement;

		//document.getElementById("div_2").innerHTML= "Arnau ";
		//alert(ajaxGlb.responseText);
		//$("#old_div_"+numGlb).hide( "fade", {}, 2000);
		//document.getElementById("old_div_"+numGlb).removeElement;
		
		//$("#div_"+numGlb).show( "fade", {}, 2000);
	}
} 
function nuevoAjax(){
	var xmlhttp=false;
 	try {
 		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 	} catch (e) {
 		try {
 			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
 		} catch (E) {
 			xmlhttp = false;
 		}
  	}

	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
 		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}
/*funciones de navegacion fichas de places*/

function mostrarFicha(id_place){
	var error = 0;
	ajax2=nuevoAjax();
	ajax2.open("POST", "http://www.eroticbibletoeurope.com/ajax_place.php",true);
	clearTimeout(time);
	stop = false;
	
			var cooqui = getCookie('ebe_'+id_place);
			//alert("place:"+id_place+" - cokie:"+cooqui);
			/*if (!cooqui)
				alert("no cuck");
			else
				alert("cokki");
	*/
	ajax2.onreadystatechange=function() {
		if (ajax2.readyState==4) {
			if(ajax2.responseText != "0"){
				//alert(ajax2.responseText);
				//document.getElementById("mid_body").style.display = 'none';
				//document.getElementById("search_results").style.display = 'none';
				//document.getElementById("search").style.display = 'none';
				document.getElementById('mid_body').innerHTML = ajax2.responseText;
				ajaxSlide();
				numVotes(document.getElementById('num_votes').value);
			}
		}
	}
	ajax2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax2.send("ids="+id_place+"&coki="+cooqui);
}

 

/* Fin funciones de navegacion fichas de places*/


/*
 *
 *	Funciones de comprovación de formulario
 *
*/

function checkForm() {
	document.getElementById("form_message").style.border = '1px solid #000';
	document.getElementById("form_email").style.border = '1px solid #000';
	document.getElementById("form_object").style.border = '1px solid #000';
	document.getElementById("form_name").style.border = '1px solid #000';
	var i = 0;
	var error_msg='';
	
	
	if (isNull(document.getElementById("form_message").value)==true){
		document.getElementById("form_message").focus();
		document.getElementById("form_message").style.border = '1px solid red';
		error_msg = 'Debes dejar tu message';
		i++;}
	if (isNull(document.getElementById("form_email").value)==true || validate_mail(document.getElementById("form_email").value)==false){
		document.getElementById("form_email").focus();
		document.getElementById("form_email").style.border = '1px solid red';
		error_msg = 'Debes dejar tu e-mail';
		i++;}
	if (document.getElementById("form_object").options[document.getElementById("form_object").selectedIndex].value==-1){
		document.getElementById("form_object").focus();
		document.getElementById("form_object").style.border = '1px solid red';
		error_msg = 'Debes dejar tu object';
		i++;}
	if (isNull(document.getElementById("form_name").value)==true){
		document.getElementById("form_name").focus();
		document.getElementById("form_name").style.border = '1px solid red';
		error_msg = 'Nombre incorrecto';
		i++;}
	
	
	if (i > 0) { 
		//document.getElementById('errMsg_donde').innerHTML = error_msg;
		//document.getElementById('errMsg_donde').setStyle('display','block');
		return false;
	}else{
		//document.getElementById('errMsg_donde').setStyle('display','none');
		return true;
	}
}

function validate_mail(field){
	var re = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	//var re = /^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+\.[a-zA-Z]{2,4}(?:\?[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)?)?(?:\&[a-zA-Z0-9_]+\=[a-zA-Z0-9_]+)*)$/;
	//var re = /^(http\:\/\/[a-zA-Z0-9_\-]+(?:\.[a-zA-Z0-9_\-]+)*\.[a-zA-Z]{2,4}(?:\/[a-zA-Z0-9_]+)*(?:\/[a-zA-Z0-9_]+(?:\?[a-zA-Z0-9_\-]+\=[a-zA-Z0-9_\-]+)?)?(?:\&[a-zA-Z0-9_\-]+\=[a-zA-Z0-9_\-]+)*)$/;
	var resultat = field.match(re);
	if(!resultat) {	return(false);
	} else {		return(true);}
}
function isNull(valor) {
	if (valor == null || valor == "" || valor == "-1") {	return(true);
	} else {							return(false);}
}

function posarNom(nom, camp){
	if (camp.value == '')
		camp.value = nom;
}
function treureNom(nom, camp){
	if (camp.value == nom)
		camp.value = '';
}

/*Funcio de cerca en ajax*/
function newSearch(){
	search_form = document.getElementById('search_form');
	searching = document.getElementById('searching');
	search_results = document.getElementById('search_results');

	ajax3=nuevoAjax();
	ajax3.open("POST", "http://www.eroticbibletoeurope.com/ajax_search.php",true);
	
	search_results.style.display = 'none';
	searching.style.display = 'block';
	
	name = document.getElementById('name').value;
	city = document.getElementById('city').value;
	category = document.getElementById('category').value;
	
	ajax3.onreadystatechange=function() {
		if (ajax3.readyState==4) {
			if(ajax3.responseText != "0"){
				stop = true;
				document.getElementById('mid_body').innerHTML = ajax3.responseText;
				star_status[1] = 0;
				star_status[2] = 0;
				star_status[3] = 0;
				star_status[4] = 0;
				star_status[5] = 0;
				star_status[6] = 0;
				
				for (it=1;it<=6;it++){
					if (document.getElementById('h_'+it)){
						var num_cokie = document.getElementById('h_'+it).value;
						
						var cooqui = getCookie('ebe_'+num_cokie);
						//alert("it:"+it+" - place:"+num_cokie+" - cokie:"+cooqui);
						if (!cooqui){
							document.getElementById('stars_wrapper_si_'+it).style.display = 'none';
							document.getElementById('stars_wrapper_si_'+it).innerHTML = "";
						}else{
							document.getElementById('stars_wrapper_no_'+it).style.display = 'none';
							document.getElementById('stars_wrapper_no_'+it).innerHTML = "";
						}
					}
				}
				time = setTimeout("canviCuadros()", 1000);
			}
		}
	}
	ajax3.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	name = "";
	city = "-1";
	category = "-1";
	ajax3.send("");
	
	searching.style.display = 'none';
	search_form.style.display = 'block';
	
}
function searchCuadros(){
	ajax4=nuevoAjax();
	ajax4.open("POST", "http://www.eroticbibletoeurope.com/ajax_search.php",true);
	
	search_form = document.getElementById('search_form');
	searching = document.getElementById('searching');
	search_results	= document.getElementById('search_results');
	
	search_form.style.display = 'none';
	searching.style.display = 'block';
	
	name = document.getElementById('name').value;
	city = document.getElementById('city').value;
	category = document.getElementById('category').value;
	
	if (name != "Name" || city != "-1" || category != "-1"){
		ajax4.onreadystatechange=function() {
			if (ajax4.readyState==4) {
				if(ajax4.responseText != "0"){
					document.getElementById('mid_body').innerHTML = ajax4.responseText;
					//Falta saber
					
					stop = false;
				}
			}
		}
		ajax4.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax4.send("name="+name+"&city="+city+"&category="+category);
	}
	
	searching.style.display = 'none';
	search_results.style.display = 'block';
	stop = true;
	time = setTimeout("canviCuadros()", 1000);
}
function searchList(){
	ajax5=nuevoAjax();
	ajax5.open("POST", "http://www.eroticbibletoeurope.com/ajax_search_list.php",true);
	
	search_form = document.getElementById('search_form');
	searching = document.getElementById('searching');
	search_results	= document.getElementById('search_results');
	
	search_form.style.display = 'none';
	searching.style.display = 'block';
	
	clearTimeout(time);
	
	name = document.getElementById('name').value;
	city = document.getElementById('city').value;
	category = document.getElementById('category').value;
	
	if ((name != "Name" && name !="")|| city != "-1" || category != "-1"){
		ajax5.onreadystatechange=function() {
			if (ajax5.readyState==4) {
				if(ajax5.responseText != "0"){
					document.getElementById('mid_body').innerHTML = ajax5.responseText;
					star_status[1] = 0;
					star_status[2] = 0;
					star_status[3] = 0;
					star_status[4] = 0;
					star_status[5] = 0;
					star_status[6] = 0;
					
					
					for (it=1;it<=6;it++){
						if (document.getElementById('h_'+it)){
							var num_cokie = document.getElementById('h_'+it).value;
							
							var cooqui = getCookie('ebe_'+num_cokie);
							//alert("it:"+it+" - place:"+num_cokie+" - cokie:"+cooqui);
							if (!cooqui){
								document.getElementById('stars_wrapper_si_'+it).style.display = 'none';
								document.getElementById('stars_wrapper_si_'+it).innerHTML = "";
							}else{
								document.getElementById('stars_wrapper_no_'+it).style.display = 'none';
								document.getElementById('stars_wrapper_no_'+it).innerHTML = "";
							}
						}
					}
					
				}
			}
		}
		ajax5.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		ajax5.send("name="+name+"&city="+city+"&category="+category+"&p=0");
	}
	
	searching.style.display = 'none';
	search_results.style.display = 'block';
}



/*
Funciones de valoracion con estrellitas o corazones
*/
var star_status = new Array();
star_status[1] = 0;
star_status[2] = 0;
star_status[3] = 0;
star_status[4] = 0;
star_status[5] = 0;
star_status[6] = 0;
var num_votes;
var votes_add = false;
var base_star_value = 0;

var star_preset = new Array();
star_preset[1] = new Array('star_off','star_off','star_off','star_off','star_off');
star_preset[2] = new Array('star_off','star_off','star_off','star_off','star_off');
star_preset[3] = new Array('star_off','star_off','star_off','star_off','star_off');
star_preset[4] = new Array('star_off','star_off','star_off','star_off','star_off');
star_preset[5] = new Array('star_off','star_off','star_off','star_off','star_off');
star_preset[6] = new Array('star_off','star_off','star_off','star_off','star_off');

function numVotes(num){
	votes_add = true;
	num_votes = num;
}

function star_focus(x, y){
	if (star_status[y] == 0){
		for (i=1;i<=x;i++)	document.getElementById('star'+y+i).className = 'star star_hover';
	}
}

function star_select(pid, x, y){
	if (star_status[y] == 0){
		updateStarsCounter(pid, x, y);
	}
}
	
function star_losefocus(x, y){
	if (star_status[y] == 0){
		for (i=1;i<(x+1);i++)	document.getElementById('star'+y+i).className = 'star '+star_preset[y][i-1];
	}
}
function updateStarsCounter(pid, x, y){
	
	ajax6=nuevoAjax();
	ajax6.open("POST", "http://www.eroticbibletoeurope.com/ajax_rating.php",true);
	
	ajax6.onreadystatechange=function() {
		if (ajax6.readyState==4) {
			if (ajax6.responseText != -1) {
				var z =  Math.round(ajax6.responseText)
				var i = 0;
				var g = 0;
				for (i=1;i<=z;i++) 	document.getElementById('star'+y+i).className = 'star2 star_active';
				for (g=z+1;g<6;g++)	document.getElementById('star'+y+g).className = 'star star_off';
				star_status[y] = 1;
				setCookie( 'ebe_'+pid, '1', 30, '/', '', '');
				if (votes_add){
					num_votes++;
					document.getElementById('num_votes_span').innerHTML = num_votes;
				}
			}else
				alert("An error has occurred. Try again later");
		}
	}
	ajax6.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax6.send('pid='+pid+'&value='+x);
	
}

function initialize() {
	for (pid=1;pid<=6;pid++){
		base_star_value = document.getElementById('star_count_'+pid).value;
		var star_num = Math.round(base_star_value);
		for (var i = 0; i < star_num; i++) star_preset[pid][i] = 'star_active';
	}
}

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){
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ ){
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name ){
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 ){
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}	
}
/*
Fin - Funciones de valoracion con estrellitas o corazones
*/

