var j=0;

$(document).ready(function(){
    
	//Top Header Show/Hide function
	maxHeight = 60;
    minHeight = 0;
	minWidth= 0;
	maxWidth =563;
	
	
    $("#toggle").click(function () {
		var cur_l = $('#content').css('left');
		//alert(cur_l);
		if(cur_l == "560px") {
			$("#content").animate({left: "0px"}, {queue:false});
		} else {
			if(cur_l == "0px") {
				$("#content").animate({left: "560px"}, {queue:false});	
			}
		}

    });
	
	$('#bafform').submit(function(){
		$('#bafresults').show().text("Processing...");
		
		inpemailaddress=$('.bafemail').val();
		
		if(CheckEmail(inpemailaddress))
		{
			$.post("media/php/bafform.php",{emailadd:inpemailaddress,innname:'The Pelican Inn'},function(data){
				$('#bafresults').show().text(data);			  
			});
			$('.bafemail').val("");
			$('#bafresults').animate({
				top:"0px"						 
			},3000,function(){
				$(this).hide();	
			});
		}
		else
		{
			$('#bafresults').show().text("Not a valid email address.");
			$('.bafemail').val("");
			$('#bafresults').animate({
				top:"0px"						 
			},3000,function(){
				$(this).hide();	
			});
		}
		return false;							  
	});
	
	
	$('#res').submit(function(){
		if($('#res #hidtxbx').val()=="")
		{
			//Validate
			j=0;
			valCount=$('#res .req').size();
			$('#res .req').each(function(){
				elmAttr=$(this).attr('name');
				thisVal=$(this).val();
				if(ValVal(thisVal,elmAttr)){j++;}
			});
			
			if(j==valCount){
				
				emlArr=new Array();
				$('#res .text').each(function(){thisStr=$(this).val();emlArr.push(thisStr);});
				$('#res .dropdown').each(function(){thisStr=$(this).val();emlArr.push(thisStr);});
				emlArr.push($('.roomdrop').val());

				 $.ajax({
				   type: "POST",
				   url: "media/php/accomform.php",
				   data: "func="+emlArr.toString(),
				   success: function(data){
						$('#retval').AnimateMessage(data);
						$('#res .text').val("");
				   }
				 });
			}
			else
			{
				$('#retval').AnimateMessage("Some fields where not filled in correctly.");
			}
		}
		return false;						  
	});
	
	var togheight = $('#content').height();
	$('#toggle').height(togheight);

	//Cycle plugin
	$('#images') .cycle({  
		//fx:     'scrollLeft',
		easing: 'easeInOut',
		speed: 1000,
		delay:  -5000,
		timeout:  7000
	});
	
	//Cycle Thumb function
	$('#goto1').click(function() { 
		$('#images').cycle(0); 
		return false; 
	}); 
	 
	$('#goto2').click(function() {  
		$('#images').cycle(1);  
		return false;  
	}); 
	$('#goto3').click(function() { 
		$('#images').cycle(2); 
		return false; 
	}); 
	 
	$('#goto4').click(function() {  
		$('#images').cycle(3);  
		return false;  
	});
	
	$('#goto5').click(function() {  
		$('#images').cycle(4);  
		return false;  
	});
});

$(function(){
	$.fn.AnimateMessage = function(returnmessage){
		$(this).text(returnmessage).fadeIn(300,function(){
			$(this).animate({opacity:1},3000,function(){$(this).fadeOut(300);});
		});
	};		   
});


function CheckEmail(inputemail) {
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");

	if (AtPos == -1 || StopPos == -1)
	{
		return false;
	}
	else
	{
		return true;	
	}

}

function IsNumeric(strString)
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;
   strString=strString.replace(" ","");
   
   if (strString.length == 0) return false;
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

function ValVal(ImpString,elmName)
{
	strInp=ImpString.replace(" ","");
	if(elmName=="name"){if(ImpString!=""){return true;}}
	else if(elmName=="email"){if(CheckEmail(ImpString)){return true;}}
	else if(elmName=="persons"){if(IsNumeric(ImpString) && ImpString>0){return true;}}
	else if(elmName=="phonenumber"){if(IsNumeric(ImpString) && strInp.length>=11){return true;}}
}