var xmlhttp;

function SendData(form) {
	$('#FormSubmitted').html('<img src="images/mailer_loader.gif"/>');
	xmlhttp=GetXmlHttpObject();

	if (xmlhttp==null) {
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	if(form == 'phone'){
		var url="includes/phone_mailer.php?phone="+$('#PN').val()+"&name="+$('#FN').val();
	}
	else if(form == 'housing'){
		var fName = $('#fName').val();
		var lName = $('#lName').val();
		var email = $('#email').val();
		var dPhone = $('#dPhone').val();
		var ePhone = $('#ePhone').val();
		var cPhone = $('#cPhone').val();
		var address = $('#address').val();
		var city = $('#city').val();
		var state = $('#state').val();
		var zip = $('#zip').val();
		var company = $('#company').val();
		var moveIn = $('#moveIn').val();
		var moveOut = $('#moveOut').val();
		var zlocal = $('#zlocal').val();  
		var preferences = $('#preference').val();
		var rooms = $('#rooms').val();
		var payment = $('#payment').val();
		var bedrooms = $('#bedrooms').val();
		var bathrooms = $('#bathrooms').val();
		var people = $('#people').val();
		var price = $('#price').val();
		var amenities = $('#amenities').val();
		var comments = $('#comments').val();
		var url="includes/housing_mailer.php?fName="+fName+"&lName="+lName+"&email="+email+"&dPhone="+dPhone+"&ePhone="+ePhone+"&cPhone="+cPhone+"&address="+address+"&city="+city+"&state="+state+"&zip="+zip+"&company="+company+"&moveIn="+moveIn+"&moveOut="+moveOut+"&locations="+zlocal+"&preference="+preferences+"&rooms="+rooms+"&payment="+payment+"&bedrooms="+bedrooms+"&bathrooms="+bathrooms+"&people="+people+"&price="+price+"&amenities="+amenities+"&comments="+comments;
	}
	else if(form == 'maintenance'){
		var fName = $('#fName').val();
		var lName = $('#lName').val();
		var email = $('#email').val();
		var dPhone = $('#dPhone').val();
		var ePhone = $('#ePhone').val();
		var cPhone = $('#cPhone').val();
		var address = $('#address').val();
		var uNumber = $('#uNumber').val();
		var city = $('#city').val();
		var state = $('#state').val();
		var zip = $('#zip').val();
		var issue = $('#issue').val();
		var url="includes/maintenance_mailer.php?fName="+fName+"&lName="+lName+"&email="+email+"&dPhone="+dPhone+"&ePhone="+ePhone+"&cPhone="+cPhone+"&address="+address+"&uNumber="+uNumber+"&city="+city+"&state="+state+"&zip="+zip+"&issue="+issue;
	}
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function stateChanged() {
	if (xmlhttp.readyState==4) {
		 $('#PhoneSubmitted').html('Your Name/Phone have been sent.');
		 
		 $('#FormSubmitted').html('Your Housing Request has been sent.');
	}
}

function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
	 	 // code for IE7+, Firefox, Chrome, Opera, Safari
	 	 return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
	 	 // code for IE6, IE5
	  	return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}



function Overlays(val){
		
		switch(val){
			case 'conditions':
				OverlayPopulate('conditions.html','DMC Management Services, LLC Web Site Use Agreement','600','500',false);
				break;
			case 'privacy':
				OverlayPopulate('privacy.html','Privacy Statement','600','500',false);
				break;
			case 'news':
				OverlayPopulate('news.html','DMC News','600','500',false);
				break;
			case 'furniture':
				OverlayPopulate('furniture_housewares.html','Our Furniture & Housewares','600','500',false);
				break;
			case 'brochure':
				OverlayPopulate('eBrochure.html','eBrochure','520','350',true);
				break;
			case 'maintenance':
				OverlayPopulate('maintenance_request.html','Place a Maintenance Request','440','480',true);
				break;
			case 'housing':
				OverlayPopulate('housing_request.html','Place a Housing Request','480','480',false);
				break;
			case 'weather':
				OverlayPopulate('http://www.weather.com/weather/today/'+$('#LW').val(),'Local Weather','980','1900',false);
				break;
			case 'directions':
				OverlayPopulate('http://maps.google.com/maps?f=d&saddr='+$('#SA').val()+'&daddr='+$('#EA').val(),'Driving Directions','950','5800',false);
				break;
			case 'gsa':
				OverlayPopulate('http://www.gsaelibrary.gsa.gov/ElibMain/contractorInfo.do?contractNumber=GS-33F-0004V&contractorName=DMC+MANAGEMENT+SERVICES%2C+LLC&executeQuery=YES','GSA Schedule 48 Vendor','950','400',false);
				break;
			case 'federal':
				//OverlayPopulate('http://www.gsa.gov/Portal/gsa/ep/contentView.do?contentType=GSA_BASIC&contentId=17943','Federal Travel Per Diem Rates','920','1000',false,true);
				OverlayPopulate('http://www.gsa.gov/portal/category/21287','Federal Travel Per Diem Rates','1100','1000',false,true);
				break;
			default:
				 break;
		}
	
		
	}
	
	

	function OverlayPopulate(content,title,width,height,hidden,scrollFrame){
		scrollTo(0,0);
		$('#Overlay').css('display','block');
		$('#Overlay').animate({
		    opacity: 0.70
		  }, 500, function() {
		 });
		
		if(scrollFrame){
			$('#OverlayScroll').html('<iframe id="OverlayContent" src="'+content+'" scrolling="yes" frameborder="0" style="display: none; background-color: #ffffff; height: 1500px"></iframe>');	
		}else{
			$('#OverlayScroll').html('<iframe id="OverlayContent" src="'+content+'" scrolling="no" frameborder="0" style="display: none; background-color: #ffffff; height: 1500px"></iframe>');	
		}
		
		
		$('.OverlayPageTitle').html(title);
		
		$('#OverlayScroll').css('overflow','auto');
		$('#OverlayScroll').css('overflowY','auto');
		$('#OverlayScroll').css('overflowX','hidden');
		
		$('#OverlayScroll').width(width+'px');
		$('#OverlayContainer').width(width+'px');
		$('.OverlayDivider').width(width+'px');
		$('#OverlayContent').width((width-10)+'px');
		
		if(height){
			$('#OverlayScroll').height(height+'px');
			$('#OverlayContainer').height(height+'px');
			$('#OverlayContent').height(height+'px');
		}
		if(hidden){ 
			$('#OverlayContainer').height(height+'px');
			$('#OverlayScroll').height(height+'px');
			$('#OverlayContent').width(width+'px');
			$('#OverlayScroll').css('overflow','hidden');
			$('#OverlayScroll').css('overflowY','hidden');
			$('#OverlayScroll').css('overflowX','hidden');
		}
	
		
		var CenterOverlay = (document.body.offsetWidth/2) - ($('#OverlayContainer').width()/2);
		//var MiddleOverlay = (document.body.offsetHeight/2) - ($('#OverlayContainer').height()/2);
		var MiddleOverlay = 20;
		$('#VideoTour').css('display','none')
		$('#VideoTourContent').css('display','none');
		$('#OverlayContainer').css('left',CenterOverlay + 'px');
		$('#OverlayContainer').css('top',MiddleOverlay + 'px');
		$('#OverlayContainer').fadeIn(100);
		$('#OverlayContent').fadeIn(1200); 
		
		
		$("#OverlayContent").load(function () {
			$('#OverlayContent').height($("#OverlayContent").contents().find('body').height());
		});
	}
	
	


$(document).ready(function() {
	
	var RoundedCorners = '<div class="TopLeft"></div><div class="TopRight"></div><div class="BottomLeft"></div><div class="BottomRight"></div>';
	
	var NavigationHeaderBackground = '<div class="LeftSide"></div><div class="Middle"></div><div class="RightSide"></div>';
	
	$('.RoundedCorners').each(function(i) {
		$(this).html($(this).html() + RoundedCorners);
	});
	$('.NavigationBoxHeader').each(function(i) {
		var NavigationBoxTitle = '<div class="Title">'+$(this).html()+'</div>'
		$(this).html(NavigationBoxTitle + NavigationHeaderBackground);
	});
	$('.IntroImage').each(function(i) {
		$(this).wrap('<div class="Image"><div class="ImageBorderA"><div class="ImageBorderB"></div></div></div>');	
	});
	
	$('#Overlay').height($('#MainContainer').height() + $('#Footer').height());
	
	$('.OverlayClose').click(function(){
		
		$('#OverlayContent').fadeOut(100); 
		$('#OverlayContainer').fadeOut(500);
		$('#CallMe').fadeOut(500);
		$('#LocalWeather').fadeOut(500);
		$('#PhoneSubmitted').html('');
		$('#DrivingDirections').fadeOut(500);
		$('#Overlay').animate({
		    opacity: 0
		  }, 500, function() {
		 });
		$('#Overlay').fadeOut(500); 
		$('#VideoTour').css('display','block')
		$('#VideoTourContent').css('display','block');
		$('#OverlayContent').attr('src','');
	});
	
	 $('#LocalWeatherButton').click(function(){
	   	$('#LocalWeather').fadeIn(500);
		$('#DrivingDirections').fadeOut(500);
		$('#CallMe').fadeOut(500);
		return false;
    	});
	 $('#WButton').click(function(){
		Overlays('weather'); 
		$('#LocalWeather').fadeOut(500);
	 });
	  $('#DrivingDirectionsButton').click(function(){
	   	$('#DrivingDirections').fadeIn(500);
		$('#LocalWeather').fadeOut(500);
		$('#CallMe').fadeOut(500);
		return false;
   	 });
	  $('#DDButton').click(function(){
		Overlays('directions'); 
		$('#DrivingDirections').fadeOut(500);
	 });
	  
	  $('#CallMeButton').click(function(){
	   	$('#CallMe').fadeIn(500);
		$('#LocalWeather').fadeOut(500);
		$('#DrivingDirections').fadeOut(500);
		return false;
   	 });
	  $('#PButton').click(function(){
		$('#PhoneSubmitted').html('');
		validateForm('call')
		return false;
	 });
	
});



	
	
	
$(window).resize(function(){
	var CenterOverlay = (document.body.offsetWidth/2) - ($('#OverlayContainer').width()/2);
	var MiddleOverlay = (document.body.offsetHeight/2) - ($('#OverlayContainer').height()/2);
	$('#OverlayContainer').css('left',CenterOverlay + 'px');
	$('#OverlayContainer').css('top',MiddleOverlay + 'px');
});


function validateForm(form){
	var Errors = 0;
	$(".required").each(function (i) {
		if(!isRequiredFE(this.value)){
				this.style.border = '1px solid #ff0000';
				Errors++;
			}else{
				this.style.border = '1px solid #ccc';	
			}
	
	 });
	$(".requiredEmail").each(function (i) {
			if(!isValidEmailFE(this.value) || !isRequiredFE(this.value)){
				this.style.border = '1px solid #ff0000';
				Errors++;
			}else{
				this.style.border = '1px solid #ccc';	
			}
	
	 });
	$(".requiredPhone").each(function (i) {
			if(!isValidUSPhoneFE(this.value) || !isRequiredFE(this.value)){
					this.style.border = '1px solid #ff0000';
					Errors++;
			}else{
				this.style.border = '1px solid #ccc';	
			}
	
	 });
	if(Errors == 0){
		  if(form == 'housing'){
			SendData('housing');
			$('#Errors').html('');
		  }else if(form == 'maintenance'){
			SendData('maintenance');
			$('#Errors').html('');
		  }else if(form == 'call'){
			  SendData('phone');
			  $('#Errors').html('');
		  }
	}else{
		if($('#Errors').length) {
			$('#Errors').html('Please fill in required fields.');
		}
	}
}


function isRequiredFE(val){
	if(val == null || val == ''){
		return false;
	}else{
		return true;
	}
}

function isValidEmailFE(val){
	var validEmail = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (validEmail.test(val)) {
		return true;
	}
}

function isValidUSPhoneFE(val){
	var validUSPhone = /^\(?\d{3}\)?[-|\s|.]\d{3}[-|.]\d{4}$/;
	if (validUSPhone.test(val)) {
		return true;
	}
}
