//globals
var pppg;
var ERRORCOLOR="#9ACCCD"; 
var SENTCOLOR="lightblue";
var blocked=0;

$(function() {
	
var siteroot = "http://www.videoandvision.com/";	

       var sPath = window.location.pathname;var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);if (sPage=="") sPage="index.php";
       
      $.historyInit(pageload,sPage);


$('#curveTop').onImagesLoaded(function(_this){$(_this).fadeIn("slow");});	
$('#logo').onImagesLoaded(function(_this){$(_this).fadeIn("slow");});
$('#fader').onImagesLoaded(function(_this){$(_this).fadeIn("slow");$('#fader').cycle({fx: 'turnUp'});	});

for(var ii = 1; ii <= 12; ii++) {eval("$('.navi" + ii + "').mouseout(function(){$(this).attr('src', siteroot + 'images/buttons/buttons_" + ii + ".png');});");}
for(var ii = 1; ii <= 12; ii++) {eval("$('.navi" + ii + "').mouseover(function(){$(this).attr('src', siteroot + 'images/buttons/buttons-glow_" + ii + ".png');});");}

       	$("a[rel='history']").click(function(){ 
			if(!blocked)
			{
				blocked=1;
			var hash = this.href;hash = hash.replace(/^.*#/, '');
			$.historyLoad(hash);return false;
			}
		});


		function pageload(hash) {
		$('#loadProgress').html('<img src="images/loader.gif">');
		 $('#waveHolder').hide(0,loadCont());

			
			function loadCont(){if(hash) {
					if($.browser.msie) {hash = encodeURIComponent(hash);}
                                   pppg=hash;
					$("#waveHolder").load(hash + ".php #waveContent",finishUp);
				} else {
					// start page
                                   pppg=sPage;
					$("#waveHolder").load(sPage + " #waveContent",finishUp);
				}
			}
			

			function finishUp(){
		          $('#waveHolder').fadeIn("slow");
			   $('#loadProgress img').fadeOut('slow',removeProg);
                        if (pppg=="video_contact") loadContactJS();
                        blocked=0;                    
			}

			function removeProg(){
				$('#loadProgress').html('');
			}
			
		}




$('#loadProgress img').fadeOut('fast');



function loadContactJS()
{

	$('#captcha_form').ajaxForm({						
					target: 		'#sentError',
					beforeSubmit: 	validateForm,  
					success:  checkSent
			});
hideErrors();
			
			 $("input").click(function () { 
      $(this).css("background-color","white");
	  });
	  
	  

	  
	 			 $("textarea").click(function () { 
      $(this).css("background-color","white");
    });
	
			 $("#changeCaptcha").click(function () { 
 
          $('#captcha').attr('src','/contactForm/captcha.php?'+Math.random());
return false;
     });

function checkSent()
		{
		var retVal =$("#sentError").text();	
		
		 if (retVal.length<=1) 
		 { 
			$("#userpass").animate( {backgroundColor:ERRORCOLOR}, 1000);
			errInForm("Incorrect image text");	
		 }
		 
		 if (retVal.length>1)
		 {
			$("#sentError").empty().append("<i>MESSAGE SENT</i><br/>Thank you for mailing us!<br/>  A representative will  be in touch with you shortly.");;
			$("#sentError").css("background-color","#336799");
			$("#sentError").slideDown("slow");
			$("#sentError").animate( {backgroundColor:SENTCOLOR}, 400).animate( {backgroundColor:'#336799'}, 1000);;
			clearForm();
		 }
		
		}
		
		function errInForm(sss){
		 $("#sentError").empty();
		 $("#sentError").css("background-color","white");
		 $("#sentError").append(sss).show();
		 $("#sentError").animate( {backgroundColor:ERRORCOLOR}, 600);
		}
		
		function validateForm(){
		
			hideErrors();
			
			var fname = $("#fname").val();
			var msg = $("#message").val();
            var ema = $("#email").val();
			var emaX = isValidEmail(ema);
			
			if (fname == null || fname == '') {
				errInForm("Your name is required");
				$("#fname").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;
			}


			
			if (ema == null || ema == '') {
				errInForm("Email is required");
				$("#email").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;
			}
			
			if (emaX == false) {
				errInForm("Invalid email address");
				$("#email").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;;
			}

			if (msg == null || msg == '') {
				errInForm("Messsage is required");
				$("#message").animate( {backgroundColor:ERRORCOLOR}, 1000);
				return false;
			}
			
			return true;	
		   
		}		
		
		function hideErrors()  
		{
		 //hide error containers
		 $("#sentError").empty().hide();

         //return background colors
         $("#fname").css("background-color","white");
         $("#lname").css("background-color","white");
         $("#email").css("background-color","white");
		 $("#message").css("background-color","white");
		 $("#userpass").css("background-color","white");
		}
		
		function clearForm()
		{
			$("#fname").val("");
			$("#email").val("");
			$("#phone").val("");
			$("#city").val("");
			$("#state").val("");
		       $("#message").val("");
			$("#userpass").val("");	
		}
		

		function isValidEmail(strEmail){
           validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
           strEmail = document.forms[0].email.value;
           if (strEmail.search(validRegExp) == -1) 
           {return false;} 
           return true; 
       }


}


 });








