function sendContact(){
    var _contact_name = document.getElementById('contact_name').value;    
    var _contact_email = document.getElementById('contact_email').value;
    var _contact_comment = document.getElementById('contact_comment').value;
    var _contact_type = document.getElementById('contact_type').value;    
    var ok = true;
    if(ok){
    	document.getElementById('send_contact').setAttribute("onclick","return false;");  
        $.ajax({
           url: base_url+"js/sendContact.php",
           type: "POST",
           dataType: "text",
           data: {
        	    contact_name:		_contact_name,
        	    contact_email: 		_contact_email,
        	    contact_comment: 	_contact_comment, 
        	    contact_type:		_contact_type
           },
           error: function(req, err, obj) {
            alert(err);
           },
           success: function(txt) {
                if(txt == "OK"){
                	alert("Formulario enviado correctamente.");
                	document.getElementById('send_ok').style.display="block";
                   }else{
                       if(txt == "ERROR"){
                           document.getElementById('error_contact').style.display="block";
                       }else{
                    	    alert("Formulario enviado correctamente");

                       }                        
                   }
           }
        });
    }
    return false;
}
/** *
 * showNew
 * Efecto añadido en los botones de un listado contenido en capas, para pasar de una a otra con efecto Slide
 * */
function showNew(i,actual,dir){
	if(dir=='L'){
		$('#new_'+actual).hide("slide", { direction: "left"}, 1000);
		$('#new_'+i).show("slide", { direction: "right" }, 1000);
		$('#new_points_'+i).show();
		$('#new_points_'+actual).hide();
	}else if(dir=='R'){
		$('#new_'+actual).hide("slide", { direction: "right"}, 1000);
		$('#new_'+i).show("slide", { direction: "left" }, 1000);	
	}


}
/**
 * expandNew
 * 
 *  */
function expandNew(target,action){
	if(action=='mostrar'){
		$('#'+target+'_s').css("display", "none");
		$('#'+target+'_l').slideDown(400);		
	}else{
		$('#'+target+'_l').slideUp(400);
		$('#'+target+'_s').css("display", "block");	
	}	
}
/** *
 * showMenu
 * Manejar los efectos de ocultar y mostrar del menú que CSS no puede manejar
 * */
function showMenu(actual){
	$('ul#areas').hide();
	$('ul#nosotros').hide();
	$('ul#desarrollo').hide();
	$('ul#noticias').hide();
	$('ul#contacto').hide();
	if(actual!=''){
		$('ul#'+actual).show();			
	}
}



/**
 *  function openWindow
 * Carga una página en el sitio indicado.
 * @params _file: fichero a cargar
 * @params option:
 * 			overlay: ventana emergente
 * 			reload: recarga el contenido de la página principal
 * 			section: sección donde cargar la página
 * */
function openWindow(_file,option,section){
	if(section=="" || typeof section=="undefined"){ section='new_window';}
	_file=base_url+""+_file;
	//alert(section);
	if(section=='new_window'){
		$('div#block_background').fadeIn(350, function(){
			$('div#new_window').fadeIn(350, function(){
				$(function() {
					if(option=="overlay"){
						$("div#new_window").draggable({handle:"div.new_windows_top"});
					}				
					
				});
				loadWindow(_file,section,option);
			}); 
		});
	}else{
		loadWindow(_file,section,option);
	}
}
/**
 * function closeWindow
 * Cierra una página. Normalmente las emergentes
 * @param option:
 * 			no_reload: cierra la ventana emergente sin recargar
 * */
function closeWindow(option){
	$('div#new_window').fadeOut(350, function(){
		$('div#block_background').fadeOut(350, function(){
			document.getElementById('new_window').innerHTML ="";
			if(option!="no_reload"){
				reloadWindow();				
			}
		}); 
	}); 
	$('div#new_popup').fadeOut(350, function(){
		$('div#block_background').fadeOut(350, function(){
			document.getElementById('new_popup').innerHTML ="";
			if(option!="no_reload"){
				reloadWindow();				
			}
		}); 
	}); 
}
/**
 *  function reloadWindow
 * Recarga una página. Normalmente el contendio de la página pricnipal
 * */
function reloadWindow() {
	if(contenido!="/"){
		loadWindow(contenido, 'contenidosmain','reload');		
	}

}
/**
 *  FUNCTION loadWindow y LoadW3
 * Cargar una página.
 * @param url: dirección de la página a cargar
 * @param target: div donde cargar el contenido
 * @param option:
 * 			@param overlay: ventana emergente
 * 			@param reload: recarga el contenido de la página principal (normalmente al guardar cambios)
 * */
function loadWindow(url, target, option) {
	var _option_close;	
	if(option=="overlay" || option=="reload" || option=="no_header"){
		url=url+"&window_option=no_headers";
	}
	if(option=="reload"){
		_option_close="";		
	}else{
		_option_close="no_reload";		
	}

	if(option=="overlay"){
		$('#'+target).html("<div class=\"new_windows_top\"><a href=\"#\" style='margin-right:20px;' onclick=\"closeWindow('"+_option_close+"');return false;\">"+dic_close+"</a></div><div class='new_window_content'>"+" Loading, please wait...</div>");
	}else{
		$('#'+target).html(' Loading, please wait...');
	}
    
    if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (req != undefined) {
    req.onreadystatechange = function() {LoadW3(url, target, option);};

    req.open("GET", url, true);
    req.send("");
    }
}
     
