jQuery.noConflict();

jQuery(document).ready(function() {    //Document Ready
 	 	
 	 	
 //google stuff
 
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-5773560-5']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
  
// Placeholder para IE y otros...

	if (!Modernizr.input.placeholder) 
		{
			jQuery("#placer").focus(function() 
				{
					if (jQuery("#placer").val() == jQuery("#placer").attr('placeholder'))
						{
							jQuery('#placer').val("");
						}
				});
			jQuery("#placer").blur(function() 
				{
					if(jQuery("#placer").val()=="") 
						{
							jQuery("#placer").val(jQuery("#placer").attr('placeholder'));;
				
						}
				});
			jQuery("#placer").blur();
		}
		
		
});


//  Validación del Formulario de contacto
	//Versión 2.3 / Manuel Martín - Proyecto 12

//name - email - asunto - telefono - mensaje

function validar(form)
	{
		fail = validarNombre(form.name.value)
		fail += validarMail(form.email.value)
		fail += validarAsunto(form.asunto.value)
		fail += validarTelefono(form.telefono.value)
		fail += validarMensaje(form.mensaje.value)
	
		if(fail =="") 
			{ 
				name = form.name.value
				email = form.email.value
				asunto = form.asunto.value
				telefono = form.telefono.value
				mensaje = form.mensaje.value
				enviarmail(name,email,telefono,asunto,mensaje) 
				return false;
			
			}
		else { addPopup("<p>" + fail + "</p>"); //utilizamos la función para sacar el pop up
			 return false;
 			}
	
	}

	function validarNombre(field)
		{
			if(field=="") return "Error: Olvidó colocar su nombre <br />"
			return ""
		}
	function validarMail(field)
		{
			if(field=="") return "Error: Olvidó colocar su Correo <br />"
			else if (!( (field.indexOf(".") > 0) ) && (!(field.indexOf("@") > 0) ) || /[^a-zA-Z0-9.@_-]/.test(field) )
				return "Error: El formato de su correo es inválido <br />";
			return ""
		}
	function validarAsunto(field)
		{
			if(field=="") return "Error: Olvidó colocar el asunto  <br />"
			return ""
		}
	function validarTelefono(field)
		{
			if(field=="") return ""
			else if (/[a-zA-Z]/.test(field)) return "Error: El teléfono sólo puede contener números, guiones (_ -) y paréntesis. <br />"
			return ""
		}
	function validarMensaje(field)
		{
			if(field=="") return "Error: Olvidó colocar el mensaje <br />"
			else if (field.length < 10) return "Necesitamos más datos en el mensaje para poder responder adecuadamente  <br />" 
			return ""
		}

//Ajax Call para validad el formulario

 //Geting the data from the ajaxRequest for responseText or responseXML <-- chanching any of those things
 
 
 

function enviarmail(name,email,telefono,asunto,mensaje)
{
	

nocache = "&nocache=" + Math.random() * 1000000
request = new ajaxRequest()
request.open("GET","http://www.graberdecorama.com/portal/wp-content/themes/graber2/mail.php?" + "name=" + name + "&email=" + email + "&telefono=" + telefono + "&asunto=" + asunto + "&mensaje=" + mensaje + nocache,true)

request.onreadystatechange = function()
 	{
 		if(this.readyState == 4)
 			{
 				if(this.status == 200)
 					{
 						if(this.responseText != null)
 							{
 								addPopup("<p> Su mensaje se ha enviado correctamente</p> <p>Uno de nuestros asesores se pondrá en contacto con usted, muchas gracias</p>"); // Mensajtio
 							}
 						else alert("Ajax Error: No se recibieron los datos." )
 					}
 				else alert("Ajax Error: " + this.statusText)
 			}
 	}
 	
 	// after his we do
 	
 	request.send(null)  // if we are using get, if we use post, we put the string with the value name
 	
}
 function ajaxRequest()
 	{
 		try // for real browsers (non IE)
 			{
 				var request = new XMLHttpRequest()
 			}
 		catch(e1) // for IE6 + suckwers 
 			{
 				try
 					{
 						request = new ActiveXOBject("Msxml2.XMLHTTP")
 					}
 				catch(e2)
 					{
 						request = false
 					}
 			}
 		return request
 	}
 	
// Pop-up para mensaje enviado, JQuery = $, estoy usando el modo seguro de JQuery para wordpresss

function addPopup(notice) {
			jQuery('<div class="notice"></div>')
				.append('<div class="skin"></div>')
				.append('<a href="#" class="x">X</a>')
				.append(jQuery('<div class="content"></div>').html(jQuery(notice)))
				.hide()
				.appendTo('#pop')
				.fadeIn(1000);
			jQuery('#pop')  //busca el objeto que nos sirve de locker para el popup
				.find('.x')  // buscamos el botón de cerrar por su clase
				.live('click', function() {  // on click...
					jQuery(this)
						.closest('.notice')
						.animate({
							border: 'none',
							height: 0,
							marginBottom: 0,
							martinTop: '-6px',
							opacity: 0,
							paddingBottom: 0,
							paddingTop: 0,
							queue: false
						}, 500, function(){
							jQuery(this).remove();
						});
				})
		}
		
// Media Queries adaptación

