// JavaScript Document

;
at = '@';
function compose(who, client1, client2, color){
		if(color == undefined){
			document.write('<a href="mailto:' + who + at + client1 + '.' + client2 + '">' + who+ ' at ' + client1 + '.' + client2 + '</a>');
		}else{
			document.write('<a style="color: ' + color + ';" href="mailto:' + who + at + client1 + '.' + client2 + '">' + who+ ' at ' + client1 + '.' + client2 + '</a>');
		}
	}
	
	function getXMLHttpRequest(){
		try{
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
		}catch (e){
	  		// Internet Explorer
			try{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}catch (e){
				try{
					xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				}catch (e){
					alert("Your Browser does not support the technology necessary to retrieve our music schedule.");
					return xmlHttp;
				}
			}
		}
		
		return xmlHttp;
	}
	
	function send(){
		
		var name  = document.getElementsByName('name')[0].value;
		var subject = document.getElementsByName('subject')[0].value;
		var from = document.getElementsByName('from')[0].value;
		var message = document.getElementsByName('message')[0].value;
		
		if(checkEmail(from)){
			
			request = getXMLHttpRequest();
			request.onreadystatechange=function(){
				if(request.readyState == 4){
					if(xmlHttp.responseText == "result=success"){
						alert("Your email address has succesfully been submitted. Lesley will get back to you as soon as possible.");
					}else{
						alert("We were unable to process your request at this time. Please try again later. If the problem persists please contact the site administrator.");
					}
				}
				}
		
			request.open("GET","http://greyhoundplayground.com/mail/mailer.htm?from=" + from + "&name=" + name + "&subject=" + subject + "&message=" + message);
			request.send(null);
		}
  	}
	
	function sendReservationRequest(){
		
		var name  = document.getElementsByName('name')[0].value;
		var subject = "Reservation  from GreyhoundPlayground.com";
		var email = document.getElementsByName('from')[0].value;
		var phone = document.getElementsByName('phone')[0].value;
		var street = document.getElementsByName('street')[0].value;
		var city = document.getElementsByName('city')[0].value;
		var state = document.getElementsByName('state')[0].value;
		var zip = document.getElementsByName('zip')[0].value;
		var numGreys = document.getElementsByName('numGreys')[0].value;
		var greyNames = document.getElementsByName('greyNames')[0].value;
		var numOthers = document.getElementsByName('numOthers')[0].value;
		var otherNames = document.getElementsByName('otherNames')[0].value;
		var checkIn = document.getElementsByName('checkIn')[0].value;
		var checkOut = document.getElementsByName('checkOut')[0].value;
		var pickup = null;
		var agree = null;
		if(document.getElementsByName('pickup')[0].checked){
			pickup = "Yes";
		}else{
			pickup = "No";
		}
		
		if(document.getElementsByName('agree')[0].checked){
			agree = "Yes";
		}else{
			agree = "No";
		}
		var message = document.getElementsByName('message')[0].value;
		
		
		if(checkEmail(email)){
			
			request = getXMLHttpRequest();
			request.onreadystatechange=function(){
				if(request.readyState == 4){
					if(xmlHttp.responseText == "result=success"){
						alert("Your message has succesfully been submitted. Lesley will get back to you as soon as possible.");
					}else{
						alert("We were unable to process your request at this time. Please try again later. If the problem persists please contact the site administrator.");
					}
				}
			}
			
			/*alert("http://localhost/greyhoundPlayground/mailer.htm?name=" + name + 
			   "&subject=" + subject + "&from=" +
			  email + "&phone=" + phone + "&street=" + street + "&city=" + city +
			  "&state=" + state + "&zip=" + zip + "&numGreys=" + numGreys +
			  "&greyNames=" + greyNames + "&numOthers=" + numOthers + "&otherNames" +
			  otherNames + "&checkIn=" + checkIn + "&checkOut=" + checkOut + "&pickup=" + pickup +
			  "&message=" + message, true);*/
		
			request.open("POST","http://greyhoundplayground.com/mail/mailer.htm?resrevation=TRUE&name="+ name + 
			   "&subject=" + subject + "&from=" +
			  email + "&phone=" + phone + "&street=" + street + "&city=" + city +
			  "&state=" + state + "&zip=" + zip + "&numGreys=" + numGreys +
			  "&greyNames=" + greyNames + "&numOthers=" + numOthers + "&otherNames=" +
			  otherNames + "&checkIn=" + checkIn + "&checkOut=" + checkOut + "&pickup=" + pickup +
			  "&agree=" + agree + "&message=" + message, true);
			request.send(null);
		}
  	}
	
	function checkEmail (strng) {
		var error="";
		var emailFilter=/^.+@.+\..{2,3}$/;
		
		if (strng == "") {
		error = "You didn't enter an email address.\n";
		}
	
		if (!(emailFilter.test(strng))) { 
			error = "Please enter a valid email address.\n";
		}else {
			//test email for illegal characters
			var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
			if (strng.match(illegalChars)) {
				error = "The email address contains illegal characters.\n";
			}
		}
		
		if(error != ""){
			alert(error);
			return false;
		}
		
		return true;    
	}
	