function LoadW3(url, target, option) {
	var _option_close;
	var t;
    if (req.readyState == 4) { // only if req is "loaded"
    	if (req.status == 200) { // only if "OK"
    		if(option=="reload"){
    			_option_close="";		
    		}else{
    			_option_close="no_reload";		
    		}    		
    		if(option=="overlay"){
    			$('#'+target).html("<div class=\"new_windows_top\"><a href=\"#\" style='margin-right:20px;' onclick=\"closeWindow('"+_option_close+"');return false;\">"+dic_close+"</a></div><div class='new_window_content'>"+req.responseText+"</div>");
    			//t = setTimeout("j_style_1()", 1000); //no la ejecuta en el instante solo pasados 1/10 segundos porque aun no tiene todo el HTML	
    		}else{
    			$('#'+target).html(req.responseText);
    		}
    		
    	} else {
    		if(option=="overlay"){
    			$('#'+target).html("<div class=\"new_windows_top\"><a href=\"#\" style='margin-right:20px;' onclick=\"closeWindow('"+_option_close+"');return false;\">"+dic_close+"</a></div><div class='new_window_content'>"+" [LOAD Error]:\n"+ req.status + "\n" +req.statusText+"</div>");
    		}else{
    			$('#'+target).html(" [LOAD Error]:\n"+ req.status + "\n" +req.statusText);
    		}
    		
    	}
    }
}

/**
 * sendNewsletterUser
 * Guarda el destinatario de newsletter
 *  */
function sendNewsletterUser(){
	
    var _id = document.getElementById('id').value;
    var _email = document.getElementById('email').value;
    $('#email').removeClass("input_error");
    var _name = document.getElementById('name').value; 
    $('#name').removeClass("input_error");    
    var _surname = document.getElementById('surname').value;
    $('#surname').removeClass("input_error"); 
    var _email = document.getElementById('email').value;
    $('#email').removeClass("input_error");    
    var _cat = document.getElementById('cat').value;
    $('#cat').removeClass("input_error");  
    
    var ok = true;    
    if(cm_is_empty(_name)){
    	showError('name');    	
        ok=false;
    } 
    if(cm_is_empty(_email)){  	
        document.getElementById('error_email_req').style.display="block";
        $('#email').addClass("input_error");
        ok=false;
    }else{
    	 if(!cm_is_email(_email)){
	        document.getElementById('error_email_format').style.display="block";
	        $('#email').addClass("input_error");
	        ok=false;    	 
    	 }
    }    
    if(ok && _id!=''){	
        $.ajax({
           url: base_url+"js/sendNewsletterUser.php",
           type: "POST",
           dataType: "text",
           data: {
        		id:						_id,
        		cat:					_cat,
        		name:					_name,
        		surname:				_surname,
        		email:					_email
           },
           timeout : 20000,
           tryCount : 0,
           retryLimit : 3,
           error : function(xhr, textStatus, errorThrown ) {
               if (textStatus == 'timeout') {
                   this.tryCount++;
                   if (this.tryCount <= this.retryLimit) {
                       //try again
                       $.ajax(this);
                       return;
                   }
                   alert('We have tried ' + this.retryLimit + ' times and it is still not working. We give in. Sorry.');
                   return;
               }
				if(xhr.status==0){
					alert('You are offline!!\n Please Check Your Network.');
				}else if(xhr.status==404){
					alert('Requested URL not found.');
				}else if(xhr.status==500){
					alert('Internel Server Error.');
				} 
				if(textStatus=='parsererror'){
					alert('Error.\nParsing Request failed.');
				}else if(textStatus=='timeout'){
					alert('Request Time out.');
				}else {
					alert('Unknow Error.\n'+xhr.responseText);
				}
           },   
           success: function(txt) {
                if(txt == "OK"){
                	window.location.replace(window.location.href);
                }else{
                	if(txt == "ERROR"){
                		document.getElementById('error').style.display="block";
                	}else{
                       	document.getElementById('error_crass').style.display="block";
                        document.getElementById('send').setAttribute("onclick","return false;");  
                	}                        
                }
           }
        });
    	
        return true;
    }else{
    	document.getElementById('error_gen').style.display="block";
        return false;    	
    }
}


/**
 * sendNewsletterUser
 * Guarda el destinatario de newsletter
 *  */
