// JavaScript Document
function httpRequest()
{
    var xmlhttp;
    try {
        xmlhttp = new XMLHttpRequest();
    } catch (e) {
        xmlhttp = false;
    }
    if (!xmlhttp) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            if (!xmlhttp) {
                try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    xmlhttp = false;
                }
            }
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}



function sendMail(vars,validate,email)
{
	xmlHttp=httpRequest();
	if (xmlHttp==null)
	{
		alert ("Error! Su navegador no soporta peticiones HTTP. Actualice su navegador");
		return;
	}
	
	var params = "";
	var url="sendmail.php";
	var errors = 0;		
	
	for (i=0;i<validate.length;i++)
	{
		if((document.getElementById(validate[i]).value==0) || (document.getElementById(validate[i]).value.length==0) || (document.getElementById(validate[i]).value=="-"))
			errors++;
	}	
	
	if(errors!=0)
	{		
		document.getElementById("txtHintEmail").innerHTML = "<span style=\"color:#FF0000\">Rellene los campos requeridos</span>";
		return;
	}
	
	if(validEmail(document.getElementById(email).value) == false)
	{
		document.getElementById("txtHintEmail").innerHTML = "<span style=\"color:#FF0000\">Introduzca una direccion de email valida</span>";
		return;
	}
	
	for (i=0;i<vars.length;i++)
	{
		params += "&"+vars[i]+"="+document.getElementById(vars[i]).value;
	}
		params = params.substring(1);
	
	xmlHttp.onreadystatechange=sendMailChanged;
			
	xmlHttp.open("POST",url,true);
		
	//Send the proper header information along with the request	
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
		
	xmlHttp.send(params);
}

function sendMailChanged() 
{ 		
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 	{ 
 		document.getElementById("txtHintEmail").innerHTML = xmlHttp.responseText;
		document.forms["contactform"].reset();
	}
	else
	{
		document.getElementById("txtHintEmail").innerHTML = "Enviando. Espere...";		
	}
}


function toggleFormVisibility(id)
{
	if(document.getElementById(id).style.display == "none")
		document.getElementById(id).style.display = "block";
	else
		document.getElementById(id).style.display = "none";
}


function validEmail(email) 
{
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false)
   {
      return false;
   }
}

function setVisible(id) {
	
	//hideVisible(id);
	var e = document.getElementById(id);
       if(e.className == 'show_content')
	   {
          e.className = 'hide_content';
		  document.images[id+'_img'].src = "img/more_services.png";
	   }
       else
	   {
          e.className = 'show_content';
		  document.images[id+'_img'].src = "img/close_services.png";
	   }
 }

function hideVisible(id) {
		
		var divid = "";
		var i=0;
		var divs = document.getElementsByTagName("div");
		for(i=1;i<=divs.length;i++)
		{
			divid = '_container'+i;
			if((e=document.getElementById(divid)) && (divid!=id))
				e.className = 'hide_content';
			if(document.images[divid+'_img'])
				document.images[divid+'_img'].src = "img/more_services.png";
		}
}



function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

$(document).ready(function(){
	
	
	$(".number").keyup(function(){
		var value = $(this).val();
		$(this).val(value.replace(/[^0-9,]/ig,''));
	});	
	
	$(".number").blur(function(){
		var value = $(this).val();
		$(this).val(addCommas(value));
	});
	
	$(".number").focus(function(){
		var value = $(this).val();
		$(this).val(value.replace(/[,]/ig,''));
	});
		
	
	$("a.top5").hover(function(){
        var img = $(this).attr("id");
		$(this).css({color:"#FF9900"});
		$("#top5image").attr("src","images/"+img);		
      }, 
      function(){
		 $(this).css({color:"#1f6a91"}); 
      }
    );
	


//hide the all of the element with class msgbody
$(".msgbody").hide();
//toggle the componenet with class msgbody
$(".msghead").click(function()
{
$(this).next(".msgbody").slideToggle(600);
});
							   
});

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}

//dynamic content
var loadContent = function(url,title)
{
	var target = "dyncontent";
	if(!title)
	 title = "Karolyi Gardens Management Service";
	$(".text_block_title").html("Loading, please wait...");	
	$.ajax({
		  url: url,
		  cache: false,
		  success: function(html){
			$(".text_block_title").html(title);
			$("#"+target).html(html).hide().fadeIn("slow");
			tb_init('a.thickbox, area.thickbox, input.thickbox');
			if($("#flashcontent").length > 0)
				$.getScript("js/video.js");
		}
	});
}