function validateDate(theDate, field){
	var datePat = /^(\d{1,2})(\/|-|.)(\d{1,2})(\/|-|.)(\d{2,4})$/; 
	var matchArray = theDate.match(datePat); 
	if (matchArray == null) { 
		field.value = "";
		field.focus();
		return false; 
	} 
	month = matchArray[1]; 
	day = matchArray[3]; 
	year = matchArray[5]; 
	if (month < 1 || month > 12) { 
		alert("Month must be between 1 and 12."); 
		return false; 
	} 
	if (day < 1 || day > 31) { 
		alert("Day must be between 1 and 31."); 
		return false; 
	} 
	if ((month==4 || month==6 || month==9 || month==11) && day==31) { 
		alert("Month "+month+" doesn't have 31 days!") 
		return false; 
	} 
	if (month == 2) { 
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
		if (day > 29 || (day==29 && !isleap)) { 
			alert("February " + year + " doesn't have " + day + " days!"); 
			return false; 
		} 
	} 
							 
	setCalendarControlDate(year, month, day);
	return true; 
} 

function setDates(){
		startDate = nextDay(new Date());
		endDate = nextDay(new Date(startDate));
		document.getElementById("checkIn").value = startDate;
		document.getElementById("checkOut").value = endDate;
}

function validateAndSetEnd(){
	var checkIn = document.getElementById('checkIn');
	var checkOut = document.getElementById('checkOut');
	var valid = validateDate(checkIn.value);
	if(!valid){
		checkIn.value = nextDay(new Date());
		invalidField = checkIn;
	}else{
		var out = new Date(checkOut.value);
		if(new Date(checkIn.value) > out){
			checkOut.value = nextDay(new Date(checkIn.value));
			alert(nextDay(checkIn.value));
		}
	}
}
	

function isLastDayOfMonth(month, day, year){
	 var thirtyDays = "04 06 09 11";
	 var thirtyOneDays = "1  3 6 8 10 12";
	 var result = false;
	 if(thirtyDays.indexOf(month) != -1 && day == 30){
		 result = true;
	 }else if(thirtyOneDays.indexOf(month != -1) && day == 31){
		 result = true;
	 }else if(month == "02"){
		 var isLeap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)); 
		 if(isLeap && day == 29){
			 result = true;
		 }else if(day == 28 && !isLeap){
			 result = true;
		 }
	 }
	 return result;
}

function nextDay(date){
	var month = date.getMonth() + 1;
	var year = date.getFullYear();
	var day = date.getDate();
	if(isLastDayOfMonth(month, day, year)){
		day = "01";
		if(month == 12){
			month = 1;
			year = year + 1;
		}else{
			month = month + 1;
		}
	}else{
		day = day + 1;
	}
	if( month < 10){
		month = "0" + month;
	}
	
	return month + "/" + day + "/" + year;
}



/*alert("01/31/2009"+ ", " + nextDay(new Date("01/31/2009")) + "\n" +
				"02/28/2009" + ", " + nextDay(new Date("02/28/2009")) + "\n" +
				"02/31/2008" + ", " + nextDay(new Date("02/31/2008")) + "\n" +		
				"02/29/2008" + ", " + nextDay(new Date("02/29/2008")) + "\n" +	
				"02/28/2008" + ", " + nextDay(new Date("02/28/2008")) + "\n" +	
				"03/31/2009" + ", " + nextDay(new Date("03/31/2009")) + "\n" +
				"04/30/2009" + ", " + nextDay(new Date("04/30/2009")) + "\n" +
				"05/31/2009" + ", " + nextDay(new Date("05/31/2009")) + "\n" +
				"06/30/2009" + ", " + nextDay(new Date("06/30/2009")) + "\n" +
				"07/31/2009" + ", " + nextDay(new Date("07/31/2009")) + "\n" +
				"08/31/2009" + ", " + nextDay(new Date("08/31/2009")) + "\n" +
				"09/30/2009" + ", " + nextDay(new Date("09/30/2009")) + "\n" +
				"10/31/2009" + ", " + nextDay(new Date("10/31/2009")) + "\n" +
				"1/30/2009" + ", " + nextDay(new Date("11/30/2009")) + "\n" +
				"12/31/2009" + ", " + nextDay(new Date("12/31/2009")) + "\n");*/