function sendRates(){
	
    var _id = document.getElementById('id').value;
    var _email = document.getElementById('email').value;
    $('#email').removeClass("input_error");
    var _name = document.getElementById('name').value; 
    $('#name').removeClass("input_error");    
    var _email = document.getElementById('email').value;
    $('#email').removeClass("input_error");    
    var _comment = document.getElementById('comment').value;
    $('#comment').removeClass("input_error");    
    var _type = document.getElementById('type').value;
    $('#type').removeClass("input_error");  
    
    var ok = true;    
    if(cm_is_empty(_name)){
    	showError('name');    	
        ok=false;
    } 
    if(cm_is_empty(_email)){  	
        document.getElementById('error_email_req').style.display="block";
        $('#email').addClass("input_error");
        ok=false;
    }else{
    	 if(!cm_is_email(_email)){
	        document.getElementById('error_email_format').style.display="block";
	        $('#email').addClass("input_error");
	        ok=false;    	 
    	 }
    }    
    if(ok && _id!=''){	
        $.ajax({
           url: base_url+"js/sendRate.php",
           type: "POST",
           dataType: "text",
           data: {
        		id:						_id,
        		comment:				_comment,
        		name:					_name,
        		email:					_email,
        		type:					_type
           },
           timeout : 20000,
           tryCount : 0,
           retryLimit : 3,
           error : function(xhr, textStatus, errorThrown ) {
               if (textStatus == 'timeout') {
                   this.tryCount++;
                   if (this.tryCount <= this.retryLimit) {
                       //try again
                       $.ajax(this);
                       return;
                   }
                   alert('We have tried ' + this.retryLimit + ' times and it is still not working. We give in. Sorry.');
                   return;
               }
				if(xhr.status==0){
					alert('You are offline!!\n Please Check Your Network.');
				}else if(xhr.status==404){
					alert('Requested URL not found.');
				}else if(xhr.status==500){
					alert('Internel Server Error.');
				} 
				if(textStatus=='parsererror'){
					alert('Error.\nParsing Request failed.');
				}else if(textStatus=='timeout'){
					alert('Request Time out.');
				}else {
					alert('Unknow Error.\n'+xhr.responseText);
				}
           },   
           success: function(txt) {
                if(txt == "OK"){
                	window.location.replace(window.location.href);
                }else{
                	if(txt == "ERROR"){
                		document.getElementById('error').style.display="block";
                	}else{
                       	document.getElementById('error_crass').style.display="block";
                        document.getElementById('send').setAttribute("onclick","return false;");  
                	}                        
                }
           }
        });
    	
        return true;
    }else{
    	alert("ID:"+_id);
    	document.getElementById('error_gen').style.display="block";
        return false;    	
    }
}
/**
 *  function openPopup
 * Carga un contenido en el sitio indicado.
 * @params _file: fichero a cargar
 * @params option:
 * 			overlay: ventana emergente
 * 			reload: recarga el contenido de la página principal
 * 			section: sección donde cargar la página
 * */
function openPopup(_content,section,title){
	var new_content="<div class=\"new_popup_top\" id=\"new_popup_top\"><div class='new_popup_title' >"+title+"</div><a href=\"#\" style='margin-right:20px;' onclick=\"closeWindow('no_reload');return false;\">"+dic_close+"</a></div><div class='new_popup_content' id='new_popup_content'>" +
		"<div>"+_content+"</div></div>";
	if(section=="" || typeof section=="undefined"){ section='new_window';}
		$('div#block_background').fadeIn(350, function(){
			$('div#'+section).fadeIn(350, function(){
				$(function() {
						$("div#"+section).draggable({handle:"div.new_popup_top"});
						$("div#"+section).html(new_content);
						document.getElementById('new_popup_content').style.display="none";
						//setTimeout("$('#new_popup_top').show('slide', { direction: 'left'},1000)",100);
						setTimeout("$('#new_popup_content').show('slide', { direction: 'up'},1000)",100);
						//setTimeout("$('#new_popup').show('slide', { direction: 'up'},1000)",1100);
						//$('#new_popup_content').show('slide', { direction: 'up'},500)
						setTimeout("$('#new_popup_content').effect('bounce', { times:3, distance: 4 }, 200)",1100);						
						//setTimeout("$('#new_popup_content').show('slide', { direction: 'up'},500)",1000);
						//setTimeout("$('#new_popup_content').effect('bounce', { times:3, distance: 4 }, 200)",2200);
				});
			}); 
		});
}

/**
 * showLayer
 * Muestra u oculta una capa y personalizado el efecto
 *  */

function showLayer(action,section,effect){
	if($('#'+section)){
		if(effect=='fade'){
			if(action=='mostrar'){
				$('#'+section).fadeIn("600");		
			}else{
				$('#'+section).fadeOut("600");
			}		
		}else if (effect=='slide'){
			if(action=='mostrar'){
				$('#'+section).show("slide", { direction: "up" }, 1000);	
			}else{
				$('#'+section).hide("slide", { direction: "up"}, 1000);
			}		
		}else if (effect=='nofx'){
			if(action=='mostrar'){
				$('#'+section).css("display","block");	
			}else{
				$('#'+section).css("display","none");	
			}		
		}		
	}
	return false;	
}
/**
 * showHelp
 * Muestra u oculta la ayuda
 *  */
function showHelp(action,section){
	showLayer(action,section+'_content','fade');
	return false;
}
